source: xssim/branches/tpiontek/src/simulator/GridSchedulingSimulator.java @ 241

Revision 241, 40.7 KB checked in by piontek, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator;
2
3//import eduni.cloudsim.GSSIM;
4import eduni.simjava.Sim_system;
5import gridsim.GridSim;
6import gridsim.gssim.GssimConstants;
7import gridsim.gssim.network.ExtendedGridSim;
8import gridsim.gssim.network.ExtendedGridSimCore;
9import gridsim.gssim.network.ExtendedGridSimTags;
10import gridsim.gssim.resource.AbstractComputingResource;
11import gssim.schedframe.scheduling.plugin.grid.network.manager.Topology;
12import gssim.schedframe.scheduling.plugin.grid.network.utils.GSSIMNetworkReader;
13
14import java.io.File;
15import java.io.FileInputStream;
16import java.io.FileNotFoundException;
17import java.io.FileOutputStream;
18import java.io.FileReader;
19import java.io.FileWriter;
20import java.io.FilenameFilter;
21import java.io.IOException;
22import java.io.PrintStream;
23import java.security.InvalidParameterException;
24import java.text.NumberFormat;
25import java.util.ArrayList;
26import java.util.Calendar;
27import java.util.Date;
28
29import javax.swing.JFileChooser;
30import javax.swing.filechooser.FileFilter;
31
32import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
33import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
34import org.apache.commons.logging.Log;
35import org.apache.commons.logging.LogFactory;
36import org.joda.time.DateTimeUtilsExt;
37import org.qcg.broker.schemas.jobdesc.QcgJob;
38
39import simulator.stats.AccumulatedStatistics;
40import simulator.stats.implementation.GSSimStatistics;
41import simulator.utils.LogErrStream;
42import simulator.workload.WorkloadLoader;
43import simulator.workload.reader.archive.AbstractWAReader;
44import simulator.workload.reader.archive.WAReader;
45import simulator.workload.reader.xmlJob.Grms3XmlJobReader;
46import simulator.workload.reader.xmlJob.XMLJobReader;
47import test.rewolucja.GSSimUsersNew;
48import test.rewolucja.resources.physical.base.ComputingResource;
49import test.rewolucja.resources.physical.implementation.DataCenter;
50import test.rewolucja.resources.reader.TempResourceReader;
51import test.rewolucja.resources.utils.ResourceController;
52import cern.jet.random.Uniform;
53import cern.jet.random.engine.MersenneTwister64;
54
55/**
56 * The main class of the Grid Scheduling Simulator. It has the
57 * {@link #main(String[])} method used to invoke the program. This class also
58 * provides second possibility to start the simulator, namely one may use the
59 * {@link #performSimulation(ConfigurationOptions, GSSimStatisticsOld)} method.
60 * In this case, the input parameter, describing the simulation options, must be
61 * earlier prepared. The results of the simulation can be acquired using the
62 * {@link #getAccumulatedStatistics()} method.
63 *
64 * @author Stanislaw Szczepanowski
65 */
66public class GridSchedulingSimulator {
67
68        /**
69         * The name of the simulator application
70         */
71        public static final String SIMULATOR_NAME = "Grid Scheduling Simulator";
72
73        /**
74         * GridSim statistics output filename
75         */
76        public static final String GridSim_stat = "GridSim_stat.txt";
77
78        /**
79         * SimJava2 report filename
80         */
81        public static final String sim_report = "sim_report";
82
83        /**
84         * SimJava2 trace filename
85         */
86        public static final String sim_trace = "sim_trace";
87
88        /**
89         * The name of the file with the statistics for the whole run of the
90         * experiment
91         */
92        public static final String globalStatisticsFileName = "Statistics.txt";
93
94        /**
95         * Stores the statistical data of last run of the simulator (it may consist
96         * of several runs of simulations)
97         */
98        protected static AccumulatedStatistics accumulatedStatistics;
99
100        /**
101         * Determines the maximum fraction digits when printing floating point
102         * values
103         */
104        public static int MAXIMUM_FRACTION_DIGITS = 3;
105
106        /**
107         * The format of the numbers that will be printed
108         */
109        public static NumberFormat DFAULT_NUMBER_FORMAT = NumberFormat
110                        .getInstance();
111        static {
112                DFAULT_NUMBER_FORMAT.setMaximumFractionDigits(MAXIMUM_FRACTION_DIGITS); // 0,001
113        }
114
115        protected static int MAXIMUM_NETWORK_FRACTION_DIGITS = 6;
116        // protected static NumberFormat DFAULT_NUMBER_FORMAT =
117        // NumberFormat.getInstance();
118        public static NumberFormat NETWORK_NUMBER_FORMAT = NumberFormat
119                        .getInstance();
120        static {
121                // DFAULT_NUMBER_FORMAT.setMaximumFractionDigits(MAXIMUM_FRACTION_DIGITS);
122                NETWORK_NUMBER_FORMAT
123                                .setMaximumFractionDigits(MAXIMUM_NETWORK_FRACTION_DIGITS);
124        }
125
126        private Log log = LogFactory.getLog(GridSchedulingSimulator.class);
127
128        protected ArrayList<GSSimUsers> wl_ = new ArrayList<GSSimUsers>();
129        protected ArrayList<GridBroker> brokerInterface_ = new ArrayList<GridBroker>();
130        // protected ArrayList<String>statsOutputPath_ = new ArrayList<String>();
131        protected AbstractComputingResource resources_[];
132        protected String statsOutputPath;
133        protected static int simulationRunNumber = 0;
134
135        /**
136         * Default empty constructor
137         */
138        public GridSchedulingSimulator() {
139        }
140
141        /**
142         * The main method to start the simulation the accepted arguments are:
143         * args[0] - the name of the resource bundle file with the configuration
144         * args[1] (optional) - the number of simulations that are to be performed
145         * (the default value is 1)
146         *
147         * @param args
148         *            the arguments passed to the application
149         */
150        public static void main(String[] args) {
151                GridSchedulingSimulator gssim = new GridSchedulingSimulator();
152                gssim.run(args);
153        }
154
155        public void run(String args[]) {
156                System.setErr(new PrintStream(new LogErrStream(log)));
157
158                String propertiesFileName = null;
159                if (args.length == 0) {
160                        JFileChooser ch = new JFileChooser(".");
161                        FileFilter filter = new FileFilter() {
162                                @Override
163                                public boolean accept(File f) {
164                                        return f.getName().endsWith(".properties")
165                                                        || f.isDirectory();
166                                }
167
168                                @Override
169                                public String getDescription() {
170                                        return "GSSim experiment file";
171                                }
172                        };
173                        ch.setFileFilter(filter);
174                        ch.setDialogTitle("Choose the GSSim simulation experiment file");
175                        int result = ch.showOpenDialog(ch);
176                        if (result == JFileChooser.APPROVE_OPTION) {
177                                propertiesFileName = ch.getSelectedFile().getAbsolutePath();
178                        } else {
179                                if (log.isFatalEnabled())
180                                        log.fatal("Resource bundle file name was not provided.");
181                                return;
182                        }
183                } else if (args.length == 2 && "-multiuser".equals(args[0])) {
184                        runMultiuser(args[1], this);
185                        return;
186                } else {
187                        propertiesFileName = args[0];
188                }
189
190                ConfigurationOptions configurationOptions = ConfigurationOptions
191                                .getConfiguration(propertiesFileName);
192                int noOfSimulations = 1; // default value is one
193
194                if(configurationOptions.getNumberOfSimulations() > 1)
195                        noOfSimulations = configurationOptions.getNumberOfSimulations();
196                else if (args.length == 2) {// second parameter is given
197                        noOfSimulations = Integer.parseInt(args[1]);
198                        if (noOfSimulations < 1) {
199                                throw new InvalidParameterException(
200                                                "Number of simulations cannot be less than 1");
201                        }
202                }
203
204                if (log.isInfoEnabled())
205                        log.info(":: Starting " + SIMULATOR_NAME + ". " + noOfSimulations
206                                        + " simulation runs will be performed. ::");
207
208                accumulatedStatistics = new AccumulatedStatistics(noOfSimulations);
209
210                File outputDir = null;
211
212                try {
213                        outputDir = prepareDirecotry(configurationOptions);
214                } catch (Exception e) {
215                        if (log.isErrorEnabled())
216                                log.error("FAILED to create the output path", e);
217                        return;
218                }
219
220                File outputPropertiesFile = new File(outputDir, "experiment.properties");
221
222                for (int i = 0; i < noOfSimulations; ++i) {
223                        simulationRunNumber++;
224                        String simulationIdentifier = "Simulation " + (i + 1);
225                        try {
226                                // GSSimStatisticsNew simulationStatistics = new
227                                // GSSimStatisticsNew(simulationIdentifier,
228                                // configurationOptions);
229                                // accumulatedStatistics.add(simulationStatistics);
230
231                                if (configurationOptions.networkTopologyFileName != null)
232                                        performNetworkSimulation(simulationIdentifier,
233                                                        configurationOptions); // run the simulation using
234                                // network extension
235                                else
236                                        performSimulation(simulationIdentifier,
237                                                        configurationOptions); // run the simulation
238
239                        } catch (Exception e) {
240                                if (log.isErrorEnabled())
241                                        log.error("ERROR in simulation run \""
242                                                        + simulationIdentifier + "\":", e);
243                                break;
244                        }
245                }
246                /*
247                 * String outputPath; String outputFile = globalStatisticsFileName; if
248                 * (configurationOptions.createScenario) { outputPath =
249                 * configurationOptions.outputFolder +
250                 * configurationOptions.statsOutputSubfolderNameCreate + File.separator;
251                 * } else { String prefix = null; if(configurationOptions.inputFolder !=
252                 * null) { prefix = configurationOptions.inputFolder; } else
253                 * if(configurationOptions.inputWorkloadFileName != null){ prefix = new
254                 * File(configurationOptions.inputWorkloadFileName).getParent(); } else
255                 * { prefix = System.getProperty("user.dir"); }
256                 *
257                 *
258                 * outputPath = prefix +"/"+
259                 * configurationOptions.statsOutputSubfolderNameRerad + File.separator;
260                 * }
261                 *
262                 * if(log.isInfoEnabled()) log.info(":: Creating statistics ... ");
263                 *
264                 * //clean all gantts /*File file = new File(outputPath); File
265                 * fileList[] = file.listFiles(); for(int i = 0; i < fileList.length;
266                 * i++){ File tmpFile = fileList[i]; String name = tmpFile.getName();
267                 * if(name.length() > 4){ String subName = name.substring(0, 5);
268                 * if(subName.compareTo("Gantt") == 0){ tmpFile.delete(); } } }
269                 */
270                //accumulatedStatistics.saveStatistics();
271
272                if (log.isInfoEnabled())
273                        log.info("Done :: " + SIMULATOR_NAME + " has finished "
274                                        + noOfSimulations + " simulation runs ::");
275
276                // fixed name with z in front to be last file in alphabetical order
277
278                System.out.flush();
279                System.err.flush();
280
281                try {
282                        copyFile(new File(propertiesFileName), outputPropertiesFile);
283                } catch (IOException e) {
284                        log
285                                        .error("IO exception occured during copying properties file to output path");
286                }
287
288                FilenameFilter fileFilter = new FilenameFilter() {
289                        public boolean accept(File dir, String name) {
290                                return (!name.endsWith(".sjg") && !name.endsWith(".png"));
291                        }
292                };
293
294                /*File tarArchive = new File(outputDir, "results.tar");
295
296                File networkTopologyFile = (configurationOptions.networkTopologyFileName == null) ? null
297                                : new File(configurationOptions.networkTopologyFileName);
298
299                try {
300                        createResultsTar(tarArchive, outputDir.listFiles(fileFilter),
301                                        networkTopologyFile);
302                } catch (IOException e) {
303                        log.error("IOException occured while creating archive file", e);
304                }*/     }
305
306        public void runMultiuser(String rootDirPath, GridSchedulingSimulator gssim) {
307                /*
308                 * System.out.println("************ Runing in multiuser mode ************"
309                 * ); File mainDirectory = null; mainDirectory = new File(rootDirPath);
310                 * if(!mainDirectory.exists() || !mainDirectory.isDirectory()) throw new
311                 * RuntimeException("Second argument must be a root directory name.");
312                 *
313                 * java.io.FileFilter dirFilter = new WorkloadDirectoryFilter();
314                 * java.io.FileFilter propertiesFilter = new PropertiesFileFilter();
315                 *
316                 * File workloadDirs[] = mainDirectory.listFiles(dirFilter); File
317                 * propertiesFile[] = null; File dir;
318                 *
319                 * File localResProperiesFiles[] =
320                 * mainDirectory.listFiles(propertiesFilter); ConfigurationOptions
321                 * configurationOptions =
322                 * ConfigurationOptions.getConfiguration(localResProperiesFiles
323                 * [0].getAbsolutePath());
324                 *
325                 * //BEGIN: Initializing the GridSim:
326                 *
327                 * // use simulation start time from one, specific swf file String
328                 * fileName = configurationOptions.inputWorkloadFileName; Date date =
329                 * null; try { date = AbstractSWFJobReader.getStartTime(fileName); }
330                 * catch (IOException e1) { date = new Date(0l); } catch
331                 * (NoSuchCommentException e1) { date = new Date(0l); }
332                 *
333                 * Calendar calendar = Calendar.getInstance(); calendar.setTime(date);
334                 *
335                 * boolean traceFlag = true; // means: trace GridSim events/activities
336                 *
337                 * String[] excludeFromFile = {""}, excludeFromProcessing = {""}; String
338                 * reportFileName = GssConstants.BrokerInterfaceEntityName+"_0"; //FIXME
339                 * solve this issue more elegant - this is a result of many brokers in
340                 * simulation
341                 *
342                 * GridSim.init(workloadDirs.length, calendar, traceFlag,
343                 * excludeFromFile, excludeFromProcessing, reportFileName);
344                 *
345                 * //END: Initializing the GridSim:
346                 *
347                 * VirtualClock vClock = VirtualClock.initialize();
348                 *
349                 * //Startup of the random number generators must be set up with a
350                 * random seed that is greater than zero //if seed is not given then all
351                 * consecutive simulations are the same //if seed < 0 then the random
352                 * numbers are equal to zero Uniform uniform = new Uniform(new
353                 * MersenneTwister64(new Date())); //seed should be > 0 and fits to int
354                 * size (which is also important) long seed = uniform.nextLongFromTo(1,
355                 * Integer.MAX_VALUE); assert seed > 0 && seed <= Integer.MAX_VALUE :
356                 * "Initial seed is <= 0, what is improper"; Sim_system.set_seed(seed);
357                 *
358                 * accumulatedStatistics = new AccumulatedStatistics(); try {
359                 *
360                 * // wczytanie i zbudowanie opisu swiata
361                 *
362                 * ResourceGeneratorInterface hostDescGenerator = new ResourceGenerator(
363                 * configurationOptions.forecastFinishTimePluginName,
364                 * configurationOptions.localAllocPolicyPluginName);
365                 * hostDescGenerator_.add(hostDescGenerator);
366                 * hostDescGenerator.readConfigurationHostsDescriptionXMLFile(
367                 * configurationOptions.resdescFileName);
368                 *
369                 * hostDescGenerator.createResources(vClock); resources_ =
370                 * hostDescGenerator.getCreatedResources();
371                 * System.out.println("localResources: "
372                 * +localResProperiesFiles[0].getAbsolutePath());
373                 *
374                 * // wczytanie i przygotowanie opisu zadan for(int i = 0; i <
375                 * workloadDirs.length; i++){ dir = workloadDirs[i]; propertiesFile =
376                 * dir.listFiles(propertiesFilter); if(propertiesFile.length == 0) throw
377                 * new RuntimeException("No properties file in "+dir.getPath());
378                 * if(propertiesFile.length > 1) throw new
379                 * RuntimeException("Leave only one properties file in "+dir.getPath());
380                 *
381                 * System.out.println(propertiesFile[0].getAbsolutePath());
382                 * configurationOptions =
383                 * ConfigurationOptions.getConfiguration(propertiesFile
384                 * [0].getAbsolutePath()); GSSimStatistics simulationStatistics = new
385                 * GSSimStatistics("stats_for_"+propertiesFile[0].getName());
386                 * simulationStatistics_.add(simulationStatistics);
387                 * accumulatedStatistics.add(simulationStatistics);
388                 * gssim.prepareSimulation(configurationOptions, simulationStatistics,
389                 * vClock);
390                 *
391                 * }
392                 *
393                 *
394                 * gssim.runMultiuserSimulation();
395                 *
396                 * } catch (Exception e) { e.printStackTrace(); }
397                 */
398        }
399
400        public void prepareSimulation(ConfigurationOptions options,
401                        GSSimStatistics simulationStatistics/* , VirtualClock vClock */)
402                        throws Exception {
403
404                /*
405                 * System.out.println("\n:: " +
406                 * simulationStatistics.getSimulationIdentifier() + "\" ::");
407                 * System.out.print(":: In the the mode of "); String statsOutputPath =
408                 * null; if (options.createScenario) { File outputFolderFile = new
409                 * File(options.outputFolder); if (! outputFolderFile.exists()) { if (!
410                 * outputFolderFile.mkdirs()) throw new
411                 * IOException("Cannot create the output path: "
412                 * +statsOutputPath+". Cause: "); }
413                 *
414                 * System.out.println("CREATING SCENARIO ::"); statsOutputPath =
415                 * options.outputFolder + STATS_OUTPUT_SUBFOLDER_NAME_CREATE; } else {
416                 * System.out.println("READING SCENARIO ::"); statsOutputPath =
417                 * options.inputFolder + STATS_OUTPUT_SUBFOLDER_NAME_READ; }
418                 * statsOutputPath += File.separator;
419                 * statsOutputPath_.add(statsOutputPath);
420                 *
421                 * System.out.println();
422                 *
423                 * Sim_system.set_trace_detail(true, true, true);
424                 * Sim_system.generate_graphs(statsOutputPath +
425                 * GssConstants.GraphsFileName);
426                 * Sim_system.set_trace_level(Integer.MAX_VALUE);
427                 *
428                 * // reseting the gridlet numbering //
429                 * GssConstants.resetGridletIDSequencer();
430                 *
431                 * JobGenerator <QcgJob>jobDescGenerator = new JobGenerator_GRMS3(); if
432                 * (options.createScenario) {
433                 * jobDescGenerator.generateWorkload(options.workloadDescFileName,
434                 * options.outputFolder, options.overwriteFiles,
435                 * options.outputWorkloadFileName);
436                 * jobDescGenerator.performPostprocessing(options.resdescFileName,
437                 * options.outputFolder, options.outputWorkloadFileName);
438                 * options.inputFolder = options.outputFolder;
439                 * options.inputWorkloadFileName = options.outputWorkloadFileName; }
440                 *
441                 * if(options.createXMLSupplement){
442                 * jobDescGenerator.generateXMLSupplement(options.workloadDescFileName,
443                 * options.inputWorkloadFileName, options.outputFolder,
444                 * options.overwriteFiles); }
445                 *
446                 * jobDescGenerator.loadWorkload(options.inputFolder,
447                 * options.inputWorkloadFileName);
448                 *
449                 *
450                 * if (!jobDescGenerator.isReady()) { System.err.println(
451                 * "Error: xml job genereator is not fully initialized. The simulation will terminate."
452                 * ); System.exit(1); }
453                 *
454                 *
455                 * //creating the broker interface entity GridScheduler brokerInterface
456                 * = new GridScheduler(GssConstants.BrokerInterfaceEntityName + "_" +
457                 * brokerInterface_.size(), options, jobDescGenerator.getJobCount(),
458                 * jobDescGenerator.getTaskCount());
459                 *
460                 * GSSimUsers<JobGridlet> wl = new
461                 * GSSimUsers<JobGridlet>("GridUsers"+wl_.size(),
462                 * GssConstants.BrokerInterfaceEntityName + "_" +
463                 * brokerInterface_.size(), jobDescGenerator);
464                 *
465                 * for(int i = 0; i < resources_.length; i++){
466                 * brokerInterface.addToResourceSubset(resources_[i].get_id()); }
467                 *
468                 * wl_.add(wl); brokerInterface_.add(brokerInterface);
469                 *
470                 * //create the output folder File statsOutputPathFile = new
471                 * File(statsOutputPath); if (! statsOutputPathFile.exists()) { if (!
472                 * statsOutputPathFile.mkdirs()) throw new
473                 * IOException("Cannot create the output (stats) path: "
474                 * +statsOutputPath+". Cause: "); }
475                 */
476
477        }
478
479        /**
480         * Starts a single simulation run.
481         *
482         * @param options
483         *            the configuration options according to which the simulation is
484         *            to be performed
485         * @param simulationStatistics
486         *            the statistics object that is to be filled after the
487         *            simulation run
488         * @throws Exception
489         *             any exception that may occur
490         */
491        public void performSimulation(String simulationIdentifier,
492                        ConfigurationOptions options) throws Exception {
493                // Startup of the random number generators must be set up with a random
494                // seed that is greater than zero
495                // if seed is not given then all consecutive simulations are the same
496                // if seed < 0 then the random numbers are equal to zero
497                Uniform uniform = new Uniform(new MersenneTwister64(new Date()));
498                // seed should be > 0 and fits to int size (which is also important)
499                long seed = uniform.nextLongFromTo(1, Integer.MAX_VALUE);
500                if (log.isDebugEnabled())
501                        log.debug("Shuffled initial seed: " + seed);
502
503                assert seed > 0 && seed <= Integer.MAX_VALUE : "Initial seed is <= 0, what is improper";
504                Sim_system.set_seed(seed);
505
506                long startSimulation = System.currentTimeMillis();
507                if (log.isInfoEnabled()) {
508                        log.info(":: Starting simulation run: \"" + simulationIdentifier
509                                        + "\" ::");
510                        log.info(":: In the the mode of ");
511                }
512                /*
513                 * String statsOutputPath = null; if (options.createScenario) { File
514                 * outputFolderFile = new File(options.outputFolder); if (!
515                 * outputFolderFile.exists()) { if (! outputFolderFile.mkdirs()) throw
516                 * new
517                 * IOException("Cannot create the output path: "+statsOutputPath+". Cause: "
518                 * ); }
519                 *
520                 * if(log.isInfoEnabled()) log.info("CREATING SCENARIO ::");
521                 * statsOutputPath = options.outputFolder +
522                 * options.statsOutputSubfolderNameCreate; } else {
523                 * if(log.isInfoEnabled()) log.info("READING SCENARIO ::"); String
524                 * prefix = null; if(options.inputFolder != null){ prefix =
525                 * options.inputFolder; }else if(options.inputWorkloadFileName != null){
526                 * prefix = new File(options.inputWorkloadFileName).getParent(); }else{
527                 * prefix = System.getProperty("user.dir"); } statsOutputPath = prefix +
528                 * "/" + options.statsOutputSubfolderNameRerad; } statsOutputPath +=
529                 * File.separator;
530                 */
531
532                Sim_system.set_trace_detail(true, true, true);
533                Sim_system.generate_graphs(statsOutputPath
534                                + GssimConstants.GraphsFileName);
535                Sim_system.set_trace_level(Integer.MAX_VALUE);
536
537                XMLJobReader<QcgJob> xmlJobReader = null;
538                WAReader<QcgJob> swfReader = null;
539
540                String wlFileName = options.inputWorkloadFileName;
541
542                if (options.inputFolder != null) {
543                        File f = null;
544                        if (options.inputTar != null) {
545                                f = new File(options.inputFolder + File.separator
546                                                + options.inputTar);
547                        } else {
548                                f = new File(options.inputFolder);
549                        }
550
551                        xmlJobReader = new Grms3XmlJobReader(f);
552
553                        wlFileName = options.inputFolder + File.separator
554                                        + options.inputWorkloadFileName;
555                }
556
557                swfReader = AbstractWAReader.getInstace(wlFileName);
558
559                WorkloadLoader workload = new WorkloadLoader(xmlJobReader, swfReader);
560                workload.load();
561
562                /*
563                 * if (options.createScenario) {
564                 * jobDescGenerator.generateWorkload(options.workloadDescFileName,
565                 * options.outputFolder, options.overwriteFiles,
566                 * options.outputWorkloadFileName);
567                 * jobDescGenerator.performPostprocessing(options.resdescFileName,
568                 * options.outputFolder, options.outputWorkloadFileName);
569                 * options.inputFolder = options.outputFolder;
570                 * options.inputWorkloadFileName = options.outputWorkloadFileName; }
571                 *
572                 * if(options.createXMLSupplement){
573                 * jobDescGenerator.generateXMLSupplement(options.workloadDescFileName,
574                 * options.inputWorkloadFileName, options.outputFolder,
575                 * options.overwriteFiles); }
576                 *
577                 * jobDescGenerator.loadWorkload(options.inputFolder,
578                 * options.inputWorkloadFileName);
579                 *
580                 *
581                 * if (!jobDescGenerator.isReady()) { System.err.println(
582                 * "Error: xml job genereator is not fully initialized. The simulation will terminate."
583                 * ); System.exit(1); }
584                 */
585                // BEGIN: Initializing the GridSim:
586                int numUser = 1; // the number of users in the experiment simulation
587                // (default 1)
588                // Date date = jobDescGenerator.getSimulationStartTime();
589                Date date = workload.getSimulationStartTime();
590                Calendar calendar = Calendar.getInstance();
591                if (date == null)
592                        calendar.setTimeInMillis(0L);
593                else
594                        calendar.setTime(date);
595
596                boolean traceFlag = true; // means: trace GridSim events/activities
597
598                String[] excludeFromFile = { "" }, excludeFromProcessing = { "" };
599                String reportFileName = GssimConstants.BrokerInterfaceEntityName;
600               
601                //GSSIM.init(numUser, calendar, traceFlag);
602                GridSim.init(numUser, calendar, traceFlag, excludeFromFile,
603                                excludeFromProcessing, "COMPUTING_GRID_0");
604                // END: Initializing the GridSim:
605
606                DateTimeUtilsExt.initVirtualTimeAccess(calendar);
607
608                // creating the broker interface entity
609                /*GridBroker brokerInterface = new GridBroker(
610                                GssimConstants.BrokerInterfaceEntityName, options, workload
611                                                .getJobCount(), workload.getTaskCount());*/
612
613                // creating the resources only after GridSim had been initialized
614                //AbstractComputingResource resources[] = null;
615                TempResourceReader resourceReader = new TempResourceReader(
616                                options);
617
618                //resources = new AbstractComputingResource[resourceReader
619                //              .getNoOfResources()];
620                //AbstractComputingResource resource = null;
621                //int i = 0;
622                //while ((resource = resourceReader.read()) != null
623                //              && i < resources.length) {
624                //      resources[i++] = resource;
625                //}
626
627                ResourceController rc = resourceReader.read();
628
629                GSSimUsersNew wl = new GSSimUsersNew("GridUsers",
630                                rc.getResources().getName(), workload);
631
632                // create the output folder
633                /*
634                 * File statsOutputPathFile = new File(statsOutputPath); if (!
635                 * statsOutputPathFile.exists()) { if (! statsOutputPathFile.mkdirs())
636                 * throw new
637                 * IOException("Cannot create the output (stats) path: "+statsOutputPath
638                 * +". Cause: "); }
639                 */
640
641                GridSim.startGridSimulation();
642                //GSSIM.startSimulation();
643                long stopSimulation = System.currentTimeMillis();
644
645                GSSimStatistics stats = new GSSimStatistics(simulationIdentifier,
646                                options, wl, statsOutputPath, rc);
647                accumulatedStatistics.add(stats);
648                if (log.isInfoEnabled())
649                        log.info("Generating statistics...");
650                stats.generateStatistics();
651
652                // Fill the statistical data
653                // simulationStatistics.setGenPEdiagram(false);
654                // simulationStatistics.gatherStatistics(wl, brokerInterface,
655                // resources);
656
657                // Print history
658                /*
659                 * simulationStatistics.printTasksInformation(new PrintStream( new
660                 * FileOutputStream( new File(statsOutputPath,
661                 * GssimConstants.tasksStatisitcsOutputFileName))),
662                 * options.printHistory);
663                 * simulationStatistics.printResourcesInformation(new PrintStream( new
664                 * FileOutputStream( new File(statsOutputPath,
665                 * GssimConstants.resourceStatisticsOutputFileName))));
666                 */
667
668                long duration = (stopSimulation - startSimulation) / 1000;
669                if (log.isInfoEnabled())
670                        log.info("The simulation run took " + duration + " seconds");
671
672                // Move output files to the output directory
673                String name = GridSim_stat;
674                String fileName = statsOutputPath + name;
675                if (!moveFile(name, fileName))
676                        if (log.isErrorEnabled())
677                                log.error("Error moving file " + fileName);
678
679                name = "sim_report";
680                fileName = statsOutputPath + name;
681                if (!moveFile(name, fileName))
682                        if (log.isErrorEnabled())
683                                log.error("Error moving file " + fileName);
684
685                name = "sim_trace";
686                fileName = statsOutputPath + name;
687                if (!moveFile(name, fileName))
688                        if (log.isErrorEnabled())
689                                log.error("Error moving file " + fileName);
690
691                // if necessary generate gifs from sjvg - need to rewrite the SJVG
692                // classes for public methods
693                if (log.isInfoEnabled())
694                        log.info(":: Finished simulation run: \"" + simulationIdentifier
695                                        + "\" ::");
696
697                System.out.flush();
698                System.err.flush();
699        }
700
701        /**
702         * Starts a single network simulation run.
703         *
704         * @param options
705         *            the configuration options according to which the simulation is
706         *            to be performed
707         * @param simulationStatistics
708         *            the statistics object that is to be filled after the
709         *            simulation run
710         * @throws Exception
711         *             any exception that may occur
712         */
713        public void performNetworkSimulation(String simulationIdentifier,
714                        ConfigurationOptions options) throws Exception {
715                // Startup of the random number generators must be set up with a random
716                // seed that is greater than zero
717                // if seed is not given then all consecutive simulations are the same
718                // if seed < 0 then the random numbers are equal to zero
719                Uniform uniform = new Uniform(new MersenneTwister64(new Date()));
720                // seed should be > 0 and fits to int size (which is also important)
721                long seed = uniform.nextLongFromTo(1, Integer.MAX_VALUE);
722                if (log.isDebugEnabled())
723                        log.debug("Shuffled initial seed: " + seed);
724
725                assert seed > 0 && seed <= Integer.MAX_VALUE : "Initial seed is <= 0, what is improper";
726                Sim_system.set_seed(seed);
727
728                long startSimulation = System.currentTimeMillis();
729                if (log.isInfoEnabled()) {
730                        log.info(":: Starting simulation run: \"" + simulationIdentifier
731                                        + "\" ::");
732                        log.info(":: In the the mode of ");
733                }
734                /*
735                 * String statsOutputPath = null; if (options.createScenario) { File
736                 * outputFolderFile = new File(options.outputFolder); if (!
737                 * outputFolderFile.exists()) { if (! outputFolderFile.mkdirs()) throw
738                 * new
739                 * IOException("Cannot create the output path: "+statsOutputPath+". Cause: "
740                 * ); }
741                 *
742                 * if(log.isInfoEnabled()) log.info("CREATING SCENARIO ::");
743                 * statsOutputPath = options.outputFolder +
744                 * options.statsOutputSubfolderNameCreate; } else {
745                 * if(log.isInfoEnabled()) log.info("READING SCENARIO ::"); String
746                 * prefix = null; if(options.inputFolder != null){ prefix =
747                 * options.inputFolder; }else if(options.inputWorkloadFileName != null){
748                 * prefix = new File(options.inputWorkloadFileName).getParent(); }else{
749                 * prefix = System.getProperty("user.dir"); } statsOutputPath = prefix +
750                 * "/" + options.statsOutputSubfolderNameRerad; } statsOutputPath +=
751                 * File.separator;
752                 */
753
754                Sim_system.set_trace_detail(true, true, true);
755                Sim_system.generate_graphs(statsOutputPath
756                                + GssimConstants.GraphsFileName);
757                Sim_system.set_trace_level(Integer.MAX_VALUE);
758
759                XMLJobReader<QcgJob> xmlJobReader = null;
760                WAReader<QcgJob> swfReader = null;
761                String wlFileName = options.inputWorkloadFileName;
762
763                if (options.inputFolder != null) {
764                        File f = new File(options.inputFolder);
765                        xmlJobReader = new Grms3XmlJobReader(f);
766                        if (f.isDirectory()) {
767                                wlFileName = options.inputFolder + File.separator
768                                                + options.inputWorkloadFileName;
769                        }
770                }
771
772                swfReader = AbstractWAReader.getInstace(wlFileName);
773
774                WorkloadLoader workload = new WorkloadLoader(xmlJobReader, swfReader);
775                workload.load();
776
777                /*
778                 * if (options.createScenario) {
779                 * jobDescGenerator.generateWorkload(options.workloadDescFileName,
780                 * options.outputFolder, options.overwriteFiles,
781                 * options.outputWorkloadFileName);
782                 * jobDescGenerator.performPostprocessing(options.resdescFileName,
783                 * options.outputFolder, options.outputWorkloadFileName);
784                 * options.inputFolder = options.outputFolder;
785                 * options.inputWorkloadFileName = options.outputWorkloadFileName; }
786                 *
787                 * if(options.createXMLSupplement){
788                 * jobDescGenerator.generateXMLSupplement(options.workloadDescFileName,
789                 * options.inputWorkloadFileName, options.outputFolder,
790                 * options.overwriteFiles); }
791                 *
792                 * jobDescGenerator.loadWorkload(options.inputFolder,
793                 * options.inputWorkloadFileName);
794                 *
795                 *
796                 * if (!jobDescGenerator.isReady()) { System.err.println(
797                 * "Error: xml job genereator is not fully initialized. The simulation will terminate."
798                 * ); System.exit(1); }
799                 */
800                // BEGIN: Initializing the GridSim:
801                int numUser = 1; // the number of users in the experiment simulation
802                // (default 1)
803                // Date date = jobDescGenerator.getSimulationStartTime();
804                Date date = workload.getSimulationStartTime();
805                Calendar calendar = Calendar.getInstance();
806                if (date == null)
807                        calendar.setTimeInMillis(0L);
808                else
809                        calendar.setTime(date);
810
811                boolean traceFlag = true; // means: trace GridSim events/activities
812
813                String[] excludeFromFile = { "" }, excludeFromProcessing = { "" };
814                String reportFileName = GssimConstants.BrokerInterfaceEntityName;
815
816                ExtendedGridSim
817                                .initNetworkType(ExtendedGridSimTags.RESERVATION_NET_FLOW_LEVEL);
818                ExtendedGridSimCore
819                                .initNetworkType(ExtendedGridSimTags.RESERVATION_NET_FLOW_LEVEL);
820                //ExtendedGridSim.init(numUser, calendar, traceFlag, excludeFromFile,
821                //              excludeFromProcessing, reportFileName);
822               
823                ExtendedGridSim.init(numUser, calendar, traceFlag, excludeFromFile,
824                                excludeFromProcessing, "COMPUTING_GRID_0");
825                // END: Initializing the GridSim:
826
827                DateTimeUtilsExt.initVirtualTimeAccess(calendar);
828
829                // creating the network broker interface entity
830                /*
831                 * NetworkGridBroker brokerInterface = new
832                 * NetworkGridBroker(GssimConstants.BrokerInterfaceEntityName, new
833                 * GSSIMFlowLink(GssimConstants.BrokerInterfaceEntityName + "_link",
834                 * Double .parseDouble("1")* 1000000, Double.parseDouble("300"),
835                 * Integer.parseInt("100000")), options, workload.getJobCount(),
836                 * workload.getTaskCount());
837                 */
838
839                // creating networktopology
840                Topology topology = GSSIMNetworkReader
841                                .createNetworkTopology(options.networkTopologyFileName);
842
843                // creating the network broker interface entity
844                /*NetworkGridBroker brokerInterface = new NetworkGridBroker(
845                                GssimConstants.BrokerInterfaceEntityName,
846                                ((GSSIMFlowLink) GSSIMNetworkReader.getBrokerLinkMapping().get(
847                                                GssimConstants.BrokerInterfaceEntityName)), topology,
848                                options, workload.getJobCount(), workload.getTaskCount());
849*/
850                AbstractComputingResource resources[] = null;
851        /*      ResourceReader resourceReader = new ResourceReader(
852                                options);
853
854                resources = new AbstractComputingResource[resourceReader
855                                .getNoOfResources()];
856                AbstractComputingResource resource = null;
857                int i = 0;
858                //while ((resource = resourceReader.read()) != null
859                //              && i < resources.length) {
860                //      resources[i++] = resource;
861                //}
862
863                AbstractResource res = resourceReader.close();
864
865                GSSimUsersNew wl = new GSSimUsersNew("GridUsers",
866                                res.getResourceName(), workload);
867
868*/
869                GSSIMNetworkReader.createResourceConnections();
870                /*
871                 * brokerInterface.networkManager.getTopology().printLinks();
872                 * brokerInterface.networkManager.getTopology().printResourcesNames();
873                 * brokerInterface.networkManager.getTopology().printRouters();
874                 * brokerInterface.networkManager.getTopology().printUsers();
875                 * List<DataLink> list=
876                 * brokerInterface.networkManager.getAbstractTopology().getLinks();
877                 * Iterator it=list.iterator(); while(it.hasNext()){ DataLink l =
878                 * (DataLink)it.next();
879                 * System.out.println(l.getId()+";"+l.getEndpoint1()
880                 * .getId()+";"+l.getEndpoint2().getId()); } List<Node> list2=
881                 * brokerInterface.networkManager.getAbstractTopology().getNodes();
882                 * Iterator it2=list2.iterator(); while(it2.hasNext()){ Node n =
883                 * (Node)it2.next();
884                 * System.out.println(n.getId()+";"+n.getType().toString()); }
885                 */
886
887
888                // create the output folder
889                /*
890                 * File statsOutputPathFile = new File(statsOutputPath); if (!
891                 * statsOutputPathFile.exists()) { if (! statsOutputPathFile.mkdirs())
892                 * throw new
893                 * IOException("Cannot create the output (stats) path: "+statsOutputPath
894                 * +". Cause: "); }
895                 */
896
897                ExtendedGridSim.startGridSimulation();
898                long stopSimulation = System.currentTimeMillis();
899
900                /*GSSimStatistics stats = new GSSimStatistics(simulationIdentifier,
901                                options, wl, null, resources, null, statsOutputPath);
902                accumulatedStatistics.add(stats);
903                if (log.isInfoEnabled())
904                        log.info("Generating statistics...");
905                stats.generateStatistics();*/
906
907                // Fill the statistical data
908                // GSSimStatistics simulationStatistics = new GSSimStatistics("old",
909                // options);
910                // simulationStatistics.setGenPEdiagram(false);
911                // simulationStatistics.gatherStatistics(wl, brokerInterface,
912                // resources);
913
914                // Print history
915                /*
916                 * simulationStatistics.printTasksInformation(new PrintStream( new
917                 * FileOutputStream( new File(statsOutputPath,
918                 * GssimConstants.tasksStatisitcsOutputFileName))),
919                 * options.printHistory);
920                 * simulationStatistics.printResourcesInformation(new PrintStream( new
921                 * FileOutputStream( new File(statsOutputPath,
922                 * GssimConstants.resourceStatisticsOutputFileName))));
923                 */
924
925                long duration = (stopSimulation - startSimulation) / 1000;
926                if (log.isInfoEnabled())
927                        log.info("The simulation run took " + duration + " seconds");
928
929                // Move output files to the output directory
930                String name = GridSim_stat;
931                String fileName = statsOutputPath + name;
932                if (!moveFile(name, fileName))
933                        if (log.isErrorEnabled())
934                                log.error("Error moving file " + fileName);
935
936                name = "sim_report";
937                fileName = statsOutputPath + name;
938                if (!moveFile(name, fileName))
939                        if (log.isErrorEnabled())
940                                log.error("Error moving file " + fileName);
941
942                name = "sim_trace";
943                fileName = statsOutputPath + name;
944                if (!moveFile(name, fileName))
945                        if (log.isErrorEnabled())
946                                log.error("Error moving file " + fileName);
947
948                // if necessary generate gifs from sjvg - need to rewrite the SJVG
949                // classes for public methods
950                if (log.isInfoEnabled())
951                        log.info(":: Finished simulation run: \"" + simulationIdentifier
952                                        + "\" ::");
953
954                System.out.flush();
955                System.err.flush();
956        }
957
958        /**
959         * Static method to move a file from given "from" location to "to" location
960         *
961         * @param from
962         *            from location
963         * @param to
964         *            to location
965         * @return true if the operation succeeded, false otherwise
966         */
967        public static boolean moveFile(String from, String to) {
968                File fromFile = new File(from);
969                File toFile = new File(to);
970                if (toFile.exists()) {
971                        if (!toFile.delete())
972                                return false;
973                }
974                if (!fromFile.renameTo(toFile)) {
975                        return false;
976                }
977                return true;
978        }
979
980        /**
981         * Removes the given directory no matter if it is empty or non empty. If the
982         * given parameter represents a file, it is not deleted. See the description
983         * of the return statement.
984         *
985         * @param dirPath
986         *            the file representing the directory to be deleted.
987         * @return true, if the given directory with all its contents have been
988         *         remove; false if deletion of any of files ended with error (all
989         *         other files are possibly also deleted)
990         */
991        public static boolean deleteDirectory(File dirPath) {
992                if (dirPath.exists() && dirPath.isDirectory()) {
993                        boolean result = true;
994                        File[] files = dirPath.listFiles();
995                        for (File file : files) {
996                                if (file.isFile())
997                                        result |= file.delete();
998                                else
999                                        result |= deleteDirectory(file);
1000                        }
1001                        result |= dirPath.delete();
1002                        return result;
1003                }
1004                return false; // it is not a directory
1005        }
1006
1007        /**
1008         * Returns the accumulated simulation statistics from the last run of the
1009         * simulator
1010         *
1011         * @return the accumulated simulation statistics from the last run of the
1012         *         simulator
1013         */
1014        public static AccumulatedStatistics getAccumulatedStatistics() {
1015                return accumulatedStatistics;
1016        }
1017
1018        protected class WorkloadDirectoryFilter implements java.io.FileFilter {
1019
1020                public boolean accept(File file) {
1021                        if (file.isDirectory() && file.getName().startsWith("workload"))
1022                                return true;
1023                        else
1024                                return false;
1025                }
1026
1027        }
1028
1029        protected class PropertiesFileFilter implements java.io.FileFilter {
1030
1031                public boolean accept(File file) {
1032                        if (file.isFile() && file.getName().endsWith(".properties"))
1033                                return true;
1034                        else
1035                                return false;
1036                }
1037
1038        }
1039
1040        public File prepareDirecotry(ConfigurationOptions options) throws Exception {
1041                // String statsOutputPath = null;
1042                if (options.createScenario) {
1043                        File outputFolderFile = new File(options.outputFolder);
1044                        if (!outputFolderFile.exists()) {
1045                                if (!outputFolderFile.mkdirs())
1046                                        throw new IOException("Cannot create the output path: "
1047                                                        + statsOutputPath + ". Cause: ");
1048                        }
1049
1050                        if (log.isInfoEnabled())
1051                                log.info("CREATING SCENARIO ::");
1052                        statsOutputPath = options.outputFolder
1053                                        + options.statsOutputSubfolderNameCreate;
1054                } else {
1055                        if (log.isInfoEnabled())
1056                                log.info("READING SCENARIO ::");
1057                        String prefix = null;
1058                        if (options.inputFolder != null) {
1059                                prefix = options.inputFolder;
1060                        } else if (options.inputWorkloadFileName != null) {
1061                                prefix = new File(options.inputWorkloadFileName).getParent();
1062                        } else {
1063                                prefix = System.getProperty("user.dir");
1064                        }
1065                        statsOutputPath = prefix + File.separator
1066                                        + options.statsOutputSubfolderNameRerad;
1067                }
1068                statsOutputPath += File.separator;
1069
1070                // create the output folder
1071                File statsOutputPathFile = new File(statsOutputPath);
1072                if (!statsOutputPathFile.exists()) {
1073                        if (!statsOutputPathFile.mkdirs())
1074                                throw new IOException("Cannot create the output (stats) path: "
1075                                                + statsOutputPath + ". Cause: ");
1076                }
1077
1078                File folder = new File(statsOutputPath);
1079                File fileList[] = folder.listFiles();
1080                for (int i = 0; i < fileList.length; i++) {
1081                        File tmpFile = fileList[i];
1082                        String name = tmpFile.getName();
1083                        if (name.length() > 4) {
1084                                String subName = name.substring(0, 5);
1085                                if (subName.compareTo("Chart") == 0
1086                                                || subName.compareTo("Stats") == 0) {
1087                                        tmpFile.delete();
1088                                }
1089                        }
1090                }
1091
1092                return statsOutputPathFile;
1093        }
1094
1095        public void createResultsTar(File outputTar, File[] files, File networkFile)
1096                        throws IOException {
1097
1098                if (!outputTar.exists())
1099                        outputTar.createNewFile();
1100                TarArchiveOutputStream tos = null;
1101                try {
1102                        tos = new TarArchiveOutputStream(new FileOutputStream(outputTar));
1103                        for (File file : files) {
1104                                if (file.exists())
1105                                        addEntryToArchive(tos, file);
1106                                else
1107                                        log.warn("File " + file.getName() + " does not exist");
1108                        }
1109                        if (networkFile != null)
1110                                addEntryWithParentsToArchive(tos, networkFile);
1111                } catch (FileNotFoundException e1) {
1112                        log.error("Archive output file was not found", e1);
1113                } finally {
1114                        if (tos != null)
1115                                try {
1116                                        tos.close();
1117                                } catch (IOException e) {
1118                                        log.error("IOException occured while "
1119                                                        + "closing tar archive", e);
1120                                }
1121                }
1122
1123        }
1124
1125        private void addEntryWithParentsToArchive(TarArchiveOutputStream tos,
1126                        File file) {
1127                File parent = file.getParentFile();
1128                if (parent != null)
1129                        addEntryWithParentsToArchive(tos, parent);
1130                String filePath = file.isDirectory() ? file.getPath() + File.separator
1131                                : file.getPath();
1132                addEntryToArchive(tos, file, filePath);
1133        }
1134
1135        private void addEntryToArchive(TarArchiveOutputStream tos, File file) {
1136                addEntryToArchive(tos, file, file.getName());
1137        }
1138
1139        private void addEntryToArchive(TarArchiveOutputStream tos, File file,
1140                        String name) {
1141                FileInputStream fis = null;
1142                TarArchiveEntry in = new TarArchiveEntry(name);
1143                in.setModTime(file.lastModified());
1144                if (!file.isDirectory())
1145                        in.setSize(file.length());
1146
1147                try {
1148                        tos.putArchiveEntry(in);
1149                        if (!file.isDirectory()) {
1150                                byte[] b = new byte[(int) file.length()];
1151                                fis = new FileInputStream(file);
1152                                while (fis.read(b) > 0) {
1153                                        tos.write(b);
1154                                }
1155                        }
1156                } catch (FileNotFoundException e) {
1157                        log.error("File " + file.getName() + " was not found", e);
1158                } catch (IOException e) {
1159                        log.error("IOException occured while adding file " + file.getName()
1160                                        + " to archive");
1161
1162                } finally {
1163                        try {
1164                                if (fis != null)
1165                                        fis.close();
1166                                tos.closeArchiveEntry();
1167                        } catch (IOException e) {
1168                                log.error("IOException occured while closing "
1169                                                + "archive input stream or entry", e);
1170                        }
1171
1172                }
1173
1174        }
1175
1176        public void copyFile(File inputFile, File outputFile) throws IOException {
1177
1178                FileReader in = new FileReader(inputFile);
1179                FileWriter out = new FileWriter(outputFile);
1180                int c;
1181
1182                while ((c = in.read()) != -1)
1183                        out.write(c);
1184
1185                in.close();
1186                out.close();
1187        }
1188       
1189        public static int getSimulationRunNumber(){
1190                return simulationRunNumber;
1191        }
1192}
Note: See TracBrowser for help on using the repository browser.