15 #if defined(CV_WINDOWS)
19 #elif defined(CV_LINUX)
20 #include <sys/sysinfo.h>
24 #elif defined(CV_MAC_OS)
25 #include <mach/host_info.h>
26 #include <mach/mach_host.h>
27 #include <mach/mach_init.h>
28 #include <mach/mach_types.h>
29 #include <mach/vm_statistics.h>
30 #include <sys/sysctl.h>
31 #include <sys/types.h>
35 #warning "System unrecognized. Can't found memory infos."
45 std::ifstream file(
"/proc/meminfo");
46 while (file >> token) {
47 if (token ==
"MemAvailable:") {
66 #if defined(CV_WINDOWS)
68 GlobalMemoryStatus(&memory);
76 infos.
freeSwap = memory.dwAvailVirtual;
77 #elif defined(CV_LINUX)
78 struct sysinfo sys_info;
81 infos.
totalRam = sys_info.totalram * sys_info.mem_unit;
82 infos.
freeRam = sys_info.freeram * sys_info.mem_unit;
89 infos.
totalSwap = sys_info.totalswap * sys_info.mem_unit;
90 infos.
freeSwap = sys_info.freeswap * sys_info.mem_unit;
91 #elif defined(CV_MAC_OS)
93 size_t len =
sizeof physmem;
94 int mib[2] = {CTL_HW, HW_MEMSIZE};
95 size_t miblen =
sizeof(mib) /
sizeof(mib[0]);
98 if (sysctl(mib, miblen, &physmem, &len,
NULL, 0) == 0 &&
99 len ==
sizeof(physmem))
104 mib[1] = VM_SWAPUSAGE;
105 struct xsw_usage
swap;
106 len =
sizeof(
struct xsw_usage);
107 if (sysctl(mib, miblen, &
swap, &len,
NULL, 0) == 0) {
115 mach_port_t stat_port = mach_host_self();
117 vm_statistics64_data_t vm_stat;
118 mach_msg_type_number_t
count = HOST_VM_INFO64_COUNT;
119 if (KERN_SUCCESS == host_page_size(stat_port, &page_size) &&
120 KERN_SUCCESS == host_statistics64(stat_port, HOST_VM_INFO64,
121 (host_info64_t)&vm_stat, &
count)) {
123 infos.
freeRam = (int64_t)vm_stat.free_count * (int64_t)page_size;
131 uint64_t app_memory = ((uint64_t)vm_stat.active_count +
132 (uint64_t)vm_stat.wire_count) *
147 const double convertionGb = std::pow(2, 30);
149 <<
"\t- Total RAM: " << (infos.
totalRam / convertionGb) <<
" GB"
151 <<
"\t- Free RAM: " << (infos.
freeRam / convertionGb) <<
" GB"
153 <<
"\t- Available RAM: " << (infos.
availableRam / convertionGb) <<
" GB"
155 <<
"\t- Total swap: " << (infos.
totalSwap / convertionGb) <<
" GB"
157 <<
"\t- Free swap: " << (infos.
freeSwap / convertionGb) <<
" GB"
163 std::stringstream info;
QTextStream & endl(QTextStream &stream)
MemoryInfo getMemoryInfo()
unsigned long linuxGetAvailableRam()
std::ostream & operator<<(std::ostream &os, const MemoryInfo &infos)
void swap(optional< T > &x, optional< T > &y) noexcept(noexcept(x.swap(y)))
Generic file read and write utility for python interface.