Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Declare function "noreturn".
[simgrid.git] / teshsuite / s4u / host-on-off-actors / host-on-off-actors.cpp
1 /* Copyright (c) 2010-2020. 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 #include "simgrid/Exception.hpp"
7 #include "simgrid/s4u.hpp"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
10
11 int tasks_done = 0;
12
13 XBT_ATTRIB_NORETURN static void actor_daemon()
14 {
15   const simgrid::s4u::Host* host = simgrid::s4u::Host::current();
16   XBT_INFO("  Start daemon on %s (%f)", host->get_cname(), host->get_speed());
17   for (;;) {
18     XBT_INFO("  Execute daemon");
19     simgrid::s4u::this_actor::execute(host->get_speed());
20     tasks_done++;
21   }
22   xbt_die("  daemon done. See you!");
23 }
24
25 static void commTX()
26 {
27   XBT_INFO("  Start TX");
28   std::string* payload = new std::string("COMM");
29   simgrid::s4u::Mailbox::by_name("comm")->put_init(payload, 100000000)->detach();
30   // We should wait a bit (if not the process will end before the communication, hence an exception on the other side).
31   try {
32     simgrid::s4u::this_actor::sleep_for(30);
33   } catch (const simgrid::HostFailureException&) {
34     XBT_INFO("The host has died ... as expected.");
35   }
36   delete payload;
37
38   XBT_INFO("  TX done");
39 }
40
41 static void commRX()
42 {
43   const std::string* payload = nullptr;
44   XBT_INFO("  Start RX");
45   try {
46     payload = static_cast<std::string*>(simgrid::s4u::Mailbox::by_name("comm")->get());
47     XBT_INFO("  Receive message: %s", payload->c_str());
48   } catch (const simgrid::HostFailureException&) {
49     XBT_INFO("  Receive message: HOST_FAILURE");
50   } catch (const simgrid::NetworkFailureException&) {
51     XBT_INFO("  Receive message: TRANSFER_FAILURE");
52   }
53
54   delete payload;
55   XBT_INFO("  RX Done");
56 }
57
58 static void test_launcher(int test_number)
59 {
60   simgrid::s4u::Host* jupiter = simgrid::s4u::Host::by_name("Jupiter");
61   simgrid::s4u::ActorPtr daemon;
62   simgrid::s4u::VirtualMachine* vm0 = nullptr;
63
64   switch (test_number) {
65     case 1:
66       // Create a process running a simple task on a host and turn the host off during the execution of the actor.
67       XBT_INFO("Test 1:");
68       XBT_INFO("  Create an actor on Jupiter");
69       simgrid::s4u::Actor::create("actor_daemon", jupiter, actor_daemon);
70       simgrid::s4u::this_actor::sleep_for(3);
71       XBT_INFO("  Turn off Jupiter");
72       jupiter->turn_off();
73       simgrid::s4u::this_actor::sleep_for(10);
74       XBT_INFO("Test 1 seems ok, cool !(#Actors: %zu, it should be 1; #tasks: %d)",
75                simgrid::s4u::Engine::get_instance()->get_actor_count(), tasks_done);
76       break;
77     case 2:
78       // Create an actorthat on a host that is turned off (this is not allowed)
79       XBT_INFO("Test 2:");
80       XBT_INFO("  Turn off Jupiter");
81       // adsein: Jupiter is already off, hence nothing should happen
82       // adsein: This can be one additional test, to check that you cannot shutdown twice a host
83       jupiter->turn_off();
84       try {
85         simgrid::s4u::Actor::create("actor_daemon", jupiter, actor_daemon);
86         simgrid::s4u::this_actor::sleep_for(10);
87         XBT_INFO("  Test 2 does crash as it should. This message will not be displayed.");
88       } catch (const simgrid::HostFailureException&) {
89         xbt_die("Could not launch a new actor on failed host %s.", jupiter->get_cname());
90       }
91       break;
92     case 3:
93       // Create an actor running successive sleeps on a host and turn the host off during the execution of the actor.
94       xbt_die("Test 3 is superseded by activity-lifecycle");
95       break;
96     case 4:
97       XBT_INFO("Test 4 (turn off src during a communication) : Create an actor/task to make a communication between "
98                "Jupiter and Tremblay and turn off Jupiter during the communication");
99       jupiter->turn_on();
100       simgrid::s4u::this_actor::sleep_for(10);
101       simgrid::s4u::Actor::create("commRX", simgrid::s4u::Host::by_name("Tremblay"), commRX);
102       simgrid::s4u::Actor::create("commTX", jupiter, commTX);
103       XBT_INFO("  number of actors: %zu", simgrid::s4u::Engine::get_instance()->get_actor_count());
104       simgrid::s4u::this_actor::sleep_for(10);
105       XBT_INFO("  Turn Jupiter off");
106       jupiter->turn_off();
107       XBT_INFO("Test 4 is ok.  (number of actors : %zu, it should be 1 or 2 if RX has not been satisfied)."
108                " An exception is raised when we turn off a node that has an actor sleeping",
109                simgrid::s4u::Engine::get_instance()->get_actor_count());
110       break;
111     case 5:
112       XBT_INFO("Test 5 (turn off dest during a communication : Create an actor/task to make a communication between "
113                "Tremblay and Jupiter and turn off Jupiter during the communication");
114       jupiter->turn_on();
115       simgrid::s4u::this_actor::sleep_for(10);
116       simgrid::s4u::Actor::create("commRX", jupiter, commRX);
117       simgrid::s4u::Actor::create("commTX", simgrid::s4u::Host::by_name("Tremblay"), commTX);
118       XBT_INFO("  number of actors: %zu", simgrid::s4u::Engine::get_instance()->get_actor_count());
119       simgrid::s4u::this_actor::sleep_for(10);
120       XBT_INFO("  Turn Jupiter off");
121       jupiter->turn_off();
122       XBT_INFO("Test 5 seems ok (number of actors: %zu, it should be 2)",
123                simgrid::s4u::Engine::get_instance()->get_actor_count());
124       break;
125     case 6:
126       XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch an actor inside the VM, and turn off the node");
127
128       // Create VM0
129       vm0 = new simgrid::s4u::VirtualMachine("vm0", jupiter, 1);
130       vm0->start();
131
132       daemon = simgrid::s4u::Actor::create("actor_daemon", vm0, actor_daemon);
133       simgrid::s4u::Actor::create("actor_daemonJUPI", jupiter, actor_daemon);
134
135       daemon->suspend();
136       vm0->set_bound(90);
137       daemon->resume();
138
139       simgrid::s4u::this_actor::sleep_for(10);
140
141       XBT_INFO("  Turn Jupiter off");
142       jupiter->turn_off();
143       XBT_INFO("  Shutdown vm0");
144       vm0->shutdown();
145       XBT_INFO("  Destroy vm0");
146       vm0->destroy();
147       XBT_INFO("Test 6 is also weird: when the node Jupiter is turned off once again, the VM and its daemon are not "
148                "killed. However, the issue regarding the shutdown of hosted VMs can be seen a feature not a bug ;)");
149       break;
150     default:
151       xbt_die("Unknown test case.");
152   }
153
154   XBT_INFO("  Test done. See you!");
155 }
156
157 int main(int argc, char* argv[])
158 {
159   simgrid::s4u::Engine e(&argc, argv);
160   xbt_assert(argc == 3, "Usage: %s platform_file test_number\n\tExample: %s msg_platform.xml 1\n", argv[0], argv[0]);
161
162   e.load_platform(argv[1]);
163
164   simgrid::s4u::Actor::create("test_launcher", simgrid::s4u::Host::by_name("Tremblay"), test_launcher,
165                               std::stoi(argv[2]));
166
167   e.run();
168
169   XBT_INFO("Simulation time %g", e.get_clock());
170
171   return 0;
172 }