Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e81f2e000212ef78be0636e3f3bd69dc6d464b12
[simgrid.git] / examples / cpp / energy-vm / s4u-energy-vm.cpp
1 /* Copyright (c) 2007-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/plugins/energy.h"
8 #include "simgrid/s4u/VirtualMachine.hpp"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(energy_vm, "Messages of this example");
11 namespace sg4 = simgrid::s4u;
12
13 static void executor()
14 {
15   sg4::this_actor::execute(300E6);
16   XBT_INFO("This worker is done.");
17 }
18
19 static void dvfs()
20 {
21   sg4::Host* host1 = sg4::Host::by_name("MyHost1");
22   sg4::Host* host2 = sg4::Host::by_name("MyHost2");
23   sg4::Host* host3 = sg4::Host::by_name("MyHost3");
24
25   /* Host 1 */
26   XBT_INFO("Creating and starting two VMs");
27   auto* vm_host1 = host1->create_vm("vm_host1", 1);
28   vm_host1->start();
29   auto* vm_host2 = host2->create_vm("vm_host2", 1);
30   vm_host2->start();
31
32   XBT_INFO("Create two activities on Host1: both inside a VM");
33   sg4::Actor::create("p11", vm_host1, executor);
34   sg4::Actor::create("p12", vm_host1, executor);
35
36   XBT_INFO("Create two activities on Host2: one inside a VM, the other directly on the host");
37   sg4::Actor::create("p21", vm_host2, executor);
38   sg4::Actor::create("p22", host2, executor);
39
40   XBT_INFO("Create two activities on Host3: both directly on the host");
41   sg4::Actor::create("p31", host3, executor);
42   sg4::Actor::create("p32", host3, executor);
43
44   XBT_INFO("Wait 5 seconds. The activities are still running (they run for 3 seconds, but 2 activities are co-located, "
45            "so they run for 6 seconds)");
46   sg4::this_actor::sleep_for(5);
47   XBT_INFO("Wait another 5 seconds. The activities stop at some point in between");
48   sg4::this_actor::sleep_for(5);
49
50   vm_host1->destroy();
51   vm_host2->destroy();
52 }
53
54 int main(int argc, char* argv[])
55 {
56   sg_host_energy_plugin_init();
57   sg4::Engine e(&argc, argv);
58
59   xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s ../platforms/energy_platform.xml\n", argv[0], argv[0]);
60
61   e.load_platform(argv[1]);
62
63   sg4::Actor::create("dvfs", e.host_by_name("MyHost1"), dvfs);
64
65   e.run();
66
67   XBT_INFO("Total simulation time: %.2f; Host2 and Host3 must have the exact same energy consumption; Host1 is "
68            "multi-core and will differ.",
69            sg4::Engine::get_clock());
70
71   return 0;
72 }