Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c0843c6ad10d56933e70efdaac3eb180d9ebdd4a
[simgrid.git] / teshsuite / s4u / activity-lifecycle / activity-lifecycle.cpp
1 /* Copyright (c) 2010-2019. 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/s4u.hpp"
7
8 #include <cmath>
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
11
12 std::vector<simgrid::s4u::Host*> all_hosts;
13
14 /* Helper function easing the testing of actor's ending condition */
15 static void assert_exit(int status, double duration)
16 {
17   double expected_time = simgrid::s4u::Engine::get_clock() + duration;
18   simgrid::s4u::this_actor::on_exit(
19       [status, expected_time](int got_status, void* /*ignored*/) {
20         xbt_assert(status == got_status, "Exit status mismatch. Expected %d, got %d", status, got_status);
21         xbt_assert(std::fabs(expected_time - simgrid::s4u::Engine::get_clock()) < 0.001,
22                    "Exit time mismatch. Expected %f", expected_time);
23         XBT_VERB("Checks on exit successful");
24       },
25       nullptr);
26 }
27 /* Helper function in charge of running a test and doing some sanity checks afterward */
28 static void run_test(const char* test_name, std::function<void()> test)
29 {
30   simgrid::s4u::Actor::create(test_name, all_hosts[0], test);
31   simgrid::s4u::this_actor::sleep_for(10);
32
33   /* Check that no actor remain (but on host[0], where main_dispatcher lives */
34   for (unsigned int i = 0; i < all_hosts.size(); i++) {
35     std::vector<simgrid::s4u::ActorPtr> all_actors = all_hosts[i]->get_all_actors();
36     unsigned int expected_count = (i == 0) ? 1 : 0; // host[0] contains main_dispatcher, all other are empty
37     if (all_actors.size() != expected_count) {
38       XBT_CRITICAL("Host %s contains %zu actors but %u are expected (i=%u). Existing actors: ",
39                    all_hosts[i]->get_cname(), all_actors.size(), expected_count, i);
40       for (auto act : all_actors)
41         XBT_CRITICAL(" - %s", act->get_cname());
42       xbt_die("This is wrong");
43     }
44   }
45   xbt_assert("TODO: Check that all LMM are empty");
46   XBT_INFO("SUCCESS: %s", test_name);
47   XBT_INFO("#########################################################################################################");
48 }
49
50 /**
51  ** Each tests
52  **/
53
54 static void test_sleep()
55 {
56   XBT_INFO("%s: Launch a sleep(5), and let it proceed", __func__);
57   bool global = false;
58
59   simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5", all_hosts[1], [&global]() {
60     assert_exit(0, 5.);
61     simgrid::s4u::this_actor::sleep_for(5);
62     global = true;
63   });
64   simgrid::s4u::this_actor::sleep_for(10);
65   xbt_assert(global, "The forked actor did not modify the global after sleeping. Was it killed before?");
66 }
67
68 static void test_sleep_kill_middle()
69 {
70   XBT_INFO("%s: Launch a sleep(5), and kill it after 2 secs", __func__);
71
72   simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_killed", all_hosts[1], []() {
73     assert_exit(1, 2);
74     simgrid::s4u::this_actor::sleep_for(5);
75     xbt_die("I should be dead now");
76   });
77
78   simgrid::s4u::this_actor::sleep_for(2);
79   sleeper5->kill();
80 }
81
82 static void test_sleep_kill_begin()
83 {
84   XBT_INFO("%s: Launch a sleep(5), and kill it right after start", __func__);
85
86   simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_killed", all_hosts[1], []() {
87     assert_exit(1, 0);
88     simgrid::s4u::this_actor::sleep_for(5);
89     xbt_die("I should be dead now");
90   });
91
92   simgrid::s4u::this_actor::yield();
93   sleeper5->kill();
94 }
95
96 static void test_sleep_restart_begin()
97 {
98   XBT_INFO("%s: Launch a sleep(5), and restart its host right after start", __func__);
99
100   simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
101     assert_exit(1, 0);
102     simgrid::s4u::this_actor::sleep_for(5);
103     xbt_die("I should be dead now");
104   });
105
106   simgrid::s4u::this_actor::yield();
107   sleeper5->get_host()->turn_off();
108   sleeper5->get_host()->turn_on();
109   XBT_INFO("Test %s is ending", __func__);
110 }
111
112 static void test_sleep_restart_middle()
113 {
114   XBT_INFO("%s: Launch a sleep(5), and restart its host after 2 secs", __func__);
115
116   simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
117     assert_exit(1, 2);
118     simgrid::s4u::this_actor::sleep_for(5);
119     xbt_die("I should be dead now");
120   });
121
122   simgrid::s4u::this_actor::sleep_for(2);
123   sleeper5->get_host()->turn_off();
124   sleeper5->get_host()->turn_on();
125   XBT_INFO("Test %s is ending", __func__);
126 }
127 static void test_sleep_restart_end()
128 {
129   XBT_INFO("%s: Launch a sleep(5), and restart its host right when it stops", __func__);
130   bool sleeper_done = false;
131
132   simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], [&sleeper_done]() {
133     assert_exit(0, 5);
134     simgrid::s4u::this_actor::sleep_for(5);
135     sleeper_done = true;
136   });
137   simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
138     simgrid::s4u::this_actor::sleep_for(5);
139     XBT_INFO("Killer!");
140     all_hosts[1]->turn_off();
141     all_hosts[1]->turn_on();
142   });
143   simgrid::s4u::this_actor::sleep_for(10);
144   xbt_assert(sleeper_done,
145              "Restarted actor was already dead in the scheduling round during which the host_off simcall was issued");
146 }
147
148 static void test_comm()
149 {
150   XBT_INFO("%s: Launch a communication", __func__);
151   bool send_done = false;
152   bool recv_done = false;
153
154   simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
155     assert_exit(0, 5);
156     char* payload = xbt_strdup("toto");
157     simgrid::s4u::Mailbox::by_name("mb")->put(payload, 5000);
158     send_done = true;
159   });
160   simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
161     assert_exit(0, 5);
162     void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
163     xbt_free(payload);
164     recv_done = true;
165   });
166
167   simgrid::s4u::this_actor::sleep_for(10);
168   xbt_assert(send_done, "Sender killed somehow. It shouldn't");
169   xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
170 }
171
172 static void test_comm_killsend()
173 {
174   XBT_INFO("%s: Launch a communication and kill the sender", __func__);
175   bool send_done = false;
176   bool recv_done = false;
177
178   simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
179     assert_exit(1, 2);
180     char* payload = xbt_strdup("toto");
181     simgrid::s4u::Mailbox::by_name("mb")->put(payload, 5000);
182     send_done = true;
183   });
184   simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
185     assert_exit(0, 2);
186     bool got_exception = false;
187     try {
188       void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
189       xbt_free(payload);
190     } catch (simgrid::NetworkFailureException const&) {
191       got_exception = true;
192     }
193     xbt_assert(got_exception);
194     recv_done = true;
195   });
196
197   simgrid::s4u::this_actor::sleep_for(2);
198   sender->kill();
199
200   xbt_assert(not send_done, "Sender was not killed properly");
201   // xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
202 }
203
204 /* We need an extra actor here, so that it can sleep until the end of each test */
205 static void main_dispatcher()
206 {
207   run_test("sleep", static_cast<std::function<void()>>(test_sleep));
208   run_test("sleep killed at start", test_sleep_kill_begin);
209   run_test("sleep killed in middle", test_sleep_kill_middle);
210   /* We cannot kill right at the end of the action because killer actors are always rescheduled to the end of the round
211    * to avoid that they exit before their victim dereferences their name */
212   run_test("sleep restarted at start", test_sleep_restart_begin);
213   run_test("sleep restarted at middle", test_sleep_restart_middle);
214   run_test("sleep restarted at end", test_sleep_restart_end);
215
216   run_test("comm", test_comm);
217   // run_test("comm kill sender", test_comm_killsend);
218 }
219
220 int main(int argc, char* argv[])
221 {
222   simgrid::s4u::Engine e(&argc, argv);
223
224   const char* platf = argv[1];
225   if (argc <= 1) {
226     XBT_WARN("No platform file provided. Using './testing_platform.xml'");
227     platf = "./testing_platform.xml";
228   }
229   e.load_platform(platf);
230
231   all_hosts = e.get_all_hosts();
232   simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0], main_dispatcher);
233
234   e.run();
235
236   XBT_INFO("Simulation done");
237
238   return 0;
239 }