Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / src / bindings / java / org / simgrid / msg / Process.java
1 /* Copyright (c) 2006-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 package org.simgrid.msg;
7
8 import java.util.ArrayList;
9 import java.util.Arrays;
10
11 /**
12  * A process may be defined as a code, with some private data, executing
13  * in a location (host). All the process used by your simulation must be
14  * declared in the deployment file (XML format).
15  * To create your own process you must inherit your own process from this
16  * class and override the method "main()". For example if you want to use
17  * a process named Slave proceed as it :
18  *
19  * (1) import the class Process of the package simgrid.msg
20  * import simgrid.msg.Process;
21  *
22  * public class Slave extends simgrid.msg.Process {
23  *
24  *  (2) Override the method function
25  *
26  *  \verbatim
27  *      public void main(String[] args) {
28  *              System.out.println("Hello MSG");
29  *      }
30  *  \endverbatim
31  * }
32  * The name of your process must be declared in the deployment file of your simulation.
33  * For the example, for the previous process Slave this file must contains a line :
34  * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
35  * Slave. All the process of your simulation are automatically launched and managed by Msg.
36  * A process use tasks to simulate communications or computations with another process.
37  * For more information see Task. For more information on host concept
38  * see Host.
39  *
40  */
41
42 public abstract class Process implements Runnable {
43         /**
44          * This attribute represents a bind between a java process object and
45          * a native process. Even if this attribute is public you must never
46          * access to it. It is set automatically during the build of the object.
47          */
48         private long bind = 0;
49         /** Indicates if the process is started */
50
51         /** Time at which the process should be created  */
52         protected double startTime = 0;
53         /** Time at which the process should be killed */
54         private double killTime = -1; // Used from the C world
55
56         private String name = null;
57         
58         private int pid = -1;
59         private int ppid = -1;
60         private Host host = null;
61
62         /** The arguments of the method function of the process. */
63         private ArrayList<String> args = new ArrayList<>();
64
65         /**
66          * Constructs a new process from the name of a host and his name. The method
67          * function of the process doesn't have argument.
68          *
69          * @param hostname              Where to create the process.
70          * @param name                  The name of the process.
71          *
72          * @exception                   HostNotFoundException  if no host with this name exists.
73          *
74          *
75          */
76         public Process(String hostname, String name) throws HostNotFoundException {
77                 this(Host.getByName(hostname), name, null);
78         }
79         /**
80          * Constructs a new process from the name of a host and his name. The arguments
81          * of the method function of the process are specified by the parameter args.
82          *
83          * @param hostname              Where to create the process.
84          * @param name                  The name of the process.
85          * @param args                  The arguments of the main function of the process.
86          *
87          * @exception                   HostNotFoundException  if no host with this name exists.
88          *
89          */
90         public Process(String hostname, String name, String[] args) throws HostNotFoundException {
91                 this(Host.getByName(hostname), name, args);
92         }
93         /**
94          * Constructs a new process from a host and his name. The method function of the
95          * process doesn't have argument.
96          *
97          * @param host                  Where to create the process.
98          * @param name                  The name of the process.
99          *
100          */
101         public Process(Host host, String name) {
102                 this(host, name, null);
103         }
104         /**
105          * Constructs a new process from a host and his name, the arguments of here method function are
106          * specified by the parameter args.
107          *
108          * @param host                  Where to create the process.
109          * @param name                  The name of the process.
110          * @param argsParam             The arguments of main method of the process.
111          */     
112         public Process(Host host, String name, String[]argsParam)
113         {
114                 if (host == null)
115                         throw new IllegalArgumentException("Cannot create a process on the null host");
116                 if (name == null)
117                         throw new IllegalArgumentException("Process name cannot be null");
118                 
119                 this.host = host;
120                 this.name = name;
121
122                 this.args = new ArrayList<>();
123                 if (null != argsParam)
124                         this.args.addAll(Arrays.asList(argsParam));
125         }
126         /**
127          * Constructs a new process from a host and his name, the arguments of here method function are
128          * specified by the parameter args.
129          *
130          * @param host                  Where to create the process.
131          * @param name                  The name of the process.
132          * @param args                  The arguments of main method of the process.
133          * @param startTime             Start time of the process
134          * @param killTime              Kill time of the process
135          *
136          */
137         public Process(Host host, String name, String[]args, double startTime, double killTime) {
138                 this(host, name, args);
139                 this.startTime = startTime;
140                 this.killTime = killTime;
141         }
142         /**
143          * The native method to create an MSG process.
144          * @param host    where to create the process.
145          */
146         protected native void create(Host host);
147         
148         /**
149          * This method kills all running process of the simulation.
150          */
151         public static native void killAll();
152
153         /** Simply kills the receiving process.
154          *
155          * SimGrid sometimes have issues when you kill processes that are currently communicating and such. We are working on it to fix the issues.
156          */
157         public native void kill();
158         public static void kill(Process p) {
159                 p.kill();
160         }
161         
162         /** Suspends the process. See {@link #resume()} to resume it afterward */
163         public native void suspend();
164         /** Resume a process that was suspended by {@link #suspend()}. */
165         public native void resume();    
166         /** Tests if a process is suspended.
167          *
168          * @see #suspend()
169          * @see #resume()
170          */
171         public native boolean isSuspended();
172         
173         /** Yield the current process. All other processes that are ready at the same timestamp will be executed first */
174         public static native void yield();
175         
176         /**
177          * Specify whether the process should restart when its host restarts after a failure
178          *
179          * A process naturally stops when its host stops. It starts again only if autoRestart is set to true.
180          * Otherwise, it just disappears when the host stops.
181          */
182         public native void setAutoRestart(boolean autoRestart);
183         /** Restarts the process from the beginning */
184         public native void restart();
185         /**
186          * Returns the name of the process
187          */
188         public String getName() {
189                 return this.name;
190         }
191         /**
192          * Returns the host of the process.
193          * @return                              The host instance of the process.
194          */
195         public Host getHost() {
196                 return this.host;
197         }
198         /**
199          * This static method gets a process from a PID.
200          *
201          * @param pid                   The process identifier of the process to get.
202          *
203          * @return                              The process with the specified PID.
204          */
205         public static native Process fromPID(int pid);
206         /**
207          * This method returns the PID of the process.
208          *
209          * @return                              The PID of the process.
210          *
211          */
212         public int getPID()  {
213                 if (pid == -1) // Don't traverse the JNI barrier if you already have the answer
214                         pid = nativeGetPID();
215                 return pid;
216         }
217         // This should not be used: the PID is supposed to be initialized from the C directly when the actor is created,
218         // but this sometimes fail, so let's play nasty but safe here.
219         private native int nativeGetPID();
220         /**
221          * This method returns the PID of the parent of a process.
222          *
223          * @return                              The PID of the parent of the process.
224          *
225          */
226         public int getPPID()  {
227                 return ppid;
228         }
229         /**
230          * Returns the value of a given process property.
231          */
232         public native String getProperty(String name);
233
234         /**
235          * Set the kill time of the process
236          * @param killTime the time when the process is killed
237          */
238         public native void setKillTime(double killTime);
239
240         /**
241          * This static method returns the currently running process.
242          *
243          * @return                              The current process.
244          *
245          */
246         public static native Process getCurrentProcess();
247         /**
248          * Migrates a process to another host.
249          *
250          * @param host                  The host where to migrate the process.
251          *
252          */
253         public native void migrate(Host host);  
254         /**
255          * Makes the current process sleep until millis milliseconds have elapsed.
256          * You should note that unlike "waitFor" which takes seconds (as usual in SimGrid), this method takes milliseconds (as usual for sleep() in Java).
257          * 
258          * @param millis the length of time to sleep in milliseconds.
259          */
260         public static void sleep(long millis) throws HostFailureException  {
261                 sleep(millis,0);
262         }
263         /**
264          * Makes the current process sleep until millis milliseconds and nanos nanoseconds
265          * have elapsed.
266          * Unlike {@link #waitFor(double)} which takes seconds, this method takes
267          * milliseconds and nanoseconds.
268          * Overloads Thread.sleep.
269          * @param millis the length of time to sleep in milliseconds.
270          * @param nanos additional nanoseconds to sleep.
271          */
272         public static native void sleep(long millis, int nanos) throws HostFailureException;
273         /**
274          * Makes the current process sleep until time seconds have elapsed.
275          * @param seconds               The time the current process must sleep.
276          */
277         public native void waitFor(double seconds) throws HostFailureException;
278         /**
279          * This method actually creates and run the process.
280          * It is a noop if the process is already launched.
281          */
282         public final void start() {
283            if (bind == 0)
284              create(host);
285         }
286
287         /** This method runs the process. It calls the method function that you must overwrite. */
288         @Override
289         public void run() {
290
291                 try {
292                         String[] argsArray = new String[this.args.size()];
293                         this.args.toArray(argsArray);
294
295                         this.main(argsArray);
296                 }
297                 catch(MsgException e) {
298                         e.printStackTrace();
299                         Msg.info("Unexpected behavior. Stopping now");
300                         System.exit(1);
301                 }
302                 /* Let the ProcessKilledError (that we'd get if the process is forcefully killed) flow back to the caller */
303         }
304
305         /**
306          * The main function of the process (to implement by the user).
307          *
308          * @param args
309          * @throws MsgException
310          */
311         public abstract void main(String[]args) throws MsgException;
312
313         /** Stops the execution of the current actor */
314         public void exit() {
315                 this.kill();
316         }
317         /**
318          * Class initializer, to initialize various JNI stuff
319          */
320         private static native void nativeInit();
321         static {
322                 org.simgrid.NativeLib.nativeInit();
323                 nativeInit();
324         }
325         /**
326          * This static method returns the current amount of processes running
327          *
328          * @return                      The count of the running processes
329          */
330         public static native int getCount();
331
332         public static void debugAllThreads() {
333             // Search remaining threads that are not main nor daemon
334             for (Thread t : Thread.getAllStackTraces().keySet())
335                 if (! t.isDaemon() && !t.getName().equals("main"))
336                     System.err.println("Thread "+t.getName()+" is still running! Please report that bug");
337         }
338 }