4 * Copyright 2006,2007 Martin Quinson, Malek Cherier
7 * This program is free software; you can redistribute
8 * it and/or modify it under the terms of the license
9 *(GNU LGPL) which comes with this package.
12 package org.simgrid.msg;
14 import java.util.Arrays;
15 import java.util.Hashtable;
16 import java.util.Vector;
19 * A process may be defined as a code, with some private data, executing
20 * in a location (host). All the process used by your simulation must be
21 * declared in the deployment file (XML format).
22 * To create your own process you must inherit your own process from this
23 * class and override the method "main()". For example if you want to use
24 * a process named Slave proceed as it :
26 * (1) import the class Process of the package simgrid.msg
27 * import simgrid.msg.Process;
29 * public class Slave extends simgrid.msg.Process {
31 * (2) Override the method function
34 * public void main(String[] args) {
35 * System.out.println("Hello MSG");
39 * The name of your process must be declared in the deployment file of your simulation.
40 * For the example, for the previous process Slave this file must contains a line :
41 * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
42 * Slave. All the process of your simulation are automatically launched and managed by Msg.
43 * A process use tasks to simulate communications or computations with another process.
44 * For more information see Task. For more information on host concept
49 public abstract class Process extends Thread {
51 * This attribute represents a bind between a java process object and
52 * a native process. Even if this attribute is public you must never
53 * access to it. It is set automatically during the build of the object.
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.
73 public Hashtable<String,String> properties;
76 * The name of the process.
78 protected String name;
80 * The PID of the process
82 protected int pid = -1;
84 * The PPID of the process
86 protected int ppid = -1;
88 * The host of the process
90 protected Host host = null;
95 public String msgName() {
98 /** The arguments of the method function of the process. */
99 public Vector<String> args;
101 /* process synchronization tools */
108 protected Sem schedBegin, schedEnd;
109 private boolean nativeStop = false;
112 * Default constructor (used in ApplicationHandler to initialize it)
114 protected Process() {
116 this.id = nextProcessId++;
119 this.args = new Vector<String>();
120 this.properties = null;
121 schedBegin = new Sem(0);
122 schedEnd = new Sem(0);
127 * Constructs a new process from the name of a host and his name. The method
128 * function of the process doesn't have argument.
130 * @param hostname The name of the host of the process to create.
131 * @param name The name of the process.
133 * @exception HostNotFoundException if no host with this name exists.
137 public Process(String hostname, String name) throws HostNotFoundException {
138 this(Host.getByName(hostname), name, null);
141 * Constructs a new process from the name of a host and his name. The arguments
142 * of the method function of the process are specified by the parameter args.
144 * @param hostname The name of the host of the process to create.
145 * @param name The name of the process.
146 * @param args The arguments of the main function of the process.
148 * @exception HostNotFoundException if no host with this name exists.
150 * @throws NativeException
153 public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
154 this(Host.getByName(hostname), name, args);
157 * Constructs a new process from a host and his name. The method function of the
158 * process doesn't have argument.
160 * @param host The host of the process to create.
161 * @param name The name of the process.
164 public Process(Host host, String name) {
165 this(host, name, null);
168 * Constructs a new process from a host and his name, the arguments of here method function are
169 * specified by the parameter args.
171 * @param host The host of the process to create.
172 * @param name The name of the process.
173 * @param args The arguments of main method of the process.
176 public Process(Host host, String name, String[]args) {
177 /* This is the constructor called by all others */
181 throw new NullPointerException("Process name cannot be NULL");
184 this.args = new Vector<String>();
186 this.args.addAll(Arrays.asList(args));
189 MsgNative.processCreate(this, host.getName());
190 } catch (HostNotFoundException e) {
191 throw new RuntimeException("The impossible happend (yet again): the host that I have were not found",e);
198 * This method kills all running process of the simulation.
200 * @param resetPID Should we reset the PID numbers. A negative number means no reset
201 * and a positive number will be used to set the PID of the next newly
204 * @return The function returns the PID of the next created process.
207 public static int killAll(int resetPID) {
208 return MsgNative.processKillAll(resetPID);
212 * This method sets a flag to indicate that this thread must be killed. End user must use static method kill
217 public void nativeStop()
222 * getter for the flag that indicates that this thread must be killed
227 public boolean getNativeStop()
233 * This method kill a process.
234 * @param process the process to be killed.
239 Msg.info("Process " + msgName() + " will be killed.");
244 * Suspends the process by suspending the task on which it was
245 * waiting for the completion.
248 public void pause() {
249 MsgNative.processSuspend(this);
252 * Resumes a suspended process by resuming the task on which it was
253 * waiting for the completion.
257 public void restart() {
258 MsgNative.processResume(this);
261 * Tests if a process is suspended.
263 * @return The method returns true if the process is suspended.
264 * Otherwise the method returns false.
266 public boolean isSuspended() {
267 return MsgNative.processIsSuspended(this);
270 * Returns the host of a process.
272 * @return The host instance of the process.
276 public Host getHost() {
277 if (this.host == null) {
278 this.host = MsgNative.processGetHost(this);
283 * This static method gets a process from a PID.
285 * @param PID The process identifier of the process to get.
287 * @return The process with the specified PID.
289 * @exception NativeException on error in the native SimGrid code
291 public static Process fromPID(int PID) throws NativeException {
292 return MsgNative.processFromPID(PID);
295 * This method returns the PID of the process.
297 * @return The PID of the process.
300 public int getPID() {
302 pid = MsgNative.processGetPID(this);
307 * This method returns the PID of the parent of a process.
309 * @return The PID of the parent of the process.
312 public int getPPID() {
314 ppid = MsgNative.processGetPPID(this);
319 * This static method returns the currently running process.
321 * @return The current process.
324 public static Process currentProcess() {
325 return MsgNative.processSelf();
328 * Migrates a process to another host.
330 * @param process The process to migrate.
331 * @param host The host where to migrate the process.
334 public static void migrate(Process process, Host host) {
335 MsgNative.processMigrate(process, host);
339 * Makes the current process sleep until time seconds have elapsed.
341 * @param seconds The time the current process must sleep.
343 * @exception HostFailureException on error in the native SimGrid code
345 public static void waitFor(double seconds) throws HostFailureException {
346 MsgNative.processWaitFor(seconds);
351 public void showArgs() {
352 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
354 for (int i = 0; i < this.args.size(); i++)
355 Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
356 "] args[" + i + "]=" + (String) (this.args.get(i)));
359 * Let the simulated process sleep for the given amount of millisecond in the simulated world.
361 * You don't want to use sleep instead, because it would freeze your simulation
362 * run without any impact on the simulated world.
365 public native void simulatedSleep(double seconds);
368 * This method runs the process. Il calls the method function that you must overwrite.
372 String[]args = null; /* do not fill it before the signal or this.args will be empty */
374 //waitSignal(); /* wait for other people to fill the process in */
378 schedBegin.acquire();
379 } catch(InterruptedException e) {
383 args = new String[this.args.size()];
384 if (this.args.size() > 0) {
385 this.args.toArray(args);
389 MsgNative.processExit(this);
391 } catch(MsgException e) {
393 Msg.info("Unexpected behavior. Stopping now");
396 catch(ProcessKilled pk) {
399 MsgNative.processExit(this);
400 } catch (ProcessKilled pk2) {
401 /* Ignore that other exception that *will* occur all the time.
402 * This is because the C mechanic gives the control to the now-killed process
403 * so that it does some garbage collecting on its own. When it does so here,
404 * the Java thread checks when starting if it's supposed to be killed (to inform
405 * the C world). To avoid the infinite loop or anything similar, we ignore that
406 * exception now. This should be ok since we ignore only a very specific exception
407 * class and not a generic (such as any RuntimeException).
409 System.err.println(currentThread().getName()+": I ignore that other exception");
411 Msg.info(" Process " + ((Process) Thread.currentThread()).msgName() + " has been killed.");
415 pk.printStackTrace();
416 Msg.info("Unexpected behavior. Stopping now");
423 * The main function of the process (to implement).
426 * @throws MsgException
428 public abstract void main(String[]args) throws MsgException;
431 /** @brief Gives the control from the given user thread back to the maestro
433 * schedule() and unschedule() are the basis of interactions between the user threads
434 * (executing the user code), and the maestro thread (executing the platform models to decide
435 * which user thread should get executed when. Once it decided which user thread should be run
436 * (because the blocking action it were blocked onto are terminated in the simulated world), the
437 * maestro passes the control to this uthread by calling uthread.schedule() in the maestro thread
438 * (check its code for the simple semaphore-based synchronization schema).
440 * The uthread executes (while the maestro is blocked), until it starts another blocking
441 * action, such as a communication or so. In that case, uthread.unschedule() gets called from
444 * As other complications, these methods are called directly by the C through a JNI upcall in
445 * response to the JNI downcalls done by the Java code. For example, you have this (simplified)
447 * - a process calls the Task.send() method in java
448 * - this calls Java_org_simgrid_msg_MsgNative_taskSend() in C through JNI
449 * - this ends up calling jprocess_unschedule(), still in C
450 * - this calls the java method "org/simgrid/msg/Process/unschedule()V" through JNI
451 * - that is to say, the unschedule() method that you are reading the documentation of.
453 * To understand all this, you must keep in mind that there is no difference between the C thread
454 * describing a process, and the Java thread doing the same. Most of the time, they are system
455 * threads from the kernel anyway. In the other case (such as when using green java threads when
456 * the OS does not provide any thread feature), I'm unsure of what happens: it's a very long time
457 * that I didn't see any such OS.
459 * The synchronization itself is implemented using simple semaphores in Java, as you can see by
460 * checking the code of these functions (and run() above). That's super simple, and thus welcome
461 * given the global complexity of the synchronization architecture: getting C and Java cooperate
462 * with regard to thread handling in a portable manner is very uneasy. A simple and straightforward
463 * implementation of each synchronization point is precious.
465 * But this kinda limits the system scalability. It may reveal difficult to simulate dozens of
466 * thousands of processes this way, both for memory limitations and for hard limits pushed by the
467 * system on the amount of threads and semaphores (we have 2 semaphores per user process).
469 * At time of writing, the best source of information on how to simulate large systems within the
470 * Java bindings of simgrid is here: http://tomp2p.net/dev/simgrid/
473 public void unschedule() {
474 /* this function is called from the user thread only */
477 /* unlock the maestro before going to sleep */
479 /* Here, the user thread is locked, waiting for the semaphore, and maestro executes instead */
480 schedBegin.acquire();
481 /* now that the semaphore is acquired, it means that maestro gave us the control back */
483 /* the user thread is starting again after giving the control to maestro.
484 * Let's check if we were asked to die in between */
485 if ( (Thread.currentThread() instanceof Process) &&((Process) Thread.currentThread()).getNativeStop()) {
486 throw new ProcessKilled();
489 } catch (InterruptedException e) {
490 /* ignore this exception because this is how we get killed on process.kill or end of simulation.
491 * I don't like hiding exceptions this way, but fail to see any other solution
497 /** @brief Gives the control from the maestro back to the given user thread
499 * Must be called from the maestro thread -- see unschedule() for details.
502 public void schedule() {
504 /* unlock the user thread before going to sleep */
505 schedBegin.release();
506 /* Here, maestro is locked, waiting for the schedEnd semaphore to get signaled by used thread, that executes instead */
508 /* Maestro now has the control back and the user thread went to sleep gently */
510 } catch(InterruptedException e) {
511 throw new RuntimeException("The impossible did happend once again: I got interrupted in schedEnd.acquire()",e);
515 /** Send the given task in the mailbox associated with the specified alias (waiting at most given time)
519 * @throws TimeoutException
520 * @throws HostFailureException
521 * @throws TransferFailureException */
522 public void taskSend(String mailbox, Task task, double timeout) throws TransferFailureException, HostFailureException, TimeoutException {
523 MsgNative.taskSend(mailbox, task, timeout);
526 /** Send the given task in the mailbox associated with the specified alias
529 * @throws TimeoutException
530 * @throws HostFailureException
531 * @throws TransferFailureException */
532 public void taskSend(String mailbox, Task task) throws TransferFailureException, HostFailureException, TimeoutException {
533 MsgNative.taskSend(mailbox, task, -1);
536 /** Receive a task on mailbox associated with the specified mailbox
539 * @throws TransferFailureException
540 * @throws HostFailureException
541 * @throws TimeoutException
543 public Task taskReceive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
544 return MsgNative.taskReceive(mailbox, -1.0, null);
547 /** Receive a task on mailbox associated with the specified alias (waiting at most given time)
551 * @throws TransferFailureException
552 * @throws HostFailureException
553 * @throws TimeoutException
555 public Task taskReceive(String mailbox, double timeout) throws TransferFailureException, HostFailureException, TimeoutException {
556 return MsgNative.taskReceive(mailbox, timeout, null);
559 /** Receive a task on mailbox associated with the specified alias from given sender
564 * @throws TransferFailureException
565 * @throws HostFailureException
566 * @throws TimeoutException
568 public Task taskReceive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
569 return MsgNative.taskReceive(mailbox, timeout, host);
572 /** Receive a task on mailbox associated with the specified alias from given sender
576 * @throws TransferFailureException
577 * @throws HostFailureException
578 * @throws TimeoutException
580 public Task taskReceive(String mailbox, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
581 return MsgNative.taskReceive(mailbox, -1.0, host);