From: Frederic Suter Date: Mon, 20 Mar 2017 21:56:06 +0000 (+0100) Subject: attempt to extend S4U to migrate actors X-Git-Tag: v3_15~30^2~4 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/022dfa9c45540c987b7e6095d89f4b051fba02ed attempt to extend S4U to migrate actors probably need a cleaning/documentation pass lack suspend/resume to mimic C example --- diff --git a/.gitignore b/.gitignore index feeb1ee395..9c402a91cb 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/examples/s4u/CMakeLists.txt b/examples/s4u/CMakeLists.txt index 13d30b084c..9c6c9b055b 100644 --- a/examples/s4u/CMakeLists.txt +++ b/examples/s4u/CMakeLists.txt @@ -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 index 0000000000..c5ecf5fb8a --- /dev/null +++ b/examples/s4u/actor-migration/s4u_actor-migration.cpp @@ -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 +#include + +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 index 0000000000..ef6fad7096 --- /dev/null +++ b/examples/s4u/actor-migration/s4u_actor-migration.tesh @@ -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 diff --git a/examples/s4u/app-masterworker/s4u_app-masterworker.cpp b/examples/s4u/app-masterworker/s4u_app-masterworker.cpp index 69539ae950..bc5d34a138 100644 --- a/examples/s4u/app-masterworker/s4u_app-masterworker.cpp +++ b/examples/s4u/app-masterworker/s4u_app-masterworker.cpp @@ -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); } diff --git a/include/simgrid/s4u/Actor.hpp b/include/simgrid/s4u/Actor.hpp index b5047d9e21..016799a33a 100644 --- a/include/simgrid/s4u/Actor.hpp +++ b/include/simgrid/s4u/Actor.hpp @@ -134,7 +134,6 @@ namespace s4u { /** @brief Simulation Agent */ XBT_PUBLIC_CLASS Actor : public simgrid::xbt::Extendable { - 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); }; /** @} */ diff --git a/include/simgrid/s4u/conditionVariable.hpp b/include/simgrid/s4u/conditionVariable.hpp index d3e777e6ae..47451e69ca 100644 --- a/include/simgrid/s4u/conditionVariable.hpp +++ b/include/simgrid/s4u/conditionVariable.hpp @@ -48,6 +48,7 @@ public: // Wait functions without time: + void wait(MutexPtr lock); void wait(std::unique_lock& lock); template void wait(std::unique_lock& lock, P pred) diff --git a/src/s4u/s4u_actor.cpp b/src/s4u/s4u_actor.cpp index abc984afc5..97d7ec1f32 100644 --- a/src/s4u/s4u_actor.cpp +++ b/src/s4u/s4u_actor.cpp @@ -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); +} } } } diff --git a/src/s4u/s4u_conditionVariable.cpp b/src/s4u/s4u_conditionVariable.cpp index 3074f1253d..be528ba20c 100644 --- a/src/s4u/s4u_conditionVariable.cpp +++ b/src/s4u/s4u_conditionVariable.cpp @@ -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& lock) { simcall_cond_wait(cond_, lock.mutex()->mutex_); } diff --git a/src/simix/ActorImpl.cpp b/src/simix/ActorImpl.cpp index 502ffeaf12..bcce486833 100644 --- a/src/simix/ActorImpl.cpp +++ b/src/simix/ActorImpl.cpp @@ -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");