Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6845b071302aeadb8738259b8e6ed3c2f40a316a
[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   void update();
41   void reset();
42
43 private:
44   simgrid::s4u::Host* host = nullptr;
45   double last_updated      = 0;
46   double last_reset        = 0;
47   double current_speed     = 0;
48   double current_flops     = 0;
49   double computed_flops    = 0;
50   double idle_time         = 0;
51   long   theor_max_flops   = 0;
52   bool   was_prev_idle     = true; /* A host is idle at the beginning */
53 };
54
55 simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> HostLoad::EXTENSION_ID;
56
57 HostLoad::HostLoad(simgrid::s4u::Host* ptr)
58     : host(ptr)
59     , last_updated(surf_get_clock())
60     , last_reset(surf_get_clock())
61     , current_speed(host->getSpeed())
62     , current_flops(host->pimpl_cpu->constraint()->get_usage())
63     , theor_max_flops(0)
64     , was_prev_idle(current_flops == 0)
65 {
66 }
67
68 HostLoad::~HostLoad() = default;
69
70 void HostLoad::update()
71 {
72   double now = surf_get_clock();
73   if (last_updated < now) {
74     /* Current flop per second computed by the cpu; current_flops = k * pstate_speed_in_flops, k \in {0, 1, ..., cores}
75      * number of active cores */
76     current_flops = host->pimpl_cpu->constraint()->get_usage();
77
78     /* flops == pstate_speed * cores_being_currently_used */
79     computed_flops += (now - last_updated) * current_flops;
80     last_updated = now;
81   }
82 }
83
84 double HostLoad::getCurrentLoad()
85 {
86   return current_flops / static_cast<double>(host->getSpeed() * host->getCoreCount());
87 }
88
89 double HostLoad::getAverageLoad()
90 {
91   return getComputedFlops() / (host->getSpeed() * host->getCoreCount() * (surf_get_clock() - last_reset));
92 }
93
94 double HostLoad::getComputedFlops()
95 {
96   update();
97
98   return computed_flops;
99 }
100
101 /*
102  * Resets the counters
103  */
104 void HostLoad::reset()
105 {
106   last_updated   = surf_get_clock();
107   last_reset     = surf_get_clock();
108   computed_flops = 0;
109 }
110 }
111 }
112
113 using simgrid::plugin::HostLoad;
114
115 /* **************************** events  callback *************************** */
116 /* This callback is fired either when the host changes its state (on/off) or its speed
117  * (because the user changed the pstate, or because of external trace events) */
118 static void onHostChange(simgrid::s4u::Host& host)
119 {
120   if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
121     return;
122
123   host.extension<HostLoad>()->update();
124 }
125
126 /* This callback is called when an action (computation, idle, ...) terminates */
127 static void onActionStateChange(simgrid::surf::CpuAction* action, simgrid::surf::Action::State /*previous*/)
128 {
129   for (simgrid::surf::Cpu* const& cpu : action->cpus()) {
130     simgrid::s4u::Host* host = cpu->getHost();
131
132     if (dynamic_cast<simgrid::s4u::VirtualMachine*>(host)) // Ignore virtual machines
133       return;
134
135     if (host != nullptr) {
136       host->extension<HostLoad>()->update();
137     }
138   }
139 }
140
141 /* **************************** Public interface *************************** */
142 extern "C" {
143
144 /** \ingroup plugin_load
145  * \brief Initializes the HostLoad plugin
146  * \details The HostLoad plugin provides an API to get the current load of each host.
147  */
148 void sg_host_load_plugin_init()
149 {
150   if (HostLoad::EXTENSION_ID.valid())
151     return;
152
153   HostLoad::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostLoad>();
154
155   /* When attaching a callback into a signal, you can use a lambda as follows, or a regular function as done below */
156
157   simgrid::s4u::Host::onCreation.connect([](simgrid::s4u::Host& host) {
158     if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
159       return;
160     host.extension_set(new HostLoad(&host));
161   });
162
163   simgrid::surf::CpuAction::onStateChange.connect(&onActionStateChange);
164   simgrid::s4u::Host::onStateChange.connect(&onHostChange);
165   simgrid::s4u::Host::onSpeedChange.connect(&onHostChange);
166 }
167
168 /** @brief Returns the current load of the host passed as argument
169  *
170  *  See also @ref plugin_load
171  */
172 double sg_host_get_current_load(sg_host_t host)
173 {
174   xbt_assert(HostLoad::EXTENSION_ID.valid(),
175              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
176
177   return host->extension<HostLoad>()->getCurrentLoad();
178 }
179
180 double sg_host_get_computed_flops(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>()->getComputedFlops();
186 }
187
188 void sg_host_load_reset(sg_host_t host)
189 {
190   xbt_assert(HostLoad::EXTENSION_ID.valid(),
191              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
192
193   host->extension<HostLoad>()->reset();
194 }
195 }