Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / teshsuite / s4u / vm-suicide / vm-suicide.cpp
1 /* Copyright (c) 2021-2022. The SimGrid Team. All rights reserved.               */
2
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. */
5
6 #include "simgrid/s4u.hpp"
7 #include "simgrid/s4u/VirtualMachine.hpp"
8
9 #include <limits>
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(example, "Messages specific to this example");
12
13 /* Test for issue https://github.com/simgrid/simgrid/issues/322: Issue when an actor kills his host vm
14  *
15  * A VM is created, and destroyed either from the outside or from the inside.
16  *
17  * The latter case initially failed because:
18  * - a call to "vm->destroy()" implicitly does a "vm->shutdown()" first;
19  * - when an actor wanted to destroy its own VM, it was killed by the shutdown phase,
20  *   and could not finish the destruction.
21  */
22
23 /* Output infos about physical hosts first: name, load, actors present in them
24  * and then output infos about vm hosts: name, physical host, load, state, actors present in them
25  */
26 static void print_status(const std::vector<simgrid::s4u::Host*>& hosts)
27 {
28   XBT_INFO("---- HOSTS and VMS STATUS ----");
29   XBT_INFO("--- HOSTS ---");
30   for (auto const& host : hosts) {
31     XBT_INFO("+ Name:%s Load:%f", host->get_cname(), host->get_load());
32     for (auto const& actor : host->get_all_actors())
33       XBT_INFO("++ actor: %s", actor->get_cname());
34   }
35   XBT_INFO("--- VMS ---");
36   for (auto const& host : simgrid::s4u::Engine::get_instance()->get_all_hosts()) {
37     if (auto vm = dynamic_cast<simgrid::s4u::VirtualMachine*>(host)) {
38       XBT_INFO("+ Name:%s Host:%s Load:%f State: %s", vm->get_cname(), vm->get_pm()->get_cname(), vm->get_load(),
39                simgrid::s4u::VirtualMachine::to_c_str(vm->get_state()));
40       for (auto const& actor : host->get_all_actors())
41         XBT_INFO("++ actor: %s", actor->get_cname());
42     }
43   }
44   XBT_INFO("------------------------------");
45 }
46
47 /* actor launching an infinite task on all the cores of its vm
48  */
49 static void life_cycle_manager()
50 {
51   simgrid::s4u::Host* vm = simgrid::s4u::this_actor::get_host();
52   for (int i = 0; i < vm->get_core_count(); i++)
53     simgrid::s4u::this_actor::exec_async(std::numeric_limits<double>::max());
54   simgrid::s4u::this_actor::sleep_for(50);
55   XBT_INFO("I'm done sleeping, time to kill myself");
56   vm->destroy();
57 }
58
59 /* master actor (placed on hosts[0])
60  *
61  * create a vm on hoss[1] and hosts[2]
62  * launch an actor creating an infinite task (exec_async) inside this vm
63  *
64  * task is either killed by this master or by the actor himself
65  */
66 static void master(const std::vector<simgrid::s4u::Host*>& hosts)
67 {
68   for (int i = 1; i <= 2; i++) {
69     auto* vm = hosts.at(i)->create_vm("test_vm", 4);
70     vm->start();
71     simgrid::s4u::Actor::create("life_cycle_manager-" + std::to_string(i), vm, life_cycle_manager);
72
73     simgrid::s4u::this_actor::sleep_for(10);
74     print_status(hosts);
75
76     simgrid::s4u::this_actor::sleep_for(30);
77     if (i == 1) {
78       XBT_INFO("Time to kill VM from master");
79       vm->destroy();
80     }
81
82     simgrid::s4u::this_actor::sleep_for(20);
83     print_status(hosts);
84   }
85 }
86
87 int main(int argc, char* argv[])
88 {
89   simgrid::s4u::Engine e(&argc, argv);
90
91   xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
92
93   e.load_platform(argv[1]);
94   std::vector<simgrid::s4u::Host*> hosts = e.get_all_hosts();
95   xbt_assert(hosts.size() > 3);
96   hosts.resize(3);
97   simgrid::s4u::Actor::create("test_master", hosts[0], master, hosts);
98
99   e.run();
100   return 0;
101 }