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/Exception.hpp"
7 #include "simgrid/s4u.hpp"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
13 static void actor_daemon()
15 simgrid::s4u::Host* host = simgrid::s4u::Host::current();
16 XBT_INFO(" Start daemon on %s (%f)", host->get_cname(), host->get_speed());
18 XBT_INFO(" Execute daemon");
19 simgrid::s4u::this_actor::execute(host->get_speed());
22 XBT_INFO(" daemon done. See you!");
27 XBT_INFO(" Start TX");
28 simgrid::s4u::Mailbox::by_name("comm")->put_init(xbt_strdup("COMM"), 100000000)->detach();
29 // We should wait a bit (if not the process will end before the communication, hence an exception on the other side).
31 simgrid::s4u::this_actor::sleep_for(30);
32 } catch (const simgrid::HostFailureException&) {
33 XBT_INFO("The host has died ... as expected.");
40 char* payload = nullptr;
41 XBT_INFO(" Start RX");
43 payload = static_cast<char*>(simgrid::s4u::Mailbox::by_name("comm")->get());
44 XBT_INFO(" Receive message: %s", payload);
45 } catch (const simgrid::HostFailureException&) {
46 XBT_INFO(" Receive message: HOST_FAILURE");
47 } catch (const simgrid::NetworkFailureException&) {
48 XBT_INFO(" Receive message: TRANSFER_FAILURE");
55 static void test_launcher(int test_number)
57 simgrid::s4u::Host* jupiter = simgrid::s4u::Host::by_name("Jupiter");
58 simgrid::s4u::ActorPtr daemon;
59 simgrid::s4u::VirtualMachine* vm0 = nullptr;
61 switch (test_number) {
63 // Create a process running a simple task on a host and turn the host off during the execution of the actor.
65 XBT_INFO(" Create an actor on Jupiter");
66 simgrid::s4u::Actor::create("actor_daemon", jupiter, actor_daemon);
67 simgrid::s4u::this_actor::sleep_for(3);
68 XBT_INFO(" Turn off Jupiter");
70 simgrid::s4u::this_actor::sleep_for(10);
71 XBT_INFO("Test 1 seems ok, cool !(#Actors: %zu, it should be 1; #tasks: %d)",
72 simgrid::s4u::Engine::get_instance()->get_actor_count(), tasks_done);
75 // Create an actorthat on a host that is turned off (this is not allowed)
77 XBT_INFO(" Turn off Jupiter");
78 // adsein: Jupiter is already off, hence nothing should happen
79 // adsein: This can be one additional test, to check that you cannot shutdown twice a host
82 simgrid::s4u::Actor::create("actor_daemon", jupiter, actor_daemon);
83 simgrid::s4u::this_actor::sleep_for(10);
84 XBT_INFO(" Test 2 does crash as it should. This message will not be displayed.");
85 } catch (const simgrid::HostFailureException&) {
86 xbt_die("Could not launch a new actor on failed host %s.", jupiter->get_cname());
90 // Create an actor running successive sleeps on a host and turn the host off during the execution of the actor.
91 xbt_die("Test 3 is superseded by activity-lifecycle");
94 XBT_INFO("Test 4 (turn off src during a communication) : Create an actor/task to make a communication between "
95 "Jupiter and Tremblay and turn off Jupiter during the communication");
97 simgrid::s4u::this_actor::sleep_for(10);
98 simgrid::s4u::Actor::create("commRX", simgrid::s4u::Host::by_name("Tremblay"), commRX);
99 simgrid::s4u::Actor::create("commTX", jupiter, commTX);
100 XBT_INFO(" number of actors: %zu", simgrid::s4u::Engine::get_instance()->get_actor_count());
101 simgrid::s4u::this_actor::sleep_for(10);
102 XBT_INFO(" Turn Jupiter off");
104 XBT_INFO("Test 4 is ok. (number of actors : %zu, it should be 1 or 2 if RX has not been satisfied)."
105 " An exception is raised when we turn off a node that has an actor sleeping",
106 simgrid::s4u::Engine::get_instance()->get_actor_count());
109 XBT_INFO("Test 5 (turn off dest during a communication : Create an actor/task to make a communication between "
110 "Tremblay and Jupiter and turn off Jupiter during the communication");
112 simgrid::s4u::this_actor::sleep_for(10);
113 simgrid::s4u::Actor::create("commRX", jupiter, commRX);
114 simgrid::s4u::Actor::create("commTX", simgrid::s4u::Host::by_name("Tremblay"), commTX);
115 XBT_INFO(" number of actors: %zu", simgrid::s4u::Engine::get_instance()->get_actor_count());
116 simgrid::s4u::this_actor::sleep_for(10);
117 XBT_INFO(" Turn Jupiter off");
119 XBT_INFO("Test 5 seems ok (number of actors: %zu, it should be 2)",
120 simgrid::s4u::Engine::get_instance()->get_actor_count());
123 XBT_INFO("Test 6: Turn on Jupiter, assign a VM on Jupiter, launch an actor inside the VM, and turn off the node");
126 vm0 = new simgrid::s4u::VirtualMachine("vm0", jupiter, 1);
129 daemon = simgrid::s4u::Actor::create("actor_daemon", vm0, actor_daemon);
130 simgrid::s4u::Actor::create("actor_daemonJUPI", jupiter, actor_daemon);
136 simgrid::s4u::this_actor::sleep_for(10);
138 XBT_INFO(" Turn Jupiter off");
140 XBT_INFO(" Shutdown vm0");
142 XBT_INFO(" Destroy vm0");
144 XBT_INFO("Test 6 is also weird: when the node Jupiter is turned off once again, the VM and its daemon are not "
145 "killed. However, the issue regarding the shutdown of hosted VMs can be seen a feature not a bug ;)");
148 xbt_die("Unknown test case.");
151 XBT_INFO(" Test done. See you!");
154 int main(int argc, char* argv[])
156 simgrid::s4u::Engine e(&argc, argv);
157 xbt_assert(argc == 3, "Usage: %s platform_file test_number\n\tExample: %s msg_platform.xml 1\n", argv[0], argv[0]);
159 e.load_platform(argv[1]);
161 simgrid::s4u::Actor::create("test_launcher", simgrid::s4u::Host::by_name("Tremblay"), test_launcher,
166 XBT_INFO("Simulation time %g", e.get_clock());