Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / core / CommonState.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.core;
20
21 import org.simgrid.msg.Msg;
22
23 import peersim.Simulator;
24 import peersim.config.*;
25 import peersim.util.*;
26 import psgsim.PSGPlatform;
27
28 /**
29  * This is the common state of the simulation all objects see.
30  * Static singleton. One of its purposes is
31  * simplification of parameter structures and increasing efficiency by putting
32  * state information here instead of passing parameters.
33  *<p>
34  * <em>The set methods should not be used by applications</em>,
35  * they are for system
36  * components. Use them only if you know exactly what you are doing, e.g.
37  * if you are so advanced that you can write your own simulation engine.
38  * Ideally, they should not be visible, but due to the lack of more
39  * flexibility in java access rights, we are forced to make them public.
40  */
41 public class CommonState
42 {
43
44 //======================= constants ===============================
45 //=================================================================
46
47 /**
48 * Constant that can be used as a value of simulation phase.
49 * It means that the simulation has finished.
50 * @see #getPhase
51 */
52 public static final int POST_SIMULATION = 1;
53
54 /**
55 * Constant that can be used as a value of simulation phase.
56 * It means that the simulation phase information has not been set (unknown).
57 * @see #getPhase
58 */
59 public static final int PHASE_UNKNOWN = 0;
60
61 // ======================= fields ==================================
62 // =================================================================
63
64 /**
65  * Current time. Note that this value is simulator independent, all simulation
66  * models have a notion related to time. For example, in the cycle based model,
67  * the cycle id gives time, while in even driven simulations there is a more
68  * realistic notion of time.
69  */
70 private static long time = 0;
71
72 /**
73  * The maximal value {@link #time} can ever take.
74  */
75 private static long endtime = -1;
76
77 /**
78  * Number of used bits in the long representation of time, calculated
79  * based on the endtime.
80  */
81 private static int toshift = -1;
82
83 /**
84  * Information about where exactly the simulation is.
85  */
86 private static int phase = PHASE_UNKNOWN;
87
88 /**
89  * The current pid.
90  */
91 private static int pid;
92
93 /**
94  * The current node.
95  */
96 private static Node node;
97
98 /**
99 * This source of randomness should be used by all components.
100 * This field is public because it doesn't matter if it changes
101 * during an experiment (although it shouldn't) until no other sources of
102 * randomness are used within the system. Besides, we can save the cost
103 * of calling a wrapper method, which is important because this is needed
104 * very often.
105 */
106 public static ExtendedRandom r = null;
107
108
109 // ======================== initialization =========================
110 // =================================================================
111
112 /**
113 * Configuration parameter used to define which random generator
114 * class should be used. If not specified, the default implementation
115 * {@link ExtendedRandom} is used. User-specified random generators 
116 * must extend class {@link ExtendedRandom}. 
117 * @config
118 */
119 public static final String PAR_RANDOM = "random";
120
121 /**
122 * Configuration parameter used to initialize the random seed.
123 * If it is not specified the current time is used.
124 * @config
125 */
126 public static final String PAR_SEED = "random.seed";
127
128
129 /**
130 * Initializes the field {@link r} according to the configuration.
131 * Assumes that the configuration is already
132 * loaded.
133 */
134 static {
135         
136         long seed =
137                 Configuration.getLong(PAR_SEED,System.currentTimeMillis());
138         initializeRandom(seed);
139 }
140
141
142 /** Does nothing. To avoid construction but allow extension. */
143 protected CommonState() {}
144
145 // ======================= methods =================================
146 // =================================================================
147
148
149 /**
150  * Returns current time. In event-driven simulations, returns the current
151  * time (a long-value).
152  * In cycle-driven simulations, returns the current cycle (a long that
153  * can safely be cast into an integer).
154  */
155 public static long getTime()
156 {
157         /* if the engine simulator used is PSG (simId=2 */
158         if(Simulator.getSimID()==2)
159                 return (long) PSGPlatform.getTime();
160         else
161         return time;
162 }
163
164 //-----------------------------------------------------------------
165
166 /**
167  * Returns current time in integer format. The purpose is to enhance the
168  * performance of protocols (ints are smaller and faster) when absolute
169  * precision is not required. It assumes that endtime has been set via
170  * {@link #setEndTime} by the simulation engine. It uses the endtime for
171  * the optimal mapping to get the maximal precision.
172  * In particular, in the cycle
173  * based model, time is the same as cycle which can be safely cast into
174  * integer, so no precision is lost.
175  */
176 public static int getIntTime()
177 {
178         return (int)(time>>toshift);
179 }
180
181 //-----------------------------------------------------------------
182
183 /**
184  * Sets the current time. 
185  */
186 public static void setTime(long t)
187 {
188         time = t;
189 }
190
191 //-----------------------------------------------------------------
192
193 /**
194  * Returns endtime.
195  * It is the maximal value {@link #getTime} ever returns. If it's negative, it
196  * means the endtime is not known.
197  */
198 public static long getEndTime()
199 {
200         return endtime;
201 }
202
203 //-----------------------------------------------------------------
204
205 /**
206  * Sets the endtime. 
207  */
208 public static void setEndTime(long t)
209 {
210         if( endtime >= 0 )
211                 throw new RuntimeException("You can set endtime only once");
212         if( t < 0 )
213                 throw new RuntimeException("No negative values are allowed");
214                 
215         endtime = t;
216         toshift = 32-Long.numberOfLeadingZeros(t);
217         if( toshift<0 ) toshift = 0;
218 }
219
220 //-----------------------------------------------------------------
221
222 /**
223  * Returns the simulation phase. Currently the following phases are
224  * understood.
225  * <ul>
226  * <li>{@link #PHASE_UNKNOWN} phase is unknown</li>
227  * <li>{@link #POST_SIMULATION} the simulation is completed</li>
228  * </ul>
229  */
230 public static int getPhase()
231 {
232         return phase;
233 }
234
235 // -----------------------------------------------------------------
236
237 public static void setPhase(int p)
238 {
239         phase = p;
240 }
241
242 // -----------------------------------------------------------------
243
244 /**
245 * Returns the current protocol identifier. In other words, control is
246 * held by the indicated protocol on node {@link #getNode}.
247 */
248 public static int getPid()
249 {
250         return pid;
251 }
252
253 //-----------------------------------------------------------------
254
255 /** Sets the current protocol identifier.*/
256 public static void setPid(int p)
257 {
258         pid = p;
259 }
260
261 //-----------------------------------------------------------------
262
263 /**
264  * Returns the current node. When a protocol is executing, it is the node
265  * hosting the protocol.
266  */
267 public static Node getNode()
268 {
269         return node;
270 }
271
272 //-----------------------------------------------------------------
273
274 /** Sets the current node */
275 public static void setNode(Node n)
276 {
277         node = n;
278 }
279
280 //-----------------------------------------------------------------
281
282 public static void initializeRandom(long seed)
283 {
284         if (r == null) {
285                 r = (ExtendedRandom) Configuration.getInstance(PAR_RANDOM, new ExtendedRandom(seed));
286         }
287         r.setSeed(seed);
288 }
289
290 //-----------------------------------------------------------------
291
292 /*
293 public static void main(String pars[]) {
294         
295         setEndTime(Long.parseLong(pars[0]));
296         setTime(Long.parseLong(pars[1]));
297         System.err.println(getTime()+" "+getIntTime());
298 }
299 */
300 }
301