geopm 3.2.1.dev479+g613c3fa6a
GEOPM - Global Extensible Open Power Manager
Loading...
Searching...
No Matches
LevelZeroImp.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 - 2025 Intel Corporation
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef LEVELZEROIMP_HPP_INCLUDE
7#define LEVELZEROIMP_HPP_INCLUDE
8
9#include <string>
10#include <thread>
11#include <mutex>
12#include <atomic>
13#include <chrono>
14#include <exception>
15
16#include <level_zero/ze_api.h>
17#include <level_zero/zes_api.h>
18#include <level_zero/zet_api.h>
19
20#include "LevelZero.hpp"
21
22#include "geopm_time.h"
23
24namespace geopm
25{
26 class LevelZeroImp : public LevelZero
27 {
28 public:
30 virtual ~LevelZeroImp();
31 int num_gpu(void) const override;
32 int num_gpu(int domain) const override;
33 int frequency_domain_count(unsigned int l0_device_idx,
34 int domain) const override;
35 double frequency_status(unsigned int l0_device_idx,
36 int l0_domain, int l0_domain_idx) const override;
37 double frequency_efficient(unsigned int l0_device_idx,
38 int l0_domain, int l0_domain_idx) const override;
39 double frequency_min(unsigned int l0_device_idx, int l0_domain,
40 int l0_domain_idx) const override;
41 double frequency_max(unsigned int l0_device_idx, int l0_domain,
42 int l0_domain_idx) const override;
43 std::vector<double> frequency_supported(unsigned int l0_device_idx,
44 int l0_domain,
45 int l0_domain_idx) const override;
46 uint32_t frequency_throttle_reasons(unsigned int l0_device_idx, int l0_domain,
47 int l0_domain_idx) const override;
48 std::pair<double, double> frequency_range(unsigned int l0_device_idx,
49 int l0_domain,
50 int l0_domain_idx) const override;
51 int temperature_domain_count(unsigned int l0_device_idx,
52 int l0_domain) const override;
53 double temperature_max(unsigned int l0_device_idx, int l0_domain,
54 int l0_domain_idx) const override;
55 int engine_domain_count(unsigned int l0_device_idx, int domain) const override;
56 std::pair<uint64_t, uint64_t> active_time_pair(unsigned int l0_device_idx,
57 int l0_domain, int l0_domain_idx) const override;
58 uint64_t active_time(unsigned int l0_device_idx, int l0_domain,
59 int l0_domain_idx) const override;
60 uint64_t active_time_timestamp(unsigned int l0_device_idx,
61 int l0_domain, int l0_domain_idx) const override;
62 int power_domain_count(int geopm_domain, unsigned int l0_device_idx,
63 int l0_domain) const override;
64 std::pair<uint64_t, uint64_t> energy_pair(int geopm_domain, unsigned int l0_device_idx,
65 int l0_domain_idx) const override;
66 uint64_t energy(int geopm_domain, unsigned int l0_device_idx,
67 int l0_domain, int l0_domain_idx) const override;
68 uint64_t energy_timestamp(int geopm_domain,
69 unsigned int l0_device_idx,
70 int l0_domain,
71 int l0_domain_idx) const override;
72 int performance_domain_count(unsigned int l0_device_idx,
73 int l0_domain) const override;
74 double performance_factor(unsigned int l0_device_idx,
75 int l0_domain, int l0_domain_idx) const override;
76
77 int32_t power_limit_tdp(unsigned int l0_device_idx) const override;
78 int32_t power_limit_min(unsigned int l0_device_idx) const override;
79 int32_t power_limit_max(unsigned int l0_device_idx) const override;
80
81 void frequency_control(unsigned int l0_device_idx,
82 int l0_domain,
83 int l0_domain_idx,
84 double range_min,
85 double range_max) const override;
86
87 void performance_factor_control(unsigned int l0_device_idx,
88 int l0_domain,
89 int l0_domain_idx,
90 double setting) const override;
91
92 int ras_domain_count(unsigned int l0_device_idx,int l0_domain) const override;
93 double ras_reset_count_correctable(unsigned int l0_device_idx,
94 int l0_domain,
95 int l0_domain_idx) const override;
96 double ras_programming_errcount_correctable(unsigned int l0_device_idx,
97 int l0_domain,
98 int l0_domain_idx) const override;
99 double ras_driver_errcount_correctable(unsigned int l0_device_idx,
100 int l0_domain,
101 int l0_domain_idx) const override;
102 double ras_compute_errcount_correctable(unsigned int l0_device_idx,
103 int l0_domain,
104 int l0_domain_idx) const override;
105 double ras_noncompute_errcount_correctable(unsigned int l0_device_idx,
106 int l0_domain,
107 int l0_domain_idx) const override;
108 double ras_cache_errcount_correctable(unsigned int l0_device_idx,
109 int l0_domain,
110 int l0_domain_idx) const override;
111 double ras_display_errcount_correctable(unsigned int l0_device_idx,
112 int l0_domain,
113 int l0_domain_idx) const override;
114 double ras_reset_count_uncorrectable(unsigned int l0_device_idx,
115 int l0_domain,
116 int l0_domain_idx) const override;
117 double ras_programming_errcount_uncorrectable(unsigned int l0_device_idx,
118 int l0_domain,
119 int l0_domain_idx) const override;
120 double ras_driver_errcount_uncorrectable(unsigned int l0_device_idx,
121 int l0_domain,
122 int l0_domain_idx) const override;
123 double ras_compute_errcount_uncorrectable(unsigned int l0_device_idx,
124 int l0_domain,
125 int l0_domain_idx) const override;
126 double ras_noncompute_errcount_uncorrectable(unsigned int l0_device_idx,
127 int l0_domain,
128 int l0_domain_idx) const override;
129 double ras_cache_errcount_uncorrectable(unsigned int l0_device_idx,
130 int l0_domain,
131 int l0_domain_idx) const override;
132 double ras_display_errcount_uncorrectable(unsigned int l0_device_idx,
133 int l0_domain,
134 int l0_domain_idx) const override;
135 std::vector<double> metric_sample(unsigned int l0_device_idx,
136 unsigned int l0_domain_idx,
137 std::string metric_name) const override;
138 uint32_t metric_update_rate(unsigned int l0_device_idx) const override;
139 void metric_update_rate_control(unsigned int l0_device_idx, uint32_t setting) override;
140
141 void metric_read(unsigned int l0_device_idx,
142 unsigned int l0_domain_idx) override;
143
144 private:
145 enum m_error_type {
146 M_ERROR_TYPE_CORRECTABLE,
147 M_ERROR_TYPE_UNCORRECTABLE,
148 M_NUM_ERROR_TYPE,
149 };
150
151 struct m_frequency_s {
152 double voltage = 0;
153 double request = 0;
154 double tdp = 0;
155 double efficient = 0;
156 double actual = 0;
157 uint32_t throttle_reasons = 0;
158 };
159 struct m_power_limit_s {
160 int32_t tdp = 0;
161 int32_t min = 0;
162 int32_t max = 0;
163 };
164
165 struct m_subdevice_s {
166 // These are enum geopm_levelzero_domain_e indexed, then subdevice indexed
167 std::vector<std::vector<zes_freq_handle_t> > freq_domain;
168 std::vector<std::vector<zes_temp_handle_t> > temp_domain_max;
169 std::vector<std::vector<zes_engine_handle_t> > engine_domain;
170 mutable std::vector<std::vector<uint64_t> > cached_timestamp;
171
172 //uint32_t num_subdevice_perf_domain;
173 std::vector<std::vector<zes_perf_handle_t>> perf_domain;
174
175 uint32_t num_subdevice_power_domain;
176 std::vector<zes_pwr_handle_t> power_domain;
177 mutable std::vector<uint64_t> cached_energy_timestamp;
178
179 // Note: For RAS counters, as of LevelZero ver 1.9, can't be neatly
180 // categorized as being specific to compute/memory domains.
181 // So, assume, L0_domain_type = M_DOMAIN_ALL
182
183 // The RAS counters are index first by subdevice indexed,
184 // then by error set type (correctable vs uncorrectable)
185 std::vector<zes_ras_handle_t> ras_domain;
186
187 //ZE Context used for ZET data collection. Chip indexed
188 std::vector<ze_context_handle_t> context;
189
190 // required for L0 metric querying. Chip indexed
191 std::vector<uint32_t> num_metric;
192 std::vector<uint32_t> num_reports;
193 std::vector<bool> metric_domain_cached;
194 std::vector<zet_metric_streamer_handle_t> metric_streamer;
195 std::vector<zet_metric_group_handle_t> metric_group_handle; //ComputeBasic only
196 std::vector<size_t> zet_data_size;
197 std::vector<std::vector<uint8_t>> zet_data;
198 std::vector<size_t> report_byte_size; // Learned per-report byte size (0 until first read)
199
200 // Cached metric name -> index within a report. Chip indexed.
201 // Populated once during metric_group_init, used in metric_calc
202 // to avoid repeated zetMetricGet/zetMetricGetProperties calls.
203 std::vector<std::map<std::string, size_t>> metric_name_idx;
204
205 // required for L0 metric result tracking. Chip indexed
206 mutable std::vector<std::map<std::string, std::vector<double>>> metric_data;
207 // Online per-metric aggregate written by the background sampling
208 // thread: the running sum and report count between read_batch()
209 // calls. Bounded regardless of the consumer's read cadence; the
210 // read_batch() serving thread reduces this to the mean in
211 // metric_data once per read_batch(). Chip indexed.
213 double sum = 0.0;
214 uint64_t count = 0;
215 };
216 mutable std::vector<std::map<std::string, m_metric_aggregate>> metric_data_accum;
217 // Whether the background thread should drain this chip's
218 // streamer (set when the chip is first read via read_batch()).
219 // Chip indexed.
220 std::vector<bool> metric_active;
221 // Time the chip's streamer was last drained (by either the
222 // read_batch() serving thread or the background thread). Used so
223 // the thread only drains as a keep-alive when read_batch() isn't
224 // draining frequently enough on its own. Chip indexed.
225 std::vector<std::chrono::steady_clock::time_point> metric_last_drain;
226 mutable std::vector<bool> metrics_initialized;
227 };
228
229 struct m_device_info_s {
230 ze_driver_handle_t driver;
231 zes_device_handle_t device_handle;
232 ze_device_properties_t property;
233 uint32_t num_subdevice;
234 std::vector<zes_device_handle_t> subdevice_handle;
235
236 // Sub-Device domain tracking. Because levelzero returns ALL handles for a
237 // 'class' (freq, power, etc) regardless of subdevice it is easier to track
238 // this as class.domain.subdevice where domain is compute/memory. This avoids
239 // an additional step of sorting handles to determine how many per subdevice
240 m_subdevice_s subdevice;
241
242 // Device/Package domains
243 zes_pwr_handle_t power_domain;
244
245 uint32_t num_device_power_domain;
246 mutable uint64_t cached_energy_timestamp;
247
248 uint32_t metric_sampling_period_ns;
249 };
250
251 void ras_domain_cache(unsigned int l0_device_idx);
252
253 void frequency_domain_cache(unsigned int l0_device_idx);
254 void power_domain_cache(unsigned int l0_device_idx);
255 void perf_domain_cache(unsigned int l0_device_idx);
256 void engine_domain_cache(unsigned int l0_device_idx);
257 void temperature_domain_cache(unsigned int l0_device_idx);
258 void check_ze_result(ze_result_t ze_result, int error, std::string message,
259 int line) const;
260
261 std::pair<double, double> frequency_min_max(unsigned int l0_device_idx,
262 int l0_domain, int l0_domain_idx) const;
263
264 m_power_limit_s power_limit_default(unsigned int l0_device_idx) const;
265 uint64_t ras_status_helper(unsigned int l0_device_idx,
266 int l0_domain,
267 int l0_domain_idx,
268 zes_ras_error_cat_t errorcat,
269 int errortype) const;
270
271 m_frequency_s frequency_status_helper(unsigned int l0_device_idx,
272 int l0_domain, int l0_domain_idx) const;
273
274 uint32_t m_num_gpu;
275 uint32_t m_num_gpu_subdevice;
276
277 std::vector<ze_driver_handle_t> m_levelzero_driver;
278 std::vector<m_device_info_s> m_devices;
279
280 static constexpr uint32_t SAMPLING_PERIOD_NS = 500000; // 0.5 ms
281 static constexpr size_t DEFAULT_REPORT_BUFFER_SIZE = 16 * 1024 * 1024; // 16 MB
282 static constexpr uint32_t DEFAULT_MAX_REPORTS_PER_READ = 30;
283 // Fixed cadence at which the background thread drains the metric
284 // streamers, independent of the read_batch() cadence. Frequent
285 // draining keeps the Intel metric streamer delivering continuously
286 // (a slow drain interval stalls it into multi-second NOT_READY bursts).
287 static constexpr uint64_t METRIC_DRAIN_PERIOD_US = 20000; // 20 ms
288
289 // Background metric sampling thread and the state it shares with the
290 // read_batch() serving thread -- the controller, or the geopmd batch
291 // server when LevelZero is accessed through the service
292 // (metric_data_accum, protected by m_metric_mutex).
293 std::thread m_metric_thread;
294 std::mutex m_metric_mutex;
295 std::atomic<bool> m_metric_thread_active;
296 bool m_metric_thread_started;
297 // Captures an exception thrown by metric_drain on the background
298 // thread so the read_batch() path can rethrow it, rather than the
299 // thread terminating the process. Protected by m_metric_mutex.
300 std::exception_ptr m_metric_thread_error;
301
302 void metric_group_init(unsigned int l0_device_idx);
303 void metric_calc(unsigned int l0_device_idx, unsigned int l0_domain_idx,
304 size_t data_size, const uint8_t *data);
305
306 void metric_execute(unsigned int l0_device_idx,
307 unsigned int l0_domain_idx);
308 void metric_destroy(unsigned int l0_device_idx,
309 unsigned int l0_domain_idx);
310
311 // Background sampling thread: drains active streamers every
312 // METRIC_DRAIN_PERIOD_US and accumulates reports.
313 void metric_sample_thread(void);
314 // Read one chip's streamer and append the reports to the accumulator.
315 void metric_drain(unsigned int l0_device_idx, unsigned int l0_domain_idx);
316 void metric_thread_start(void);
317 void metric_thread_stop(void);
318
319 double metric_data_convert(zet_typed_value_t data) const;
320 };
321}
322#endif
Definition LevelZero.hpp:18
Definition LevelZeroImp.hpp:27
int frequency_domain_count(unsigned int l0_device_idx, int domain) const override
Get the number of LevelZero frequency domains of a certain type.
Definition LevelZero.cpp:1254
double ras_programming_errcount_correctable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of correctable hardware exceptions generated by the way workloads h...
Definition LevelZero.cpp:1311
uint64_t active_time(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero device active time in microseconds.
Definition LevelZero.cpp:1545
LevelZeroImp()
Definition LevelZero.cpp:39
double ras_reset_count_uncorrectable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of uncorrectable accelerator engine resets attempted by the driver.
Definition LevelZero.cpp:1359
double frequency_efficient(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero device efficient frequency in MHz.
Definition LevelZero.cpp:1441
std::pair< uint64_t, uint64_t > energy_pair(int geopm_domain, unsigned int l0_device_idx, int l0_domain_idx) const override
Get the LevelZero device energy and timestamp in microjoules and microseconds.
Definition LevelZero.cpp:1590
int num_gpu(void) const override
Number of GPUs on the platform.
Definition LevelZero.cpp:1211
void metric_read(unsigned int l0_device_idx, unsigned int l0_domain_idx) override
Definition LevelZero.cpp:915
double ras_compute_errcount_uncorrectable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of uncorrectable errors that have occurred in the compute accelerat...
Definition LevelZero.cpp:1383
double frequency_status(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero device actual frequency in MHz.
Definition LevelZero.cpp:1435
double temperature_max(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero device maximum temperature in Celsius.
Definition LevelZero.cpp:1527
int ras_domain_count(unsigned int l0_device_idx, int l0_domain) const override
Get the number of LevelZero RAS domains of a certain type.
Definition LevelZero.cpp:1287
std::pair< uint64_t, uint64_t > active_time_pair(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero device active time and timestamp in microseconds.
Definition LevelZero.cpp:1551
double ras_noncompute_errcount_correctable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of correctable errors that have occurred in the fixed-function acce...
Definition LevelZero.cpp:1335
int power_domain_count(int geopm_domain, unsigned int l0_device_idx, int l0_domain) const override
Get the number of LevelZero power domains of a certain type.
Definition LevelZero.cpp:1238
double ras_noncompute_errcount_uncorrectable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of uncorrectable errors that have occurred in the fixed-function ac...
Definition LevelZero.cpp:1391
double ras_display_errcount_correctable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of correctable errors that have occurred in the display.
Definition LevelZero.cpp:1351
double ras_display_errcount_uncorrectable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of uncorrectable errors that have occurred in the display.
Definition LevelZero.cpp:1407
int temperature_domain_count(unsigned int l0_device_idx, int l0_domain) const override
Get the number of LevelZero temperature domains.
Definition LevelZero.cpp:1269
void metric_update_rate_control(unsigned int l0_device_idx, uint32_t setting) override
Definition LevelZero.cpp:1706
int engine_domain_count(unsigned int l0_device_idx, int domain) const override
Get the number of LevelZero engine domains.
Definition LevelZero.cpp:1259
void performance_factor_control(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx, double setting) const override
Set the performance factor for the LevelZero device.
Definition LevelZero.cpp:1691
int32_t power_limit_max(unsigned int l0_device_idx) const override
Get the LevelZero device maximum power limit in milliwatts.
Definition LevelZero.cpp:1642
int32_t power_limit_tdp(unsigned int l0_device_idx) const override
Get the LevelZero device default power limit in milliwatts.
Definition LevelZero.cpp:1624
uint64_t active_time_timestamp(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the cachced LevelZero device timestamp for the active time value in microseconds.
Definition LevelZero.cpp:1539
uint64_t energy_timestamp(int geopm_domain, unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero device energy cached timestamp in microseconds.
Definition LevelZero.cpp:1569
double ras_cache_errcount_correctable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of correctable errors that have occurred in caches (L1/L3/register ...
Definition LevelZero.cpp:1343
std::pair< double, double > frequency_range(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero device minimum and maximum frequency control range in MHz.
Definition LevelZero.cpp:1515
double performance_factor(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the performance factor value of various LevelZero domains.
Definition LevelZero.cpp:1274
virtual ~LevelZeroImp()
Definition LevelZero.cpp:236
uint32_t frequency_throttle_reasons(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero device frequency throttle reasons.
Definition LevelZero.cpp:1447
double ras_cache_errcount_uncorrectable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of uncorrectable errors that have occurred in caches (L1/L3/registe...
Definition LevelZero.cpp:1399
double frequency_min(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero device mininmum frequency in MHz.
Definition LevelZero.cpp:1473
std::vector< double > frequency_supported(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero device supported frequencies in MHz.
Definition LevelZero.cpp:1497
double ras_reset_count_correctable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of correctable accelerator engine resets attempted by the driver.
Definition LevelZero.cpp:1303
int32_t power_limit_min(unsigned int l0_device_idx) const override
Get the LevelZero device minimum power limit in milliwatts.
Definition LevelZero.cpp:1633
double frequency_max(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero device maximum frequency in MHz.
Definition LevelZero.cpp:1479
double ras_compute_errcount_correctable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of correctable errors that have occurred in the compute accelerator...
Definition LevelZero.cpp:1327
std::vector< double > metric_sample(unsigned int l0_device_idx, unsigned int l0_domain_idx, std::string metric_name) const override
Definition LevelZero.cpp:1112
void frequency_control(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx, double range_min, double range_max) const override
Set min and max frequency for LevelZero device.
Definition LevelZero.cpp:1667
double ras_driver_errcount_correctable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of low level driver communication correctable errors have occurred.
Definition LevelZero.cpp:1319
int performance_domain_count(unsigned int l0_device_idx, int l0_domain) const override
Get the number of LevelZero perf domains of a certain type.
Definition LevelZero.cpp:1264
double ras_programming_errcount_uncorrectable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of uncorrectable hardware exceptions generated by the way workloads...
Definition LevelZero.cpp:1367
double ras_driver_errcount_uncorrectable(unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero count of number of low level driver communication uncorrectable errors have occurre...
Definition LevelZero.cpp:1375
uint32_t metric_update_rate(unsigned int l0_device_idx) const override
Definition LevelZero.cpp:1701
uint64_t energy(int geopm_domain, unsigned int l0_device_idx, int l0_domain, int l0_domain_idx) const override
Get the LevelZero device energy in microjoules.
Definition LevelZero.cpp:1584
Definition Agg.cpp:20
double sum
Definition LevelZeroImp.hpp:213
uint64_t count
Definition LevelZeroImp.hpp:214