Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New signal: Actor::on_termination (when its code terminates)
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Fri, 2 Aug 2019 15:50:49 +0000 (17:50 +0200)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Fri, 2 Aug 2019 15:50:49 +0000 (17:50 +0200)
ChangeLog
examples/s4u/README.rst
examples/s4u/actor-exiting/s4u-actor-exiting.cpp
examples/s4u/actor-exiting/s4u-actor-exiting.tesh
examples/smpi/trace/trace.tesh
include/simgrid/s4u/Actor.hpp
src/kernel/actor/ActorImpl.cpp
src/s4u/s4u_Actor.cpp

index c4e8643..0198945 100644 (file)
--- 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
index e332ab6..df88b03 100644 (file)
@@ -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 <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-exiting/s4u-actor-exiting.cpp>`_
index 64cd981..cb47913 100644 (file)
  * 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);
index 703cee5..e59ceb8 100644 (file)
@@ -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
index 9bb7c5e..9b07381 100644 (file)
@@ -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
 
index 3b847f2..2d03fe5 100644 (file)
@@ -162,9 +162,15 @@ public:
   static xbt::signal<void(Actor const&)> on_migration_start;
   /** Signal to others that an actor is has been migrated to another host **/
   static xbt::signal<void(Actor const&)> 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<void(Actor const&)> 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<void(Actor const&)> on_destruction;
 
   /** Create an actor from a std::function<void()>
index fc75399..992ab56 100644 (file)
@@ -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()
index 4a45890..33d319a 100644 (file)
@@ -28,6 +28,7 @@ xbt::signal<void(Actor const&)> s4u::Actor::on_sleep;
 xbt::signal<void(Actor const&)> s4u::Actor::on_wake_up;
 xbt::signal<void(Actor const&)> s4u::Actor::on_migration_start;
 xbt::signal<void(Actor const&)> s4u::Actor::on_migration_end;
+xbt::signal<void(Actor const&)> s4u::Actor::on_termination;
 xbt::signal<void(Actor const&)> s4u::Actor::on_destruction;
 
 // ***** Actor creation *****