9 #include "fwMemory/tools/DarwinMemoryMonitorTools.hpp" 11 #include <fwCore/base.hpp> 13 #include <mach/mach.h> 14 #include <mach/mach_host.h> 15 #include <mach/mach_init.h> 16 #include <mach/mach_types.h> 17 #include <mach/vm_statistics.h> 19 #include <sys/sysctl.h> 20 #include <sys/types.h> 32 DarwinMemoryMonitorTools::DarwinMemoryMonitorTools()
38 DarwinMemoryMonitorTools::~DarwinMemoryMonitorTools()
44 std::uint64_t DarwinMemoryMonitorTools::estimateFreeMem()
46 std::uint64_t freeMemory = 0;
48 freeMemory = getFreeSystemMemory();
51 struct task_basic_info t_info;
52 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
54 if (KERN_SUCCESS != task_info(mach_task_self(),
55 TASK_BASIC_INFO, (task_info_t)&t_info,
58 SLM_ASSERT(
"Failed to retrieve used process memory information", 0);
63 const std::uint64_t maxMemory = 3221225472LL;
64 const std::uint64_t usedProcessMemory = getUsedProcessMemory();
65 freeMemory = std::min( maxMemory - usedProcessMemory, freeMemory);
66 const std::uint64_t maxVMemory = 4294967296LL;
67 freeMemory = std::min( maxVMemory - t_info.virtual_size, freeMemory);
75 void DarwinMemoryMonitorTools::printProcessMemoryInformation()
81 void DarwinMemoryMonitorTools::printSystemMemoryInformation()
87 void DarwinMemoryMonitorTools::printMemoryInformation()
89 printSystemMemoryInformation();
90 printProcessMemoryInformation();
95 std::uint64_t DarwinMemoryMonitorTools::getTotalSystemMemory()
97 static int64_t physical_memory = 0;
99 if (physical_memory == 0)
104 size_t length =
sizeof(int64_t);
105 sysctl(mib, 2, &physical_memory, &length, NULL, 0);
108 return physical_memory;
113 std::uint64_t DarwinMemoryMonitorTools::getUsedSystemMemory()
116 mach_port_t mach_port;
117 mach_msg_type_number_t count;
118 vm_statistics_data_t vm_stats;
120 mach_port = mach_host_self();
121 count =
sizeof(vm_stats) /
sizeof(natural_t);
122 if (KERN_SUCCESS == host_page_size(mach_port, &page_size) &&
123 KERN_SUCCESS == host_statistics(mach_port, HOST_VM_INFO,
124 (host_info_t)&vm_stats, &count))
126 uint64_t used_memory = (
127 (int64_t)vm_stats.active_count +
128 (int64_t)vm_stats.wire_count
129 ) * (int64_t)page_size;
133 SLM_ASSERT(
"Failed to retrieve used system memory information", 0);
139 std::uint64_t DarwinMemoryMonitorTools::getFreeSystemMemory()
142 mach_port_t mach_port;
143 mach_msg_type_number_t count;
144 vm_statistics_data_t vm_stats;
146 mach_port = mach_host_self();
147 count =
sizeof(vm_stats) /
sizeof(natural_t);
148 if (KERN_SUCCESS == host_page_size(mach_port, &page_size) &&
149 KERN_SUCCESS == host_statistics(mach_port, HOST_VM_INFO,
150 (host_info_t)&vm_stats, &count))
152 uint64_t freeMemory = (
153 (int64_t)vm_stats.free_count +
154 (int64_t)vm_stats.inactive_count
155 ) * (int64_t)page_size;
158 SLM_ASSERT(
"Failed to retrieve free system memory information", 0);
164 std::uint64_t DarwinMemoryMonitorTools::getUsedProcessMemory()
166 struct task_basic_info t_info;
167 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
169 if (KERN_SUCCESS != task_info(mach_task_self(),
170 TASK_BASIC_INFO, (task_info_t)&t_info,
173 SLM_ASSERT(
"Failed to retrieve used process memory information", 0);
177 return t_info.resident_size;
The namespace fwMemory contains tools to manage memory. Use for dump.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...