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(9);
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__);
126 static void test_sleep_restart_end()
128 XBT_INFO("%s: Launch a sleep(5), and restart its host right when it stops", __func__);
129 bool sleeper_done = false;
131 simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], [&sleeper_done]() {
132 assert_exit(false, 5);
133 simgrid::s4u::this_actor::sleep_for(5);
136 simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
137 simgrid::s4u::this_actor::sleep_for(5);
139 all_hosts[1]->turn_off();
140 all_hosts[1]->turn_on();
142 simgrid::s4u::this_actor::sleep_for(9);
143 xbt_assert(sleeper_done,
144 "Restarted actor was already dead in the scheduling round during which the host_off simcall was issued");
147 static void test_exec()
149 XBT_INFO("%s: Launch a execute(5s), and let it proceed", __func__);
152 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&global]() {
153 assert_exit(false, 5.);
154 simgrid::s4u::this_actor::execute(500000000);
157 simgrid::s4u::this_actor::sleep_for(9);
158 xbt_assert(global, "The forked actor did not modify the global after executing. Was it killed before?");
161 static void test_exec_kill_middle()
163 XBT_INFO("%s: Launch a execute(5s), and kill it after 2 secs", __func__);
165 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_killed", all_hosts[1], []() {
166 assert_exit(true, 2);
167 simgrid::s4u::this_actor::execute(500000000);
168 xbt_die("I should be dead now");
171 simgrid::s4u::this_actor::sleep_for(2);
175 static void test_exec_kill_begin()
177 XBT_INFO("%s: Launch a execute(5s), and kill it right after start", __func__);
179 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_killed", all_hosts[1], []() {
180 assert_exit(true, 0);
181 simgrid::s4u::this_actor::execute(500000000);
182 xbt_die("I should be dead now");
185 simgrid::s4u::this_actor::yield();
189 static void test_exec_restart_begin()
191 XBT_INFO("%s: Launch a execute(5s), and restart its host right after start", __func__);
193 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_restarted", all_hosts[1], []() {
195 simgrid::s4u::this_actor::execute(500000000);
196 xbt_die("I should be dead now");
199 simgrid::s4u::this_actor::yield();
200 exec5->get_host()->turn_off();
201 exec5->get_host()->turn_on();
202 XBT_INFO("Test %s is ending", __func__);
205 static void test_exec_restart_middle()
207 XBT_INFO("%s: Launch a execute(5s), and restart its host after 2 secs", __func__);
209 simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5_restarted", all_hosts[1], []() {
210 assert_exit(true, 2);
211 simgrid::s4u::this_actor::execute(500000000);
212 xbt_die("I should be dead now");
215 simgrid::s4u::this_actor::sleep_for(2);
216 exec5->get_host()->turn_off();
217 exec5->get_host()->turn_on();
218 XBT_INFO("Test %s is ending", __func__);
221 static void test_exec_restart_end()
223 XBT_INFO("%s: Launch a execute(5s), and restart its host right when it stops", __func__);
224 bool execution_done = false;
226 simgrid::s4u::Actor::create("exec5_restarted", all_hosts[1], [&execution_done]() {
227 assert_exit(false, 5);
228 simgrid::s4u::this_actor::execute(500000000);
229 execution_done = true;
231 simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
232 simgrid::s4u::this_actor::sleep_for(5);
234 all_hosts[1]->turn_off();
235 all_hosts[1]->turn_on();
237 simgrid::s4u::this_actor::sleep_for(9);
238 xbt_assert(execution_done,
239 "Restarted actor was already dead in the scheduling round during which the host_off simcall was issued");
242 static void test_comm()
244 XBT_INFO("%s: Launch a communication", __func__);
245 bool send_done = false;
246 bool recv_done = false;
248 simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
249 assert_exit(false, 5);
250 char* payload = xbt_strdup("toto");
251 simgrid::s4u::Mailbox::by_name("mb")->put(payload, 5000);
254 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
255 assert_exit(false, 5);
256 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
261 simgrid::s4u::this_actor::sleep_for(9);
262 xbt_assert(send_done, "Sender killed somehow. It shouldn't");
263 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
266 static void test_comm_dsend_and_quit_put_before_get()
268 XBT_INFO("%s: Launch a detached communication and end right after", __func__);
269 bool dsend_done = false;
270 bool recv_done = false;
272 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
273 assert_exit(false, 0);
274 char* payload = xbt_strdup("toto");
275 simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
279 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
280 assert_exit(false, 3);
281 simgrid::s4u::this_actor::sleep_for(2);
282 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
287 // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
288 simgrid::s4u::this_actor::sleep_for(4);
289 xbt_assert(dsend_done, "Sender killed somehow. It shouldn't");
290 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
293 static void test_comm_dsend_and_quit_get_before_put()
295 XBT_INFO("%s: Launch a detached communication and end right after", __func__);
296 bool dsend_done = false;
297 bool recv_done = false;
299 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
300 assert_exit(false, 2);
301 char* payload = xbt_strdup("toto");
302 simgrid::s4u::this_actor::sleep_for(2);
303 simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
307 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
308 assert_exit(false, 3);
309 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
314 // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
315 simgrid::s4u::this_actor::sleep_for(4);
316 xbt_assert(dsend_done, "Sender killed somehow. It shouldn't");
317 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
321 static void test_comm_killsend()
323 XBT_INFO("%s: Launch a communication and kill the sender", __func__);
324 bool send_done = false;
325 bool recv_done = false;
327 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
328 assert_exit(true, 2);
329 // Encapsulate the payload in a std::unique_ptr so that it is correctly free'd when the sender is killed during its
330 // communication (thanks to RAII). The pointer is then released when the communication is over.
331 std::unique_ptr<char, decltype(&xbt_free_f)> payload(xbt_strdup("toto"), &xbt_free_f);
332 simgrid::s4u::Mailbox::by_name("mb")->put(payload.get(), 5000);
336 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
337 assert_exit(false, 2);
338 bool got_exception = false;
340 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
342 } catch (simgrid::NetworkFailureException const&) {
343 got_exception = true;
345 xbt_assert(got_exception);
349 simgrid::s4u::this_actor::sleep_for(2);
351 // let the test ends by itself. waiting for surf_precision should be enough.
352 simgrid::s4u::this_actor::sleep_for(0.00001);
354 xbt_assert(not send_done, "Sender was not killed properly");
355 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
358 static void test_host_off_while_receive()
360 XBT_INFO("%s: Launch an actor that waits on a recv, kill its host", __func__);
361 bool in_on_exit = false;
362 bool returned_from_main = false;
363 bool in_catch_before_on_exit = false;
364 bool in_catch_after_on_exit = false;
365 bool send_done = false;
367 simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create(
368 "receiver", all_hosts[1],
369 [&in_on_exit, &returned_from_main, &in_catch_before_on_exit, &in_catch_after_on_exit]() {
370 assert_exit(true, 1);
372 simgrid::s4u::Mailbox::by_name("mb")->get();
373 } catch (simgrid::NetworkFailureException const&) {
374 // Shouldn't get in here
375 in_catch_before_on_exit = not in_on_exit;
376 in_catch_after_on_exit = in_on_exit;
378 returned_from_main = true;
381 receiver->on_exit([&in_on_exit](bool) { in_on_exit = true; });
383 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&send_done]() {
384 assert_exit(false, 1);
385 bool got_exception = false;
388 simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000);
389 } catch (simgrid::NetworkFailureException const&) {
390 got_exception = true;
392 xbt_assert(got_exception);
396 simgrid::s4u::this_actor::sleep_for(1);
397 receiver->get_host()->turn_off();
399 // Note: If we don't sleep here, we don't "see" the bug
400 simgrid::s4u::this_actor::sleep_for(1);
402 xbt_assert(in_on_exit,
403 "Receiver's on_exit function was never called");
404 xbt_assert(not in_catch_before_on_exit,
405 "Receiver mistakenly went to catch clause (before the on_exit function was called)");
406 xbt_assert(not in_catch_after_on_exit,
407 "Receiver mistakenly went to catch clause (after the on_exit function was called)");
408 xbt_assert(not returned_from_main,
409 "Receiver returned from main normally even though its host was killed");
410 xbt_assert(send_done, "Sender killed somehow. It shouldn't");
411 receiver->get_host()->turn_on();
414 static void test_link_off_helper(double delay)
416 const double start = simgrid::s4u::Engine::get_clock();
418 simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], [&start]() {
419 assert_exit(false, 9);
420 double milestone[5] = {0.5, 3.5, 4.5, 7.5, 9.0};
421 for (int i = 0; i < 5; i++)
422 milestone[i] += start;
423 for (int i = 0; i < 4; i++) {
424 simgrid::s4u::this_actor::sleep_until(milestone[i]);
426 XBT_VERB("get(%c)", 'A' + i);
427 simgrid::s4u::Mailbox::by_name("mb")->get();
429 } catch (simgrid::NetworkFailureException const&) {
430 XBT_VERB("got expected NetworkFailureException");
433 simgrid::s4u::this_actor::sleep_until(milestone[4]);
436 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&start]() {
437 assert_exit(false, 9);
439 double milestone[5] = {1.5, 2.5, 5.5, 6.5, 9.0};
440 for (int i = 0; i < 5; i++)
441 milestone[i] += start;
442 for (int i = 0; i < 2; i++) {
443 simgrid::s4u::this_actor::sleep_until(milestone[i]);
444 XBT_VERB("dsend(%c)", 'A' + i);
445 simgrid::s4u::Mailbox::by_name("mb")->put_init(&data, 100000)->detach();
447 for (int i = 2; i < 4; i++) {
448 simgrid::s4u::this_actor::sleep_until(milestone[i]);
450 XBT_VERB("put(%c)", 'A' + i);
451 simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000);
453 } catch (simgrid::NetworkFailureException const&) {
454 XBT_VERB("got expected NetworkFailureException");
457 simgrid::s4u::this_actor::sleep_until(milestone[4]);
460 for (int i = 0; i < 4; i++) {
461 XBT_VERB("##### %d / 4 #####", i + 1);
462 simgrid::s4u::this_actor::sleep_for(delay);
463 XBT_VERB("link off");
464 simgrid::s4u::Link::by_name("link1")->turn_off();
465 simgrid::s4u::this_actor::sleep_for(2.0 - delay);
467 simgrid::s4u::Link::by_name("link1")->turn_on();
469 simgrid::s4u::this_actor::sleep_for(1.5);
472 static void test_link_off_before_send_recv()
474 XBT_INFO("%s: try to communicate with communicating link turned off before start", __func__);
475 test_link_off_helper(0.0);
477 static void test_link_off_between_send_recv()
479 XBT_INFO("%s: try to communicate with communicating link turned off between send and receive", __func__);
480 test_link_off_helper(1.0);
482 static void test_link_off_during_transfer()
484 XBT_INFO("%s: try to communicate with communicating link turned off during transfer", __func__);
485 test_link_off_helper(2.0);
488 static void test_link_off_during_wait_any()
490 simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], []() {
491 assert_exit(false, 2);
492 bool receiver_got_network_failure_execution = false;
493 bool receiver_got_base_execution = false;
495 std::vector<simgrid::s4u::CommPtr> pending_comms;
496 simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name("mb")->get_async((void**)&data);
497 pending_comms.push_back(comm);
499 simgrid::s4u::Comm::wait_any(&pending_comms);
500 } catch (simgrid::NetworkFailureException const&) {
501 XBT_VERB("got expected NetworkFailureException");
502 receiver_got_network_failure_execution = true;
503 } catch (simgrid::Exception const&) {
504 XBT_VERB("got unexpected base Exception");
505 receiver_got_base_execution = true;
507 xbt_assert(receiver_got_network_failure_execution, "The receiver should have gotten a NetworkFailureException");
508 xbt_assert(not receiver_got_base_execution, "The receiver should not have gotten a base Exception");
511 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], []() {
512 assert_exit(false, 2);
514 bool sender_got_network_failure_execution = false;
515 bool sender_got_base_execution = false;
517 simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000);
518 } catch (simgrid::NetworkFailureException const&) {
519 XBT_VERB("got expected NetworkFailureException");
520 sender_got_network_failure_execution = true;
521 } catch (simgrid::Exception const&) {
522 XBT_VERB("got unexpected base Exception");
523 sender_got_base_execution = true;
525 xbt_assert(sender_got_network_failure_execution, "The sender should have gotten a NetworkFailureException");
526 xbt_assert(not sender_got_base_execution, "The sender should not have gotten a base Exception");
529 simgrid::s4u::this_actor::sleep_for(2.0);
530 XBT_VERB("link off");
531 simgrid::s4u::Link::by_name("link1")->turn_off();
532 simgrid::s4u::this_actor::sleep_for(2.0);
534 simgrid::s4u::Link::by_name("link1")->turn_on();
538 /* We need an extra actor here, so that it can sleep until the end of each test */
539 static void main_dispatcher()
541 run_test("sleep", test_sleep);
542 run_test("sleep killed at start", test_sleep_kill_begin);
543 run_test("sleep killed in middle", test_sleep_kill_middle);
544 /* We cannot kill right at the end of the action because killer actors are always rescheduled to the end of the round
545 * to avoid that they exit before their victim dereferences their name */
546 run_test("sleep restarted at start", test_sleep_restart_begin);
547 run_test("sleep restarted in middle", test_sleep_restart_middle);
548 run_test("sleep restarted at end", test_sleep_restart_end);
550 run_test("exec", test_exec);
551 run_test("exec killed at start", test_exec_kill_begin);
552 run_test("exec killed in middle", test_exec_kill_middle);
553 run_test("exec restarted at start", test_exec_restart_begin);
554 run_test("exec restarted in middle", test_exec_restart_middle);
555 run_test("exec restarted at end", test_exec_restart_end);
557 run_test("comm", test_comm);
558 run_test("comm dsend and quit (put before get)", test_comm_dsend_and_quit_put_before_get);
559 run_test("comm dsend and quit (get before put)", test_comm_dsend_and_quit_get_before_put);
560 run_test("comm kill sender", test_comm_killsend);
562 run_test("comm recv and kill", test_host_off_while_receive);
563 run_test("comm turn link off before send/recv", test_link_off_before_send_recv);
564 run_test("comm turn link off between send/recv", test_link_off_between_send_recv);
565 run_test("comm turn link off during transfer", test_link_off_during_transfer);
566 run_test("comm turn link off during wait_any", test_link_off_during_wait_any);
569 int main(int argc, char* argv[])
571 simgrid::s4u::Engine e(&argc, argv);
573 const char* platf = argv[1];
575 XBT_WARN("No platform file provided. Using './testing_platform.xml'");
576 platf = "./testing_platform.xml";
578 e.load_platform(platf);
580 all_hosts = e.get_all_hosts();
581 simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0], main_dispatcher);
585 XBT_INFO("Simulation done");