Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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_flops     = 0;
48   double computed_flops    = 0;
49 };
50
51 simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> HostLoad::EXTENSION_ID;
52
53 HostLoad::HostLoad(simgrid::s4u::Host* ptr)
54     : host(ptr)
55     , last_updated(surf_get_clock())
56     , last_reset(surf_get_clock())
57     , current_flops(lmm_constraint_get_usage(host->pimpl_cpu->constraint()))
58 {
59 }
60
61 HostLoad::~HostLoad() = default;
62
63 void HostLoad::update()
64 {
65   double now = surf_get_clock();
66   if (last_updated < now) {
67     /* Current flop per second computed by the cpu; current_flops = k * pstate_speed_in_flops, k \in {0, 1, ..., cores}
68      * number of active cores */
69     current_flops = lmm_constraint_get_usage(host->pimpl_cpu->constraint());
70
71     /* flops == pstate_speed * cores_being_currently_used */
72     computed_flops += (now - last_updated) * current_flops;
73     last_updated = now;
74   }
75 }
76
77 double HostLoad::getCurrentLoad()
78 {
79   return current_flops / (host->getSpeed() * host->getCoreCount());
80 }
81
82 double HostLoad::getAverageLoad()
83 {
84   return getComputedFlops() / (host->getSpeed() * host->getCoreCount() * (surf_get_clock() - last_reset));
85 }
86
87 double HostLoad::getComputedFlops()
88 {
89   update();
90
91   return computed_flops;
92 }
93
94 /*
95  * Resets the counters
96  */
97 void HostLoad::reset()
98 {
99   last_updated   = surf_get_clock();
100   last_reset     = surf_get_clock();
101   computed_flops = 0;
102 }
103 }
104 }
105
106 using simgrid::plugin::HostLoad;
107
108 /* **************************** events  callback *************************** */
109 static void on_host_added(simgrid::s4u::Host& host)
110 {
111   if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
112     return;
113   host.extension_set(new HostLoad(&host));
114 }
115
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   HostLoad* host_load = host.extension<HostLoad>();
124   host_load->update();
125 }
126
127 /* This callback is called when an action (computation, idle, ...) terminates */
128 static void onActionStateChange(simgrid::surf::CpuAction* action, simgrid::surf::Action::State previous)
129 {
130   for (simgrid::surf::Cpu* const& cpu : action->cpus()) {
131     simgrid::s4u::Host* host = cpu->getHost();
132
133     if (host != nullptr) {
134       // Get the host_load extension for the relevant host
135       HostLoad* host_load = host->extension<HostLoad>();
136       host_load->update();
137     }
138   }
139 }
140
141 /* **************************** Public interface *************************** */
142 SG_BEGIN_DECL()
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   simgrid::s4u::Host::onCreation.connect(&on_host_added);
156   simgrid::surf::CpuAction::onStateChange.connect(&onActionStateChange);
157   simgrid::s4u::Host::onStateChange.connect(&onHostChange);
158   simgrid::s4u::Host::onSpeedChange.connect(&onHostChange);
159 }
160
161 /** @brief Returns the current load of the host passed as argument
162  *
163  *  See also @ref plugin_load
164  */
165 double sg_host_get_current_load(sg_host_t host)
166 {
167   xbt_assert(HostLoad::EXTENSION_ID.valid(),
168              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
169
170   return host->extension<HostLoad>()->getCurrentLoad();
171 }
172
173 double sg_host_get_computed_flops(sg_host_t host)
174 {
175   xbt_assert(HostLoad::EXTENSION_ID.valid(),
176              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
177
178   return host->extension<HostLoad>()->getComputedFlops();
179 }
180
181 void sg_host_load_reset(sg_host_t host)
182 {
183   xbt_assert(HostLoad::EXTENSION_ID.valid(),
184              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
185
186   host->extension<HostLoad>()->reset();
187 }
188
189 SG_END_DECL()