From 4d5f62386b6fb07b8f4447f1fcd19924d1d1c817 Mon Sep 17 00:00:00 2001 From: FlightlessMango Date: Sun, 1 Mar 2020 22:01:59 +0100 Subject: [PATCH] added gpu mem and core clocks to hud --- src/cpu_gpu.h | 38 +++++++++++++++++++++++++++++++++++-- src/loaders/loader_nvml.cpp | 15 ++++++++++++++- src/loaders/loader_nvml.h | 1 + src/nvidia_info.h | 3 +-- src/nvml.cpp | 4 +++- src/overlay.cpp | 22 ++++++++++++++++++++- src/overlay_params.cpp | 9 ++++++++- src/overlay_params.h | 3 +++ 8 files changed, 87 insertions(+), 8 deletions(-) diff --git a/src/cpu_gpu.h b/src/cpu_gpu.h index afb324c3..0c591d46 100644 --- a/src/cpu_gpu.h +++ b/src/cpu_gpu.h @@ -7,8 +7,8 @@ using namespace std; -int gpuLoad = 0, gpuTemp = 0, cpuTemp = 0; -FILE *amdGpuFile = nullptr, *amdTempFile = nullptr, *cpuTempFile = nullptr, *amdGpuVramTotalFile = nullptr, *amdGpuVramUsedFile = nullptr; +int gpuLoad = 0, gpuTemp = 0, cpuTemp = 0, gpuMemClock, gpuCoreClock; +FILE *amdGpuFile = nullptr, *amdTempFile = nullptr, *cpuTempFile = nullptr, *amdGpuVramTotalFile = nullptr, *amdGpuVramUsedFile = nullptr, *amdGpuCoreClockFile = nullptr, *amdGpuMemoryClockFile = nullptr; float gpuMemUsed = 0, gpuMemTotal = 0; int numCpuCores = std::thread::hardware_concurrency(); @@ -19,6 +19,8 @@ struct amdGpu { int temp; int64_t memoryUsed; int64_t memoryTotal; + int MemClock; + int CoreClock; }; extern struct amdGpu amdgpu; @@ -66,6 +68,8 @@ void *getNvidiaGpuInfo(void *){ gpuLoad = nvidiaUtilization.gpu; gpuTemp = nvidiaTemp; gpuMemUsed = float(nvidiaMemory.used / (1024 * 1024)) / 1000; + gpuCoreClock = nvidiaCoreClock; + gpuMemClock = nvidiaMemClock * 2; } pthread_detach(gpuThread); @@ -107,6 +111,36 @@ void *getAmdGpuUsage(void *){ amdgpu.memoryUsed /= (1024 * 1024); gpuMemUsed = float(amdgpu.memoryUsed) / 1000; } + + if (amdGpuCoreClockFile) { + rewind(amdGpuCoreClockFile); + fflush(amdGpuCoreClockFile); + char line[255]; + while (fgets(line, sizeof(line), amdGpuCoreClockFile)){ + std::string row = line; + row.erase(row.begin()); + if (row.find("*") != std::string::npos){ + row = std::regex_replace(row, std::regex(R"([^0-9])"), ""); + amdgpu.CoreClock = stoi(row); + gpuCoreClock = amdgpu.CoreClock; + } + } + } + + if (amdGpuMemoryClockFile) { + rewind(amdGpuMemoryClockFile); + fflush(amdGpuMemoryClockFile); + char line[255]; + while (fgets(line, sizeof(line), amdGpuMemoryClockFile)){ + std::string row = line; + row.erase(row.begin()); + if (row.find("*") != std::string::npos){ + row = std::regex_replace(row, std::regex(R"([^0-9])"), ""); + amdgpu.MemClock = stoi(row) * 2; + gpuMemClock = amdgpu.MemClock; + } + } + } pthread_detach(gpuThread); return NULL; diff --git a/src/loaders/loader_nvml.cpp b/src/loaders/loader_nvml.cpp index 8bf27940..262b41a6 100644 --- a/src/loaders/loader_nvml.cpp +++ b/src/loaders/loader_nvml.cpp @@ -135,7 +135,7 @@ bool libnvml_loader::Load(const std::string& library_name) { return false; } - #if defined(LIBRARY_LOADER_NVML_H_DLOPEN) +#if defined(LIBRARY_LOADER_NVML_H_DLOPEN) nvmlDeviceGetMemoryInfo = reinterpret_castnvmlDeviceGetMemoryInfo)>( dlsym(library_, "nvmlDeviceGetMemoryInfo")); @@ -148,6 +148,19 @@ bool libnvml_loader::Load(const std::string& library_name) { return false; } +#if defined(LIBRARY_LOADER_NVML_H_DLOPEN) + nvmlDeviceGetClockInfo = + reinterpret_castnvmlDeviceGetClockInfo)>( + dlsym(library_, "nvmlDeviceGetClockInfo")); +#endif +#if defined(LIBRARY_LOADER_NVML_H_DT_NEEDED) + nvmlDeviceGetClockInfo = &::nvmlDeviceGetClockInfo; +#endif + if (!nvmlDeviceGetClockInfo) { + CleanUp(true); + return false; + } + loaded_ = true; return true; } diff --git a/src/loaders/loader_nvml.h b/src/loaders/loader_nvml.h index 5b9522ef..dfb65d26 100644 --- a/src/loaders/loader_nvml.h +++ b/src/loaders/loader_nvml.h @@ -30,6 +30,7 @@ class libnvml_loader { decltype(&::nvmlDeviceGetHandleByIndex_v2) nvmlDeviceGetHandleByIndex_v2; decltype(&::nvmlDeviceGetHandleByPciBusId_v2) nvmlDeviceGetHandleByPciBusId_v2; decltype(&::nvmlDeviceGetMemoryInfo) nvmlDeviceGetMemoryInfo; + decltype(&::nvmlDeviceGetClockInfo) nvmlDeviceGetClockInfo; private: void CleanUp(bool unload); diff --git a/src/nvidia_info.h b/src/nvidia_info.h index b906c41b..a3e47cc8 100644 --- a/src/nvidia_info.h +++ b/src/nvidia_info.h @@ -3,8 +3,7 @@ #include extern nvmlReturn_t result; -extern unsigned int nvidiaTemp, processSamplesCount, lastSeenTimeStamp, *vgpuInstanceSamplesCount; -extern nvmlValueType_t *sampleValType; +extern unsigned int nvidiaTemp, processSamplesCount, *vgpuInstanceSamplesCount, nvidiaCoreClock, nvidiaMemClock; extern nvmlDevice_t nvidiaDevice; extern struct nvmlUtilization_st nvidiaUtilization; extern struct nvmlMemory_st nvidiaMemory; diff --git a/src/nvml.cpp b/src/nvml.cpp index 5c46eacb..7ff8a655 100644 --- a/src/nvml.cpp +++ b/src/nvml.cpp @@ -6,7 +6,7 @@ libnvml_loader nvml("libnvidia-ml.so.1"); nvmlReturn_t result; nvmlDevice_t nvidiaDevice; bool nvmlSuccess = false; -unsigned int nvidiaTemp; +unsigned int nvidiaTemp, nvidiaCoreClock, nvidiaMemClock; struct nvmlUtilization_st nvidiaUtilization; struct nvmlMemory_st nvidiaMemory {}; @@ -30,4 +30,6 @@ void getNvidiaInfo(){ nvml.nvmlDeviceGetUtilizationRates(nvidiaDevice, &nvidiaUtilization); nvml.nvmlDeviceGetTemperature(nvidiaDevice, NVML_TEMPERATURE_GPU, &nvidiaTemp); nvml.nvmlDeviceGetMemoryInfo(nvidiaDevice, &nvidiaMemory); + nvml.nvmlDeviceGetClockInfo(nvidiaDevice, NVML_CLOCK_GRAPHICS, &nvidiaCoreClock); + nvml.nvmlDeviceGetClockInfo(nvidiaDevice, NVML_CLOCK_MEM, &nvidiaMemClock); } \ No newline at end of file diff --git a/src/overlay.cpp b/src/overlay.cpp index 9024aed7..bb220946 100644 --- a/src/overlay.cpp +++ b/src/overlay.cpp @@ -820,6 +820,10 @@ void init_gpu_stats(struct device_data *device_data) amdGpuVramTotalFile = fopen((path + "/device/mem_info_vram_total").c_str(), "r"); if (!amdGpuVramUsedFile) amdGpuVramUsedFile = fopen((path + "/device/mem_info_vram_used").c_str(), "r"); + if (!amdGpuMemoryClockFile) + amdGpuMemoryClockFile = fopen((path + "/device/pp_dpm_mclk").c_str(), "r"); + if (!amdGpuCoreClockFile) + amdGpuCoreClockFile = fopen((path + "/device/pp_dpm_sclk").c_str(), "r"); path = path + "/device/hwmon/"; string tempFolder; @@ -1117,7 +1121,7 @@ static void compute_swapchain_display(struct swapchain_data *data) if (!instance_data->params.no_display){ ImGui::Begin("Main", &open, ImGuiWindowFlags_NoDecoration); - ImGui::BeginTable("hud", 3); + ImGui::BeginTable("hud", instance_data->params.tableCols); if (instance_data->params.enabled[OVERLAY_PARAM_ENABLED_time]){ ImGui::TableNextRow(); ImGui::TextColored(ImVec4(1.0f, 1.0f, 1.0f, 1.00f), "%s", data->time.c_str()); @@ -1133,6 +1137,14 @@ static void compute_swapchain_display(struct swapchain_data *data) ImGui::TableNextCell(); ImGui::Text("%i%s", gpuTemp, "°C"); } + if (instance_data->params.enabled[OVERLAY_PARAM_ENABLED_gpu_core_clock]){ + ImGui::TableNextCell(); + ImGui::Text("%i", gpuCoreClock); + ImGui::SameLine(0, 1.0f); + ImGui::PushFont(data->font1); + ImGui::Text("MHz"); + ImGui::PopFont(); + } } if(instance_data->params.enabled[OVERLAY_PARAM_ENABLED_cpu_stats]){ ImGui::TableNextRow(); @@ -1205,6 +1217,14 @@ static void compute_swapchain_display(struct swapchain_data *data) ImGui::PushFont(data->font1); ImGui::Text("GB"); ImGui::PopFont(); + if (instance_data->params.enabled[OVERLAY_PARAM_ENABLED_gpu_mem_clock]){ + ImGui::TableNextCell(); + ImGui::Text("%i", gpuMemClock); + ImGui::SameLine(0, 1.0f); + ImGui::PushFont(data->font1); + ImGui::Text("MHz"); + ImGui::PopFont(); + } } if (instance_data->params.enabled[OVERLAY_PARAM_ENABLED_ram]){ ImGui::TableNextRow(); diff --git a/src/overlay_params.cpp b/src/overlay_params.cpp index 9ff6b0c8..88a6a1bf 100644 --- a/src/overlay_params.cpp +++ b/src/overlay_params.cpp @@ -343,10 +343,17 @@ parse_overlay_config(struct overlay_params *params, // Command buffer gets reused and timestamps cause hangs for some reason, force off for now params->enabled[OVERLAY_PARAM_ENABLED_gpu_timing] = false; + + params->tableCols = 3; if (!params->font_size) params->font_size = 24; //increase hud width if io read and write - if (params->enabled[OVERLAY_PARAM_ENABLED_io_read] && params->enabled[OVERLAY_PARAM_ENABLED_io_write]) + if (params->enabled[OVERLAY_PARAM_ENABLED_io_read] && params->enabled[OVERLAY_PARAM_ENABLED_io_write] && params->width == 280) params->width = 15 * params->font_size; + + if (params->enabled[OVERLAY_PARAM_ENABLED_gpu_core_clock] && params->enabled[OVERLAY_PARAM_ENABLED_gpu_temp]){ + params->tableCols = 4; + params->width = 20 * params->font_size; + } } \ No newline at end of file diff --git a/src/overlay_params.h b/src/overlay_params.h index e68a3b00..3b46601d 100644 --- a/src/overlay_params.h +++ b/src/overlay_params.h @@ -54,6 +54,8 @@ extern "C" { OVERLAY_PARAM_BOOL(read_cfg) \ OVERLAY_PARAM_BOOL(io_read) \ OVERLAY_PARAM_BOOL(io_write) \ + OVERLAY_PARAM_BOOL(gpu_mem_clock) \ + OVERLAY_PARAM_BOOL(gpu_core_clock) \ OVERLAY_PARAM_CUSTOM(fps_sampling_period) \ OVERLAY_PARAM_CUSTOM(output_file) \ OVERLAY_PARAM_CUSTOM(position) \ @@ -111,6 +113,7 @@ struct overlay_params { unsigned offset_y; unsigned vsync; unsigned crosshair_color; + unsigned tableCols; float font_size; float background_alpha; KeySym toggle_hud;