2025-02-02 21:42:30 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "nvidia-sensors.h"
|
|
|
|
|
2025-02-02 23:28:28 -08:00
|
|
|
void init_nvml()
|
2025-02-02 21:42:30 -08:00
|
|
|
{
|
|
|
|
nvmlReturn_t result;
|
|
|
|
result = nvmlInit_v2();
|
2025-02-02 23:28:28 -08:00
|
|
|
if (result != NVML_SUCCESS)
|
2025-02-02 21:42:30 -08:00
|
|
|
printf("Could not init NVML: %s\n", nvmlErrorString(result));
|
|
|
|
}
|
|
|
|
|
2025-02-07 08:23:46 -08:00
|
|
|
int get_nvml_handle(nvmlDevice_t *device, struct card_info *card)
|
|
|
|
{
|
|
|
|
nvmlReturn_t result;
|
|
|
|
result = nvmlDeviceGetHandleByPciBusId_v2(card->pci_id, device);
|
|
|
|
if (result != NVML_SUCCESS) {
|
|
|
|
printf("Failed to get device handle for card at %s: %s\n", card->pci_id, nvmlErrorString(result));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2025-02-02 21:42:30 -08:00
|
|
|
void print_nvml_clock_reason(int compact, struct card_info *card)
|
|
|
|
{
|
|
|
|
unsigned long long reasons = get_nvml_clock_reasons(card);
|
|
|
|
int single_reason = 1;
|
2025-02-03 06:34:33 -08:00
|
|
|
|
2025-02-02 21:42:30 -08:00
|
|
|
for (int i = 0; i < (sizeof(clock_reason_names) / sizeof(struct clock_reason)); i++) {
|
|
|
|
if (reasons & clock_reason_names[i].mask) {
|
|
|
|
if (!single_reason) {
|
|
|
|
if (compact)
|
|
|
|
printf(",");
|
|
|
|
else
|
|
|
|
printf(", ");
|
|
|
|
}
|
|
|
|
single_reason = 0;
|
|
|
|
|
|
|
|
if (compact)
|
|
|
|
printf("%s", clock_reason_names[i].short_name);
|
|
|
|
else
|
|
|
|
printf("%s", clock_reason_names[i].long_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (single_reason)
|
|
|
|
printf("None");
|
|
|
|
|
|
|
|
if (!compact)
|
2025-02-07 08:23:46 -08:00
|
|
|
printf(" (0x%llx)", reasons);
|
2025-02-02 21:42:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int get_nvml_temp(struct card_info *card)
|
|
|
|
{
|
|
|
|
nvmlDevice_t nvml_device;
|
2025-02-07 08:23:46 -08:00
|
|
|
if (!get_nvml_handle(&nvml_device, card))
|
2025-02-02 21:42:30 -08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned int temp;
|
2025-02-07 08:23:46 -08:00
|
|
|
nvmlReturn_t result = nvmlDeviceGetTemperature(nvml_device, NVML_TEMPERATURE_GPU, &temp);
|
2025-02-02 21:42:30 -08:00
|
|
|
if (result != NVML_SUCCESS) {
|
|
|
|
printf("Failed to get temperature for card at %s: %s\n", card->pci_id, nvmlErrorString(result));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long get_nvml_clock_reasons(struct card_info *card)
|
|
|
|
{
|
|
|
|
nvmlDevice_t nvml_device;
|
2025-02-07 08:23:46 -08:00
|
|
|
if (!get_nvml_handle(&nvml_device, card))
|
2025-02-02 21:42:30 -08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned long long reasons;
|
2025-02-07 08:23:46 -08:00
|
|
|
nvmlReturn_t result = nvmlDeviceGetCurrentClocksEventReasons(nvml_device, &reasons) ;
|
2025-02-02 21:42:30 -08:00
|
|
|
if (result != NVML_SUCCESS) {
|
|
|
|
printf("Failed to get clock reasons for card at %s: %s\n", card->pci_id, nvmlErrorString(result));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return reasons;
|
|
|
|
}
|
|
|
|
|
2025-02-07 08:23:46 -08:00
|
|
|
unsigned int get_nvml_mem_util(struct card_info *card)
|
|
|
|
{
|
|
|
|
nvmlDevice_t nvml_device;
|
|
|
|
if (!get_nvml_handle(&nvml_device, card))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nvmlUtilization_t util;
|
|
|
|
nvmlReturn_t result = nvmlDeviceGetUtilizationRates(nvml_device, &util);
|
|
|
|
if (result != NVML_SUCCESS) {
|
|
|
|
printf("Failed to get clock reasons for card at %s: %s\n", card->pci_id, nvmlErrorString(result));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.memory;
|
|
|
|
}
|
|
|
|
|