Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into partial_shared_malloc
[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   double getAverageLoad();
39   void update();
40   void reset();
41
42 private:
43   simgrid::s4u::Host* host = nullptr;
44   double last_updated      = 0;
45   double last_reset        = 0;
46   double current_flops     = 0;
47   double computed_flops    = 0;
48 };
49
50 simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> HostLoad::EXTENSION_ID;
51
52 HostLoad::HostLoad(simgrid::s4u::Host* ptr)
53     : host(ptr)
54     , last_updated(surf_get_clock())
55     , last_reset(surf_get_clock())
56     , current_flops(lmm_constraint_get_usage(host->pimpl_cpu->constraint()))
57 {
58 }
59
60 HostLoad::~HostLoad() = default;
61
62 void HostLoad::update()
63 {
64   double now = surf_get_clock();
65   if (last_updated < now) {
66     /* Current flop per second computed by the cpu; current_flops = k * pstate_speed_in_flops, k \in {0, 1, ..., cores}
67      * number of active cores */
68     current_flops = lmm_constraint_get_usage(host->pimpl_cpu->constraint());
69
70     /* flops == pstate_speed * cores_being_currently_used */
71     computed_flops += (now - last_updated) * current_flops;
72     last_updated = now;
73   }
74 }
75
76 double HostLoad::getCurrentLoad()
77 {
78   return current_flops / (host->speed() * host->coreCount());
79 }
80
81 double HostLoad::getAverageLoad()
82 {
83   return getComputedFlops() / (host->speed() * host->coreCount() * (surf_get_clock() - last_reset));
84 }
85
86 double HostLoad::getComputedFlops()
87 {
88   update();
89
90   return computed_flops;
91 }
92
93 /*
94  * Resets the counters
95  */
96 void HostLoad::reset()
97 {
98   last_updated   = surf_get_clock();
99   last_reset     = surf_get_clock();
100   computed_flops = 0;
101 }
102 }
103 }
104
105 using simgrid::plugin::HostLoad;
106
107 /* **************************** events  callback *************************** */
108 static void on_host_added(simgrid::s4u::Host& host)
109 {
110   if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
111     return;
112   host.extension_set(new HostLoad(&host));
113 }
114
115 /* This callback is fired either when the host changes its state (on/off) or its speed
116  * (because the user changed the pstate, or because of external trace events) */
117 static void onHostChange(simgrid::s4u::Host& host)
118 {
119   if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
120     return;
121
122   HostLoad* host_load = host.extension<HostLoad>();
123   host_load->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* cpu : action->cpus()) {
130     simgrid::s4u::Host* host = cpu->getHost();
131
132     if (host != nullptr) {
133       // Get the host_load extension for the relevant host
134       HostLoad* host_load = host->extension<HostLoad>();
135       host_load->update();
136     }
137   }
138 }
139
140 /* **************************** Public interface *************************** */
141 SG_BEGIN_DECL()
142
143 /** \ingroup SURF_plugin_load
144  * \brief Initializes the HostLoad plugin
145  * \details The HostLoad plugin provides an API to get the current load of each host.
146  */
147 void sg_host_load_plugin_init()
148 {
149   if (HostLoad::EXTENSION_ID.valid())
150     return;
151
152   HostLoad::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostLoad>();
153
154   simgrid::s4u::Host::onCreation.connect(&on_host_added);
155   simgrid::surf::CpuAction::onStateChange.connect(&onActionStateChange);
156   simgrid::s4u::Host::onStateChange.connect(&onHostChange);
157   simgrid::s4u::Host::onSpeedChange.connect(&onHostChange);
158 }
159
160 /** @brief Returns the current load of the host passed as argument
161  *
162  *  See also @ref SURF_plugin_load
163  */
164 double sg_host_get_current_load(sg_host_t host)
165 {
166   xbt_assert(HostLoad::EXTENSION_ID.valid(),
167              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
168
169   return host->extension<HostLoad>()->getCurrentLoad();
170 }
171
172 double sg_host_get_computed_flops(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>()->getComputedFlops();
178 }
179
180 void sg_host_load_reset(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   host->extension<HostLoad>()->reset();
186 }
187
188 SG_END_DECL()