Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'Adrien.Gougeon/simgrid-master'
[simgrid.git] / teshsuite / s4u / vm-live-migration / vm-live-migration.cpp
1 /* Copyright (c) 2007-2020. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/plugins/energy.h"
8 #include "simgrid/plugins/live_migration.h"
9 #include "simgrid/s4u.hpp"
10 #include <cmath>
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(example, "Messages specific for this example");
13
14 static void task_executor()
15 {
16   simgrid::s4u::ExecPtr ptr = simgrid::s4u::this_actor::exec_init(3000000000.0)->start();
17
18   // ptr->test(); // Harmless
19   simgrid::s4u::this_actor::sleep_for(3); // Breaks everything
20
21   ptr->wait();
22   const auto* vm = dynamic_cast<simgrid::s4u::VirtualMachine*>(ptr->get_host());
23   xbt_assert(vm != nullptr, "Hey, I expected to run on a VM");
24   XBT_INFO("Task done. It's running on %s@%s that runs at %.0ef/s. host2 runs at %.0ef/s", vm->get_cname(),
25            vm->get_pm()->get_cname(), vm->get_speed(), simgrid::s4u::Host::by_name("host2")->get_speed());
26 }
27
28 int main(int argc, char* argv[])
29 {
30   simgrid::s4u::Engine e(&argc, argv);
31   simgrid::s4u::Engine::set_config("network/model:CM02"); // Much less realistic, but easier to compute manually
32
33   sg_vm_live_migration_plugin_init();
34   sg_host_energy_plugin_init();
35
36   xbt_assert(argc == 2, "Usage: %s platform.xml\n", argv[0]);
37
38   e.load_platform(argv[1]);
39   auto* pm = simgrid::s4u::Host::by_name("host1");
40   auto* vm = new simgrid::s4u::VirtualMachine("VM0", pm, 1 /*nCores*/);
41   vm->set_ramsize(1250000000)->start();
42   simgrid::s4u::Actor::create("executor", vm, task_executor);
43
44   simgrid::s4u::Actor::create("migration", pm, [vm]() {
45     XBT_INFO("%s migration started", vm->get_cname());
46     const auto* old = vm->get_pm();
47
48     sg_vm_migrate(vm, simgrid::s4u::Host::by_name("host2"));
49
50     XBT_INFO("VM '%s' migrated from '%s' to '%s'", vm->get_cname(), old->get_cname(), vm->get_pm()->get_cname());
51   });
52
53   e.run();
54   return 0;
55 }