Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a FAILED state to activities. tested on comm and exec
[simgrid.git] / examples / cpp / exec-failure / s4u-exec-failure.cpp
1 /* Copyright (c) 2021. 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   long index;
37   try {
38     index = sg4::Exec::wait_any(pending_execs);
39     XBT_INFO("Wait any returned index %ld (exec on %s)", index, pending_execs.at(index)->get_host()->get_cname());
40   } catch (simgrid::HostFailureException& e) {
41     XBT_INFO("Dispatcher has experienced a host failure exception, so it knows that something went wrong");
42     XBT_INFO("Now it needs to figure out which of the two execs failed by looking at their state");
43   }
44
45   XBT_INFO("Exec on %s has state: %s", pending_execs[0]->get_host()->get_cname(), pending_execs[0]->get_state_str());
46   XBT_INFO("Exec on %s has state: %s", pending_execs[1]->get_host()->get_cname(), pending_execs[1]->get_state_str());
47
48   try {
49     pending_execs[1]->wait();
50   } catch (simgrid::HostFailureException& e) {
51     XBT_INFO("Waiting on a FAILED exec raises an exception: '%s'", e.what());
52   }
53   pending_execs.pop_back();
54   XBT_INFO("Wait for remaining exec, just to be nice");
55   index = simgrid::s4u::Exec::wait_any(pending_execs);
56   XBT_INFO("Dispatcher ends");
57 }
58
59 static void host_killer(sg4::Host* to_kill)
60 {
61   XBT_INFO("HostKiller  sleeping 10 seconds...");
62   sg4::this_actor::sleep_for(10.0);
63   XBT_INFO("HostKiller turning off host %s", to_kill->get_cname());
64   to_kill->turn_off();
65   XBT_INFO("HostKiller ends");
66 }
67
68 int main(int argc, char** argv)
69 {
70
71   sg4::Engine engine(&argc, argv);
72
73   auto* zone  = sg4::create_full_zone("AS0");
74   auto* host1 = zone->create_host("Host1", "1f");
75   auto* host2 = zone->create_host("Host2", "1f");
76   zone->seal();
77
78   sg4::Actor::create("Dispatcher", host1, dispatcher, host1, host2);
79   sg4::Actor::create("HostKiller", host1, host_killer, host2)->daemonize();
80
81   engine.run();
82
83   return 0;
84 }