Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / examples / cpp / energy-boot / s4u-energy-boot.cpp
1 /* Copyright (c) 2007-2023. 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 namespace sg4 = simgrid::s4u;
38
39 static void simulate_bootup(sg4::Host* host)
40 {
41   unsigned long previous_pstate = host->get_pstate();
42
43   XBT_INFO("Switch to virtual pstate 3, that encodes the 'booting up' state in that platform");
44   host->set_pstate(3);
45
46   XBT_INFO("Actually start the host");
47   host->turn_on();
48
49   XBT_INFO("Wait 150s to simulate the boot time.");
50   sg4::this_actor::sleep_for(150);
51
52   XBT_INFO("The host is now up and running. Switch back to previous pstate %lu", previous_pstate);
53   host->set_pstate(previous_pstate);
54 }
55
56 static void simulate_shutdown(sg4::Host* host)
57 {
58   unsigned long previous_pstate = host->get_pstate();
59
60   XBT_INFO("Switch to virtual pstate 4, that encodes the 'shutting down' state in that platform");
61   host->set_pstate(4);
62
63   XBT_INFO("Wait 7 seconds to simulate the shutdown time.");
64   sg4::this_actor::sleep_for(7);
65
66   XBT_INFO("Switch back to previous pstate %lu, that will be used on reboot.", previous_pstate);
67   host->set_pstate(previous_pstate);
68
69   XBT_INFO("Actually shutdown the host");
70   host->turn_off();
71 }
72
73 static void monitor()
74 {
75   sg4::Host* host1 = sg4::Host::by_name("MyHost1");
76
77   XBT_INFO("Initial pstate: %lu; Energy dissipated so far:%.0E J", host1->get_pstate(),
78            sg_host_get_consumed_energy(host1));
79
80   XBT_INFO("Sleep for 10 seconds");
81   sg4::this_actor::sleep_for(10);
82   XBT_INFO("Done sleeping. Current pstate: %lu; Energy dissipated so far: %.2f J", host1->get_pstate(),
83            sg_host_get_consumed_energy(host1));
84
85   simulate_shutdown(host1);
86   XBT_INFO("Host1 is now OFF. Current pstate: %lu; Energy dissipated so far: %.2f J", host1->get_pstate(),
87            sg_host_get_consumed_energy(host1));
88
89   XBT_INFO("Sleep for 10 seconds");
90   sg4::this_actor::sleep_for(10);
91   XBT_INFO("Done sleeping. Current pstate: %lu; Energy dissipated so far: %.2f J", host1->get_pstate(),
92            sg_host_get_consumed_energy(host1));
93
94   simulate_bootup(host1);
95   XBT_INFO("Host1 is now ON again. Current pstate: %lu; Energy dissipated so far: %.2f J", host1->get_pstate(),
96            sg_host_get_consumed_energy(host1));
97 }
98
99 int main(int argc, char* argv[])
100 {
101   sg_host_energy_plugin_init();
102   sg4::Engine e(&argc, argv);
103
104   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s platform.xml\n", argv[0], argv[0]);
105
106   e.load_platform(argv[1]);
107   sg4::Actor::create("Boot Monitor", e.host_by_name("MyHost2"), monitor);
108
109   e.run();
110
111   XBT_INFO("End of simulation.");
112
113   return 0;
114 }