1 /* Copyright (c) 2022-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 // Chaos Monkey plugin: See the simgrid-monkey script for more information
8 #include <simgrid/kernel/Timer.hpp>
9 #include <simgrid/s4u/Engine.hpp>
10 #include <simgrid/s4u/Host.hpp>
11 #include <xbt/config.hpp>
13 #include "src/simgrid/module.hpp" // SIMGRID_REGISTER_PLUGIN
15 namespace sg4 = simgrid::s4u;
16 static simgrid::config::Flag<bool> cfg_tell{"cmonkey/tell", "Request the Chaos Monkey to display all timestamps",
18 static simgrid::config::Flag<double> cfg_time{"cmonkey/time", "When should the chaos monkey kill a resource", -1.};
19 static simgrid::config::Flag<int> cfg_link{"cmonkey/link", "Which link should be killed (number)", -1};
20 static simgrid::config::Flag<int> cfg_host{"cmonkey/host", "Which host should be killed (number)", -1};
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(cmonkey, kernel, "Chaos Monkey plugin");
24 static void sg_chaos_monkey_plugin_run()
26 const auto* engine = sg4::Engine::get_instance();
27 auto hosts = engine->get_all_hosts();
28 auto links = engine->get_all_links();
30 sg4::Engine::on_deadlock_cb([]() { exit(2); });
32 if (not cfg_tell && cfg_time < 0 && cfg_host == -1 && cfg_link == -1) {
34 "You invoked the Chaos Monkey without anything to do.\n"
35 "Bored, it kills your simulation after this message about how to use it manually (note that the\n"
36 "simgrid-monkey script can tame the monkey for you if you prefer).\n\n"
37 "First of all, you need information. Use --cfg=cmonkey/tell:1 to get all information about the existing\n"
38 "resources and the timestamps of interest in your simulation.\n\n"
39 "Then, use --cfg=cmonkey/host:0 --cfg=cmonkey/time:0.1 to turn off and on the host #0 at time 0.1s,\n"
40 "or --cfg=cmonkey/link:22 --cfg=cmonkey/time:0.4 to turn the link #22 off and on again at time 0.4s.\n"
41 "Only one resource can be rebooted in a given run.\n\n"
42 "Please read the comments at the beginning of the simgrid-monkey script about how to exhaustively test a\n"
43 "program resilience.\n");
48 XBT_INFO("HOST_COUNT=%zu", hosts.size());
49 XBT_INFO("LINK_COUNT=%zu", links.size());
50 sg4::Engine::on_time_advance_cb([engine](double /* delta*/) { XBT_INFO("TIMESTAMP=%lf", engine->get_clock()); });
56 xbt_assert(host >= 0 || link >= 0,
57 "If a kill time is given, you must also specify a resource to kill (either a link or an host)");
58 xbt_assert(host < 0 || link < 0, "Cannot specify both a link and an host to kill");
60 auto* h = hosts.at(host);
61 simgrid::kernel::timer::Timer::set(cfg_time, [h]() {
62 XBT_INFO("Kill host %s", h->get_cname());
65 simgrid::kernel::timer::Timer::set(cfg_time + 30, [h]() {
66 XBT_INFO("Restart host %s", h->get_cname());
71 auto* l = links.at(link);
72 simgrid::kernel::timer::Timer::set(cfg_time, [l]() {
73 XBT_INFO("Kill link %s", l->get_cname());
76 simgrid::kernel::timer::Timer::set(cfg_time + 30, [l]() {
77 XBT_INFO("Restart host %s", l->get_cname());
83 sg4::Engine::on_simulation_end_cb([]() { XBT_INFO("Chaos Monkey done!"); });
86 // Makes sure that this plugin can be activated from the command line with ``--cfg=plugin:cmonkey``
87 SIMGRID_REGISTER_PLUGIN(cmonkey, "Chaos monkey", []() {
88 XBT_INFO("Initializing the chaos monkey.");
90 // delay the initialization until after the parameter are parsed
91 sg4::Engine::on_simulation_start_cb(sg_chaos_monkey_plugin_run);