From: Martin Quinson Date: Mon, 27 Nov 2017 20:10:37 +0000 (+0100) Subject: Merge pull request #244 from Takishipp/actor-yield X-Git-Tag: v3.18~227 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/7bc340a73928fe73a57a9664aeba0cf5a92b654c?hp=5d2730ebe780871aad000c557cbe4e8f414a0134 Merge pull request #244 from Takishipp/actor-yield S4U version of actor-yield example --- diff --git a/examples/s4u/CMakeLists.txt b/examples/s4u/CMakeLists.txt index 5b88ee4b44..3e90de1bdc 100644 --- a/examples/s4u/CMakeLists.txt +++ b/examples/s4u/CMakeLists.txt @@ -1,7 +1,7 @@ foreach (example actions-comm actions-storage actor-create actor-daemon actor-execute actor-kill actor-lifetime actor-migration actor-suspend actor-priority app-masterworker app-pingpong app-token-ring - async-wait async-waitany async-waitall + async-wait async-waitany async-waitall actor-yield energy-link plugin-hostload io mutex) add_executable (s4u-${example} ${example}/s4u-${example}.cpp) @@ -33,6 +33,7 @@ endforeach() set(examples_src ${examples_src} PARENT_SCOPE) set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/app-bittorrent/s4u-app-bittorrent.tesh ${CMAKE_CURRENT_SOURCE_DIR}/dht-chord/s4u-dht-chord.tesh + ${CMAKE_CURRENT_SOURCE_DIR}/actor-yield/s4u-actor-yield.tesh ${CMAKE_CURRENT_SOURCE_DIR}/actor-priority/s4u-actor-priority.tesh ${CMAKE_CURRENT_SOURCE_DIR}/actor-lifetime/s4u-actor-lifetime.tesh ${CMAKE_CURRENT_SOURCE_DIR}/async-wait/s4u-async-wait.tesh @@ -50,6 +51,7 @@ set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u-a ${CMAKE_CURRENT_SOURCE_DIR}/async-waitany/s4u-async-waitany_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/async-waitall/s4u-async-waitall_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/async-wait/s4u-async-wait_d.xml + ${CMAKE_CURRENT_SOURCE_DIR}/actor-yield/s4u-actor-yield_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/dht-chord/s4u-dht-chord_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/actor-lifetime/s4u-actor-lifetime_d.xml PARENT_SCOPE) @@ -62,7 +64,7 @@ set(txt_files ${txt_files} ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u-a foreach(example actions-comm actions-storage actor-create actor-daemon actor-execute actor-kill actor-lifetime actor-migration actor-suspend app-bittorrent app-masterworker app-pingpong app-token-ring - async-wait async-waitall async-waitany actor-priority + async-wait async-waitall async-waitany actor-priority actor-yield dht-chord energy-link plugin-hostload io mutex) diff --git a/examples/s4u/README.doc b/examples/s4u/README.doc index 78254fbd73..f626d7ec70 100644 --- a/examples/s4u/README.doc +++ b/examples/s4u/README.doc @@ -101,6 +101,12 @@ documentation, but it should remain readable directly. @ref examples/s4u/actor-migration/s4u-actor-migration.cpp \n Actors can move or be moved from a host to another with the @ref migrate method. + - Yielding to other actor. + @ref examples/s4u/actor-yield/s4u-actor-yield.c\n + The @ref yield function interrupts the execution of the + current actor, leaving a chance to run to the other actor + that are ready to run at the exact same timestamp + @section s4u_ex_synchro Inter-Actor Synchronization - Mutex: @ref examples/s4u/mutex/s4u-mutex.cpp \n diff --git a/examples/s4u/actor-yield/s4u-actor-yield.cpp b/examples/s4u/actor-yield/s4u-actor-yield.cpp new file mode 100644 index 0000000000..7eea17a88e --- /dev/null +++ b/examples/s4u/actor-yield/s4u-actor-yield.cpp @@ -0,0 +1,51 @@ +/* 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" + +/* This example does not much: It just spans over-polite actor that yield a large amount +* of time before ending. +* +* This serves as an example for the s4u-actor-yield() function, with which an actor can request +* to be rescheduled after the other actor that are ready at the current timestamp. +* +* It can also be used to benchmark our context-switching mechanism. +*/ +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_yield, "Messages specific for this s4u example"); +/* Main function of the Yielder process */ +class yielder { + long number_of_yields; +public: + explicit yielder() = default; + explicit yielder(std::vector args) +{ + number_of_yields = std::stod(args[1]); +} +void operator()() +{ + for (int i = 0; i < number_of_yields; i++) + simgrid::s4u::this_actor::yield(); + XBT_INFO("I yielded %ld times. Goodbye now!", number_of_yields); +} +}; + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + + xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n" + "\tExample: %s platform.xml deployment.xml\n", + argv[0], argv[0]); + + e.loadPlatform(argv[1]); /* - Load the platform description */ + e.registerFunction("yielder"); + std::vector args; + + e.loadDeployment(argv[2]); + + e.run(); /* - Run the simulation */ + + return 0; +} diff --git a/examples/s4u/actor-yield/s4u-actor-yield.tesh b/examples/s4u/actor-yield/s4u-actor-yield.tesh new file mode 100644 index 0000000000..938470db71 --- /dev/null +++ b/examples/s4u/actor-yield/s4u-actor-yield.tesh @@ -0,0 +1,5 @@ +#! ./tesh + +$ $SG_TEST_EXENV ${bindir:=.}/s4u-actor-yield ${srcdir:=.}/small_platform_fatpipe.xml ${srcdir:=.}/../s4u/actor-yield/s4u-actor-yield_d.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n" +> [ 0.000000] (1:yielder@Tremblay) I yielded 10 times. Goodbye now! +> [ 0.000000] (2:yielder@Ruby) I yielded 15 times. Goodbye now! diff --git a/examples/s4u/actor-yield/s4u-actor-yield_d.xml b/examples/s4u/actor-yield/s4u-actor-yield_d.xml new file mode 100644 index 0000000000..b1ab776f57 --- /dev/null +++ b/examples/s4u/actor-yield/s4u-actor-yield_d.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/include/simgrid/s4u/Actor.hpp b/include/simgrid/s4u/Actor.hpp index e9cdd87e85..92fc4852ce 100644 --- a/include/simgrid/s4u/Actor.hpp +++ b/include/simgrid/s4u/Actor.hpp @@ -217,7 +217,9 @@ public: /** Resume a suspended actor by resuming the task on which it was waiting for the completion. */ void resume(); - + + void yield(); + /** Returns true if the actor is suspended. */ int isSuspended(); @@ -347,6 +349,9 @@ XBT_PUBLIC(Host*) getHost(); /** @brief Suspend the actor. */ XBT_PUBLIC(void) suspend(); +/** @brief yield the actor. */ +XBT_PUBLIC(void) yield(); + /** @brief Resume the actor. */ XBT_PUBLIC(void) resume(); diff --git a/src/s4u/s4u_actor.cpp b/src/s4u/s4u_actor.cpp index 1553bb9313..86ea2e2fb4 100644 --- a/src/s4u/s4u_actor.cpp +++ b/src/s4u/s4u_actor.cpp @@ -214,6 +214,11 @@ void sleep_for(double duration) simcall_process_sleep(duration); } +void yield() +{ + simgrid::simix::kernelImmediate([] { /* do nothing*/ }); +} + XBT_PUBLIC(void) sleep_until(double timeout) { double now = SIMIX_get_clock();