2 * Copyright 2006-2012 The SimGrid team
5 * This program is free software; you can redistribute
6 * it and/or modify it under the terms of the license
7 * (GNU LGPL) which comes with this package.
10 package org.simgrid.msg;
12 import java.util.Arrays;
13 import java.util.Vector;
16 * A process may be defined as a code, with some private data, executing
17 * in a location (host). All the process used by your simulation must be
18 * declared in the deployment file (XML format).
19 * To create your own process you must inherit your own process from this
20 * class and override the method "main()". For example if you want to use
21 * a process named Slave proceed as it :
23 * (1) import the class Process of the package simgrid.msg
24 * import simgrid.msg.Process;
26 * public class Slave extends simgrid.msg.Process {
28 * (2) Override the method function
31 * public void main(String[] args) {
32 * System.out.println("Hello MSG");
36 * The name of your process must be declared in the deployment file of your simulation.
37 * For the example, for the previous process Slave this file must contains a line :
38 * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
39 * Slave. All the process of your simulation are automatically launched and managed by Msg.
40 * A process use tasks to simulate communications or computations with another process.
41 * For more information see Task. For more information on host concept
46 public abstract class Process implements Runnable {
48 * This attribute represents a bind between a java process object and
49 * a native process. Even if this attribute is public you must never
50 * access to it. It is set automatically during the build of the object.
54 * Indicates if the process is started
58 * Even if this attribute is public you must never access to it.
59 * It is used to compute the id of an MSG process.
61 public static long nextProcessId = 0;
64 * Even if this attribute is public you must never access to it.
65 * It is compute automatically during the creation of the object.
66 * The native functions use this identifier to synchronize the process.
71 * Start time of the process
73 public double startTime = 0;
75 * Kill time of the process
77 public double killTime = -1;
80 * The name of the process.
82 protected String name;
84 * The PID of the process
86 protected int pid = -1;
88 * The PPID of the process
90 protected int ppid = -1;
92 * The host of the process
94 protected Host host = null;
96 /** The arguments of the method function of the process. */
97 public Vector<String> args;
101 * Default constructor
103 protected Process() {
104 this.id = nextProcessId++;
107 this.args = new Vector<String>();
112 * Constructs a new process from the name of a host and his name. The method
113 * function of the process doesn't have argument.
115 * @param hostname The name of the host of the process to create.
116 * @param name The name of the process.
118 * @exception HostNotFoundException if no host with this name exists.
122 public Process(String hostname, String name) throws HostNotFoundException {
123 this(Host.getByName(hostname), name, null);
126 * Constructs a new process from the name of a host and his name. The arguments
127 * of the method function of the process are specified by the parameter args.
129 * @param hostname The name of the host of the process to create.
130 * @param name The name of the process.
131 * @param args The arguments of the main function of the process.
133 * @exception HostNotFoundException if no host with this name exists.
135 * @throws NativeException
138 public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
139 this(Host.getByName(hostname), name, args);
142 * Constructs a new process from a host and his name. The method function of the
143 * process doesn't have argument.
145 * @param host The host of the process to create.
146 * @param name The name of the process.
149 public Process(Host host, String name) {
150 this(host, name, null);
153 * Constructs a new process from a host and his name, the arguments of here method function are
154 * specified by the parameter args.
156 * @param host The host of the process to create.
157 * @param name The name of the process.
158 * @param args The arguments of main method of the process.
160 public Process(Host host, String name, String[]args) {
164 throw new NullPointerException("Process name cannot be NULL");
167 this.args = new Vector<String>();
169 this.args.addAll(Arrays.asList(args));
172 * Constructs a new process from a host and his name, the arguments of here method function are
173 * specified by the parameter args.
175 * @param host The host of the process to create.
176 * @param name The name of the process.
177 * @param args The arguments of main method of the process.
178 * @param startTime Start time of the process
179 * @param killTime Kill time of the process
182 public Process(Host host, String name, String[]args, double startTime, double killTime) {
186 throw new NullPointerException("Process name cannot be NULL");
189 this.args = new Vector<String>();
191 this.args.addAll(Arrays.asList(args));
193 this.startTime = startTime;
194 this.killTime = killTime;
197 * The natively implemented method to create an MSG process.
198 * @param hostName A valid (bound) host where create the process.
200 protected native void create(String hostName) throws HostNotFoundException;
202 * This method kills all running process of the simulation.
204 * @param resetPID Should we reset the PID numbers. A negative number means no reset
205 * and a positive number will be used to set the PID of the next newly
208 * @return The function returns the PID of the next created process.
211 public static native int killAll(int resetPID);
214 * This method kill a process.
217 public native void kill();
219 * Suspends the process by suspending the task on which it was
220 * waiting for the completion.
222 public native void suspend();
224 * Suspends the process by suspending the task on which it was
225 * waiting for the completion.
226 * DEPRECATED: use suspend instead.
229 public void pause() {
233 * Sets the "auto-restart" flag of the process.
235 public native void setAutoRestart(boolean autoRestart);
237 * Restarts the process from the beginning
239 public native void restart();
241 * Resumes a suspended process by resuming the task on which it was
242 * waiting for the completion.
244 public native void resume();
246 * Tests if a process is suspended.
248 * @return The method returns true if the process is suspended.
249 * Otherwise the method returns false.
251 public native boolean isSuspended();
253 * Returns the name of the process
255 public String msgName() {
259 * Returns the host of the process.
260 * @return The host instance of the process.
262 public Host getHost() {
266 * This static method gets a process from a PID.
268 * @param PID The process identifier of the process to get.
270 * @return The process with the specified PID.
272 * @exception NativeException on error in the native SimGrid code
274 public static native Process fromPID(int PID) throws NativeException;
276 * This method returns the PID of the process.
278 * @return The PID of the process.
281 public int getPID() {
285 * This method returns the PID of the parent of a process.
287 * @return The PID of the parent of the process.
290 public int getPPID() {
294 * Returns the value of a given process property.
296 public native String getProperty(String name);
299 * Set the kill time of the process
300 * @param killTime the time when the process is killed
302 public native void setKillTime(double killTime);
305 * This static method returns the currently running process.
307 * @return The current process.
310 public static native Process currentProcess();
312 * Migrates a process to another host.
314 * @param host The host where to migrate the process.
317 public native void migrate(Host host);
319 * Makes the current process sleep until millis millisecondes have elapsed.
320 * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
321 * FIXME: Not optimal, maybe we should have two native functions.
322 * @param millis the length of time to sleep in milliseconds.
324 public static void sleep(long millis) throws HostFailureException {
328 * Makes the current process sleep until millis milliseconds and nanos nanoseconds
330 * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds and nanoseconds.
331 * Overloads Thread.sleep.
332 * @param millis the length of time to sleep in milliseconds.
333 * @param nanos additionnal nanoseconds to sleep.
335 public native static void sleep(long millis, int nanos) throws HostFailureException;
337 * Makes the current process sleep until time seconds have elapsed.
338 * @param seconds The time the current process must sleep.
340 public native void waitFor(double seconds) throws HostFailureException;
344 public void showArgs() {
345 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
347 for (int i = 0; i < this.args.size(); i++)
348 Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
349 "] args[" + i + "]=" + this.args.get(i));
352 * This method actually creates and run the process.
353 * It is a noop if the process is already launched.
354 * @throws HostNotFoundException
356 public final void start() throws HostNotFoundException {
359 create(host.getName());
364 * This method runs the process. Il calls the method function that you must overwrite.
368 String[] args = null; /* do not fill it before the signal or this.args will be empty */
369 //waitSignal(); /* wait for other people to fill the process in */
372 args = new String[this.args.size()];
373 if (this.args.size() > 0) {
374 this.args.toArray(args);
378 } catch(MsgException e) {
380 Msg.info("Unexpected behavior. Stopping now");
383 catch(ProcessKilledError pk) {
390 * The main function of the process (to implement).
393 * @throws MsgException
395 public abstract void main(String[]args) throws MsgException;
397 public native void exit();
399 * Class initializer, to initialize various JNI stuff
401 public static native void nativeInit();
407 * This static method returns the current amount of processes running
409 * @return The count of the running processes
411 public native static int getCount();