Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ensures that the native libraries are always loaded before trying to access them
[simgrid.git] / org / simgrid / msg / Process.java
index 454566e..edde2bf 100644 (file)
@@ -1,18 +1,15 @@
 /*
- * $Id$
- *
- * Copyright 2006,2007 Martin Quinson, Malek Cherier           
+ * Copyright 2006-2012 The SimGrid team
  * All right reserved. 
  *
  * This program is free software; you can redistribute 
  * it and/or modify it under the terms of the license 
- *(GNU LGPL) which comes with this package. 
+ * (GNU LGPL) which comes with this package.
  */
 
 package org.simgrid.msg;
  
 import java.util.Arrays;
-import java.util.Hashtable;
 import java.util.Vector;
 
 /**
@@ -46,67 +43,68 @@ import java.util.Vector;
  * 
  */
 
-public abstract class Process extends Thread {
+public abstract class Process implements Runnable {
        /**
         * This attribute represents a bind between a java process object and
         * a native process. Even if this attribute is public you must never
         * access to it. It is set automatically during the build of the object.
         */
-       public long bind;
-
+       private long bind;
+       /**
+        * Indicates if the process is started
+        */
+       boolean started;
        /**
         * Even if this attribute is public you must never access to it.
         * It is used to compute the id of an MSG process.
         */
        public static long nextProcessId = 0;
-
+       
        /**
         * Even if this attribute is public you must never access to it.
         * It is compute automatically during the creation of the object. 
         * The native functions use this identifier to synchronize the process.
         */
        public long id;
-
-    /**
-     *
-     */
-    public Hashtable<String,String> properties;
-
+       
+       /**
+        * Start time of the process
+        */
+       public double startTime = 0;
+       /**
+        * Kill time of the process
+        */
+       public double killTime = -1;
+       
        /**
         * The name of the process.                                                     
         */
        protected String name;
-    /**
-     *
-     * @return
-     */
-    public String msgName() {
-               return this.name;
-       }
+       /**
+         * The PID of the process
+         */
+       protected int pid = -1;
+       /**
+        * The PPID of the process 
+        */
+       protected int ppid = -1;
+       /**
+        * The host of the process
+        */
+       protected Host host = null;
+
        /** The arguments of the method function of the process. */     
        public Vector<String> args;
 
-       /* process synchronization tools */
-    /**
-     *
-     */
-    /**
-     *
-     */
-    protected Sem schedBegin, schedEnd;
-
+       
        /**
         * Default constructor (used in ApplicationHandler to initialize it)
         */
        protected Process() {
-               super();
                this.id = nextProcessId++;
                this.name = null;
                this.bind = 0;
                this.args = new Vector<String>();
-               this.properties = null;
-               schedBegin = new Sem(0);
-               schedEnd = new Sem(0);
        }
 
 
@@ -158,12 +156,10 @@ public abstract class Process extends Thread {
         * @param host                  The host of the process to create.
         * @param name                  The name of the process.
         * @param args                  The arguments of main method of the process.
-        *
-        */
+        */     
        public Process(Host host, String name, String[]args) {
-               /* This is the constructor called by all others */
                this();
-               
+               this.host = host;
                if (name == null)
                        throw new NullPointerException("Process name cannot be NULL");
                this.name = name;
@@ -171,11 +167,37 @@ public abstract class Process extends Thread {
                this.args = new Vector<String>();
                if (null != args)
                        this.args.addAll(Arrays.asList(args));
+       }       
+       /**
+        * Constructs a new process from a host and his name, the arguments of here method function are
+        * specified by the parameter args.
+        *
+        * @param host                  The host of the process to create.
+        * @param name                  The name of the process.
+        * @param args                  The arguments of main method of the process.
+        * @param startTime             Start time of the process
+        * @param killTime              Kill time of the process
+        *
+        */
+       public Process(Host host, String name, String[]args, double startTime, double killTime) {
+               this();
+               this.host = host;
+               if (name == null)
+                       throw new NullPointerException("Process name cannot be NULL");
+               this.name = name;
 
-               MsgNative.processCreate(this, host);
+               this.args = new Vector<String>();
+               if (null != args)
+                       this.args.addAll(Arrays.asList(args));
+                               
+               this.startTime = startTime;
+               this.killTime = killTime;               
        }
-
-
+       /**
+        * The natively implemented method to create an MSG process.
+        * @param host    A valid (binded) host where create the process.
+        */
+       protected native void create(String hostName) throws HostNotFoundException;
        /**
         * This method kills all running process of the simulation.
         *
@@ -186,67 +208,59 @@ public abstract class Process extends Thread {
         * @return                              The function returns the PID of the next created process.
         *                      
         */ 
-       public static int killAll(int resetPID) {
-               return MsgNative.processKillAll(resetPID);
-       }
-
+       public static native int killAll(int resetPID);
 
        /**
-        * This method kill the current process.
-        * @param process  the process to be killed.
+        * This method kill a process.
         *
         */
-       public static void kill(Process process) {
-                MsgNative.processKill(process);
-       }
+       public native void kill();
        /**
-        * This method adds an argument in the list of the arguments of the main function
-        * of the process. 
-        *
-        * @param arg                   The argument to add.
-     *
-     * @deprecated
-     */
-       @Deprecated
-       protected void addArg(String arg) {
-               args.add(arg);
-       }
-
+        * Suspends the process by suspending the task on which it was
+        * waiting for the completion.
+        */
+       public native void suspend();
        /**
         * Suspends the process by suspending the task on which it was
         * waiting for the completion.
-        *
+        * DEPRECATED: use suspend instead.
         */
+       @Deprecated
        public void pause() {
-               MsgNative.processSuspend(this);
+               suspend();
        }
+       /**
+        * Sets the "auto-restart" flag of the process.
+        */
+       public native void setAutoRestart(boolean autoRestart);
+       /**
+        * Restarts the process from the beginning
+        */
+       public native void restart();
        /**
         * Resumes a suspended process by resuming the task on which it was
         * waiting for the completion.
-        *
-        *
-        */ 
-       public void restart()  {
-               MsgNative.processResume(this);
-       }
+        */
+       public native void resume();    
        /**
         * Tests if a process is suspended.
         *
         * @return                              The method returns true if the process is suspended.
         *                                              Otherwise the method returns false.
         */ 
-       public boolean isSuspended() {
-               return MsgNative.processIsSuspended(this);
+       public native boolean isSuspended();
+       /**
+        * Returns the name of the process
+        */
+       public String msgName() {
+               return this.name;
        }
        /**
-        * Returns the host of a process.
-        *
+        * Returns the host of the process.
         * @return                              The host instance of the process.
-        *
-        *
         */ 
        public Host getHost() {
-               return MsgNative.processGetHost(this);
+               return this.host;
        }
        /**
         * This static method gets a process from a PID.
@@ -257,9 +271,7 @@ public abstract class Process extends Thread {
         *
         * @exception                   NativeException on error in the native SimGrid code
         */ 
-       public static Process fromPID(int PID) throws NativeException {
-               return MsgNative.processFromPID(PID);
-       }
+       public static native Process fromPID(int PID) throws NativeException;
        /**
         * This method returns the PID of the process.
         *
@@ -267,7 +279,7 @@ public abstract class Process extends Thread {
         *
         */ 
        public int getPID()  {
-               return MsgNative.processGetPID(this);
+               return pid;
        }
        /**
         * This method returns the PID of the parent of a process.
@@ -276,17 +288,26 @@ public abstract class Process extends Thread {
         *
         */ 
        public int getPPID()  {
-               return MsgNative.processGetPPID(this);
+               return ppid;
        }
+       /**
+        * @brief Returns the value of a given process property. 
+        */
+       public native String getProperty(String name);
+       
+       /**
+        * Set the kill time of the process
+        * @param killTime the time when the process is killed
+        */
+       public native void setKillTime(double killTime);
+       
        /**
         * This static method returns the currently running process.
         *
         * @return                              The current process.
         *
         */ 
-       public static Process currentProcess()  {
-               return MsgNative.processSelf();
-       }
+       public static native Process currentProcess();
        /**
         * Migrates a process to another host.
         *
@@ -294,20 +315,31 @@ public abstract class Process extends Thread {
         * @param host                  The host where to migrate the process.
         *
         */
-       public static void migrate(Process process, Host host)  {
-               MsgNative.processMigrate(process, host);
+       public native void migrate(Host host);  
+       /**
+        * Makes the current process sleep until millis millisecondes have elapsed.
+        * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
+        * FIXME: Not optimal, maybe we should have two native functions.
+        * @param millis the length of time to sleep in milliseconds.
+        */
+       public static void sleep(long millis) throws HostFailureException  {
+               sleep(millis,0);
        }
+       /**
+        * Makes the current process sleep until millis milliseconds and nanos nanoseconds 
+        * have elapsed.
+        * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds and nanoseconds.
+        * Overloads Thread.sleep.
+        * @param millis the length of time to sleep in milliseconds.
+        * @param nanos additionnal nanoseconds to sleep.
+        */
+       public native static void sleep(long millis, int nanos) throws HostFailureException;
        /**
         * Makes the current process sleep until time seconds have elapsed.
-        *
         * @param seconds               The time the current process must sleep.
-        *
-        * @exception                   HostFailureException on error in the native SimGrid code
         */ 
-       public static void waitFor(double seconds) throws HostFailureException {
-               MsgNative.processWaitFor(seconds);
-       } 
-    /**
+       public native void waitFor(double seconds) throws HostFailureException;    
+       /**
      *
      */
     public void showArgs() {
@@ -317,21 +349,26 @@ public abstract class Process extends Thread {
                        Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
                                        "] args[" + i + "]=" + (String) (this.args.get(i)));
        }
+    /**
+     * This method actually creates and run the process.
+     * It is a noop if the process is already launched.
+     * @throws HostNotFoundException
+     */
+    public final void start() throws HostNotFoundException {
+       if (!started) {
+               started = true;
+               create(host.getName());
+       }
+    }
+    
        /**
         * This method runs the process. Il calls the method function that you must overwrite.
         */
        public void run() {
 
-               String[]args = null;      /* do not fill it before the signal or this.args will be empty */
-
+               String[] args = null;      /* do not fill it before the signal or this.args will be empty */
                //waitSignal(); /* wait for other people to fill the process in */
 
-
-               try {
-                       schedBegin.acquire();
-               } catch(InterruptedException e) {
-               }
-
                try {
                        args = new String[this.args.size()];
                        if (this.args.size() > 0) {
@@ -339,13 +376,15 @@ public abstract class Process extends Thread {
                        }
 
                        this.main(args);
-                       MsgNative.processExit(this);
-                       schedEnd.release();
                } catch(MsgException e) {
                        e.printStackTrace();
                        Msg.info("Unexpected behavior. Stopping now");
                        System.exit(1);
                }
+                catch(ProcessKilledError pk) {
+                        
+                }      
+               exit();
        }
 
        /**
@@ -356,100 +395,20 @@ public abstract class Process extends Thread {
      */
        public abstract void main(String[]args) throws MsgException;
 
-
-    /**
-     *
-     */
-    public void unschedule() {
-               try {
-                       schedEnd.release();
-                       schedBegin.acquire();
-               } catch (InterruptedException e) {
-                       /* stopped by jprocess_exit: I must terminate right now */
-                       /* FIXME: how to do that? */
-               }
-       }
-
-    /**
-     *
-     */
-    public void schedule() {
-          //System.err.println("Scheduling process in Java");
-               try {
-                       schedBegin.release();
-                       schedEnd.acquire();
-               } catch(InterruptedException e) {
-                  System.err.println("Got an interuption while scheduling process in Java");
-                  e.printStackTrace();
-               }
-       }
-
-       /** Send the given task in the mailbox associated with the specified alias  (waiting at most given time) 
-     * @param mailbox
-     * @param task 
-     * @param timeout
-     * @throws TimeoutException
-        * @throws HostFailureException 
-        * @throws TransferFailureException */
-       public void taskSend(String mailbox, Task task, double timeout) throws TransferFailureException, HostFailureException, TimeoutException {
-               MsgNative.taskSend(mailbox, task, timeout);
-       }
-
-       /** Send the given task in the mailbox associated with the specified alias
-     * @param mailbox
-     * @param task
-     * @throws TimeoutException
-        * @throws HostFailureException 
-        * @throws TransferFailureException */
-       public void taskSend(String mailbox, Task task) throws  TransferFailureException, HostFailureException, TimeoutException {
-               MsgNative.taskSend(mailbox, task, -1);
-       }
-
-    /** Receive a task on mailbox associated with the specified mailbox
-     * @param mailbox
-     * @return
-     * @throws TransferFailureException
-     * @throws HostFailureException
-     * @throws TimeoutException
-     */
-       public Task taskReceive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
-               return MsgNative.taskReceive(mailbox, -1.0, null);
-       }
-
-    /** Receive a task on mailbox associated with the specified alias (waiting at most given time)
-     * @param mailbox
-     * @param timeout
-     * @return
-     * @throws TransferFailureException
-     * @throws HostFailureException
-     * @throws TimeoutException
-     */
-       public Task taskReceive(String mailbox, double timeout) throws  TransferFailureException, HostFailureException, TimeoutException {
-               return MsgNative.taskReceive(mailbox, timeout, null);
-       }
-
-    /** Receive a task on mailbox associated with the specified alias from given sender
-     * @param mailbox
-     * @param host
-     * @param timeout
-     * @return
-     * @throws TransferFailureException
-     * @throws HostFailureException
-     * @throws TimeoutException
-     */
-       public Task taskReceive(String mailbox, double timeout, Host host) throws  TransferFailureException, HostFailureException, TimeoutException {
-               return MsgNative.taskReceive(mailbox, timeout, host);
+       public native void exit();    
+       /**
+        * Class initializer, to initialize various JNI stuff
+        */
+       public static native void nativeInit();
+       static {
+               Msg.nativeInit();
+               nativeInit();
        }
+       /**
+        * This static method returns the current amount of processes running
+        *
+        * @return                      The count of the running processes
+        */ 
+       public native static int getCount();
 
-    /** Receive a task on mailbox associated with the specified alias from given sender
-     * @param mailbox
-     * @param host
-     * @return
-     * @throws TransferFailureException
-     * @throws HostFailureException
-     * @throws TimeoutException
-     */
-       public Task taskReceive(String mailbox, Host host) throws  TransferFailureException, HostFailureException, TimeoutException {
-               return MsgNative.taskReceive(mailbox, -1.0, host);
-       }
 }