Logo AND Algorithmique Numérique Distribuée

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