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

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