forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryInfo.cpp
424 lines (360 loc) · 12.6 KB
/
MemoryInfo.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of Natron <https://natrongithub.github.io/>,
* (C) 2018-2021 The Natron developers
* (C) 2013-2018 INRIA and Alexandre Gauthier-Foichat
*
* Natron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Natron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Natron. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
// ***** BEGIN PYTHON BLOCK *****
// from <https://docs.python.org/3/c-api/intro.html#include-files>:
// "Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included."
#include <Python.h>
// ***** END PYTHON BLOCK *****
#include "Engine/MemoryInfo.h"
/*
* Author: David Robert Nadeau
* Site: http://NadeauSoftware.com/
* License: Creative Commons Attribution 3.0 Unported License
* http://creativecommons.org/licenses/by/3.0/deed.en_US
*/
#include <vector>
#include <iostream>
#include <cmath>
#include <algorithm> // min, max
#include <stdexcept>
#include <sstream> // stringstream
#if defined(_WIN32)
# include <windows.h>
# include <psapi.h>
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__ ) )
# include <unistd.h>
# include <sys/resource.h>
# if defined(__APPLE__) && defined(__MACH__)
# include <mach/mach.h>
# include <mach/task.h>
# include <mach/task_info.h>
# include <sys/sysctl.h>
# include <sys/statvfs.h>
# include <stdexcept>
# include <sys/errno.h>
# elif (defined(_AIX) || defined(__TOS__AIX__ ) ) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__ ) ) )
# include <fcntl.h>
# include <procfs.h>
# elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__) || defined(__FreeBSD__)
# include <stdio.h>
# include <unistd.h>
# if defined(__FreeBSD__)
# include <sys/sysctl.h>
# include <sys/types.h>
# else
# include <sys/sysinfo.h>
# endif
# endif
#else
# error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."
#endif
#include <QtCore/QString>
#include <QtCore/QLocale>
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include "Global/GlobalDefines.h"
NATRON_NAMESPACE_ENTER
U64
getSystemTotalRAM()
{
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
// see http://source.winehq.org/git/wine.git/blob/HEAD:/dlls/kernel32/heap.c
uint64_t total;
#ifdef __APPLE__
unsigned int val;
#else
unsigned long val;
#endif
int mib[2];
size_t size_sys;
#ifdef HW_MEMSIZE
uint64_t val64 = 0;
#endif
total = 0;
mib[0] = CTL_HW;
#ifdef HW_MEMSIZE
mib[1] = HW_MEMSIZE;
size_sys = sizeof(val64);
if (!sysctl(mib, 2, &val64, &size_sys, NULL, 0) && size_sys == sizeof(val64) && val64) {
total = val64;
}
#endif
#if defined(__MACH__)
if (!total) {
host_name_port_t host = mach_host_self();
mach_msg_type_number_t count;
host_basic_info_data_t info;
count = HOST_BASIC_INFO_COUNT;
if (host_info(host, HOST_BASIC_INFO, (host_info_t)&info, &count) == KERN_SUCCESS) {
total = info.max_mem;
}
mach_port_deallocate(mach_task_self(), host);
}
#endif
if (!total) {
mib[1] = HW_PHYSMEM;
size_sys = sizeof(val);
if (!sysctl(mib, 2, &val, &size_sys, NULL, 0) && size_sys == sizeof(val) && val) {
total = val;
}
}
return total;
#elif defined(_WIN32)
///On Windows, but not Cygwin, the new GlobalMemoryStatusEx( ) function fills a 64-bit
///safe MEMORYSTATUSEX struct with information about physical and virtual memory. Structure fields include:
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
return status.ullTotalPhys;
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__) || defined(__FreeBSD__)
long pages = sysconf(_SC_PHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
return pages * page_size;
#endif
}
U64
getSystemTotalRAM_conditionnally()
{
if ( isApplication32Bits() ) {
return std::min( (U64)0x100000000ULL, getSystemTotalRAM() );
} else {
return getSystemTotalRAM();
}
}
// prints RAM value as KB, MB or GB
QString
printAsRAM(U64 bytes)
{
// According to the Si standard KB is 1000 bytes, KiB is 1024
// but on windows sizes are calculated by dividing by 1024 so we do what they do.
const U64 kb = 1024;
const U64 mb = 1024 * kb;
const U64 gb = 1024 * mb;
const U64 tb = 1024 * gb;
if (bytes >= tb) {
return QCoreApplication::translate("MemoryInfo", "%1 TiB").arg( QLocale().toString(qreal(bytes) / tb, 'f', 3) );
}
if (bytes >= gb) {
return QCoreApplication::translate("MemoryInfo", "%1 GiB").arg( QLocale().toString(qreal(bytes) / gb, 'f', 2) );
}
if (bytes >= mb) {
return QCoreApplication::translate("MemoryInfo", "%1 MiB").arg( QLocale().toString(qreal(bytes) / mb, 'f', 1) );
}
if (bytes >= kb) {
return QCoreApplication::translate("MemoryInfo", "%1 KiB").arg( QLocale().toString( (uint)(bytes / kb) ) );
}
return QCoreApplication::translate("MemoryInfo", "%1 byte(s)").arg( QLocale().toString( (uint)bytes ) );
}
#if 0 // not used for now
/**
* Returns the peak (maximum so far) resident set size (physical
* memory use) measured in bytes, or zero if the value cannot be
* determined on this OS.
*/
std::size_t
getPeakRSS( )
{
#if defined(_WIN32)
/* Windows -------------------------------------------------- */
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
return (size_t)info.PeakWorkingSetSize;
#elif (defined(_AIX) || defined(__TOS__AIX__ ) ) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__ ) ) )
/* AIX and Solaris ------------------------------------------ */
struct psinfo psinfo;
int fd = -1;
if ( ( fd = open( "/proc/self/psinfo", O_RDONLY ) ) == -1 ) {
return (size_t)0L; /* Can't open? */
}
if ( read( fd, &psinfo, sizeof(psinfo) ) != sizeof(psinfo) ) {
close( fd );
return (size_t)0L; /* Can't read? */
}
close( fd );
return (size_t)(psinfo.pr_rssize * 1024L);
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__ ) )
/* BSD, Linux, and OSX -------------------------------------- */
struct rusage rusage;
getrusage( RUSAGE_SELF, &rusage );
#if defined(__APPLE__) && defined(__MACH__)
return (size_t)rusage.ru_maxrss;
#else
return (size_t)(rusage.ru_maxrss * 1024L);
#endif
#else
/* Unknown OS ----------------------------------------------- */
return (size_t)0L; /* Unsupported. */
#endif
}
#endif // 0
#if 0 // not used for now
/**
* Returns the current resident set size (physical memory use) measured
* in bytes, or zero if the value cannot be determined on this OS.
*/
std::size_t
getCurrentRSS( )
{
#if defined(_WIN32)
/* Windows -------------------------------------------------- */
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
return (size_t)info.WorkingSetSize;
#elif defined(__APPLE__) && defined(__MACH__)
/* OSX ------------------------------------------------------ */
# ifndef MACH_TASK_BASIC_INFO
struct task_basic_info info;
mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
if (task_info( mach_task_self( ), TASK_BASIC_INFO,
(task_info_t)&info, &infoCount ) != KERN_SUCCESS) {
return (size_t)0L; /* Can't access? */
}
# else
struct mach_task_basic_info info;
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
if (task_info( mach_task_self( ), MACH_TASK_BASIC_INFO,
(task_info_t)&info, &infoCount ) != KERN_SUCCESS) {
return (size_t)0L; /* Can't access? */
}
# endif
return (size_t)info.resident_size;
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
/* Linux ---------------------------------------------------- */
long rss = 0L;
FILE* fp = NULL;
if ( ( fp = fopen( "/proc/self/statm", "r" ) ) == NULL ) {
return (size_t)0L; /* Can't open? */
}
if (fscanf( fp, "%*s%ld", &rss ) != 1) {
fclose( fp );
return (size_t)0L; /* Can't read? */
}
fclose( fp );
return (size_t)rss * (size_t)sysconf( _SC_PAGESIZE);
#else
/* AIX, BSD, Solaris, and Unknown OS ------------------------ */
return (size_t)0L; /* Unsupported. */
#endif
} // getCurrentRSS
#endif // 0
std::size_t
getAmountFreePhysicalRAM()
{
#if defined(_WIN32)
///On Windows, but not Cygwin, the new GlobalMemoryStatusEx( ) function fills a 64-bit
///safe MEMORYSTATUSEX struct with information about physical and virtual memory. Structure fields include:
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
return statex.ullAvailPhys;
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
struct sysinfo memInfo;
sysinfo (&memInfo);
long long totalAvailableRAM = memInfo.freeram;
totalAvailableRAM *= memInfo.mem_unit;
return totalAvailableRAM;
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
// and http://source.winehq.org/git/wine.git/blob/HEAD:/dlls/kernel32/heap.c
unsigned long long ullAvailPhys;
unsigned long long ullTotalPhys = 0;
uint64_t total;
#ifdef __APPLE__
unsigned int val;
#else
unsigned long val;
#endif
int mib[2];
size_t size_sys;
#ifdef HW_MEMSIZE
uint64_t val64 = 0;
#endif
#if 0
#ifdef VM_SWAPUSAGE
struct xsw_usage swap;
#endif
#endif
total = 0;
ullAvailPhys = 0;
mib[0] = CTL_HW;
#ifdef HW_MEMSIZE
mib[1] = HW_MEMSIZE;
size_sys = sizeof(val64);
if (!sysctl(mib, 2, &val64, &size_sys, NULL, 0) && size_sys == sizeof(val64) && val64)
total = val64;
#endif
#if defined(__MACH__)
// see http://opensource.apple.com/source/system_cmds/system_cmds-498.2/vm_stat.tproj/vm_stat.c
{
host_name_port_t host = mach_host_self();
mach_msg_type_number_t count;
#ifdef HOST_VM_INFO64_COUNT
vm_size_t page_size;
vm_statistics64_data_t vm_stat;
count = HOST_VM_INFO64_COUNT;
if (host_statistics64(host, HOST_VM_INFO64, (host_info64_t)&vm_stat, &count) == KERN_SUCCESS &&
host_page_size(host, &page_size) == KERN_SUCCESS) {
ullAvailPhys = (vm_stat.free_count + vm_stat.inactive_count) * (unsigned long long)page_size;
}
#endif
if (!total) {
host_basic_info_data_t info;
count = HOST_BASIC_INFO_COUNT;
if (host_info(host, HOST_BASIC_INFO, (host_info_t)&info, &count) == KERN_SUCCESS) {
total = info.max_mem;
}
}
mach_port_deallocate(mach_task_self(), host);
}
#endif
if (!total) {
mib[1] = HW_PHYSMEM;
size_sys = sizeof(val);
if (!sysctl(mib, 2, &val, &size_sys, NULL, 0) && size_sys == sizeof(val) && val)
total = val;
}
if (total) {
ullTotalPhys = total;
}
if (!ullAvailPhys) {
mib[1] = HW_USERMEM;
size_sys = sizeof(val);
if (!sysctl(mib, 2, &val, &size_sys, NULL, 0) && size_sys == sizeof(val) && val)
ullAvailPhys = val;
}
if (!ullAvailPhys) {
ullAvailPhys = ullTotalPhys;
}
#if 0
ullTotalPageFile = ullAvailPhys;
ullAvailPageFile = ullAvailPhys;
#ifdef VM_SWAPUSAGE
mib[0] = CTL_VM;
mib[1] = VM_SWAPUSAGE;
size_sys = sizeof(swap);
if (!sysctl(mib, 2, &swap, &size_sys, NULL, 0) && size_sys == sizeof(swap))
{
lpmemex->ullTotalPageFile = swap.xsu_total;
lpmemex->ullAvailPageFile = swap.xsu_avail;
}
#endif
#endif
return ullAvailPhys;
#endif
}
NATRON_NAMESPACE_EXIT