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 */
55 private double killTime = -1; // Used from the C world
57 private String name = null;
60 private int ppid = -1;
61 private Host host = null;
63 /** The arguments of the method function of the process. */
64 private ArrayList<String> args = new ArrayList<>();
67 * Constructs a new process from the name of a host and his name. The method
68 * function of the process doesn't have argument.
70 * @param hostname Where to create the process.
71 * @param name The name of the process.
73 * @exception HostNotFoundException if no host with this name exists.
77 public Process(String hostname, String name) throws HostNotFoundException {
78 this(Host.getByName(hostname), name, null);
81 * Constructs a new process from the name of a host and his name. The arguments
82 * of the method function of the process are specified by the parameter args.
84 * @param hostname Where to create the process.
85 * @param name The name of the process.
86 * @param args The arguments of the main function of the process.
88 * @exception HostNotFoundException if no host with this name exists.
91 public Process(String hostname, String name, String[] args) throws HostNotFoundException {
92 this(Host.getByName(hostname), name, args);
95 * Constructs a new process from a host and his name. The method function of the
96 * process doesn't have argument.
98 * @param host Where to create the process.
99 * @param name The name of the process.
102 public Process(Host host, String name) {
103 this(host, name, null);
106 * Constructs a new process from a host and his name, the arguments of here method function are
107 * specified by the parameter args.
109 * @param host Where to create the process.
110 * @param name The name of the process.
111 * @param args The arguments of main method of the process.
113 public Process(Host host, String name, String[]args)
116 throw new IllegalArgumentException("Cannot create a process on the null host");
118 throw new IllegalArgumentException("Process name cannot be null");
123 this.args = new ArrayList<>();
125 this.args.addAll(Arrays.asList(args));
128 * Constructs a new process from a host and his name, the arguments of here method function are
129 * specified by the parameter args.
131 * @param host Where to create the process.
132 * @param name The name of the process.
133 * @param args The arguments of main method of the process.
134 * @param startTime Start time of the process
135 * @param killTime Kill time of the process
138 public Process(Host host, String name, String[]args, double startTime, double killTime) {
139 this(host, name, args);
140 this.startTime = startTime;
141 this.killTime = killTime;
144 * The native method to create an MSG process.
145 * @param host where to create the process.
147 protected native void create(Host host);
150 * This method kills all running process of the simulation.
152 * @param resetPID Should we reset the PID numbers. A negative number means no reset
153 * and a positive number will be used to set the PID of the next newly
156 * @return The function returns the PID of the next created process.
159 public static native int killAll(int resetPID);
161 /** Simply kills the receiving process.
163 * SimGrid sometimes have issues when you kill processes that are currently communicating and such. We are working on it to fix the issues.
165 public native void kill();
166 public static void kill(Process p) {
170 /** Suspends the process. See {@link #resume()} to resume it afterward */
171 public native void suspend();
172 /** Resume a process that was suspended by {@link #suspend()}. */
173 public native void resume();
174 /** Tests if a process is suspended.
179 public native boolean isSuspended();
181 /** Yield the current process. All other processes that are ready at the same timestamp will be executed first */
182 public static native void yield();
185 * Specify whether the process should restart when its host restarts after a failure
187 * A process naturally stops when its host stops. It starts again only if autoRestart is set to true.
188 * Otherwise, it just disappears when the host stops.
190 public native void setAutoRestart(boolean autoRestart);
191 /** Restarts the process from the beginning */
192 public native void restart();
194 * Returns the name of the process
196 public String getName() {
200 * Returns the host of the process.
201 * @return The host instance of the process.
203 public Host getHost() {
207 * This static method gets a process from a PID.
209 * @param pid The process identifier of the process to get.
211 * @return The process with the specified PID.
213 public static native Process fromPID(int pid);
215 * This method returns the PID of the process.
217 * @return The PID of the process.
220 public int getPID() {
224 * This method returns the PID of the parent of a process.
226 * @return The PID of the parent of the process.
229 public int getPPID() {
233 * Returns the value of a given process property.
235 public native String getProperty(String name);
238 * Set the kill time of the process
239 * @param killTime the time when the process is killed
241 public native void setKillTime(double killTime);
244 * This static method returns the currently running process.
246 * @return The current process.
249 public static native Process getCurrentProcess();
251 * Migrates a process to another host.
253 * @param host The host where to migrate the process.
256 public native void migrate(Host host);
258 * Makes the current process sleep until millis milliseconds have elapsed.
259 * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
260 * FIXME: Not optimal, maybe we should have two native functions.
261 * @param millis the length of time to sleep in milliseconds.
263 public static void sleep(long millis) throws HostFailureException {
267 * Makes the current process sleep until millis milliseconds and nanos nanoseconds
269 * Unlike {@link #waitFor(double)} which takes seconds, this method takes
270 * milliseconds and nanoseconds.
271 * Overloads Thread.sleep.
272 * @param millis the length of time to sleep in milliseconds.
273 * @param nanos additional nanoseconds to sleep.
275 public static native void sleep(long millis, int nanos) throws HostFailureException;
277 * Makes the current process sleep until time seconds have elapsed.
278 * @param seconds The time the current process must sleep.
280 public native void waitFor(double seconds) throws HostFailureException;
282 * This method actually creates and run the process.
283 * It is a noop if the process is already launched.
284 * @throws HostNotFoundException
286 public final void start() throws HostNotFoundException {
291 /** This method runs the process. It calls the method function that you must overwrite. */
295 String[] args = null; /* do not fill it before the signal or this.args will be empty */
298 args = new String[this.args.size()];
299 if (!this.args.isEmpty()) {
300 this.args.toArray(args);
305 catch(MsgException e) {
307 Msg.info("Unexpected behavior. Stopping now");
310 catch(ProcessKilledError pk) {
311 /* The process was killed before its end. With a kill() or something. */
316 * The main function of the process (to implement).
319 * @throws MsgException
321 public abstract void main(String[]args) throws MsgException;
323 /** Stops the execution of the current actor */
328 * Class initializer, to initialize various JNI stuff
330 private static native void nativeInit();
332 org.simgrid.NativeLib.nativeInit();
336 * This static method returns the current amount of processes running
338 * @return The count of the running processes
340 public static native int getCount();