source: xssim/src/simulator/stats/implementation/GSSimStatistics.java @ 104

Revision 104, 41.5 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator.stats.implementation;
2
3import gridsim.gssim.GssimConstants;
4import gridsim.gssim.ResourceHistoryItem;
5import gssim.schedframe.scheduling.AbstractExecutable;
6import gssim.schedframe.scheduling.ExecTaskInterface;
7
8import java.awt.Color;
9import java.awt.Paint;
10import java.io.File;
11import java.io.FileOutputStream;
12import java.io.IOException;
13import java.io.PrintStream;
14import java.text.NumberFormat;
15import java.util.ArrayList;
16import java.util.Calendar;
17import java.util.Collections;
18import java.util.Comparator;
19import java.util.Date;
20import java.util.HashMap;
21import java.util.List;
22import java.util.Map;
23import java.util.SortedMap;
24import java.util.TreeMap;
25
26import org.apache.commons.logging.Log;
27import org.apache.commons.logging.LogFactory;
28import org.jfree.chart.ChartFactory;
29import org.jfree.chart.ChartRenderingInfo;
30import org.jfree.chart.ChartUtilities;
31import org.jfree.chart.JFreeChart;
32import org.jfree.chart.axis.AxisLocation;
33import org.jfree.chart.axis.DateAxis;
34import org.jfree.chart.axis.NumberAxis;
35import org.jfree.chart.encoders.ImageFormat;
36import org.jfree.chart.labels.CategoryItemLabelGenerator;
37import org.jfree.chart.labels.ItemLabelAnchor;
38import org.jfree.chart.labels.ItemLabelPosition;
39import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
40import org.jfree.chart.plot.CategoryPlot;
41import org.jfree.chart.plot.CombinedDomainXYPlot;
42import org.jfree.chart.plot.PlotOrientation;
43import org.jfree.chart.plot.XYPlot;
44import org.jfree.chart.renderer.category.CategoryItemRenderer;
45import org.jfree.chart.renderer.category.GanttRenderer;
46import org.jfree.chart.renderer.xy.XYStepAreaRenderer;
47import org.jfree.chart.title.TextTitle;
48import org.jfree.chart.title.Title;
49import org.jfree.data.gantt.TaskSeries;
50import org.jfree.data.gantt.TaskSeriesCollection;
51import org.jfree.data.time.FixedMillisecond;
52import org.jfree.data.xy.XYDataset;
53import org.jfree.data.xy.XYSeries;
54import org.jfree.data.xy.XYSeriesCollection;
55import org.jfree.ui.TextAnchor;
56import org.joda.time.DateTime;
57import org.joda.time.DateTimeUtilsExt;
58
59import schedframe.resources.profile.PowerUsage;
60import schedframe.resources.units.ResourceUnit;
61import schedframe.scheduling.Job;
62import schedframe.scheduling.JobInterface;
63import schedframe.scheduling.utils.ResourceParameterName;
64import simulator.ConfigurationOptions;
65import simulator.GenericUser;
66import simulator.GridSchedulingSimulator;
67import simulator.stats.SimulationStatistics;
68import simulator.stats.implementation.out.AbstractStringSerializer;
69import simulator.stats.implementation.out.FormatedStringSerializer;
70import simulator.stats.implementation.out.StringSerializer;
71import test.rewolucja.energy.extension.EnergyExtension;
72import test.rewolucja.extensions.Extension;
73import test.rewolucja.extensions.ExtensionList;
74import test.rewolucja.extensions.ExtensionType;
75import test.rewolucja.resources.ProcessingElements;
76import test.rewolucja.resources.ResourceType;
77import test.rewolucja.resources.exception.ResourceException;
78import test.rewolucja.resources.physical.base.ComputingResource;
79import test.rewolucja.resources.utils.ResourceController;
80import test.rewolucja.scheduling.JobRegistry;
81import csiro.mit.utils.jfreechart.timetablechart.TimetableChartFactory;
82import csiro.mit.utils.jfreechart.timetablechart.data.Timetable;
83import csiro.mit.utils.jfreechart.timetablechart.data.TimetableEventGroup;
84import csiro.mit.utils.jfreechart.timetablechart.data.TimetableEventSource;
85import csiro.mit.utils.jfreechart.timetablechart.renderer.TimetableRenderer;
86
87public class GSSimStatistics implements SimulationStatistics {
88
89        private Log log = LogFactory.getLog(GSSimStatistics.class);
90
91        protected static float ALPHA = 0.5f;
92        protected static int GANTT_WIDTH = 1200;
93
94        //protected static final int BITS = 8;
95        //protected static final int MILLI_SEC = 1000;
96
97        protected static final String RAW_TASKS_STATISTICS_OUTPUT_FILE_NAME = "Tasks.txt";
98        protected static final String JOBS_STATISTICS_OUTPUT_FILE_NAME = "Jobs.txt";
99
100        protected static final String SIMULATION_STATISTICS_OUTPUT_FILE_NAME = "Simulation.txt";
101        protected static final String ENERGYUSAGE_STATISTICS_OUTPUT_FILE_NAME = "Energy.txt";
102        protected static final String RESOURCEUSAGE_STATISTICS_OUTPUT_FILE_NAME = "ResourceUtilization.txt";
103
104        protected static final String DIAGRAMS_FILE_NAME_PREFIX = "Chart_";
105        protected static final String STATS_FILE_NAME_PREFIX = "Stats_";
106
107        protected static final String TASK_SEPARATOR = ";";
108        protected static final String JOB_SEPARATOR = ";";
109
110        protected String outputFolderName;
111
112        protected String simulationIdentifier;
113        protected ConfigurationOptions configuration;
114
115
116
117
118        protected GSSAccumulatorsStats accStats;
119       
120        private GenericUser users;
121        protected ResourceController resourceController;
122        protected boolean generateDiagrams = true;
123        protected AbstractStringSerializer serializer;
124        protected long startSimulationTime;
125        protected long endSimulationTime;
126       
127       
128        //RESOURCES
129        protected Timetable ganttDiagramPeTimetable;
130        protected Map<ResourceType, List<XYDataset>> resourceEnergyDiagrams;
131        protected Map<ResourceType, List<XYDataset>> resourceLoadDiagrams;
132        protected Map<String,List<ResStat>> basicResStats;
133        protected HashMap<String, Double> basicResLoad;
134       
135        protected Map<String, TimetableEventSource> peGanttMap;
136        protected Map<String, TimetableEventGroup> taskGanttMap;
137       
138       
139        //TASKS
140        protected int numOfdelayedTasks = 0;
141        protected int numOfNotExecutedTasks = 0;
142        protected double maxCj = 0;
143
144        protected boolean allTasksFinished;
145        protected TaskSeriesCollection ganttDiagramTaskSeriesCollection;
146        protected TaskSeriesCollection ganttDiagramWaitingTimeSeriesCollection;
147        protected HashMap<String, List<String>> task_processorsMap;
148
149       
150
151
152        JobRegistry jr;
153
154       
155        public GSSimStatistics(String simulationIdentifier, ConfigurationOptions co, GenericUser users,
156                        String outputFolderName, ResourceController resourceController) {
157                this.simulationIdentifier = simulationIdentifier;
158                this.configuration = co;
159                this.users = users;
160
161                this.outputFolderName = outputFolderName;
162
163                if (this.configuration.formatstatisticsoutput)
164                        this.serializer = new FormatedStringSerializer();
165                else
166                        this.serializer = new StringSerializer();
167
168                this.serializer.setDefaultNumberFormat(GridSchedulingSimulator.DFAULT_NUMBER_FORMAT);
169
170                this.resourceController = resourceController;
171                 jr = new JobRegistry("COMPUTING_GRID_0#BROKER");
172                init();
173        }
174
175        public void generateStatistics() {
176
177                this.startSimulationTime = DateTimeUtilsExt.getOffsetTime().getTimeInMillis();
178
179                this.endSimulationTime = DateTimeUtilsExt.currentTimeMillis();
180
181                long s = 0;
182                long e = 0;
183
184                log.info("gatherResourceStatistics");
185                s = System.currentTimeMillis();
186                gatherResourceStatistics();
187                e = System.currentTimeMillis();
188                log.info("time in sec: " + ((e - s) / 1000));
189               
190                log.info("gatherTaskStatistics");
191                s = System.currentTimeMillis();
192                gatherTaskStatistics();
193                e = System.currentTimeMillis();
194                log.info("time in sec: " + ((e - s) / 1000));
195
196                log.info("saveSimulationStatistics");
197                s = System.currentTimeMillis();
198                saveSimulationStatistics();
199                e = System.currentTimeMillis();
200                log.info("time in sec: " + ((e - s) / 1000));
201        }
202
203
204        public String getOutputFolderName() {
205                return outputFolderName;
206        }
207
208        private void init() {
209
210                task_processorsMap = new HashMap<String, List<String>>();
211                accStats = new GSSAccumulatorsStats();
212        }
213
214        public void saveSimulationStatistics() {
215                PrintStream simulationStatsFile = null;
216                if (configuration.createsimulationstatistics) {
217                        try {
218                                File file = new File(outputFolderName + STATS_FILE_NAME_PREFIX + simulationIdentifier + "_"
219                                                + SIMULATION_STATISTICS_OUTPUT_FILE_NAME);
220                                simulationStatsFile = new PrintStream(new FileOutputStream(file));
221                        } catch (IOException e) {
222                                e.printStackTrace();
223                        }
224                }
225                if (simulationStatsFile != null) {
226                        Object txt = accStats.serialize(this.serializer);
227                        simulationStatsFile.println(txt);
228                }
229
230                if (simulationStatsFile != null) {
231                        simulationStatsFile.close();
232                }
233
234        }
235
236
237
238        /************* RESOURCE STATISTICS SECTION **************/
239        private void gatherResourceStatistics() {
240               
241               
242                //TEMPORARY CONFIGURATION SECTION
243                ResourceType [] resForAnalyze = {ResourceType.COMPUTING_RESOURCE, ResourceType.COMPUTING_NODE, ResourceType.CPU};
244                HashMap<ResourceType, List<Stats>> type_stats = new HashMap<ResourceType, List<Stats>>();
245               
246                List<Stats> crStats = new ArrayList<Stats>();
247                crStats.add(Stats.textLoad);
248                crStats.add(Stats.chartLoad);
249                crStats.add(Stats.textEnergy);
250                crStats.add(Stats.chartEnergy);
251                type_stats.put(ResourceType.COMPUTING_RESOURCE, crStats);
252               
253                List<Stats> cnStats = new ArrayList<Stats>();
254                cnStats.add(Stats.textLoad);
255                //cnStats.add(Stats.chartLoad);
256                cnStats.add(Stats.textEnergy);
257                //cnStats.add(Stats.chartEnergy);
258                type_stats.put(ResourceType.COMPUTING_NODE, cnStats);
259               
260                List<Stats> cpuStats = new ArrayList<Stats>();
261                cpuStats.add(Stats.textLoad);
262                //cpuStats.add(Stats.chartLoad);
263                cpuStats.add(Stats.textEnergy);
264                //cpuStats.add(Stats.chartEnergy);
265                type_stats.put(ResourceType.CPU, cpuStats);
266               
267               
268               
269                peGanttMap = new HashMap<String, TimetableEventSource>();
270                taskGanttMap = new HashMap<String, TimetableEventGroup>();
271               
272               
273                resourceLoadDiagrams = new HashMap<ResourceType, List<XYDataset>>();
274                resourceEnergyDiagrams = new HashMap<ResourceType, List<XYDataset>>();
275               
276                ganttDiagramPeTimetable = new Timetable(new FixedMillisecond(
277                                startSimulationTime), new FixedMillisecond(endSimulationTime));
278               
279                PrintStream resourceLoadStatsFile = null;
280                try {
281                        File file = new File(outputFolderName + STATS_FILE_NAME_PREFIX
282                                        + simulationIdentifier + "_"
283                                        + RESOURCEUSAGE_STATISTICS_OUTPUT_FILE_NAME);
284                        resourceLoadStatsFile = new PrintStream(new FileOutputStream(file));
285                } catch (IOException e) {
286                        resourceLoadStatsFile = null;
287                }
288               
289                PrintStream energyStatsFile = null;
290                try {
291                        File file = new File(outputFolderName + STATS_FILE_NAME_PREFIX
292                                        + simulationIdentifier + "_"
293                                        + ENERGYUSAGE_STATISTICS_OUTPUT_FILE_NAME);
294                        energyStatsFile = new PrintStream(new FileOutputStream(file));
295                } catch (IOException e) {
296                        energyStatsFile = null;
297                }
298               
299                basicResStats = gatherPEStats(JobRegistry.getAllocationHistory());
300                peStatsPostProcessing(basicResStats);
301                basicResLoad = calculatePELoad( basicResStats);
302
303                if (configuration.creatediagrams_processors) {
304                        createPEGanttDiagram(basicResStats);
305                }
306               
307                for(ResourceType resType: resForAnalyze){
308                        List<ComputingResource> resources = null;
309                        try {
310                                resources = (List<ComputingResource>) resourceController.getResources().getDescendantsByType(resType);
311                        } catch (Exception e) {
312                                continue;
313                        }
314                        if(type_stats.containsKey(resType)){
315                                for(ComputingResource resource: resources){
316                                        ResourceUsageStats resourceUsage = null;
317                                        ResourceEnergyStats energyUsage = null;
318                                        if(type_stats.get(resType).contains(Stats.textLoad)){
319                                                resourceUsage = gatherResourceLoadStats(resource, basicResStats);
320                                                resourceUsage.setMeanUsage(calculateResourceLoad(resource, basicResLoad));
321                                                if (resourceLoadStatsFile != null) {
322                                                        Object txt = resourceUsage.serialize(serializer);
323                                                        resourceLoadStatsFile.println(txt);
324                                                }
325                                        }
326                                        if(type_stats.get(resType).contains(Stats.chartLoad)){
327                                                if (configuration.creatediagrams_resources) {
328                                                        createResourceLoadDiagram(resourceUsage);
329                                                }
330                                        }
331                                        if(type_stats.get(resType).contains(Stats.textEnergy)){
332                                                energyUsage = gatherResourceEnergyStats(resource);
333                                                energyUsage.setMeanUsage(calculateEnergyLoad(energyUsage));
334                                                energyUsage.setSumUsage(energyUsage.getMeanUsage() * (endSimulationTime-startSimulationTime)/(3600*1000));
335                                                if (energyStatsFile != null) {
336                                                        Object txt = energyUsage.serialize(serializer);
337                                                        energyStatsFile.println(txt);
338                                                }
339                                        }
340                                        if(type_stats.get(resType).contains(Stats.chartEnergy)){
341
342                                                if (configuration.creatediagrams_energyusage) {
343                                                        createResourceEnergyGanttDiagram(energyUsage);
344                                                }
345                                        }
346                                }
347                        }
348                }
349               
350                saveResourceGanttDiagrams();
351                createAccumulatedResourceSimulationStatistic();
352                if (energyStatsFile != null) {
353                        energyStatsFile.close();
354                }
355                if (resourceLoadStatsFile != null) {
356                        resourceLoadStatsFile.close();
357                }
358       
359        }
360       
361        private void peStatsPostProcessing(Map<String, List<ResStat>> basicResStats){
362                ResourceType resType = null;
363
364                for(String key: basicResStats.keySet()){
365                        List<ResStat> resStats = basicResStats.get(key);
366                        resType = resStats.get(0).getResType();
367                        break;
368                }
369                List<ComputingResource> resources = null;
370                try {
371                        resources = (List<ComputingResource>) resourceController.getResources().getDescendantsByType(resType);
372                } catch (Exception e) {
373                        return;
374                }
375                for(ComputingResource resource: resources){
376                        if(!basicResStats.containsKey(resource.getName())){
377                                basicResStats.put(resource.getName(), new ArrayList<ResStat>());
378                        }
379                }
380        }
381        private Map<String, List<ResStat>> gatherPEStats(Map<Integer, Map<String, Object>> history) {
382
383                Map<String, List<ResStat>> basicResStats = new TreeMap<String, List<ResStat>>(new MapPEIdComparator());
384               
385                for (Integer executableID : history.keySet()) {
386
387                        Map<String, Object> historyItem = (Map<String, Object>) history.get(executableID);
388                        List<ResourceHistoryItem> resHistItemList = (List<ResourceHistoryItem>) historyItem.get(GssimConstants.RESOURCES);
389                        Map<ResourceParameterName, ResourceUnit> res = resHistItemList.get(resHistItemList.size() - 1).getResourceUnits();
390                        ResourceUnit resUnit = res.get(ResourceParameterName.PROCESSINGELEMENTS);
391                        ProcessingElements pes = (ProcessingElements) resUnit ;
392                       
393                        for(ComputingResource pe: pes){
394                                String peName = pe.getName();
395                                long startDate = ((DateTime) historyItem.get(GssimConstants.START_TIME)).getMillis();
396                                long endDate = ((DateTime) historyItem.get(GssimConstants.END_TIME)).getMillis();
397                                JobRegistry jr = new JobRegistry("stats");
398                                ExecTaskInterface execTask = jr.getTaskExecutable(executableID);
399                                String uniqueTaskID = execTask.getJobId() + "_" + execTask.getId();
400                               
401                                ResStat resStat = new ResStat(peName, pe.getType(), startDate, endDate, uniqueTaskID);
402                               
403                                List<ResStat> resStats = basicResStats.get(peName);
404                                if (resStats == null) {
405                                        resStats = new ArrayList<ResStat>();
406                                        resStats.add(resStat);
407                                        basicResStats.put(peName, resStats);
408                                } else {
409                                        resStats.add(resStat);
410                                }
411
412                                List<String> peNames = task_processorsMap.get(uniqueTaskID);
413                                if (peNames == null) {
414                                        peNames = new ArrayList<String>();
415                                        peNames.add(peName);
416                                        task_processorsMap.put(uniqueTaskID, peNames);
417                                } else {
418                                        peNames.add(peName);
419                                }
420                        }
421                }
422                return basicResStats;
423        }
424       
425        private ResourceUsageStats gatherResourceLoadStats(ComputingResource resource, Map<String,List<ResStat>> basicStats) {
426                ResourceUsageStats usageStats = new ResourceUsageStats(resource.getName(), resource.getType(), "resourceLoadStats");
427                for(String resName: basicStats.keySet()){
428                        try {
429                                if(resource.getDescendantsByName(resName) != null || resource.getName().compareTo(resName) == 0){
430                                        createResourceLoadData(usageStats, basicStats.get(resName));
431                                }
432                        } catch (ResourceException e) {
433                                // TODO Auto-generated catch block
434                                e.printStackTrace();
435                        }
436                }
437                return usageStats;
438        }
439       
440        private TreeMap<Long, Integer> createResourceLoadData(ResourceUsageStats usageStats, List<ResStat> resStats) {
441
442                TreeMap<Long, Integer> ganttData = (TreeMap)usageStats.getUsage();
443                for (ResStat resStat : resStats) {
444                       
445                        long start_time = resStat.getStartDate();
446                        long end_time = resStat.getEndDate();
447                        if (end_time > start_time) {
448                                Integer end = ganttData.get(end_time);
449                                if (end == null) {
450                                        ganttData.put(end_time, 0);
451                                        Long left = getLeftKey(ganttData, end_time);
452                                        if (left != null) {
453                                                Integer leftValue = ganttData.get(left);
454                                                ganttData.put(end_time, leftValue);
455                                        }
456                                }
457
458                                Integer start = ganttData.get(start_time);
459                                if (start == null) {
460                                        ganttData.put(start_time, 0);
461                                        Long left = getLeftKey(ganttData, start_time);
462                                        if (left != null) {
463                                                Integer leftValue = ganttData.get(left);
464                                                ganttData.put(start_time, leftValue);
465                                        }
466                                }
467
468                                SortedMap<Long, Integer> sm = ganttData.subMap(start_time,
469                                                end_time);
470                                for (Long key : sm.keySet()) {
471                                        Integer keyVal = ganttData.get(key);
472                                        ganttData.put(key, keyVal + 1);
473                                }
474                        }
475                }
476                return ganttData;
477        }
478
479        private Long getLeftKey(TreeMap<Long, Integer> ganttData, Long toKey) {
480
481                SortedMap<Long, Integer> sm = ganttData.headMap(toKey);
482                if (sm.isEmpty()) {
483                        return null;
484                }
485                return sm.lastKey();
486        }
487       
488       
489        private ResourceEnergyStats gatherResourceEnergyStats(ComputingResource resource) {
490                double power = 0;
491                ResourceEnergyStats resEnergyUsage = new ResourceEnergyStats(resource.getName(), resource.getType(), "resourceEnergyStats");
492                Map<Long, Double> usage = resEnergyUsage.getEnergy();
493               
494                ExtensionList extensionList = resource.getExtensionList();
495                if(extensionList != null){
496                        for (Extension extension : extensionList) {
497                                if (extension.getType() == ExtensionType.ENERGY_EXTENSION) {
498                                        EnergyExtension ee = (EnergyExtension)extension;
499                                        List<PowerUsage> powerUsage = ee.getPowerUsageHistory();
500                                        if(powerUsage.size()==0)
501                                                break;
502                                        long lastTime = DateTimeUtilsExt.getOffsetTime().getTimeInMillis();
503                                        long endSimulationTime = DateTimeUtilsExt.currentTimeMillis();
504                                        double lastPower = 0;
505                                        powerUsage.add(new PowerUsage(endSimulationTime, ee.getPowerUsageHistory().get(ee.getPowerUsageHistory().size()-1).getValue()));
506                                        for(PowerUsage pu:powerUsage){
507                                                usage.put(pu.getTimestamp(), pu.getValue());
508                                               
509                                        ///     System.out.println(resource.getName() + ":"+new DateTime(pu.getTimestamp())+";"+pu.getValue());
510                                                power = power + (pu.getTimestamp() - lastTime) * lastPower/ (3600 * 1000);
511                                                lastPower = pu.getValue();
512                                                lastTime = pu.getTimestamp();
513                                        }
514                                }
515                        }
516                }
517                //System.out.println(power);
518                return resEnergyUsage;
519        }
520       
521        private void createResourceEnergyGanttDiagram(ResourceEnergyStats energyUsage) {
522
523                XYDataset dataset = createResourceEnergyGanttDataSet(energyUsage,
524                                startSimulationTime, endSimulationTime);
525               
526                List<XYDataset> energyDiagram = resourceEnergyDiagrams.get(energyUsage.getResourceType());
527                if(energyDiagram == null){
528                        energyDiagram = new ArrayList<XYDataset>();
529                        energyDiagram.add(dataset);
530                        resourceEnergyDiagrams.put(energyUsage.getResourceType(), energyDiagram);
531                } else {
532                        energyDiagram.add(dataset);
533                }
534
535        }
536
537        private XYDataset createResourceEnergyGanttDataSet(
538                        ResourceEnergyStats energyUsage, long start, long end) {
539
540                TreeMap<Long, Double> ganttdata = createResourceEnergyGanttData(energyUsage);
541                XYSeriesCollection dataset = new XYSeriesCollection();
542                XYSeries data = new XYSeries(energyUsage.getResourceName(), false, true);
543                //data.add(start, 0);
544                for (Long key : ganttdata.keySet()) {
545                        Double val = ganttdata.get(key);
546                        data.add(key, val);
547                }
548                data.add(end, 0);
549                dataset.addSeries(data);
550                return dataset;
551        }
552
553        private TreeMap<Long, Double> createResourceEnergyGanttData(
554                        ResourceEnergyStats energyUsage) {
555
556                Map<Long, Double> energy = energyUsage.getEnergy();
557                TreeMap<Long, Double> ganttData = new TreeMap<Long, Double>();
558                for (Long euKey : energy.keySet()) {
559
560                        ganttData.put(euKey, energy.get(euKey));
561                }
562                return ganttData;
563        }
564
565
566        private boolean saveResourceGanttDiagrams() {
567                JFreeChart resourceDiagram = null;
568                JFreeChart peDiagram = null;
569                JFreeChart resourceEnergyDiagram = null;
570                if (!generateDiagrams)
571                        return false;
572
573                String fileName = new File(outputFolderName, DIAGRAMS_FILE_NAME_PREFIX
574                                + simulationIdentifier + "_").getAbsolutePath();
575
576                String chartName = "Gantt diagram for "
577                                + GridSchedulingSimulator.SIMULATOR_NAME;
578                String simulationTime = "Simulation time";
579                Title subtitle = new TextTitle("created for \"" + simulationIdentifier
580                                + "\" at " + Calendar.getInstance().getTime().toString());
581
582               
583                if (configuration.creatediagrams_processors) {
584                        peDiagram = getPeGanttDiagram(chartName, subtitle,
585                                        simulationTime);
586                        if (!saveCategoryChart(peDiagram, fileName + "Processing_Elements",
587                                        "{0}"))
588                                return false;
589                }
590
591                if (configuration.creatediagrams_resources) {
592                        for(ResourceType resType: resourceLoadDiagrams.keySet()){
593                                resourceDiagram = getResourcesLoadDiagram(resType, chartName, subtitle,
594                                                simulationTime);
595                                if (!saveXYPlotChart(resourceDiagram, fileName + "Resources Load - "+resType))
596                                        return false;
597                        }
598
599                }
600                if (configuration.creatediagrams_energyusage) {
601                        for(ResourceType resType: resourceEnergyDiagrams.keySet()){
602                                resourceEnergyDiagram = getResourcesEnergyDiagram(resType, chartName,
603                                                subtitle, simulationTime);
604                                if (!saveXYPlotChart(resourceEnergyDiagram, fileName + "Energy - "+resType))
605                                        return false;
606                        }
607                }
608                return true;
609        }
610       
611        private JFreeChart getResourcesLoadDiagram(ResourceType resType, String chartName, Title subtitle,
612                        String simulationTime) {
613                String cpu = "CPU";
614                boolean urls = false;
615                boolean tooltip = true;
616                boolean legend = true;
617                CombinedDomainXYPlot cPlot = new CombinedDomainXYPlot();
618                DateAxis xAxis = new DateAxis(simulationTime);
619                JFreeChart chart = null;
620
621                List<XYDataset> loadDiagram = resourceLoadDiagrams.get(resType);
622                for (XYDataset dataset : loadDiagram) {
623                        JFreeChart tChart = ChartFactory.createXYStepAreaChart("", "",
624                                null, dataset, PlotOrientation.VERTICAL, legend, tooltip,
625                                urls);
626                        XYPlot tPlot = tChart.getXYPlot();
627                        NumberAxis yAxis = new NumberAxis(cpu);
628                        yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
629                        tPlot.setRangeAxis(yAxis);
630                        XYStepAreaRenderer rend = (XYStepAreaRenderer) tPlot.getRenderer();
631                        rend.setShapesVisible(false);
632                        cPlot.add(tPlot);
633                }
634                cPlot.setDomainAxis(xAxis);
635                cPlot.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
636                chart = new JFreeChart(chartName, cPlot);
637                chart.addSubtitle(subtitle);
638                return chart;
639        }
640
641        private JFreeChart getResourcesEnergyDiagram(ResourceType resType, String chartName,
642                        Title subtitle, String simulationTime) {
643                String energy = "ENERGY [W]";
644                boolean urls = false;
645                boolean tooltip = true;
646                boolean legend = true;
647                CombinedDomainXYPlot cPlot = new CombinedDomainXYPlot();
648                DateAxis xAxis = new DateAxis(simulationTime);
649
650                List<XYDataset> energyDiagram = resourceEnergyDiagrams.get(resType);
651                for (XYDataset dataset : energyDiagram) {
652
653                        JFreeChart tChart = ChartFactory.createXYStepAreaChart("", "",
654                                        null, dataset, PlotOrientation.VERTICAL, legend, tooltip,
655                                        urls);
656
657                        XYPlot tPlot = tChart.getXYPlot();
658                        NumberAxis yAxis = new NumberAxis(energy);
659                        yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
660                        tPlot.setRangeAxis(yAxis);
661                        XYStepAreaRenderer rend = (XYStepAreaRenderer) tPlot.getRenderer();
662                        rend.setShapesVisible(false);
663                        cPlot.add(tPlot);
664                }
665                cPlot.setDomainAxis(xAxis);
666                cPlot.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
667                JFreeChart chart = new JFreeChart(chartName, cPlot);
668                chart.addSubtitle(subtitle);
669                return chart;
670        }
671       
672       
673        private JFreeChart getPeGanttDiagram(String chartName, Title subtitle,
674                        String simulationTime) {
675                String processors = "Processing Elements";
676                boolean tooltip = true;
677                boolean legend = true;
678                JFreeChart chart = TimetableChartFactory.createTimetableChart(
679                                chartName, processors, simulationTime,
680                                ganttDiagramPeTimetable, legend, tooltip);
681                chart.addSubtitle(subtitle);
682                TimetableRenderer rend = (TimetableRenderer) chart.getCategoryPlot()
683                                .getRenderer();
684                rend.setBackgroundBarPaint(null);
685                return chart;
686        }
687       
688       
689        private boolean saveXYPlotChart(JFreeChart c, String fileName) {
690                c.setNotify(false);
691                c.setAntiAlias(true);
692                c.setTextAntiAlias(true);
693                c.setBorderVisible(false);
694
695                int height = 900;
696
697                CombinedDomainXYPlot cPlot = (CombinedDomainXYPlot) c.getPlot();
698                int nPlots = cPlot.getSubplots().size();
699
700                if (nPlots > 3) {
701                        height = nPlots * 300;
702                }
703                int width = GANTT_WIDTH;
704
705                if (configuration.creatediagrams_resources_scale != -1)
706                        width = (int) ((endSimulationTime - startSimulationTime) * configuration.creatediagrams_resources_scale);
707                ChartRenderingInfo info = new ChartRenderingInfo();
708                File f = new File(fileName + "." + ImageFormat.PNG);
709                final String gssimSubtitle = "Created by "
710                                + GridSchedulingSimulator.SIMULATOR_NAME
711                                + " http://www.gssim.org/";
712                c.addSubtitle(new TextTitle(gssimSubtitle));
713                boolean encodeAlpha = false;
714                int compression = 9;// values 0-9
715                try {
716                        ChartUtilities.saveChartAsPNG(f, c, width, height, info,
717                                        encodeAlpha, compression);
718                } catch (IOException e) {
719                        e.printStackTrace();
720                        return false;
721                }
722                return true;
723        }
724       
725        private void createPEGanttDiagram(Map<String,List<ResStat>> basicResStats) {
726                for(String peName: basicResStats.keySet()){
727                        TimetableEventSource pe = new TimetableEventSource(peName);
728                        ganttDiagramPeTimetable.addEventSource(pe);
729                        peGanttMap.put(peName, pe);
730                        for(ResStat resStat: basicResStats.get(peName)){
731
732                                pe = peGanttMap.get(resStat.getPeName());
733                                if(pe == null){
734                                        //pe = new TimetableEventSource(resStat.getPeName());
735                                //      ganttDiagramPeTimetable.addEventSource(pe);
736                                //      peGanttMap.put(resStat.getPeName(), pe);
737                                }
738                                TimetableEventGroup task = taskGanttMap.get(resStat.getTaskID());
739                                long startDate = resStat.getStartDate();
740                                long endDate = resStat.getEndDate();
741                                if (task == null) {
742                                        task = new TimetableEventGroup(resStat.getTaskID());
743                                        taskGanttMap.put(resStat.getTaskID(), task);
744                                        ganttDiagramPeTimetable.addEventGroup(task);
745                                }
746                                ganttDiagramPeTimetable.addEvent(pe, task,
747                                                new FixedMillisecond(startDate), new FixedMillisecond(endDate));
748                        }
749                }
750        }
751
752        private void createResourceLoadDiagram(ResourceUsageStats resStats) {
753
754                XYDataset dataset = createResourceLoadDataSet(resStats);
755
756                List<XYDataset> loadDiagram = resourceLoadDiagrams.get(resStats.getResourceType());
757                if(loadDiagram == null){
758                        loadDiagram = new ArrayList<XYDataset>();
759                        loadDiagram.add(dataset);
760                        resourceLoadDiagrams.put(resStats.getResourceType(), loadDiagram);
761                } else {
762                        loadDiagram.add(dataset);
763                }
764        }
765
766        private XYDataset createResourceLoadDataSet(ResourceUsageStats resStats) {
767
768                XYSeriesCollection dataset = new XYSeriesCollection();
769                XYSeries data = new XYSeries(resStats.resourceName, false, true);
770                Map<Long, Integer> usage = resStats.getUsage();
771               
772                for (Long key : usage.keySet()) {
773                        Integer val = usage.get(key);
774                        data.add(key, val);
775                }
776                dataset.addSeries(data);
777                return dataset;
778        }
779
780        private void createAccumulatedResourceSimulationStatistic() {
781
782                ComputingResource resource = resourceController.getResources();
783
784                for(ComputingResource child :resource.getChildren()){
785                        double load = calculateResourceLoad(child, basicResLoad);
786                        accStats.meanTotalLoad.add(load);
787                //      accStats.meanEnergyUsage.add(tempMeanEnergyUsage.getMean());
788                }
789
790        }
791
792        private HashMap<String, Double> calculatePELoad(Map<String,List<ResStat>> basicResStats){
793                HashMap<String, Double> peLoad = new HashMap<String, Double>();
794                for(String resName: basicResStats.keySet()){
795                        List<ResStat> resStats = basicResStats.get(resName);
796                        double sum = 0;
797                        for(ResStat resStat:resStats){
798                                sum += (resStat.endDate-resStat.startDate);
799                        }
800                        double load = sum/(endSimulationTime-startSimulationTime);
801                        peLoad.put(resName, load);
802                }
803                return peLoad;
804        }
805       
806        private  Double calculateResourceLoad(ComputingResource resource, HashMap<String, Double> peLoad ){
807                int peCnt = 0;
808                double sum = 0;
809                for(String resName: peLoad.keySet()){
810
811                        try {
812                                if(resource.getDescendantsByName(resName)!= null || resource.getName().compareTo(resName)==0){
813                                        Double load = peLoad.get(resName);
814                                        sum += load;
815                                        peCnt++;
816                                }
817                        } catch (ResourceException e) {
818                                // TODO Auto-generated catch block
819                                e.printStackTrace();
820                        }
821                }
822               
823                return sum/peCnt;
824        }
825       
826        private double calculateEnergyLoad(ResourceEnergyStats resEnergyStats ){
827                double meanEnergyUsage = 0;
828                long time = 0;
829                double load = 0;
830                Map<Long, Double> usage = resEnergyStats.getEnergy();
831                for (Long key : usage.keySet()) {
832                       
833                        if (time != 0) {
834                                meanEnergyUsage += (load * (key - time))
835                                                / (endSimulationTime - startSimulationTime);
836
837                                time = key;
838                        } else {
839                                time = key;
840                        }
841                        load = (double) usage.get(key);
842                }
843                return meanEnergyUsage;
844        }
845       
846        /*private void createPEGanttDiagram(String peName,
847                        String uniqueTaskID, Map<String, Object> historyItem) {
848                TimetableEventSource pe = peGanttMap.get(peName);
849                if(pe == null){
850                        pe = new TimetableEventSource(peName);
851                        ganttDiagramPETimetable.addEventSource(pe);
852                        peGanttMap.put(peName, pe);
853                }
854                TimetableEventGroup task = taskGanttMap.get(uniqueTaskID);
855                long startDate = ((DateTime) historyItem.get(GssimConstants.START_TIME))
856                                .getMillis();
857                long endDate = ((DateTime) historyItem.get(GssimConstants.END_TIME))
858                                .getMillis();
859                if (task == null) {
860                        task = new TimetableEventGroup(uniqueTaskID);
861                        taskGanttMap.put(uniqueTaskID, task);
862                        ganttDiagramPETimetable.addEventGroup(task);
863                }
864                ganttDiagramPETimetable.addEvent(pe, task,
865                                new FixedMillisecond(startDate), new FixedMillisecond(endDate));
866        }*/
867       
868
869
870        /************* TASK STATISTICS SECTION **************/
871        public void gatherTaskStatistics() {
872
873                List<JobInterface<?>> jobs = users.getAllReceivedJobs();
874                Collections.sort(jobs, new JobIdComparator());
875
876                ganttDiagramTaskSeriesCollection = new TaskSeriesCollection();
877                ganttDiagramWaitingTimeSeriesCollection = new TaskSeriesCollection();
878
879                PrintStream taskStatsFile = null;
880                try {
881                        File file = new File(outputFolderName + STATS_FILE_NAME_PREFIX + simulationIdentifier + "_"
882                                        + RAW_TASKS_STATISTICS_OUTPUT_FILE_NAME);
883                        taskStatsFile = new PrintStream(new FileOutputStream(file));
884                } catch (IOException e) {
885                        taskStatsFile = null;
886                }
887
888                PrintStream jobStatsFile = null;
889                if (configuration.createjobsstatistics) {
890                        try {
891                                File file = new File(outputFolderName + STATS_FILE_NAME_PREFIX + simulationIdentifier + "_"
892                                                + JOBS_STATISTICS_OUTPUT_FILE_NAME);
893                                jobStatsFile = new PrintStream(new FileOutputStream(file));
894                        } catch (IOException e) {
895                                jobStatsFile = null;
896                        }
897                }
898
899                for (int i = 0; i < jobs.size(); i++) {
900                        Job job = (Job) jobs.get(i);
901
902                        //List<TaskInterface> execList = JobRegistry.getInstance("COMPUTING_GRID_0#BROKER").getAllSubmittedTasks();
903                        List<AbstractExecutable> execList = jr.getJobExecutables(job.getId());
904                        List<TaskStats> taskStatsList = new ArrayList<TaskStats>();
905
906                        this.serializer.setFieldSeparator(TASK_SEPARATOR);
907
908                        for (int j = 0; j < execList.size(); j++) {
909
910                                AbstractExecutable task = (AbstractExecutable) execList.get(j);
911
912                                TaskStats taskStats = createTaskStats(task);
913                                if (taskStats != null && taskStatsFile != null) {
914                                        Object txt = taskStats.serialize(serializer);
915                                        taskStatsFile.println(txt);
916                                }
917                                if (taskStats != null && taskStats.getExecFinishDate() != -1) {
918                                        if (configuration.createsimulationstatistics) {
919                                                createAccumulatedTaskSimulationStatistic(taskStats);
920                                        }
921                                        allTasksFinished &= task.isFinished();
922                                        if (generateDiagrams) {
923                                                if (configuration.creatediagrams_tasks)
924                                                        createTaskDiagramData(task);
925                                                if (configuration.creatediagrams_taskswaitingtime)
926                                                        createTaskWaitingTimeDiagramData(task);
927
928                                        }
929                                        taskStatsList.add(taskStats);
930                                }
931                        }
932                        if (configuration.createjobsstatistics && taskStatsList.size() > 0) {
933
934                                this.serializer.setFieldSeparator(JOB_SEPARATOR);
935
936                                JobStats jobStats = createJobStats(taskStatsList);
937                                if (jobStatsFile != null) {
938                                        Object txt = jobStats.serialize(serializer);
939                                        jobStatsFile.println(txt);
940                                }
941                        }
942                }
943
944                if (configuration.createsimulationstatistics) {
945                        accStats.makespan.add(maxCj);
946                        accStats.delayedTasks.add(numOfdelayedTasks);
947                        accStats.failedRequests.add(users.getAllSentTasks().size() - users.getFinishedTasksCount());
948
949                }
950
951                saveTaskGanttDiagrams();
952
953                if (taskStatsFile != null) {
954                        taskStatsFile.close();
955                }
956                if (jobStatsFile != null) {
957                        jobStatsFile.close();
958                }
959        }
960
961        private TaskStats createTaskStats(AbstractExecutable task) {
962                TaskStats taskStats = new TaskStats(task, startSimulationTime);
963
964                String uniqueTaskID = taskStats.getJobID() + "_" + taskStats.getTaskID();
965
966                taskStats.setProcessorsName(task_processorsMap.get(uniqueTaskID));
967
968                return taskStats;
969        }
970
971        private JobStats createJobStats(List<TaskStats> tasksStats) {
972
973                String jobID = ((TaskStats) tasksStats.get(0)).getJobID();
974                JobStats jobStats = new JobStats(jobID);
975                double maxCj = 0;
976
977                for (int i = 0; i < tasksStats.size(); i++) {
978
979                        TaskStats task = (TaskStats) tasksStats.get(i);
980                        jobStats.meanTaskStartTime.add(task.getStartTime());
981                        jobStats.meanTaskCompletionTime.add(task.getCompletionTime());
982                        maxCj = Math.max(maxCj, task.getCompletionTime());
983                        jobStats.meanTaskExecutionTime.add(task.getExecutionTime());
984                        jobStats.meanTaskWaitingTime.add(task.getWaitingTime());
985                        jobStats.meanTaskFlowTime.add(task.getFlowTime());
986                        jobStats.meanTaskGQ_WaitingTime.add(task.getGQ_WaitingTime());
987                        jobStats.lateness.add(task.getLateness());
988                        jobStats.tardiness.add(task.getTardiness());
989                }
990                jobStats.makespan.add(maxCj);
991
992                return jobStats;
993        }
994
995        private void createAccumulatedTaskSimulationStatistic(TaskStats taskStats) {
996                accStats.meanTaskFlowTime.add(taskStats.getFlowTime());
997                accStats.meanTaskExecutionTime.add(taskStats.getExecutionTime());
998                accStats.meanTaskCompletionTime.add(taskStats.getCompletionTime());
999                accStats.meanTaskWaitingTime.add(taskStats.getWaitingTime());
1000                accStats.meanTaskStartTime.add(taskStats.getStartTime());
1001
1002                accStats.lateness.add(taskStats.getLateness());
1003                accStats.tardiness.add(taskStats.getTardiness());
1004
1005                if (taskStats.getExecFinishDate() == -1) {
1006                        numOfNotExecutedTasks++;
1007                }
1008                if (taskStats.getTardiness() > 0.0) {
1009                        numOfdelayedTasks++;
1010                }
1011
1012                maxCj = Math.max(maxCj, taskStats.getCompletionTime());
1013        }
1014
1015        private void createTaskDiagramData(AbstractExecutable task) {
1016                String uniqueTaskID = task.getJobId() + "_" + task.getId();
1017
1018                String resIDlist[] = task.getAllResourceName();
1019                String resID = resIDlist[resIDlist.length - 1];
1020
1021                long execStartTime = Double.valueOf(task.getExecStartTime()).longValue() * 1000;
1022                long execEndTime = Double.valueOf(task.getFinishTime()).longValue() * 1000;
1023
1024                TaskSeries taskRes = ganttDiagramTaskSeriesCollection.getSeries(resID);
1025                if (taskRes == null) {
1026                        taskRes = new TaskSeries(resID);
1027                        ganttDiagramTaskSeriesCollection.add(taskRes);
1028                }
1029                org.jfree.data.gantt.Task ganttTask = new org.jfree.data.gantt.Task(uniqueTaskID, new Date(execStartTime),
1030                                new Date(execEndTime));
1031                if (!task.isFinished()) {
1032                        ganttTask.setPercentComplete(1.0);
1033                }
1034                taskRes.add(ganttTask);
1035        }
1036
1037        private void createTaskWaitingTimeDiagramData(AbstractExecutable task) {
1038                String uniqueTaskID = task.getJobId() + "_" + task.getId();
1039
1040                String resIDlist[] = task.getAllResourceName();
1041                String resID = resIDlist[resIDlist.length - 1];
1042
1043                long execStartTime = Double.valueOf(task.getExecStartTime()).longValue() * 1000;
1044                long execEndTime = Double.valueOf(task.getFinishTime()).longValue() * 1000;
1045
1046                TaskSeries waitRes = ganttDiagramWaitingTimeSeriesCollection.getSeries(resID);
1047                if (waitRes == null) {
1048                        waitRes = new TaskSeries(resID);
1049                        ganttDiagramWaitingTimeSeriesCollection.add(waitRes);
1050                }
1051
1052                long sub = Double.valueOf(task.getSubmissionTime()).longValue() * 1000;
1053                org.jfree.data.gantt.Task wait_exec = new org.jfree.data.gantt.Task(uniqueTaskID, new Date(sub), new Date(
1054                                execEndTime));
1055                org.jfree.data.gantt.Task exec = new org.jfree.data.gantt.Task(uniqueTaskID, new Date(execStartTime), new Date(
1056                                execEndTime));
1057                if (!task.isFinished()) {
1058                        exec.setPercentComplete(1.0);
1059                }
1060                wait_exec.addSubtask(wait_exec);
1061                wait_exec.addSubtask(exec);
1062                waitRes.add(wait_exec);
1063        }
1064
1065        private boolean saveTaskGanttDiagrams() {
1066
1067                JFreeChart taskDiagram = null;
1068                JFreeChart waitingTimeDiagram = null;
1069
1070                if (!generateDiagrams)
1071                        return false;
1072
1073                String fileName = new File(outputFolderName, DIAGRAMS_FILE_NAME_PREFIX + simulationIdentifier + "_")
1074                                .getAbsolutePath();
1075
1076                String chartName = "Gantt diagram for " + GridSchedulingSimulator.SIMULATOR_NAME;
1077                String simulationTime = "Simulation time";
1078                Title subtitle = new TextTitle("created for \"" + simulationIdentifier + "\" at "
1079                                + Calendar.getInstance().getTime().toString());
1080
1081                if (configuration.creatediagrams_tasks) {
1082                        taskDiagram = getTaskDiagram(chartName, subtitle, simulationTime);
1083                        if (!saveCategoryChart(taskDiagram, fileName + "Tasks", null /* "{0}" */))
1084                                return false;
1085                }
1086
1087                if (configuration.creatediagrams_taskswaitingtime) {
1088                        waitingTimeDiagram = getWaitingTimeDiagram(chartName, subtitle, simulationTime);
1089                        if (!saveCategoryChart(waitingTimeDiagram, fileName + "Waiting_Time", null /* "Task {1} at {0}" */))
1090                                return false;
1091                }
1092
1093                return true;
1094        }
1095
1096        private JFreeChart getTaskDiagram(String chartName, Title subtitle, String simulationTime) {
1097                String tasks = "Tasks";
1098                boolean urls = false;
1099                boolean tooltip = true;
1100                boolean legend = true;
1101                JFreeChart chart = ChartFactory.createGanttChart(chartName, tasks, simulationTime,
1102                                ganttDiagramTaskSeriesCollection, legend, tooltip, urls);
1103                chart.addSubtitle(subtitle);
1104                GanttRenderer re = (GanttRenderer) chart.getCategoryPlot().getRenderer();
1105                re.setCompletePaint(Color.black);
1106                return chart;
1107        }
1108
1109        private JFreeChart getWaitingTimeDiagram(String chartName, Title subtitle, String simulationTime) {
1110                String tasks = "Tasks";
1111                boolean urls = false;
1112                boolean tooltip = true;
1113                boolean legend = true;
1114                JFreeChart chart = ChartFactory.createGanttChart(chartName, tasks, simulationTime,
1115                                ganttDiagramWaitingTimeSeriesCollection, legend, tooltip, urls);
1116                chart.addSubtitle(subtitle);
1117                chart.getPlot().setForegroundAlpha(ALPHA);
1118                GanttRenderer re = (GanttRenderer) chart.getCategoryPlot().getRenderer();
1119                re.setCompletePaint(Color.black);
1120                return chart;
1121        }
1122
1123        private boolean saveCategoryChart(JFreeChart c, String fileName, String labelFormat) {
1124                c.setNotify(false);
1125                c.setAntiAlias(true);
1126                c.setTextAntiAlias(true);
1127                c.setBorderVisible(false);
1128
1129                CategoryPlot categoryplot = (CategoryPlot) c.getPlot();
1130                categoryplot.setDomainGridlinesVisible(true);
1131
1132                if (labelFormat != null) {
1133                        CategoryItemRenderer rend = categoryplot.getRenderer();
1134                        rend.setBaseItemLabelsVisible(true);
1135                        ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER);
1136                        rend.setBasePositiveItemLabelPosition(position);
1137                        NumberFormat nf = NumberFormat.getInstance();
1138                        CategoryItemLabelGenerator gen = new StandardCategoryItemLabelGenerator(labelFormat, nf);
1139                        rend.setBaseItemLabelGenerator(gen);
1140                }
1141
1142                int rows = c.getCategoryPlot().getDataset().getColumnKeys().size();
1143
1144                int height = 900;
1145                if (rows > 45) {
1146                        height = rows * 20;
1147                }
1148                int width = calculateChartWidth(c);
1149                CategoryItemRenderer rend = c.getCategoryPlot().getRenderer();
1150
1151                for (int i = 0; i < c.getCategoryPlot().getDataset().getRowKeys().size(); i++) {
1152                        Paint collor = c.getCategoryPlot().getDrawingSupplier().getNextPaint();
1153                        rend.setSeriesOutlinePaint(i, collor);
1154                        rend.setSeriesPaint(i, collor);
1155                }
1156
1157                ChartRenderingInfo info = new ChartRenderingInfo();
1158                // info.setEntityCollection(null);
1159                File f = new File(fileName + "." + ImageFormat.PNG);
1160                final String gssimSubtitle = "Created by " + GridSchedulingSimulator.SIMULATOR_NAME + " http://www.gssim.org/";
1161                c.addSubtitle(new TextTitle(gssimSubtitle));
1162                boolean encodeAlpha = false;
1163                int compression = 9;
1164                try {
1165                        ChartUtilities.saveChartAsPNG(f, c, width, height, info, encodeAlpha, compression);
1166
1167                } catch (IOException e) {
1168                        e.printStackTrace();
1169                        return false;
1170                } catch (Exception e) {
1171                        if (log.isErrorEnabled())
1172                                log.error("The png file (" + fileName
1173                                                + ")\nwill not be generated (It can be too large data size problem)", e);
1174                } catch (Throwable t) {
1175                        log.error("The png file (" + fileName + ")\nwill not be generated (It can be too large data size problem)",
1176                                        t);
1177                }
1178
1179                return true;
1180        }
1181
1182        private int calculateChartWidth(JFreeChart c) {
1183
1184                int ganttWidth = GANTT_WIDTH;
1185                int rows = c.getCategoryPlot().getDataset().getColumnKeys().size();
1186
1187                if (rows > 45) {
1188                        int height = rows * 20;
1189                        ganttWidth = Math.max((int) (height * 1.5), GANTT_WIDTH);
1190                }
1191                return ganttWidth;
1192        }
1193
1194        private static class JobIdComparator implements Comparator<JobInterface<?>> {
1195
1196                public int compare(JobInterface<?> o1, JobInterface<?> o2) {
1197                        try {
1198                                Integer o1int;
1199                                Integer o2int;
1200                                try{
1201                                        o1int = Integer.parseInt(o1.getId());
1202                                        o2int = Integer.parseInt(o2.getId());
1203                                        return o1int.compareTo(o2int);
1204                                } catch(NumberFormatException e){
1205                                        return o1.getId().compareTo(o2.getId());
1206                                }
1207
1208                        } catch (NoSuchFieldException e) {
1209                                e.printStackTrace();
1210                        }
1211                        return -1;
1212                }
1213        }
1214       
1215        private static class MapPEIdComparator implements Comparator<String> {
1216
1217                public int compare(String o1, String o2)  {
1218                        Integer o1int;
1219                        Integer o2int;
1220
1221                        o1int = Integer.parseInt(o1.substring(o1.indexOf("_")+1));
1222                        o2int = Integer.parseInt(o2.substring(o2.indexOf("_")+1));
1223                        return o1int.compareTo(o2int);
1224                }
1225        }
1226
1227}
1228
1229class ResStat{
1230       
1231       
1232        public long getStartDate() {
1233                return startDate;
1234        }
1235        public void setStartDate(long startDate) {
1236                this.startDate = startDate;
1237        }
1238        public long getEndDate() {
1239                return endDate;
1240        }
1241        public void setEndDate(long endDate) {
1242                this.endDate = endDate;
1243        }
1244        public String getTaskID() {
1245                return taskID;
1246        }
1247        public void setTaskID(String taskID) {
1248                this.taskID = taskID;
1249        }
1250
1251        public ResStat( String peName, long startDate, long endDate, String taskID) {
1252                super();
1253                this.startDate = startDate;
1254                this.endDate = endDate;
1255                this.taskID = taskID;
1256                this.peName = peName;
1257        }
1258        public String getPeName() {
1259                return peName;
1260        }
1261        public void setPeName(String peName) {
1262                this.peName = peName;
1263        }
1264        public ResStat(String peName, ResourceType resType, long startDate, long endDate, String taskID) {
1265                super();
1266                this.peName = peName;
1267                this.resType = resType;
1268                this.startDate = startDate;
1269                this.endDate = endDate;
1270                this.taskID = taskID;
1271        }
1272        String peName;
1273        ResourceType resType;
1274        long startDate;
1275        long endDate;
1276        String taskID;
1277
1278        public ResourceType getResType() {
1279                return resType;
1280        }
1281        public void setResType(ResourceType resType) {
1282                this.resType = resType;
1283        }
1284}
1285
1286enum Stats{
1287        textLoad,
1288        chartLoad,
1289        textEnergy,
1290        chartEnergy;
1291}
Note: See TracBrowser for help on using the repository browser.