Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
authorMartin Quinson <martin.quinson@loria.fr>
Wed, 21 Feb 2018 22:04:42 +0000 (23:04 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Wed, 21 Feb 2018 22:04:42 +0000 (23:04 +0100)
examples/s4u/CMakeLists.txt
examples/s4u/README.doc
examples/s4u/actor-kill-pid/s4u-actor-kill-pid.cpp [new file with mode: 0644]
examples/s4u/actor-kill-pid/s4u-actor-kill-pid.tesh [new file with mode: 0644]
tools/tesh/tesh.py

index 22ef7d3..29d75ac 100644 (file)
@@ -1,4 +1,5 @@
-foreach (example actor-create actor-daemon actor-join actor-kill actor-lifetime actor-migration actor-suspend actor-yield
+foreach (example actor-create actor-daemon actor-join actor-kill actor-kill-pid
+                 actor-lifetime actor-migration actor-suspend actor-yield
                  app-chainsend app-masterworker app-pingpong app-token-ring
                  async-wait async-waitany async-waitall
                  cloud-capping cloud-migration cloud-simple
@@ -83,7 +84,8 @@ set(txt_files     ${txt_files}    ${CMAKE_CURRENT_SOURCE_DIR}/replay-comm/s4u-re
                                   ${CMAKE_CURRENT_SOURCE_DIR}/replay-storage/s4u-replay-storage.txt
                                   ${CMAKE_CURRENT_SOURCE_DIR}/README.doc                                   PARENT_SCOPE)
 
-foreach(example actor-create actor-daemon actor-join actor-kill actor-lifetime actor-migration actor-suspend actor-yield
+foreach(example actor-create actor-daemon actor-join actor-kill actor-kill-pid
+                actor-lifetime actor-migration actor-suspend actor-yield
                 app-bittorrent app-chainsend app-masterworker app-pingpong app-token-ring 
                 async-wait async-waitall async-waitany
                 cloud-capping cloud-migration cloud-simple
index cafaae8..1780dde 100644 (file)
@@ -58,7 +58,12 @@ TODO: document here the examples about plugins
   - <b>Kill actors</b>.
     @ref examples/s4u/actor-kill/s4u-actor-kill.cpp \n
     Actors can forcefully stop other actors with the @ref
-    simgrid::s4u::Actor::kill() method.
+    simgrid::s4u::Actor::kill(void) method.
+
+  - <b>Kill actors (other function)</b>.
+    @ref examples/s4u/actor-kill-pid/s4u-actor-kill-pid.cpp \n
+    Actors can forcefully stop other actors with the @ref
+    simgrid::s4u::Actor::kill(aid_t) method.
 
   - <b>Controling the actor life cycle from the XML</b>.
     @ref examples/s4u/actor-lifetime/s4u-actor-lifetime.cpp 
@@ -344,4 +349,4 @@ than the previous examples.
 @example examples/platforms/energy_platform.xml
 @example examples/platforms/prop.xml
 
-*/
\ No newline at end of file
+*/
diff --git a/examples/s4u/actor-kill-pid/s4u-actor-kill-pid.cpp b/examples/s4u/actor-kill-pid/s4u-actor-kill-pid.cpp
new file mode 100644 (file)
index 0000000..3b8d5a3
--- /dev/null
@@ -0,0 +1,83 @@
+/* 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_kill_pid, "Messages specific for this s4u example");
+
+static int on_exit(void*, void*)
+{
+  XBT_INFO("I have been killed!");
+  return 0;
+}
+
+static void victimA_fun()
+{
+  simgrid::s4u::this_actor::onExit(on_exit, nullptr);
+  XBT_INFO("Hello!");
+  XBT_INFO("Suspending myself");
+  simgrid::s4u::this_actor::suspend(); /* - Start by suspending itself */
+  XBT_INFO("OK, OK. Let's work");      /* - Then is resumed and start to execute a task */
+  simgrid::s4u::this_actor::execute(1e9);
+  XBT_INFO("Bye!"); /* - But will never reach the end of it */
+}
+
+static void victimB_fun()
+{
+  XBT_INFO("Terminate before being killed");
+}
+
+static void killer()
+{
+  XBT_INFO("Hello!"); /* - First start a victim process */
+  simgrid::s4u::ActorPtr victimA =
+      simgrid::s4u::Actor::createActor("victim A", simgrid::s4u::Host::by_name("Fafard"), victimA_fun);
+  simgrid::s4u::ActorPtr victimB =
+      simgrid::s4u::Actor::createActor("victim B", simgrid::s4u::Host::by_name("Jupiter"), victimB_fun);
+  simgrid::s4u::this_actor::sleep_for(10); /* - Wait for 10 seconds */
+
+  XBT_INFO("Resume the victim A"); /* - Resume it from its suspended state */
+  victimA->resume();
+  simgrid::s4u::this_actor::sleep_for(2);
+
+  aid_t pidA = victimA->getPid();
+  XBT_INFO("Kill the victim A (pid=%lu)", pidA); /* - and then kill it */
+  simgrid::s4u::Actor::kill(pidA);
+
+  aid_t pidB = victimB->getPid();
+  XBT_INFO("Kill victimB (pid=%lu), even if it's already dead", pidB); /* that's a no-op, there is no zombies in SimGrid */
+  try
+  {
+    simgrid::s4u::Actor::kill(pidB);
+  }
+  catch (const std::runtime_error &)
+  {}
+  simgrid::s4u::this_actor::sleep_for(1);
+
+  XBT_INFO("Killing everybody but myself");
+  simgrid::s4u::Actor::killAll();
+
+  aid_t pidMe = simgrid::s4u::this_actor::getPid();
+  XBT_INFO("OK, goodbye now. I commit a suicide (pid=%lu).", pidMe);
+  simgrid::s4u::Actor::kill(pidMe);
+
+  XBT_INFO("This line will never get displayed: I'm already dead since the previous line.");
+}
+
+int main(int argc, char* argv[])
+{
+  simgrid::s4u::Engine e(&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 killer process, that will create the victim actors  */
+  simgrid::s4u::Actor::createActor("killer", simgrid::s4u::Host::by_name("Tremblay"), killer);
+
+  e.run(); /* - Run the simulation */
+
+  XBT_INFO("Simulation time %g", e.getClock());
+
+  return 0;
+}
diff --git a/examples/s4u/actor-kill-pid/s4u-actor-kill-pid.tesh b/examples/s4u/actor-kill-pid/s4u-actor-kill-pid.tesh
new file mode 100644 (file)
index 0000000..e6171fc
--- /dev/null
@@ -0,0 +1,15 @@
+#!/usr/bin/env tesh
+
+$ $SG_TEST_EXENV ${bindir:=.}/s4u-actor-kill-pid ${platfdir}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n"
+>[  0.000000] (killer@Tremblay) Hello!
+>[  0.000000] (victim A@Fafard) Hello!
+>[  0.000000] (victim A@Fafard) Suspending myself
+>[  0.000000] (victim B@Jupiter) Terminate before being killed
+>[ 10.000000] (killer@Tremblay) Resume the victim A
+>[ 10.000000] (victim A@Fafard) OK, OK. Let's work
+>[ 12.000000] (killer@Tremblay) Kill the victim A (pid=2)
+>[ 12.000000] (killer@Tremblay) Kill victimB (pid=3), even if it's already dead
+>[ 13.000000] (killer@Tremblay) Killing everybody but myself
+>[ 13.000000] (victim A@Fafard) I have been killed!
+>[ 13.000000] (killer@Tremblay) OK, goodbye now. I commit a suicide (pid=1).
+>[ 13.000000] (maestro@) Simulation time 13
index a8d52eb..6a2df55 100755 (executable)
@@ -39,8 +39,6 @@ if sys.version_info[0] == 3:
 else:
     raise "This program is expected to run with Python3 only"
 
-
-
 ##############
 #
 # Utilities
@@ -98,6 +96,26 @@ except NameError:
     #py2
     FileNotFoundError = OSError
 
+##############
+#
+# Cleanup on signal
+#
+#
+
+# Global variable. Stores which process group should be killed (or -1 if none)
+pgtokill = -1
+
+def kill_process_group(pgid):
+    # print("Kill process group {}".format(pgid))
+    os.killpg(pgid, signal.SIGTERM)
+
+def signal_handler(signal, frame):
+    print("Caught signal {}".format(SIGNALS_TO_NAMES_DICT[signal]))
+    if pgtokill != -1:
+        kill_process_group(pgtokill)
+    tesh_exit(5)
+
+
 
 ##############
 #
@@ -289,8 +307,11 @@ class Cmd(object):
         args = shlex.split(self.args)
         #print (args)
 
+        global pgtokill
+
         try:
             proc = subprocess.Popen(args, bufsize=1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, preexec_fn=os.setsid)
+            pgtokill = os.getpgid(proc.pid)
         except FileNotFoundError:
             print("["+FileReader().filename+":"+str(self.linenumber)+"] Cannot start '"+args[0]+"': File not found")
             tesh_exit(3)
@@ -302,9 +323,10 @@ class Cmd(object):
         cmdName = FileReader().filename+":"+str(self.linenumber)
         try:
             (stdout_data, stderr_data) = proc.communicate("\n".join(self.input_pipe), self.timeout)
+            pgtokill = -1
         except subprocess.TimeoutExpired:
             print("Test suite `"+FileReader().filename+"': NOK (<"+cmdName+"> timeout after "+str(self.timeout)+" sec)")
-            os.killpg(os.getpgid(proc.pid), signal.SIGKILL)
+            kill_process_group(pgtokill)
             tesh_exit(3)
 
         if self.output_display:
@@ -396,6 +418,8 @@ class Cmd(object):
 
 
 if __name__ == '__main__':
+    signal.signal(signal.SIGINT, signal_handler)
+    signal.signal(signal.SIGTERM, signal_handler)
 
     parser = argparse.ArgumentParser(description='tesh -- testing shell', add_help=True)
     group1 = parser.add_argument_group('Options')