geopm  3.1.1.dev272+gdfb40a8d
GEOPM - Global Extensible Open Power Manager
MSRIOGroup.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 MSRIOGROUP_HPP_INCLUDE
7 #define MSRIOGROUP_HPP_INCLUDE
8 
9 #include <functional>
10 #include <map>
11 #include <memory>
12 #include <set>
13 #include <string>
14 #include <vector>
15 
16 #include "geopm/json11.hpp"
17 
18 #include "geopm/Cpuid.hpp"
19 #include "geopm_time.h"
20 #include "geopm_access.h"
21 
22 #include "geopm/IOGroup.hpp"
23 
24 namespace geopm
25 {
26  class MSRIO;
27  class PlatformTopo;
28  class PlatformIO;
29  class Signal;
30  class Control;
31  class SaveControl;
32 
34  class MSRIOGroup : public IOGroup
35  {
36  public:
37  enum m_cpuid_e {
38  M_CPUID_SNB = 0x62D,
39  M_CPUID_IVT = 0x63E,
40  M_CPUID_HSX = 0x63F,
41  M_CPUID_BDX = 0x64F,
42  M_CPUID_KNL = 0x657,
43  M_CPUID_SKX = 0x655,
44  M_CPUID_ICX = 0x66A,
45  M_CPUID_SPR = 0x68F,
46  };
47 
48  MSRIOGroup() = delete;
49  MSRIOGroup(bool use_msr_safe);
51  std::shared_ptr<MSRIO> msrio,
52  std::shared_ptr<Cpuid> cpuid,
53  std::shared_ptr<SaveControl> save_control);
54  virtual ~MSRIOGroup() = default;
55  std::set<std::string> signal_names(void) const override;
56  std::set<std::string> control_names(void) const override;
57  bool is_valid_signal(const std::string &signal_name) const override;
58  bool is_valid_control(const std::string &control_name) const override;
59  int signal_domain_type(const std::string &signal_name) const override;
60  int control_domain_type(const std::string &control_name) const override;
61  int push_signal(const std::string &signal_name,
62  int domain_type,
63  int domain_idx) override;
64  int push_control(const std::string &control_name,
65  int domain_type,
66  int domain_idx) override;
67  void read_batch(void) override;
68  void write_batch(void) override;
69  double sample(int sample_idx) override;
70  void adjust(int control_idx,
71  double setting) override;
72  double read_signal(const std::string &signal_name,
73  int domain_type,
74  int domain_idx) override;
75  void write_control(const std::string &control_name,
76  int domain_type,
77  int domain_idx,
78  double setting) override;
79  void save_control(void) override;
80  void restore_control(void) override;
81  std::function<double(const std::vector<double> &)> agg_function(const std::string &signal_name) const override;
82  std::function<std::string(double)> format_function(const std::string &signal_name) const override;
83  std::string signal_description(const std::string &signal_name) const override;
84  std::string control_description(const std::string &control_name) const override;
85  int signal_behavior(const std::string &signal_name) const override;
86  void save_control(const std::string &save_path) override;
87  void restore_control(const std::string &save_path) override;
88  std::string name(void) const override;
89 
92  void parse_json_msrs(const std::string &str);
99  static std::string msr_allowlist(int cpuid);
100  static std::string msr_allowlist(int cpuid,
101  const PlatformTopo &topo,
102  std::shared_ptr<MSRIO> msrio);
103 
105  static int cpuid(void);
106  static std::string plugin_name(void);
107  static std::unique_ptr<IOGroup> make_plugin(void);
108  static std::unique_ptr<IOGroup> make_plugin_safe(void);
109  private:
112  static void parse_json_msrs_allowlist(const std::string &str,
113  std::map<uint64_t, std::pair<uint64_t, std::string> > &allowlist_data,
114  const PlatformTopo &topo,
115  std::shared_ptr<MSRIO> msrio);
122  static std::string format_allowlist(const std::map<uint64_t, std::pair<uint64_t, std::string> > &allowlist_data);
123 
126  static std::string platform_data(int cpu_id);
127 
128  enum MsrConfigWarningPreference_e {
129  SILENCE_CONFIG_DEPRECATION_WARNING,
130  EMIT_CONFIG_DEPRECATION_WARNING,
131  };
137  static std::set<std::string> msr_data_files(
138  MsrConfigWarningPreference_e warning_preference = SILENCE_CONFIG_DEPRECATION_WARNING);
141  void set_signal_description(const std::string &name, const std::string &description);
144  void set_control_description(const std::string &name, const std::string &description);
146  void register_signal_alias(const std::string &signal_name, const std::string &msr_field_name);
148  void register_control_alias(const std::string &control_name, const std::string &msr_field_name);
151  void register_temperature_signals(void);
154  void register_power_signals(void);
157  void register_pcnt_scalability_signals(void);
160  void register_rdt_signals(void);
163  void register_frequency_signals(void);
166  void register_frequency_controls(void);
169  void check_control(const std::string &control_name);
170 
171  void check_control_pwrite(void);
172 
174  void check_control_lock(const std::string &lock_name, const std::string &error);
175 
177  static void check_top_level(const json11::Json &root);
178  static void check_msr_root(const json11::Json &msr_root,
179  const std::string &msr_name);
180  static void check_msr_field(const json11::Json &msr_field,
181  const std::string &msr_name,
182  const std::string &msr_field_name);
183  // Used to validate type and value of JSON objects
184  static bool json_check_null_func(const json11::Json &obj);
185  static bool json_check_is_hex_string(const json11::Json &obj);
186  static bool json_check_is_valid_offset(const json11::Json &obj);
187  static bool json_check_is_valid_domain(const json11::Json &domain);
188  static bool json_check_is_integer(const json11::Json &num);
189  static bool json_check_is_valid_aggregation(const json11::Json &obj);
190  // Add raw MSR as an available signal
191  void add_raw_msr_signal(const std::string &msr_name, int domain_type, uint64_t msr_offset);
192  // Add a bitfield of an MSR as an available signal
193  void add_msr_field_signal(const std::string &msr_name, const std::string &msr_field_name,
194  int domain_type, int begin_bit, int end_bit, int function,
195  double scalar, int units, const std::string &aggregation,
196  const std::string &description, int behavior,
197  const std::function<std::string(double)> &format_function);
198  // Add a bitfield of an MSR as an available control
199  void add_msr_field_control(const std::string &msr_field_name, int domain_type,
200  uint64_t msr_offset, int begin_bit, int end_bit,
201  int function, double scalar, int units,
202  const std::string &description);
203 
204  static const std::string M_DEFAULT_DESCRIPTION;
205  static const std::string M_PLUGIN_NAME;
206  static const std::string M_NAME_PREFIX;
207  const PlatformTopo &m_platform_topo;
208  std::shared_ptr<MSRIO> m_msrio;
209  int m_save_restore_ctx;
210  std::shared_ptr<Cpuid> m_cpuid;
211  bool m_is_active;
212  bool m_is_read;
213  std::vector<bool> m_is_adjusted;
214 
215  // time for derivative signals
216  std::shared_ptr<geopm_time_s> m_time_zero;
217  std::shared_ptr<double> m_time_batch;
218 
219  bool m_is_hwp_enabled;
220 
221  Cpuid::rdt_info_s m_rdt_info;
222 
223  uint32_t m_pmc_bit_width;
224 
225  int m_derivative_window;
226  double m_sleep_time;
227 
228  // All available signals: map from name to signal_info.
229  // The signals vector is over the indices for the domain.
230  // The signals pointers should be copied when signal is
231  // pushed and used directly for read_signal.
232  struct signal_info
233  {
234  std::vector<std::shared_ptr<Signal> > signals;
235  int domain;
236  int units;
237  std::function<double(const std::vector<double> &)> agg_function;
238  std::string description;
239  int behavior;
240  std::function<std::string(double)> format_function;
241  };
242  std::map<std::string, signal_info> m_signal_available;
243 
244  struct control_info
245  {
246  std::vector<std::shared_ptr<Control> > controls;
247  int domain;
248  int units;
249  std::string description;
250  };
251  std::map<std::string, control_info> m_control_available;
252 
253  // Mapping of signal index to pushed signals.
254  std::vector<std::shared_ptr<Signal> > m_signal_pushed;
255  // Mapping of control index to pushed controls
256  std::vector<std::shared_ptr<Control> > m_control_pushed;
257 
258  std::shared_ptr<SaveControl> m_mock_save_ctl;
259  };
260 }
261 
262 #endif
Definition: IOGroup.hpp:21
IOGroup that provides signals and controls based on MSRs.
Definition: MSRIOGroup.hpp:35
int control_domain_type(const std::string &control_name) const override
Query the domain for a named control.
Definition: MSRIOGroup.cpp:736
void write_batch(void) override
Write all of the pushed controls so that values previously given to adjust() are written to the platf...
Definition: MSRIOGroup.cpp:853
void adjust(int control_idx, double setting) override
Adjust a setting for a particular control that was previously pushed with push_control()....
Definition: MSRIOGroup.cpp:879
int signal_domain_type(const std::string &signal_name) const override
Query the domain for a named signal.
Definition: MSRIOGroup.cpp:726
std::set< std::string > signal_names(void) const override
Returns the names of all signals provided by the IOGroup.
Definition: MSRIOGroup.cpp:698
void write_control(const std::string &control_name, int domain_type, int domain_idx, double setting) override
Interpret the setting and write setting to the platform. Does not modify the values stored by calling...
Definition: MSRIOGroup.cpp:908
m_cpuid_e
Definition: MSRIOGroup.hpp:37
@ M_CPUID_BDX
Definition: MSRIOGroup.hpp:41
@ M_CPUID_SKX
Definition: MSRIOGroup.hpp:43
@ M_CPUID_HSX
Definition: MSRIOGroup.hpp:40
@ M_CPUID_SPR
Definition: MSRIOGroup.hpp:45
@ M_CPUID_IVT
Definition: MSRIOGroup.hpp:39
@ M_CPUID_KNL
Definition: MSRIOGroup.hpp:42
@ M_CPUID_SNB
Definition: MSRIOGroup.hpp:38
@ M_CPUID_ICX
Definition: MSRIOGroup.hpp:44
static int cpuid(void)
DEPRECATED Get the cpuid of the current platform.
Definition: MSRIOGroup.cpp:956
std::string control_description(const std::string &control_name) const override
Returns a description of the control. This string can be used by tools to generate help text for user...
Definition: MSRIOGroup.cpp:1123
static std::string msr_allowlist(int cpuid)
Fill string with the msr-safe allowlist file contents reflecting all known MSRs for the specified pla...
Definition: MSRIOGroup.cpp:1810
static std::unique_ptr< IOGroup > make_plugin(void)
Definition: MSRIOGroup.cpp:1041
int signal_behavior(const std::string &signal_name) const override
Returns a hint about how a signal will change as a function of time.
Definition: MSRIOGroup.cpp:1147
static std::string plugin_name(void)
Definition: MSRIOGroup.cpp:1031
std::function< double(const std::vector< double > &)> agg_function(const std::string &signal_name) const override
Return a function that should be used when aggregating the given signal.
Definition: MSRIOGroup.cpp:1066
void parse_json_msrs(const std::string &str)
Parse a JSON string and add any raw MSRs and fields as available signals and controls.
Definition: MSRIOGroup.cpp:1595
bool is_valid_control(const std::string &control_name) const override
Test if control_name refers to a control supported by the group.
Definition: MSRIOGroup.cpp:721
void restore_control(void) override
Restore all controls to values recorded in previous call to the save() method.
Definition: MSRIOGroup.cpp:946
double sample(int sample_idx) override
Retrieve signal value from data read by last call to read_batch() for a particular signal previously ...
Definition: MSRIOGroup.cpp:865
int push_control(const std::string &control_name, int domain_type, int domain_idx) override
Add a control to the list of controls that is written by write_batch() and configured with adjust().
Definition: MSRIOGroup.cpp:790
virtual ~MSRIOGroup()=default
std::set< std::string > control_names(void) const override
Returns the names of all controls provided by the IOGroup.
Definition: MSRIOGroup.cpp:707
std::string name(void) const override
Get the IOGroup name.
Definition: MSRIOGroup.cpp:1036
int push_signal(const std::string &signal_name, int domain_type, int domain_idx) override
Add a signal to the list of signals that is read by read_batch() and sampled by sample().
Definition: MSRIOGroup.cpp:746
void save_control(void) override
Save the state of all controls so that any subsequent changes made through the IOGroup can be undone ...
Definition: MSRIOGroup.cpp:936
std::string signal_description(const std::string &signal_name) const override
Returns a description of the signal. This string can be used by tools to generate help text for users...
Definition: MSRIOGroup.cpp:1098
double read_signal(const std::string &signal_name, int domain_type, int domain_idx) override
Read from platform and interpret into SI units a signal given its name and domain....
Definition: MSRIOGroup.cpp:889
std::function< std::string(double)> format_function(const std::string &signal_name) const override
Returns a function that can be used to convert a signal of the given name into a printable string....
Definition: MSRIOGroup.cpp:1076
bool is_valid_signal(const std::string &signal_name) const override
Test if signal_name refers to a signal supported by the group.
Definition: MSRIOGroup.cpp:716
void read_batch(void) override
Read all pushed signals from the platform so that the next call to sample() will reflect the updated ...
Definition: MSRIOGroup.cpp:841
static std::unique_ptr< IOGroup > make_plugin_safe(void)
Definition: MSRIOGroup.cpp:1046
Definition: PlatformTopo.hpp:28
Definition: Agg.cpp:20
const PlatformTopo & platform_topo(void)
Definition: PlatformTopo.cpp:81
Definition: Cpuid.hpp:18