From: Martin Quinson Date: Thu, 3 Jan 2019 22:07:16 +0000 (+0100) Subject: yet another example: actor-suspend.py X-Git-Tag: v3_22~704 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/2b087eb7fda569c229b83497f9bf86730ca64b86?hp=29cb40ec5bfe53aae2f519ddb9c766455bd021b8 yet another example: actor-suspend.py --- diff --git a/examples/python/CMakeLists.txt b/examples/python/CMakeLists.txt index 7ba51894d8..367f7954d2 100644 --- a/examples/python/CMakeLists.txt +++ b/examples/python/CMakeLists.txt @@ -1,4 +1,4 @@ -foreach(example actor-create actor-join actor-migrate actor-yield +foreach(example actor-create actor-join actor-migrate actor-suspend actor-yield exec-basic) set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/${example}.tesh) set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/${example}.py) diff --git a/examples/python/actor-suspend/actor-suspend.py b/examples/python/actor-suspend/actor-suspend.py new file mode 100644 index 0000000000..e3036edeac --- /dev/null +++ b/examples/python/actor-suspend/actor-suspend.py @@ -0,0 +1,70 @@ +# Copyright (c) 2017-2018. 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. + +from simgrid import * +import sys + +# The Lazy guy only wants to sleep, but can be awaken by the dream_master process. +def lazy_guy(): + this_actor.info("Nobody's watching me ? Let's go to sleep.") + this_actor.suspend() # - Start by suspending itself + this_actor.info("Uuuh ? Did somebody call me ?") + + this_actor.info("Going to sleep...") # - Then repetitively go to sleep, but get awaken + this_actor.sleep_for(10) + this_actor.info("Mmm... waking up.") + + this_actor.info("Going to sleep one more time (for 10 sec)...") + this_actor.sleep_for(10) + this_actor.info("Waking up once for all!") + + this_actor.info("Ok, let's do some work, then (for 10 sec on Boivin).") + this_actor.execute(980.95e6) + + this_actor.info("Mmmh, I'm done now. Goodbye.") + +# The Dream master +def dream_master(): + this_actor.info("Let's create a lazy guy.") # Create a lazy_guy process + lazy = Actor.create("Lazy", this_actor.get_host(), lazy_guy) + this_actor.info("Let's wait a little bit...") + this_actor.sleep_for(10) # Wait for 10 seconds + this_actor.info("Let's wake the lazy guy up! >:) BOOOOOUUUHHH!!!!") + lazy.resume() # Then wake up the lazy_guy + + this_actor.sleep_for(5) # Repeat two times: + this_actor.info("Suspend the lazy guy while he's sleeping...") + lazy.suspend() # Suspend the lazy_guy while he's asleep + this_actor.info("Let him finish his siesta.") + this_actor.sleep_for(10) # Wait for 10 seconds + this_actor.info("Wake up, lazy guy!") + lazy.resume() # Then wake up the lazy_guy again + + this_actor.sleep_for(5) + this_actor.info("Suspend again the lazy guy while he's sleeping...") + lazy.suspend() + this_actor.info("This time, don't let him finish his siesta.") + this_actor.sleep_for(2) + this_actor.info("Wake up, lazy guy!") + lazy.resume() + + this_actor.sleep_for(5) + this_actor.info("Give a 2 seconds break to the lazy guy while he's working...") + lazy.suspend() + this_actor.sleep_for(2) + this_actor.info("Back to work, lazy guy!") + lazy.resume() + + this_actor.info("OK, I'm done here.") + +if __name__ == '__main__': + e = Engine(sys.argv) + if len(sys.argv) < 2: raise AssertionError("Usage: actor-suspend.py platform_file [other parameters]") + + e.load_platform(sys.argv[1]) # Load the platform description + list = e.get_all_hosts() + Actor.create("dream_master", list[0], dream_master) + + e.run() # Run the simulation diff --git a/examples/python/actor-suspend/actor-suspend.tesh b/examples/python/actor-suspend/actor-suspend.tesh new file mode 100644 index 0000000000..e64839820e --- /dev/null +++ b/examples/python/actor-suspend/actor-suspend.tesh @@ -0,0 +1,26 @@ +#!/usr/bin/env tesh + +p Testing the suspend/resume feature of SimGrid + +! output sort 19 +$ python3 ${bindir:=.}/actor-suspend.py ${platfdir}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n" +> [ 0.000000] (dream_master@Boivin) Let's create a lazy guy. +> [ 0.000000] (Lazy@Boivin) Nobody's watching me ? Let's go to sleep. +> [ 0.000000] (dream_master@Boivin) Let's wait a little bit... +> [ 10.000000] (dream_master@Boivin) Let's wake the lazy guy up! >:) BOOOOOUUUHHH!!!! +> [ 10.000000] (Lazy@Boivin) Uuuh ? Did somebody call me ? +> [ 10.000000] (Lazy@Boivin) Going to sleep... +> [ 15.000000] (dream_master@Boivin) Suspend the lazy guy while he's sleeping... +> [ 15.000000] (dream_master@Boivin) Let him finish his siesta. +> [ 25.000000] (dream_master@Boivin) Wake up, lazy guy! +> [ 25.000000] (Lazy@Boivin) Mmm... waking up. +> [ 25.000000] (Lazy@Boivin) Going to sleep one more time (for 10 sec)... +> [ 30.000000] (dream_master@Boivin) Suspend again the lazy guy while he's sleeping... +> [ 30.000000] (dream_master@Boivin) This time, don't let him finish his siesta. +> [ 32.000000] (dream_master@Boivin) Wake up, lazy guy! +> [ 35.000000] (Lazy@Boivin) Waking up once for all! +> [ 35.000000] (Lazy@Boivin) Ok, let's do some work, then (for 10 sec on Boivin). +> [ 37.000000] (dream_master@Boivin) Give a 2 seconds break to the lazy guy while he's working... +> [ 39.000000] (dream_master@Boivin) Back to work, lazy guy! +> [ 39.000000] (dream_master@Boivin) OK, I'm done here. +> [ 47.000000] (Lazy@Boivin) Mmmh, I'm done now. Goodbye. diff --git a/src/bindings/python/simgrid_python.cpp b/src/bindings/python/simgrid_python.cpp index d42f180245..ca377e66bc 100644 --- a/src/bindings/python/simgrid_python.cpp +++ b/src/bindings/python/simgrid_python.cpp @@ -79,6 +79,7 @@ PYBIND11_MODULE(simgrid, m) // Currently this can be dangling, we should wrap this somehow. return new simgrid::s4u::Engine(&argc, argv.get()); })) + .def("get_all_hosts", &Engine::get_all_hosts, "Returns the list of all hosts found in the platform") .def("get_clock", &Engine::get_clock, "Retrieve the simulation time") .def("load_platform", &Engine::load_platform, "Load a platform file describing the environment, see :cpp:func:`simgrid::s4u::Engine::load_platform()`")