From 9b466758c546f0cbf96e8c069daa2c08887452a4 Mon Sep 17 00:00:00 2001 From: Frederic Suter Date: Tue, 4 Jul 2017 15:40:03 +0200 Subject: [PATCH] add daemonize for s4u actors too --- examples/s4u/CMakeLists.txt | 4 +- .../s4u/actor-daemon/s4u_actor-daemon.cpp | 43 +++++++++++++++++++ .../s4u/actor-daemon/s4u_actor-daemon.tesh | 11 +++++ include/simgrid/s4u/Actor.hpp | 2 +- src/msg/msg_process.cpp | 4 +- src/s4u/s4u_actor.cpp | 5 +++ 6 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 examples/s4u/actor-daemon/s4u_actor-daemon.cpp create mode 100644 examples/s4u/actor-daemon/s4u_actor-daemon.tesh diff --git a/examples/s4u/CMakeLists.txt b/examples/s4u/CMakeLists.txt index 088ef9eb89..dfdc6340af 100644 --- a/examples/s4u/CMakeLists.txt +++ b/examples/s4u/CMakeLists.txt @@ -1,4 +1,4 @@ -foreach (example actions-comm actions-storage actor-create actor-kill actor-migration actor-suspend +foreach (example actions-comm actions-storage actor-create actor-daemon actor-kill actor-migration actor-suspend app-masterworker app-token-ring io mutex ) add_executable (s4u_${example} ${example}/s4u_${example}.cpp) target_link_libraries(s4u_${example} simgrid) @@ -31,7 +31,7 @@ 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 actions-comm actions-storage actor-create actor-kill actor-migration actor-suspend +foreach(example actions-comm actions-storage actor-create actor-daemon actor-kill actor-migration actor-suspend app-masterworker app-token-ring dht-chord io mutex ) 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-daemon/s4u_actor-daemon.cpp b/examples/s4u/actor-daemon/s4u_actor-daemon.cpp new file mode 100644 index 0000000000..5ef84f6116 --- /dev/null +++ b/examples/s4u/actor-daemon/s4u_actor-daemon.cpp @@ -0,0 +1,43 @@ +/* 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" + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_daemon, "Messages specific for this s4u example"); + +/* The worker process, working for a while before leaving */ +static void worker() +{ + XBT_INFO("Let's do some work (for 10 sec on Boivin)."); + simgrid::s4u::this_actor::execute(980.95e6); + + XBT_INFO("I'm done now. I leave even if it makes the daemon die."); +} + +/* The daemon, displaying a message every 3 seconds until all other processes stop */ +static void my_daemon() +{ + simgrid::s4u::Actor::self()->daemonize(); + + while (1) { + XBT_INFO("Hello from the infinite loop"); + simgrid::s4u::this_actor::sleep_for(3.0); + } + + XBT_INFO("I will never reach that point: daemons are killed when regular processes are done"); +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv); + + e->loadPlatform(argv[1]); + simgrid::s4u::Actor::createActor("worker", simgrid::s4u::Host::by_name("Boivin"), worker); + simgrid::s4u::Actor::createActor("daemon", simgrid::s4u::Host::by_name("Tremblay"), my_daemon); + + e->run(); + + return 0; +} diff --git a/examples/s4u/actor-daemon/s4u_actor-daemon.tesh b/examples/s4u/actor-daemon/s4u_actor-daemon.tesh new file mode 100644 index 0000000000..bc422371c1 --- /dev/null +++ b/examples/s4u/actor-daemon/s4u_actor-daemon.tesh @@ -0,0 +1,11 @@ +#! ./tesh + +p Testing the process daemonization feature + +$ $SG_TEST_EXENV ${bindir:=.}/s4u_actor-daemon ${srcdir:=.}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n" +> [ 0.000000] (worker@Boivin) Let's do some work (for 10 sec on Boivin). +> [ 0.000000] (daemon@Tremblay) Hello from the infinite loop +> [ 3.000000] (daemon@Tremblay) Hello from the infinite loop +> [ 6.000000] (daemon@Tremblay) Hello from the infinite loop +> [ 9.000000] (daemon@Tremblay) Hello from the infinite loop +> [ 10.000000] (worker@Boivin) I'm done now. I leave even if it makes the daemon die. diff --git a/include/simgrid/s4u/Actor.hpp b/include/simgrid/s4u/Actor.hpp index d50ac131e3..f8febaaecd 100644 --- a/include/simgrid/s4u/Actor.hpp +++ b/include/simgrid/s4u/Actor.hpp @@ -195,7 +195,7 @@ public: // ***** Methods ***** /** This actor will be automatically terminated when the last non-daemon process finishes **/ - void deamonize(); + void daemonize(); /** Retrieves the name of that actor as a C string */ const char* cname(); diff --git a/src/msg/msg_process.cpp b/src/msg/msg_process.cpp index ffe758f351..e8e8b2f3a7 100644 --- a/src/msg/msg_process.cpp +++ b/src/msg/msg_process.cpp @@ -479,9 +479,7 @@ XBT_PUBLIC(msg_process_t) MSG_process_restart(msg_process_t process) { */ XBT_PUBLIC(void) MSG_process_daemonize(msg_process_t process) { - simgrid::simix::kernelImmediate([process]() { - process->getImpl()->daemonize(); - }); + simgrid::simix::kernelImmediate([process]() { process->getImpl()->daemonize(); }); } /** @ingroup m_process_management diff --git a/src/s4u/s4u_actor.cpp b/src/s4u/s4u_actor.cpp index 3b27ed8473..f9b2cf90b0 100644 --- a/src/s4u/s4u_actor.cpp +++ b/src/s4u/s4u_actor.cpp @@ -80,6 +80,11 @@ s4u::Host* Actor::host() return this->pimpl_->host; } +void Actor::daemonize() +{ + simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); }); +} + const char* Actor::cname() { return this->pimpl_->name.c_str(); -- 2.20.1