1 /* Copyright (c) 2006-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 package org.simgrid.msg;
9 import java.util.Arrays;
10 import java.util.ArrayList;
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 :
20 * (1) import the class Process of the package simgrid.msg
21 * import simgrid.msg.Process;
23 * public class Slave extends simgrid.msg.Process {
25 * (2) Override the method function
28 * public void main(String[] args) {
29 * System.out.println("Hello MSG");
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
43 public abstract class Process implements Runnable {
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.
49 private long bind = 0;
50 /** Indicates if the process is started */
53 * Even if this attribute is public you must never access to it.
54 * It is used to compute the id of an MSG process.
56 private static long nextProcessId = 0;
59 * Even if this attribute is public you must never access to it.
60 * It is compute automatically during the creation of the object.
61 * The native functions use this identifier to synchronize the process.
65 /** Time at which the process should be created */
66 protected double startTime = 0;
67 /** Time at which the process should be killed.
69 * Set at creation, and used internally by SimGrid
71 private double killTime = -1;
73 private String name = null;
76 private int ppid = -1;
77 private Host host = null;
79 /** The arguments of the method function of the process. */
80 private ArrayList<String> args = new ArrayList<>();
83 * Constructs a new process from the name of a host and his name. The method
84 * function of the process doesn't have argument.
86 * @param hostname Where to create the process.
87 * @param name The name of the process.
89 * @exception HostNotFoundException if no host with this name exists.
93 public Process(String hostname, String name) throws HostNotFoundException {
94 this(Host.getByName(hostname), name, null);
97 * Constructs a new process from the name of a host and his name. The arguments
98 * of the method function of the process are specified by the parameter args.
100 * @param hostname Where to create the process.
101 * @param name The name of the process.
102 * @param args The arguments of the main function of the process.
104 * @exception HostNotFoundException if no host with this name exists.
107 public Process(String hostname, String name, String[] args) throws HostNotFoundException {
108 this(Host.getByName(hostname), name, args);
111 * Constructs a new process from a host and his name. The method function of the
112 * process doesn't have argument.
114 * @param host Where to create the process.
115 * @param name The name of the process.
118 public Process(Host host, String name) {
119 this(host, name, null);
122 * Constructs a new process from a host and his name, the arguments of here method function are
123 * specified by the parameter args.
125 * @param host Where to create the process.
126 * @param name The name of the process.
127 * @param args The arguments of main method of the process.
129 public Process(Host host, String name, String[]args)
132 throw new NullPointerException("Cannot create a process on the null host");
134 throw new NullPointerException("Process name cannot be null");
136 this.id = nextProcessId++;
140 this.args = new ArrayList<>();
142 this.args.addAll(Arrays.asList(args));
145 * Constructs a new process from a host and his name, the arguments of here method function are
146 * specified by the parameter args.
148 * @param host Where to create the process.
149 * @param name The name of the process.
150 * @param args The arguments of main method of the process.
151 * @param startTime Start time of the process
152 * @param killTime Kill time of the process
155 public Process(Host host, String name, String[]args, double startTime, double killTime) {
156 this(host, name, args);
157 this.startTime = startTime;
158 this.killTime = killTime;
161 * The natively implemented method to create an MSG process.
162 * @param hostName A valid (bound) host where create the process.
164 protected native void create(String hostName) throws HostNotFoundException;
166 * This method kills all running process of the simulation.
168 * @param resetPID Should we reset the PID numbers. A negative number means no reset
169 * and a positive number will be used to set the PID of the next newly
172 * @return The function returns the PID of the next created process.
175 public static native int killAll(int resetPID);
177 /** Simply kills the receiving process.
179 * SimGrid sometimes have issues when you kill processes that are currently communicating and such. We are working on it to fix the issues.
181 public native void kill();
182 public static void kill(Process p) {
186 /** Suspends the process. See {@link #resume()} to resume it afterward */
187 public native void suspend();
188 /** Resume a process that was suspended by {@link #suspend()}. */
189 public native void resume();
190 /** Tests if a process is suspended.
195 public native boolean isSuspended();
197 /** Yield the current process. All other processes that are ready at the same timestamp will be executed first */
198 public static native void yield();
201 * Specify whether the process should restart when its host restarts after a failure
203 * A process naturally stops when its host stops. It starts again only if autoRestart is set to true.
204 * Otherwise, it just disappears when the host stops.
206 public native void setAutoRestart(boolean autoRestart);
207 /** Restarts the process from the beginning */
208 public native void restart();
210 * Returns the name of the process
212 public String getName() {
216 * Returns the host of the process.
217 * @return The host instance of the process.
219 public Host getHost() {
223 * This static method gets a process from a PID.
225 * @param pid The process identifier of the process to get.
227 * @return The process with the specified PID.
229 public static native Process fromPID(int pid);
231 * This method returns the PID of the process.
233 * @return The PID of the process.
236 public int getPID() {
240 * This method returns the PID of the parent of a process.
242 * @return The PID of the parent of the process.
245 public int getPPID() {
249 * Returns the value of a given process property.
251 public native String getProperty(String name);
254 * Set the kill time of the process
255 * @param killTime the time when the process is killed
257 public native void setKillTime(double killTime);
260 * This static method returns the currently running process.
262 * @return The current process.
265 public static native Process getCurrentProcess();
267 * Migrates a process to another host.
269 * @param host The host where to migrate the process.
272 public native void migrate(Host host);
274 * Makes the current process sleep until millis milliseconds have elapsed.
275 * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
276 * FIXME: Not optimal, maybe we should have two native functions.
277 * @param millis the length of time to sleep in milliseconds.
279 public static void sleep(long millis) throws HostFailureException {
283 * Makes the current process sleep until millis milliseconds and nanos nanoseconds
285 * Unlike {@link #waitFor(double)} which takes seconds, this method takes
286 * milliseconds and nanoseconds.
287 * Overloads Thread.sleep.
288 * @param millis the length of time to sleep in milliseconds.
289 * @param nanos additional nanoseconds to sleep.
291 public static native void sleep(long millis, int nanos) throws HostFailureException;
293 * Makes the current process sleep until time seconds have elapsed.
294 * @param seconds The time the current process must sleep.
296 public native void waitFor(double seconds) throws HostFailureException;
298 * This method actually creates and run the process.
299 * It is a noop if the process is already launched.
300 * @throws HostNotFoundException
302 public final void start() throws HostNotFoundException {
305 create(host.getName());
309 /** This method runs the process. It calls the method function that you must overwrite. */
313 String[] args = null; /* do not fill it before the signal or this.args will be empty */
316 args = new String[this.args.size()];
317 if (!this.args.isEmpty()) {
318 this.args.toArray(args);
323 catch(MsgException e) {
325 Msg.info("Unexpected behavior. Stopping now");
328 catch(ProcessKilledError pk) {
329 /* The process was killed before its end. With a kill() or something. */
334 * The main function of the process (to implement).
337 * @throws MsgException
339 public abstract void main(String[]args) throws MsgException;
341 /** Stops the execution of the current actor */
346 * Class initializer, to initialize various JNI stuff
348 private static native void nativeInit();
350 org.simgrid.NativeLib.nativeInit();
354 * This static method returns the current amount of processes running
356 * @return The count of the running processes
358 public static native int getCount();