1 // Created on: 2011-10-05
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #if (defined(_WIN32) || defined(__WIN32__))
23 #pragma comment(lib, "Psapi.lib")
25 #elif (defined(__APPLE__))
26 #include <mach/task.h>
27 #include <mach/mach.h>
28 #include <malloc/malloc.h>
38 #include <OSD_MemInfo.hxx>
40 // =======================================================================
41 // function : OSD_MemInfo
43 // =======================================================================
44 OSD_MemInfo::OSD_MemInfo()
49 // =======================================================================
52 // =======================================================================
53 void OSD_MemInfo::Update()
56 for (Standard_Integer anIter = 0; anIter < MemCounter_NB; ++anIter)
58 myCounters[anIter] = Standard_Size(-1);
61 #if (defined(_WIN32) || defined(__WIN32__))
62 MEMORYSTATUSEX aStatEx;
63 aStatEx.dwLength = sizeof(aStatEx);
64 GlobalMemoryStatusEx (&aStatEx);
65 myCounters[MemVirtual] = Standard_Size(aStatEx.ullTotalVirtual - aStatEx.ullAvailVirtual);
68 HANDLE aProcess = GetCurrentProcess();
69 #if (_WIN32_WINNT >= 0x0501)
70 PROCESS_MEMORY_COUNTERS_EX aProcMemCnts;
72 PROCESS_MEMORY_COUNTERS aProcMemCnts;
74 if (GetProcessMemoryInfo (aProcess, (PROCESS_MEMORY_COUNTERS* )&aProcMemCnts, sizeof(aProcMemCnts)))
76 #if (_WIN32_WINNT >= 0x0501)
77 myCounters[MemPrivate] = aProcMemCnts.PrivateUsage;
79 myCounters[MemWorkingSet] = aProcMemCnts.WorkingSetSize;
80 myCounters[MemWorkingSetPeak] = aProcMemCnts.PeakWorkingSetSize;
81 myCounters[MemSwapUsage] = aProcMemCnts.PagefileUsage;
82 myCounters[MemSwapUsagePeak] = aProcMemCnts.PeakPagefileUsage;
89 myCounters[MemHeapUsage] = 0;
90 while((heapstatus = _heapwalk(&hinfo)) == _HEAPOK)
92 if(hinfo._useflag == _USEDENTRY)
93 myCounters[MemHeapUsage] += hinfo._size;
96 #elif (defined(__linux__) || defined(__linux))
97 // use procfs on Linux
99 snprintf (aBuff, sizeof(aBuff), "/proc/%d/status", getpid());
102 if (!aFile.is_open())
109 memset (aBuff, 0, sizeof(aBuff));
110 aFile.getline (aBuff, 4096);
111 if (aBuff[0] == '\0')
116 if (strncmp (aBuff, "VmSize:", strlen ("VmSize:")) == 0)
118 myCounters[MemVirtual] = atol (aBuff + strlen ("VmSize:")) * 1024;
120 //else if (strncmp (aBuff, "VmPeak:", strlen ("VmPeak:")) == 0)
121 // myVirtualPeak = atol (aBuff + strlen ("VmPeak:")) * 1024;
122 else if (strncmp (aBuff, "VmRSS:", strlen ("VmRSS:")) == 0)
124 myCounters[MemWorkingSet] = atol (aBuff + strlen ("VmRSS:")) * 1024; // RSS - resident set size
126 else if (strncmp (aBuff, "VmHWM:", strlen ("VmHWM:")) == 0)
128 myCounters[MemWorkingSetPeak] = atol (aBuff + strlen ("VmHWM:")) * 1024; // HWM - high water mark
130 else if (strncmp (aBuff, "VmData:", strlen ("VmData:")) == 0)
132 if (myCounters[MemPrivate] == Standard_Size(-1)) ++myCounters[MemPrivate];
133 myCounters[MemPrivate] += atol (aBuff + strlen ("VmData:")) * 1024;
135 else if (strncmp (aBuff, "VmStk:", strlen ("VmStk:")) == 0)
137 if (myCounters[MemPrivate] == Standard_Size(-1)) ++myCounters[MemPrivate];
138 myCounters[MemPrivate] += atol (aBuff + strlen ("VmStk:")) * 1024;
143 struct mallinfo aMI = mallinfo();
144 myCounters[MemHeapUsage] = aMI.uordblks;
146 #elif (defined(__APPLE__))
147 struct task_basic_info aTaskInfo;
148 mach_msg_type_number_t aTaskInfoCount = TASK_BASIC_INFO_COUNT;
149 if (task_info (mach_task_self(), TASK_BASIC_INFO,
150 (task_info_t )&aTaskInfo, &aTaskInfoCount) == KERN_SUCCESS)
152 // On Mac OS X, these values in bytes, not pages!
153 myCounters[MemVirtual] = aTaskInfo.virtual_size;
154 myCounters[MemWorkingSet] = aTaskInfo.resident_size;
156 //Getting malloc statistics
157 malloc_statistics_t aStats;
158 malloc_zone_statistics (NULL, &aStats);
160 myCounters[MemHeapUsage] = aStats.size_in_use;
165 // =======================================================================
166 // function : ToString
168 // =======================================================================
169 TCollection_AsciiString OSD_MemInfo::ToString() const
171 TCollection_AsciiString anInfo;
172 if (myCounters[MemPrivate] != Standard_Size(-1))
174 anInfo += TCollection_AsciiString(" Private memory: ") + Standard_Integer (ValueMiB (MemPrivate)) + " MiB\n";
176 if (myCounters[MemWorkingSet] != Standard_Size(-1))
178 anInfo += TCollection_AsciiString(" Working Set: ") + Standard_Integer (ValueMiB (MemWorkingSet)) + " MiB";
179 if (myCounters[MemWorkingSetPeak] != Standard_Size(-1))
181 anInfo += TCollection_AsciiString(" (peak: ") + Standard_Integer (ValueMiB (MemWorkingSetPeak)) + " MiB)";
185 if (myCounters[MemSwapUsage] != Standard_Size(-1))
187 anInfo += TCollection_AsciiString(" Pagefile usage: ") + Standard_Integer (ValueMiB (MemSwapUsage)) + " MiB";
188 if (myCounters[MemSwapUsagePeak] != Standard_Size(-1))
190 anInfo += TCollection_AsciiString(" (peak: ") + Standard_Integer (ValueMiB (MemSwapUsagePeak)) + " MiB)";
194 if (myCounters[MemVirtual] != Standard_Size(-1))
196 anInfo += TCollection_AsciiString(" Virtual memory: ") + Standard_Integer (ValueMiB (MemVirtual)) + " MiB\n";
198 if (myCounters[MemHeapUsage] != Standard_Size(-1))
200 anInfo += TCollection_AsciiString(" Heap memory: ") + Standard_Integer (ValueMiB (MemHeapUsage)) + " MiB\n";
205 // =======================================================================
208 // =======================================================================
209 Standard_Size OSD_MemInfo::Value (const OSD_MemInfo::Counter theCounter) const
211 if (theCounter < 0 || theCounter >= MemCounter_NB)
213 return Standard_Size(-1);
215 return myCounters[theCounter];
218 // =======================================================================
219 // function : ValueMiB
221 // =======================================================================
222 Standard_Size OSD_MemInfo::ValueMiB (const OSD_MemInfo::Counter theCounter) const
224 if (theCounter < 0 || theCounter >= MemCounter_NB)
226 return Standard_Size(-1);
228 return (myCounters[theCounter] == Standard_Size(-1))
229 ? Standard_Size(-1) : (myCounters[theCounter] / (1024 * 1024));
232 // =======================================================================
233 // function : ShowInfo
235 // =======================================================================
236 TCollection_AsciiString OSD_MemInfo::PrintInfo()
239 return anInfo.ToString();