1 /* Copyright (c) 2010-2020. 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_turn_off_itself()
244 XBT_INFO("%s: Launch a sleep(5), then saw off the branch it's sitting on", __func__);
246 simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
247 assert_exit(true, 5);
248 simgrid::s4u::this_actor::sleep_for(5);
249 simgrid::s4u::this_actor::get_host()->turn_off();
250 xbt_die("I should be dead now");
252 simgrid::s4u::this_actor::sleep_for(9);
253 all_hosts[1]->turn_on();
254 XBT_INFO("Test %s is ending", __func__);
257 static void test_comm()
259 XBT_INFO("%s: Launch a communication", __func__);
260 bool send_done = false;
261 bool recv_done = false;
263 simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
264 assert_exit(false, 5);
265 char* payload = xbt_strdup("toto");
266 simgrid::s4u::Mailbox::by_name("mb")->put(payload, 5000);
269 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
270 assert_exit(false, 5);
271 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
276 simgrid::s4u::this_actor::sleep_for(9);
277 xbt_assert(send_done, "Sender killed somehow. It shouldn't");
278 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
281 static void test_comm_dsend_and_quit_put_before_get()
283 XBT_INFO("%s: Launch a detached communication and end right after", __func__);
284 bool dsend_done = false;
285 bool recv_done = false;
287 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
288 assert_exit(false, 0);
289 char* payload = xbt_strdup("toto");
290 simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
294 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
295 assert_exit(false, 3);
296 simgrid::s4u::this_actor::sleep_for(2);
297 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
302 // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
303 simgrid::s4u::this_actor::sleep_for(4);
304 xbt_assert(dsend_done, "Sender killed somehow. It shouldn't");
305 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
308 static void test_comm_dsend_and_quit_get_before_put()
310 XBT_INFO("%s: Launch a detached communication and end right after", __func__);
311 bool dsend_done = false;
312 bool recv_done = false;
314 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
315 assert_exit(false, 2);
316 char* payload = xbt_strdup("toto");
317 simgrid::s4u::this_actor::sleep_for(2);
318 simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
322 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
323 assert_exit(false, 3);
324 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
329 // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
330 simgrid::s4u::this_actor::sleep_for(4);
331 xbt_assert(dsend_done, "Sender killed somehow. It shouldn't");
332 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
336 static void test_comm_killsend()
338 XBT_INFO("%s: Launch a communication and kill the sender", __func__);
339 bool send_done = false;
340 bool recv_done = false;
342 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
343 assert_exit(true, 2);
344 // Encapsulate the payload in a std::unique_ptr so that it is correctly free'd when the sender is killed during its
345 // communication (thanks to RAII). The pointer is then released when the communication is over.
346 std::unique_ptr<char, decltype(&xbt_free_f)> payload(xbt_strdup("toto"), &xbt_free_f);
347 simgrid::s4u::Mailbox::by_name("mb")->put(payload.get(), 5000);
351 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
352 assert_exit(false, 2);
353 bool got_exception = false;
355 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
357 } catch (simgrid::NetworkFailureException const&) {
358 got_exception = true;
360 xbt_assert(got_exception);
364 simgrid::s4u::this_actor::sleep_for(2);
366 // let the test ends by itself. waiting for surf_precision should be enough.
367 simgrid::s4u::this_actor::sleep_for(0.00001);
369 xbt_assert(not send_done, "Sender was not killed properly");
370 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
373 static void test_host_off_while_receive()
375 XBT_INFO("%s: Launch an actor that waits on a recv, kill its host", __func__);
376 bool in_on_exit = false;
377 bool returned_from_main = false;
378 bool in_catch_before_on_exit = false;
379 bool in_catch_after_on_exit = false;
380 bool send_done = false;
382 simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create(
383 "receiver", all_hosts[1],
384 [&in_on_exit, &returned_from_main, &in_catch_before_on_exit, &in_catch_after_on_exit]() {
385 assert_exit(true, 1);
387 simgrid::s4u::Mailbox::by_name("mb")->get();
388 } catch (simgrid::NetworkFailureException const&) {
389 // Shouldn't get in here
390 in_catch_before_on_exit = not in_on_exit;
391 in_catch_after_on_exit = in_on_exit;
393 returned_from_main = true;
396 receiver->on_exit([&in_on_exit](bool) { in_on_exit = true; });
398 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&send_done]() {
399 assert_exit(false, 1);
400 bool got_exception = false;
403 simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000);
404 } catch (simgrid::NetworkFailureException const&) {
405 got_exception = true;
407 xbt_assert(got_exception);
411 simgrid::s4u::this_actor::sleep_for(1);
412 receiver->get_host()->turn_off();
414 // Note: If we don't sleep here, we don't "see" the bug
415 simgrid::s4u::this_actor::sleep_for(1);
417 xbt_assert(in_on_exit,
418 "Receiver's on_exit function was never called");
419 xbt_assert(not in_catch_before_on_exit,
420 "Receiver mistakenly went to catch clause (before the on_exit function was called)");
421 xbt_assert(not in_catch_after_on_exit,
422 "Receiver mistakenly went to catch clause (after the on_exit function was called)");
423 xbt_assert(not returned_from_main,
424 "Receiver returned from main normally even though its host was killed");
425 xbt_assert(send_done, "Sender killed somehow. It shouldn't");
426 receiver->get_host()->turn_on();
429 static void test_link_off_helper(double delay)
431 const double start = simgrid::s4u::Engine::get_clock();
433 simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], [&start]() {
434 assert_exit(false, 9);
435 double milestone[5] = {0.5, 3.5, 4.5, 7.5, 9.0};
436 for (int i = 0; i < 5; i++)
437 milestone[i] += start;
438 for (int i = 0; i < 4; i++) {
439 simgrid::s4u::this_actor::sleep_until(milestone[i]);
441 XBT_VERB("get(%c)", 'A' + i);
442 simgrid::s4u::Mailbox::by_name("mb")->get();
444 } catch (simgrid::NetworkFailureException const&) {
445 XBT_VERB("got expected NetworkFailureException");
448 simgrid::s4u::this_actor::sleep_until(milestone[4]);
451 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], [&start]() {
452 assert_exit(false, 9);
454 double milestone[5] = {1.5, 2.5, 5.5, 6.5, 9.0};
455 for (int i = 0; i < 5; i++)
456 milestone[i] += start;
457 for (int i = 0; i < 2; i++) {
458 simgrid::s4u::this_actor::sleep_until(milestone[i]);
459 XBT_VERB("dsend(%c)", 'A' + i);
460 simgrid::s4u::Mailbox::by_name("mb")->put_init(&data, 100000)->detach();
462 for (int i = 2; i < 4; i++) {
463 simgrid::s4u::this_actor::sleep_until(milestone[i]);
465 XBT_VERB("put(%c)", 'A' + i);
466 simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000);
468 } catch (simgrid::NetworkFailureException const&) {
469 XBT_VERB("got expected NetworkFailureException");
472 simgrid::s4u::this_actor::sleep_until(milestone[4]);
475 for (int i = 0; i < 4; i++) {
476 XBT_VERB("##### %d / 4 #####", i + 1);
477 simgrid::s4u::this_actor::sleep_for(delay);
478 XBT_VERB("link off");
479 simgrid::s4u::Link::by_name("link1")->turn_off();
480 simgrid::s4u::this_actor::sleep_for(2.0 - delay);
482 simgrid::s4u::Link::by_name("link1")->turn_on();
484 simgrid::s4u::this_actor::sleep_for(1.5);
487 static void test_link_off_before_send_recv()
489 XBT_INFO("%s: try to communicate with communicating link turned off before start", __func__);
490 test_link_off_helper(0.0);
492 static void test_link_off_between_send_recv()
494 XBT_INFO("%s: try to communicate with communicating link turned off between send and receive", __func__);
495 test_link_off_helper(1.0);
497 static void test_link_off_during_transfer()
499 XBT_INFO("%s: try to communicate with communicating link turned off during transfer", __func__);
500 test_link_off_helper(2.0);
503 static void test_link_off_during_wait_any()
505 simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], []() {
506 assert_exit(false, 2);
507 bool receiver_got_network_failure_execution = false;
508 bool receiver_got_base_execution = false;
510 std::vector<simgrid::s4u::CommPtr> pending_comms;
511 simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name("mb")->get_async((void**)&data);
512 pending_comms.push_back(comm);
514 simgrid::s4u::Comm::wait_any(&pending_comms);
515 } catch (simgrid::NetworkFailureException const&) {
516 XBT_VERB("got expected NetworkFailureException");
517 receiver_got_network_failure_execution = true;
518 } catch (simgrid::Exception const&) {
519 XBT_VERB("got unexpected base Exception");
520 receiver_got_base_execution = true;
522 xbt_assert(receiver_got_network_failure_execution, "The receiver should have gotten a NetworkFailureException");
523 xbt_assert(not receiver_got_base_execution, "The receiver should not have gotten a base Exception");
526 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[2], []() {
527 assert_exit(false, 2);
529 bool sender_got_network_failure_execution = false;
530 bool sender_got_base_execution = false;
532 simgrid::s4u::Mailbox::by_name("mb")->put(&data, 100000);
533 } catch (simgrid::NetworkFailureException const&) {
534 XBT_VERB("got expected NetworkFailureException");
535 sender_got_network_failure_execution = true;
536 } catch (simgrid::Exception const&) {
537 XBT_VERB("got unexpected base Exception");
538 sender_got_base_execution = true;
540 xbt_assert(sender_got_network_failure_execution, "The sender should have gotten a NetworkFailureException");
541 xbt_assert(not sender_got_base_execution, "The sender should not have gotten a base Exception");
544 simgrid::s4u::this_actor::sleep_for(2.0);
545 XBT_VERB("link off");
546 simgrid::s4u::Link::by_name("link1")->turn_off();
547 simgrid::s4u::this_actor::sleep_for(2.0);
549 simgrid::s4u::Link::by_name("link1")->turn_on();
553 /* We need an extra actor here, so that it can sleep until the end of each test */
554 static void main_dispatcher()
556 run_test("sleep", test_sleep);
557 run_test("sleep killed at start", test_sleep_kill_begin);
558 run_test("sleep killed in middle", test_sleep_kill_middle);
559 /* We cannot kill right at the end of the action because killer actors are always rescheduled to the end of the round
560 * to avoid that they exit before their victim dereferences their name */
561 run_test("sleep restarted at start", test_sleep_restart_begin);
562 run_test("sleep restarted in middle", test_sleep_restart_middle);
563 run_test("sleep restarted at end", test_sleep_restart_end);
565 run_test("exec", test_exec);
566 run_test("exec killed at start", test_exec_kill_begin);
567 run_test("exec killed in middle", test_exec_kill_middle);
568 run_test("exec restarted at start", test_exec_restart_begin);
569 run_test("exec restarted in middle", test_exec_restart_middle);
570 run_test("exec restarted at end", test_exec_restart_end);
572 run_test("turn off its own host", test_turn_off_itself);
574 run_test("comm", test_comm);
575 run_test("comm dsend and quit (put before get)", test_comm_dsend_and_quit_put_before_get);
576 run_test("comm dsend and quit (get before put)", test_comm_dsend_and_quit_get_before_put);
577 run_test("comm kill sender", test_comm_killsend);
579 run_test("comm recv and kill", test_host_off_while_receive);
580 run_test("comm turn link off before send/recv", test_link_off_before_send_recv);
581 run_test("comm turn link off between send/recv", test_link_off_between_send_recv);
582 run_test("comm turn link off during transfer", test_link_off_during_transfer);
583 run_test("comm turn link off during wait_any", test_link_off_during_wait_any);
586 int main(int argc, char* argv[])
588 simgrid::s4u::Engine e(&argc, argv);
590 const char* platf = argv[1];
592 XBT_WARN("No platform file provided. Using './testing_platform.xml'");
593 platf = "./testing_platform.xml";
595 e.load_platform(platf);
597 all_hosts = e.get_all_hosts();
598 simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0], main_dispatcher);
602 XBT_INFO("Simulation done");