1 /* Copyright (c) 2021-2023. 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"
7 #include "simgrid/s4u/VirtualMachine.hpp"
11 XBT_LOG_NEW_DEFAULT_CATEGORY(example, "Messages specific to this example");
13 /* Test for issue https://github.com/simgrid/simgrid/issues/322: Issue when an actor kills his host vm
15 * A VM is created, and destroyed either from the outside or from the inside.
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.
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
26 static void print_status(const std::vector<simgrid::s4u::Host*>& hosts)
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());
35 XBT_INFO("--- VMS ---");
36 for (auto const* host : simgrid::s4u::Engine::get_instance()->get_all_hosts()) {
37 if (auto const* vm = dynamic_cast<const 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());
44 XBT_INFO("------------------------------");
47 /* actor launching an infinite task on all the cores of its vm
49 static void life_cycle_manager()
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");
59 /* master actor (placed on hosts[0])
61 * create a vm on hoss[1] and hosts[2]
62 * launch an actor creating an infinite task (exec_async) inside this vm
64 * task is either killed by this master or by the actor himself
66 static void master(const std::vector<simgrid::s4u::Host*>& hosts)
68 for (int i = 1; i <= 2; i++) {
69 auto* vm = hosts.at(i)->create_vm("test_vm", 4);
71 simgrid::s4u::Actor::create("life_cycle_manager-" + std::to_string(i), vm, life_cycle_manager);
73 simgrid::s4u::this_actor::sleep_for(10);
76 simgrid::s4u::this_actor::sleep_for(30);
78 XBT_INFO("Time to kill VM from master");
82 simgrid::s4u::this_actor::sleep_for(20);
87 int main(int argc, char* argv[])
89 simgrid::s4u::Engine e(&argc, argv);
91 xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
93 e.load_platform(argv[1]);
94 std::vector<simgrid::s4u::Host*> hosts = e.get_all_hosts();
95 xbt_assert(hosts.size() > 3);
97 simgrid::s4u::Actor::create("test_master", hosts[0], master, hosts);