|
8 | 8 | #define PokemonAutomation_MemoryUtilization_Mac_TPP |
9 | 9 |
|
10 | 10 | #include <iostream> |
11 | | -#include <unistd.h> // For getpid() |
12 | 11 | #include <sys/types.h> |
13 | 12 | #include <sys/sysctl.h> |
14 | | -#include <sys/proc_info.h> |
15 | | -#include <libproc.h> |
16 | 13 | #include <mach/mach.h> |
17 | 14 | #include <mach/mach_host.h> |
18 | 15 | #include <mach/mach_vm.h> |
| 16 | +#include <mach/task_info.h> |
19 | 17 | #include <mach/vm_statistics.h> |
20 | 18 |
|
21 | 19 | namespace PokemonAutomation{ |
@@ -85,38 +83,38 @@ void analyze_vm_regions() { |
85 | 83 |
|
86 | 84 | MemoryUsage process_memory_usage(){ |
87 | 85 | MemoryUsage usage; |
88 | | - |
89 | | - pid_t pid = getpid(); |
90 | | - struct proc_taskinfo task_info; |
91 | | - int ret = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &task_info, sizeof(task_info)); |
92 | | - |
93 | | - if (ret <= 0) { |
94 | | - std::cerr << "Error getting process info for PID " << pid << ": " << strerror(errno) << std::endl; |
95 | | - }else{ |
96 | | - usage.process_physical_memory = task_info.pti_resident_size; |
97 | | - usage.process_virtual_memory = task_info.pti_virtual_size; |
| 86 | + static int64_t physical_memory = 0; |
| 87 | + if (physical_memory == 0) { |
| 88 | + int mib[] = {CTL_HW, HW_MEMSIZE}; |
| 89 | + size_t length = sizeof(physical_memory); |
| 90 | + if (sysctl(mib, 2, &physical_memory, &length, NULL, 0) != 0) { |
| 91 | + std::cerr << "Error calling sysctl()." << std::endl; |
| 92 | + } |
98 | 93 | } |
99 | | - |
100 | | - int mib[] = {CTL_HW, HW_MEMSIZE}; |
101 | | - int64_t physical_memory = 0; |
102 | | - size_t length = sizeof(physical_memory); |
103 | | - |
104 | | - if (sysctl(mib, 2, &physical_memory, &length, NULL, 0) != 0) { |
105 | | - std::cerr << "Error calling sysctl()." << std::endl; |
106 | | - } else{ |
107 | | - usage.total_system_memory = physical_memory; |
| 94 | + usage.total_system_memory = physical_memory; |
| 95 | + { |
| 96 | + task_vm_info_data_t tvi; |
| 97 | + mach_msg_type_number_t count = TASK_VM_INFO_COUNT; |
| 98 | + if(KERN_SUCCESS == task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &tvi, &count)) |
| 99 | + { |
| 100 | + usage.process_physical_memory = tvi.resident_size; // resident_size = internal + external + reusable |
| 101 | + usage.process_virtual_memory = tvi.virtual_size; |
| 102 | + } else { |
| 103 | + std::cerr << "Failed to get task info." << std::endl; |
| 104 | + } |
108 | 105 | } |
109 | | - |
110 | | - vm_statistics_data_t vm_stats; |
111 | | - mach_msg_type_number_t count = HOST_VM_INFO_COUNT; |
112 | | - if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_stats, &count) != 0) { |
113 | | - std::cerr << "Failed to get host statistics." << std::endl; |
114 | | - } else { |
115 | | - // Calculate used memory from vm_statistics |
116 | | - // Used = active_count + wire_count |
117 | | - // Cached = inactive_count |
118 | | - // Buffered = purgeable_count |
119 | | - usage.total_used_system_memory = (size_t)(vm_stats.active_count + vm_stats.wire_count) * (size_t)vm_page_size; |
| 106 | + { |
| 107 | + vm_statistics_data_t vm_stats; |
| 108 | + mach_msg_type_number_t count = HOST_VM_INFO_COUNT; |
| 109 | + if (KERN_SUCCESS == host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_stats, &count)) { |
| 110 | + // Calculate used memory from vm_statistics |
| 111 | + // Used = active_count + wire_count |
| 112 | + // Cached = inactive_count |
| 113 | + // Buffered = purgeable_count |
| 114 | + usage.total_used_system_memory = (size_t)(vm_stats.active_count + vm_stats.wire_count) * (size_t)vm_page_size; |
| 115 | + } else { |
| 116 | + std::cerr << "Failed to get host statistics." << std::endl; |
| 117 | + } |
120 | 118 | } |
121 | 119 | return usage; |
122 | 120 | } |
|
0 commit comments