Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into tomerge
[simgrid.git] / contrib / psg / src / psgsim / PSGPlatform.java
1 package psgsim;
2
3 import java.io.*;
4 import java.util.Comparator;
5 import java.util.LinkedHashMap;
6 import java.util.Map;
7 import java.util.TreeMap;
8
9 import org.jdom2.*;
10 import org.jdom2.output.*;
11 import org.simgrid.msg.Host;
12 import org.simgrid.msg.Msg;
13
14 import peersim.config.Configuration;
15 import peersim.core.Control;
16 import peersim.core.Protocol;
17
18 /**
19  * A class store different configuration information for simulation. It creates
20  * the deployment file according to this informations.
21  * 
22  * @author Khaled Baati 26/10/2014
23  * @version version 1.1
24  */
25
26 public class PSGPlatform {
27
28         enum timeUnit {
29                 us, ms, sec;
30         }
31
32         /** unit of measure. **/
33         static int unit;
34
35         /** the clock. **/
36         static double time;
37
38         /** the default unit of measure **/
39         static final String sec = "sec";
40
41         /** All protocols defined in the configuration file. **/
42         static Protocol[] protocolsName;
43
44         /** A numeric identifier associated for each protocol. **/
45         static int[] pid;
46
47         /** List of hos.t **/
48         static Host[] hostList;
49
50         /** A collection map represents the Control and its associated step. **/
51         static Map<Control, Double> controlStepMap = new LinkedHashMap<Control, Double>();
52
53         /** A collection map represents the protocol and its associated pid. **/
54         static TreeMap<Protocol, Integer> protocolsPidsMap = new TreeMap<Protocol, Integer>(
55                         new Comparator<Protocol>() {
56                                 public int compare(Protocol p1, Protocol p2) {
57                                         return p1.toString().compareTo(p2.toString());
58                                 }
59                         });
60
61         /** A collection map represents all CDProtocol and its associated step **/
62         static TreeMap<Protocol, Double> cdProtocolsStepMap = new TreeMap<Protocol, Double>(
63                         new Comparator<Protocol>() {
64                                 public int compare(Protocol p1, Protocol p2) {
65                                         return p1.toString().compareTo(p2.toString());
66                                 }
67                         });
68         /** the default platform file **/
69         static final String platformFile = "platforms/psg.xml";
70
71         /** the deployment file **/
72         static final String deploymentFile = "deployment.xml";
73
74         static Element racine;
75         static Document document;
76         static boolean interfED = false;
77         static boolean interfCD = false;
78
79         /** Prepare the deployment file **/
80         static {
81                 DocType dtype = new DocType("platform",
82                                 "http://simgrid.gforge.inria.fr/simgrid.dtd");
83                 racine = new Element("platform");
84                 document = new Document(racine, dtype);
85                 Attribute version = new Attribute("version", "3");
86                 racine.setAttribute(version);
87         }
88
89         // ========================== methods ==================================
90         // =====================================================================
91
92         /**
93          * Convert PS unit time to Simgrid unit time
94          * 
95          * @param valeur
96          *            the value to convert
97          * @return time converted
98          */
99         public static double psToSgTime(long valeur) {
100                 timeUnit unit = unit();
101                 switch (unit) {
102                 case us:
103                         return ((double) valeur) / 1000000;
104                 case ms:
105                         return ((double) valeur) / 1000;
106                 default:
107                         return (double) valeur;
108
109                 }
110         }
111
112         /**
113          * Convert Simgrid unit time to PS unit time
114          * 
115          * @param valeur
116          *            the value to convert
117          * @return time converted
118          */
119         public static long sgToPsTime(double valeur) {
120                 timeUnit unit = unit();
121                 switch (unit) {
122                 case us:
123                         return (long) valeur * 1000000;
124                 case ms:
125                         return (long) valeur * 1000;
126                 default:
127                         return (long) valeur;
128
129                 }
130         }
131
132         /**
133          * 
134          * @return the duration of simulation.
135          */
136         public static long getDuration() {
137                 return Configuration.getLong("simulation.duration");
138         }
139
140         /**
141          * 
142          * @return PeerSim Time
143          */
144         public static long getTime() {
145                 return sgToPsTime(Msg.getClock());
146         }
147
148         /**
149          * 
150          * @return the Simgrid Clock
151          */
152         public static double getClock() {
153                 return Msg.getClock();
154         }
155
156         /**
157          * Load and run initializers.
158          */
159         public static void init() {
160                 Object[] inits = Configuration.getInstanceArray("init");
161                 String names[] = Configuration.getNames("init");
162                 for (int i = 0; i < inits.length; ++i) {
163                         System.err.println("- Running initializer " + names[i] + ": "
164                                         + inits[i].getClass().toString());
165                         ((Control) inits[i]).execute();
166                 }
167         }
168
169         /**
170          * Load all controls and stores them in {@link #controlStepMap} collection
171          * to be scheduled, and executed in {@link psgsim.PSGProcessController}.
172          */
173         public static void control() {
174                 // load controls
175                 String[] names = Configuration.getNames("control");
176                 Control control;
177                 for (int i = 0; i < names.length; ++i) {
178                         control = (Control) Configuration.getInstance(names[i]);
179                         Long stepControl = Configuration.getLong(names[i] + "." + "step");
180                         controlStepMap.put(control, psToSgTime(stepControl));
181                 }
182         }
183
184         /**
185          * Lookup all protocols in the configuration file
186          */
187         public static void protocols() {
188                 String[] names = Configuration.getNames("protocol");
189                 Class[] interfaces;
190                 protocolsName = new Protocol[names.length];
191                 pid = new int[names.length];
192                 boolean save = false;
193                 for (int i = 0; i < names.length; i++) {
194                         protocolsName[i] = (Protocol) Configuration.getInstance(names[i]);
195                         if (i == names.length - 1)
196                                 save = true;
197                         userProtocol(protocolsName[i], names[i], save);
198                         pid[i] = i;
199                         protocolsPidsMap.put(protocolsName[i], pid[i]);
200                 }
201
202         }
203
204         /**
205          * Lookup CDProtocol and EDProtocol among all protocols
206          * 
207          * @param prot
208          *            the protocol class
209          * @param names
210          *            the protocol name
211          * @param save
212          *            parameter equal true when parsing all protocols
213          */
214         public static void userProtocol(Protocol prot, String names, boolean save) {
215                 Class[] interfaces = prot.getClass().getInterfaces();
216                 for (int j = 0; j < interfaces.length; j++) {
217                         if (interfaces[j].getName().endsWith("EDProtocol")) {
218                                 interfED = true;
219                         }
220                         if (interfaces[j].getName().endsWith("CDProtocol")) {
221                                 String protName = names.substring("protocol".length() + 1);
222                                 long step = Configuration.getLong("protocol" + "." + protName
223                                                 + "." + "step");
224                                 cdProtocolsStepMap.put(prot, psToSgTime(step));
225                         }
226                 }
227                 if (save) {
228                         edProt();
229                 }
230         }
231
232         /**
233          * 
234          */
235         private static void edProt() {
236                 Host hostVal;
237                 hostList = Host.all();
238                 for (int i = 0; i < PSGSimulator.size; i++) {
239                         hostVal = hostList[i];
240                         Element process = new Element("process");
241                         racine.addContent(process);
242                         Attribute host = new Attribute("host", hostVal.getName());
243                         Attribute function = new Attribute("function",
244                                         "psgsim.PSGProcessEvent");
245                         process.setAttribute(host);
246                         process.setAttribute(function);
247                 }
248                 save(deploymentFile);
249
250         }
251
252         /**
253          * 
254          */
255         @Deprecated
256         private static void cdProt() {
257                 for (int i = 0; i < PSGSimulator.size; i++) {
258                         Element process = new Element("process");
259                         racine.addContent(process);
260                         Attribute host = new Attribute("host", String.valueOf(i));
261                         Attribute function = new Attribute("function",
262                                         "psgsim.PSGProcessCycle");
263                         process.setAttribute(host);
264                         process.setAttribute(function);
265
266                 }
267                 save("deployment.xml");
268
269         }
270
271         /**
272          * Reads given configuration property: "platform". If not found, returns the
273          * default value.
274          * 
275          * @return the platform file
276          */
277         public static String platformFile() {
278                 String defFile = platformFile;
279                 String file = Configuration.getString("platform", defFile);
280                 return file;
281         }
282
283         /**
284          * Reads given configuration property: "unit". If not found, returns the
285          * default value (ms).
286          * 
287          * @return the unit of measure
288          */
289         public static timeUnit unit() {
290                 String defUnit = sec;
291                 String unit = Configuration.getString("unit", defUnit);
292                 timeUnit t = timeUnit.valueOf(unit);
293                 return t;
294         }
295
296         /**
297          * Create the deployment file
298          * 
299          * @param file
300          *            the name of the deployment file
301          */
302         public static void save(String file) {
303                 try {
304                         // On utilise ici un affichage classique avec getPrettyFormat()
305                         XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
306                         out.output(document, new FileOutputStream(file));
307                 } catch (java.io.IOException e) {
308                 }
309         }
310
311         /**
312          * Delete the deployment file
313          * 
314          * @param path
315          *            the path of the deployment file
316          */
317         public static void delete(String path) {
318                 File file = new File(path);
319                 try {
320                         file.delete();
321                 } catch (Exception e) {
322                         System.err.println("deployment file not found");
323
324                 }
325                 System.err.println(path + "file deleted");
326
327         }
328
329 }