1+ #include < iostream>
2+
3+ #if defined(__linux) || defined(__APPLE__)
4+ // get process memory and total system memory for Linux and macOS
5+ #include < libproc.h>
6+ #include < sys/proc_info.h>
7+ #include < unistd.h> // For getpid()
8+ #endif
9+
10+ #if defined(__APPLE__)
11+ // get total used system memory for macOS
12+ #include < mach/mach_host.h>
13+ #include < mach/vm_statistics.h>
14+ #endif
15+
16+ #include " MemoryUtilization.h"
17+
18+ namespace PokemonAutomation {
19+
20+
21+
22+ void print_macos_total_used_memory () {
23+
24+ }
25+
26+
27+ MemoryUsage process_memory_usage (){
28+ MemoryUsage usage;
29+
30+ #if defined(__linux) || defined(__APPLE__)
31+ pid_t pid = getpid ();
32+ struct proc_taskinfo task_info;
33+ int ret = proc_pidinfo (pid, PROC_PIDTASKINFO, 0 , &task_info, sizeof (task_info));
34+
35+ if (ret <= 0 ) {
36+ std::cerr << " Error getting process info for PID " << pid << " : " << strerror (errno) << std::endl;
37+ return usage;
38+ }
39+
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 ;
43+
44+ #if defined(__APPLE__) // compute total used system memory on macOS
45+ vm_size_t page_size;
46+ vm_statistics_data_t vm_stats;
47+ mach_port_t mach_port = mach_host_self ();
48+ mach_msg_type_number_t count = sizeof (vm_stats) / sizeof (integer_t );
49+
50+ // Get the host statistics
51+ if (KERN_SUCCESS != host_statistics (mach_port, HOST_VM_INFO, (host_info_t )&vm_stats, &count)) {
52+ 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);
57+
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 ;
63+
64+ usage.total_used_system_memory = (wired_pages + active_pages + inactive_pages) * page_size;
65+ #else // compute total used system memory on Linux
66+ // TODO
67+ #endif
68+
69+ #else // compute memory info on Windows
70+ // TODO
71+ #endif
72+ return usage;
73+ }
74+
75+
76+ }
0 commit comments