From 781aee7fedc47a3c0acc35a738b0ffdecdb15211 Mon Sep 17 00:00:00 2001 From: Frederic Suter Date: Sat, 8 Feb 2020 16:56:22 +0100 Subject: [PATCH] add sg_actor_exit and sg_actor_on_exit + convert the corresponding MSG example to s4u-c + MSG_process_on_exit becomes legacy --- MANIFEST.in | 4 +- examples/c/CMakeLists.txt | 8 +- examples/c/actor-kill/actor-kill.c | 87 ++++++++++++++++++++ examples/c/actor-kill/actor-kill.tesh | 16 ++++ include/simgrid/actor.h | 3 + src/msg/msg_legacy.cpp | 5 ++ src/msg/msg_process.cpp | 9 -- src/s4u/s4u_Actor.cpp | 14 ++++ teshsuite/msg/CMakeLists.txt | 4 +- teshsuite/msg/process-kill/process-kill.c | 53 ------------ teshsuite/msg/process-kill/process-kill.tesh | 12 --- 11 files changed, 135 insertions(+), 80 deletions(-) create mode 100644 examples/c/actor-kill/actor-kill.c create mode 100644 examples/c/actor-kill/actor-kill.tesh delete mode 100644 teshsuite/msg/process-kill/process-kill.c delete mode 100644 teshsuite/msg/process-kill/process-kill.tesh diff --git a/MANIFEST.in b/MANIFEST.in index 86ce6f929c..28116929db 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -23,6 +23,8 @@ include examples/c/actor-daemon/actor-daemon.c include examples/c/actor-daemon/actor-daemon.tesh include examples/c/actor-join/actor-join.c include examples/c/actor-join/actor-join.tesh +include examples/c/actor-kill/actor-kill.c +include examples/c/actor-kill/actor-kill.tesh include examples/c/app-pingpong/app-pingpong.c include examples/c/app-pingpong/app-pingpong.tesh include examples/c/app-pingpong/app-pingpong_d.xml @@ -663,8 +665,6 @@ include teshsuite/msg/platform-properties/platform-properties.tesh include teshsuite/msg/platform-properties/platform-properties_d.xml include teshsuite/msg/plugin-hostload/plugin-hostload.c include teshsuite/msg/plugin-hostload/plugin-hostload.tesh -include teshsuite/msg/process-kill/process-kill.c -include teshsuite/msg/process-kill/process-kill.tesh include teshsuite/msg/process-lifetime/baseline_d.xml include teshsuite/msg/process-lifetime/kill_d.xml include teshsuite/msg/process-lifetime/process-lifetime.c diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 72ae8cfe6a..6f52d4604d 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -1,4 +1,6 @@ -foreach(x actor-create actor-daemon actor-join app-pingpong app-token-ring async-waitany io-disk-raw) +foreach(x + actor-create actor-daemon actor-join actor-kill + app-pingpong app-token-ring async-waitany io-disk-raw) add_executable (${x}-c EXCLUDE_FROM_ALL ${x}/${x}.c) target_link_libraries(${x}-c simgrid) set_target_properties(${x}-c PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${x}) @@ -16,7 +18,9 @@ set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/actor-create/actor-cr ${CMAKE_CURRENT_SOURCE_DIR}/async-waitany/async-waitany_d.xml PARENT_SCOPE) -foreach(x actor-create actor-daemon actor-join app-pingpong app-token-ring async-waitany io-disk-raw) +foreach(x + actor-create actor-daemon actor-join actor-kill + app-pingpong app-token-ring async-waitany io-disk-raw) ADD_TESH(c-${x} --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --setenv bindir=${CMAKE_BINARY_DIR}/examples/c/${x} --cd ${CMAKE_HOME_DIRECTORY}/examples/c/${x} diff --git a/examples/c/actor-kill/actor-kill.c b/examples/c/actor-kill/actor-kill.c new file mode 100644 index 0000000000..e92b3d3db2 --- /dev/null +++ b/examples/c/actor-kill/actor-kill.c @@ -0,0 +1,87 @@ +/* Copyright (c) 2007-2020. 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/actor.h" +#include "simgrid/engine.h" +#include "simgrid/host.h" + +#include "xbt/log.h" +#include "xbt/sysdep.h" + +XBT_LOG_NEW_DEFAULT_CATEGORY(actor_kill, "Messages specific for this example"); + +static int victim_on_exit(XBT_ATTRIB_UNUSED int ignored1, XBT_ATTRIB_UNUSED void* ignored2) +{ + XBT_INFO("I have been killed!"); + return 0; +} + +static void victimA_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[]) +{ + sg_actor_on_exit(&victim_on_exit, NULL); + XBT_INFO("Hello!"); + XBT_INFO("Suspending myself"); + sg_actor_suspend(sg_actor_self()); /* - First suspend itself */ + XBT_INFO("OK, OK. Let's work"); /* - Then is resumed and start to execute a task */ + sg_actor_self_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_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[]) +{ + XBT_INFO("Hello!"); /* - First start a victim process */ + sg_actor_t victimA = sg_actor_init("victim A", sg_host_by_name("Fafard")); + sg_actor_start(victimA, victimA_fun, 0, NULL); + sg_actor_t victimB = sg_actor_init("victim B", sg_host_by_name("Jupiter")); + sg_actor_start(victimB, victimB_fun, 0, NULL); + sg_actor_sleep_for(10.0); + + XBT_INFO("Resume the victim A"); /* - Resume it from its suspended state */ + sg_actor_resume(victimA); + sg_actor_sleep_for(2.0); + + XBT_INFO("Kill the victim A"); /* - and then kill it */ + sg_actor_kill(victimA); + sg_actor_sleep_for(1.0); + + XBT_INFO("Kill victimB, even if it's already dead"); /* that's a no-op, there is no zombies in SimGrid */ + sg_actor_kill(victimB); // the actor is automatically garbage-collected after this last reference + sg_actor_sleep_for(1.0); + + XBT_INFO("Start a new actor, and kill it right away"); + sg_actor_t victimC = sg_actor_init("victim C", sg_host_by_name("Jupiter")); + sg_actor_start(victimC, victimA_fun, 0, NULL); + sg_actor_kill(victimC); + sg_actor_sleep_for(1.0); + + XBT_INFO("Killing everybody but myself"); + sg_actor_kill_all(); + + XBT_INFO("OK, goodbye now. I commit a suicide."); + sg_actor_exit(); + + XBT_INFO("This line will never get displayed: I'm already dead since the previous line."); +} + +int main(int argc, char* argv[]) +{ + simgrid_init(&argc, argv); + xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]); + + simgrid_load_platform(argv[1]); + + /* - Create and deploy killer process, that will create the victim process */ + sg_actor_t killer = sg_actor_init("killer", sg_host_by_name("Tremblay")); + sg_actor_start(killer, killer_fun, 0, NULL); + + simgrid_run(); + + return 0; +} diff --git a/examples/c/actor-kill/actor-kill.tesh b/examples/c/actor-kill/actor-kill.tesh new file mode 100644 index 0000000000..78e8f5e011 --- /dev/null +++ b/examples/c/actor-kill/actor-kill.tesh @@ -0,0 +1,16 @@ +#!/usr/bin/env tesh + +$ ${bindir:=.}/actor-kill-c ${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 +> [ 12.000000] (victim A@Fafard) I have been killed! +> [ 13.000000] (killer@Tremblay) Kill victimB, even if it's already dead +> [ 14.000000] (killer@Tremblay) Start a new actor, and kill it right away +> [ 14.000000] (victim C@Jupiter) I have been killed! +> [ 15.000000] (killer@Tremblay) Killing everybody but myself +> [ 15.000000] (killer@Tremblay) OK, goodbye now. I commit a suicide. diff --git a/include/simgrid/actor.h b/include/simgrid/actor.h index 6f4a63cbbc..384b91e35b 100644 --- a/include/simgrid/actor.h +++ b/include/simgrid/actor.h @@ -25,6 +25,9 @@ XBT_PUBLIC sg_actor_t sg_actor_init(const char* name, sg_host_t host); * * Note that argv is copied over, so you should free your own copy once the actor is started. */ XBT_PUBLIC void sg_actor_start(sg_actor_t actor, xbt_main_func_t code, int argc, const char* const* argv); +XBT_PUBLIC void sg_actor_exit(); +XBT_PUBLIC void sg_actor_on_exit(int_f_int_pvoid_t fun, void* data); + XBT_PUBLIC aid_t sg_actor_get_PID(const_sg_actor_t actor); XBT_PUBLIC aid_t sg_actor_get_PPID(const_sg_actor_t actor); XBT_PUBLIC sg_actor_t sg_actor_by_PID(aid_t pid); diff --git a/src/msg/msg_legacy.cpp b/src/msg/msg_legacy.cpp index 01de8f8065..505ea3b7ce 100644 --- a/src/msg/msg_legacy.cpp +++ b/src/msg/msg_legacy.cpp @@ -51,6 +51,11 @@ int MSG_task_listen(const char* alias) } /* ************************** Actors *************************** */ +void MSG_process_on_exit(int_f_int_pvoid_t fun, void* data) +{ + sg_actor_on_exit(fun, data); +} + int MSG_process_get_PID(const_sg_actor_t actor) { return sg_actor_get_PID(actor); diff --git a/src/msg/msg_process.cpp b/src/msg/msg_process.cpp index 1c349b03bb..7b1055e38a 100644 --- a/src/msg/msg_process.cpp +++ b/src/msg/msg_process.cpp @@ -101,12 +101,3 @@ xbt_dynar_t MSG_processes_as_dynar() { } return res; } - -/** @brief Add a function to the list of "on_exit" functions for the current process. - * The on_exit functions are the functions executed when your process is killed. - * You should use them to free the data used by your process. - */ -void MSG_process_on_exit(int_f_int_pvoid_t fun, void* data) -{ - simgrid::s4u::this_actor::on_exit([fun, data](bool failed) { fun(failed ? 1 /*FAILURE*/ : 0 /*SUCCESS*/, data); }); -} diff --git a/src/s4u/s4u_Actor.cpp b/src/s4u/s4u_Actor.cpp index f2d5612ca1..ac4af83d99 100644 --- a/src/s4u/s4u_Actor.cpp +++ b/src/s4u/s4u_Actor.cpp @@ -472,11 +472,17 @@ void sg_actor_start(sg_actor_t actor, xbt_main_func_t code, int argc, const char actor->start(std::move(function)); } +void sg_actor_exit() +{ + simgrid::s4u::this_actor::exit(); +} + /** @ingroup m_actor_management * @brief Returns the process ID of @a actor. * * This function checks whether @a actor is a valid pointer and return its PID (or 0 in case of problem). */ + aid_t sg_actor_get_PID(const_sg_actor_t actor) { /* Do not raise an exception here: this function is called by the logs @@ -753,3 +759,11 @@ void sg_actor_data_set(sg_actor_t actor, void* userdata) { actor->set_data(userdata); } +/** @brief Add a function to the list of "on_exit" functions for the current process. + * The on_exit functions are the functions executed when your process is killed. + * You should use them to free the data used by your process. + */ +void sg_actor_on_exit(int_f_int_pvoid_t fun, void* data) +{ + simgrid::s4u::this_actor::on_exit([fun, data](bool failed) { fun(failed ? 1 /*FAILURE*/ : 0 /*SUCCESS*/, data); }); +} diff --git a/teshsuite/msg/CMakeLists.txt b/teshsuite/msg/CMakeLists.txt index 7e4e1812ae..09743d10d1 100644 --- a/teshsuite/msg/CMakeLists.txt +++ b/teshsuite/msg/CMakeLists.txt @@ -2,7 +2,7 @@ foreach(x async-wait async-waitall async-waitany cloud-capping cloud-migration cloud-two-tasks cloud-simple get_sender host_on_off host_on_off_recv - process-kill process-lifetime process-migration process-suspend process-yield + process-lifetime process-migration process-suspend process-yield energy-consumption energy-ptask energy-pstate platform-properties io-file io-file-remote task-priority @@ -96,7 +96,7 @@ if(enable_msg) host_on_off host_on_off_processes host_on_off_recv get_sender task_destroy_cancel task_listen_from task_progress - process-kill process-lifetime process-migration process-suspend process-yield + process-lifetime process-migration process-suspend process-yield energy-consumption energy-ptask io-file io-file-remote platform-properties diff --git a/teshsuite/msg/process-kill/process-kill.c b/teshsuite/msg/process-kill/process-kill.c deleted file mode 100644 index 2f25c67aac..0000000000 --- a/teshsuite/msg/process-kill/process-kill.c +++ /dev/null @@ -1,53 +0,0 @@ -/* Copyright (c) 2007-2020. 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/msg.h" - -XBT_LOG_NEW_DEFAULT_CATEGORY(msg_process_kill, "Messages specific for this msg example"); - -static int victim_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[]) -{ - XBT_INFO("Hello!"); - XBT_INFO("Suspending myself"); - MSG_process_suspend(MSG_process_self()); /* - First suspend itself */ - XBT_INFO("OK, OK. Let's work"); /* - Then is resumed and start to execute a task */ - MSG_task_execute(MSG_task_create("work", 1e9, 0, NULL)); - XBT_INFO("Bye!"); /* - But will never reach the end of it */ - return 0; -} - -static int killer(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[]) -{ - XBT_INFO("Hello!"); /* - First start a victim process */ - msg_process_t victim = MSG_process_create("victim", victim_fun, NULL, MSG_host_by_name("Fafard")); - MSG_process_sleep(10.0); - - XBT_INFO("Resume victim"); /* - Resume it from its suspended state */ - MSG_process_resume(victim); - - XBT_INFO("Kill victim"); /* - and then kill it */ - MSG_process_kill(victim); - - XBT_INFO("OK, goodbye now. I commit a suicide."); - MSG_process_kill(MSG_process_self()); - - XBT_INFO("This line will never get displayed: I'm already dead since the previous line."); - return 0; -} - -int main(int argc, char* argv[]) -{ - MSG_init(&argc, argv); - xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]); - - MSG_create_environment(argv[1]); /* - Load the platform description */ - /* - Create and deploy killer process, that will create the victim process */ - MSG_process_create("killer", killer, NULL, MSG_host_by_name("Tremblay")); - - msg_error_t res = MSG_main(); /* - Run the simulation */ - - XBT_INFO("Simulation time %g", MSG_get_clock()); - return res != MSG_OK; -} diff --git a/teshsuite/msg/process-kill/process-kill.tesh b/teshsuite/msg/process-kill/process-kill.tesh deleted file mode 100644 index fa1dc2d73a..0000000000 --- a/teshsuite/msg/process-kill/process-kill.tesh +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env tesh - -p Testing a MSG_process_kill function - -$ ${bindir:=.}/process-kill ${platfdir}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n" -> [ 0.000000] (1:killer@Tremblay) Hello! -> [ 0.000000] (2:victim@Fafard) Hello! -> [ 0.000000] (2:victim@Fafard) Suspending myself -> [ 10.000000] (1:killer@Tremblay) Resume victim -> [ 10.000000] (1:killer@Tremblay) Kill victim -> [ 10.000000] (1:killer@Tremblay) OK, goodbye now. I commit a suicide. -> [ 10.000000] (0:maestro@) Simulation time 10 -- 2.20.1