Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use signals and callbacks to trace actors
[simgrid.git] / src / s4u / s4u_Actor.cpp
index c4ac691..b342ac4 100644 (file)
@@ -3,11 +3,12 @@
 /* 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. */
 
+#include "simgrid/actor.h"
 #include "simgrid/s4u/Actor.hpp"
 #include "simgrid/s4u/Exec.hpp"
 #include "simgrid/s4u/Host.hpp"
-#include "src/instr/instr_private.hpp"
 #include "src/simix/smx_private.hpp"
+#include <sstream>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor, "S4U actors");
 
@@ -15,6 +16,10 @@ namespace simgrid {
 namespace s4u {
 
 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_creation;
+simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_suspend;
+simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_resume;
+simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_migration_start;
+simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_migration_end;
 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_destruction;
 
 // ***** Actor creation *****
@@ -69,7 +74,7 @@ void Actor::set_auto_restart(bool autorestart)
 
 void Actor::on_exit(int_f_pvoid_pvoid_t fun, void* data)
 {
-  simcall_process_on_exit(pimpl_, fun, data);
+  simgrid::simix::kernelImmediate([this, fun, data] { SIMIX_process_on_exit(pimpl_, fun, data); });
 }
 
 /** @brief Moves the actor to another host
@@ -83,23 +88,7 @@ void Actor::on_exit(int_f_pvoid_pvoid_t fun, void* data)
  */
 void Actor::migrate(Host* new_host)
 {
-  std::string key;
-  simgrid::instr::LinkType* link = nullptr;
-  bool tracing                   = TRACE_actor_is_enabled();
-  if (tracing) {
-    static long long int counter = 0;
-
-    key = std::to_string(counter);
-    counter++;
-
-    // start link
-    container_t actor_container = simgrid::instr::Container::byName(instr_pid(this));
-    link                        = simgrid::instr::Container::getRoot()->getLink("ACTOR_LINK");
-    link->startEvent(actor_container, "M", key);
-
-    // destroy existing container of this process
-    actor_container->removeFromParent();
-  }
+  s4u::Actor::on_migration_start(this);
 
   simgrid::simix::kernelImmediate([this, new_host]() {
     if (pimpl_->waiting_synchro != nullptr) {
@@ -113,12 +102,7 @@ void Actor::migrate(Host* new_host)
     SIMIX_process_change_host(this->pimpl_, new_host);
   });
 
-  if (tracing) {
-    // create new container on the new_host location
-    simgrid::instr::Container::byName(new_host->get_name())->createChild(instr_pid(this), "ACTOR");
-    // end link
-    link->endEvent(simgrid::instr::Container::byName(instr_pid(this)), "M", key);
-  }
+  s4u::Actor::on_migration_end(this);
 }
 
 s4u::Host* Actor::get_host()
@@ -158,17 +142,14 @@ aid_t Actor::get_ppid() const
 
 void Actor::suspend()
 {
-  if (TRACE_actor_is_enabled())
-    simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->pushEvent("suspend");
-
+  s4u::Actor::on_suspend(this);
   simcall_process_suspend(pimpl_);
 }
 
 void Actor::resume()
 {
   simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
-  if (TRACE_actor_is_enabled())
-    simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->popEvent();
+  s4u::Actor::on_resume(this);
 }
 
 int Actor::is_suspended()
@@ -225,7 +206,8 @@ ActorPtr Actor::by_pid(aid_t pid)
 
 void Actor::kill_all()
 {
-  simcall_process_killall();
+  smx_actor_t self = SIMIX_process_self();
+  simgrid::simix::kernelImmediate([&self] { SIMIX_process_killall(self); });
 }
 
 std::map<std::string, std::string>* Actor::get_properties()
@@ -354,20 +336,17 @@ Host* get_host()
 
 void suspend()
 {
-  if (TRACE_actor_is_enabled())
-    instr::Container::byName(get_name() + "-" + std::to_string(get_pid()))
-        ->getState("ACTOR_STATE")
-        ->pushEvent("suspend");
-  simcall_process_suspend(SIMIX_process_self());
+  smx_actor_t actor = SIMIX_process_self();
+  simgrid::s4u::Actor::on_suspend(actor->iface());
+
+  simcall_process_suspend(actor);
 }
 
 void resume()
 {
   smx_actor_t process = SIMIX_process_self();
   simgrid::simix::kernelImmediate([process] { process->resume(); });
-
-  if (TRACE_actor_is_enabled())
-    instr::Container::byName(get_name() + "-" + std::to_string(get_pid()))->getState("ACTOR_STATE")->popEvent();
+  simgrid::s4u::Actor::on_resume(process->iface());
 }
 
 bool is_suspended()
@@ -384,7 +363,7 @@ void kill()
 
 void on_exit(int_f_pvoid_pvoid_t fun, void* data)
 {
-  simcall_process_on_exit(SIMIX_process_self(), fun, data);
+  SIMIX_process_self()->iface()->on_exit(fun, data);
 }
 
 /** @brief Moves the current actor to another host
@@ -460,6 +439,19 @@ int sg_actor_get_PPID(sg_actor_t actor)
   return actor->get_ppid();
 }
 
+/** \ingroup m_actor_management
+ *
+ * \brief Return a #sg_actor_t given its PID.
+ *
+ * This function search in the list of all the created sg_actor_t for a sg_actor_t  whose PID is equal to \a PID.
+ * If none is found, \c nullptr is returned.
+   Note that the PID are unique in the whole simulation, not only on a given host.
+ */
+sg_actor_t sg_actor_by_PID(aid_t pid)
+{
+  return simgrid::s4u::Actor::by_pid(pid).get();
+}
+
 /** \ingroup m_actor_management
  * \brief Return the name of an actor.
  */