Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
snake_case some methods in s4u::Host
[simgrid.git] / examples / s4u / energy-boot / s4u-energy-boot.cpp
1 /* Copyright (c) 2007-2018. 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 /* This is an example of how the bootup and shutdown periods can be modeled
7  * with SimGrid, taking both the time and overall consumption into account.
8  *
9  * The main idea is to augment the platform description to declare fake
10  * pstate that represent these states. The CPU speed of these state is zero
11  * (the CPU delivers 0 flop per second when booting) while the energy
12  * consumption is the one measured on average on the modeled machine.
13  *
14  * When you want to bootup the machine, you set it into the pstate encoding
15  * the boot (3 in this example), and leave it so for the right time using a
16  * sleep_for(). During that time no other execution can progress since the
17  * resource speed is set at 0 flop/s in this fake pstate. Once this is over,
18  * the boot is done and we switch back to the regular pstate. Conversely,
19  * the fake pstate 4 is used to encode the shutdown delay.
20  *
21  * Some people don't like the idea to add fake pstates for the boot time, and
22  * would like SimGrid to provide a "cleaner" model for that. But the "right"
23  * model depends on the study you want to conduct. If you want to study the
24  * instantaneous consumption of a rebooting data center, the model used here
25  * is not enough since it considers only the average consumption over the boot,
26  * while the instantaneous consumption changes dramatically. Conversely, a
27  * model taking the instantaneous changes into account will be very difficult
28  * to instantiate correctly (which values will you use?), so it's not adapted
29  * to most studies. At least, fake pstates allow you to do exactly what you
30  * need for your very study.
31  */
32
33 #include "simgrid/s4u.hpp"
34 #include "simgrid/plugins/energy.h"
35
36 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this example");
37
38 static void simulate_bootup(simgrid::s4u::Host* host)
39 {
40   int previous_pstate = host->getPstate();
41
42   XBT_INFO("Switch to virtual pstate 3, that encodes the 'booting up' state in that platform");
43   host->setPstate(3);
44
45   XBT_INFO("Actually start the host");
46   host->turn_on();
47
48   XBT_INFO("Wait 150s to simulate the boot time.");
49   simgrid::s4u::this_actor::sleep_for(150);
50
51   XBT_INFO("The host is now up and running. Switch back to previous pstate %d", previous_pstate);
52   host->setPstate(previous_pstate);
53 }
54
55 static void simulate_shutdown(simgrid::s4u::Host* host)
56 {
57   int previous_pstate = host->getPstate();
58
59   XBT_INFO("Switch to virtual pstate 4, that encodes the 'shutting down' state in that platform");
60   host->setPstate(4);
61
62   XBT_INFO("Wait 7 seconds to simulate the shutdown time.");
63   simgrid::s4u::this_actor::sleep_for(7);
64
65   XBT_INFO("Switch back to previous pstate %d, that will be used on reboot.", previous_pstate);
66   host->setPstate(previous_pstate);
67
68   XBT_INFO("Actually shutdown the host");
69   host->turn_off();
70 }
71
72 static int monitor()
73 {
74   simgrid::s4u::Host* host1 = simgrid::s4u::Host::by_name("MyHost1");
75
76   XBT_INFO("Initial pstate: %d; Energy dissipated so far:%.0E J", host1->getPstate(),
77            sg_host_get_consumed_energy(host1));
78
79   XBT_INFO("Sleep for 10 seconds");
80   simgrid::s4u::this_actor::sleep_for(10);
81   XBT_INFO("Done sleeping. Current pstate: %d; Energy dissipated so far: %.2f J", host1->getPstate(),
82            sg_host_get_consumed_energy(host1));
83
84   simulate_shutdown(host1);
85   XBT_INFO("Host1 is now OFF. Current pstate: %d; Energy dissipated so far: %.2f J", host1->getPstate(),
86            sg_host_get_consumed_energy(host1));
87
88   XBT_INFO("Sleep for 10 seconds");
89   simgrid::s4u::this_actor::sleep_for(10);
90   XBT_INFO("Done sleeping. Current pstate: %d; Energy dissipated so far: %.2f J", host1->getPstate(),
91            sg_host_get_consumed_energy(host1));
92
93   simulate_bootup(host1);
94   XBT_INFO("Host1 is now ON again. Current pstate: %d; Energy dissipated so far: %.2f J", host1->getPstate(),
95            sg_host_get_consumed_energy(host1));
96
97   return 0;
98 }
99
100 int main(int argc, char* argv[])
101 {
102   sg_host_energy_plugin_init();
103   simgrid::s4u::Engine e(&argc, argv);
104
105   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s platform.xml\n", argv[0], argv[0]);
106
107   e.load_platform(argv[1]);
108   simgrid::s4u::Actor::create("Boot Monitor", simgrid::s4u::Host::by_name("MyHost2"), monitor);
109
110   e.run();
111
112   XBT_INFO("End of simulation.");
113
114   return 0;
115 }