Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert this example too
[simgrid.git] / examples / s4u / energy-vm / s4u-energy-vm.cpp
1 /* Copyright (c) 2007-2010, 2013-2015, 2017. 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/s4u.hpp"
8 #include "simgrid/plugins/energy.h"
9 #include "simgrid/s4u/VirtualMachine.hpp"
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(energy_vm, "Messages of this example");
12
13 static void executor()
14 {
15   simgrid::s4u::this_actor::execute(300E6);
16   XBT_INFO("This worker is done.");
17 }
18
19 static void dvfs()
20 {
21   simgrid::s4u::Host* host1 = simgrid::s4u::Host::by_name("MyHost1");
22   simgrid::s4u::Host* host2 = simgrid::s4u::Host::by_name("MyHost2");
23   simgrid::s4u::Host* host3 = simgrid::s4u::Host::by_name("MyHost3");
24
25   /* Host 1 */
26   XBT_INFO("Creating and starting two VMs");
27   simgrid::s4u::VirtualMachine* vm_host1 = new simgrid::s4u::VirtualMachine("vm_host1", host1, 1);
28   vm_host1->start();
29   simgrid::s4u::VirtualMachine* vm_host2 = new simgrid::s4u::VirtualMachine("vm_host2", host2, 1);
30   vm_host2->start();
31
32   XBT_INFO("Create two tasks on Host1: both inside a VM");
33   simgrid::s4u::Actor::createActor("p11", vm_host1, executor);
34   simgrid::s4u::Actor::createActor("p12", vm_host1, executor);
35
36   XBT_INFO("Create two tasks on Host2: one inside a VM, the other directly on the host");
37   simgrid::s4u::Actor::createActor("p21", vm_host2, executor);
38   simgrid::s4u::Actor::createActor("p22", host2, executor);
39
40   XBT_INFO("Create two tasks on Host3: both directly on the host");
41   simgrid::s4u::Actor::createActor("p31", host3, executor);
42   simgrid::s4u::Actor::createActor("p32", host3, executor);
43
44   XBT_INFO("Wait 5 seconds. The tasks are still running (they run for 3 seconds, but 2 tasks are co-located, "
45            "so they run for 6 seconds)");
46   simgrid::s4u::this_actor::sleep_for(5);
47   XBT_INFO("Wait another 5 seconds. The tasks stop at some point in between");
48   simgrid::s4u::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   simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
58
59   xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
60
61   e->loadPlatform(argv[1]);
62
63   simgrid::s4u::Actor::createActor("dvfs", simgrid::s4u::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            simgrid::s4u::Engine::getClock());
70
71   return 0;
72 }