From 3ea7a928f1914fabad883965ef552beb9578197f Mon Sep 17 00:00:00 2001 From: mquinson Date: Thu, 2 Dec 2010 23:56:10 +0000 Subject: [PATCH] Reorganize java sources git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/contrib/trunk/simgrid-java@8963 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- java/org/simgrid/msg/ApplicationHandler.java | 144 ++++++ java/org/simgrid/msg/Host.java | 165 +++++++ .../org/simgrid/msg/HostFailureException.java | 32 ++ .../simgrid/msg/HostNotFoundException.java | 32 ++ java/org/simgrid/msg/JniException.java | 35 ++ java/org/simgrid/msg/Msg.java | 113 +++++ java/org/simgrid/msg/MsgException.java | 42 ++ java/org/simgrid/msg/MsgNative.java | 430 ++++++++++++++++++ java/org/simgrid/msg/NativeException.java | 28 ++ java/org/simgrid/msg/Process.java | 383 ++++++++++++++++ .../simgrid/msg/ProcessNotFoundException.java | 35 ++ java/org/simgrid/msg/Sem.java | 35 ++ java/org/simgrid/msg/Task.java | 223 +++++++++ .../simgrid/msg/TaskCancelledException.java | 32 ++ java/org/simgrid/msg/TimeoutException.java | 32 ++ .../simgrid/msg/TransferFailureException.java | 32 ++ 16 files changed, 1793 insertions(+) create mode 100644 java/org/simgrid/msg/ApplicationHandler.java create mode 100644 java/org/simgrid/msg/Host.java create mode 100644 java/org/simgrid/msg/HostFailureException.java create mode 100644 java/org/simgrid/msg/HostNotFoundException.java create mode 100644 java/org/simgrid/msg/JniException.java create mode 100644 java/org/simgrid/msg/Msg.java create mode 100644 java/org/simgrid/msg/MsgException.java create mode 100644 java/org/simgrid/msg/MsgNative.java create mode 100644 java/org/simgrid/msg/NativeException.java create mode 100644 java/org/simgrid/msg/Process.java create mode 100644 java/org/simgrid/msg/ProcessNotFoundException.java create mode 100644 java/org/simgrid/msg/Sem.java create mode 100644 java/org/simgrid/msg/Task.java create mode 100644 java/org/simgrid/msg/TaskCancelledException.java create mode 100644 java/org/simgrid/msg/TimeoutException.java create mode 100644 java/org/simgrid/msg/TransferFailureException.java diff --git a/java/org/simgrid/msg/ApplicationHandler.java b/java/org/simgrid/msg/ApplicationHandler.java new file mode 100644 index 0000000000..9b0e1f0835 --- /dev/null +++ b/java/org/simgrid/msg/ApplicationHandler.java @@ -0,0 +1,144 @@ +/* + * These are the upcalls used by the FleXML parser for application files + * + * Copyright 2006,2007,2010 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. + */ + +package org.simgrid.msg; + +import java.util.Hashtable; +import java.util.Vector; + +public final class ApplicationHandler { + + + /** + * The vector which contains the arguments of the main function + * of the process object. + */ + public static Vector args; + + public static Hashtable properties; + + /** + * The name of the host of the process. + */ + private static String hostName; + + /** + * The function of the process. + */ + private static String function; + + /** + * This method is called by the start element handler. + * It sets the host and the function of the process to create, + * and clear the vector containing the arguments of the + * previouse process function if needed. + * + * @host The host of the process to create. + * @function The function of the process to create. + * + */ + public static void setProcessIdentity(String hostName_, String function_) { + hostName = hostName_; + function = function_; + + if (!args.isEmpty()) + args.clear(); + + if(!properties.isEmpty()) + properties.clear(); + } + /** + * This method is called by the startElement() handler. + * It stores the argument of the function of the next + * process to create in the vector of arguments. + * + * @arg The argument to add. + * + */ public static void registerProcessArg(String arg) { + args.add(arg); + } + + public static void setProperty(String id, String value) + { + properties.put(id,value); + } + + public static String getHostName() + { + return hostName; + } + + @SuppressWarnings("unchecked") + public static void createProcess() { + try { + Class cls = (Class) Class.forName(function); + + Process process = cls.newInstance(); + process.name = function; + process.id = simgrid.msg.Process.nextProcessId++; + Host host = Host.getByName(hostName); + + MsgNative.processCreate(process, host); + Vector args_ = args; + int size = args_.size(); + + for (int index = 0; index < size; index++) + process.args.add(args_.get(index)); + + process.properties = properties; + properties = new Hashtable(); + + } catch(HostNotFoundException e) { + System.out.println(e.toString()); + e.printStackTrace(); + + } catch(ClassNotFoundException e) { + System.out.println(function + + " class not found\n The attribut function of the element process of your deployment file\n must correspond to the name of a Msg Proces class)"); + e.printStackTrace(); + + } catch(InstantiationException e) { + System.out.println("Unable to create the process. I got an instantiation exception"); + e.printStackTrace(); + } catch(IllegalAccessException e) { + System.out.println("Unable to create the process. I got an illegal access exception"); + e.printStackTrace(); + } + + } + + + public static void onStartDocument() { + args = new Vector(); + properties = new Hashtable(); + hostName = null; + function = null; + } + + public static void onBeginProcess(String hostName, String function) { + setProcessIdentity(hostName, function); + + } + public static void onProperty(String id, String value) { + setProperty(id, value); + } + + public static void onProcessArg(String arg) { + registerProcessArg(arg); + } + + public static void onEndProcess() { + createProcess(); + } + + public static void onEndDocument() { + } +} diff --git a/java/org/simgrid/msg/Host.java b/java/org/simgrid/msg/Host.java new file mode 100644 index 0000000000..de12f11a34 --- /dev/null +++ b/java/org/simgrid/msg/Host.java @@ -0,0 +1,165 @@ +/* + * Bindings to the MSG hosts + * + * Copyright 2006,2007,2010 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. + * + */ +package org.simgrid.msg; + +/** + * A host object represents a location (any possible place) where a process may run. + * Thus it is represented as a physical resource with computing capabilities, some + * mailboxes to enable running process to communicate with remote ones, and some private + * data that can be only accessed by local process. An instance of this class is always + * binded with the corresponding native host. All the native hosts are automatically created + * during the call of the method Msg.createEnvironment(). This method take as parameter a + * platform file which describes all elements of the platform (host, link, root..). + * You cannot create a host yourself. + * + * The best way to get an host instance is to call the static method + * Host.getByName(). + * + * For example to get the instance of the host. If your platform + * file description contains an host named "Jacquelin" : + * + * \verbatim +Host jacquelin; + +try { + jacquelin = Host.getByName("Jacquelin"); +} catch(HostNotFoundException e) { + System.err.println(e.toString()); +} +... +\endverbatim + * + */ +public class Host { + + /** + * This attribute represents a bind between a java host object and + * a native host. Even if this attribute is public you must never + * access to it. It is set automatically during the call of the + * static method Host.getByName(). + * + * @see Host.getByName(). + */ + public long bind; + + + /** + * User data. + */ + private Object data; + protected Host() { + this.bind = 0; + this.data = null; + }; + + /** + * This static method gets an host instance associated with a native + * host of your platform. This is the best way to get a java host object. + * + * @param name The name of the host to get. + * + * @exception HostNotFoundException if the name of the host is not valid. + * NativeException if the native version of this method failed. + */ + public static Host getByName(String name) + throws HostNotFoundException { + if (name==null) + throw new NullPointerException("No host can have a null name"); + return MsgNative.hostGetByName(name); + } + + /** + * This static method returns the count of the installed hosts. + * + * @return The count of the installed hosts. + * + */ + public static int getCount() { + return MsgNative.hostGetCount(); + } + + /** + * This static method return an instance to the host of the current process. + * + * @return The host on which the current process is executed. + */ + public static Host currentHost() { + return MsgNative.hostSelf(); + } + + /** + * This static method returns all of the hosts of the installed platform. + * + * @return An array containing all the hosts installed. + * + */ + public static Host[] all() { + return MsgNative.allHosts(); + } + + /** + * This method returns the name of a host. + * + * @return The name of the host. + * + */ + public String getName() { + return MsgNative.hostGetName(this); + } + + /** + * Sets the data of the host. + * + */ + public void setData(Object data) { + this.data = data; + } + /** + * Gets the data of the host. + */ + public Object getData() { + return this.data; + } + + /** + * Checks whether a host has data. + */ + public boolean hasData() { + return null != this.data; + } + + /** + * This method returns the number of tasks currently running on a host. + * The external load is not taken in account. + * + * @return The number of tasks currently running on a host. + */ + public int getLoad() { + return MsgNative.hostGetLoad(this); + } + + /** + * This method returns the speed of the processor of a host, + * regardless of the current load of the machine. + * + * @return The speed of the processor of the host in flops. + * + */ + public double getSpeed() { + return MsgNative.hostGetSpeed(this); + } + + /** This method tests if a host is avail. */ + public boolean isAvail() { + return MsgNative.hostIsAvail(this); + } +} diff --git a/java/org/simgrid/msg/HostFailureException.java b/java/org/simgrid/msg/HostFailureException.java new file mode 100644 index 0000000000..083aa4a6d2 --- /dev/null +++ b/java/org/simgrid/msg/HostFailureException.java @@ -0,0 +1,32 @@ +/* + * This exception is raised when looking for a non-existing host. + * + * Copyright 2006,2007,2010 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. + */ + +package org.simgrid.msg; + +/** + * This exception is raised when The host on which you are running has just been rebooted. + */ +public class HostFailureException extends MsgException { + private static final long serialVersionUID = 1L; + + /** Constructs an HostFailureException without a detail message. */ + public HostFailureException() { + super(); + } + /** + * Constructs an HostFailureException with a detail message. + * + * @param s the detail message. + */ + public HostFailureException(String s) { + super(s); + } +} diff --git a/java/org/simgrid/msg/HostNotFoundException.java b/java/org/simgrid/msg/HostNotFoundException.java new file mode 100644 index 0000000000..524e0a842b --- /dev/null +++ b/java/org/simgrid/msg/HostNotFoundException.java @@ -0,0 +1,32 @@ +/* + * This exception is raised when looking for a non-existing host. + * + * Copyright 2006,2007,2010 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. + */ + +package org.simgrid.msg; + +/** + * This exception is raised when looking for a non-existing host. + */ +public class HostNotFoundException extends MsgException { + private static final long serialVersionUID = 1L; + + /** Constructs an HostNotFoundException without a detail message. */ + public HostNotFoundException() { + super(); + } + /** + * Constructs an HostNotFoundException with a detail message. + * + * @param s the detail message. + */ + public HostNotFoundException(String s) { + super(s); + } +} diff --git a/java/org/simgrid/msg/JniException.java b/java/org/simgrid/msg/JniException.java new file mode 100644 index 0000000000..ede0d59624 --- /dev/null +++ b/java/org/simgrid/msg/JniException.java @@ -0,0 +1,35 @@ +/* + * This exception is raised when there is a problem within the bindings (in JNI). + * + * Copyright 2006,2007,2010 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. + */ +package org.simgrid.msg; + +/** + * This exception is raised when there is a problem within the bindings (in JNI). + * That's a RuntimeException: I guess nobody wants to survive a JNI error in SimGrid + */ +public class JniException extends RuntimeException { + private static final long serialVersionUID = 1L; + + + /** + * Constructs an JniException without a + * detail message. + */ + public JniException() { + super(); + } + /** + * Constructs an JniException with a detail message. + * + * @param s the detail message. + */ public JniException(String s) { + super(s); + } +} diff --git a/java/org/simgrid/msg/Msg.java b/java/org/simgrid/msg/Msg.java new file mode 100644 index 0000000000..1cf2cc0d2f --- /dev/null +++ b/java/org/simgrid/msg/Msg.java @@ -0,0 +1,113 @@ +/* + * JNI interface to C code for MSG. + * + * Copyright 2006,2007,2010 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. + */ + +package org.simgrid.msg; + +public final class Msg { + /* Statically load the library which contains all native functions used in here */ + static { + try { + System.loadLibrary("simgrid-java"); + } catch(UnsatisfiedLinkError e) { + System.err.println("Cannot load the bindings to the simgrid library: "); + e.printStackTrace(); + System.err.println( + "Please check your LD_LIBRARY_PATH, or copy the simgrid and simgrid-java libraries to the current directory"); + System.exit(1); + } + } + + /* FIXME: kill these C crufts */ + /** Returns the last error code of the simulation */ + public final static native int getErrCode(); + + /** Everything is right. Keep on going the way ! */ + public static final int SUCCESS = 0; + + /** Something must be not perfectly clean (but I may be paranoid freak...) */ + public static final int WARNING = 1; + + /** There has been a problem during your task transfer. + * Either the network is down or the remote host has been shutdown */ + public static final int TRANSFERT_FAILURE = 2; + + /** System shutdown. + * The host on which you are running has just been rebooted. + * Free your data structures and return now ! */ + public static final int HOST_FAILURE = 3; + + /** Canceled task. This task has been canceled by somebody ! */ + public static final int TASK_CANCELLLED = 4; + + /** You've done something wrong. You'd better look at it... */ + public static final int FATAL_ERROR = 5; + + /** Retrieve the simulation time */ + public final static native double getClock(); + + /** Issue an information logging message */ + public final static native void info(String s); + + /********************************************************************************* + * Deployment and initialization related functions * + *********************************************************************************/ + + /** + * The natively implemented method to initialize a MSG simulation. + * + * @param args The arguments of the command line of the simulation. + * + * @see Msg.init() + */ + public final static native void init(String[]args); + + /** + * Run the MSG simulation, and cleanup everything afterward. + * + * If you want to chain simulations in the same process, you + * should call again createEnvironment and deployApplication afterward. + * + * @see MSG_run, MSG_clean + */ + public final static native void run() ; + + /** + * The native implemented method to create the environment of the simulation. + * + * @param platformFile The XML file which contains the description of the environment of the simulation + * + */ + public final static native void createEnvironment(String platformFile); + + /** + * The method to deploy the simulation. + * + * @param platformFile The XML file which contains the description of the application to deploy. + */ + public final static native void deployApplication(String deploymentFile); + + /** Example launcher. You can use it or provide your own launcher, as you wish */ + static public void main(String[]args) throws MsgException { + /* initialize the MSG simulation. Must be done before anything else (even logging). */ + Msg.init(args); + + if (args.length < 2) { + Msg.info("Usage: Msg platform_file deployment_file"); + System.exit(1); + } + + /* Load the platform and deploy the application */ + Msg.createEnvironment(args[0]); + Msg.deployApplication(args[1]); + /* Execute the simulation */ + Msg.run(); + } +} diff --git a/java/org/simgrid/msg/MsgException.java b/java/org/simgrid/msg/MsgException.java new file mode 100644 index 0000000000..51ebae99ee --- /dev/null +++ b/java/org/simgrid/msg/MsgException.java @@ -0,0 +1,42 @@ +/* + * This exception is an abstract class grouping all MSG-related exceptions + * + * Copyright 2006,2007,2010 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. + */ +package org.simgrid.msg; + +/** + * This exception is an abstract class grouping all MSG-related exceptions + + \htmlonly \endhtmlonly + + */ +public abstract class MsgException extends Exception { + private static final long serialVersionUID = 1L; + + /** + * Constructs an MsgException without a + * detail message. + */ + public MsgException() { + super(); + } + /** + * Constructs an MsgException with a detail message. + * + * @param s the detail message. + */ + public MsgException(String s) { + super(s); + } +} diff --git a/java/org/simgrid/msg/MsgNative.java b/java/org/simgrid/msg/MsgNative.java new file mode 100644 index 0000000000..c278bb1379 --- /dev/null +++ b/java/org/simgrid/msg/MsgNative.java @@ -0,0 +1,430 @@ +/* + * Contains all the native methods related to Process, Host and Task. + * + * Copyright 2006,2007,2010 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. + */ + +package org.simgrid.msg; + +import org.simgrid.msg.Process; + +/* FIXME: split into internal classes of Msg, Task, Host etc. */ + +/** + * Contains all the native methods related to Process, Host and Task. + */ +final class MsgNative { + + /****************************************************************** + * The natively implemented methods connected to the MSG Process * + ******************************************************************/ + /** + * The natively implemented method to create an MSG process. + * + * @param process The java process object to bind with the MSG native process. + * @param host A valid (binded) host where create the process. + * + * @see Process constructors. + */ + final static native + void processCreate(Process process, Host host); + + /** + * The natively implemented method to kill all the process of the simulation. + * + * @param resetPID Should we reset the PID numbers. A negative number means no reset + * and a positive number will be used to set the PID of the next newly + * created process. + * + * @return The function returns the PID of the next created process. + */ + final static native int processKillAll(int resetPID); + + /** + * The natively implemented method to suspend an MSG process. + * + * @param process The valid (binded with a native process) java process to suspend. + * + * @see Process.pause() + */ + final static native void processSuspend(Process process); + + /** + * The natively implemented method to kill a MSG process. + * + * @param process The valid (binded with a native process) java process to kill. + * + * @see Process.kill() + */ + final static native void processKill(Process process); + + /** + * The natively implemented method to resume a suspended MSG process. + * + * @param process The valid (binded with a native process) java process to resume. + * + * + * @see Process.restart() + */ + final static native void processResume(Process process); + + /** + * The natively implemented method to test if MSG process is suspended. + * + * @param process The valid (binded with a native process) java process to test. + * + * @return If the process is suspended the method retuns true. Otherwise the + * method returns false. + * + * @see Process.isSuspended() + */ + final static native boolean processIsSuspended(Process process); + + /** + * The natively implemented method to get the host of a MSG process. + * + * @param process The valid (binded with a native process) java process to get the host. + * + * @return The method returns the host where the process is running. + * + * @exception HostNotFoundException if the SimGrid native code failed (initialization error?). + * + * @see Process.getHost() + */ + final static native Host processGetHost(Process process); + + /** + * The natively implemented method to get a MSG process from his PID. + * + * @param PID The PID of the process to get. + * + * @return The process with the specified PID. + * + * @see Process.getFromPID() + */ + final static native Process processFromPID(int PID) ; + + /** + * The natively implemented method to get the PID of a MSG process. + * + * @param process The valid (binded with a native process) java process to get the PID. + * + * @return The PID of the specified process. + * + * @see Process.getPID() + */ + final static native int processGetPID(Process process); + + /** + * The natively implemented method to get the PPID of a MSG process. + * + * @param process The valid (binded with a native process) java process to get the PID. + * + * @return The PPID of the specified process. + * + * @see Process.getPPID() + */ + final static native int processGetPPID(Process process); + + /** + * The natively implemented method to get the current running process. + * + * @return The current process. + * + * @see Process.currentProcess() + */ + final static native Process processSelf(); + + /** + * The natively implemented method to migrate a process from his currnet host to a new host. + * + * @param process The (valid) process to migrate. + * @param host A (valid) host where move the process. + * + * + * @see Process.migrate() + * @see Host.getByName() + */ + final static native void processChangeHost(Process process, Host host) ; + + /** + * The natively implemented native to request the current process to sleep + * until time seconds have elapsed. + * + * @param seconds The time the current process must sleep. + * + * @exception HostFailureException if the SimGrid native code failed. + * + * @see Process.waitFor() + */ + final static native void processWaitFor(double seconds) throws HostFailureException; + + /** + * The natively implemented native method to exit a process. + * + * @see Process.exit() + */ + final static native void processExit(Process process); + + + /****************************************************************** + * The natively implemented methods connected to the MSG host * + ******************************************************************/ + + /** + * The natively implemented method to get an host from his name. + * + * @param name The name of the host to get. + * + * @return The host having the specified name. + * + * @exception HostNotFoundException if there is no such host + * + * + * @see Host.getByName() + */ + final static native Host hostGetByName(String name) throws HostNotFoundException; + + /** + * The natively implemented method to get the name of an MSG host. + * + * @param host The host (valid) to get the name. + * + * @return The name of the specified host. + * + * @see Host.getName() + */ + final static native String hostGetName(Host host); + + /** + * The natively implemented method to get the number of hosts of the simulation. + * + * @return The number of hosts of the simulation. + * + * @see Host.getNumber() + */ + final static native int hostGetCount(); + + /** + * The natively implemented method to get the host of the current runing process. + * + * @return The host of the current running process. + * + * @see Host.currentHost() + */ + final static native Host hostSelf(); + + /** + * The natively implemented method to get the speed of a MSG host. + * + * @param host The host to get the host. + * + * @return The speed of the specified host. + * + * @see Host.getSpeed() + */ + + final static native double hostGetSpeed(Host host); + + /** + * The natively implemented native method to test if an host is avail. + * + * @param host The host to test. + * + * @return If the host is avail the method returns true. + * Otherwise the method returns false. + * + * @see Host.isAvail() + */ + final static native boolean hostIsAvail(Host host); + + /** + * The natively implemented native method to get all the hosts of the simulation. + * + * @return A array which contains all the hosts of simulation. + */ + + final static native Host[] allHosts(); + + /** + * The natively implemented native method to get the number of running tasks on a host. + * + * @param The host concerned by the operation. + * + * @return The number of running tasks. + */ + final static native int hostGetLoad(Host host); + + /****************************************************************** + * The natively implemented methods connected to the MSG task * + ******************************************************************/ + + /** + * The natively implemented method to create a MSG task. + * + * @param name The name of th task. + * @param computeDuration A value of the processing amount (in flop) needed + * to process the task. If 0, then it cannot be executed + * with the execute() method. This value has to be >= 0. + * @param messageSize A value of amount of data (in bytes) needed to transfert + * this task. If 0, then it cannot be transfered this task. + * If 0, then it cannot be transfered with the get() and put() + * methods. This value has to be >= 0. + * @param task The java task object to bind with the native task to create. + * + * @exception IllegalArgumentException if compute duration <0 or message size <0 + * + * @see Task.create() + */ + final static native void taskCreate(Task task, String name, + double computeDuration, + double messageSize) + throws IllegalArgumentException; + + /** + * The natively implemented method to get the sender of a task. + * + * @param task The task (valid) to get the sender. + * + * @return The sender of the task. + * + * @see Task.getSender() + */ + final static native Process taskGetSender(Task task); + + /** + * The natively implementd method to get the source of a task. + * + * @param task The task to get the source. + * + * @return The source of the task. + * + * + * @see Task.getSource() + */ + final static native Host taskGetSource(Task task); + + /** + * The natively implemented method to get the name of the task. + * + * @param task The task to get the name. + * + * @return The name of the specified task. + * + * @see Task.getName() + */ + final static native String taskGetName(Task task); + + /** + * The natively implemented method to cancel a task. + * + * @param task The task to cancel. + * + * + * @see Task.cancel(). + */ + final static native void taskCancel(Task task); + + /** + * The natively implemented method to create a MSG parallel task. + * + * @param name The name of the parallel task. + * @param hosts The list of hosts implied by the parallel task. + * @param computeDurations The total number of operations that have to be performed + * on the hosts. + * @param messageSizes An array of doubles + * + * @see ParallelTask.create() + */ + final static native void parallelTaskCreate(Task pTask, String name, + Host[]hosts, + double[]computeDurations, + double[]messageSizes) + throws NullPointerException, IllegalArgumentException; + + /** + * The natively implemented method to get the computing amount of the task. + * + * @param task The task to get the computing amount. + * + * @return The computing amount of the specified task. + * + * @see Task.getComputeDuration() + */ + final static native double taskGetComputeDuration(Task task); + + /** + * The natively implemented method to get the remaining computation + * + * @param task The task to get the remaining computation. + * + * @return The remaining computation of the specified task. + * + * @see Task.getRemainingDuration() + */ + final static native double taskGetRemainingDuration(Task task); + + /** + * The natively implemented method to set the priority of a task. + * + * @param task The task to set the priority + * + * @param priority The new priority of the specified task. + * + * @see Task.setPriority() + */ + final static native void taskSetPriority(Task task, double priority); + + /** + * The natively implemented method to destroy a MSG task. + * + * @param The task to destroy. + * + * + * @see Task.destroy() + */ + final static native void taskDestroy(Task task) ; + + /** + * The natively implemented method to execute a MSG task. + * + * @param task The task to execute. + * + * @exception HostFailureException,TaskCancelledException on error in the C world + * + * @see Task.execute() + */ + final static native void taskExecute(Task task) throws HostFailureException,TaskCancelledException; + + /* **************************************************************** + * Communication methods thru mailboxes * + **************************************************************** */ + + final static native void taskSend(String alias, Task task, double timeout) throws TransferFailureException,HostFailureException,TimeoutException; + final static native Task taskReceive(String alias, double timeout, Host host) throws TransferFailureException,HostFailureException,TimeoutException; + final static native int taskListenFrom(String alias); + final static native boolean taskListen(String alias); + final static native int taskListenFromHost(String alias, Host host); + + /* *************************************************************** + * Task sending methods * + *************************************************************** */ + + /** + * The natively implemented method to send a task in a mailbox associated with an alias, with a bounded transmition + * rate. + * + * @param alias The alias of the mailbox. + * @param task The task to put. + * @param max_rate The bounded transmition rate. + * + * @exception NativeException on error in the C world + */ + final static native void taskSendBounded(String alias, Task task, double maxrate) throws TransferFailureException,HostFailureException,TimeoutException; + +} diff --git a/java/org/simgrid/msg/NativeException.java b/java/org/simgrid/msg/NativeException.java new file mode 100644 index 0000000000..eaec2faaa7 --- /dev/null +++ b/java/org/simgrid/msg/NativeException.java @@ -0,0 +1,28 @@ +/* + * This exception is raised when there is an error within the C world of SimGrid. + * + * Copyright 2006,2007,2010 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. + */ +package org.simgrid.msg; + +/** + * This exception is raised when there is an error within the C world of SimGrid. + * Refer to the string message for more info on what went wrong. + */ +public class NativeException extends MsgException { + private static final long serialVersionUID = 1L; + + /** + * Constructs an NativeException with a detail message. + * + * @param s the detail message. + */ + public NativeException(String s) { + super(s); + } +} diff --git a/java/org/simgrid/msg/Process.java b/java/org/simgrid/msg/Process.java new file mode 100644 index 0000000000..333e91f4ff --- /dev/null +++ b/java/org/simgrid/msg/Process.java @@ -0,0 +1,383 @@ +/* + * $Id$ + * + * Copyright 2006,2007 Martin Quinson, Malek Cherier + * 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. + */ + +package org.simgrid.msg; + +import java.util.Arrays; +import java.util.Hashtable; +import java.util.Vector; + +/** + * A process may be defined as a code, with some private data, executing + * in a location (host). All the process used by your simulation must be + * declared in the deployment file (XML format). + * To create your own process you must inherit your own process from this + * class and override the method "main()". For example if you want to use + * a process named Slave proceed as it : + * + * (1) import the class Process of the package simgrid.msg + * import simgrid.msg.Process; + * + * public class Slave extends simgrid.msg.Process { + * + * (2) Override the method function + * + * \verbatim + * public void main(String[] args) { + * System.out.println("Hello MSG"); + * } + * \endverbatim + * } + * The name of your process must be declared in the deployment file of your simulation. + * For the example, for the previous process Slave this file must contains a line : + * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process + * Slave. All the process of your simulation are automatically launched and managed by Msg. + * A process use tasks to simulate communications or computations with another process. + * For more information see Task. For more information on host concept + * see Host. + * + */ + +public abstract class Process extends Thread { + /** + * 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; + + /** + * 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 properties; + + /** + * The name of the process. + */ + protected String name; + public String msgName() { + return this.name; + } + /** The arguments of the method function of the process. */ + public Vector 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(); + this.properties = null; + schedBegin = new Sem(0); + schedEnd = new Sem(0); + } + + + /** + * Constructs a new process from the name of a host and his name. The method + * function of the process doesn't have argument. + * + * @param hostname The name of the host of the process to create. + * @param name The name of the process. + * + * @exception HostNotFoundException if no host with this name exists. + * + * + */ + public Process(String hostname, String name) throws HostNotFoundException { + this(Host.getByName(hostname), name, null); + } + /** + * Constructs a new process from the name of a host and his name. The arguments + * of the method function of the process are specified by the parameter args. + * + * @param hostname The name of the host of the process to create. + * @param name The name of the process. + * @param args The arguments of the main function of the process. + * + * @exception HostNotFoundException if no host with this name exists. + * NativeException + * + */ + public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException { + this(Host.getByName(hostname), name, args); + } + /** + * Constructs a new process from a host and his name. The method function of the + * process doesn't have argument. + * + * @param host The host of the process to create. + * @param name The name of the process. + * + */ + public Process(Host host, String name) { + this(host, name, null); + } + /** + * 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. + * + */ + public Process(Host host, String name, String[]args) { + /* This is the constructor called by all others */ + this(); + + if (name == null) + throw new NullPointerException("Process name cannot be NULL"); + this.name = name; + + this.args = new Vector(); + if (null != args) + this.args.addAll(Arrays.asList(args)); + + MsgNative.processCreate(this, host); + } + + + /** + * This method kills all running process of the simulation. + * + * @param resetPID Should we reset the PID numbers. A negative number means no reset + * and a positive number will be used to set the PID of the next newly + * created process. + * + * @return The function returns the PID of the next created process. + * + */ + public static int killAll(int resetPID) { + return MsgNative.processKillAll(resetPID); + } + + /** + * 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 + protected void addArg(String arg) { + args.add(arg); + } + + /** + * Suspends the process by suspending the task on which it was + * waiting for the completion. + * + */ + public void pause() { + MsgNative.processSuspend(this); + } + /** + * Resumes a suspended process by resuming the task on which it was + * waiting for the completion. + * + * + */ + public void restart() { + MsgNative.processResume(this); + } + /** + * 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); + } + /** + * Returns the host of a process. + * + * @return The host instance of the process. + * + * @exception NativeException on error in the native SimGrid code + * + */ + public Host getHost() { + return MsgNative.processGetHost(this); + } + /** + * This static method gets a process from a PID. + * + * @param PID The process identifier of the process to get. + * + * @return The process with the specified PID. + * + * @exception NativeException on error in the native SimGrid code + */ + public static Process fromPID(int PID) throws NativeException { + return MsgNative.processFromPID(PID); + } + /** + * This method returns the PID of the process. + * + * @return The PID of the process. + * + */ + public int getPID() { + return MsgNative.processGetPID(this); + } + /** + * This method returns the PID of the parent of a process. + * + * @return The PID of the parent of the process. + * + */ + public int getPPID() { + return MsgNative.processGetPPID(this); + } + /** + * This static method returns the currently running process. + * + * @return The current process. + * + */ + public static Process currentProcess() { + return MsgNative.processSelf(); + } + /** + * Migrates a process to another host. + * + * @param host The host where to migrate the process. + * + */ + public void migrate(Host host) { + MsgNative.processChangeHost(this, host); + } + /** + * 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 void showArgs() { + Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" + + this.args.size()); + for (int i = 0; i < this.args.size(); i++) + Msg.info("[" + this.msgName() + "/" + this.getHost().getName() + + "] args[" + i + "]=" + (String) (this.args.get(i))); + } + /** + * 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 */ + + //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) { + this.args.toArray(args); + } + + this.main(args); + MsgNative.processExit(this); + schedEnd.release(); + } catch(MsgException e) { + e.printStackTrace(); + Msg.info("Unexpected behavior. Stopping now"); + System.exit(1); + } + } + + /** + * The main function of the process (to implement). + */ + public abstract void main(String[]args) throws MsgException; + + + public void unschedule() { + try { + schedEnd.release(); + schedBegin.acquire(); + } catch(InterruptedException e) { + } + } + + 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) + * @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 + * @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 */ + 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) */ + 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 */ + public Task taskReceive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException { + return MsgNative.taskReceive(mailbox, timeout, host); + } + + /** Receive a task on mailbox associated with the specified alias from given sender*/ + public Task taskReceive(String mailbox, Host host) throws TransferFailureException, HostFailureException, TimeoutException { + return MsgNative.taskReceive(mailbox, -1.0, host); + } +} diff --git a/java/org/simgrid/msg/ProcessNotFoundException.java b/java/org/simgrid/msg/ProcessNotFoundException.java new file mode 100644 index 0000000000..f4b313d0ed --- /dev/null +++ b/java/org/simgrid/msg/ProcessNotFoundException.java @@ -0,0 +1,35 @@ +/* + * This exception is raised when looking for a non-existing process. + * + * Copyright 2006,2007,2010 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. + */ + +package org.simgrid.msg; + +/** + * This exception is raised when looking for a non-existing process. + */ +public class ProcessNotFoundException extends MsgException { + private static final long serialVersionUID = 1L; + + /** + * Constructs an ProcessNotFoundException without a + * detail message. + */ + public ProcessNotFoundException() { + super(); + } + /** + * Constructs an ProcessNotFoundException with a detail message. + * + * @param s the detail message. + */ + public ProcessNotFoundException(String s) { + super(s); + } +} diff --git a/java/org/simgrid/msg/Sem.java b/java/org/simgrid/msg/Sem.java new file mode 100644 index 0000000000..84ab9f25ef --- /dev/null +++ b/java/org/simgrid/msg/Sem.java @@ -0,0 +1,35 @@ +/* + * Simple semaphore implementation, from Doug Lea (public domain) + * + * Copyright 2006,2007,2010 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. + */ + package org.simgrid.msg; + public class Sem { + /******************************************************************/ + /* Simple semaphore implementation, from Doug Lea (public domain) */ + /******************************************************************/ + private int permits_; + public Sem(int i) { permits_ = i; } public void acquire() throws InterruptedException { + if (Thread.interrupted()) + throw new InterruptedException(); + synchronized(this) { + try { + while (permits_ <= 0) + wait(); + --permits_; + } + catch(InterruptedException ex) { + notify(); + throw ex; + } + } + } + public synchronized void release() { + ++(this.permits_); + notify(); + } } diff --git a/java/org/simgrid/msg/Task.java b/java/org/simgrid/msg/Task.java new file mode 100644 index 0000000000..25fbe79d04 --- /dev/null +++ b/java/org/simgrid/msg/Task.java @@ -0,0 +1,223 @@ +/* + * simgrid.msg.Task.java 1.00 07/05/01 + * + * Copyright 2006,2007 Martin Quinson, Malek Cherier + * 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. + */ + +package org.simgrid.msg; + +/** + * A task is either something to compute somewhere, or something to exchange between two hosts (or both). + * It is defined by a computing amount and a message size. + * + */ +public class Task { + /** + * This attribute represents a bind between a java task object and + * a native task. 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 = 0; + + + /** Default constructor (all fields to 0 or null) */ + public Task() { + MsgNative.taskCreate(this, null, 0, 0); + } + /* * * * + * * Constructors * * + * * * */ + /** + * Construct an new task with the specified processing amount and amount + * of data needed. + * + * @param name Task's name + * + * @param computeDuration A value of the processing amount (in flop) needed to process the task. + * If 0, then it cannot be executed with the execute() method. + * This value has to be >= 0. + * + * @param messageSize A value of amount of data (in bytes) needed to transfert this task. + * If 0, then it cannot be transfered with the get() and put() methods. + * This value has to be >= 0. + */ + public Task(String name, double computeDuration, double messageSize) { + MsgNative.taskCreate(this, name, computeDuration, messageSize); + } + /** + * Construct an new parallel task with the specified processing amount and amount for each host + * implied. + * + * @param name The name of the parallel task. + * @param hosts The list of hosts implied by the parallel task. + * @param computeDurations The amount of operations to be performed by each host of \a hosts. + * @param messageSizes A matrix describing the amount of data to exchange between hosts. + */ + public Task(String name, Host[]hosts, double[]computeDurations, double[]messageSizes) { + MsgNative.parallelTaskCreate(this, name, hosts, computeDurations, messageSizes); + } + /* * * * + * * Getters / Setters * * + * * * */ + /** Gets the name of a task */ + public String getName() { + return MsgNative.taskGetName(this); + } + /** Gets the sender of the task */ + Process getSender() { + return MsgNative.taskGetSender(this); + } + /** Gets the source of the task */ + public Host getSource() { + return MsgNative.taskGetSource(this); + } + /** Gets the computing amount of the task */ + public double getComputeDuration() { + return MsgNative.taskGetComputeDuration(this); + } + /** Gets the remaining computation of the task */ + public double getRemainingDuration() { + return MsgNative.taskGetRemainingDuration(this); + } + /** + * This method sets the priority of the computation of the task. + * The priority doesn't affect the transfert rate. For example a + * priority of 2 will make the task receive two times more cpu than + * the other ones. + * + * @param priority The new priority of the task. + */ + public void setPriority(double priority) { + MsgNative.taskSetPriority(this, priority); + } + /* * * * + * * Communication-related * * + * * * */ + + + /* * * * + * * Computation-related * * + * * * */ + /** + * Executes a task on the location on which the process is running. + * + * @exception HostFailureException,TaskCancelledException + */ + public void execute() throws HostFailureException,TaskCancelledException { + MsgNative.taskExecute(this); + } + /** + * Cancels a task. + * + * @exception NativeException if the cancellation failed. + */ + public void cancel() { + MsgNative.taskCancel(this); + } + /** Deletes a task. + * + * @exception NativeException if the destruction failed. + */ + protected void finalize() throws NativeException { + if (this.bind != 0) + MsgNative.taskDestroy(this); + } + + /** + * Sends the task on the mailbox identified by the specified name + * + * @throws TimeoutException + * @throws HostFailureException + * @throws TransferFailureException + */ + public void send(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException { + MsgNative.taskSend(mailbox, this, -1); + } + + /** + * Sends the task on the mailbox identified by the specified name (wait at most \a timeout seconds) + * + * @exception NativeException if the retrieval fails. + * @throws TimeoutException + * @throws HostFailureException + * @throws TransferFailureException + */ + public void send(String mailbox, double timeout) throws NativeException, TransferFailureException, HostFailureException, TimeoutException { + MsgNative.taskSend(mailbox, this, timeout); + } + + /** + * Sends the task on the mailbox identified by the specified alias (capping the sending rate to \a maxrate) + * + * @exception TransferFailureException, HostFailureException, TimeoutException. + */ + public void sendBounded(String alias, double maxrate) throws TransferFailureException, HostFailureException, TimeoutException { + MsgNative.taskSendBounded(alias, this, maxrate); + } + + /** + * Retrieves next task from the mailbox identified by the specified name + * + * @exception TransferFailureException, HostFailureException, TimeoutException if the retrieval fails. + */ + + public static Task receive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException { + return MsgNative.taskReceive(mailbox, -1.0, null); + } + + /** + * Retrieves next task on the mailbox identified by the specified name (wait at most \a timeout seconds) + * + * @exception TransferFailureException, HostFailureException, TimeoutException if the retrieval fails. + */ + public static Task receive(String mailbox, double timeout) throws TransferFailureException, HostFailureException, TimeoutException { + return MsgNative.taskReceive(mailbox, timeout, null); + } + + /** + * Retrieves next task sent by a given host on the mailbox identified by the specified alias + * + * @exception TransferFailureException, HostFailureException, TimeoutException if the retrieval fails. + */ + + public static Task receive(String mailbox, Host host) throws TransferFailureException, HostFailureException, TimeoutException { + return MsgNative.taskReceive(mailbox, -1.0, host); + } + + /** + * Retrieves next task sent by a given host on the mailbox identified by the specified alias (wait at most \a timeout seconds) + * + * @exception TransferFailureException, HostFailureException, TimeoutException if the retrieval fails. + */ + public static Task receive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException { + return MsgNative.taskReceive(mailbox, timeout, host); + } + + /** + * Tests whether there is a pending communication on the mailbox identified by the specified alias, and who sent it + * + */ + public static int listenFrom(String mailbox) { + return MsgNative.taskListenFrom(mailbox); + } + /** + * Listen whether there is a waiting task on the mailbox identified by the specified alias + * + */ + public static boolean listen(String mailbox) { + return MsgNative.taskListen(mailbox); + } + + /** + * Counts the number of tasks waiting to be received on the \a mailbox identified by the specified alia and sended by the specified \a host. + * + */ + public static int listenFromHost(String alias, Host host) { + return MsgNative.taskListenFromHost(alias, host); + } +} diff --git a/java/org/simgrid/msg/TaskCancelledException.java b/java/org/simgrid/msg/TaskCancelledException.java new file mode 100644 index 0000000000..4a7053d9b3 --- /dev/null +++ b/java/org/simgrid/msg/TaskCancelledException.java @@ -0,0 +1,32 @@ +/* + * This exception is raised when looking for a non-existing host. + * + * Copyright 2006,2007,2010 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. + */ + +package org.simgrid.msg; + +/** + * This exception is raised when task is cancelled. + */ +public class TaskCancelledException extends MsgException { + private static final long serialVersionUID = 1L; + + /** Constructs an TaskCancelledException without a detail message. */ + public TaskCancelledException() { + super(); + } + /** + * Constructs an TaskCancelledException with a detail message. + * + * @param s the detail message. + */ + public TaskCancelledException(String s) { + super(s); + } +} diff --git a/java/org/simgrid/msg/TimeoutException.java b/java/org/simgrid/msg/TimeoutException.java new file mode 100644 index 0000000000..edd4091f03 --- /dev/null +++ b/java/org/simgrid/msg/TimeoutException.java @@ -0,0 +1,32 @@ +/* + * This exception is raised when looking for a non-existing host. + * + * Copyright 2006,2007,2010 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. + */ + +package org.simgrid.msg; + +/** + * This exception is raised when time's out while sending tasks. + */ +public class TimeoutException extends MsgException { + private static final long serialVersionUID = 1L; + + /** Constructs an TimeoutFailureException without a detail message. */ + public TimeoutException() { + super(); + } + /** + * Constructs an TransferFailureException with a detail message. + * + * @param s the detail message. + */ + public TimeoutException(String s) { + super(s); + } +} diff --git a/java/org/simgrid/msg/TransferFailureException.java b/java/org/simgrid/msg/TransferFailureException.java new file mode 100644 index 0000000000..6068761aeb --- /dev/null +++ b/java/org/simgrid/msg/TransferFailureException.java @@ -0,0 +1,32 @@ +/* + * This exception is raised when looking for a non-existing host. + * + * Copyright 2006,2007,2010 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. + */ + +package org.simgrid.msg; + +/** + * This exception is raised if transfer failed while sending tasks. + */ +public class TransferFailureException extends MsgException { + private static final long serialVersionUID = 1L; + + /** Constructs an TransferFailureException without a detail message. */ + public TransferFailureException() { + super(); + } + /** + * Constructs an TransferFailureException with a detail message. + * + * @param s the detail message. + */ + public TransferFailureException(String s) { + super(s); + } +} -- 2.20.1