blob: 386c65cd4927c91d74afaa952781e5a032aec343 (
plain) (
blame)
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
|
#include "memoryusage.h"
#include <QtGlobal>
#ifdef Q_OS_LINUX
#include <QFile>
#include <QRegularExpression>
#else
#ifdef Q_OS_WIN32
#include "windows.h"
#include "psapi.h"
#else
#ifdef Q_OS_MAC
#include <mach/mach.h>
#endif // Q_OS_MAC
#endif // Q_OS_WIN32
#endif // Q_OS_LINUX
#ifdef Q_OS_LINUX
int getMemoryUsage()
{
static const QRegularExpression re("VmSize\\:\\s+(\\d+)\\s+(\\w+)");
QFile file("/proc/self/status");
if (!file.open(QIODevice::ReadOnly))
return -1;
QString contents = file.readAll();
QRegularExpressionMatch match = re.match(contents);
if (!match.hasMatch())
return -1;
bool ok;
int result = match.captured(1).toInt(&ok);
if (!ok)
return -1;
QString unit = match.captured(2).toLower();
if (unit == "mb")
return result * 1024 * 1024;
if (unit == "kb")
return result * 1024;
return result;
}
#else
#ifdef Q_OS_WIN32
int getMemoryUsage()
{
PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
return pmc.PrivateUsage;
}
#else
#ifdef Q_OS_MAC
int getMemoryUsage()
{
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 -1;
return t_info.virtual_size;
}
#else
int getMemoryUsage()
{
return -1;
}
#endif // Q_OS_MAC
#endif // Q_OS_WIN32
#endif // Q_OS_LINUX
|