5 #include <sys/sysinfo.h>
9 #ifdef Q_WS_MAC // Mac includes
10 #include <sys/types.h>
11 #include <sys/sysctl.h>
12 #include <mach/mach.h>
13 #include <mach/vm_statistics.h>
14 #include <mach/mach_types.h>
15 #include <mach/mach_init.h>
16 #include <mach/mach_host.h>
18 #ifndef Q_WS_WIN // Unix and Mac includes
20 #else // Windows includes
31 #ifdef Q_WS_X11 // Unix: based on /proc/$PID/stat
33 QFile f(
"/proc/" + QString::number( getpid() ) +
"/stat" );
34 f.open( QIODevice::ReadOnly );
35 QByteArray ba = f.read( 512 );
38 const static int kk = PAGE_SIZE / 1024;
40 rssnow = QString( ba ).section(
" ", 23, 23 ).toLong() * kk;
43 #ifdef Q_WS_MAC // Mac : use task_info call
44 struct task_basic_info task_stats;
45 mach_msg_type_number_t inf_count = TASK_BASIC_INFO_COUNT;
46 task_t task = current_task();
48 int stat1 = task_info( task, TASK_BASIC_INFO, (task_info_t)&task_stats, &inf_count );
49 if ( stat1 == KERN_SUCCESS )
51 rssnow = ( (long)task_stats.resident_size + 512 ) / 1024;
55 #ifdef Q_WS_WIN // Windows: direct use of GetProcessMemoryInfo
58 PROCESS_MEMORY_COUNTERS pmc;
59 pmc.cb = (DWORD)
sizeof( pmc );
60 processID = _getpid();
62 hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
64 if ( hProcess != NULL )
66 if ( GetProcessMemoryInfo( hProcess, &pmc,
sizeof( pmc ) ) )
68 rssnow = ( (long)pmc.PeakWorkingSetSize + 512 ) / 1024;
71 CloseHandle( hProcess );
81 rssmax = qMax( rssmax,
rss_now() );
93 #ifdef Q_WS_X11 // Unix: use free command
95 qproc.start(
"free", QStringList() <<
"-m" );
96 qproc.waitForFinished( -1 );
97 QString totmem = QString( qproc.readAllStandardOutput() ).trimmed();
98 totmem = totmem.section(
"\n", 1, 1 );
99 totmem.replace( QRegExp(
"\\s+" ),
" " );
100 qDebug() <<
" UsMEM:X11: totmem" << totmem;
101 int fmtotal = totmem.section(
" ", 1, 1 ).toInt();
102 int fmused = totmem.section(
" ", 2, 2 ).toInt();
103 int fmfree = totmem.section(
" ", 3, 3 ).toInt();
104 int fmbuffer = totmem.section(
" ", 5, 5 ).toInt();
105 int fmcache = totmem.section(
" ", 6, 6 ).toInt();
106 qDebug() <<
" UsMEM:X11: fmtotal,used,free,buffer,cache" << fmtotal << fmused
107 << fmfree << fmbuffer << fmcache;
109 memused = qMax( fmused, ( fmtotal - fmfree - fmcache ) );
110 memavail = memtotal - memused;
112 #ifdef Q_WS_MAC // Mac: use sysctl and rss_now()
113 const double mb_bytes = ( 1024. * 1024. );
114 const double kb_bytes = 1024.;
116 qproc.start(
"sysctl", QStringList() <<
"-n" <<
"hw.memsize" );
117 qproc.waitForFinished( -1 );
118 QString totmem = QString( qproc.readAllStandardOutput() ).trimmed();
119 memtotal = qRound( totmem.toDouble() / mb_bytes );
120 memused = qRound( (
double)
rss_now() / kb_bytes );
121 memavail = memtotal - memused;
122 qDebug() <<
" UsMEM:Mac: totmem" << totmem <<
"memtotal" << memtotal;
124 #ifdef Q_WS_WIN // Windows: direct use of GlobalMemoryStatusEx
125 const double mb_bytes = ( 1024. * 1024. );
126 MEMORYSTATUSEX mstatx;
127 mstatx.dwLength =
sizeof( mstatx );
128 GlobalMemoryStatusEx( &mstatx );
129 long memload = (long)mstatx.dwMemoryLoad;
130 memtotal = qRound( (
double)mstatx.ullTotalPhys / mb_bytes );
131 memavail = qRound( (
double)mstatx.ullAvailPhys / mb_bytes );
132 memused = memtotal - memavail;
133 memavpc = qRound( memavail * 100.0 / memtotal );
138 qDebug() <<
" UsMEM:Win: memload" << memload <<
"memavpc" << memavpc;
140 qDebug() <<
" UsMEM: memtotal,avail,used" << memtotal << memavail << memused;
142 memavpc = qRound( memavail * 100.0 / memtotal );
143 if ( pMemA != NULL ) *pMemA = memavail;
144 if ( pMemT != NULL ) *pMemT = memtotal;
145 if ( pMemU != NULL ) *pMemU = memused;