Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f353661d25eff6d2e07f92906f1c8fc94effb829
[simgrid.git] / src / surf / plugins / host_load.cpp
1 /* Copyright (c) 2010-2017. 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/plugins/load.h"
7 #include "simgrid/simix.hpp"
8 #include "src/plugins/vm/VirtualMachineImpl.hpp"
9 #include "src/surf/cpu_interface.hpp"
10
11 #include "simgrid/s4u/Engine.hpp"
12
13 #include <boost/algorithm/string/classification.hpp>
14 #include <boost/algorithm/string/split.hpp>
15 #include <string>
16 #include <utility>
17 #include <vector>
18
19 /** @addtogroup plugin_load
20
21 This plugin makes it very simple for users to obtain the current load for each host.
22
23 */
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_plugin_load, surf, "Logging specific to the HostLoad plugin");
26
27 namespace simgrid {
28 namespace plugin {
29
30 class HostLoad {
31 public:
32   static simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> EXTENSION_ID;
33
34   explicit HostLoad(simgrid::s4u::Host* ptr);
35   ~HostLoad();
36
37   double getCurrentLoad();
38   double getComputedFlops();
39   double getAverageLoad();
40   double getIdleTime();
41   void update();
42   void reset();
43
44 private:
45   simgrid::s4u::Host* host = nullptr;
46   double last_updated      = 0;
47   double last_reset        = 0;
48   double current_speed     = 0;
49   double current_flops     = 0;
50   double computed_flops    = 0;
51   double idle_time         = 0;
52   long   theor_max_flops   = 0;
53   bool   was_prev_idle     = true; /* A host is idle at the beginning */
54 };
55
56 simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> HostLoad::EXTENSION_ID;
57
58 HostLoad::HostLoad(simgrid::s4u::Host* ptr)
59     : host(ptr)
60     , last_updated(surf_get_clock())
61     , last_reset(surf_get_clock())
62     , current_speed(host->getSpeed())
63     , current_flops(host->pimpl_cpu->constraint()->get_usage())
64     , theor_max_flops(0)
65     , was_prev_idle(current_flops == 0)
66 {
67 }
68
69 HostLoad::~HostLoad() = default;
70
71 void HostLoad::update()
72 {
73   double now = surf_get_clock();
74   if (last_updated < now) {
75     /* Current flop per second computed by the cpu; current_flops = k * pstate_speed_in_flops, k \in {0, 1, ..., cores}
76      * number of active cores */
77     current_flops = host->pimpl_cpu->constraint()->get_usage();
78
79     /* flops == pstate_speed * cores_being_currently_used */
80     computed_flops += (now - last_updated) * current_flops;
81     last_updated = now;
82   }
83 }
84
85 double HostLoad::getCurrentLoad()
86 {
87   return current_flops / static_cast<double>(host->getSpeed() * host->getCoreCount());
88 }
89
90 /**
91  * Return idle time since last reset
92  */
93 double HostLoad::getIdleTime() {
94   return idle_time;
95 }
96
97 double HostLoad::getAverageLoad()
98 {
99   return getComputedFlops() / (host->getSpeed() * host->getCoreCount() * (surf_get_clock() - last_reset));
100 }
101
102 double HostLoad::getComputedFlops()
103 {
104   update();
105
106   return computed_flops;
107 }
108
109 /*
110  * Resets the counters
111  */
112 void HostLoad::reset()
113 {
114   last_updated   = surf_get_clock();
115   last_reset     = surf_get_clock();
116   computed_flops = 0;
117 }
118 }
119 }
120
121 using simgrid::plugin::HostLoad;
122
123 /* **************************** events  callback *************************** */
124 /* This callback is fired either when the host changes its state (on/off) or its speed
125  * (because the user changed the pstate, or because of external trace events) */
126 static void onHostChange(simgrid::s4u::Host& host)
127 {
128   if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
129     return;
130
131   host.extension<HostLoad>()->update();
132 }
133
134 /* This callback is called when an action (computation, idle, ...) terminates */
135 static void onActionStateChange(simgrid::surf::CpuAction* action, simgrid::surf::Action::State /*previous*/)
136 {
137   for (simgrid::surf::Cpu* const& cpu : action->cpus()) {
138     simgrid::s4u::Host* host = cpu->getHost();
139
140     if (dynamic_cast<simgrid::s4u::VirtualMachine*>(host)) // Ignore virtual machines
141       return;
142
143     if (host != nullptr) {
144       host->extension<HostLoad>()->update();
145     }
146   }
147 }
148
149 /* **************************** Public interface *************************** */
150 extern "C" {
151
152 /** \ingroup plugin_load
153  * \brief Initializes the HostLoad plugin
154  * \details The HostLoad plugin provides an API to get the current load of each host.
155  */
156 void sg_host_load_plugin_init()
157 {
158   if (HostLoad::EXTENSION_ID.valid())
159     return;
160
161   HostLoad::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostLoad>();
162
163   /* When attaching a callback into a signal, you can use a lambda as follows, or a regular function as done below */
164
165   simgrid::s4u::Host::onCreation.connect([](simgrid::s4u::Host& host) {
166     if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
167       return;
168     host.extension_set(new HostLoad(&host));
169   });
170
171   simgrid::surf::CpuAction::onStateChange.connect(&onActionStateChange);
172   simgrid::s4u::Host::onStateChange.connect(&onHostChange);
173   simgrid::s4u::Host::onSpeedChange.connect(&onHostChange);
174 }
175
176 /** @brief Returns the current load of the host passed as argument
177  *
178  *  See also @ref plugin_load
179  */
180 double sg_host_get_current_load(sg_host_t host)
181 {
182   xbt_assert(HostLoad::EXTENSION_ID.valid(),
183              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
184
185   return host->extension<HostLoad>()->getCurrentLoad();
186 }
187
188 /** @brief Returns the time this host was idle since the last reset
189  *
190  *  See also @ref plugin_load
191  */
192 double sg_host_get_idle_time(sg_host_t host)
193 {
194   xbt_assert(HostLoad::EXTENSION_ID.valid(),
195              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
196
197   return host->extension<HostLoad>()->getIdleTime();
198 }
199
200 double sg_host_get_computed_flops(sg_host_t host)
201 {
202   xbt_assert(HostLoad::EXTENSION_ID.valid(),
203              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
204
205   return host->extension<HostLoad>()->getComputedFlops();
206 }
207
208 void sg_host_load_reset(sg_host_t host)
209 {
210   xbt_assert(HostLoad::EXTENSION_ID.valid(),
211              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
212
213   host->extension<HostLoad>()->reset();
214 }
215 }