From 23b82e125fb0f1a0ae3e11c52743a733ded9088f Mon Sep 17 00:00:00 2001 From: Jussi Kuokkanen Date: Wed, 4 Oct 2023 15:51:04 +0300 Subject: [PATCH] use NVML to get pcie bandwidth utilization Note: this might differ slightly from XNVCtrl reported values, eg. nvidia-settings --- src/plugins/Nvidia.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/plugins/Nvidia.cpp b/src/plugins/Nvidia.cpp index f1a5080..568afa2 100644 --- a/src/plugins/Nvidia.cpp +++ b/src/plugins/Nvidia.cpp @@ -409,6 +409,40 @@ std::vector> getMemoryUtilization(NvidiaGPUData data) { return {}; } +std::vector> getPcieUtilization(NvidiaGPUData data) { + uint linkSpeed, linkWidth; + auto widthRet = nvmlDeviceGetCurrPcieLinkWidth(data.devHandle, &linkWidth); + auto speedRet = nvmlDeviceGetPcieSpeed(data.devHandle, &linkSpeed); + if (speedRet != NVML_SUCCESS || widthRet != NVML_SUCCESS) + return {}; + + auto func = [=]() -> ReadResult{ + uint rx, tx; + auto rxret = nvmlDeviceGetPcieThroughput(data.devHandle, NVML_PCIE_UTIL_RX_BYTES, &rx); + auto txret = nvmlDeviceGetPcieThroughput(data.devHandle, NVML_PCIE_UTIL_TX_BYTES, &tx); + + if (txret == NVML_SUCCESS && rxret == NVML_SUCCESS) { + auto totalMBs = (rx + tx) / 1000; + auto maxMBs = (linkSpeed / 8) * linkWidth; + auto percentage = + (static_cast(totalMBs) / static_cast(maxMBs)) * 100; + // We could display this more granularly now, but is that really needed? + return static_cast(round(percentage)); + } + return ReadError::UnknownError; + }; + + DynamicReadable dr{func, "%"}; + + if (hasReadableValue(func())) + return {DeviceNode{ + .name = "PCIe Bandwidth Utilization", + .interface = dr, + .hash = md5(data.uuid + "PCIe Bandwidth Utilization"), + }}; + return {}; +} + std::vector> getPowerUsage(NvidiaGPUData data) { auto func = [data]() -> ReadResult{ uint value; @@ -645,6 +679,7 @@ std::vector> getUtilizationsRoot(NvidiaGPUData data) { auto gpuTree = TreeConstructor{ getGPUName, { {getUtilizationsRoot, { + {getPcieUtilization, {}}, {getCoreUtilization, {}}, {getMemoryUtilization, {}}, }},