-
-
Notifications
You must be signed in to change notification settings - Fork 49
Add NVIDIA GPU monitoring via NVML #533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wateenellende
wants to merge
1
commit into
elementary:main
Choose a base branch
from
wateenellende:feat/nvidia-nvml-monitoring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,163 +3,157 @@ | |
| * SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io) | ||
| */ | ||
|
|
||
| public class Monitor.GPUNvidia : IGPU, Object { | ||
|
|
||
| public Gee.HashMap<string, HwmonTemperature> hwmon_temperatures { get; set; } | ||
| [Compact] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create a separate vapi file |
||
| [CCode (cname = "MonitorNvmlGpu", free_function = "monitor_nvml_gpu_free")] | ||
| private class MonitorNvmlGpu { | ||
| } | ||
|
|
||
| public string hwmon_module_name { get; set; } | ||
| [CCode (cname = "MonitorNvmlSample")] | ||
| private struct MonitorNvmlSample { | ||
| public uint gpu_percent; | ||
| public uint64 mem_used; | ||
| public uint64 mem_total; | ||
| public uint temperature_c; | ||
| public bool ok_util; | ||
| public bool ok_mem; | ||
| public bool ok_temp; | ||
| } | ||
|
|
||
| public string driver_name { get; set; } | ||
| [CCode (cname = "monitor_nvml_gpu_new_from_pci_bus_id")] | ||
| private extern MonitorNvmlGpu? monitor_nvml_gpu_new_from_pci_bus_id (string pci_bus_id); | ||
|
|
||
| public string name { get; set; } | ||
| [CCode (cname = "monitor_nvml_gpu_sample")] | ||
| private extern bool monitor_nvml_gpu_sample (MonitorNvmlGpu gpu, out MonitorNvmlSample sample); | ||
|
|
||
| public class Monitor.GPUNvidia : IGPU, Object { | ||
| public Gee.HashMap<string, HwmonTemperature> hwmon_temperatures { get; set; } | ||
| public string hwmon_module_name { get; protected set; } | ||
| public string driver_name { get; protected set; } | ||
| public string name { get; protected set; } | ||
| public int percentage { get; protected set; } | ||
|
|
||
| public int memory_percentage { get; protected set; } | ||
|
|
||
| public int fb_percentage { get; protected set; } | ||
|
|
||
| public double memory_vram_used { get; protected set; } | ||
|
|
||
| public double memory_vram_total { get; set; } | ||
|
|
||
| public double memory_vram_total { get; protected set; } | ||
| public double temperature { get; protected set; } | ||
| protected string sysfs_path { get; protected set; } | ||
|
|
||
| protected string sysfs_path { get; set; } | ||
|
|
||
| public int nvidia_temperature = 0; | ||
|
|
||
| public int nvidia_memory_vram_used = 0; | ||
|
|
||
| public int nvidia_memory_vram_total = 0; | ||
|
|
||
| public int nvidia_memory_percentage = 0; | ||
|
|
||
| public int nvidia_fb_percentage = 0; | ||
|
|
||
| public int nvidia_percentage = 0; | ||
|
|
||
| public char * nvidia_used = ""; | ||
|
|
||
| public bool nvidia_resources_temperature; | ||
|
|
||
| public bool nvidia_resources_vram_used; | ||
|
|
||
| public bool nvidia_resources_vram_total; | ||
|
|
||
| public bool nvidia_resources_used; | ||
|
|
||
| public X.Display nvidia_display; | ||
| private MonitorNvmlGpu? nvml_gpu; | ||
| private MonitorNvmlSample sample_cache; | ||
|
|
||
| public GPUNvidia (Pci.Access pci_access, Pci.Dev pci_device) { | ||
| name = pci_parse_name (pci_access, pci_device); | ||
| name = "nVidia® " + name; | ||
|
|
||
| name = "NVIDIA " + pci_parse_name (pci_access, pci_device); | ||
| sysfs_path = pci_parse_sysfs_path (pci_access, pci_device); | ||
| driver_name = pci_device.get_string_property (Pci.FILL_DRIVER); | ||
| } | ||
| hwmon_module_name = driver_name; | ||
| hwmon_temperatures = new Gee.HashMap<string, HwmonTemperature> (); | ||
|
|
||
| construct { | ||
| nvidia_display = new X.Display (); | ||
| } | ||
|
|
||
| private void update_nv_resources () { | ||
| #if NVIDIA_SUPPORT | ||
| nvidia_resources_temperature = NVCtrl.XNVCTRLQueryAttribute ( | ||
| nvidia_display, | ||
| 0, | ||
| 0, | ||
| NV_CTRL_GPU_CORE_TEMPERATURE, | ||
| &nvidia_temperature | ||
| ); | ||
|
|
||
| if (!nvidia_resources_temperature) { | ||
| debug ("Could not query NV_CTRL_GPU_CORE_TEMPERATURE attribute!\n"); | ||
| return; | ||
| string pci_bus_id = "%04x:%02x:%02x.%u".printf ( | ||
| pci_device.domain_16, | ||
| pci_device.bus, | ||
| pci_device.dev, | ||
| pci_device.func | ||
| ); | ||
|
|
||
| nvml_gpu = monitor_nvml_gpu_new_from_pci_bus_id (pci_bus_id); | ||
| if (nvml_gpu == null) { | ||
| warning ("Failed to initialize NVML handle for %s (%s)", name, pci_bus_id); | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
| nvidia_resources_vram_used = NVCtrl.XNVCTRLQueryTargetAttribute ( | ||
| nvidia_display, | ||
| NV_CTRL_TARGET_TYPE_GPU, | ||
| 0, | ||
| 0, | ||
| NV_CTRL_USED_DEDICATED_GPU_MEMORY, | ||
| &nvidia_memory_vram_used | ||
| ); | ||
|
|
||
| if (!nvidia_resources_vram_used) { | ||
| debug ("Could not query NV_CTRL_USED_DEDICATED_GPU_MEMORY attribute!\n"); | ||
| return; | ||
| private bool update_nv_resources () { | ||
| #if NVIDIA_SUPPORT | ||
| if (nvml_gpu == null) { | ||
| return false; | ||
| } | ||
|
|
||
| nvidia_resources_vram_total = NVCtrl.XNVCTRLQueryTargetAttribute ( | ||
| nvidia_display, | ||
| NV_CTRL_TARGET_TYPE_GPU, | ||
| 0, | ||
| 0, | ||
| NV_CTRL_TOTAL_DEDICATED_GPU_MEMORY, | ||
| &nvidia_memory_vram_total | ||
| ); | ||
|
|
||
| if (!nvidia_resources_vram_total) { | ||
| debug ("Could not query NV_CTRL_TOTAL_DEDICATED_GPU_MEMORY attribute!\n"); | ||
| return; | ||
| if (!monitor_nvml_gpu_sample (nvml_gpu, out sample_cache)) { | ||
| return false; | ||
| } | ||
|
|
||
| nvidia_resources_used = NVCtrl.XNVCTRLQueryTargetStringAttribute ( | ||
| nvidia_display, | ||
| NV_CTRL_TARGET_TYPE_GPU, | ||
| 0, | ||
| 0, | ||
| NV_CTRL_STRING_GPU_UTILIZATION, | ||
| &nvidia_used | ||
| ); | ||
|
|
||
| // var str_used = (string)nvidia_used; | ||
| nvidia_percentage = int.parse (((string) nvidia_used).split_set ("=,")[1]); | ||
| nvidia_fb_percentage = int.parse (((string) nvidia_used).split_set ("=,")[3]); | ||
| debug ("USED_GRAPHICS: %d%\n", nvidia_percentage); | ||
| debug ("USED_FB_MEMORY: %d%\n", nvidia_fb_percentage); | ||
| return true; | ||
| #else | ||
| return false; | ||
| #endif | ||
|
|
||
| if (!nvidia_resources_used) { | ||
| debug ("Could not query NV_CTRL_STRING_GPU_UTILIZATION attribute!\n"); | ||
| return; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private void update_temperature () { | ||
| temperature = nvidia_temperature; | ||
| } | ||
|
|
||
| private void update_memory_vram_used () { | ||
| memory_vram_used = (double) nvidia_memory_vram_used * 1000000.0; | ||
| public void update_temperature () { | ||
| #if NVIDIA_SUPPORT | ||
| if (sample_cache.ok_temp) { | ||
| temperature = (double) sample_cache.temperature_c; | ||
| } else { | ||
| temperature = 0; | ||
| } | ||
| #else | ||
| temperature = 0; | ||
| #endif | ||
| } | ||
|
|
||
| private void update_memory_vram_total () { | ||
| memory_vram_total = (double) nvidia_memory_vram_total * 1000000.0; | ||
| public void update_memory_vram_used () { | ||
| #if NVIDIA_SUPPORT | ||
| if (sample_cache.ok_mem) { | ||
| memory_vram_used = (double) sample_cache.mem_used; | ||
| } else { | ||
| memory_vram_used = 0; | ||
| } | ||
| #else | ||
| memory_vram_used = 0; | ||
| #endif | ||
| } | ||
|
|
||
| private void update_memory_percentage () { | ||
| memory_percentage = (int) (Math.round ((memory_vram_used / memory_vram_total) * 100)); | ||
| public void update_memory_vram_total () { | ||
| #if NVIDIA_SUPPORT | ||
| if (sample_cache.ok_mem) { | ||
| memory_vram_total = (double) sample_cache.mem_total; | ||
| } else { | ||
| memory_vram_total = 0; | ||
| } | ||
| #else | ||
| memory_vram_total = 0; | ||
| #endif | ||
| } | ||
|
|
||
| private void update_fb_percentage () { | ||
| fb_percentage = nvidia_fb_percentage; | ||
| public void update_memory_percentage () { | ||
| if (memory_vram_total > 0) { | ||
| memory_percentage = (int) Math.round ((memory_vram_used / memory_vram_total) * 100.0); | ||
| fb_percentage = memory_percentage; | ||
| } else { | ||
| memory_percentage = 0; | ||
| fb_percentage = 0; | ||
| } | ||
| } | ||
|
|
||
| private void update_percentage () { | ||
| percentage = nvidia_percentage; | ||
| public void update_percentage () { | ||
| #if NVIDIA_SUPPORT | ||
| if (sample_cache.ok_util) { | ||
| percentage = (int) sample_cache.gpu_percent; | ||
| } else { | ||
| percentage = 0; | ||
| } | ||
| #else | ||
| percentage = 0; | ||
| #endif | ||
| } | ||
|
|
||
| public void update () { | ||
| update_nv_resources (); | ||
| bool ok = update_nv_resources (); | ||
|
|
||
| if (!ok) { | ||
| percentage = 0; | ||
| memory_percentage = 0; | ||
| fb_percentage = 0; | ||
| memory_vram_used = 0; | ||
| memory_vram_total = 0; | ||
| temperature = 0; | ||
| return; | ||
| } | ||
|
|
||
| update_temperature (); | ||
| update_memory_vram_used (); | ||
| update_memory_vram_total (); | ||
| update_memory_percentage (); | ||
| update_percentage (); | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why there's a c code? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| #include "nvml-bridge.h" | ||
|
|
||
| #include <nvml.h> | ||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| struct MonitorNvmlGpu { | ||
| nvmlDevice_t handle; | ||
| }; | ||
|
|
||
| static bool monitor_nvml_initialized = false; | ||
|
|
||
| static bool | ||
| monitor_nvml_init_once(void) { | ||
| if (monitor_nvml_initialized) { | ||
| return true; | ||
| } | ||
|
|
||
| nvmlReturn_t ret = nvmlInit_v2(); | ||
| if (ret != NVML_SUCCESS) { | ||
| fprintf(stderr, "NVML init failed: %s\n", nvmlErrorString(ret)); | ||
| return false; | ||
| } | ||
|
|
||
| monitor_nvml_initialized = true; | ||
| return true; | ||
| } | ||
|
|
||
| MonitorNvmlGpu* | ||
| monitor_nvml_gpu_new_from_pci_bus_id(const char* pci_bus_id) { | ||
| if (pci_bus_id == NULL || pci_bus_id[0] == '\0') { | ||
| return NULL; | ||
| } | ||
|
|
||
| if (!monitor_nvml_init_once()) { | ||
| return NULL; | ||
| } | ||
|
|
||
| MonitorNvmlGpu* gpu = calloc(1, sizeof(*gpu)); | ||
| if (gpu == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
||
| nvmlReturn_t ret = nvmlDeviceGetHandleByPciBusId_v2(pci_bus_id, &gpu->handle); | ||
| if (ret != NVML_SUCCESS) { | ||
| fprintf(stderr, "NVML PCI lookup failed for %s: %s\n", pci_bus_id, nvmlErrorString(ret)); | ||
| free(gpu); | ||
| return NULL; | ||
| } | ||
|
|
||
| return gpu; | ||
| } | ||
|
|
||
| void | ||
| monitor_nvml_gpu_destroy(MonitorNvmlGpu* gpu) { | ||
| free(gpu); | ||
| } | ||
|
|
||
| bool | ||
| monitor_nvml_gpu_sample(MonitorNvmlGpu* gpu, MonitorNvmlSample* out) { | ||
| bool any_success = false; | ||
|
|
||
| if (gpu == NULL || out == NULL) { | ||
| return false; | ||
| } | ||
|
|
||
| memset(out, 0, sizeof(*out)); | ||
|
|
||
| nvmlUtilization_t util = {0}; | ||
| if (nvmlDeviceGetUtilizationRates(gpu->handle, &util) == NVML_SUCCESS) { | ||
| out->gpu_percent = util.gpu; | ||
| out->ok_util = true; | ||
| any_success = true; | ||
| } | ||
|
|
||
| nvmlMemory_t memory = {0}; | ||
| if (nvmlDeviceGetMemoryInfo(gpu->handle, &memory) == NVML_SUCCESS) { | ||
| out->mem_used = memory.used; | ||
| out->mem_total = memory.total; | ||
| out->ok_mem = true; | ||
| any_success = true; | ||
| } | ||
|
|
||
| unsigned int temp = 0; | ||
| if (nvmlDeviceGetTemperature(gpu->handle, NVML_TEMPERATURE_GPU, &temp) == NVML_SUCCESS) { | ||
| out->temperature_c = temp; | ||
| out->ok_temp = true; | ||
| any_success = true; | ||
| } | ||
|
|
||
| return any_success; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use previous construction