geopm  3.1.1.dev214+gba4f9f6d
GEOPM - Global Extensible Open Power Manager
PowerBalancerAgent.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 - 2024 Intel Corporation
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef POWERBALANCERAGENT_HPP_INCLUDE
7 #define POWERBALANCERAGENT_HPP_INCLUDE
8 
9 #include <vector>
10 #include <functional>
11 #include <memory>
12 
13 #include "geopm/Agent.hpp"
14 
15 namespace geopm
16 {
17  class PlatformIO;
18  class PlatformTopo;
19  class PowerBalancer;
20  class PowerGovernor;
21  class SampleAggregator;
22  class Waiter;
23 
24  class PowerBalancerAgent : public Agent
25  {
26  public:
27  enum m_policy_e {
67  };
68 
69  enum m_sample_e {
89  };
90 
98  };
99 
100  enum m_step_e {
121  };
122 
123  PowerBalancerAgent(PlatformIO &platform_io,
124  const PlatformTopo &platform_topo,
125  std::shared_ptr<SampleAggregator> sample_agg,
126  std::vector<std::shared_ptr<PowerBalancer> > power_balancer,
127  double min_power,
128  double max_power,
129  std::shared_ptr<Waiter> waiter);
131  virtual ~PowerBalancerAgent() = default;
132  void init(int level, const std::vector<int> &fan_in, bool is_level_root) override;
133  void validate_policy(std::vector<double> &policy) const override;
134  void split_policy(const std::vector<double> &in_policy,
135  std::vector<std::vector<double> > &out_policy) override;
136  bool do_send_policy(void) const override;
137  void aggregate_sample(const std::vector<std::vector<double> > &in_sample,
138  std::vector<double> &out_sample) override;
139  bool do_send_sample(void) const override;
140  void adjust_platform(const std::vector<double> &in_policy) override;
141  bool do_write_batch(void) const override;
142  void sample_platform(std::vector<double> &out_sample) override;
143  void wait(void) override;
144  std::vector<std::pair<std::string, std::string> > report_header(void) const override;
145  std::vector<std::pair<std::string, std::string> > report_host(void) const override;
146  std::map<uint64_t, std::vector<std::pair<std::string, std::string> > > report_region(void) const override;
147  std::vector<std::string> trace_names(void) const override;
148  std::vector<std::function<std::string(double)> > trace_formats(void) const override;
149  void trace_values(std::vector<double> &values) override;
150  void enforce_policy(const std::vector<double> &policy) const override;
151 
152  static std::string plugin_name(void);
153  static std::unique_ptr<Agent> make_plugin(void);
154  static std::vector<std::string> policy_names(void);
155  static std::vector<std::string> sample_names(void);
156  static std::string format_step_count(double step);
157  protected:
158  class Step;
159  class Role {
160  public:
163  virtual bool descend(const std::vector<double> &in_policy,
164  std::vector<std::vector<double> >&out_policy);
167  virtual bool ascend(const std::vector<std::vector<double> > &in_sample,
168  std::vector<double> &out_sample);
171  virtual bool adjust_platform(const std::vector<double> &in_policy);
174  virtual bool sample_platform(std::vector<double> &out_sample);
177  virtual void trace_values(std::vector<double> &values);
178  protected:
179  int step(size_t step_count) const;
180  int step(void) const;
181  const Step& step_imp();
182  Role(int num_node);
183  virtual ~Role() = default;
184  const std::vector<std::shared_ptr<const Step> > M_STEP_IMP;
185  public:
186  std::vector<double> m_policy;
187  protected:
189  public:
190  const int M_NUM_NODE;
191  };
192 
193  PlatformIO &m_platform_io;
194  const PlatformTopo &m_platform_topo;
195  std::shared_ptr<SampleAggregator> m_sample_agg;
196  std::shared_ptr<Role> m_role;
197  std::vector<std::shared_ptr<PowerBalancer> > m_power_balancer;
198  static constexpr double M_WAIT_SEC = 0.005;
199  double m_power_tdp;
205  const double M_TIME_WINDOW;
206  std::shared_ptr<Waiter> m_waiter;
207 
208  class RootRole;
209  class LeafRole;
210  class TreeRole;
211 
212  class Step {
213  public:
214  Step() = default;
215  virtual ~Step() = default;
216  virtual void update_policy(Role &role, const std::vector<double> &sample) const = 0;
217  virtual void enter_step(LeafRole &role, const std::vector<double> &in_policy) const = 0;
218  virtual void sample_platform(LeafRole &role) const = 0;
219  };
220 
221  class SendDownLimitStep : public Step {
222  public:
223  SendDownLimitStep() = default;
224  ~SendDownLimitStep() = default;
225  void update_policy(PowerBalancerAgent::Role &role, const std::vector<double> &sample) const;
226  void enter_step(PowerBalancerAgent::LeafRole &role, const std::vector<double> &in_policy) const;
228  };
229 
230  class MeasureRuntimeStep : public Step {
231  public:
232  MeasureRuntimeStep() = default;
233  ~MeasureRuntimeStep() = default;
234  void update_policy(PowerBalancerAgent::Role &role, const std::vector<double> &sample) const;
235  void enter_step(PowerBalancerAgent::LeafRole &role, const std::vector<double> &in_policy) const;
237  };
238 
239  class ReduceLimitStep : public Step {
240  public:
241  ReduceLimitStep() = default;
242  ~ReduceLimitStep() = default;
243  void update_policy(PowerBalancerAgent::Role &role, const std::vector<double> &sample) const;
244  void enter_step(PowerBalancerAgent::LeafRole &role, const std::vector<double> &in_policy) const;
246  };
247 
248  class TreeRole : public Role {
249  friend class SendDownLimitStep;
250  friend class MeasureRuntimeStep;
251  friend class ReduceLimitStep;
252  public:
253  TreeRole(int level, const std::vector<int> &fan_in);
254  virtual ~TreeRole() = default;
255  virtual bool descend(const std::vector<double> &in_policy,
256  std::vector<std::vector<double> >&out_policy) override;
257  virtual bool ascend(const std::vector<std::vector<double> > &in_sample,
258  std::vector<double> &out_sample) override;
259  protected:
260  const std::vector<std::function<double(const std::vector<double>&)> > M_AGG_FUNC;
261  const int M_NUM_CHILDREN;
263  };
264 
265  class RootRole : public TreeRole {
266  friend class SendDownLimitStep;
267  friend class MeasureRuntimeStep;
268  friend class ReduceLimitStep;
269  public:
270  RootRole(int level, const std::vector<int> &fan_in, double min_power, double max_power);
271  virtual ~RootRole() = default;
272  bool descend(const std::vector<double> &in_policy,
273  std::vector<std::vector<double> >&out_policy) override;
274  bool ascend(const std::vector<std::vector<double> > &in_sample,
275  std::vector<double> &out_sample) override;
276  private:
277  double m_root_cap;
278  const double M_MIN_PKG_POWER_SETTING;
279  const double M_MAX_PKG_POWER_SETTING;
280  };
281 
282  class LeafRole : public Role {
283  friend class SendDownLimitStep;
284  friend class MeasureRuntimeStep;
285  friend class ReduceLimitStep;
286  public:
287  LeafRole(PlatformIO &platform_io,
288  const PlatformTopo &platform_topo,
289  std::shared_ptr<SampleAggregator> sample_agg,
290  std::vector<std::shared_ptr<PowerBalancer> > power_balancer,
291  double min_power,
292  double max_power,
293  double time_window,
294  bool is_single_node,
295  int num_node);
296  virtual ~LeafRole() = default;
297  bool adjust_platform(const std::vector<double> &in_policy) override;
298  bool sample_platform(std::vector<double> &out_sample) override;
299  void trace_values(std::vector<double> &values) override;
300  private:
301  void init_platform_io(void);
302  void are_steps_complete(bool is_complete);
303  bool are_steps_complete(void);
304  PlatformIO &m_platform_io;
305  const PlatformTopo &m_platform_topo;
306  std::shared_ptr<SampleAggregator> m_sample_agg;
308  int m_num_domain;
309  std::vector<int> m_count_pio_idx;
310  std::vector<int> m_time_agg_idx;
311  std::vector<int> m_network_agg_idx;
312  std::vector<int> m_ignore_agg_idx;
313  std::vector<std::shared_ptr<PowerBalancer> > m_power_balancer;
314  const double M_STABILITY_FACTOR;
315  struct m_package_s {
316  int last_epoch_count;
317  double runtime;
318  double actual_limit;
319  double power_slack;
320  double power_headroom;
321  bool is_out_of_bounds;
322  bool is_step_complete;
323  int pio_power_idx;
324  };
325  std::vector<m_package_s> m_package;
326  const double M_MIN_PKG_POWER_SETTING;
327  const double M_MAX_PKG_POWER_SETTING;
328  bool m_is_single_node;
329  bool m_is_first_policy;
330  };
331  };
332 }
333 
334 #endif
Definition: Agent.hpp:20
Definition: PowerBalancerAgent.hpp:282
LeafRole(PlatformIO &platform_io, const PlatformTopo &platform_topo, std::shared_ptr< SampleAggregator > sample_agg, std::vector< std::shared_ptr< PowerBalancer > > power_balancer, double min_power, double max_power, double time_window, bool is_single_node, int num_node)
Definition: PowerBalancerAgent.cpp:115
bool sample_platform(std::vector< double > &out_sample) override
Definition: PowerBalancerAgent.cpp:251
bool adjust_platform(const std::vector< double > &in_policy) override
Definition: PowerBalancerAgent.cpp:188
void trace_values(std::vector< double > &values) override
Definition: PowerBalancerAgent.cpp:283
Definition: PowerBalancerAgent.hpp:230
void update_policy(PowerBalancerAgent::Role &role, const std::vector< double > &sample) const
Definition: PowerBalancerAgent.cpp:504
void sample_platform(PowerBalancerAgent::LeafRole &role) const
Definition: PowerBalancerAgent.cpp:513
void enter_step(PowerBalancerAgent::LeafRole &role, const std::vector< double > &in_policy) const
Definition: PowerBalancerAgent.cpp:509
Definition: PowerBalancerAgent.hpp:239
void sample_platform(PowerBalancerAgent::LeafRole &role) const
Definition: PowerBalancerAgent.cpp:553
void enter_step(PowerBalancerAgent::LeafRole &role, const std::vector< double > &in_policy) const
Definition: PowerBalancerAgent.cpp:545
void update_policy(PowerBalancerAgent::Role &role, const std::vector< double > &sample) const
Definition: PowerBalancerAgent.cpp:538
Definition: PowerBalancerAgent.hpp:159
const std::vector< std::shared_ptr< const Step > > M_STEP_IMP
Definition: PowerBalancerAgent.hpp:184
int m_step_count
Definition: PowerBalancerAgent.hpp:188
int step(void) const
Definition: PowerBalancerAgent.cpp:105
virtual void trace_values(std::vector< double > &values)
Definition: PowerBalancerAgent.cpp:92
virtual bool ascend(const std::vector< std::vector< double > > &in_sample, std::vector< double > &out_sample)
Definition: PowerBalancerAgent.cpp:64
Role(int num_node)
Definition: PowerBalancerAgent.cpp:36
const int M_NUM_NODE
Definition: PowerBalancerAgent.hpp:190
virtual bool descend(const std::vector< double > &in_policy, std::vector< std::vector< double > > &out_policy)
Definition: PowerBalancerAgent.cpp:54
std::vector< double > m_policy
Definition: PowerBalancerAgent.hpp:186
virtual bool sample_platform(std::vector< double > &out_sample)
Definition: PowerBalancerAgent.cpp:83
virtual bool adjust_platform(const std::vector< double > &in_policy)
Definition: PowerBalancerAgent.cpp:74
const Step & step_imp()
Definition: PowerBalancerAgent.cpp:110
Definition: PowerBalancerAgent.hpp:265
RootRole(int level, const std::vector< int > &fan_in, double min_power, double max_power)
Definition: PowerBalancerAgent.cpp:390
bool ascend(const std::vector< std::vector< double > > &in_sample, std::vector< double > &out_sample) override
Definition: PowerBalancerAgent.cpp:401
bool descend(const std::vector< double > &in_policy, std::vector< std::vector< double > > &out_policy) override
Definition: PowerBalancerAgent.cpp:417
Definition: PowerBalancerAgent.hpp:221
void sample_platform(PowerBalancerAgent::LeafRole &role) const
Definition: PowerBalancerAgent.cpp:500
void update_policy(PowerBalancerAgent::Role &role, const std::vector< double > &sample) const
Definition: PowerBalancerAgent.cpp:458
void enter_step(PowerBalancerAgent::LeafRole &role, const std::vector< double > &in_policy) const
Definition: PowerBalancerAgent.cpp:463
Definition: PowerBalancerAgent.hpp:212
virtual void enter_step(LeafRole &role, const std::vector< double > &in_policy) const =0
virtual void sample_platform(LeafRole &role) const =0
virtual void update_policy(Role &role, const std::vector< double > &sample) const =0
Definition: PowerBalancerAgent.hpp:248
bool m_is_step_complete
Definition: PowerBalancerAgent.hpp:262
virtual bool ascend(const std::vector< std::vector< double > > &in_sample, std::vector< double > &out_sample) override
Definition: PowerBalancerAgent.cpp:363
TreeRole(int level, const std::vector< int > &fan_in)
Definition: PowerBalancerAgent.cpp:310
const std::vector< std::function< double(const std::vector< double > &)> > M_AGG_FUNC
Definition: PowerBalancerAgent.hpp:260
const int M_NUM_CHILDREN
Definition: PowerBalancerAgent.hpp:261
virtual bool descend(const std::vector< double > &in_policy, std::vector< std::vector< double > > &out_policy) override
Definition: PowerBalancerAgent.cpp:329
Definition: PowerBalancerAgent.hpp:25
m_sample_e
Definition: PowerBalancerAgent.hpp:69
@ M_SAMPLE_STEP_COUNT
The the step counter that is currently in execution. Note that the step is equal to the step counter ...
Definition: PowerBalancerAgent.hpp:76
@ M_NUM_SAMPLE
Number of elements in a sample vector.
Definition: PowerBalancerAgent.hpp:88
@ M_SAMPLE_MAX_EPOCH_RUNTIME
Maximum expected runtime for any node below.
Definition: PowerBalancerAgent.hpp:79
@ M_SAMPLE_SUM_POWER_SLACK
The sum of all slack power available from children below the agent.
Definition: PowerBalancerAgent.hpp:82
@ M_SAMPLE_MIN_POWER_HEADROOM
Smallest difference between maximum power limit and current power limit for any node below.
Definition: PowerBalancerAgent.hpp:86
std::vector< std::function< std::string(double)> > trace_formats(void) const override
Returns format string for each column added to the trace.
Definition: PowerBalancerAgent.cpp:723
static std::unique_ptr< Agent > make_plugin(void)
Definition: PowerBalancerAgent.cpp:752
void sample_platform(std::vector< double > &out_sample) override
Read signals from the platform and interpret/aggregate these signals to create a sample which can be ...
Definition: PowerBalancerAgent.cpp:688
void aggregate_sample(const std::vector< std::vector< double > > &in_sample, std::vector< double > &out_sample) override
Aggregate samples from children for the next level up the tree.
Definition: PowerBalancerAgent.cpp:661
std::vector< std::pair< std::string, std::string > > report_header(void) const override
Custom fields that will be added to the report header when this agent is used.
Definition: PowerBalancerAgent.cpp:698
void validate_policy(std::vector< double > &policy) const override
Called by Controller to validate incoming policy values and configure defaults requested in incoming ...
Definition: PowerBalancerAgent.cpp:795
std::vector< std::string > trace_names(void) const override
Column headers to be added to the trace.
Definition: PowerBalancerAgent.cpp:713
void adjust_platform(const std::vector< double > &in_policy) override
Adjust the platform settings based the policy from above.
Definition: PowerBalancerAgent.cpp:672
virtual ~PowerBalancerAgent()=default
static std::vector< std::string > policy_names(void)
Definition: PowerBalancerAgent.cpp:757
m_policy_e
Definition: PowerBalancerAgent.hpp:27
@ M_POLICY_POWER_SLACK
This value is updated in step M_STEP_ADJUST_LIMIT to the amount that each leaf agent should increase ...
Definition: PowerBalancerAgent.hpp:63
@ M_POLICY_CPU_POWER_LIMIT
The power cap enforced on average over all nodes running the application. This has value 0....
Definition: PowerBalancerAgent.hpp:38
@ M_POLICY_STEP_COUNT
Step that the root is providing a policy for. The parent has received a sample matching this step in ...
Definition: PowerBalancerAgent.hpp:48
@ M_NUM_POLICY
Number of steps in each iteration of the balancing algorithm.
Definition: PowerBalancerAgent.hpp:66
@ M_POLICY_MAX_EPOCH_RUNTIME
The largest runtime reported by any leaf agent since the last redistribution of power....
Definition: PowerBalancerAgent.hpp:54
bool m_do_send_policy
Definition: PowerBalancerAgent.hpp:201
std::vector< std::shared_ptr< PowerBalancer > > m_power_balancer
Definition: PowerBalancerAgent.hpp:197
const double M_MAX_PKG_POWER_SETTING
Definition: PowerBalancerAgent.hpp:204
static std::string plugin_name(void)
Definition: PowerBalancerAgent.cpp:747
std::shared_ptr< Role > m_role
Definition: PowerBalancerAgent.hpp:196
static std::vector< std::string > sample_names(void)
Definition: PowerBalancerAgent.cpp:765
PowerBalancerAgent()
Definition: PowerBalancerAgent.cpp:581
bool do_write_batch(void) const override
Called by the Controller to decide whether to call write_batch() to update platform controls.
Definition: PowerBalancerAgent.cpp:683
void wait(void) override
Called by Controller to wait for sample period to elapse. This controls the cadence of the Controller...
Definition: PowerBalancerAgent.cpp:693
void enforce_policy(const std::vector< double > &policy) const override
Enforce the policy one time with PlatformIO::write_control(). Called to enforce static policies in th...
Definition: PowerBalancerAgent.cpp:738
m_step_e
Definition: PowerBalancerAgent.hpp:100
@ M_STEP_SEND_DOWN_LIMIT
On first iteration send down resource manager average limit requested, otherwise send down average ex...
Definition: PowerBalancerAgent.hpp:104
@ M_STEP_REDUCE_LIMIT
Decrease power limit on all nodes (other than the slowest) until epoch runtime matches the slowest....
Definition: PowerBalancerAgent.hpp:118
@ M_STEP_MEASURE_RUNTIME
Measure epoch runtime several times and apply median filter. Aggregate epoch runtime up tree by apply...
Definition: PowerBalancerAgent.hpp:111
@ M_NUM_STEP
Number of steps in process.
Definition: PowerBalancerAgent.hpp:120
std::shared_ptr< SampleAggregator > m_sample_agg
Definition: PowerBalancerAgent.hpp:195
void trace_values(std::vector< double > &values) override
Called by Controller to get latest values to be added to the trace.
Definition: PowerBalancerAgent.cpp:733
void init(int level, const std::vector< int > &fan_in, bool is_level_root) override
Set the level where this Agent is active and push signals/controls for that level.
Definition: PowerBalancerAgent.cpp:617
m_trace_sample_e
Definition: PowerBalancerAgent.hpp:91
@ M_TRACE_SAMPLE_POLICY_MAX_EPOCH_RUNTIME
Definition: PowerBalancerAgent.hpp:94
@ M_TRACE_SAMPLE_ENFORCED_POWER_LIMIT
Definition: PowerBalancerAgent.hpp:96
@ M_TRACE_SAMPLE_POLICY_STEP_COUNT
Definition: PowerBalancerAgent.hpp:93
@ M_TRACE_SAMPLE_POLICY_POWER_SLACK
Definition: PowerBalancerAgent.hpp:95
@ M_TRACE_NUM_SAMPLE
Definition: PowerBalancerAgent.hpp:97
@ M_TRACE_SAMPLE_POLICY_CPU_POWER_LIMIT
Definition: PowerBalancerAgent.hpp:92
bool do_send_policy(void) const override
Called by Controller to determine if new policy values should be sent down the tree to the Agent's ch...
Definition: PowerBalancerAgent.cpp:656
PlatformIO & m_platform_io
Definition: PowerBalancerAgent.hpp:193
std::map< uint64_t, std::vector< std::pair< std::string, std::string > > > report_region(void) const override
Custom fields for each region in the report.
Definition: PowerBalancerAgent.cpp:708
void split_policy(const std::vector< double > &in_policy, std::vector< std::vector< double > > &out_policy) override
Called by Controller to split policy for children at next level down the tree.
Definition: PowerBalancerAgent.cpp:644
bool m_do_write_batch
Definition: PowerBalancerAgent.hpp:202
const double M_MIN_PKG_POWER_SETTING
Definition: PowerBalancerAgent.hpp:203
std::vector< std::pair< std::string, std::string > > report_host(void) const override
Custom fields for the host section of the report.
Definition: PowerBalancerAgent.cpp:703
static std::string format_step_count(double step)
Definition: PowerBalancerAgent.cpp:773
bool do_send_sample(void) const override
Definition: PowerBalancerAgent.cpp:667
bool m_do_send_sample
Definition: PowerBalancerAgent.hpp:200
static constexpr double M_WAIT_SEC
Definition: PowerBalancerAgent.hpp:198
std::shared_ptr< Waiter > m_waiter
Definition: PowerBalancerAgent.hpp:206
const double M_TIME_WINDOW
Definition: PowerBalancerAgent.hpp:205
double m_power_tdp
Definition: PowerBalancerAgent.hpp:199
const PlatformTopo & m_platform_topo
Definition: PowerBalancerAgent.hpp:194
Definition: Accumulator.cpp:12