Added support for Zen/Zen3 RAPL package power sensor.

This commit is contained in:
2025-02-27 20:35:24 -08:00
parent 5319945f08
commit 4bb85c461e
6 changed files with 180 additions and 30 deletions
+52 -27
View File
@@ -16,14 +16,19 @@
#include "icx3.h"
#include "evga-card.h"
#include "board-sensors.h"
#include "zen3-rapl.h"
#define MAX_GPUS 16
#define MAX_BOARD_SENSORS 256
#define HEADER_COLOR_START "\x1b[36m"
#define HEADER_COLOR_END "\x1b[39m"
char *header_start = "";
char *header_end = "";
static char *header_start = "";
static char *header_end = "";
static int zen3_rapl_sensor = -1; /* Board sensor number for the RAPL sensor */
static int compact = 0; /* Compact one-line per GPU display */
static int no_reasons = 0; /* Don't probe or display NVML clock reasons */
static const char helpstring[] = "Available options:\n"
"--i2c N : Only probe I2C bus N instead of all (may help with stuttering or freezing when probing I2C devices)\n"
@@ -43,20 +48,18 @@ static const char helpstring[] = "Available options:\n"
"--no-reasons : Do not query NVML for clock reasons (can cause stuttering)\n"
"--board : Also print temperatures from the CPU, motherboard, and other sensors";
void print_gpu_info(int gpu_num, struct card_info gpus[], int compact, int no_reasons);
void print_board_info(struct hwmon_avail_sensor *board_sensors, int num_sensors, int compact);
void print_gpu_info(int gpu_num, struct card_info gpus[]);
void print_board_info(struct hwmon_avail_sensor *board_sensors, int num_sensors);
int main (int argc, char **argv)
{
struct card_info gpus[MAX_GPUS];
struct hwmon_avail_sensor board_sensors[MAX_BOARD_SENSORS];
int gpu_count, board_sensor_count;
int print_info = 0;
int compact = 0;
int print_info = 0;
int gpu_num = -1; /* Card to control */
int i2c_bus = -1; /* Specific i2c bus to probe instead of all */
int overwrite = 0; /* Overwrite printed console info in compact mode */
int no_reasons = 0; /* Don't probe or display NVML clock reasons */
int overwrite = 0; /* Overwrite printed console info in compact mode */
unsigned int watch = 0; /* Refresh display every this many seconds */
int print_board_sensors = 0; /* Print CPU/motherbord/other sensors as well */
char *fan_speed[ICX3_MAX_FANS] = {NULL};
@@ -159,8 +162,14 @@ int main (int argc, char **argv)
}
/* Scan for motherboard/CPU/other sensors */
if (print_board_sensors)
if (print_board_sensors) {
board_sensor_count = find_board_sensors(board_sensors, MAX_BOARD_SENSORS);
if (init_rapl() && board_sensor_count < MAX_BOARD_SENSORS) {
board_sensors[board_sensor_count] = rapl_sensor;
zen3_rapl_sensor = board_sensor_count;
board_sensor_count++;
}
}
/* execute fan commands */
if (gpu_num == -1) {
@@ -199,17 +208,17 @@ int main (int argc, char **argv)
printf("\x1b[K"); /* Clear current console line */
if (print_board_sensors)
print_board_info(board_sensors, board_sensor_count, compact);
print_board_info(board_sensors, board_sensor_count);
if (gpu_num == -1) {
/* No GPU specified on command line, loop over all supported GPUs */
for (int i = 0; i < gpu_count; i++){
if (i > 0)
printf("\n");
print_gpu_info(i, &gpus[i], compact, no_reasons);
print_gpu_info(i, &gpus[i]);
}
} else if (gpu_num <= gpu_count - 1) {
print_gpu_info(gpu_num, &gpus[gpu_num], compact, no_reasons);
print_gpu_info(gpu_num, &gpus[gpu_num]);
}
if (!overwrite)
@@ -231,7 +240,7 @@ int main (int argc, char **argv)
#endif
}
void print_board_info(struct hwmon_avail_sensor *board_sensors, int num_sensors, int compact)
void print_board_info(struct hwmon_avail_sensor *board_sensors, int num_sensors)
{
int printed_sensors = 0;
int current_sort_index = 0;
@@ -241,39 +250,56 @@ void print_board_info(struct hwmon_avail_sensor *board_sensors, int num_sensors,
char *last_short_name = NULL;
char *last_units = NULL;
while (printed_sensors < num_sensors) {
for (int i=0; i < num_sensors; i++){
float rapl_power;
for (int i = 0; i < (sizeof(hwmon_sensor_info) / sizeof(struct hwmon_sensor)); i++) {
/* Inject our Zen RAPL power reading here */
if (strcmp(hwmon_sensor_info[i].driver_name, "zen-rapl") == 0) {
board_sensors[zen3_rapl_sensor].sort_index = current_sort_index;
board_sensors[zen3_rapl_sensor].sensor_info = &hwmon_sensor_info[i];
}
for (int j=0; j < num_sensors; j++) {
struct hwmon_avail_sensor *current_sensor = &board_sensors[j];
/* Loop over all sensors, but only output those with the current sort index so they come out sort of sorted
Duplicates (e.g.) multiple NVMe will come out in whatever sort of order the directory listing happened to */
if (board_sensors[i].sort_index == current_sort_index) {
if (current_sensor->sort_index == current_sort_index) {
printed_sensors++;
good_reading = get_sensor_reading(&board_sensors[i], &current_reading);
if (j == zen3_rapl_sensor) {
good_reading = 1;
current_reading = get_rapl_package_power();
} else {
good_reading = get_sensor_reading(current_sensor, &current_reading);
}
if (!good_reading)
continue;
if (compact) {
/* Print units if needed */
if (last_units != NULL && strcmp(board_sensors[i].sensor_info->units, last_units))
if (last_units != NULL && strcmp(current_sensor->sensor_info->units, last_units))
printf("%s", last_units);
/* Print new section header if needed */
if (last_short_name == NULL || strcmp(board_sensors[i].sensor_info->short_name, last_short_name)) {
if (last_short_name == NULL || strcmp(current_sensor->sensor_info->short_name, last_short_name)) {
if (last_short_name != NULL) /* Spacer for all headings not the first one */
printf(" ");
printf("%s%s%s", header_start, board_sensors[i].sensor_info->short_name, header_end);
printf("%s%s%s", header_start, current_sensor->sensor_info->short_name, header_end);
}
printf(" %3.0f", current_reading);
last_short_name = board_sensors[i].sensor_info->short_name;
last_units = board_sensors[i].sensor_info->units;
last_short_name = current_sensor->sensor_info->short_name;
last_units = current_sensor->sensor_info->units;
} else {
printf("%s%s: %+.1f%s\n",
board_sensors[i].sensor_info->name_prefix,
board_sensors[i].sensor_name,
current_sensor->sensor_info->name_prefix,
current_sensor->sensor_name,
current_reading,
board_sensors[i].sensor_info->units);
current_sensor->sensor_info->units);
}
}
@@ -287,7 +313,7 @@ void print_board_info(struct hwmon_avail_sensor *board_sensors, int num_sensors,
printf("\n");
}
void print_gpu_info(int gpu_num, struct card_info *gpu, int compact, int no_reasons)
void print_gpu_info(int gpu_num, struct card_info *gpu)
{
if (compact) {
/* One line per GPU */
@@ -360,6 +386,5 @@ void print_gpu_info(int gpu_num, struct card_info *gpu, int compact, int no_reas
printf("\n");
#endif
}
}