Logo AND Algorithmique Numérique Distribuée

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