From f9521627fe1cef73bb8eccd5b11735dcbd906dbd Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Fri, 2 Aug 2019 17:50:49 +0200 Subject: [PATCH] New signal: Actor::on_termination (when its code terminates) --- ChangeLog | 3 +++ examples/s4u/README.rst | 1 + .../s4u/actor-exiting/s4u-actor-exiting.cpp | 17 +++++++++++++---- .../s4u/actor-exiting/s4u-actor-exiting.tesh | 6 ++++-- examples/smpi/trace/trace.tesh | 4 ++-- include/simgrid/s4u/Actor.hpp | 12 +++++++++--- src/kernel/actor/ActorImpl.cpp | 12 +++++++++--- src/s4u/s4u_Actor.cpp | 1 + 8 files changed, 42 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index c4e8643124..0198945fde 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,9 @@ S4U: - Barrier::wait returns SG_BARRIER_SERIAL_THREAD for (only) one actor for consistency with pthread_barrier_wait() - Host::get_englobing_zone() returns the englobing netzone + - Actor::on_destruction is now called in the destructor + Actor::on_termination new signal called when the actor terminates + its code. MSG: - convert a new set of functions to the S4U C interface and move the old MSG diff --git a/examples/s4u/README.rst b/examples/s4u/README.rst index e332ab6783..df88b03c43 100644 --- a/examples/s4u/README.rst +++ b/examples/s4u/README.rst @@ -49,6 +49,7 @@ Starting and Stoping Actors of doing so, depending of whether you want your callback to be executed when a specific actor ends (with ```this_actor::on_exit()```) or whether it should be executed when any actor ends (with + ```Actor::on_termination()```) or when it gets destroyed (with ```Actor::on_destruction()```) - |cpp| `examples/s4u/actor-exiting/s4u-actor-exiting.cpp `_ diff --git a/examples/s4u/actor-exiting/s4u-actor-exiting.cpp b/examples/s4u/actor-exiting/s4u-actor-exiting.cpp index 64cd98185a..cb479137b5 100644 --- a/examples/s4u/actor-exiting/s4u-actor-exiting.cpp +++ b/examples/s4u/actor-exiting/s4u-actor-exiting.cpp @@ -15,12 +15,18 @@ * Usually, the functions registered in this_actor::on_exit() are in charge * of releasing any memory allocated by the actor during its execution. * - * The other way of getting informed when an actor dies is to connect a - * function in the Actor::on_destruction signal, that is shared between + * The other way of getting informed when an actor terminates is to connect a + * function in the Actor::on_termination signal, that is shared between * all actors. Callbacks to this signal are executed for each terminating * actors, no matter what. This is useful in many cases, in particular * when developping SimGrid plugins. * + * Finally, you can attach callbacks to the Actor::on_destruction signal. + * It is also shared between all actors, and gets fired when the actors + * are destroyed. A delay is possible between the termination of an actor + * (ie, when it terminates executing its code) and its destruction (ie, + * when it is not referenced anywhere in the simulation and can be collected). + * * In both cases, you can stack more than one callback in the signal. * They will all be executed in the registration order. */ @@ -49,9 +55,12 @@ int main(int argc, char* argv[]) e.load_platform(argv[1]); /* - Load the platform description */ - /* Register a callback in the Actor::on_destruction signal. It will be called for every terminated actors */ + /* Register a callback in the Actor::on_termination signal. It will be called for every terminated actors */ + simgrid::s4u::Actor::on_termination.connect( + [](simgrid::s4u::Actor const& actor) { XBT_INFO("Actor %s terminates now", actor.get_cname()); }); + /* Register a callback in the Actor::on_destruction signal. It will be called for every destructed actors */ simgrid::s4u::Actor::on_destruction.connect( - [](simgrid::s4u::Actor const& actor) { XBT_INFO("Actor %s stops now", actor.get_cname()); }); + [](simgrid::s4u::Actor const& actor) { XBT_INFO("Actor %s gets destroyed now", actor.get_cname()); }); /* Create some actors */ simgrid::s4u::Actor::create("A", simgrid::s4u::Host::by_name("Tremblay"), actor_a); diff --git a/examples/s4u/actor-exiting/s4u-actor-exiting.tesh b/examples/s4u/actor-exiting/s4u-actor-exiting.tesh index 703cee5b5e..e59ceb8e02 100644 --- a/examples/s4u/actor-exiting/s4u-actor-exiting.tesh +++ b/examples/s4u/actor-exiting/s4u-actor-exiting.tesh @@ -2,5 +2,7 @@ $ ${bindir:=.}/s4u-actor-exiting ${platfdir}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n" > [ 10.194200] (A@Tremblay) I stop now -> [ 10.194200] (maestro@) Actor A stops now -> [ 26.213694] (maestro@) Actor B stops now +> [ 10.194200] (maestro@) Actor A terminates now +> [ 26.213694] (maestro@) Actor A gets destroyed now +> [ 26.213694] (maestro@) Actor B terminates now +> [ 26.213694] (maestro@) Actor B gets destroyed now diff --git a/examples/smpi/trace/trace.tesh b/examples/smpi/trace/trace.tesh index 9bb7c5e06d..9b0738189d 100644 --- a/examples/smpi/trace/trace.tesh +++ b/examples/smpi/trace/trace.tesh @@ -1333,14 +1333,14 @@ $ tail -n +3 ${bindir:=.}/smpi_trace.trace > 13 11.904056 2 1 > 12 11.904056 2 1 18 > 13 11.904056 2 1 -> 7 11.904056 1 1 > 13 11.905518 2 2 > 13 11.905518 2 2 > 12 11.905518 2 2 18 -> 7 11.905518 1 2 > 13 11.906032 2 3 > 12 11.906032 2 3 18 > 13 11.906032 2 3 +> 7 11.906032 1 1 +> 7 11.906032 1 2 > 7 11.906032 1 3 $ rm -f ${bindir:=.}/smpi_trace.trace diff --git a/include/simgrid/s4u/Actor.hpp b/include/simgrid/s4u/Actor.hpp index 3b847f249f..2d03fe5cec 100644 --- a/include/simgrid/s4u/Actor.hpp +++ b/include/simgrid/s4u/Actor.hpp @@ -162,9 +162,15 @@ public: static xbt::signal on_migration_start; /** Signal to others that an actor is has been migrated to another host **/ static xbt::signal on_migration_end; - /** Signal indicating that an actor is about to disappear. - * This signal is fired for any dying actor, which is mostly useful when designing plugins and extensions. If you - * want to register to the termination of a given actor, use this_actor::on_exit() instead.*/ + /** Signal indicating that an actor terminated its code. + * The actor may continue to exist if it is still referenced in the simulation, but it's not active anymore. + * If you want to free extra data when the actor's destructor is called, use Actor::on_destruction. + * If you want to register to the termination of a given actor, use this_actor::on_exit() instead.*/ + static xbt::signal on_termination; + /** Signal indicating that an actor is about to disappear (its destructor was called). + * This signal is fired for any destructed actor, which is mostly useful when designing plugins and extensions. + * If you want to react to the end of the actor's code, use Actor::on_termination instead. + * If you want to register to the termination of a given actor, use this_actor::on_exit() instead.*/ static xbt::signal on_destruction; /** Create an actor from a std::function diff --git a/src/kernel/actor/ActorImpl.cpp b/src/kernel/actor/ActorImpl.cpp index fc75399d82..992ab56b37 100644 --- a/src/kernel/actor/ActorImpl.cpp +++ b/src/kernel/actor/ActorImpl.cpp @@ -61,9 +61,11 @@ ActorImpl::ActorImpl(const simgrid::xbt::string& name, s4u::Host* host) : host_( ActorImpl::~ActorImpl() { - context_->iwannadie = false; // don't let the simcall's yield() do a Context::stop(), to avoid infinite loops - simgrid::simix::simcall([this] { simgrid::s4u::Actor::on_destruction(*ciface()); }); - context_->iwannadie = true; + if (this != simix_global->maestro_process) { + context_->iwannadie = false; // don't let the simcall's yield() do a Context::stop(), to avoid infinite loops + simgrid::simix::simcall([this] { simgrid::s4u::Actor::on_destruction(*ciface()); }); + context_->iwannadie = true; + } } /* Become an actor in the simulation @@ -183,6 +185,10 @@ void ActorImpl::cleanup() } simix_global->mutex.unlock(); + + context_->iwannadie = false; // don't let the simcall's yield() do a Context::stop(), to avoid infinite loops + simgrid::simix::simcall([this] { simgrid::s4u::Actor::on_termination(*ciface()); }); + context_->iwannadie = true; } void ActorImpl::exit() diff --git a/src/s4u/s4u_Actor.cpp b/src/s4u/s4u_Actor.cpp index 4a4589098e..33d319a022 100644 --- a/src/s4u/s4u_Actor.cpp +++ b/src/s4u/s4u_Actor.cpp @@ -28,6 +28,7 @@ xbt::signal s4u::Actor::on_sleep; xbt::signal s4u::Actor::on_wake_up; xbt::signal s4u::Actor::on_migration_start; xbt::signal s4u::Actor::on_migration_end; +xbt::signal s4u::Actor::on_termination; xbt::signal s4u::Actor::on_destruction; // ***** Actor creation ***** -- 2.20.1