-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
MemoryInformation.cpp
262 lines (261 loc) · 7.94 KB
/
MemoryInformation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//=============================================================================
// Copyright (c) 2016-present Allan CORNET (Nelson)
//=============================================================================
// This file is part of the Nelson.
//=============================================================================
// LICENCE_BLOCK_BEGIN
// SPDX-License-Identifier: LGPL-3.0-or-later
// LICENCE_BLOCK_END
//=============================================================================
#ifdef _MSC_VER
#include <Windows.h>
#include <psapi.h>
#else
#if defined(__APPLE__) || defined(__MACH__)
#include <mach/mach.h>
#include <mach/mach_host.h>
#include <mach/mach_init.h>
#include <mach/mach_types.h>
#include <mach/vm_statistics.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#else
#include <cerrno>
#include <fstream>
#include <iostream>
#include <cstdint>
#include <linux/sysctl.h>
#include <sys/resource.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <unistd.h>
#endif
#endif
#include "MemoryInformation.hpp"
//=============================================================================
namespace Nelson {
#ifdef _MSC_VER
//=============================================================================
double
getTotalVirtualMemory()
{
double res;
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
DWORDLONG totalVirtualMem = memInfo.ullTotalPageFile;
res = static_cast<double>(totalVirtualMem);
return res;
}
//=============================================================================
double
getTotalVirtualMemoryUsed()
{
double res;
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
DWORDLONG totalVirtualMemUsed = memInfo.ullTotalPageFile - memInfo.ullAvailPageFile;
res = static_cast<double>(totalVirtualMemUsed);
return res;
}
//=============================================================================
double
getTotalVirtualMemoryByNelson()
{
double res;
PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(
GetCurrentProcess(), reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), sizeof(pmc));
SIZE_T virtualMemUsedByMe = pmc.PrivateUsage;
res = static_cast<double>(virtualMemUsedByMe);
return res;
}
//=============================================================================
double
getTotalPhysicalMemory()
{
double res;
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
DWORDLONG totalPhysMem = memInfo.ullTotalPhys;
res = static_cast<double>(totalPhysMem);
return res;
}
//=============================================================================
double
getTotalPhysicalMemoryUsed()
{
double res;
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
DWORDLONG physMemUsed = memInfo.ullTotalPhys - memInfo.ullAvailPhys;
res = static_cast<double>(physMemUsed);
return res;
}
//=============================================================================
double
getTotalPhysicalMemoryByNelson()
{
double res;
PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(
GetCurrentProcess(), reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), sizeof(pmc));
SIZE_T physMemUsedByMe = pmc.WorkingSetSize;
res = static_cast<double>(physMemUsedByMe);
return res;
}
//=============================================================================
#else
double
getTotalVirtualMemory()
{
double res = 0;
#if defined(__APPLE__) || defined(__MACH__)
struct statfs stats;
if (0 == statfs("/", &stats)) {
res = (double)((uint64_t)stats.f_bsize * stats.f_bfree);
}
#else
struct sysinfo si;
sysinfo(&si);
res = (double)(si.totalswap);
#endif
return res;
}
//=============================================================================
double
getTotalVirtualMemoryUsed()
{
double res;
#if defined(__APPLE__) || defined(__MACH__)
xsw_usage vmusage = { 0 };
size_t size = sizeof(vmusage);
if (sysctlbyname("vm.swapusage", &vmusage, &size, NULL, 0) != 0) {
res = 0;
} else {
res = (double)vmusage.xsu_used;
}
#else
struct sysinfo si;
sysinfo(&si);
res = (double)(si.totalswap - si.freeswap);
#endif
return res;
}
//=============================================================================
double
getTotalVirtualMemoryByNelson()
{
double res = 0;
#if defined(__APPLE__) || defined(__MACH__)
struct task_basic_info t_info;
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
if (KERN_SUCCESS
!= task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count)) {
return 0;
}
res = (double)t_info.virtual_size;
#else
std::ifstream f;
f.open("/proc/self/stat", std::ifstream::in);
if (f.is_open()) {
int pid;
char name[1024];
char state;
int ppid, pgrp, session, tty, tpgid;
unsigned flags, minflt, cminflt, majflt, cmajflt;
int utime, stime, cutime, cstime, counter, priority, timeout;
unsigned itrealvalue, starttime;
int vsize = 0, rss = 0;
std::cin.width(4096);
f >> pid >> name >> state >> ppid >> pgrp >> session >> tty >> tpgid >> flags;
f >> minflt >> cminflt >> majflt >> cmajflt >> utime >> stime >> cutime;
f >> cstime >> counter >> priority >> timeout >> itrealvalue >> starttime;
f >> vsize >> rss;
res = (double)vsize;
}
#endif
return res;
}
//=============================================================================
double
getTotalPhysicalMemory()
{
double res = 0;
#if defined(__APPLE__) || defined(__MACH__)
unsigned int physmem;
size_t len = sizeof physmem;
static int mib[2] = { CTL_HW, HW_PHYSMEM };
if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0 && len == sizeof(physmem)) {
res = (double)physmem;
}
#else
struct sysinfo si;
sysinfo(&si);
res = (double)(si.totalram);
// res = (double)(sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE));
#endif
return res;
}
//=============================================================================
double
getTotalPhysicalMemoryUsed()
{
double res = 0;
#if defined(__APPLE__) || defined(__MACH__)
vm_size_t page_size;
mach_port_t mach_port;
mach_msg_type_number_t count;
vm_statistics64_data_t vm_stats;
mach_port = mach_host_self();
count = sizeof(vm_stats) / sizeof(natural_t);
if (KERN_SUCCESS == host_page_size(mach_port, &page_size)
&& KERN_SUCCESS
== host_statistics64(mach_port, HOST_VM_INFO, (host_info64_t)&vm_stats, &count)) {
long long used_memory = ((int64_t)vm_stats.active_count + (int64_t)vm_stats.inactive_count
+ (int64_t)vm_stats.wire_count)
* (int64_t)page_size;
res = (double)used_memory;
}
#else
struct sysinfo si;
sysinfo(&si);
res = (double)(si.totalram - si.freeram);
#endif
return res;
}
//=============================================================================
double
getTotalPhysicalMemoryByNelson()
{
double res = 0;
#if defined(__APPLE__) || defined(__MACH__)
struct task_basic_info t_info;
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
if (KERN_SUCCESS
!= task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count)) {
return 0;
}
res = (double)t_info.resident_size;
#else
errno = 0;
struct rusage r_usage;
getrusage(RUSAGE_SELF, &r_usage);
if ((errno == EFAULT) || (errno == EINVAL)) {
res = 0;
} else {
res = (double)r_usage.ru_maxrss * 1024;
}
#endif
return res;
}
//=============================================================================
#endif
} // namespace Nelson
//=============================================================================