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 */
52 /** Time at which the process should be created */
53 protected double startTime = 0;
54 /** Time at which the process should be killed.
56 * Set at creation, and used internally by SimGrid
58 private double killTime = -1;
60 private String name = null;
63 private int ppid = -1;
64 private Host host = null;
66 /** The arguments of the method function of the process. */
67 private ArrayList<String> args = new ArrayList<>();
70 * Constructs a new process from the name of a host and his name. The method
71 * function of the process doesn't have argument.
73 * @param hostname Where to create the process.
74 * @param name The name of the process.
76 * @exception HostNotFoundException if no host with this name exists.
80 public Process(String hostname, String name) throws HostNotFoundException {
81 this(Host.getByName(hostname), name, null);
84 * Constructs a new process from the name of a host and his name. The arguments
85 * of the method function of the process are specified by the parameter args.
87 * @param hostname Where to create the process.
88 * @param name The name of the process.
89 * @param args The arguments of the main function of the process.
91 * @exception HostNotFoundException if no host with this name exists.
94 public Process(String hostname, String name, String[] args) throws HostNotFoundException {
95 this(Host.getByName(hostname), name, args);
98 * Constructs a new process from a host and his name. The method function of the
99 * process doesn't have argument.
101 * @param host Where to create the process.
102 * @param name The name of the process.
105 public Process(Host host, String name) {
106 this(host, name, null);
109 * Constructs a new process from a host and his name, the arguments of here method function are
110 * specified by the parameter args.
112 * @param host Where to create the process.
113 * @param name The name of the process.
114 * @param args The arguments of main method of the process.
116 public Process(Host host, String name, String[]args)
119 throw new NullPointerException("Cannot create a process on the null host");
121 throw new NullPointerException("Process name cannot be null");
126 this.args = new ArrayList<>();
128 this.args.addAll(Arrays.asList(args));
131 * Constructs a new process from a host and his name, the arguments of here method function are
132 * specified by the parameter args.
134 * @param host Where to create the process.
135 * @param name The name of the process.
136 * @param args The arguments of main method of the process.
137 * @param startTime Start time of the process
138 * @param killTime Kill time of the process
141 public Process(Host host, String name, String[]args, double startTime, double killTime) {
142 this(host, name, args);
143 this.startTime = startTime;
144 this.killTime = killTime;
147 * The native method to create an MSG process.
148 * @param host where to create the process.
150 protected native void create(Host host);
153 * This method kills all running process of the simulation.
155 * @param resetPID Should we reset the PID numbers. A negative number means no reset
156 * and a positive number will be used to set the PID of the next newly
159 * @return The function returns the PID of the next created process.
162 public static native int killAll(int resetPID);
164 /** Simply kills the receiving process.
166 * SimGrid sometimes have issues when you kill processes that are currently communicating and such. We are working on it to fix the issues.
168 public native void kill();
169 public static void kill(Process p) {
173 /** Suspends the process. See {@link #resume()} to resume it afterward */
174 public native void suspend();
175 /** Resume a process that was suspended by {@link #suspend()}. */
176 public native void resume();
177 /** Tests if a process is suspended.
182 public native boolean isSuspended();
184 /** Yield the current process. All other processes that are ready at the same timestamp will be executed first */
185 public static native void yield();
188 * Specify whether the process should restart when its host restarts after a failure
190 * A process naturally stops when its host stops. It starts again only if autoRestart is set to true.
191 * Otherwise, it just disappears when the host stops.
193 public native void setAutoRestart(boolean autoRestart);
194 /** Restarts the process from the beginning */
195 public native void restart();
197 * Returns the name of the process
199 public String getName() {
203 * Returns the host of the process.
204 * @return The host instance of the process.
206 public Host getHost() {
210 * This static method gets a process from a PID.
212 * @param pid The process identifier of the process to get.
214 * @return The process with the specified PID.
216 public static native Process fromPID(int pid);
218 * This method returns the PID of the process.
220 * @return The PID of the process.
223 public int getPID() {
227 * This method returns the PID of the parent of a process.
229 * @return The PID of the parent of the process.
232 public int getPPID() {
236 * Returns the value of a given process property.
238 public native String getProperty(String name);
241 * Set the kill time of the process
242 * @param killTime the time when the process is killed
244 public native void setKillTime(double killTime);
247 * This static method returns the currently running process.
249 * @return The current process.
252 public static native Process getCurrentProcess();
254 * Migrates a process to another host.
256 * @param host The host where to migrate the process.
259 public native void migrate(Host host);
261 * Makes the current process sleep until millis milliseconds have elapsed.
262 * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
263 * FIXME: Not optimal, maybe we should have two native functions.
264 * @param millis the length of time to sleep in milliseconds.
266 public static void sleep(long millis) throws HostFailureException {
270 * Makes the current process sleep until millis milliseconds and nanos nanoseconds
272 * Unlike {@link #waitFor(double)} which takes seconds, this method takes
273 * milliseconds and nanoseconds.
274 * Overloads Thread.sleep.
275 * @param millis the length of time to sleep in milliseconds.
276 * @param nanos additional nanoseconds to sleep.
278 public static native void sleep(long millis, int nanos) throws HostFailureException;
280 * Makes the current process sleep until time seconds have elapsed.
281 * @param seconds The time the current process must sleep.
283 public native void waitFor(double seconds) throws HostFailureException;
285 * This method actually creates and run the process.
286 * It is a noop if the process is already launched.
287 * @throws HostNotFoundException
289 public final void start() throws HostNotFoundException {
294 /** This method runs the process. It calls the method function that you must overwrite. */
298 String[] args = null; /* do not fill it before the signal or this.args will be empty */
301 args = new String[this.args.size()];
302 if (!this.args.isEmpty()) {
303 this.args.toArray(args);
308 catch(MsgException e) {
310 Msg.info("Unexpected behavior. Stopping now");
313 catch(ProcessKilledError pk) {
314 /* The process was killed before its end. With a kill() or something. */
319 * The main function of the process (to implement).
322 * @throws MsgException
324 public abstract void main(String[]args) throws MsgException;
326 /** Stops the execution of the current actor */
331 * Class initializer, to initialize various JNI stuff
333 private static native void nativeInit();
335 org.simgrid.NativeLib.nativeInit();
339 * This static method returns the current amount of processes running
341 * @return The count of the running processes
343 public static native int getCount();