Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix issues for destroy callbacks
authorPaul Bédaride <paul.bedaride@gmail.com>
Fri, 21 Mar 2014 10:28:27 +0000 (11:28 +0100)
committerPaul Bédaride <paul.bedaride@gmail.com>
Fri, 21 Mar 2014 10:28:48 +0000 (11:28 +0100)
examples/java/surfPlugin/TestPlugin.java
examples/java/surfPlugin/TracePlugin.java
examples/java/surfPlugin/surf_plugin.tesh
src/bindings/java/surf.i
src/bindings/java/surf_swig.cpp
src/bindings/java/surf_swig.hpp
src/simix/smx_global.c
src/simix/smx_io_private.h
src/simix/smx_private.h

index cf0752b..a4b334c 100644 (file)
@@ -12,13 +12,13 @@ import org.simgrid.surf.Cpu;
 
 public class TestPlugin {
 
-  /* This only contains the launcher. If you do nothing more than than you can run 
+  /* This only contains the launcher. If you do nothing more than than you can run
   *   java simgrid.msg.Msg
   * which also contains such a launcher
   */
-  //static TracePlugin tp = new TracePlugin();
-  
-  public static void main(String[] args) throws NativeException {      
+  static TracePlugin tp = new TracePlugin();
+
+  public static void main(String[] args) throws NativeException {
     /* initialize the MSG simulation. Must be done before anything else (even logging). */
     Msg.init(args);
     if(args.length < 2) {
@@ -26,8 +26,7 @@ public class TestPlugin {
       Msg.info("example : TestPlugin ping_pong_platform.xml ping_pong_deployment.xml");
       System.exit(1);
     }
-    TracePlugin tp = new TracePlugin();
-         
+
     /* construct the platform and deploy the application */
     Msg.createEnvironment(args[0]);
     Msg.deployApplication(args[1]);
index e121274..6de1823 100644 (file)
@@ -7,21 +7,25 @@ import java.util.HashMap;
 public class TracePlugin extends Plugin {
 
   public TracePlugin() {
-    activateCpuCreatedCallback(); 
-    //activateCpuDestructedCallback();
-    activateCpuStateChangedCallback();                
+    activateCpuCreatedCallback();
+    activateCpuDestructedCallback();
+    activateCpuStateChangedCallback();
     activateCpuActionStateChangedCallback();
 
-    activateNetworkLinkCreatedCallback(); 
-    //activateCpuDestructedCallback();
-    activateNetworkLinkStateChangedCallback();                
+    activateNetworkLinkCreatedCallback();
+    activateNetworkLinkDestructedCallback();
+    activateNetworkLinkStateChangedCallback();
     activateNetworkActionStateChangedCallback();
   }
+
   public void cpuCreatedCallback(Cpu cpu) {
     Msg.info("Trace: Cpu created "+cpu.getName());
   }
 
+  public void cpuDestructedCallback(Cpu cpu) {
+    Msg.info("Trace: Cpu destructed "+cpu.getName());
+  }
+
   public void cpuStateChangedCallback(Cpu cpu){
     Msg.info("Trace: Cpu state changed "+cpu.getName());
   }
@@ -34,6 +38,10 @@ public class TracePlugin extends Plugin {
     Msg.info("Trace: NetworkLink created "+link.getName());
   }
 
+  public void networkLinkDestructedCallback(NetworkLink link) {
+    Msg.info("Trace: NetworkLink destructed "+link.getName());
+  }
+
   public void networkLinkStateChangedCallback(NetworkLink link){
     Msg.info("Trace: NetworkLink state changed "+link.getName());
   }
index c209537..a369378 100644 (file)
@@ -21,3 +21,7 @@ $ java -classpath ${classpath:=.} surfPlugin/TestPlugin ${srcdir:=.}/surfPlugin/
 > [1.301103] [jmsg/INFO] Trace: CpuAction state changed cpu
 > [Boivin:surfPlugin.Receiver:(2) 1.301103] [jmsg/INFO] goodbye!
 > [1.301103] [jmsg/INFO] MSG_main finished; Cleaning up the simulation...
+> [1.301103] [jmsg/INFO] Trace: Cpu destructed Jacquelin
+> [1.301103] [jmsg/INFO] Trace: Cpu destructed Boivin
+> [1.301103] [jmsg/INFO] Trace: NetworkLink destructed link
+> [1.301103] [jmsg/INFO] Trace: NetworkLink destructed __loopback__
index 4d69190..3fc9033 100644 (file)
@@ -8,6 +8,14 @@ import org.simgrid.NativeLib;
 %pragma(java) jniclasscode=%{
   static {
     NativeLib.nativeInit("surf-java");
+    Runtime.getRuntime().addShutdownHook(
+      new Thread() {
+        public void run() {
+          Thread.currentThread().setName( "Destroyer" );
+          Surf.clean();
+        }
+      }
+    );
   }
 %}
 
index b0378b7..b5a376f 100644 (file)
@@ -1,11 +1,17 @@
 #include <boost/lambda/bind.hpp>
 #include "src/surf/surf_interface.hpp"
 #include "surf_swig.hpp"
+#include "src/simix/smx_private.h"
+
 
 double getClock() {
   return surf_get_clock();
 }
 
+void clean() {
+  SIMIX_clean();
+}
+
 void Plugin::activateCpuCreatedCallback(){
   surf_callback_connect(cpuCreatedCallbacks, boost::bind(&Plugin::cpuCreatedCallback, this, _1));
 }
index 6066116..39eb67d 100644 (file)
@@ -6,22 +6,26 @@
 
 double getClock();
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+void clean();
+#ifdef __cplusplus
+}
+#endif
+
 class Plugin {
 public:
- virtual ~Plugin() { 
+ virtual ~Plugin() {
    std::cout << "Plugin::~Plugin()" << std:: endl;
  }
 
- void exit() {
-   surf_exit();
- }
-
- void activateCpuCreatedCallback(); 
+ void activateCpuCreatedCallback();
  virtual void cpuCreatedCallback(Cpu *cpu) {}
 
  void activateCpuDestructedCallback();
  virtual void cpuDestructedCallback(Cpu *cpu) {}
+
  void activateCpuStateChangedCallback();
  virtual void cpuStateChangedCallback(Cpu *cpu) {}
 
index 6e9174a..7df250c 100644 (file)
@@ -24,8 +24,6 @@ static void* SIMIX_action_mallocator_new_f(void);
 static void SIMIX_action_mallocator_free_f(void* action);
 static void SIMIX_action_mallocator_reset_f(void* action);
 
-static void SIMIX_clean(void);
-
 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
 #include <signal.h>
 
@@ -185,12 +183,14 @@ void SIMIX_global_init(int *argc, char **argv)
  *
  * This functions remove the memory used by SIMIX
  */
-static void SIMIX_clean(void)
+int cleaned = 0;
+void SIMIX_clean(void)
 {
 #ifdef TIME_BENCH_PER_SR
   smx_ctx_raw_new_sr();
 #endif
-
+  if (cleaned) return; // to avoid double cleaning by java and C
+  cleaned = 1;
   /* Kill everyone (except maestro) */
   SIMIX_process_killall(simix_global->maestro_process, 1);
 
@@ -378,7 +378,7 @@ void SIMIX_run(void)
     /* Handle any pending timer */
     while (xbt_heap_size(simix_timers) > 0 && SIMIX_get_clock() >= SIMIX_timer_next()) {
        //FIXME: make the timers being real callbacks
-       // (i.e. provide dispatchers that read and expand the args) 
+       // (i.e. provide dispatchers that read and expand the args)
        timer = xbt_heap_pop(simix_timers);
        if (timer->func)
          ((void (*)(void*))timer->func)(timer->args);
@@ -412,7 +412,7 @@ void SIMIX_run(void)
 
 
     XBT_DEBUG("### time %f, empty %d", time, xbt_dynar_is_empty(simix_global->process_to_run));
-    // !(time == -1.0 && xbt_dynar_is_empty()) 
+    // !(time == -1.0 && xbt_dynar_is_empty())
 
 
   } while (time != -1.0 || !xbt_dynar_is_empty(simix_global->process_to_run));
index f55face..860c9f6 100644 (file)
@@ -17,7 +17,7 @@ typedef struct s_smx_storage_priv {
 
 
 static inline smx_storage_priv_t SIMIX_storage_priv(smx_storage_t storage){
-  return xbt_lib_get_level(storage, SIMIX_STORAGE_LEVEL);
+  return (smx_storage_priv_t) xbt_lib_get_level(storage, SIMIX_STORAGE_LEVEL);
 }
 
 smx_storage_t SIMIX_storage_create(const char *name, void *storage, void *data);
index 7296da4..db37ffb 100644 (file)
@@ -59,6 +59,14 @@ extern unsigned long simix_process_maxpid;
 
 extern xbt_dict_t watched_hosts_lib;
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+void SIMIX_clean(void);
+#ifdef __cplusplus
+}
+#endif
+
 /******************************** Exceptions *********************************/
 
 #define SMX_EXCEPTION(issuer, c, v, m)                                  \
@@ -135,8 +143,8 @@ typedef struct s_smx_action {
       smx_rdv_t rdv;                  /* Rendez-vous where the comm is queued */
 
 #ifdef HAVE_MC
-      smx_rdv_t rdv_cpy;              /* Copy of the rendez-vous where the comm is queued, MC needs it for DPOR 
-                                         (comm.rdv set to NULL when the communication is removed from the mailbox 
+      smx_rdv_t rdv_cpy;              /* Copy of the rendez-vous where the comm is queued, MC needs it for DPOR
+                                         (comm.rdv set to NULL when the communication is removed from the mailbox
                                          (used as garbage collector)) */
 #endif
       int refcount;                   /* Number of processes involved in the cond */
@@ -165,7 +173,7 @@ typedef struct s_smx_action {
 
       void* src_data;                 /* User data associated to communication */
       void* dst_data;
-    } comm;    
+    } comm;
 
     struct {
       smx_host_t host;                /* The host that is sleeping */
@@ -232,12 +240,12 @@ void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory);
 /* Scenario for the end of a context:
  *
  * CASE 1: death after end of the main function
- *   the context_wrapper, called internally by the context module, calls 
- *   SIMIX_context_stop after user code stops, smx_context_stop calls user 
+ *   the context_wrapper, called internally by the context module, calls
+ *   SIMIX_context_stop after user code stops, smx_context_stop calls user
  *   cleanup_func if any (in context settings), add current process to trashbin
  *   and yields back to maestro.
  *   From time to time, maestro calls SIMIX_context_empty_trash, which destroy
- *   all the process and context data structures, and frees the memory 
+ *   all the process and context data structures, and frees the memory
  *
  * CASE 2: brutal death
  *   SIMIX_process_kill (from any process) set process->iwannadie = 1 and then
@@ -268,7 +276,7 @@ static XBT_INLINE smx_context_t SIMIX_context_new(xbt_main_func_t code,
 }
 
 /**
- * \brief destroy a context 
+ * \brief destroy a context
  * \param context the context to destroy
  * Argument must be stopped first -- runs in maestro context
  */
@@ -307,7 +315,7 @@ static XBT_INLINE void SIMIX_context_runall(void)
 }
 
 /**
- \brief returns the current running context 
+ \brief returns the current running context
  */
 static XBT_INLINE smx_context_t SIMIX_context_self(void)
 {