source: DCWoRMS/trunk/src/simulator/stats/implementation/DCWormsStatistics.java @ 821

Revision 821, 52.4 KB checked in by wojtekp, 12 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator.stats.implementation;
2
3import java.awt.Color;
4import java.awt.Paint;
5import java.io.File;
6import java.io.FileOutputStream;
7import java.io.IOException;
8import java.io.PrintStream;
9import java.text.NumberFormat;
10import java.util.ArrayList;
11import java.util.Calendar;
12import java.util.Collections;
13import java.util.Comparator;
14import java.util.Date;
15import java.util.HashMap;
16import java.util.List;
17import java.util.Map;
18import java.util.SortedMap;
19import java.util.TreeMap;
20
21import org.apache.commons.lang.ArrayUtils;
22import org.apache.commons.logging.Log;
23import org.apache.commons.logging.LogFactory;
24import org.jfree.chart.ChartFactory;
25import org.jfree.chart.ChartRenderingInfo;
26import org.jfree.chart.ChartUtilities;
27import org.jfree.chart.JFreeChart;
28import org.jfree.chart.axis.AxisLocation;
29import org.jfree.chart.axis.DateAxis;
30import org.jfree.chart.axis.NumberAxis;
31import org.jfree.chart.encoders.ImageFormat;
32import org.jfree.chart.labels.CategoryItemLabelGenerator;
33import org.jfree.chart.labels.ItemLabelAnchor;
34import org.jfree.chart.labels.ItemLabelPosition;
35import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
36import org.jfree.chart.plot.CategoryPlot;
37import org.jfree.chart.plot.CombinedDomainXYPlot;
38import org.jfree.chart.plot.PlotOrientation;
39import org.jfree.chart.plot.XYPlot;
40import org.jfree.chart.renderer.category.CategoryItemRenderer;
41import org.jfree.chart.renderer.category.GanttRenderer;
42import org.jfree.chart.renderer.xy.XYStepAreaRenderer;
43import org.jfree.chart.title.TextTitle;
44import org.jfree.chart.title.Title;
45import org.jfree.data.gantt.TaskSeries;
46import org.jfree.data.gantt.TaskSeriesCollection;
47import org.jfree.data.time.FixedMillisecond;
48import org.jfree.data.xy.XYDataset;
49import org.jfree.data.xy.XYSeries;
50import org.jfree.data.xy.XYSeriesCollection;
51import org.jfree.ui.TextAnchor;
52import org.joda.time.DateTime;
53import org.joda.time.DateTimeUtilsExt;
54
55import schedframe.ExecutablesList;
56import schedframe.ResourceController;
57import schedframe.exceptions.ResourceException;
58import schedframe.resources.ResourceType;
59import schedframe.resources.UserResourceType;
60import schedframe.resources.computing.ComputingResource;
61import schedframe.resources.computing.extensions.Extension;
62import schedframe.resources.computing.extensions.ExtensionList;
63import schedframe.resources.computing.extensions.ExtensionType;
64import schedframe.resources.computing.profiles.energy.EnergyExtension;
65import schedframe.resources.computing.profiles.energy.airthroughput.AirFlowValue;
66import schedframe.resources.computing.profiles.energy.power.PowerUsage;
67import schedframe.resources.computing.profiles.energy.thermal.TemperatureValue;
68import schedframe.resources.units.PEUnit;
69import schedframe.resources.units.ProcessingElements;
70import schedframe.resources.units.ResourceUnit;
71import schedframe.resources.units.ResourceUnitName;
72import schedframe.resources.units.StandardResourceUnitName;
73import schedframe.scheduling.ResourceHistoryItem;
74import schedframe.scheduling.Scheduler;
75import schedframe.scheduling.manager.tasks.JobRegistry;
76import schedframe.scheduling.manager.tasks.JobRegistryImpl;
77import schedframe.scheduling.tasks.Job;
78import schedframe.scheduling.tasks.JobInterface;
79import simulator.ConfigurationOptions;
80import simulator.DCWormsConstants;
81import simulator.DataCenterWorkloadSimulator;
82import simulator.GenericUser;
83import simulator.stats.GSSAccumulator;
84import simulator.stats.SimulationStatistics;
85import simulator.stats.implementation.out.AbstractStringSerializer;
86import simulator.stats.implementation.out.StringSerializer;
87import csiro.mit.utils.jfreechart.timetablechart.TimetableChartFactory;
88import csiro.mit.utils.jfreechart.timetablechart.data.Timetable;
89import csiro.mit.utils.jfreechart.timetablechart.data.TimetableEventGroup;
90import csiro.mit.utils.jfreechart.timetablechart.data.TimetableEventSource;
91import csiro.mit.utils.jfreechart.timetablechart.renderer.TimetableRenderer;
92import dcworms.schedframe.scheduling.ExecTask;
93import dcworms.schedframe.scheduling.Executable;
94import eduni.simjava.Sim_stat;
95
96public class DCWormsStatistics implements SimulationStatistics {
97
98        private Log log = LogFactory.getLog(DCWormsStatistics.class);
99
100        protected static float ALPHA = 0.5f;
101        protected static int GANTT_WIDTH = 1200;
102       
103        protected static final int MILLI_SEC = 1000;
104
105        protected static final String RAW_TASKS_STATISTICS_OUTPUT_FILE_NAME = "Tasks.txt";
106        protected static final String JOBS_STATISTICS_OUTPUT_FILE_NAME = "Jobs.txt";
107
108        protected static final String SIMULATION_STATISTICS_OUTPUT_FILE_NAME = "Simulation.txt";
109        protected static final String RESOURCEUTILIZATION_STATISTICS_OUTPUT_FILE_NAME = "ResourceUtilization.txt";
110        protected static final String ENERGYUSAGE_STATISTICS_OUTPUT_FILE_NAME = "EnergyUsage.txt";
111        protected static final String AIRFLOW_STATISTICS_OUTPUT_FILE_NAME = "AirThroughput.txt";
112        protected static final String TEMPERATURE_STATISTICS_OUTPUT_FILE_NAME = "Temperature.txt";
113
114        protected static final String DIAGRAMS_FILE_NAME_PREFIX = "Chart_";
115        protected static final String STATS_FILE_NAME_PREFIX = "Stats_";
116
117        protected static final String TASK_SEPARATOR = ";";
118        protected static final String JOB_SEPARATOR = ";";
119
120        protected String outputFolderName;
121
122        protected String simulationIdentifier;
123        protected ConfigurationOptions configuration;
124
125        protected GSSAccumulatorsStats accStats;
126        protected Map<String, GSSAccumulator> statsData;
127       
128        protected GenericUser users;
129        protected ResourceController resourceController;
130        protected boolean generateDiagrams = true;
131        protected AbstractStringSerializer serializer;
132        protected long startSimulationTime;
133        protected long endSimulationTime;
134       
135        //RESOURCES
136        protected Map<String, List<ResStat>> basicResStats;
137        protected Map<String, Double> basicResLoad;
138       
139        protected Map<String, TimetableEventSource> peGanttMap;
140        protected Map<String, TimetableEventGroup> taskGanttMap;
141       
142        protected Timetable ganttDiagramTimetable;
143        protected Map<ResourceType, List<XYDataset>> resourcePowerUsageDiagrams;
144        protected Map<ResourceType, List<XYDataset>> resourceAirFlowDiagrams;
145        protected Map<ResourceType, List<XYDataset>> resourceTemperatureDiagrams;
146        protected Map<ResourceType, List<XYDataset>> resourceLoadDiagrams;
147       
148        //TASKS
149        protected int numOfdelayedTasks = 0;
150        protected int numOfNotExecutedTasks = 0;
151        protected double maxCj = 0;
152
153        protected boolean allTasksFinished;
154        protected TaskSeriesCollection ganttDiagramTaskSeriesCollection;
155        protected TaskSeriesCollection ganttDiagramWaitingTimeSeriesCollection;
156        protected HashMap<String, List<String>> task_processorsMap;
157
158        protected JobRegistry jr;
159
160       
161        public DCWormsStatistics(String simulationIdentifier, ConfigurationOptions co, GenericUser users,
162                        String outputFolderName, ResourceController resourceController) {
163                this.simulationIdentifier = simulationIdentifier;
164                this.configuration = co;
165                this.users = users;
166
167                this.outputFolderName = outputFolderName;
168
169                this.serializer = new StringSerializer();
170                this.serializer.setDefaultNumberFormat(DataCenterWorkloadSimulator.DFAULT_NUMBER_FORMAT);
171
172                this.resourceController = resourceController;
173                this.jr = new JobRegistryImpl("");
174                init();
175        }
176
177        public void generateStatistics() {
178
179                if(users.isSimStartTimeDefined())
180                        this.startSimulationTime = DateTimeUtilsExt.getOffsetTime().getTimeInMillis();
181                else
182                        this.startSimulationTime = new DateTime(users.getSubmissionStartTime() * 1000l).getMillis();
183                this.endSimulationTime = DateTimeUtilsExt.currentTimeMillis();
184
185                long s = 0;
186                long e = 0;
187
188                log.info("gatherResourceStatistics");
189                s = System.currentTimeMillis();
190                gatherResourceStatistics();
191                e = System.currentTimeMillis();
192                log.info("time in sec: " + ((e - s) / MILLI_SEC));
193               
194                log.info("gatherTaskStatistics");
195                s = System.currentTimeMillis();
196                gatherTaskStatistics();
197                e = System.currentTimeMillis();
198                log.info("time in sec: " + ((e - s) / MILLI_SEC));
199
200                log.info("saveSimulationStatistics");
201                s = System.currentTimeMillis();
202                saveSimulationStatistics();
203                e = System.currentTimeMillis();
204                log.info("time in sec: " + ((e - s) / MILLI_SEC));
205        }
206
207
208        public String getOutputFolderName() {
209                return outputFolderName;
210        }
211
212        private void init() {
213                task_processorsMap = new HashMap<String, List<String>>();
214                accStats = new GSSAccumulatorsStats();
215                statsData = new HashMap<String, GSSAccumulator>();
216        }
217
218        public void saveSimulationStatistics() {
219                PrintStream simulationStatsFile = null;
220                if (configuration.createsimulationstatistics) {
221                        try {
222                                File file = new File(outputFolderName + STATS_FILE_NAME_PREFIX + simulationIdentifier + "_"
223                                                + SIMULATION_STATISTICS_OUTPUT_FILE_NAME);
224                                simulationStatsFile = new PrintStream(new FileOutputStream(file));
225                        } catch (IOException e) {
226                                e.printStackTrace();
227                        }
228                }
229                if (simulationStatsFile != null) {
230                        Object txt = accStats.serialize(this.serializer);
231                        simulationStatsFile.println(txt);
232                }
233
234                if (simulationStatsFile != null) {
235                        simulationStatsFile.close();
236                }
237        }
238
239        /************* RESOURCE STATISTICS SECTION **************/
240        private void gatherResourceStatistics() {
241               
242                HashMap<String, List<Stats>> type_stats = new HashMap<String, List<Stats>>();
243               
244                for(String resourceName: resourceController.getComputingResourceLayers()){
245                        List<Stats> cStats = new ArrayList<Stats>();
246                        cStats.add(Stats.textLoad);
247                        if(ArrayUtils.contains(configuration.resForUtilizationChart, resourceName))
248                                cStats.add(Stats.chartLoad);
249                        cStats.add(Stats.textEnergy);
250                        if(ArrayUtils.contains(configuration.resForEnergyChart, resourceName))
251                                cStats.add(Stats.chartEnergy);
252                        cStats.add(Stats.textAirFlow);
253                        if(ArrayUtils.contains(configuration.resForAirFlowChart, resourceName))
254                                cStats.add(Stats.chartAirFlow);
255                        cStats.add(Stats.textTemperature);
256                        if(ArrayUtils.contains(configuration.resForTemperatureChart, resourceName))
257                                cStats.add(Stats.chartTemperature);
258                        type_stats.put(resourceName, cStats);
259                }               
260               
261                peGanttMap = new HashMap<String, TimetableEventSource>();
262                taskGanttMap = new HashMap<String, TimetableEventGroup>();             
263               
264                resourceLoadDiagrams = new HashMap<ResourceType, List<XYDataset>>();
265                resourcePowerUsageDiagrams = new HashMap<ResourceType, List<XYDataset>>();
266                resourceAirFlowDiagrams = new HashMap<ResourceType, List<XYDataset>>();
267                resourceTemperatureDiagrams = new HashMap<ResourceType, List<XYDataset>>();
268               
269                ganttDiagramTimetable = new Timetable(new FixedMillisecond(
270                                startSimulationTime), new FixedMillisecond(endSimulationTime));
271               
272                PrintStream resourceLoadStatsFile = null;
273                try {
274                        File file = new File(outputFolderName + STATS_FILE_NAME_PREFIX
275                                        + simulationIdentifier + "_"
276                                        + RESOURCEUTILIZATION_STATISTICS_OUTPUT_FILE_NAME);
277                        resourceLoadStatsFile = new PrintStream(new FileOutputStream(file));
278                } catch (IOException e) {
279                        resourceLoadStatsFile = null;
280                }
281               
282                PrintStream energyStatsFile = null;
283                try {
284                        File file = new File(outputFolderName + STATS_FILE_NAME_PREFIX
285                                        + simulationIdentifier + "_"
286                                        + ENERGYUSAGE_STATISTICS_OUTPUT_FILE_NAME);
287                        energyStatsFile = new PrintStream(new FileOutputStream(file));
288                } catch (IOException e) {
289                        energyStatsFile = null;
290                }
291               
292                PrintStream airFlowStatsFile = null;
293                try {
294                        File file = new File(outputFolderName + STATS_FILE_NAME_PREFIX
295                                        + simulationIdentifier + "_"
296                                        + AIRFLOW_STATISTICS_OUTPUT_FILE_NAME);
297                        airFlowStatsFile = new PrintStream(new FileOutputStream(file));
298                } catch (IOException e) {
299                        airFlowStatsFile = null;
300                }
301               
302                PrintStream temperatureStatsFile = null;
303                try {
304                        File file = new File(outputFolderName + STATS_FILE_NAME_PREFIX
305                                        + simulationIdentifier + "_"
306                                        + TEMPERATURE_STATISTICS_OUTPUT_FILE_NAME);
307                        temperatureStatsFile = new PrintStream(new FileOutputStream(file));
308                } catch (IOException e) {
309                        temperatureStatsFile = null;
310                }
311
312                basicResStats = gatherPEStats(jr.getExecutableTasks());
313                peStatsPostProcessing(basicResStats);
314                basicResLoad = calculatePELoad( basicResStats);
315
316                if (configuration.creatediagrams_gantt) {
317                        createPEGanttDiagram(basicResStats);
318                }
319               
320                for(String resourceName: resourceController.getComputingResourceLayers()){
321                        List<ComputingResource> resources = null;
322
323                        resources = new ArrayList<ComputingResource>();
324                        for(ComputingResource compRes: resourceController.getComputingResources() ){
325                                resources.addAll(compRes.getDescendantsByType(new UserResourceType(resourceName)));
326                        }
327                        if(resourceController.getComputingResources().get(0).getType().getName().equals(resourceName))
328                                resources.addAll(resourceController.getComputingResources());
329               
330                        if(type_stats.containsKey(resourceName)){
331                                for(ComputingResource resource: resources){
332                                        ResourceUsageStats resourceUsage = null;
333                                        ResourcePowerStats energyUsage = null;
334                                        ResourceAirFlowStats airFlow = null;
335                                        ResourceTemperatureStats temperature = null;
336                                        if(type_stats.get(resourceName).contains(Stats.textLoad)){
337                                                resourceUsage = gatherResourceLoadStats(resource, basicResStats);
338                                                resourceUsage.setMeanValue(calculateMeanValue(resourceUsage));
339                                                if (resourceLoadStatsFile != null) {
340                                                        Object txt = resourceUsage.serialize(serializer);
341                                                        resourceLoadStatsFile.println(txt);
342                                                }
343                                        }
344                                        if(type_stats.get(resourceName).contains(Stats.chartLoad)){
345                                                if (configuration.creatediagrams_resutilization) {
346                                                        createResourceLoadDiagram(resourceUsage);
347                                                }
348                                        }
349                                        if(type_stats.get(resourceName).contains(Stats.textEnergy)){
350                                                energyUsage = gatherResourcePowerConsumptionStats(resource);
351                                                energyUsage.setMeanValue(calculateMeanValue(energyUsage));
352                                                energyUsage.setSumValue(energyUsage.getMeanValue() * (endSimulationTime - startSimulationTime) / (3600 * MILLI_SEC));
353                                               
354                                                EnergyExtension een = (EnergyExtension)(resource.getExtensionList().getExtension(ExtensionType.ENERGY_EXTENSION));
355                                                if(resourceController.getComputingResources().contains(resource)) {
356                                                        if( een != null && een.getPowerProfile() != null &&  een.getPowerProfile().getEnergyEstimationPlugin() != null){
357                                                                accStats.meanEnergyUsage.add(energyUsage.getSumValue());
358                                                        }
359
360                                                } else if( een != null && een.getPowerProfile() != null &&  een.getPowerProfile().getEnergyEstimationPlugin() != null){
361                                                        ComputingResource parent = resource.getParent();
362                                                        boolean top = true;
363                                                        while(parent != null){
364                                                                een = (EnergyExtension)(parent.getExtensionList().getExtension(ExtensionType.ENERGY_EXTENSION));
365                                                                if(een != null &&  een.getPowerProfile() != null &&  een.getPowerProfile().getEnergyEstimationPlugin() != null) {
366                                                                        top = false;
367                                                                        break;
368                                                                }
369                                                                parent = parent.getParent();
370                                                        }
371                                                        if(top == true){
372                                                                accStats.meanEnergyUsage.add(energyUsage.getSumValue());
373                                                        }
374                                                }
375                                                if (energyStatsFile != null) {
376                                                        Object txt = energyUsage.serialize(serializer);
377                                                        energyStatsFile.println(txt);
378                                                }
379                                        }
380                                        if(type_stats.get(resourceName).contains(Stats.chartEnergy)){
381
382                                                if (configuration.creatediagrams_respowerusage) {
383                                                        createResourceEnergyDiagramData(energyUsage);
384                                                }
385                                        }
386                                       
387                                        if(type_stats.get(resourceName).contains(Stats.textAirFlow)){
388                                                airFlow = gatherResourceAirFlowStats(resource);
389                                                airFlow.setMeanValue(calculateMeanValue(airFlow));
390                                                airFlow.setSumValue(airFlow.getMeanValue() * (endSimulationTime - startSimulationTime) / (60 * MILLI_SEC));
391                                               
392                                                EnergyExtension een = (EnergyExtension)(resource.getExtensionList().getExtension(ExtensionType.ENERGY_EXTENSION));
393                                                if(resourceController.getComputingResources().contains(resource)) {
394                                                        if( een != null /*&& een.getPp() != null*/ ){
395                                                                accStats.meanAirFlow.add(airFlow.getSumValue());
396                                                        }
397
398                                                } else if( een != null && een.getAirFlowProfile() != null ){
399                                                        ComputingResource parent = resource.getParent();
400                                                        een = (EnergyExtension)(parent.getExtensionList().getExtension(ExtensionType.ENERGY_EXTENSION));
401                                                        boolean top = true;
402                                                        while(parent != null){
403                                                                if(een != null &&  een.getAirFlowProfile() != null) {
404                                                                        top = false;
405                                                                        break;
406                                                                }
407                                                                parent = parent.getParent();
408                                                        }
409                                                        if(top == true){
410                                                                accStats.meanAirFlow.add(airFlow.getSumValue());
411                                                        }
412                                                }
413                                                if (airFlowStatsFile != null) {
414                                                        Object txt = airFlow.serialize(serializer);
415                                                        airFlowStatsFile.println(txt);
416                                                }
417                                        }
418                                        if(type_stats.get(resourceName).contains(Stats.chartAirFlow)){
419
420                                                if (configuration.creatediagrams_resairflow) {
421                                                        createResourceAirFlowDiagramData(airFlow);
422                                                }
423                                        }
424                                       
425                                        if(type_stats.get(resourceName).contains(Stats.textTemperature)){
426                                                temperature = gatherResourceTemperatureStats(resource);
427                                                temperature.setMeanValue(calculateMeanValue(temperature));
428                                                temperature.setSumValue(temperature.getMeanValue() * (endSimulationTime - startSimulationTime) / (3600 * MILLI_SEC));
429                                               
430                                                EnergyExtension een = (EnergyExtension)(resource.getExtensionList().getExtension(ExtensionType.ENERGY_EXTENSION));
431                                                if(resourceController.getComputingResources().contains(resource)) {
432                                                        if( een != null /*&& een.getPp() != null*/ ){
433                                                                accStats.meanTemperature.add(temperature.getSumValue());
434                                                        }
435
436                                                } else if( een != null && een.getThermalProfile() != null ){
437                                                        ComputingResource parent = resource.getParent();
438                                                        een = (EnergyExtension)(parent.getExtensionList().getExtension(ExtensionType.ENERGY_EXTENSION));
439                                                        boolean top = true;
440                                                        while(parent != null){
441                                                                if(een != null &&  een.getThermalProfile() != null) {
442                                                                        top = false;
443                                                                        break;
444                                                                }
445                                                                parent = parent.getParent();
446                                                        }
447                                                        if(top == true){
448                                                                accStats.meanTemperature.add(temperature.getSumValue());
449                                                        }
450                                                }
451                                                if (temperatureStatsFile != null) {
452                                                        Object txt = temperature.serialize(serializer);
453                                                        temperatureStatsFile.println(txt);
454                                                }
455                                        }
456                                        if(type_stats.get(resourceName).contains(Stats.chartTemperature)){
457
458                                                if (configuration.creatediagrams_restemperature) {
459                                                        createResourceTemperatureDiagramData(temperature);
460                                                }
461                                        }
462                                }
463                        }
464                }
465               
466                saveResourceGanttDiagrams();
467                createAccumulatedResourceSimulationStatistic();
468               
469                if (temperatureStatsFile != null) {
470                        temperatureStatsFile.close();
471                }
472                if (airFlowStatsFile != null) {
473                        airFlowStatsFile.close();
474                }
475                if (energyStatsFile != null) {
476                        energyStatsFile.close();
477                }
478                if (resourceLoadStatsFile != null) {
479                        resourceLoadStatsFile.close();
480                }
481        }
482
483       
484        private void peStatsPostProcessing(Map<String, List<ResStat>> basicResStats){
485                ResourceType resType = null;
486
487                for(String key: basicResStats.keySet()){
488                        List<ResStat> resStats = basicResStats.get(key);
489                        resType = resStats.get(0).getResType();
490                        break;
491                }
492                List<ComputingResource> resources = null;
493                try {
494                        resources = new ArrayList<ComputingResource>();
495                        for(ComputingResource compRes: resourceController.getComputingResources() ){
496                                resources.addAll(compRes.getDescendantsByType(resType));
497                        }
498                } catch (Exception e) {
499                        return;
500                }
501                for(ComputingResource resource: resources){
502                        if(!basicResStats.containsKey(resource.getName())){
503                                basicResStats.put(resource.getName(), new ArrayList<ResStat>());
504                        }
505                }
506        }
507       
508        private Map<String, List<ResStat>> gatherPEStats( ExecutablesList executables) {
509               
510                Map<String, List<ResStat>> basicResStats = new TreeMap<String, List<ResStat>>(new MapPEIdComparator());
511
512                for (ExecTask execTask:executables) {
513                        Executable exec = (Executable) execTask;
514
515                        List<ResourceHistoryItem> resHistItemList = exec.getUsedResources();
516                        if(resHistItemList.size() == 0)
517                                return basicResStats;
518                        Map<ResourceUnitName, ResourceUnit> res = resHistItemList.get(resHistItemList.size() - 1).getResourceUnits();
519                        ResourceUnit resUnit = res.get(StandardResourceUnitName.PE);
520                        //ProcessingElements pes = (ProcessingElements) resUnit ;
521                        if(resUnit instanceof ProcessingElements){
522                                ProcessingElements pes = (ProcessingElements) resUnit;
523                                for(ComputingResource pe: pes){
524                                        String peName = pe.getName();
525                                        long startDate = Double.valueOf(exec.getExecStartTime()).longValue() * MILLI_SEC;
526                                        long endDate = Double.valueOf(exec.getFinishTime()).longValue() * MILLI_SEC;
527
528                                        String uniqueTaskID = getUniqueTaskId(execTask);
529                                       
530                                        ResStat resStat = new ResStat(peName, pe.getType(), startDate, endDate, uniqueTaskID);
531                                       
532                                        List<ResStat> resStats = basicResStats.get(peName);
533                                        if (resStats == null) {
534                                                resStats = new ArrayList<ResStat>();
535                                                resStats.add(resStat);
536                                                basicResStats.put(peName, resStats);
537                                        } else {
538                                                resStats.add(resStat);
539                                        }
540
541                                        List<String> peNames = task_processorsMap.get(uniqueTaskID);
542                                        if (peNames == null) {
543                                                peNames = new ArrayList<String>();
544                                                peNames.add(peName);
545                                                task_processorsMap.put(uniqueTaskID, peNames);
546                                        } else {
547                                                peNames.add(peName);
548                                        }
549                                }
550                        } else if (resUnit instanceof PEUnit){
551                                PEUnit peUnit = (PEUnit) resUnit ;
552                        }
553
554                }
555                return basicResStats;
556        }
557       
558        //change this method to adjust the colors of gantt chart
559        private String getUniqueTaskId(ExecTask execTask){
560                return execTask.getJobId() + "_" + execTask.getId();
561        }
562       
563        private ResourceUsageStats gatherResourceLoadStats(ComputingResource resource, Map<String, List<ResStat>> basicStats) {
564                ResourceUsageStats usageStats = new ResourceUsageStats(resource.getName(), resource.getType(), "resourceLoadStats");
565                int cnt = 0;
566                for(String resName: basicStats.keySet()){
567                        try {
568                                if(resource.getDescendantByName(resName) != null || resource.getName().compareTo(resName) == 0){
569                                        createResourceLoadData(usageStats, basicStats.get(resName));
570                                        cnt++;
571                                }
572                        } catch (ResourceException e) {
573                                // TODO Auto-generated catch block
574                                e.printStackTrace();
575                        }
576                }
577                for(Long key: usageStats.getHistory().keySet()){
578                        Double value = usageStats.getHistory().get(key)/cnt;
579                        usageStats.getHistory().put(key, value);
580                }
581
582                return usageStats;
583        }
584       
585        private Map<Long, Double> createResourceLoadData(ResourceUsageStats usageStats, List<ResStat> resStats) {
586
587                TreeMap<Long, Double> ganttData = (TreeMap<Long, Double>)usageStats.getHistory();
588                for (ResStat resStat : resStats) {
589                       
590                        long start_time = resStat.getStartDate();
591                        long end_time = resStat.getEndDate();
592                        if (end_time > start_time) {
593                                Double end = ganttData.get(end_time);
594                                if (end == null) {
595                                        ganttData.put(end_time, 0.0);
596                                        Long left = getLeftKey(ganttData, end_time);
597                                        if (left != null) {
598                                                Double leftValue = ganttData.get(left);
599                                                ganttData.put(end_time, leftValue);
600                                        }
601                                }
602
603                                Double start = ganttData.get(start_time);
604                                if (start == null) {
605                                        ganttData.put(start_time, 0.0);
606                                        Long left = getLeftKey(ganttData, start_time);
607                                        if (left != null) {
608                                                Double leftValue = ganttData.get(left);
609                                                ganttData.put(start_time, leftValue);
610                                        }
611                                }
612
613                                SortedMap<Long, Double> sm = ganttData.subMap(start_time,
614                                                end_time);
615                                for (Long key : sm.keySet()) {
616                                        Double keyVal = ganttData.get(key);
617                                        ganttData.put(key, keyVal + 1);
618                                }
619                        }
620                }
621
622                return ganttData;
623        }
624
625        private Long getLeftKey(TreeMap<Long, Double> ganttData, Long toKey) {
626
627                SortedMap<Long, Double> sm = ganttData.headMap(toKey);
628                if (sm.isEmpty()) {
629                        return null;
630                }
631                return sm.lastKey();
632        }
633       
634       
635        private ResourcePowerStats gatherResourcePowerConsumptionStats(ComputingResource resource) {
636                double power = 0;
637                ResourcePowerStats resEnergyUsage = new ResourcePowerStats(resource.getName(), resource.getType(), "resourcePowerConsumptionStats");
638                Map<Long, Double> usage = resEnergyUsage.getHistory();
639               
640                ExtensionList extensionList = resource.getExtensionList();
641                if(extensionList != null){
642                        for (Extension extension : extensionList) {
643                                if (extension.getType() == ExtensionType.ENERGY_EXTENSION) {
644                                        EnergyExtension ee = (EnergyExtension)extension;
645                                        if(ee.getPowerProfile() == null)
646                                                break;
647                                        List<PowerUsage> powerUsage = ee.getPowerProfile().getPowerUsageHistory();
648                                        if(powerUsage.size() == 0)
649                                                break;
650                                        long lastTime = DateTimeUtilsExt.getOffsetTime().getTimeInMillis();
651                                        long endSimulationTime = DateTimeUtilsExt.currentTimeMillis();
652                                        double lastPower = 0;
653                                        powerUsage.add(new PowerUsage(endSimulationTime, ee.getPowerProfile().getPowerUsageHistory().get(ee.getPowerProfile().getPowerUsageHistory().size()-1).getValue()));
654                                        for(PowerUsage pu:powerUsage){
655                                                usage.put(pu.getTimestamp(), pu.getValue());
656                                               
657                                        ///     System.out.println(resource.getName() + ":"+new DateTime(pu.getTimestamp())+";"+pu.getValue());
658                                                power = power + (pu.getTimestamp() - lastTime) * lastPower/ (3600 * MILLI_SEC);
659                                                lastPower = pu.getValue();
660                                                lastTime = pu.getTimestamp();
661                                        }
662                                }
663                        }
664                }
665                //System.out.println(power);
666                return resEnergyUsage;
667        }
668       
669       
670        private ResourceAirFlowStats gatherResourceAirFlowStats(ComputingResource resource) {
671
672                ResourceAirFlowStats resAirFlow = new ResourceAirFlowStats(resource.getName(), resource.getType(), "resourceAirFlowStats");
673                Map<Long, Double> airFlow = resAirFlow.getHistory();
674               
675                ExtensionList extensionList = resource.getExtensionList();
676                if(extensionList != null){
677                        for (Extension extension : extensionList) {
678                                if (extension.getType() == ExtensionType.ENERGY_EXTENSION) {
679                                        EnergyExtension ee = (EnergyExtension)extension;
680                                        if(ee.getAirFlowProfile() == null)
681                                                break;
682                                        List<AirFlowValue> airFlowHistory = ee.getAirFlowProfile().getAirThroughputHistory();
683                                        if(airFlowHistory.size() == 0)
684                                                break;
685                                        long endSimulationTime = DateTimeUtilsExt.currentTimeMillis();
686                                        airFlowHistory.add(new AirFlowValue(endSimulationTime, ee.getAirFlowProfile().getAirThroughputHistory().get(ee.getAirFlowProfile().getAirThroughputHistory().size()-1).getValue()));
687                                        for(AirFlowValue af:airFlowHistory){
688                                                airFlow.put(af.getTimestamp(), af.getValue());
689                                        }
690                                }
691                        }
692                }
693                return resAirFlow;
694        }
695       
696        private ResourceTemperatureStats gatherResourceTemperatureStats(ComputingResource resource) {
697
698                ResourceTemperatureStats resTemperature = new ResourceTemperatureStats(resource.getName(), resource.getType(), "resourceTemperatureStats");
699                Map<Long, Double> temperature = resTemperature.getHistory();
700               
701                ExtensionList extensionList = resource.getExtensionList();
702                if(extensionList != null){
703                        for (Extension extension : extensionList) {
704                                if (extension.getType() == ExtensionType.ENERGY_EXTENSION) {
705                                        EnergyExtension ee = (EnergyExtension)extension;
706                                        if(ee.getThermalProfile() == null)
707                                                break;
708
709                                        List<TemperatureValue> temperatureHistory = ee.getThermalProfile().getTemperatureHistory();
710                                        if(temperatureHistory.size() == 0)
711                                                break;
712                                        long endSimulationTime = DateTimeUtilsExt.currentTimeMillis();
713                                        temperatureHistory.add(new TemperatureValue(endSimulationTime, ee.getThermalProfile().getTemperatureHistory().get(ee.getThermalProfile().getTemperatureHistory().size()-1).getValue()));
714                                        for(TemperatureValue af: temperatureHistory){
715                                                temperature.put(af.getTimestamp(), af.getValue());
716                                        }
717                                }
718                        }
719                }
720                return resTemperature;
721        }
722       
723        private void createResourceLoadDiagram(ResourceUsageStats resLoadStats) {
724
725                for(Long key: resLoadStats.getHistory().keySet()){
726                        Double value = resLoadStats.getHistory().get(key) * 100;
727                        resLoadStats.getHistory().put(key, value);
728                }
729
730                XYDataset dataset = createResourceChartDataSet(resLoadStats,
731                                startSimulationTime, endSimulationTime);
732
733                List<XYDataset> loadDiagram = resourceLoadDiagrams.get(resLoadStats.getResourceType());
734                if(loadDiagram == null){
735                        loadDiagram = new ArrayList<XYDataset>();
736                        loadDiagram.add(dataset);
737                        resourceLoadDiagrams.put(resLoadStats.getResourceType(), loadDiagram);
738                } else {
739                        loadDiagram.add(dataset);
740                }
741        }
742       
743        private void createResourceEnergyDiagramData(ResourceDynamicStats powerConsumptionStats) {
744
745                XYDataset dataset = createResourceChartDataSet(powerConsumptionStats,
746                                startSimulationTime, endSimulationTime);
747               
748                List<XYDataset> energyDiagramData = resourcePowerUsageDiagrams.get(powerConsumptionStats.getResourceType());
749                if(energyDiagramData == null){
750                        energyDiagramData = new ArrayList<XYDataset>();
751                        energyDiagramData.add(dataset);
752                        resourcePowerUsageDiagrams.put(powerConsumptionStats.getResourceType(), energyDiagramData);
753                } else {
754                        energyDiagramData.add(dataset);
755                }
756        }
757       
758        private void createResourceTemperatureDiagramData(ResourceDynamicStats temperatureStats) {
759               
760                XYDataset dataset = createResourceChartDataSet(temperatureStats,
761                                startSimulationTime, endSimulationTime);
762               
763                List<XYDataset> temperatureDiagramData = resourceTemperatureDiagrams.get(temperatureStats.getResourceType());
764                if(temperatureDiagramData == null){
765                        temperatureDiagramData = new ArrayList<XYDataset>();
766                        temperatureDiagramData.add(dataset);
767                        resourceTemperatureDiagrams.put(temperatureStats.getResourceType(), temperatureDiagramData);
768                } else {
769                        temperatureDiagramData.add(dataset);
770                }
771        }
772
773        private void createResourceAirFlowDiagramData(ResourceDynamicStats airFlowStats) {
774
775                XYDataset dataset = createResourceChartDataSet(airFlowStats,
776                                startSimulationTime, endSimulationTime);
777               
778                List<XYDataset> airFlowDiagramData = resourceAirFlowDiagrams.get(airFlowStats.getResourceType());
779                if(airFlowDiagramData == null){
780                        airFlowDiagramData = new ArrayList<XYDataset>();
781                        airFlowDiagramData.add(dataset);
782                        resourceAirFlowDiagrams.put(airFlowStats.getResourceType(), airFlowDiagramData);
783                } else {
784                        airFlowDiagramData.add(dataset);
785                }
786        }
787
788        private XYDataset createResourceChartDataSet(
789                        ResourceDynamicStats dynamicStats, long start, long end) {
790
791                Map<Long, Double> chartData = getResourceChartRawData(dynamicStats);
792                XYSeriesCollection dataset = new XYSeriesCollection();
793                XYSeries data = new XYSeries(dynamicStats.getResourceName(), false, true);
794                data.add(start, 0);
795                for (Long key : chartData.keySet()) {
796                        Double val = chartData.get(key);
797                        if(key >= startSimulationTime)
798                                data.add(key, val);
799                }
800                data.add(end, 0);
801                dataset.addSeries(data);
802                return dataset;
803        }
804
805        private Map<Long, Double> getResourceChartRawData(
806                        ResourceDynamicStats dynamicStats) {
807
808                Map<Long, Double> history = dynamicStats.getHistory();
809                Map<Long, Double> chartData = new TreeMap<Long, Double>();
810                for (Long key : history.keySet()) {
811                        chartData.put(key, history.get(key));
812                }
813                return chartData;
814        }
815
816        private boolean saveResourceGanttDiagrams() {
817
818                if (!generateDiagrams)
819                        return false;
820
821                String fileName = new File(outputFolderName, DIAGRAMS_FILE_NAME_PREFIX
822                                + simulationIdentifier + "_").getAbsolutePath();
823
824                String chartName = "Gantt diagram for "
825                                + DataCenterWorkloadSimulator.SIMULATOR_NAME;
826                String simulationTime = "Simulation time";
827                Title subtitle = new TextTitle("created for \"" + simulationIdentifier
828                                + "\" at " + Calendar.getInstance().getTime().toString());
829
830                JFreeChart peDiagram = null;
831                if (configuration.creatediagrams_gantt) {
832                        peDiagram = getPEGanttDiagram(chartName, subtitle,
833                                        simulationTime);
834                        if (!saveCategoryChart(peDiagram, fileName + "Gantt",
835                                        "{0}"))
836                                return false;
837                }
838                chartName = "Load diagram for "
839                        + DataCenterWorkloadSimulator.SIMULATOR_NAME;
840                JFreeChart resourceLoadDiagram = null;
841                if (configuration.creatediagrams_resutilization) {
842                        String axisName = "UTILIZATION [%]";
843                        for(ResourceType resType: resourceLoadDiagrams.keySet()){
844                                resourceLoadDiagram = getResourceDynamicDiagram(resourceLoadDiagrams.get(resType), simulationTime, chartName,
845                                                subtitle, axisName);
846                                if (!saveXYPlotChart(resourceLoadDiagram, fileName + "Resources Load - "+resType.getName()))
847                                        return false;
848                        }
849                }
850               
851                chartName = "Energy usage diagram for "
852                        + DataCenterWorkloadSimulator.SIMULATOR_NAME;
853                JFreeChart resourceEnergyDiagram = null;
854                if (configuration.creatediagrams_respowerusage) {
855                        String axisName = "POWER [W]";
856                        for(ResourceType resType: resourcePowerUsageDiagrams.keySet()){
857                                resourceEnergyDiagram = getResourceDynamicDiagram(resourcePowerUsageDiagrams.get(resType), simulationTime, chartName,
858                                                subtitle, axisName);
859                                if (!saveXYPlotChart(resourceEnergyDiagram, fileName + "Energy - "+resType.getName()))
860                                        return false;
861                        }
862                }
863               
864                chartName = "Air flow diagram for "
865                        + DataCenterWorkloadSimulator.SIMULATOR_NAME;
866                JFreeChart resourceAirFlowDiagram = null;
867                if (configuration.creatediagrams_resairflow) {
868                        String axisName = "AIR FLOW [m^3/min]";
869                        for(ResourceType resType: resourceAirFlowDiagrams.keySet()){
870                                resourceAirFlowDiagram = getResourceDynamicDiagram(resourceAirFlowDiagrams.get(resType), simulationTime, chartName,
871                                                subtitle, axisName);
872                                if (!saveXYPlotChart(resourceAirFlowDiagram, fileName + "AirThroughput - "+resType.getName()))
873                                        return false;
874                        }
875                }
876               
877                chartName = "Temperature diagram for "
878                        + DataCenterWorkloadSimulator.SIMULATOR_NAME;
879                JFreeChart resourceTemperatureDiagram = null;
880                if (configuration.creatediagrams_restemperature) {
881                        String axisName = "TEMPERATURE [C]";
882                        for(ResourceType resType: resourceTemperatureDiagrams.keySet()){
883                                resourceTemperatureDiagram = getResourceDynamicDiagram(resourceTemperatureDiagrams.get(resType), simulationTime, chartName,
884                                                subtitle, axisName);
885                                if (!saveXYPlotChart(resourceTemperatureDiagram, fileName + "Temperature - "+resType.getName()))
886                                        return false;
887                        }
888                }
889                return true;
890        }
891       
892        private JFreeChart getPEGanttDiagram(String chartName, Title subtitle,
893                        String simulationTime) {
894                String processors = "Processing Elements";
895                boolean tooltip = true;
896                boolean legend = true;
897                JFreeChart chart = TimetableChartFactory.createTimetableChart(
898                                chartName, processors, simulationTime,
899                                ganttDiagramTimetable, legend, tooltip);
900                chart.addSubtitle(subtitle);
901                TimetableRenderer rend = (TimetableRenderer) chart.getCategoryPlot()
902                                .getRenderer();
903                rend.setBackgroundBarPaint(null);
904                return chart;
905        }
906       
907        private JFreeChart getResourceDynamicDiagram(List<XYDataset> diagramData, String simulationTime, String chartName, Title subtitle,
908                        String axisName) {
909                boolean urls = false;
910                boolean tooltip = true;
911                boolean legend = true;
912                CombinedDomainXYPlot cPlot = new CombinedDomainXYPlot();
913                DateAxis xAxis = new DateAxis(simulationTime);
914                JFreeChart chart = null;
915
916                for (XYDataset dataset : diagramData) {
917                        JFreeChart tChart = ChartFactory.createXYStepAreaChart("", "",
918                                null, dataset, PlotOrientation.VERTICAL, legend, tooltip, urls);
919                        XYPlot tPlot = tChart.getXYPlot();
920                        NumberAxis yAxis = new NumberAxis(axisName);
921                        yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
922                        tPlot.setRangeAxis(yAxis);
923                        XYStepAreaRenderer rend = (XYStepAreaRenderer) tPlot.getRenderer();
924                        rend.setShapesVisible(false);
925                        cPlot.add(tPlot);
926                }
927                cPlot.setDomainAxis(xAxis);
928                cPlot.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
929                chart = new JFreeChart(chartName, cPlot);
930                chart.addSubtitle(subtitle);
931                return chart;
932        }
933       
934        private boolean saveXYPlotChart(JFreeChart c, String fileName) {
935                c.setNotify(false);
936                c.setAntiAlias(true);
937                c.setTextAntiAlias(true);
938                c.setBorderVisible(false);
939
940                int height = 900;
941
942                CombinedDomainXYPlot cPlot = (CombinedDomainXYPlot) c.getPlot();
943                int nPlots = cPlot.getSubplots().size();
944
945                if (nPlots > 3) {
946                        height = nPlots * 300;
947                }
948                int width = GANTT_WIDTH;
949
950                if (configuration.creatediagrams_resources_scale != -1)
951                        width = (int) ((endSimulationTime - startSimulationTime) * configuration.creatediagrams_resources_scale);
952                ChartRenderingInfo info = new ChartRenderingInfo();
953                File f = new File(fileName + "." + ImageFormat.PNG);
954                final String dcwormsSubtitle = "Created by "
955                                + DataCenterWorkloadSimulator.SIMULATOR_NAME
956                                + " http://www.gssim.org/";
957                c.addSubtitle(new TextTitle(dcwormsSubtitle));
958                boolean encodeAlpha = false;
959                int compression = 9;// values 0-9
960                try {
961                        ChartUtilities.saveChartAsPNG(f, c, width, height, info,
962                                        encodeAlpha, compression);
963                } catch (IOException e) {
964                        e.printStackTrace();
965                        return false;
966                }
967                return true;
968        }
969       
970        private void createPEGanttDiagram(Map<String,List<ResStat>> basicResStats) {
971                for(String peName: basicResStats.keySet()){
972                        TimetableEventSource pe = new TimetableEventSource(peName);
973                        ganttDiagramTimetable.addEventSource(pe);
974                        peGanttMap.put(peName, pe);
975                        for(ResStat resStat: basicResStats.get(peName)){
976
977                                pe = peGanttMap.get(resStat.getPeName());
978                                if(pe == null){
979                                        //pe = new TimetableEventSource(resStat.getPeName());
980                                //      ganttDiagramPeTimetable.addEventSource(pe);
981                                //      peGanttMap.put(resStat.getPeName(), pe);
982                                }
983                                TimetableEventGroup task = taskGanttMap.get(resStat.getTaskID());
984                                long startDate = resStat.getStartDate();
985                                long endDate = resStat.getEndDate();
986                                if (task == null) {
987                                        task = new TimetableEventGroup(resStat.getTaskID());
988                                        taskGanttMap.put(resStat.getTaskID(), task);
989                                        ganttDiagramTimetable.addEventGroup(task);
990                                }
991                                ganttDiagramTimetable.addEvent(pe, task,
992                                                new FixedMillisecond(startDate), new FixedMillisecond(endDate));
993                        }
994                }
995        }
996
997
998        //TO DO
999        private void createAccumulatedResourceSimulationStatistic() {
1000
1001                List<ComputingResource> resources = resourceController.getComputingResources();
1002                for(ComputingResource compRes: resources){
1003                        //for(ComputingResource child :compRes.getChildren()){
1004                       
1005                                //ResourceUsageStats resourceUsage = gatherResourceLoadStats(compRes, basicResStats);
1006                                //double load = calculateMeanValue(resourceUsage);
1007                                double load = calculateResourceLoad(compRes, basicResLoad);
1008                                accStats.meanTotalLoad.add(load);
1009                                /*ResourceEnergyStats energyUsage = gatherResourceEnergyStats(compRes);
1010                                energyUsage.setMeanUsage(calculateEnergyLoad(energyUsage));
1011                                accStats.meanEnergyUsage.add(energyUsage.meanUsage);*/
1012                        //}     
1013                }
1014        }
1015
1016        private Map<String, Double> calculatePELoad(Map<String, List<ResStat>> basicResStats){
1017                Map<String, Double> peLoad = new HashMap<String, Double>();
1018                for(String resName: basicResStats.keySet()){
1019                        List<ResStat> resStats = basicResStats.get(resName);
1020                        double sum = 0;
1021                        for(ResStat resStat:resStats){
1022                                sum += (resStat.endDate - resStat.startDate);
1023                        }
1024                        double load = sum / (endSimulationTime - startSimulationTime);
1025                        peLoad.put(resName, load);
1026                }
1027                return peLoad;
1028        }
1029       
1030        private  Double calculateResourceLoad(ComputingResource resource, Map<String, Double> peLoad ){
1031                int peCnt = 0;
1032                double sum = 0;
1033                for(String resName: peLoad.keySet()){
1034
1035                        try {
1036                                if(resource.getDescendantByName(resName) != null || resource.getName().compareTo(resName) == 0){
1037                                        Double load = peLoad.get(resName);
1038                                        sum += load;
1039                                        peCnt++;
1040                                }
1041                        } catch (ResourceException e) {
1042                                // TODO Auto-generated catch block
1043                                e.printStackTrace();
1044                        }
1045                }
1046               
1047                return sum/peCnt;
1048        }
1049
1050       
1051        private double calculateMeanValue(ResourceDynamicStats resDynamicStats ){
1052                double meanValue = 0;
1053                long time = -1;
1054                double value = 0;
1055               
1056                Map<Long, Double> history = resDynamicStats.getHistory();
1057                for (Long key : history.keySet()) {
1058                       
1059                        if(key >= startSimulationTime){
1060                                if (time != -1) {
1061                                        meanValue += (value * (key - time)) / (endSimulationTime - startSimulationTime);
1062                                        time = key;
1063                                } else {
1064                                        time = key;
1065                                }       
1066                        }
1067                        value = history.get(key);
1068                }
1069                return meanValue;
1070        }
1071       
1072       
1073
1074       
1075       
1076       
1077       
1078
1079
1080        /************* TASK STATISTICS SECTION **************/
1081        public void gatherTaskStatistics() {
1082
1083                List<JobInterface<?>> jobs = users.getAllReceivedJobs();
1084                Collections.sort(jobs, new JobIdComparator());
1085
1086                ganttDiagramTaskSeriesCollection = new TaskSeriesCollection();
1087                ganttDiagramWaitingTimeSeriesCollection = new TaskSeriesCollection();
1088
1089                PrintStream taskStatsFile = null;
1090                try {
1091                        File file = new File(outputFolderName + STATS_FILE_NAME_PREFIX + simulationIdentifier + "_"
1092                                        + RAW_TASKS_STATISTICS_OUTPUT_FILE_NAME);
1093                        taskStatsFile = new PrintStream(new FileOutputStream(file));
1094                } catch (IOException e) {
1095                        taskStatsFile = null;
1096                }
1097
1098                PrintStream jobStatsFile = null;
1099                if (configuration.createjobsstatistics) {
1100                        try {
1101                                File file = new File(outputFolderName + STATS_FILE_NAME_PREFIX + simulationIdentifier + "_"
1102                                                + JOBS_STATISTICS_OUTPUT_FILE_NAME);
1103                                jobStatsFile = new PrintStream(new FileOutputStream(file));
1104                        } catch (IOException e) {
1105                                jobStatsFile = null;
1106                        }
1107                }
1108
1109                for (int i = 0; i < jobs.size(); i++) {
1110                        Job job = (Job) jobs.get(i);
1111
1112                        List<Executable> execList = jr.getExecutableTasks().getJobExecutables(job.getId());
1113                        List<TaskStats> taskStatsList = new ArrayList<TaskStats>();
1114
1115                        this.serializer.setFieldSeparator(TASK_SEPARATOR);
1116
1117                        for (int j = 0; j < execList.size(); j++) {
1118
1119                                Executable task = (Executable) execList.get(j);
1120
1121                                TaskStats taskStats = createTaskStats(task);
1122                                if (taskStats != null && taskStatsFile != null) {
1123                                        Object txt = taskStats.serialize(serializer);
1124                                        taskStatsFile.println(txt);
1125                                }
1126                                if (taskStats != null && taskStats.getExecFinishDate() != -1) {
1127                                        if (configuration.createsimulationstatistics) {
1128                                                createAccumulatedTaskSimulationStatistic(taskStats);
1129                                        }
1130                                        allTasksFinished &= task.isFinished();
1131                                        if (generateDiagrams) {
1132                                                if (configuration.creatediagrams_tasks)
1133                                                        createTaskDiagramData(task);
1134                                                if (configuration.creatediagrams_taskswaitingtime)
1135                                                        createTaskWaitingTimeDiagramData(task);
1136                                        }
1137                                        taskStatsList.add(taskStats);
1138                                }
1139                        }
1140                        if (configuration.createjobsstatistics && taskStatsList.size() > 0) {
1141
1142                                this.serializer.setFieldSeparator(JOB_SEPARATOR);
1143                                JobStats jobStats = createJobStats(taskStatsList);
1144                                if (jobStatsFile != null) {
1145                                        Object txt = jobStats.serialize(serializer);
1146                                        jobStatsFile.println(txt);
1147                                }
1148                        }
1149                }
1150
1151                if (configuration.createsimulationstatistics) {
1152                        accStats.makespan.add(maxCj);
1153                        accStats.delayedTasks.add(numOfdelayedTasks);
1154                        accStats.failedRequests.add(users.getAllSentTasks().size() - users.getFinishedTasksCount());
1155                        if(resourceController.getScheduler().getType().getName().equals("GS")){
1156                                for(Scheduler sched: resourceController.getScheduler().getChildren()){
1157                                        accStats.meanQueueLength.add(getSchedulerQueueStats(sched));
1158                                }
1159                        }
1160                        else {
1161                                accStats.meanQueueLength.add(getSchedulerQueueStats(resourceController.getScheduler()));
1162                        }
1163                }
1164
1165                saveTaskGanttDiagrams();
1166
1167                if (taskStatsFile != null) {
1168                        taskStatsFile.close();
1169                }
1170                if (jobStatsFile != null) {
1171                        jobStatsFile.close();
1172                }
1173        }
1174
1175        private double getSchedulerQueueStats(Scheduler scheduler) {
1176
1177                Sim_stat stats = scheduler.get_stat();
1178                List<Object[]> measures = stats.get_measures();
1179                for (Object[] info : measures) {
1180                        String measure = (String) info[0];
1181                        if (measure
1182                                        .startsWith(DCWormsConstants.TASKS_QUEUE_LENGTH_MEASURE_NAME)) {
1183                                return stats.average(measure);
1184                        }
1185                }
1186                return 0;
1187        }
1188       
1189        private TaskStats createTaskStats(Executable task) {
1190                TaskStats taskStats = new TaskStats(task, startSimulationTime);
1191                String uniqueTaskID = taskStats.getJobID() + "_" + taskStats.getTaskID();
1192                taskStats.setProcessorsName(task_processorsMap.get(uniqueTaskID));
1193                return taskStats;
1194        }
1195
1196        private JobStats createJobStats(List<TaskStats> tasksStats) {
1197
1198                String jobID = ((TaskStats) tasksStats.get(0)).getJobID();
1199                JobStats jobStats = new JobStats(jobID);
1200                double maxCj = 0;
1201
1202                for (int i = 0; i < tasksStats.size(); i++) {
1203
1204                        TaskStats task = (TaskStats) tasksStats.get(i);
1205                        jobStats.meanTaskStartTime.add(task.getStartTime());
1206                        jobStats.meanTaskCompletionTime.add(task.getCompletionTime());
1207                        maxCj = Math.max(maxCj, task.getCompletionTime());
1208                        jobStats.meanTaskExecutionTime.add(task.getExecutionTime());
1209                        jobStats.meanTaskWaitingTime.add(task.getWaitingTime());
1210                        jobStats.meanTaskFlowTime.add(task.getFlowTime());
1211                        jobStats.meanTaskGQ_WaitingTime.add(task.getGQ_WaitingTime());
1212                        jobStats.lateness.add(task.getLateness());
1213                        jobStats.tardiness.add(task.getTardiness());
1214                }
1215                jobStats.makespan.add(maxCj);
1216
1217                return jobStats;
1218        }
1219
1220        private void createAccumulatedTaskSimulationStatistic(TaskStats taskStats) {
1221                accStats.meanTaskFlowTime.add(taskStats.getFlowTime());
1222                accStats.meanTaskExecutionTime.add(taskStats.getExecutionTime());
1223                accStats.meanTaskCompletionTime.add(taskStats.getCompletionTime());
1224                accStats.meanTaskWaitingTime.add(taskStats.getWaitingTime());
1225                accStats.meanTaskStartTime.add(taskStats.getStartTime());
1226
1227                accStats.lateness.add(taskStats.getLateness());
1228                accStats.tardiness.add(taskStats.getTardiness());
1229
1230                if (taskStats.getExecFinishDate() == -1) {
1231                        numOfNotExecutedTasks++;
1232                }
1233                if (taskStats.getTardiness() > 0.0) {
1234                        numOfdelayedTasks++;
1235                }
1236
1237                maxCj = Math.max(maxCj, taskStats.getCompletionTime());
1238        }
1239
1240        private void createTaskDiagramData(Executable task) {
1241                String uniqueTaskID = task.getJobId() + "_" + task.getId();
1242
1243                String resID = task.getSchedulerName();
1244
1245                long execStartTime = Double.valueOf(task.getExecStartTime()).longValue() * MILLI_SEC;
1246                long execEndTime = Double.valueOf(task.getFinishTime()).longValue() * MILLI_SEC;
1247
1248                TaskSeries taskRes = ganttDiagramTaskSeriesCollection.getSeries(resID);
1249                if (taskRes == null) {
1250                        taskRes = new TaskSeries(resID);
1251                        ganttDiagramTaskSeriesCollection.add(taskRes);
1252                }
1253                org.jfree.data.gantt.Task ganttTask = new org.jfree.data.gantt.Task(uniqueTaskID, new Date(execStartTime),
1254                                new Date(execEndTime));
1255                if (!task.isFinished()) {
1256                        ganttTask.setPercentComplete(1.0);
1257                }
1258                taskRes.add(ganttTask);
1259        }
1260
1261        private void createTaskWaitingTimeDiagramData(Executable task) {
1262                String uniqueTaskID = task.getJobId() + "_" + task.getId();
1263
1264                String resID = task.getSchedulerName();
1265
1266                long execStartTime = Double.valueOf(task.getExecStartTime()).longValue() * MILLI_SEC;
1267                long execEndTime = Double.valueOf(task.getFinishTime()).longValue() * MILLI_SEC;
1268
1269                TaskSeries waitRes = ganttDiagramWaitingTimeSeriesCollection.getSeries(resID);
1270                if (waitRes == null) {
1271                        waitRes = new TaskSeries(resID);
1272                        ganttDiagramWaitingTimeSeriesCollection.add(waitRes);
1273                }
1274
1275                long sub = Double.valueOf(task.getSubmissionTime()).longValue() * MILLI_SEC;
1276                org.jfree.data.gantt.Task wait_exec = new org.jfree.data.gantt.Task(uniqueTaskID, new Date(sub), new Date(
1277                                execEndTime));
1278                org.jfree.data.gantt.Task exec = new org.jfree.data.gantt.Task(uniqueTaskID, new Date(execStartTime), new Date(
1279                                execEndTime));
1280                if (!task.isFinished()) {
1281                        exec.setPercentComplete(1.0);
1282                }
1283                wait_exec.addSubtask(wait_exec);
1284                wait_exec.addSubtask(exec);
1285                waitRes.add(wait_exec);
1286        }
1287
1288        private boolean saveTaskGanttDiagrams() {
1289
1290                JFreeChart taskDiagram = null;
1291                JFreeChart waitingTimeDiagram = null;
1292
1293                if (!generateDiagrams)
1294                        return false;
1295
1296                String fileName = new File(outputFolderName, DIAGRAMS_FILE_NAME_PREFIX + simulationIdentifier + "_")
1297                                .getAbsolutePath();
1298
1299                String chartName = "Task diagram for " + DataCenterWorkloadSimulator.SIMULATOR_NAME;
1300                String simulationTime = "Simulation time";
1301                Title subtitle = new TextTitle("created for \"" + simulationIdentifier + "\" at "
1302                                + Calendar.getInstance().getTime().toString());
1303
1304                if (configuration.creatediagrams_tasks) {
1305                        taskDiagram = getTaskDiagram(chartName, subtitle, simulationTime);
1306                        if (!saveCategoryChart(taskDiagram, fileName + "Tasks", null /* "{0}" */))
1307                                return false;
1308                }
1309
1310                chartName = "Task waiting times diagram for " + DataCenterWorkloadSimulator.SIMULATOR_NAME;
1311                if (configuration.creatediagrams_taskswaitingtime) {
1312                        waitingTimeDiagram = getWaitingTimeDiagram(chartName, subtitle, simulationTime);
1313                        if (!saveCategoryChart(waitingTimeDiagram, fileName + "Waiting_Time", null /* "Task {1} at {0}" */))
1314                                return false;
1315                }
1316
1317                return true;
1318        }
1319
1320        private JFreeChart getTaskDiagram(String chartName, Title subtitle, String simulationTime) {
1321                String tasks = "Tasks";
1322                boolean urls = false;
1323                boolean tooltip = true;
1324                boolean legend = true;
1325                JFreeChart chart = ChartFactory.createGanttChart(chartName, tasks, simulationTime,
1326                                ganttDiagramTaskSeriesCollection, legend, tooltip, urls);
1327                chart.addSubtitle(subtitle);
1328                GanttRenderer re = (GanttRenderer) chart.getCategoryPlot().getRenderer();
1329                re.setCompletePaint(Color.black);
1330                return chart;
1331        }
1332
1333        private JFreeChart getWaitingTimeDiagram(String chartName, Title subtitle, String simulationTime) {
1334                String tasks = "Tasks";
1335                boolean urls = false;
1336                boolean tooltip = true;
1337                boolean legend = true;
1338                JFreeChart chart = ChartFactory.createGanttChart(chartName, tasks, simulationTime,
1339                                ganttDiagramWaitingTimeSeriesCollection, legend, tooltip, urls);
1340                chart.addSubtitle(subtitle);
1341                chart.getPlot().setForegroundAlpha(ALPHA);
1342                GanttRenderer re = (GanttRenderer) chart.getCategoryPlot().getRenderer();
1343                re.setCompletePaint(Color.black);
1344                return chart;
1345        }
1346
1347        private boolean saveCategoryChart(JFreeChart c, String fileName, String labelFormat) {
1348                c.setNotify(false);
1349                c.setAntiAlias(true);
1350                c.setTextAntiAlias(true);
1351                c.setBorderVisible(false);
1352
1353                CategoryPlot categoryplot = (CategoryPlot) c.getPlot();
1354                categoryplot.setDomainGridlinesVisible(true);
1355
1356                if (labelFormat != null) {
1357                        CategoryItemRenderer rend = categoryplot.getRenderer();
1358                        rend.setBaseItemLabelsVisible(true);
1359                        ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER);
1360                        rend.setBasePositiveItemLabelPosition(position);
1361                        NumberFormat nf = NumberFormat.getInstance();
1362                        CategoryItemLabelGenerator gen = new StandardCategoryItemLabelGenerator(labelFormat, nf);
1363                        rend.setBaseItemLabelGenerator(gen);
1364                }
1365
1366                int rows = c.getCategoryPlot().getDataset().getColumnKeys().size();
1367
1368                int height = 900;
1369                if (rows > 45) {
1370                        height = rows * 20;
1371                }
1372                int width = calculateChartWidth(c);
1373                CategoryItemRenderer rend = c.getCategoryPlot().getRenderer();
1374
1375                for (int i = 0; i < c.getCategoryPlot().getDataset().getRowKeys().size(); i++) {
1376                        Paint collor = c.getCategoryPlot().getDrawingSupplier().getNextPaint();
1377                        rend.setSeriesOutlinePaint(i, collor);
1378                        rend.setSeriesPaint(i, collor);
1379                }
1380
1381                ChartRenderingInfo info = new ChartRenderingInfo();
1382                // info.setEntityCollection(null);
1383                File f = new File(fileName + "." + ImageFormat.PNG);
1384                final String dcwormsSubtitle = "Created by " + DataCenterWorkloadSimulator.SIMULATOR_NAME + " http://www.gssim.org/";
1385                c.addSubtitle(new TextTitle(dcwormsSubtitle));
1386                boolean encodeAlpha = false;
1387                int compression = 9;
1388                try {
1389                        ChartUtilities.saveChartAsPNG(f, c, width, height, info, encodeAlpha, compression);
1390
1391                } catch (IOException e) {
1392                        e.printStackTrace();
1393                        return false;
1394                } catch (Exception e) {
1395                        if (log.isErrorEnabled())
1396                                log.error("The png file (" + fileName
1397                                                + ")\nwill not be generated (It can be too large data size problem)", e);
1398                } catch (Throwable t) {
1399                        log.error("The png file (" + fileName + ")\nwill not be generated (It can be too large data size problem)",
1400                                        t);
1401                }
1402
1403                return true;
1404        }
1405
1406        private int calculateChartWidth(JFreeChart c) {
1407
1408                int ganttWidth = GANTT_WIDTH;
1409                int rows = c.getCategoryPlot().getDataset().getColumnKeys().size();
1410
1411                if (rows > 45) {
1412                        int height = rows * 20;
1413                        ganttWidth = Math.max((int) (height * 1.5), GANTT_WIDTH);
1414                }
1415                return ganttWidth;
1416        }
1417
1418        private static class JobIdComparator implements Comparator<JobInterface<?>> {
1419
1420                public int compare(JobInterface<?> o1, JobInterface<?> o2) {
1421                        Integer o1int;
1422                        Integer o2int;
1423                        try{
1424                                o1int = Integer.parseInt(o1.getId());
1425                                o2int = Integer.parseInt(o2.getId());
1426                                return o1int.compareTo(o2int);
1427                        } catch(NumberFormatException e){
1428                                return o1.getId().compareTo(o2.getId());
1429                        }
1430                }
1431        }
1432       
1433        private static class MapPEIdComparator implements Comparator<String> {
1434
1435                public int compare(String o1, String o2)  {
1436                        Integer o1int;
1437                        Integer o2int;
1438                        String o1string;
1439                        String o2string;
1440                        o1string = o1.substring(0, o1.indexOf("_"));
1441                        o2string = o2.substring(0, o2.indexOf("_"));
1442                        o1int = Integer.parseInt(o1.substring(o1.indexOf("_")+1));
1443                        o2int = Integer.parseInt(o2.substring(o2.indexOf("_")+1));
1444                        if(o1string.compareTo(o2string) != 0)
1445                                return o1string.compareTo(o2string);
1446                        else
1447                                return o1int.compareTo(o2int);
1448                }
1449        }
1450
1451        public GSSAccumulator getStats(String name) {
1452                return statsData.get(name);
1453        }
1454
1455        public boolean accumulatedStats() {
1456                return configuration.createsimulationstatistics;
1457        }
1458
1459}
1460
1461class ResStat{
1462       
1463        public long getStartDate() {
1464                return startDate;
1465        }
1466        public void setStartDate(long startDate) {
1467                this.startDate = startDate;
1468        }
1469        public long getEndDate() {
1470                return endDate;
1471        }
1472        public void setEndDate(long endDate) {
1473                this.endDate = endDate;
1474        }
1475        public String getTaskID() {
1476                return taskID;
1477        }
1478        public void setTaskID(String taskID) {
1479                this.taskID = taskID;
1480        }
1481
1482        public ResStat( String peName, long startDate, long endDate, String taskID) {
1483                super();
1484                this.startDate = startDate;
1485                this.endDate = endDate;
1486                this.taskID = taskID;
1487                this.peName = peName;
1488        }
1489        public String getPeName() {
1490                return peName;
1491        }
1492        public void setPeName(String peName) {
1493                this.peName = peName;
1494        }
1495        public ResStat(String peName, ResourceType resType, long startDate, long endDate, String taskID) {
1496                super();
1497                this.peName = peName;
1498                this.resType = resType;
1499                this.startDate = startDate;
1500                this.endDate = endDate;
1501                this.taskID = taskID;
1502        }
1503        String peName;
1504        ResourceType resType;
1505        long startDate;
1506        long endDate;
1507        String taskID;
1508
1509        public ResourceType getResType() {
1510                return resType;
1511        }
1512        public void setResType(ResourceType resType) {
1513                this.resType = resType;
1514        }
1515}
1516
1517enum Stats{
1518        textLoad,
1519        chartLoad,
1520        textEnergy,
1521        chartEnergy,
1522        textAirFlow,
1523        chartAirFlow,
1524        textTemperature,
1525        chartTemperature;
1526}
Note: See TracBrowser for help on using the repository browser.