Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / cpp / exec-failure / s4u-exec-failure.cpp
1 /* Copyright (c) 2021-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 /* This example shows how to serialize a set of communications going through a link
7  *
8  * As for the other asynchronous examples, the sender initiates all the messages it wants to send and
9  * pack the resulting simgrid::s4u::CommPtr objects in a vector.
10  * At the same time, the receiver starts receiving all messages asynchronously. Without serialization,
11  * all messages would be received at the same timestamp in the receiver.
12  *
13  * However, as they will be serialized in a link of the platform, the messages arrive 2 by 2.
14  *
15  * The sender then blocks until all ongoing communication terminate, using simgrid::s4u::Comm::wait_all()
16  */
17
18 #include <simgrid/s4u.hpp>
19
20 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_exec_failure, "Messages specific for this s4u example");
21 namespace sg4 = simgrid::s4u;
22
23 static void dispatcher(sg4::Host* host1, sg4::Host* host2)
24 {
25   std::vector<sg4::ExecPtr> pending_execs;
26   XBT_INFO("Initiating asynchronous exec on %s", host1->get_cname());
27   auto exec1 = sg4::this_actor::exec_init(20)->set_host(host1);
28   pending_execs.push_back(exec1);
29   exec1->start();
30   XBT_INFO("Initiating asynchronous exec on %s", host2->get_cname());
31   auto exec2 = sg4::this_actor::exec_init(20)->set_host(host2);
32   pending_execs.push_back(exec2);
33   exec2->start();
34
35   XBT_INFO("Calling wait_any..");
36   try {
37     long index = sg4::Exec::wait_any(pending_execs);
38     XBT_INFO("Wait any returned index %ld (exec on %s)", index, pending_execs.at(index)->get_host()->get_cname());
39   } catch (const simgrid::HostFailureException&) {
40     XBT_INFO("Dispatcher has experienced a host failure exception, so it knows that something went wrong");
41     XBT_INFO("Now it needs to figure out which of the two execs failed by looking at their state");
42   }
43
44   XBT_INFO("Exec on %s has state: %s", pending_execs[0]->get_host()->get_cname(), pending_execs[0]->get_state_str());
45   XBT_INFO("Exec on %s has state: %s", pending_execs[1]->get_host()->get_cname(), pending_execs[1]->get_state_str());
46
47   try {
48     pending_execs[1]->wait();
49   } catch (const simgrid::HostFailureException& e) {
50     XBT_INFO("Waiting on a FAILED exec raises an exception: '%s'", e.what());
51   }
52   pending_execs.pop_back();
53   XBT_INFO("Wait for remaining exec, just to be nice");
54   simgrid::s4u::Exec::wait_any(pending_execs);
55   XBT_INFO("Dispatcher ends");
56 }
57
58 static void host_killer(sg4::Host* to_kill)
59 {
60   XBT_INFO("HostKiller  sleeping 10 seconds...");
61   sg4::this_actor::sleep_for(10.0);
62   XBT_INFO("HostKiller turning off host %s", to_kill->get_cname());
63   to_kill->turn_off();
64   XBT_INFO("HostKiller ends");
65 }
66
67 int main(int argc, char** argv)
68 {
69   sg4::Engine engine(&argc, argv);
70
71   auto* zone  = sg4::create_full_zone("AS0");
72   auto* host1 = zone->create_host("Host1", "1f");
73   auto* host2 = zone->create_host("Host2", "1f");
74   zone->seal();
75
76   sg4::Actor::create("Dispatcher", host1, dispatcher, host1, host2);
77   sg4::Actor::create("HostKiller", host1, host_killer, host2)->daemonize();
78
79   engine.run();
80
81   return 0;
82 }