Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Introduce the SimGrid Chaos Monkey
[simgrid.git] / src / plugins / chaos_monkey.cpp
1 /* Copyright (c) 2022-2022. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 // Chaos Monkey plugin: See the simgrid-monkey script for more information
7
8 #include <simgrid/kernel/Timer.hpp>
9 #include <simgrid/s4u/Engine.hpp>
10 #include <simgrid/s4u/Host.hpp>
11 #include <xbt/config.hpp>
12
13 #include "src/surf/surf_interface.hpp" // SIMGRID_REGISTER_PLUGIN
14
15 namespace sg4 = simgrid::s4u;
16 static simgrid::config::Flag<bool> cfg_tell{"cmonkey/tell", "Request the Chaos Monkey to display all timestamps",
17                                             false};
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};
21 static void sg_chaos_monkey_plugin_init();
22 // Makes sure that this plugin can be activated from the command line with ``--cfg=plugin:chaos_monkey``
23 SIMGRID_REGISTER_PLUGIN(cmonkey, "Chaos monkey", &sg_chaos_monkey_plugin_init)
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(cmonkey, kernel, "Chaos Monkey plugin");
26
27 static void sg_chaos_monkey_plugin_init()
28 {
29   XBT_INFO("Initializing the chaos monkey");
30
31   // delay the initialization until after the parameter are parsed
32   sg4::Engine::on_platform_created_cb([]() {
33     auto engine = sg4::Engine::get_instance();
34     auto hosts  = engine->get_all_hosts();
35     auto links  = engine->get_all_links();
36
37     sg4::Engine::on_deadlock_cb([]() { exit(2); });
38
39     if (cfg_tell) {
40       XBT_INFO("HOST_COUNT=%zu", hosts.size());
41       XBT_INFO("LINK_COUNT=%zu", links.size());
42       sg4::Engine::on_time_advance_cb([engine](double /* delta*/) { XBT_INFO("TIMESTAMP=%lf", engine->get_clock()); });
43     }
44
45     if (cfg_time >= 0) {
46       int host = cfg_host;
47       int link = cfg_link;
48       xbt_assert(host >= 0 || link >= 0,
49                  "If a kill time is given, you must also specify a resource to kill (either a link or an host)");
50       xbt_assert(host < 0 || link < 0, "Cannot specify both a link and an host to kill");
51       if (host >= 0) {
52         auto* h = hosts[host];
53         simgrid::kernel::timer::Timer::set(cfg_time, [h]() {
54           XBT_INFO("Kill host %s", h->get_cname());
55           h->turn_off();
56         });
57         simgrid::kernel::timer::Timer::set(cfg_time + 30, [h]() {
58           XBT_INFO("Restart host %s", h->get_cname());
59           h->turn_on();
60         });
61       }
62       if (link >= 0) {
63         auto* l = links[link];
64         simgrid::kernel::timer::Timer::set(cfg_time, [l]() {
65           XBT_INFO("Kill link %s", l->get_cname());
66           l->turn_off();
67         });
68         simgrid::kernel::timer::Timer::set(cfg_time + 30, [l]() {
69           XBT_INFO("Restart host %s", l->get_cname());
70           l->turn_on();
71         });
72       }
73     }
74
75     sg4::Engine::on_simulation_end_cb([]() { XBT_INFO("Chaos Monkey done!"); });
76   });
77 }