From ea44a43c908f20d119d1077364ce32bb422515e4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20B=C3=A9daride?= Date: Fri, 21 Mar 2014 11:28:27 +0100 Subject: [PATCH] Fix issues for destroy callbacks --- examples/java/surfPlugin/TestPlugin.java | 11 +++++------ examples/java/surfPlugin/TracePlugin.java | 22 ++++++++++++++------- examples/java/surfPlugin/surf_plugin.tesh | 4 ++++ src/bindings/java/surf.i | 8 ++++++++ src/bindings/java/surf_swig.cpp | 6 ++++++ src/bindings/java/surf_swig.hpp | 18 ++++++++++------- src/simix/smx_global.c | 12 ++++++------ src/simix/smx_io_private.h | 2 +- src/simix/smx_private.h | 24 +++++++++++++++-------- 9 files changed, 72 insertions(+), 35 deletions(-) diff --git a/examples/java/surfPlugin/TestPlugin.java b/examples/java/surfPlugin/TestPlugin.java index cf0752bade..a4b334c7c9 100644 --- a/examples/java/surfPlugin/TestPlugin.java +++ b/examples/java/surfPlugin/TestPlugin.java @@ -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]); diff --git a/examples/java/surfPlugin/TracePlugin.java b/examples/java/surfPlugin/TracePlugin.java index e1212745cf..6de18235e8 100644 --- a/examples/java/surfPlugin/TracePlugin.java +++ b/examples/java/surfPlugin/TracePlugin.java @@ -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()); } diff --git a/examples/java/surfPlugin/surf_plugin.tesh b/examples/java/surfPlugin/surf_plugin.tesh index c20953704e..a369378b15 100644 --- a/examples/java/surfPlugin/surf_plugin.tesh +++ b/examples/java/surfPlugin/surf_plugin.tesh @@ -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__ diff --git a/src/bindings/java/surf.i b/src/bindings/java/surf.i index 4d69190b74..3fc90338f1 100644 --- a/src/bindings/java/surf.i +++ b/src/bindings/java/surf.i @@ -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(); + } + } + ); } %} diff --git a/src/bindings/java/surf_swig.cpp b/src/bindings/java/surf_swig.cpp index b0378b7253..b5a376ff0b 100644 --- a/src/bindings/java/surf_swig.cpp +++ b/src/bindings/java/surf_swig.cpp @@ -1,11 +1,17 @@ #include #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)); } diff --git a/src/bindings/java/surf_swig.hpp b/src/bindings/java/surf_swig.hpp index 6066116a03..39eb67dfc0 100644 --- a/src/bindings/java/surf_swig.hpp +++ b/src/bindings/java/surf_swig.hpp @@ -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) {} diff --git a/src/simix/smx_global.c b/src/simix/smx_global.c index 6e9174ad43..7df250c5c1 100644 --- a/src/simix/smx_global.c +++ b/src/simix/smx_global.c @@ -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 @@ -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)); diff --git a/src/simix/smx_io_private.h b/src/simix/smx_io_private.h index f55facee79..860c9f66e3 100644 --- a/src/simix/smx_io_private.h +++ b/src/simix/smx_io_private.h @@ -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); diff --git a/src/simix/smx_private.h b/src/simix/smx_private.h index 7296da4029..db37ffb679 100644 --- a/src/simix/smx_private.h +++ b/src/simix/smx_private.h @@ -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) { -- 2.20.1