2021-07-16 00:28:46 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2020-02-01 01:28:31 +00:00
|
|
|
#include "loaders/loader_nvml.h"
|
|
|
|
#include "nvidia_info.h"
|
2020-04-10 21:30:24 +00:00
|
|
|
#include <iostream>
|
2020-05-09 23:36:09 +00:00
|
|
|
#include "overlay.h"
|
2022-05-10 12:02:18 +00:00
|
|
|
#include "overlay_params.h"
|
2023-05-04 23:48:43 +00:00
|
|
|
#include "nvctrl.h"
|
2020-02-01 01:28:31 +00:00
|
|
|
|
|
|
|
nvmlReturn_t result;
|
|
|
|
nvmlDevice_t nvidiaDevice;
|
2020-05-09 23:36:09 +00:00
|
|
|
nvmlPciInfo_t nvidiaPciInfo;
|
2020-02-01 01:47:25 +00:00
|
|
|
bool nvmlSuccess = false;
|
2023-04-23 05:27:22 +00:00
|
|
|
unsigned int nvidiaTemp = 0, nvidiaCoreClock = 0, nvidiaMemClock = 0, nvidiaPowerUsage = 0, nvidiaFanSpeed = 0;
|
2022-05-03 09:00:05 +00:00
|
|
|
unsigned long long nvml_throttle_reasons;
|
2020-02-01 01:28:31 +00:00
|
|
|
struct nvmlUtilization_st nvidiaUtilization;
|
2020-02-05 07:20:09 +00:00
|
|
|
struct nvmlMemory_st nvidiaMemory {};
|
2023-04-23 05:27:22 +00:00
|
|
|
struct nvmlUnit_st* nvidiaUnit {};
|
2020-02-01 01:28:31 +00:00
|
|
|
|
2020-04-10 21:30:24 +00:00
|
|
|
bool checkNVML(const char* pciBusId){
|
2020-08-04 18:27:12 +00:00
|
|
|
auto& nvml = get_libnvml_loader();
|
2020-02-01 01:28:31 +00:00
|
|
|
if (nvml.IsLoaded()){
|
|
|
|
result = nvml.nvmlInit();
|
|
|
|
if (NVML_SUCCESS != result) {
|
2021-07-21 16:48:45 +00:00
|
|
|
SPDLOG_ERROR("Nvidia module not loaded");
|
2020-02-01 01:28:31 +00:00
|
|
|
} else {
|
2020-04-10 21:30:24 +00:00
|
|
|
nvmlReturn_t ret = NVML_ERROR_UNKNOWN;
|
2020-04-13 21:54:44 +00:00
|
|
|
if (pciBusId && ((ret = nvml.nvmlDeviceGetHandleByPciBusId(pciBusId, &nvidiaDevice)) != NVML_SUCCESS)) {
|
2021-07-21 16:48:45 +00:00
|
|
|
SPDLOG_ERROR("Getting device handle by PCI bus ID failed: {}", nvml.nvmlErrorString(ret));
|
|
|
|
SPDLOG_ERROR("Using index 0.");
|
2020-04-10 21:30:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 21:54:44 +00:00
|
|
|
if (ret != NVML_SUCCESS)
|
|
|
|
ret = nvml.nvmlDeviceGetHandleByIndex(0, &nvidiaDevice);
|
|
|
|
|
2020-04-10 22:01:37 +00:00
|
|
|
if (ret != NVML_SUCCESS)
|
2021-07-21 16:48:45 +00:00
|
|
|
SPDLOG_ERROR("Getting device handle failed: {}", nvml.nvmlErrorString(ret));
|
2020-04-10 22:01:37 +00:00
|
|
|
|
2020-04-10 21:30:24 +00:00
|
|
|
nvmlSuccess = (ret == NVML_SUCCESS);
|
2020-08-15 14:35:04 +00:00
|
|
|
if (ret == NVML_SUCCESS)
|
|
|
|
nvml.nvmlDeviceGetPciInfo_v3(nvidiaDevice, &nvidiaPciInfo);
|
2021-07-16 00:28:46 +00:00
|
|
|
|
2020-04-10 21:30:24 +00:00
|
|
|
return nvmlSuccess;
|
2020-02-01 01:28:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-21 16:48:45 +00:00
|
|
|
SPDLOG_ERROR("Failed to load NVML");
|
2020-02-01 01:28:31 +00:00
|
|
|
}
|
2021-07-16 00:28:46 +00:00
|
|
|
|
2020-02-04 07:48:42 +00:00
|
|
|
return false;
|
2020-05-10 12:11:56 +00:00
|
|
|
}
|
2020-02-01 01:28:31 +00:00
|
|
|
|
2022-05-10 12:02:18 +00:00
|
|
|
bool getNVMLInfo(const struct overlay_params& params){
|
2020-03-20 12:06:24 +00:00
|
|
|
nvmlReturn_t response;
|
2020-08-04 18:27:12 +00:00
|
|
|
auto& nvml = get_libnvml_loader();
|
2020-03-20 12:06:24 +00:00
|
|
|
response = nvml.nvmlDeviceGetUtilizationRates(nvidiaDevice, &nvidiaUtilization);
|
2020-02-01 01:28:31 +00:00
|
|
|
nvml.nvmlDeviceGetTemperature(nvidiaDevice, NVML_TEMPERATURE_GPU, &nvidiaTemp);
|
2020-02-05 07:20:09 +00:00
|
|
|
nvml.nvmlDeviceGetMemoryInfo(nvidiaDevice, &nvidiaMemory);
|
2020-03-01 21:01:59 +00:00
|
|
|
nvml.nvmlDeviceGetClockInfo(nvidiaDevice, NVML_CLOCK_GRAPHICS, &nvidiaCoreClock);
|
|
|
|
nvml.nvmlDeviceGetClockInfo(nvidiaDevice, NVML_CLOCK_MEM, &nvidiaMemClock);
|
2020-05-22 12:44:34 +00:00
|
|
|
nvml.nvmlDeviceGetPowerUsage(nvidiaDevice, &nvidiaPowerUsage);
|
2020-05-09 23:36:09 +00:00
|
|
|
deviceID = nvidiaPciInfo.pciDeviceId >> 16;
|
2022-05-10 12:02:18 +00:00
|
|
|
if (params.enabled[OVERLAY_PARAM_ENABLED_throttling_status])
|
|
|
|
nvml.nvmlDeviceGetCurrentClocksThrottleReasons(nvidiaDevice, &nvml_throttle_reasons);
|
2022-05-03 09:00:05 +00:00
|
|
|
|
2023-10-04 16:04:51 +00:00
|
|
|
nvml.nvmlDeviceGetFanSpeed(nvidiaDevice, &nvidiaFanSpeed);
|
|
|
|
|
2022-02-04 14:50:51 +00:00
|
|
|
if (response == NVML_ERROR_NOT_SUPPORTED) {
|
|
|
|
if (nvmlSuccess)
|
|
|
|
SPDLOG_ERROR("nvmlDeviceGetUtilizationRates failed");
|
2020-03-20 12:06:24 +00:00
|
|
|
nvmlSuccess = false;
|
2022-02-04 14:50:51 +00:00
|
|
|
}
|
2020-03-30 19:48:56 +00:00
|
|
|
return nvmlSuccess;
|
2020-05-10 12:11:56 +00:00
|
|
|
}
|