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.Vector;
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.
51 * Indicates if the process is started
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.
58 public static long nextProcessId = 0;
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.
67 /** Time at which the process should be created */
68 protected double startTime = 0;
70 * Kill time of the process
72 * Set at creation, and used internally by SimGrid
74 private double killTime = -1;
79 private int ppid = -1;
80 private Host host = null;
82 /** The arguments of the method function of the process. */
83 public Vector<String> args;
90 this.id = nextProcessId++;
93 this.args = new Vector<String>();
98 * Constructs a new process from the name of a host and his name. The method
99 * function of the process doesn't have argument.
101 * @param hostname The name of the host of the process to create.
102 * @param name The name of the process.
104 * @exception HostNotFoundException if no host with this name exists.
108 public Process(String hostname, String name) throws HostNotFoundException {
109 this(Host.getByName(hostname), name, null);
112 * Constructs a new process from the name of a host and his name. The arguments
113 * of the method function of the process are specified by the parameter args.
115 * @param hostname The name of the host of the process to create.
116 * @param name The name of the process.
117 * @param args The arguments of the main function of the process.
119 * @exception HostNotFoundException if no host with this name exists.
121 * @throws NativeException
124 public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
125 this(Host.getByName(hostname), name, args);
128 * Constructs a new process from a host and his name. The method function of the
129 * process doesn't have argument.
131 * @param host The host of the process to create.
132 * @param name The name of the process.
135 public Process(Host host, String name) {
136 this(host, name, null);
139 * Constructs a new process from a host and his name, the arguments of here method function are
140 * specified by the parameter args.
142 * @param host The host of the process to create.
143 * @param name The name of the process.
144 * @param args The arguments of main method of the process.
146 public Process(Host host, String name, String[]args) {
150 throw new NullPointerException("Process name cannot be NULL");
152 throw new NullPointerException("Process name cannot be NULL");
155 this.args = new Vector<String>();
157 this.args.addAll(Arrays.asList(args));
160 * Constructs a new process from a host and his name, the arguments of here method function are
161 * specified by the parameter args.
163 * @param host The host of the process to create.
164 * @param name The name of the process.
165 * @param args The arguments of main method of the process.
166 * @param startTime Start time of the process
167 * @param killTime Kill time of the process
170 public Process(Host host, String name, String[]args, double startTime, double killTime) {
174 throw new NullPointerException("Process name cannot be NULL");
176 throw new NullPointerException("Process name cannot be NULL");
179 this.args = new Vector<String>();
181 this.args.addAll(Arrays.asList(args));
183 this.startTime = startTime;
184 this.killTime = killTime;
187 * The natively implemented method to create an MSG process.
188 * @param hostName A valid (bound) host where create the process.
190 protected native void create(String hostName) throws HostNotFoundException;
192 * This method kills all running process of the simulation.
194 * @param resetPID Should we reset the PID numbers. A negative number means no reset
195 * and a positive number will be used to set the PID of the next newly
198 * @return The function returns the PID of the next created process.
201 public static native int killAll(int resetPID);
203 /** Simply kills the receiving process.
205 * SimGrid sometimes have issues when you kill processes that are currently communicating and such. We are working on it to fix the issues.
207 public native void kill();
209 /** Suspends the process. See {@link #resume()} to resume it afterward */
210 public native void suspend();
211 /** Resume a process that was suspended by {@link #suspend()}. */
212 public native void resume();
213 /** Tests if a process is suspended.
218 public native boolean isSuspended();
221 * Specify whether the process should restart when its host restarts after a failure
223 * A process naturally stops when its host stops. It starts again only if autoRestart is set to true.
224 * Otherwise, it just disappears when the host stops.
226 public native void setAutoRestart(boolean autoRestart);
227 /** Restarts the process from the beginning */
228 public native void restart();
230 * Returns the name of the process
232 public String getName() {
236 * Returns the host of the process.
237 * @return The host instance of the process.
239 public Host getHost() {
243 * This static method gets a process from a PID.
245 * @param PID The process identifier of the process to get.
247 * @return The process with the specified PID.
249 * @exception NativeException on error in the native SimGrid code
251 public static native Process fromPID(int PID) throws NativeException;
253 * This method returns the PID of the process.
255 * @return The PID of the process.
258 public int getPID() {
262 * This method returns the PID of the parent of a process.
264 * @return The PID of the parent of the process.
267 public int getPPID() {
271 * Returns the value of a given process property.
273 public native String getProperty(String name);
276 * Set the kill time of the process
277 * @param killTime the time when the process is killed
279 public native void setKillTime(double killTime);
282 * This static method returns the currently running process.
284 * @return The current process.
287 public static native Process getCurrentProcess();
289 * Migrates a process to another host.
291 * @param host The host where to migrate the process.
294 public native void migrate(Host host);
296 * Makes the current process sleep until millis millisecondes have elapsed.
297 * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
298 * FIXME: Not optimal, maybe we should have two native functions.
299 * @param millis the length of time to sleep in milliseconds.
301 public static void sleep(long millis) throws HostFailureException {
305 * Makes the current process sleep until millis milliseconds and nanos nanoseconds
307 * Unlike {@link #waitFor(double)} which takes seconds, this method takes
308 * milliseconds and nanoseconds.
309 * Overloads Thread.sleep.
310 * @param millis the length of time to sleep in milliseconds.
311 * @param nanos additionnal nanoseconds to sleep.
313 public native static void sleep(long millis, int nanos) throws HostFailureException;
315 * Makes the current process sleep until time seconds have elapsed.
316 * @param seconds The time the current process must sleep.
318 public native void waitFor(double seconds) throws HostFailureException;
320 * This method actually creates and run the process.
321 * It is a noop if the process is already launched.
322 * @throws HostNotFoundException
324 public final void start() throws HostNotFoundException {
327 create(host.getName());
332 * This method runs the process. Il calls the method function that you must overwrite.
336 String[] args = null; /* do not fill it before the signal or this.args will be empty */
337 //waitSignal(); /* wait for other people to fill the process in */
340 args = new String[this.args.size()];
341 if (this.args.size() > 0) {
342 this.args.toArray(args);
346 } catch(MsgException e) {
348 Msg.info("Unexpected behavior. Stopping now");
351 catch(ProcessKilledError pk) {
357 * The main function of the process (to implement).
360 * @throws MsgException
362 public abstract void main(String[]args) throws MsgException;
364 public native void exit();
366 * Class initializer, to initialize various JNI stuff
368 private static native void nativeInit();
370 org.simgrid.NativeLib.nativeInit();
374 * This static method returns the current amount of processes running
376 * @return The count of the running processes
378 public native static int getCount();