Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / teshsuite / s4u / monkey-masterworkers / monkey-masterworkers.cpp
1 /* Copyright (c) 2007-2023. 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 /* This is a version of the masterworkers that (hopefully) survives to the chaos monkey.
7  * It tests synchronous send/receive as well as synchronous computations.
8  *
9  * It is not written to be pleasant to read, but instead to resist the aggressions of the monkey:
10  * - Workers keep going until after a global variable `todo` reaches 0.
11  * - The master is a daemon that just sends infinitely tasks
12  *   (simgrid simulations stop as soon as all non-daemon actors are done).
13  * - The platform is created programmatically to remove path issues and control the problem size.
14  *
15  * Command-line configuration items:
16  * - host-count: how many actors to start (including the master
17  * - task-count: initial value of the `todo` global
18  * - deadline: time at which the simulation is known to be failed (to detect infinite loops).
19  *
20  * See the simgrid-monkey script for more information.
21  */
22
23 #include <simgrid/s4u.hpp>
24 #include <xbt/config.hpp>
25 #include <xbt/string.hpp>
26
27 namespace sg4 = simgrid::s4u;
28
29 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
30
31 static simgrid::config::Flag<int> cfg_host_count{"host-count", "Host count (master on one, workers on the others)", 3};
32 static simgrid::config::Flag<double> cfg_deadline{"deadline", "When to fail the simulation (infinite loop detection)",
33                                                   120};
34 static simgrid::config::Flag<int> cfg_task_count{"task-count", "Amount of tasks that must be executed to succeed", 1};
35
36 XBT_ATTRIB_NORETURN static void master(sg4::Mailbox* mailbox)
37 {
38   double comp_size = 1e6;
39   long comm_size   = 1e6;
40   bool rebooting   = sg4::Actor::self()->get_restart_count() > 0;
41
42   XBT_INFO("Master %s", rebooting ? "rebooting" : "booting");
43   if (not rebooting) // Starting for the first time
44     sg4::this_actor::on_exit(
45         [](bool forcefully) { XBT_INFO("Master dying %s.", forcefully ? "forcefully" : "peacefully"); });
46
47   while (true) { // This is a daemon
48     xbt_assert(sg4::Engine::get_clock() < cfg_deadline,
49                "Failed to run all tasks in less than %d seconds. Is this an infinite loop?", (int)cfg_deadline);
50
51     auto payload = std::make_unique<double>(comp_size);
52     try {
53       XBT_INFO("Try to send a message");
54       mailbox->put(payload.get(), comm_size, 10.0);
55       payload.release();
56     } catch (const simgrid::TimeoutException&) {
57       XBT_INFO("Timeouted while sending a task");
58     } catch (const simgrid::NetworkFailureException&) {
59       XBT_INFO("Got a NetworkFailureException. Wait a second before starting again.");
60       sg4::this_actor::sleep_for(1);
61     }
62   }
63   THROW_IMPOSSIBLE;
64 }
65
66 static void worker(int id, sg4::Mailbox* mailbox, int& todo)
67 {
68   bool rebooting = sg4::Actor::self()->get_restart_count() > 0;
69
70   XBT_INFO("Worker %s", rebooting ? "rebooting" : "booting");
71   if (not rebooting) // Starting for the first time
72     sg4::this_actor::on_exit(
73         [id](bool forcefully) { XBT_INFO("worker %d dying %s.", id, forcefully ? "forcefully" : "peacefully"); });
74
75   while (todo > 0) {
76     xbt_assert(sg4::Engine::get_clock() < cfg_deadline,
77                "Failed to run all tasks in less than %d seconds. Is this an infinite loop?", (int)cfg_deadline);
78     try {
79       XBT_INFO("Waiting a message on %s", mailbox->get_cname());
80       auto payload = mailbox->get_unique<double>(10);
81       xbt_assert(payload != nullptr, "mailbox->get() failed");
82       double comp_size = *payload;
83
84       XBT_INFO("Start execution...");
85       sg4::this_actor::execute(comp_size);
86       XBT_INFO("Execution complete.");
87       todo--;
88     } catch (const simgrid::TimeoutException&) {
89       XBT_INFO("Timeouted while getting a task.");
90     } catch (const simgrid::NetworkFailureException&) {
91       XBT_INFO("Got a NetworkFailureException. Wait a second before starting again.");
92       sg4::this_actor::sleep_for(1);
93     }
94   }
95 }
96
97 int main(int argc, char* argv[])
98 {
99   sg4::Engine e(&argc, argv);
100
101   auto* rootzone = sg4::create_full_zone("root");
102   std::vector<sg4::Host*> worker_hosts;
103
104   int todo = cfg_task_count; // remaining amount of tasks to execute, a shared variable
105   xbt_assert(todo > 0, "Please give more than %d tasks to run", todo);
106
107   xbt_assert(cfg_host_count > 2, "You need at least 2 workers (i.e., 3 hosts) or the master will be auto-killed when "
108                                  "the only worker gets killed.");
109
110   // get it once for all, to reduce the amount of simcalls during actor reboot
111   sg4::Mailbox* mailbox = sg4::Mailbox::by_name("mailbox");
112
113   sg4::Host* master_host = rootzone->create_host("lilibeth 0", 1e9); // Host where the master will stay
114   for (int i = 1; i < cfg_host_count; i++) {
115     auto hostname = "lilibeth " + std::to_string(i);
116     auto* host    = rootzone->create_host(hostname, 1e9);
117     sg4::LinkInRoute link(rootzone->create_link(hostname, "1MBps")->set_latency("24us")->seal());
118     rootzone->add_route(master_host->get_netpoint(), host->get_netpoint(), nullptr, nullptr, {link}, true);
119     worker_hosts.push_back(host);
120   }
121   rootzone->seal();
122
123   sg4::Actor::create("master", master_host, master, mailbox)->daemonize()->set_auto_restart(true);
124   int id = 0;
125   for (auto* h : worker_hosts) {
126     sg4::Actor::create("worker", h, worker, id, mailbox, std::ref(todo))->set_auto_restart(true);
127     id++;
128   }
129
130   e.run();
131
132   XBT_INFO("WE SURVIVED!");
133   return 0;
134 }