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(int status, double duration)
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");
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)
30 simgrid::s4u::Actor::create(test_name, all_hosts[0], test);
31 simgrid::s4u::this_actor::sleep_for(10);
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");
45 xbt_assert("TODO: Check that all LMM are empty");
46 XBT_INFO("SUCCESS: %s", test_name);
47 XBT_INFO("#########################################################################################################");
54 static void test_sleep()
56 XBT_INFO("%s: Launch a sleep(5), and let it proceed", __func__);
59 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5", all_hosts[1], [&global]() {
61 simgrid::s4u::this_actor::sleep_for(5);
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?");
68 static void test_sleep_kill_middle()
70 XBT_INFO("%s: Launch a sleep(5), and kill it after 2 secs", __func__);
72 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_killed", all_hosts[1], []() {
74 simgrid::s4u::this_actor::sleep_for(5);
75 xbt_die("I should be dead now");
78 simgrid::s4u::this_actor::sleep_for(2);
82 static void test_sleep_kill_begin()
84 XBT_INFO("%s: Launch a sleep(5), and kill it right after start", __func__);
86 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_killed", all_hosts[1], []() {
88 simgrid::s4u::this_actor::sleep_for(5);
89 xbt_die("I should be dead now");
92 simgrid::s4u::this_actor::yield();
96 static void test_sleep_restart_begin()
98 XBT_INFO("%s: Launch a sleep(5), and restart its host right after start", __func__);
100 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
102 simgrid::s4u::this_actor::sleep_for(5);
103 xbt_die("I should be dead now");
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__);
112 static void test_sleep_restart_middle()
114 XBT_INFO("%s: Launch a sleep(5), and restart its host after 2 secs", __func__);
116 simgrid::s4u::ActorPtr sleeper5 = simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], []() {
118 simgrid::s4u::this_actor::sleep_for(5);
119 xbt_die("I should be dead now");
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__);
127 static void test_sleep_restart_end()
129 XBT_INFO("%s: Launch a sleep(5), and restart its host right when it stops", __func__);
130 bool sleeper_done = false;
132 simgrid::s4u::Actor::create("sleep5_restarted", all_hosts[1], [&sleeper_done]() {
134 simgrid::s4u::this_actor::sleep_for(5);
137 simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
138 simgrid::s4u::this_actor::sleep_for(5);
140 all_hosts[1]->turn_off();
141 all_hosts[1]->turn_on();
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");
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]() {
154 simgrid::s4u::this_actor::execute(500000000);
157 simgrid::s4u::this_actor::sleep_for(10);
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], []() {
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], []() {
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], []() {
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__);
220 static void test_exec_restart_end()
222 XBT_INFO("%s: Launch a execute(5s), and restart its host right when it stops", __func__);
223 bool execution_done = false;
225 simgrid::s4u::Actor::create("exec5_restarted", all_hosts[1], [&execution_done]() {
227 simgrid::s4u::this_actor::execute(500000000);
228 execution_done = true;
230 simgrid::s4u::Actor::create("killer", all_hosts[0], []() {
231 simgrid::s4u::this_actor::sleep_for(5);
233 all_hosts[1]->turn_off();
234 all_hosts[1]->turn_on();
236 simgrid::s4u::this_actor::sleep_for(10);
237 xbt_assert(execution_done,
238 "Restarted actor was already dead in the scheduling round during which the host_off simcall was issued");
241 static void test_comm()
243 XBT_INFO("%s: Launch a communication", __func__);
244 bool send_done = false;
245 bool recv_done = false;
247 simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
249 char* payload = xbt_strdup("toto");
250 simgrid::s4u::Mailbox::by_name("mb")->put(payload, 5000);
253 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
255 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
260 simgrid::s4u::this_actor::sleep_for(10);
261 xbt_assert(send_done, "Sender killed somehow. It shouldn't");
262 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
265 static void test_comm_dsend_and_quit()
267 XBT_INFO("%s: Launch a detached communication and end right after", __func__);
268 bool dsend_done = false;
269 bool recv_done = false;
271 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&dsend_done]() {
273 char* payload = xbt_strdup("toto");
274 simgrid::s4u::Mailbox::by_name("mb")->put_init(payload, 1000)->detach();
279 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
281 bool got_exception = false;
282 simgrid::s4u::this_actor::sleep_for(2);
284 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
286 } catch (xbt_ex const& e) {
287 got_exception = true;
290 xbt_assert(not got_exception);
294 // Sleep long enough to let the test ends by itself. 3 + surf_precision should be enough.
295 simgrid::s4u::this_actor::sleep_for(4);
296 xbt_assert(dsend_done, "Sender killed somehow. It shouldn't");
297 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
300 static void test_comm_killsend()
302 XBT_INFO("%s: Launch a communication and kill the sender", __func__);
303 bool send_done = false;
304 bool recv_done = false;
306 simgrid::s4u::ActorPtr sender = simgrid::s4u::Actor::create("sender", all_hosts[1], [&send_done]() {
308 // Encapsulate the payload in a std::unique_ptr so that it is correctly free'd when the sender is killed during its
309 // communication (thanks to RAII). The pointer is then released when the communication is over.
310 std::unique_ptr<char, decltype(&xbt_free_f)> payload(xbt_strdup("toto"), &xbt_free_f);
311 simgrid::s4u::Mailbox::by_name("mb")->put(payload.get(), 5000);
315 simgrid::s4u::Actor::create("receiver", all_hosts[2], [&recv_done]() {
317 bool got_exception = false;
319 void* payload = simgrid::s4u::Mailbox::by_name("mb")->get();
321 } catch (simgrid::NetworkFailureException const&) {
322 got_exception = true;
324 xbt_assert(got_exception);
328 simgrid::s4u::this_actor::sleep_for(2);
330 // let the test ends by itself. waiting for surf_precision should be enough.
331 simgrid::s4u::this_actor::sleep_for(0.00001);
333 xbt_assert(not send_done, "Sender was not killed properly");
334 xbt_assert(recv_done, "Receiver killed somehow. It shouldn't");
337 /* We need an extra actor here, so that it can sleep until the end of each test */
338 static void main_dispatcher()
340 run_test("sleep", static_cast<std::function<void()>>(test_sleep));
341 run_test("sleep killed at start", test_sleep_kill_begin);
342 run_test("sleep killed in middle", test_sleep_kill_middle);
343 /* We cannot kill right at the end of the action because killer actors are always rescheduled to the end of the round
344 * to avoid that they exit before their victim dereferences their name */
345 run_test("sleep restarted at start", test_sleep_restart_begin);
346 run_test("sleep restarted at middle", test_sleep_restart_middle);
347 run_test("sleep restarted at end", test_sleep_restart_end);
349 run_test("exec", static_cast<std::function<void()>>(test_exec));
350 run_test("exec killed at start", test_exec_kill_begin);
351 run_test("exec killed in middle", test_exec_kill_middle);
352 run_test("exec restarted at start", test_exec_restart_begin);
353 run_test("exec restarted at middle", test_exec_restart_middle);
354 run_test("exec restarted at end", test_exec_restart_end);
356 run_test("comm", test_comm);
357 run_test("comm dsend and quit", test_comm_dsend_and_quit);
358 run_test("comm kill sender", test_comm_killsend);
361 int main(int argc, char* argv[])
363 simgrid::s4u::Engine e(&argc, argv);
365 const char* platf = argv[1];
367 XBT_WARN("No platform file provided. Using './testing_platform.xml'");
368 platf = "./testing_platform.xml";
370 e.load_platform(platf);
372 all_hosts = e.get_all_hosts();
373 simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0], main_dispatcher);
377 XBT_INFO("Simulation done");