11#include < iostream>
22
33#if defined(__linux) || defined(__APPLE__)
4- // get process memory and total system memory for Linux and macOS
4+ // get process phsyical memory for Linux and macOS
55#include < libproc.h>
66#include < sys/proc_info.h>
77#include < unistd.h> // For getpid()
88#endif
99
1010#if defined(__APPLE__)
11- // get total used system memory for macOS
11+ // get system memory and process virutal memory for macOS
12+ #include < sys/sysctl.h>
1213#include < mach/mach_host.h>
1314#include < mach/vm_statistics.h>
15+ #include < mach/mach.h>
16+ #include < mach/mach_vm.h>
17+ #include < sys/types.h>
1418#endif
1519
1620#include " MemoryUtilization.h"
@@ -19,8 +23,63 @@ namespace PokemonAutomation{
1923
2024
2125
22- void print_macos_total_used_memory () {
26+ /* *
27+ * @brief Retrieves and analyzes the virtual memory regions of the current process.
28+ */
29+ void analyze_vm_regions () {
30+ kern_return_t kr;
31+ mach_port_t task = mach_task_self ();
2332
33+ mach_vm_address_t address = 0 ;
34+ mach_vm_size_t size = 0 ;
35+
36+ vm_region_basic_info_data_64_t info;
37+ mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
38+
39+ mach_port_t object_name;
40+
41+ // Total virtual memory usage
42+ mach_vm_size_t total_size = 0 ;
43+
44+ std::cout << " Analyzing virtual memory regions for current process..." << std::endl;
45+
46+ // Loop through all virtual memory regions
47+ while (true ) {
48+ kr = mach_vm_region (task, &address, &size, VM_REGION_BASIC_INFO_64, (vm_region_info_t )&info, &info_count, &object_name);
49+
50+ if (kr == KERN_INVALID_ADDRESS) {
51+ // Reached the end of the address space
52+ break ;
53+ } else if (kr != KERN_SUCCESS) {
54+ std::cerr << " mach_vm_region failed with error: " << kr << std::endl;
55+ break ;
56+ }
57+
58+ // Add region size to total
59+ total_size += size;
60+
61+ // Print details of the current region
62+ std::cout << " Region at address " << (void *)address
63+ << " , size " << size << " bytes"
64+ << " , protection: " ;
65+
66+ if (info.protection & VM_PROT_READ) {
67+ std::cout << " R" ;
68+ }
69+ if (info.protection & VM_PROT_WRITE) {
70+ std::cout << " W" ;
71+ }
72+ if (info.protection & VM_PROT_EXECUTE) {
73+ std::cout << " X" ;
74+ }
75+ std::cout << std::endl;
76+
77+ // Move to the next region
78+ address += size;
79+ }
80+
81+ std::cout << " \n Total virtual memory allocated: " << total_size << " bytes" << std::endl;
82+ std::cout << " Approximately " << (double )total_size / 1048576.0 << " MB" << std::endl;
2483}
2584
2685
@@ -34,14 +93,22 @@ MemoryUsage process_memory_usage(){
3493
3594 if (ret <= 0 ) {
3695 std::cerr << " Error getting process info for PID " << pid << " : " << strerror (errno) << std::endl;
37- return usage;
96+ }else {
97+ usage.process_physical_memory = task_info.pti_resident_size ;
3898 }
99+
100+ #if defined(__APPLE__) // compute rest of the memory info on macOS
101+
102+ int mib[] = {CTL_HW, HW_MEMSIZE};
103+ int64_t physical_memory = 0 ;
104+ size_t length = sizeof (physical_memory);
39105
40- usage.process_physical_memory = task_info.pti_resident_size ;
41- usage.process_virtual_memory = task_info.pti_virtual_size ;
42- usage.total_system_memory = task_info.pti_total_system ;
106+ if (sysctl (mib, 2 , &physical_memory, &length, NULL , 0 ) != 0 ) {
107+ std::cerr << " Error calling sysctl()." << std::endl;
108+ } else {
109+ usage.total_system_memory = physical_memory;
110+ }
43111
44- #if defined(__APPLE__) // compute total used system memory on macOS
45112 vm_size_t page_size;
46113 vm_statistics_data_t vm_stats;
47114 mach_port_t mach_port = mach_host_self ();
@@ -50,18 +117,19 @@ MemoryUsage process_memory_usage(){
50117 // Get the host statistics
51118 if (KERN_SUCCESS != host_statistics (mach_port, HOST_VM_INFO, (host_info_t )&vm_stats, &count)) {
52119 std::cerr << " Failed to get host statistics." << std::endl;
53- return usage;
54- }
55- // Get the system's page size
56- host_page_size (mach_port, &page_size);
120+ } else {
121+ // Get the system's page size
122+ host_page_size (mach_port, &page_size);
57123
58- // Calculate used memory from vm_statistics
59- // Used memory = Wired + Active + Inactive
60- size_t wired_pages = vm_stats.wire_count ;
61- size_t active_pages = vm_stats.active_count ;
62- size_t inactive_pages = vm_stats.inactive_count ;
124+ // Calculate used memory from vm_statistics
125+ // Used memory = Wired + Active + Inactive
126+ size_t wired_pages = vm_stats.wire_count ;
127+ size_t active_pages = vm_stats.active_count ;
128+ size_t inactive_pages = vm_stats.inactive_count ;
63129
64- usage.total_used_system_memory = (wired_pages + active_pages + inactive_pages) * page_size;
130+ usage.total_used_system_memory = (wired_pages + active_pages + inactive_pages) * page_size;
131+ }
132+
65133#else // compute total used system memory on Linux
66134 // TODO
67135#endif
0 commit comments