|
29 | 29 | #include <stdio.h> |
30 | 30 | #include <stdlib.h> |
31 | 31 | #include <string.h> |
| 32 | +#include <sys/sysinfo.h> |
32 | 33 |
|
33 | 34 | #define NVML_SUCCESS 0 |
34 | 35 | #define NVML_ERROR_INSUFFICIENT_SIZE 7 |
@@ -607,26 +608,62 @@ static void gpuinfo_nvidia_refresh_dynamic_info(struct gpu_info *_gpu_info) { |
607 | 608 |
|
608 | 609 | // Device memory info (total,used,free) |
609 | 610 | bool got_meminfo = false; |
| 611 | + bool has_unified_memory = false; |
| 612 | + |
610 | 613 | if (nvmlDeviceGetMemoryInfo_v2) { |
611 | 614 | nvmlMemory_v2_t memory_info; |
612 | 615 | memory_info.version = 2; |
613 | 616 | last_nvml_return_status = nvmlDeviceGetMemoryInfo_v2(device, &memory_info); |
614 | 617 | if (last_nvml_return_status == NVML_SUCCESS) { |
615 | | - got_meminfo = true; |
616 | | - SET_GPUINFO_DYNAMIC(dynamic_info, total_memory, memory_info.total); |
617 | | - SET_GPUINFO_DYNAMIC(dynamic_info, used_memory, memory_info.used); |
618 | | - SET_GPUINFO_DYNAMIC(dynamic_info, free_memory, memory_info.free); |
619 | | - SET_GPUINFO_DYNAMIC(dynamic_info, mem_util_rate, memory_info.used * 100 / memory_info.total); |
| 618 | + // Check if this is a unified memory GPU (total == 0 indicates unified memory) |
| 619 | + if (memory_info.total == 0) { |
| 620 | + has_unified_memory = true; |
| 621 | + } else { |
| 622 | + got_meminfo = true; |
| 623 | + SET_GPUINFO_DYNAMIC(dynamic_info, total_memory, memory_info.total); |
| 624 | + SET_GPUINFO_DYNAMIC(dynamic_info, used_memory, memory_info.used); |
| 625 | + SET_GPUINFO_DYNAMIC(dynamic_info, free_memory, memory_info.free); |
| 626 | + SET_GPUINFO_DYNAMIC(dynamic_info, mem_util_rate, memory_info.used * 100 / memory_info.total); |
| 627 | + } |
| 628 | + } else { |
| 629 | + // Memory query failed - likely unified memory GPU (error code 13 = NOT_SUPPORTED) |
| 630 | + has_unified_memory = true; |
620 | 631 | } |
621 | 632 | } |
622 | | - if (!got_meminfo && nvmlDeviceGetMemoryInfo) { |
| 633 | + if (!got_meminfo && !has_unified_memory && nvmlDeviceGetMemoryInfo) { |
623 | 634 | nvmlMemory_v1_t memory_info; |
624 | 635 | last_nvml_return_status = nvmlDeviceGetMemoryInfo(device, &memory_info); |
625 | 636 | if (last_nvml_return_status == NVML_SUCCESS) { |
626 | | - SET_GPUINFO_DYNAMIC(dynamic_info, total_memory, memory_info.total); |
627 | | - SET_GPUINFO_DYNAMIC(dynamic_info, used_memory, memory_info.used); |
628 | | - SET_GPUINFO_DYNAMIC(dynamic_info, free_memory, memory_info.free); |
629 | | - SET_GPUINFO_DYNAMIC(dynamic_info, mem_util_rate, memory_info.used * 100 / memory_info.total); |
| 637 | + // Check if this is a unified memory GPU (total == 0 indicates unified memory) |
| 638 | + if (memory_info.total == 0) { |
| 639 | + has_unified_memory = true; |
| 640 | + } else { |
| 641 | + SET_GPUINFO_DYNAMIC(dynamic_info, total_memory, memory_info.total); |
| 642 | + SET_GPUINFO_DYNAMIC(dynamic_info, used_memory, memory_info.used); |
| 643 | + SET_GPUINFO_DYNAMIC(dynamic_info, free_memory, memory_info.free); |
| 644 | + SET_GPUINFO_DYNAMIC(dynamic_info, mem_util_rate, memory_info.used * 100 / memory_info.total); |
| 645 | + } |
| 646 | + } else { |
| 647 | + // Memory query failed - likely unified memory GPU |
| 648 | + has_unified_memory = true; |
| 649 | + } |
| 650 | + } |
| 651 | + |
| 652 | + // Handle unified memory GPUs - query system memory instead |
| 653 | + if (has_unified_memory) { |
| 654 | + struct sysinfo si; |
| 655 | + if (sysinfo(&si) == 0) { |
| 656 | + // Total system RAM |
| 657 | + unsigned long long total_ram = si.totalram * si.mem_unit; |
| 658 | + unsigned long long free_ram = si.freeram * si.mem_unit; |
| 659 | + unsigned long long used_ram = total_ram - free_ram; |
| 660 | + |
| 661 | + SET_GPUINFO_DYNAMIC(dynamic_info, total_memory, total_ram); |
| 662 | + SET_GPUINFO_DYNAMIC(dynamic_info, used_memory, used_ram); |
| 663 | + SET_GPUINFO_DYNAMIC(dynamic_info, free_memory, free_ram); |
| 664 | + if (total_ram > 0) { |
| 665 | + SET_GPUINFO_DYNAMIC(dynamic_info, mem_util_rate, used_ram * 100 / total_ram); |
| 666 | + } |
630 | 667 | } |
631 | 668 | } |
632 | 669 |
|
|
0 commit comments