Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
abfcef99d68ca86f54e7c58c223547643ca592d9
[simgrid.git] / src / bindings / java / org / simgrid / msg / Process.java
1 /* Copyright (c) 2006-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 package org.simgrid.msg;
8
9 import java.util.Arrays;
10 import java.util.Vector;
11
12 /**
13  * A process may be defined as a code, with some private data, executing 
14  * in a location (host). All the process used by your simulation must be
15  * declared in the deployment file (XML format).
16  * To create your own process you must inherit your own process from this 
17  * class and override the method "main()". For example if you want to use 
18  * a process named Slave proceed as it :
19  *
20  * (1) import the class Process of the package simgrid.msg
21  * import simgrid.msg.Process;
22  * 
23  * public class Slave extends simgrid.msg.Process {
24  *
25  *  (2) Override the method function
26  * 
27  *  \verbatim
28  *      public void main(String[] args) {
29  *              System.out.println("Hello MSG");
30  *      }
31  *  \endverbatim
32  * }
33  * The name of your process must be declared in the deployment file of your simulation.
34  * For the example, for the previous process Slave this file must contains a line :
35  * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
36  * Slave. All the process of your simulation are automatically launched and managed by Msg.
37  * A process use tasks to simulate communications or computations with another process. 
38  * For more information see Task. For more information on host concept 
39  * see Host.
40  * 
41  */
42
43 public abstract class Process implements Runnable {
44         /**
45          * This attribute represents a bind between a java process object and
46          * a native process. Even if this attribute is public you must never
47          * access to it. It is set automatically during the build of the object.
48          */
49         private long bind;
50         /**
51          * Indicates if the process is started
52          */
53         boolean started;
54         /**
55          * Even if this attribute is public you must never access to it.
56          * It is used to compute the id of an MSG process.
57          */
58         public static long nextProcessId = 0;
59
60         /**
61          * Even if this attribute is public you must never access to it.
62          * It is compute automatically during the creation of the object. 
63          * The native functions use this identifier to synchronize the process.
64          */
65         public long id;
66
67         /**
68          * Start time of the process
69          */
70         public double startTime = 0;
71         /**
72          * Kill time of the process
73          */
74         public double killTime = -1;
75
76         /**
77          * The name of the process.                                                     
78          */
79         protected String name;
80         /**
81          * The PID of the process
82          */
83         protected int pid = -1;
84         /**
85          * The PPID of the process 
86          */
87         protected int ppid = -1;
88         /**
89          * The host of the process
90          */
91         protected Host host = null;
92
93         /** The arguments of the method function of the process. */     
94         public Vector<String> args;
95
96
97         /**
98          * Default constructor
99          */
100         protected Process() {
101                 this.id = nextProcessId++;
102                 this.name = null;
103                 this.bind = 0;
104                 this.args = new Vector<String>();
105         }
106
107
108         /**
109          * Constructs a new process from the name of a host and his name. The method
110          * function of the process doesn't have argument.
111          *
112          * @param hostname              The name of the host of the process to create.
113          * @param name                  The name of the process.
114          *
115          * @exception                   HostNotFoundException  if no host with this name exists.
116          *                      
117          *
118          */
119         public Process(String hostname, String name) throws HostNotFoundException {
120                 this(Host.getByName(hostname), name, null);
121         }
122         /**
123          * Constructs a new process from the name of a host and his name. The arguments
124          * of the method function of the process are specified by the parameter args.
125          *
126          * @param hostname              The name of the host of the process to create.
127          * @param name                  The name of the process.
128          * @param args                  The arguments of the main function of the process.
129          *
130          * @exception                   HostNotFoundException  if no host with this name exists.
131          *                      NativeException
132          * @throws NativeException
133          *
134          */ 
135         public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
136                 this(Host.getByName(hostname), name, args);
137         }
138         /**
139          * Constructs a new process from a host and his name. The method function of the 
140          * process doesn't have argument.
141          *
142          * @param host                  The host of the process to create.
143          * @param name                  The name of the process.
144          *
145          */
146         public Process(Host host, String name) {
147                 this(host, name, null);
148         }
149         /**
150          * Constructs a new process from a host and his name, the arguments of here method function are
151          * specified by the parameter args.
152          *
153          * @param host                  The host of the process to create.
154          * @param name                  The name of the process.
155          * @param args                  The arguments of main method of the process.
156          */     
157         public Process(Host host, String name, String[]args) {
158                 this();
159                 this.host = host;
160                 if (name == null)
161                         throw new NullPointerException("Process name cannot be NULL");
162                 this.name = name;
163
164                 this.args = new Vector<String>();
165                 if (null != args)
166                         this.args.addAll(Arrays.asList(args));
167         }       
168         /**
169          * Constructs a new process from a host and his name, the arguments of here method function are
170          * specified by the parameter args.
171          *
172          * @param host                  The host of the process to create.
173          * @param name                  The name of the process.
174          * @param args                  The arguments of main method of the process.
175          * @param startTime             Start time of the process
176          * @param killTime              Kill time of the process
177          *
178          */
179         public Process(Host host, String name, String[]args, double startTime, double killTime) {
180                 this();
181                 this.host = host;
182                 if (name == null)
183                         throw new NullPointerException("Process name cannot be NULL");
184                 this.name = name;
185
186                 this.args = new Vector<String>();
187                 if (null != args)
188                         this.args.addAll(Arrays.asList(args));
189
190                 this.startTime = startTime;
191                 this.killTime = killTime;               
192         }
193         /**
194          * The natively implemented method to create an MSG process.
195          * @param hostName    A valid (bound) host where create the process.
196          */
197         protected native void create(String hostName) throws HostNotFoundException;
198         /**
199          * This method kills all running process of the simulation.
200          *
201          * @param resetPID              Should we reset the PID numbers. A negative number means no reset
202          *                                              and a positive number will be used to set the PID of the next newly
203          *                                              created process.
204          *
205          * @return                              The function returns the PID of the next created process.
206          *                      
207          */ 
208         public static native int killAll(int resetPID);
209
210         /**
211          * This method kill a process.
212          *
213          */
214         public native void kill();
215         /**
216          * Suspends the process by suspending the task on which it was
217          * waiting for the completion.
218          */
219         public native void suspend();
220         /**
221          * Suspends the process by suspending the task on which it was
222          * waiting for the completion.
223          * DEPRECATED: use suspend instead.
224          */
225         @Deprecated
226         public void pause() {
227                 suspend();
228         }
229         /**
230          * Sets the "auto-restart" flag of the process.
231          */
232         public native void setAutoRestart(boolean autoRestart);
233         /**
234          * Restarts the process from the beginning
235          */
236         public native void restart();
237         /**
238          * Resumes a suspended process by resuming the task on which it was
239          * waiting for the completion.
240          */
241         public native void resume();    
242         /**
243          * Tests if a process is suspended.
244          *
245          * @return                              The method returns true if the process is suspended.
246          *                                              Otherwise the method returns false.
247          */ 
248         public native boolean isSuspended();
249         /**
250          * Returns the name of the process
251          */
252         public String msgName() {
253                 return this.name;
254         }
255         /**
256          * Returns the host of the process.
257          * @return                              The host instance of the process.
258          */ 
259         public Host getHost() {
260                 return this.host;
261         }
262         /**
263          * This static method gets a process from a PID.
264          *
265          * @param PID                   The process identifier of the process to get.
266          *
267          * @return                              The process with the specified PID.
268          *
269          * @exception                   NativeException on error in the native SimGrid code
270          */ 
271         public static native Process fromPID(int PID) throws NativeException;
272         /**
273          * This method returns the PID of the process.
274          *
275          * @return                              The PID of the process.
276          *
277          */ 
278         public int getPID()  {
279                 return pid;
280         }
281         /**
282          * This method returns the PID of the parent of a process.
283          *
284          * @return                              The PID of the parent of the process.
285          *
286          */ 
287         public int getPPID()  {
288                 return ppid;
289         }
290         /**
291          * Returns the value of a given process property. 
292          */
293         public native String getProperty(String name);
294
295         /**
296          * Set the kill time of the process
297          * @param killTime the time when the process is killed
298          */
299         public native void setKillTime(double killTime);
300
301         /**
302          * This static method returns the currently running process.
303          *
304          * @return                              The current process.
305          *
306          */ 
307         public static native Process getCurrentProcess();
308         /**
309          * Migrates a process to another host.
310          *
311          * @param host                  The host where to migrate the process.
312          *
313          */
314         public native void migrate(Host host);  
315         /**
316          * Makes the current process sleep until millis millisecondes have elapsed.
317          * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
318          * FIXME: Not optimal, maybe we should have two native functions.
319          * @param millis the length of time to sleep in milliseconds.
320          */
321         public static void sleep(long millis) throws HostFailureException  {
322                 sleep(millis,0);
323         }
324         /**
325          * Makes the current process sleep until millis milliseconds and nanos nanoseconds 
326          * have elapsed.
327          * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds and nanoseconds.
328          * Overloads Thread.sleep.
329          * @param millis the length of time to sleep in milliseconds.
330          * @param nanos additionnal nanoseconds to sleep.
331          */
332         public native static void sleep(long millis, int nanos) throws HostFailureException;
333         /**
334          * Makes the current process sleep until time seconds have elapsed.
335          * @param seconds               The time the current process must sleep.
336          */ 
337         public native void waitFor(double seconds) throws HostFailureException;    
338         /**
339          *
340          */
341         public void showArgs() {
342                 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
343                                 this.args.size());
344                 for (int i = 0; i < this.args.size(); i++)
345                         Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
346                                         "] args[" + i + "]=" + this.args.get(i));
347         }
348         /**
349          * This method actually creates and run the process.
350          * It is a noop if the process is already launched.
351          * @throws HostNotFoundException
352          */
353         public final void start() throws HostNotFoundException {
354                 if (!started) {
355                         started = true;
356                         create(host.getName());
357                 }
358         }
359
360         /**
361          * This method runs the process. Il calls the method function that you must overwrite.
362          */
363         public void run() {
364
365                 String[] args = null;      /* do not fill it before the signal or this.args will be empty */
366                 //waitSignal(); /* wait for other people to fill the process in */
367
368                 try {
369                         args = new String[this.args.size()];
370                         if (this.args.size() > 0) {
371                                 this.args.toArray(args);
372                         }
373
374                         this.main(args);
375                 } catch(MsgException e) {
376                         e.printStackTrace();
377                         Msg.info("Unexpected behavior. Stopping now");
378                         System.exit(1);
379                 }
380                 catch(ProcessKilledError pk) {
381                 }       
382                 exit();
383         }
384
385         /**
386          * The main function of the process (to implement).
387          *
388          * @param args
389          * @throws MsgException
390          */
391         public abstract void main(String[]args) throws MsgException;
392
393         public native void exit();    
394         /**
395          * Class initializer, to initialize various JNI stuff
396          */
397         public static native void nativeInit();
398         static {
399                 Msg.nativeInit();
400                 nativeInit();
401         }
402         /**
403          * This static method returns the current amount of processes running
404          *
405          * @return                      The count of the running processes
406          */ 
407         public native static int getCount();
408
409 }