1 /* Copyright (c) 2007-2022. 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 /* 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.
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.
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).
20 * See the simgrid-monkey script for more information.
23 #include <simgrid/s4u.hpp>
24 #include <xbt/config.hpp>
25 #include <xbt/string.hpp>
27 namespace sg4 = simgrid::s4u;
29 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
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)",
34 static simgrid::config::Flag<int> cfg_task_count{"task-count", "Amount of tasks that must be executed to succeed", 2};
36 int todo; // remaining amount of tasks to execute, a global variable
37 sg4::Mailbox* mailbox; // as a global to reduce the amount of simcalls during actor reboot
41 double comp_size = 1e6;
43 XBT_INFO("Master booting");
44 sg4::Actor::self()->daemonize();
45 sg4::this_actor::on_exit(
46 [](bool forcefully) { XBT_INFO("Master dying %s.", forcefully ? "forcefully" : "peacefully"); });
48 while (true) { // This is a daemon
49 xbt_assert(sg4::Engine::get_clock() < cfg_deadline,
50 "Failed to run all tasks in less than %d seconds. Is this an infinite loop?", (int)cfg_deadline);
52 auto* payload = new double(comp_size);
54 XBT_INFO("Try to send a message");
55 mailbox->put(payload, comm_size, 10.0);
56 } catch (const simgrid::TimeoutException&) {
58 XBT_INFO("Timeouted while sending a task");
59 } catch (const simgrid::NetworkFailureException&) {
61 XBT_INFO("Got a NetworkFailureException. Wait a second before starting again.");
62 sg4::this_actor::sleep_for(1);
68 static void worker(int id)
70 XBT_INFO("Worker booting");
71 sg4::this_actor::on_exit(
72 [id](bool forcefully) { XBT_INFO("worker %d dying %s.", id, forcefully ? "forcefully" : "peacefully"); });
75 xbt_assert(sg4::Engine::get_clock() < cfg_deadline,
76 "Failed to run all tasks in less than %d seconds. Is this an infinite loop?", (int)cfg_deadline);
78 XBT_INFO("Waiting a message on %s", mailbox->get_cname());
79 auto payload = mailbox->get_unique<double>(10);
80 xbt_assert(payload != nullptr, "mailbox->get() failed");
81 double comp_size = *payload;
82 if (comp_size < 0) { /* - Exit when -1.0 is received */
83 XBT_INFO("I'm done. See you!");
86 /* - Otherwise, process the task */
87 XBT_INFO("Start execution...");
88 sg4::this_actor::execute(comp_size);
89 XBT_INFO("Execution complete.");
91 } catch (const simgrid::TimeoutException&) {
92 XBT_INFO("Timeouted while getting a task.");
94 } catch (const simgrid::NetworkFailureException&) {
95 XBT_INFO("Got a NetworkFailureException. Wait a second before starting again.");
96 sg4::this_actor::sleep_for(1);
101 int main(int argc, char* argv[])
103 sg4::Engine e(&argc, argv);
105 XBT_INFO("host count: %d ", (int)cfg_host_count);
107 auto* rootzone = sg4::create_full_zone("root");
108 sg4::Host* main; // First host created, where the master will stay
109 std::vector<sg4::Host*> worker_hosts;
110 for (int i = 0; i < cfg_host_count; i++) {
111 auto hostname = std::string("lilibeth ") + std::to_string(i);
112 auto* host = rootzone->create_host(hostname, 1e9);
116 sg4::LinkInRoute link(rootzone->create_link(hostname, "1MBps")->set_latency("24us")->seal());
117 rootzone->add_route(main->get_netpoint(), host->get_netpoint(), nullptr, nullptr, {link}, true);
118 worker_hosts.push_back(host);
122 sg4::Engine::get_instance()->on_platform_created(); // FIXME this should not be necessary
124 sg4::Actor::create("master", main, master)->set_auto_restart(true);
126 for (auto* h : worker_hosts)
127 sg4::Actor::create("worker", h, worker, id++)->set_auto_restart(true);
129 todo = cfg_task_count;
130 xbt_assert(todo > 0, "Please give more than %d tasks to run", todo);
131 mailbox = sg4::Mailbox::by_name("mailbox");
132 xbt_assert(cfg_host_count > 2, "You need at least 2 workers (i.e., 3 hosts) or the master will be auto-killed when "
133 "the only worker gets killed.");
137 XBT_INFO("WE SURVIVED!");