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 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); });
33 XBT_INFO("HOST_COUNT=%zu", hosts.size());
34 XBT_INFO("LINK_COUNT=%zu", links.size());
35 sg4::Engine::on_time_advance_cb([engine](double /* delta*/) { XBT_INFO("TIMESTAMP=%lf", engine->get_clock()); });
41 xbt_assert(host >= 0 || link >= 0,
42 "If a kill time is given, you must also specify a resource to kill (either a link or an host)");
43 xbt_assert(host < 0 || link < 0, "Cannot specify both a link and an host to kill");
45 auto* h = hosts[host];
46 simgrid::kernel::timer::Timer::set(cfg_time, [h]() {
47 XBT_INFO("Kill host %s", h->get_cname());
50 simgrid::kernel::timer::Timer::set(cfg_time + 30, [h]() {
51 XBT_INFO("Restart host %s", h->get_cname());
56 auto* l = links[link];
57 simgrid::kernel::timer::Timer::set(cfg_time, [l]() {
58 XBT_INFO("Kill link %s", l->get_cname());
61 simgrid::kernel::timer::Timer::set(cfg_time + 30, [l]() {
62 XBT_INFO("Restart host %s", l->get_cname());
68 sg4::Engine::on_simulation_end_cb([]() { XBT_INFO("Chaos Monkey done!"); });
71 // Makes sure that this plugin can be activated from the command line with ``--cfg=plugin:chaos_monkey``
72 SIMGRID_REGISTER_PLUGIN(cmonkey, "Chaos monkey", []() {
73 XBT_INFO("Initializing the chaos monkey");
75 // delay the initialization until after the parameter are parsed
76 sg4::Engine::on_simulation_start_cb(sg_chaos_monkey_plugin_run);