1 /* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/s4u.hpp"
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
12 std::vector<simgrid::s4u::Host*> all_hosts;
14 /* Helper function easing the testing of actor's ending condition */
15 static void assert_exit(bool exp_failed, double duration)
17 double expected_time = simgrid::s4u::Engine::get_clock() + duration;
18 simgrid::s4u::this_actor::on_exit([exp_failed, expected_time](bool got_failed) {
19 xbt_assert(exp_failed == got_failed, "Exit failure status mismatch. Expected %d, got %d", exp_failed, got_failed);
20 xbt_assert(std::fabs(expected_time - simgrid::s4u::Engine::get_clock()) < 0.001, "Exit time mismatch. Expected %f",
22 XBT_VERB("Checks on exit successful");
25 /* Helper function in charge of running a test and doing some sanity checks afterward */
26 static void run_test(const char* test_name, const std::function<void()>& test)
28 simgrid::s4u::Actor::create(test_name, all_hosts[0], test);
29 simgrid::s4u::this_actor::sleep_for(10);
31 /* Check that no actor remain (but on host[0], where main_dispatcher lives */
32 for (unsigned int i = 0; i < all_hosts.size(); i++) {
33 std::vector<simgrid::s4u::ActorPtr> all_actors = all_hosts[i]->get_all_actors();
34 unsigned int expected_count = (i == 0) ? 1 : 0; // host[0] contains main_dispatcher, all other are empty
35 if (all_actors.size() != expected_count) {
36 XBT_CRITICAL("Host %s contains %zu actors but %u are expected (i=%u). Existing actors: ",
37 all_hosts[i]->get_cname(), all_actors.size(), expected_count, i);
38 for (auto act : all_actors)
39 XBT_CRITICAL(" - %s", act->get_cname());
40 xbt_die("This is wrong");
43 xbt_assert("TODO: Check that all LMM are empty");
44 XBT_INFO("SUCCESS: %s", test_name);
45 XBT_INFO("#########################################################################################################");
52 static void test_sleep()
54 XBT_INFO("%s: Launch a sleep(5), and let it proceed", __func__);
57 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5", all_hosts[1], [&global]() {
58 assert_exit(false, 5.);
59 simgrid::s4u::this_actor::sleep_for(5);
62 simgrid::s4u::this_actor::sleep_for(10);
63 xbt_assert(global, "The forked actor did not modify the global after sleeping. Was it killed before?");
66 static void test_sleep_kill_middle()
68 XBT_INFO("%s: Launch a sleep(5), and kill it after 2 secs", __func__);
70 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_killed", all_hosts[1], []() {
72 simgrid::s4u::this_actor::sleep_for(5);
73 xbt_die("I should be dead now");
76 simgrid::s4u::this_actor::sleep_for(2);
80 static void test_sleep_kill_begin()
82 XBT_INFO("%s: Launch a sleep(5), and kill it right after start", __func__);
84 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_killed", all_hosts[1], []() {
86 simgrid::s4u::this_actor::sleep_for(5);
87 xbt_die("I should be dead now");
90 simgrid::s4u::this_actor::yield();
94 static void test_sleep_restart_begin()
96 XBT_INFO("%s: Launch a sleep(5), and restart its host right after start", __func__);
98 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
100 simgrid::s4u::this_actor::sleep_for(5);
101 xbt_die("I should be dead now");
104 simgrid::s4u::this_actor::yield();
105 sleeper5->get_host()->turn_off();
106 sleeper5->get_host()->turn_on();
107 XBT_INFO("Test %s is ending", __func__);
110 static void test_sleep_restart_middle()
112 XBT_INFO("%s: Launch a sleep(5), and restart its host after 2 secs", __func__);
114 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
115 assert_exit(true, 2);
116 simgrid::s4u::this_actor::sleep_for(5);
117 xbt_die("I should be dead now");
120 simgrid::s4u::this_actor::sleep_for(2);
121 sleeper5->get_host()->turn_off();
122 sleeper5->get_host()->turn_on();
123 XBT_INFO("Test %s is ending", __func__);
125 static void test_sleep_restart_end()
127 XBT_INFO("%s: Launch a sleep(5), and restart its host right when it stops", __func__);
128 bool sleeper_done = false;
130 simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], [&sleeper_done]() {
131 assert_exit(true, 5);
132 simgrid::s4u::this_actor::sleep_for(5);
133 all_hosts[1]->turn_off(); // kill the host right at the end of this sleep and of this actor
136 simgrid::s4u::this_actor::sleep_for(10);
137 all_hosts[1]->turn_on();
138 xbt_assert(sleeper_done, "Not sure of how the actor survived the shutdown of its host.");
140 static void test_exec()
142 XBT_INFO("%s: Launch a execute(5s), and let it proceed", __func__);
145 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&global]() {
146 assert_exit(false, 5.);
147 simgrid::s4u::this_actor::execute(500000000);
150 simgrid::s4u::this_actor::sleep_for(10);
151 xbt_assert(global, "The forked actor did not modify the global after executing. Was it killed before?");
154 static void test_exec_kill_middle()
156 XBT_INFO("%s: Launch a execute(5s), and kill it after 2 secs", __func__);
158 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_killed", all_hosts[1], []() {
159 assert_exit(true, 2);
160 simgrid::s4u::this_actor::execute(500000000);
161 xbt_die("I should be dead now");
164 simgrid::s4u::this_actor::sleep_for(2);
168 static void test_exec_kill_begin()
170 XBT_INFO("%s: Launch a execute(5s), and kill it right after start", __func__);
172 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_killed", all_hosts[1], []() {
173 assert_exit(true, 0);
174 simgrid::s4u::this_actor::execute(500000000);
175 xbt_die("I should be dead now");
178 simgrid::s4u::this_actor::yield();
182 static void test_exec_restart_begin()
184 XBT_INFO("%s: Launch a execute(5s), and restart its host right after start", __func__);
186 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_restarted", all_hosts[1], []() {
188 simgrid::s4u::this_actor::execute(500000000);
189 xbt_die("I should be dead now");
192 simgrid::s4u::this_actor::yield();
193 exec5->get_host()->turn_off();
194 exec5->get_host()->turn_on();
195 XBT_INFO("Test %s is ending", __func__);
198 static void test_exec_restart_middle()
200 XBT_INFO("%s: Launch a execute(5s), and restart its host after 2 secs", __func__);
202 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_restarted", all_hosts[1], []() {
203 assert_exit(true, 2);
204 simgrid::s4u::this_actor::execute(500000000);
205 xbt_die("I should be dead now");
208 simgrid::s4u::this_actor::sleep_for(2);
209 exec5->get_host()->turn_off();
210 exec5->get_host()->turn_on();
211 XBT_INFO("Test %s is ending", __func__);
213 static void test_exec_restart_end()
215 XBT_INFO("%s: Launch a execute(5s), and restart its host right when it stops", __func__);
216 bool execution_done = false;
218 simgrid::s4u::Actor::create("exec5_restarted", all_hosts[1], [&execution_done]() {
219 assert_exit(false, 5);
220 simgrid::s4u::this_actor::execute(500000000);
221 execution_done = true;
223 simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
224 simgrid::s4u::this_actor::sleep_for(5);
226 all_hosts[1]->turn_off();
227 all_hosts[1]->turn_on();
229 simgrid::s4u::this_actor::sleep_for(10);
230 xbt_assert(execution_done,
231 "Restarted actor was already dead in the scheduling round during which the host_off simcall was issued");
234 static void test_comm()
236 XBT_INFO("%s: Launch a communication", __func__);
237 bool send_done = false;
238 bool recv_done = false;
240 simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
241 assert_exit(false, 5);
242 char* payload = xbt_strdup("toto");
243 simgrid::s4u::Mailbox::by_name("mb")->put(payload, 5000);
246 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
247 assert_exit(false, 5);
248 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
253 simgrid::s4u::this_actor::sleep_for(10);
254 xbt_assert(send_done, "Sender killed somehow. It shouldn't");
255 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
258 static void test_comm_dsend_and_quit_put_before_get()
260 XBT_INFO("%s: Launch a detached communication and end right after", __func__);
261 bool dsend_done = false;
262 bool recv_done = false;
264 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
265 assert_exit(false, 0);
266 char* payload = xbt_strdup("toto");
267 simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
272 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
273 assert_exit(false, 3);
274 simgrid::s4u::this_actor::sleep_for(2);
275 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
281 // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
282 simgrid::s4u::this_actor::sleep_for(4);
283 xbt_assert(dsend_done, "Sender killed somehow. It shouldn't");
284 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
287 static void test_comm_dsend_and_quit_get_before_put()
289 XBT_INFO("%s: Launch a detached communication and end right after", __func__);
290 bool dsend_done = false;
291 bool recv_done = false;
293 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
294 assert_exit(false, 2);
295 char* payload = xbt_strdup("toto");
296 simgrid::s4u::this_actor::sleep_for(2);
297 simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
302 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
303 assert_exit(false, 3);
304 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
310 // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
311 simgrid::s4u::this_actor::sleep_for(4);
312 xbt_assert(dsend_done, "Sender killed somehow. It shouldn't");
313 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
317 static void test_comm_killsend()
319 XBT_INFO("%s: Launch a communication and kill the sender", __func__);
320 bool send_done = false;
321 bool recv_done = false;
323 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
324 assert_exit(true, 2);
325 // Encapsulate the payload in a std::unique_ptr so that it is correctly free'd when the sender is killed during its
326 // communication (thanks to RAII). The pointer is then released when the communication is over.
327 std::unique_ptr<char, decltype(&xbt_free_f)> payload(xbt_strdup("toto"), &xbt_free_f);
328 simgrid::s4u::Mailbox::by_name("mb")->put(payload.get(), 5000);
332 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
333 assert_exit(false, 2);
334 bool got_exception = false;
336 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
338 } catch (simgrid::NetworkFailureException const&) {
339 got_exception = true;
341 xbt_assert(got_exception);
345 simgrid::s4u::this_actor::sleep_for(2);
347 // let the test ends by itself. waiting for surf_precision should be enough.
348 simgrid::s4u::this_actor::sleep_for(0.00001);
350 xbt_assert(not send_done, "Sender was not killed properly");
351 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
354 static void test_host_off_while_receive()
356 XBT_INFO("%s: Launch an actor that waits on a recv, kill its host", __func__);
357 bool in_on_exit = false;
358 bool returned_from_main = false;
359 bool in_catch_before_on_exit = false;
360 bool in_catch_after_on_exit = false;
362 simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create(
363 "receiver", all_hosts[1],
364 [&in_on_exit, &returned_from_main, &in_catch_before_on_exit, &in_catch_after_on_exit]() {
365 assert_exit(true, 1);
367 simgrid::s4u::Mailbox::by_name("mb")->get();
368 } catch (simgrid::HostFailureException const&) {
369 in_catch_before_on_exit = not in_on_exit;
370 in_catch_after_on_exit = in_on_exit;
371 } catch (simgrid::NetworkFailureException const&) {
372 in_catch_before_on_exit = not in_on_exit;
373 in_catch_after_on_exit = in_on_exit;
375 returned_from_main = true;
378 receiver->on_exit([&in_on_exit](bool) { in_on_exit = true; });
380 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create(
381 "sender", all_hosts[2],
384 simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000);
387 simgrid::s4u::this_actor::sleep_for(1);
388 receiver->get_host()->turn_off();
390 // Note: If we don't sleep here, we don't "see" the bug
391 simgrid::s4u::this_actor::sleep_for(1);
393 xbt_assert(in_on_exit,
394 "Receiver's on_exit function was never called");
395 xbt_assert(not in_catch_before_on_exit,
396 "Receiver mistakenly went to catch clause (before the on_exit function was called)");
397 xbt_assert(not in_catch_after_on_exit,
398 "Receiver mistakenly went to catch clause (after the on_exit function was called)");
399 xbt_assert(not returned_from_main,
400 "Receiver returned from main normally even though its host was killed");
403 /* We need an extra actor here, so that it can sleep until the end of each test */
404 static void main_dispatcher()
406 run_test("sleep", static_cast<std::function<void()>>(test_sleep));
407 run_test("sleep killed at start", test_sleep_kill_begin);
408 run_test("sleep killed in middle", test_sleep_kill_middle);
409 /* We cannot kill right at the end of the action because killer actors are always rescheduled to the end of the round
410 * to avoid that they exit before their victim dereferences their name */
411 run_test("sleep restarted at start", test_sleep_restart_begin);
412 run_test("sleep restarted in middle", test_sleep_restart_middle);
413 // run_test("sleep restarted at end", test_sleep_restart_end);
415 run_test("exec", static_cast<std::function<void()>>(test_exec));
416 run_test("exec killed at start", test_exec_kill_begin);
417 run_test("exec killed in middle", test_exec_kill_middle);
418 run_test("exec restarted at start", test_exec_restart_begin);
419 run_test("exec restarted in middle", test_exec_restart_middle);
420 run_test("exec restarted at end", test_exec_restart_end);
422 run_test("comm", test_comm);
423 run_test("comm dsend and quit (put before get)", test_comm_dsend_and_quit_put_before_get);
424 run_test("comm dsend and quit (get before put)", test_comm_dsend_and_quit_get_before_put);
425 run_test("comm kill sender", test_comm_killsend);
427 //run_test("comm recv and kill", test_host_off_while_receive);
430 int main(int argc, char* argv[])
432 simgrid::s4u::Engine e(&argc, argv);
434 const char* platf = argv[1];
436 XBT_WARN("No platform file provided. Using './testing_platform.xml'");
437 platf = "./testing_platform.xml";
439 e.load_platform(platf);
441 all_hosts = e.get_all_hosts();
442 simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0], main_dispatcher);
446 XBT_INFO("Simulation done");