Logo AND Algorithmique Numérique Distribuée

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