Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Energy, onHostDestruction: ensured ptr existence
[simgrid.git] / contrib / psg / src / peersim / Simulator.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  */
18
19 package peersim;
20
21 import psgsim.PSGSimulator;
22
23 import java.io.*;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26
27 import org.simgrid.msg.HostNotFoundException;
28 import org.simgrid.msg.NativeException;
29
30 import peersim.cdsim.*;
31 import peersim.config.*;
32 import peersim.core.*;
33 import peersim.edsim.*;
34
35 /**
36  * This is the main entry point to peersim. This class loads configuration and
37  * detects the simulation type. According to this, it invokes the appropriate
38  * simulator. The known simulators at this moment, along with the way to detect
39  * them are the following:
40  * <ul>
41  * <li>{@link CDSimulator}: if {@link CDSimulator#isConfigurationCycleDriven}
42  * returns true</li>
43  * <li>{@link EDSimulator}: if {@link EDSimulator#isConfigurationEventDriven}
44  * returns true</li>
45  * </ul>
46  * This list represents the order in which these alternatives are checked. That
47  * is, if more than one return true, then the first will be taken. Note that
48  * this class checks only for these clues and does not check if the
49  * configuration is consistent or valid.
50  * 
51  * @see #main
52  */
53 public class Simulator {
54
55         // ========================== static constants ==========================
56         // ======================================================================
57         public static int nbreR = 0;
58         /** {@link CDSimulator} */
59         public static final int CDSIM = 0;
60
61         /** {@link EDSimulator} */
62         public static final int EDSIM = 1;
63
64         /** {@link psgsim.PSGSimulator} */
65         public static final int PSGSIM = 2;
66
67         /** Unknown simulator */
68         public static final int UNKNOWN = -1;
69
70         /** the class names of simulators used */
71         protected static final String[] simName = { "peersim.cdsim.CDSimulator",
72                         "peersim.edsim.EDSimulator", "psgsim.PSGSimulator" };
73
74         /**
75          * Parameter representing the number of times the experiment is run.
76          * Defaults to 1.
77          * 
78          * @config
79          */
80         public static final String PAR_EXPS = "simulation.experiments";
81
82         /**
83          * If present, this parameter activates the redirection of the standard
84          * output to a given PrintStream. This comes useful for processing the
85          * output of the simulation from within the simulator.
86          * 
87          * @config
88          */
89         public static final String PAR_REDIRECT = "simulation.stdout";
90
91         // ==================== static fields ===================================
92         // ======================================================================
93
94         /** */
95         private static int simID = UNKNOWN;
96
97         // ========================== methods ===================================
98         // ======================================================================
99
100         /**
101          * Returns the numeric id of the simulator to invoke. At the moment this can
102          * be {@link #CDSIM}, {@link #EDSIM} or {@link #UNKNOWN}.
103          */
104         public static int getSimID() {
105
106                 if (simID == UNKNOWN) {
107                         if (CDSimulator.isConfigurationCycleDriven()) {
108                                 simID = CDSIM;
109                         } else if (EDSimulator.isConfigurationEventDriven()) {
110                                 simID = EDSIM;
111                         } else
112                                 simID = PSGSIM;
113
114                 }
115                 return simID;
116         }
117
118         // ----------------------------------------------------------------------
119
120         /**
121          * Loads the configuration and executes the experiments. The number of
122          * independent experiments is given by config parameter {@value #PAR_EXPS}.
123          * In all experiments the configuration is the same, only the random seed is
124          * not re-initialized between experiments.
125          * <p>
126          * Loading the configuration is currently done with the help of constructing
127          * an instance of {@link ParsedProperties} using the constructor
128          * {@link ParsedProperties#ParsedProperties(String[])}. The parameter
129          * <code>args</code> is simply passed to this class. This class is then used
130          * to initialize the configuration.
131          * <p>
132          * After loading the configuration, the experiments are run by invoking the
133          * appropriate engine, which is identified as follows:
134          * <ul>
135          * <li>{@link CDSimulator}: if
136          * {@link CDSimulator#isConfigurationCycleDriven} returns true</li>
137          * <li>{@link EDSimulator}: if
138          * {@link EDSimulator#isConfigurationEventDriven} returns true</li>
139          * </ul>
140          * <p>
141          * This list represents the order in which these alternatives are checked.
142          * That is, if more than one return true, then the first will be taken. Note
143          * that this class checks only for these clues and does not check if the
144          * configuration is consistent or valid.
145          * 
146          * @param args
147          *            passed on to
148          *            {@link ParsedProperties#ParsedProperties(String[])}
149          * @throws InterruptedException
150          * @throws HostNotFoundException
151          * @see ParsedProperties
152          * @see Configuration
153          * @see CDSimulator
154          * @see EDSimulator
155          */
156         public static void main(String[] args) throws InterruptedException,
157                         HostNotFoundException {
158                 long time = System.currentTimeMillis();
159                 long start;
160                 System.err.println("Simulator: loading configuration");
161                 Configuration.setConfig(new ParsedProperties(args));
162                 PrintStream newout = (PrintStream) Configuration.getInstance(
163                                 PAR_REDIRECT, System.out);
164                 if (newout != System.out)
165                         System.setOut(newout);
166
167                 int exps = Configuration.getInt(PAR_EXPS, 1);
168                 final int SIMID = getSimID();
169                 if (SIMID == UNKNOWN) {
170                         System.err
171                                         .println("Simulator: unable to determine simulation engine type");
172                         return;
173                 }
174
175                 try {
176
177                         for (int k = 0; k < exps; ++k) {
178                                 if (k > 0) {
179                                         long seed = CommonState.r.nextLong();
180                                         CommonState.initializeRandom(seed);
181                                 }
182                                 System.err.print("Simulator: starting experiment " + k);
183                                 System.err.println(" invoking " + simName[SIMID]);
184                                 System.err.println("Random seed: "
185                                                 + CommonState.r.getLastSeed());
186                                 // XXX could be done through reflection, but
187                                 // this is easier to read.
188
189                                 switch (SIMID) {
190                                 case CDSIM:
191                                         CDSimulator.nextExperiment();
192                                         break;
193                                 case EDSIM:
194                                         log("ps");
195                                         start = System.currentTimeMillis();
196                                         EDSimulator.nextExperiment();
197                                         System.err.print("Duration of Simulation in ps:"
198                                                         + (System.currentTimeMillis() - start) + " ms\n");
199                                         break;
200                                 case PSGSIM:
201                                         try {
202                                                 log("psg");
203                                                 start = System.currentTimeMillis();
204                                                 PSGSimulator.main();
205                                                 System.err.print("Duration of Simulation in psg:"
206                                                                 + (System.currentTimeMillis() - start)
207                                                                 + " ms\n");
208                                         } catch (NativeException e) {
209                                                 System.err
210                                                                 .println("***********Native exception***************");
211                                                 e.printStackTrace();
212                                         }
213                                         break;
214                                 }
215                         }
216
217                 } catch (MissingParameterException e) {
218                         System.err.println(e + "");
219                         System.exit(1);
220                 } catch (IllegalParameterException e) {
221                         System.err.println(e + "");
222                         System.exit(1);
223                 }
224
225                 // undocumented testing capabilities
226                 if (Configuration.contains("__t"))
227                         System.out.println(System.currentTimeMillis() - time);
228                 if (Configuration.contains("__x"))
229                         Network.test();
230
231         }
232
233         /**
234          * 
235          * @param sim
236          */
237         public static void log(String sim) {
238                 String propName = "OutputName";
239
240                 /** le nom de l'OS */
241                 final String OS_NAME = System.getProperty("os.name");
242                 File file = null;
243                 String prot = Configuration.getString(propName, "null");
244                 if (prot.contentEquals("null")) {
245                         System.err.println("OutputName parameter not defined");
246                 } else {
247                         if ("Linux".equals(OS_NAME) || "Mac".equals(OS_NAME)) {
248                                 if (!new File("outputs" + prot).exists()) {
249                                         new File("outputs/" + prot).mkdirs();
250                                 }
251                                 String path = "outputs/" + prot + "/";
252                                 file = new File(path + sim + ".txt");
253                         } else {
254                                 if (!new File("outputs" + prot).exists())
255                                         new File("outputs\\" + prot).mkdirs();
256                                 String path = "outputs\\" + prot + "\\";
257                                 file = new File(path + sim + ".txt");
258                         }
259                         try {
260                                 PrintStream printStream = new PrintStream(file);
261                                 System.setOut(printStream);
262                                 // System.setErr(printStream);
263                         } catch (FileNotFoundException e) {
264                                 e.printStackTrace();
265                         }
266                 }
267
268         }
269
270 }