38
38
#include <vm/vm_param.h>
44
namespace MemoryUtils {
46
47
// This code has been copied from okular/core/document.cpp
47
qulonglong getTotalMemory() {
48
qulonglong getTotalMemory()
48
50
static qulonglong cachedValue = 0;
50
52
return cachedValue;
52
54
#if defined(Q_OS_LINUX)
53
55
// if /proc/meminfo doesn't exist, return 128MB
54
QFile memFile( "/proc/meminfo" );
55
if ( !memFile.open( QIODevice::ReadOnly ) )
56
QFile memFile("/proc/meminfo");
57
if (!memFile.open(QIODevice::ReadOnly))
56
58
return (cachedValue = 134217728);
58
60
// read /proc/meminfo and sum up the contents of 'MemFree', 'Buffers'
59
61
// and 'Cached' fields. consider swapped memory as used memory.
60
QTextStream readStream( &memFile );
62
QTextStream readStream(&memFile);
63
64
QString entry = readStream.readLine();
64
if ( entry.isNull() ) break;
65
if ( entry.startsWith( "MemTotal:" ) )
66
return (cachedValue = (Q_UINT64_C(1024) * entry.section( ' ', -2, -2 ).toULongLong()));
65
if (entry.isNull()) break;
66
if (entry.startsWith("MemTotal:"))
67
return (cachedValue = (Q_UINT64_C(1024) * entry.section(' ', -2, -2).toULongLong()));
68
69
#elif defined(Q_OS_FREEBSD)
69
70
qulonglong physmem;
70
71
int mib[] = {CTL_HW, HW_PHYSMEM};
71
size_t len = sizeof( physmem );
72
if ( sysctl( mib, 2, &physmem, &len, NULL, 0 ) == 0 )
72
size_t len = sizeof(physmem);
73
if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
73
74
return (cachedValue = physmem);
74
75
#elif defined(Q_OS_WIN)
75
76
MEMORYSTATUSEX stat;
76
77
stat.dwLength = sizeof(stat);
77
GlobalMemoryStatusEx (&stat);
78
GlobalMemoryStatusEx(&stat);
79
return ( cachedValue = stat.ullTotalPhys );
80
return (cachedValue = stat.ullTotalPhys);
81
82
return (cachedValue = 134217728);
85
qulonglong getFreeMemory() {
85
qulonglong getFreeMemory()
86
87
static QTime lastUpdate = QTime::currentTime();
87
88
static qulonglong cachedValue = 0;
89
if ( lastUpdate.secsTo( QTime::currentTime() ) <= 2 )
90
if (lastUpdate.secsTo(QTime::currentTime()) <= 2)
90
91
return cachedValue;
92
93
#if defined(Q_OS_LINUX)
93
94
// if /proc/meminfo doesn't exist, return MEMORY FULL
94
QFile memFile( "/proc/meminfo" );
95
if ( !memFile.open( QIODevice::ReadOnly ) )
95
QFile memFile("/proc/meminfo");
96
if (!memFile.open(QIODevice::ReadOnly))
98
99
// read /proc/meminfo and sum up the contents of 'MemFree', 'Buffers'
99
100
// and 'Cached' fields. consider swapped memory as used memory.
100
101
qulonglong memoryFree = 0;
102
QTextStream readStream( &memFile );
103
QTextStream readStream(&memFile);
105
105
entry = readStream.readLine();
106
if ( entry.isNull() ) break;
107
if ( entry.startsWith( "MemFree:" ) ||
108
entry.startsWith( "Buffers:" ) ||
109
entry.startsWith( "Cached:" ) ||
110
entry.startsWith( "SwapFree:" ) )
111
memoryFree += entry.section( ' ', -2, -2 ).toULongLong();
112
if ( entry.startsWith( "SwapTotal:" ) )
113
memoryFree -= entry.section( ' ', -2, -2 ).toULongLong();
106
if (entry.isNull()) break;
107
if (entry.startsWith("MemFree:") ||
108
entry.startsWith("Buffers:") ||
109
entry.startsWith("Cached:") ||
110
entry.startsWith("SwapFree:"))
111
memoryFree += entry.section(' ', -2, -2).toULongLong();
112
if (entry.startsWith("SwapTotal:"))
113
memoryFree -= entry.section(' ', -2, -2).toULongLong();
117
117
lastUpdate = QTime::currentTime();
119
return ( cachedValue = (Q_UINT64_C(1024) * memoryFree) );
119
return (cachedValue = (Q_UINT64_C(1024) * memoryFree));
120
120
#elif defined(Q_OS_FREEBSD)
121
121
qulonglong cache, inact, free, psize;
122
122
size_t cachelen, inactlen, freelen, psizelen;
123
cachelen = sizeof( cache );
124
inactlen = sizeof( inact );
125
freelen = sizeof( free );
126
psizelen = sizeof( psize );
123
cachelen = sizeof(cache);
124
inactlen = sizeof(inact);
125
freelen = sizeof(free);
126
psizelen = sizeof(psize);
127
127
// sum up inactive, cached and free memory
128
if ( sysctlbyname( "vm.stats.vm.v_cache_count", &cache, &cachelen, NULL, 0 ) == 0 &&
129
sysctlbyname( "vm.stats.vm.v_inactive_count", &inact, &inactlen, NULL, 0 ) == 0 &&
130
sysctlbyname( "vm.stats.vm.v_free_count", &free, &freelen, NULL, 0 ) == 0 &&
131
sysctlbyname( "vm.stats.vm.v_page_size", &psize, &psizelen, NULL, 0 ) == 0 )
128
if (sysctlbyname("vm.stats.vm.v_cache_count", &cache, &cachelen, NULL, 0) == 0 &&
129
sysctlbyname("vm.stats.vm.v_inactive_count", &inact, &inactlen, NULL, 0) == 0 &&
130
sysctlbyname("vm.stats.vm.v_free_count", &free, &freelen, NULL, 0) == 0 &&
131
sysctlbyname("vm.stats.vm.v_page_size", &psize, &psizelen, NULL, 0) == 0) {
133
132
lastUpdate = QTime::currentTime();
134
133
return (cachedValue = (cache + inact + free) * psize);
140
137
#elif defined(Q_OS_WIN)
141
138
MEMORYSTATUSEX stat;
142
139
stat.dwLength = sizeof(stat);
143
GlobalMemoryStatusEx (&stat);
140
GlobalMemoryStatusEx(&stat);
145
142
lastUpdate = QTime::currentTime();
147
return ( cachedValue = stat.ullAvailPhys );
144
return (cachedValue = stat.ullAvailPhys);
149
146
// tell the memory is full.. will act as in LOW profile