2 * Copyright (c) 2003-2005 The BISON Project
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.
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.
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.
21 import psgsim.PSGSimulator;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
27 import org.simgrid.msg.HostNotFoundException;
28 import org.simgrid.msg.NativeException;
30 import peersim.cdsim.*;
31 import peersim.config.*;
32 import peersim.core.*;
33 import peersim.edsim.*;
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:
41 * <li>{@link CDSimulator}: if {@link CDSimulator#isConfigurationCycleDriven}
43 * <li>{@link EDSimulator}: if {@link EDSimulator#isConfigurationEventDriven}
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.
53 public class Simulator {
55 // ========================== static constants ==========================
56 // ======================================================================
57 public static int nbreR = 0;
58 /** {@link CDSimulator} */
59 public static final int CDSIM = 0;
61 /** {@link EDSimulator} */
62 public static final int EDSIM = 1;
64 /** {@link psgsim.PSGSimulator} */
65 public static final int PSGSIM = 2;
67 /** Unknown simulator */
68 public static final int UNKNOWN = -1;
70 /** the class names of simulators used */
71 protected static final String[] simName = { "peersim.cdsim.CDSimulator",
72 "peersim.edsim.EDSimulator", "psgsim.PSGSimulator" };
75 * Parameter representing the number of times the experiment is run.
80 public static final String PAR_EXPS = "simulation.experiments";
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.
89 public static final String PAR_REDIRECT = "simulation.stdout";
91 // ==================== static fields ===================================
92 // ======================================================================
95 private static int simID = UNKNOWN;
97 // ========================== methods ===================================
98 // ======================================================================
101 * Returns the numeric id of the simulator to invoke. At the moment this can
102 * be {@link #CDSIM}, {@link #EDSIM} or {@link #UNKNOWN}.
104 public static int getSimID() {
106 if (simID == UNKNOWN) {
107 if (CDSimulator.isConfigurationCycleDriven()) {
109 } else if (EDSimulator.isConfigurationEventDriven()) {
118 // ----------------------------------------------------------------------
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.
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.
132 * After loading the configuration, the experiments are run by invoking the
133 * appropriate engine, which is identified as follows:
135 * <li>{@link CDSimulator}: if
136 * {@link CDSimulator#isConfigurationCycleDriven} returns true</li>
137 * <li>{@link EDSimulator}: if
138 * {@link EDSimulator#isConfigurationEventDriven} returns true</li>
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.
148 * {@link ParsedProperties#ParsedProperties(String[])}
149 * @throws InterruptedException
150 * @throws HostNotFoundException
151 * @see ParsedProperties
156 public static void main(String[] args) throws InterruptedException,
157 HostNotFoundException {
158 long time = System.currentTimeMillis();
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);
167 int exps = Configuration.getInt(PAR_EXPS, 1);
168 final int SIMID = getSimID();
169 if (SIMID == UNKNOWN) {
171 .println("Simulator: unable to determine simulation engine type");
177 for (int k = 0; k < exps; ++k) {
179 long seed = CommonState.r.nextLong();
180 CommonState.initializeRandom(seed);
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.
191 CDSimulator.nextExperiment();
195 start = System.currentTimeMillis();
196 EDSimulator.nextExperiment();
197 System.err.print("Duration of Simulation in ps:"
198 + (System.currentTimeMillis() - start) + " ms\n");
203 start = System.currentTimeMillis();
205 System.err.print("Duration of Simulation in psg:"
206 + (System.currentTimeMillis() - start)
208 } catch (NativeException e) {
210 .println("***********Native exception***************");
217 } catch (MissingParameterException e) {
218 System.err.println(e + "");
220 } catch (IllegalParameterException e) {
221 System.err.println(e + "");
225 // undocumented testing capabilities
226 if (Configuration.contains("__t"))
227 System.out.println(System.currentTimeMillis() - time);
228 if (Configuration.contains("__x"))
237 public static void log(String sim) {
238 String propName = "OutputName";
240 /** le nom de l'OS */
241 final String OS_NAME = System.getProperty("os.name");
243 String prot = Configuration.getString(propName, "null");
244 if (prot.contentEquals("null")) {
245 System.err.println("OutputName parameter not defined");
247 if ("Linux".equals(OS_NAME) || "Mac".equals(OS_NAME)) {
248 if (!new File("outputs" + prot).exists()) {
249 new File("outputs/" + prot).mkdirs();
251 String path = "outputs/" + prot + "/";
252 file = new File(path + sim + ".txt");
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");
260 PrintStream printStream = new PrintStream(file);
261 System.setOut(printStream);
262 // System.setErr(printStream);
263 } catch (FileNotFoundException e) {