Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
attempt to extend S4U to migrate actors
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Mon, 20 Mar 2017 21:56:06 +0000 (22:56 +0100)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Mon, 20 Mar 2017 21:56:06 +0000 (22:56 +0100)
probably need a cleaning/documentation pass
lack suspend/resume to mimic C example

.gitignore
examples/s4u/CMakeLists.txt
examples/s4u/actor-migration/s4u_actor-migration.cpp [new file with mode: 0644]
examples/s4u/actor-migration/s4u_actor-migration.tesh [new file with mode: 0644]
examples/s4u/app-masterworker/s4u_app-masterworker.cpp
include/simgrid/s4u/Actor.hpp
include/simgrid/s4u/conditionVariable.hpp
src/s4u/s4u_actor.cpp
src/s4u/s4u_conditionVariable.cpp
src/simix/ActorImpl.cpp

index feeb1ee..9c402a9 100644 (file)
@@ -190,6 +190,7 @@ examples/msg/trace-host-user-variables/trace-host-user-variables
 examples/s4u/app-masterworker/s4u_app-masterworker
 examples/s4u/app-token-ring/s4u_app-token-ring
 examples/s4u/actions-comm/s4u_actions-comm
+examples/s4u/actions-storage/s4u_actions-storage
 examples/s4u/basic/s4u_basic
 examples/s4u/basic/s4u_basic_deployment
 examples/s4u/basic/s4u_basic_function
index 13d30b0..9c6c9b0 100644 (file)
@@ -1,4 +1,4 @@
-foreach (example app-masterworker app-token-ring io launching mutex actions-comm actions-storage)
+foreach (example actor-migration app-masterworker app-token-ring io launching mutex actions-comm actions-storage)
   add_executable       (s4u_${example}  ${example}/s4u_${example}.cpp)
   target_link_libraries(s4u_${example}  simgrid)
   set_target_properties(s4u_${example}  PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${example})
@@ -20,6 +20,6 @@ set(txt_files     ${txt_files}    ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u_a
                                   ${CMAKE_CURRENT_SOURCE_DIR}/actions-storage/s4u_actions-storage.txt
                                   ${CMAKE_CURRENT_SOURCE_DIR}/README.doc                                   PARENT_SCOPE)
 
-foreach(example app-masterworker app-token-ring io launching mutex actions-comm actions-storage)
+foreach(example actor-migration app-masterworker app-token-ring io launching mutex actions-comm actions-storage)
   ADD_TESH_FACTORIES(s4u-${example} "thread;ucontext;raw;boost" --setenv bindir=${CMAKE_CURRENT_BINARY_DIR}/${example} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --cd ${CMAKE_HOME_DIRECTORY}/examples/s4u/${example} s4u_${example}.tesh)
 endforeach()
diff --git a/examples/s4u/actor-migration/s4u_actor-migration.cpp b/examples/s4u/actor-migration/s4u_actor-migration.cpp
new file mode 100644 (file)
index 0000000..c5ecf5f
--- /dev/null
@@ -0,0 +1,76 @@
+/* Copyright (c) 2017. The SimGrid Team. All rights reserved.          */
+
+/* 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/s4u.hpp>
+#include <simgrid/s4u/Mutex.hpp>
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_migration, "Messages specific for this s4u example");
+
+simgrid::s4u::MutexPtr checkpoint                 = nullptr;
+simgrid::s4u::ConditionVariablePtr identification = nullptr;
+static simgrid::s4u::ActorPtr controlled_process  = nullptr;
+
+/* The Emigrant process will be moved from host to host. */
+static void emigrant()
+{
+  XBT_INFO("I'll look for a new job on another machine ('Boivin') where the grass is greener.");
+  simgrid::s4u::this_actor::migrate(
+      simgrid::s4u::Host::by_name("Boivin")); /* - First, move to another host by myself */
+
+  XBT_INFO("Yeah, found something to do");
+  simgrid::s4u::this_actor::execute(98095000);
+  simgrid::s4u::this_actor::sleep_for(2);
+
+  XBT_INFO("Moving back home after work");
+  simgrid::s4u::this_actor::migrate(simgrid::s4u::Host::by_name("Jacquelin")); /* - Move back to original location */
+
+  simgrid::s4u::this_actor::migrate(simgrid::s4u::Host::by_name("Boivin")); /* - Go back to the other host to sleep*/
+  simgrid::s4u::this_actor::sleep_for(4);
+
+  checkpoint->lock();                               /* - Get controlled at checkpoint */
+  controlled_process = simgrid::s4u::Actor::self(); /* - and get moved back by the policeman process */
+  identification->notify_all();
+  checkpoint->unlock();
+
+  // TODO simgrid::s4u::this_actor::suspend(); to replace the sleep below
+  simgrid::s4u::this_actor::sleep_for(4);
+  XBT_INFO("I've been moved on this new host: %s", simgrid::s4u::this_actor::host()->cname());
+  XBT_INFO("Uh, nothing to do here. Stopping now");
+}
+
+/* The policeman check for emigrants and move them back to 'Jacquelin' */
+static void policeman()
+{
+  checkpoint->lock();
+
+  XBT_INFO("Wait at the checkpoint."); /* - block on the mutex+condition */
+  while (controlled_process == nullptr)
+    identification->wait(checkpoint);
+
+  controlled_process->migrate(simgrid::s4u::Host::by_name("Jacquelin")); /* - Move an emigrant to Jacquelin */
+  XBT_INFO("I moved the emigrant");
+  // TODO simgrid::s4u::this_actor::resume()
+
+  checkpoint->unlock();
+}
+
+int main(int argc, char* argv[])
+{
+  simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
+  xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
+  e->loadPlatform(argv[1]); /* - Load the platform description */
+
+  /* - Create and deploy the emigrant and policeman processes */
+  simgrid::s4u::Actor::createActor("emigrant", simgrid::s4u::Host::by_name("Jacquelin"), emigrant);
+  simgrid::s4u::Actor::createActor("policeman", simgrid::s4u::Host::by_name("Boivin"), policeman);
+
+  checkpoint     = simgrid::s4u::Mutex::createMutex(); /* - Initiate the mutex and conditions */
+  identification = simgrid::s4u::ConditionVariable::createConditionVariable();
+  e->run();
+
+  XBT_INFO("Simulation time %g", e->getClock());
+
+  return 0;
+}
diff --git a/examples/s4u/actor-migration/s4u_actor-migration.tesh b/examples/s4u/actor-migration/s4u_actor-migration.tesh
new file mode 100644 (file)
index 0000000..ef6fad7
--- /dev/null
@@ -0,0 +1,14 @@
+#! ./tesh
+
+p Testing the migration feature of MSG
+
+! output sort 19
+$ $SG_TEST_EXENV ${bindir:=.}/s4u_actor-migration ${srcdir:=.}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n"
+> [  0.000000] (emigrant@Jacquelin) I'll look for a new job on another machine ('Boivin') where the grass is greener.
+> [  0.000000] (emigrant@Boivin) Yeah, found something to do
+> [  0.000000] (policeman@Boivin) Wait at the checkpoint.
+> [  3.000000] (emigrant@Boivin) Moving back home after work
+> [  7.000000] (policeman@Boivin) I moved the emigrant
+> [ 11.000000] (emigrant@Jacquelin) I've been moved on this new host: Jacquelin
+> [ 11.000000] (emigrant@Jacquelin) Uh, nothing to do here. Stopping now
+> [ 11.000000] (maestro@) Simulation time 11
index 69539ae..bc5d34a 100644 (file)
@@ -78,7 +78,7 @@ public:
         break;
       }
       /*  - Otherwise, process the task */
-      double comp_size = xbt_str_parse_double(res, nullptr);
+      double comp_size = std::stod(res);
       xbt_free(res);
       simgrid::s4u::this_actor::execute(comp_size);
     }
index b5047d9..016799a 100644 (file)
@@ -134,7 +134,6 @@ namespace s4u {
 /** @brief Simulation Agent */
 XBT_PUBLIC_CLASS Actor : public simgrid::xbt::Extendable<Actor>
 {
-
   friend Mailbox;
   friend simgrid::simix::ActorImpl;
   friend simgrid::kernel::activity::MailboxImpl;
@@ -220,6 +219,7 @@ public:
   /** Retrieves the time at which that actor will be killed (or -1 if not set) */
   double killTime();
 
+  void migrate(Host * new_host);
   /** Ask the actor to die.
    *
    * It will only notice your request when doing a simcall next time (a communication or similar).
@@ -298,6 +298,11 @@ namespace this_actor {
 
   /** @brief Returns the name of the current actor. */
   XBT_PUBLIC(std::string) name();
+
+  /** @brief Returns the name of the host on which the process is running. */
+  XBT_PUBLIC(Host*) host();
+
+  XBT_PUBLIC(void) migrate(Host* new_host);
 };
 
 /** @} */
index d3e777e..47451e6 100644 (file)
@@ -48,6 +48,7 @@ public:
 
   //  Wait functions without time:
 
+  void wait(MutexPtr lock);
   void wait(std::unique_lock<Mutex>& lock);
   template<class P>
   void wait(std::unique_lock<Mutex>& lock, P pred)
index abc984a..97d7ec1 100644 (file)
@@ -52,6 +52,11 @@ void Actor::setAutoRestart(bool autorestart) {
   simcall_process_auto_restart_set(pimpl_,autorestart);
 }
 
+void Actor::migrate(Host* new_host)
+{
+  simcall_process_set_host(pimpl_, new_host);
+}
+
 s4u::Host* Actor::host()
 {
   return this->pimpl_->host;
@@ -173,6 +178,16 @@ std::string name()
 {
   return SIMIX_process_self()->name;
 }
+
+Host* host()
+{
+  return SIMIX_process_self()->host;
+}
+
+void migrate(Host* new_host)
+{
+  simcall_process_set_host(SIMIX_process_self(), new_host);
+}
 }
 }
 }
index 3074f12..be528ba 100644 (file)
@@ -20,6 +20,11 @@ ConditionVariablePtr ConditionVariable::createConditionVariable()
 /**
  * Wait functions
  */
+void ConditionVariable::wait(MutexPtr lock)
+{
+  simcall_cond_wait(cond_, lock->mutex_);
+}
+
 void ConditionVariable::wait(std::unique_lock<Mutex>& lock) {
   simcall_cond_wait(cond_, lock.mutex()->mutex_);
 }
index 502ffea..bcce486 100644 (file)
@@ -553,6 +553,7 @@ void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_actor_t process
 {
   process->new_host = dest;
 }
+
 void SIMIX_process_change_host(smx_actor_t process, sg_host_t dest)
 {
   xbt_assert((process != nullptr), "Invalid parameters");