Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
MemoryUsage.h
1 /*
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12 
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28 
29 #ifndef MEMORY_USAGE_INCLUDED
30 #define MEMORY_USAGE_INCLUDED
31 
32 #ifdef WIN32
33 
34 #include <Windows.h>
35 class MemoryInfo{
36 public:
37  size_t TotalPhysicalMemory;
38  size_t FreePhysicalMemory;
39  size_t TotalSwapSpace;
40  size_t FreeSwapSpace;
41  size_t TotalVirtualAddressSpace;
42  size_t FreeVirtualAddressSpace;
43  size_t PageSize;
44 
45  void set(void){
46  MEMORYSTATUSEX Mem;
47  SYSTEM_INFO Info;
48  ZeroMemory( &Mem, sizeof(Mem));
49  ZeroMemory( &Info, sizeof(Info));
50  Mem.dwLength = sizeof(Mem);
51  ::GlobalMemoryStatusEx( &Mem );
52  ::GetSystemInfo( &Info );
53 
54  TotalPhysicalMemory = (size_t)Mem.ullTotalPhys;
55  FreePhysicalMemory = (size_t)Mem.ullAvailPhys;
56  TotalSwapSpace = (size_t)Mem.ullTotalPageFile;
57  FreeSwapSpace = (size_t)Mem.ullAvailPageFile;
58  TotalVirtualAddressSpace = (size_t)Mem.ullTotalVirtual;
59  FreeVirtualAddressSpace = (size_t)Mem.ullAvailVirtual;
60  PageSize = (size_t)Info.dwPageSize;
61  }
62  size_t usage(void) const {return TotalVirtualAddressSpace-FreeVirtualAddressSpace;}
63 
64  static size_t Usage(void){
65  MEMORY_BASIC_INFORMATION mbi;
66  size_t dwMemUsed = 0;
67  PVOID pvAddress = 0;
68 
69 
70  memset(&mbi, 0, sizeof(MEMORY_BASIC_INFORMATION));
71  while(VirtualQuery(pvAddress, &mbi, sizeof(MEMORY_BASIC_INFORMATION)) == sizeof(MEMORY_BASIC_INFORMATION)){
72  if(mbi.State == MEM_COMMIT && mbi.Type == MEM_PRIVATE){dwMemUsed += mbi.RegionSize;}
73  pvAddress = ((BYTE*)mbi.BaseAddress) + mbi.RegionSize;
74  }
75  return dwMemUsed;
76  }
77 };
78 
79 #else // !WIN32
80 
81 #ifndef __APPLE__ // Linux variants
82 
83 #include <sys/time.h>
84 #include <sys/resource.h>
85 
87 {
88  public:
89  static size_t Usage(void)
90  {
91  FILE* f = fopen("/proc/self/stat","rb");
92 
93  int d;
94  long ld;
95  unsigned long lu;
96  unsigned long long llu;
97  char tmp[256];
98  char* s = &tmp[0];
99  char c;
100 
101  int pid;
102  unsigned long vm;
103 
104  fscanf(f, "%d %255s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %d %ld %llu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu"
105  ,&pid ,s ,&c ,&d ,&d ,&d ,&d ,&d ,&lu ,&lu ,&lu ,&lu ,&lu ,&lu ,&lu ,&ld ,&ld ,&ld ,&ld ,&d ,&ld ,&llu ,&vm ,&ld ,&lu ,&lu ,&lu ,&lu ,&lu ,&lu ,&lu ,&lu ,&lu ,&lu ,&lu ,&lu ,&lu ,&d ,&d ,&lu ,&lu );
106 
107  fclose(f);
108 /*
109 pid %d
110 comm %s
111 state %c
112 ppid %d
113 pgrp %d
114 session %d
115 tty_nr %d
116 tpgid %d
117 flags %lu
118 minflt %lu
119 cminflt %lu
120 majflt %lu
121 cmajflt %lu
122 utime %lu
123 stime %lu
124 cutime %ld
125 cstime %ld
126 priority %ld
127 nice %ld
128 0 %ld
129 itrealvalue %ld
130 starttime %lu
131 vsize %lu
132 rss %ld
133 rlim %lu
134 startcode %lu
135 endcode %lu
136 startstack %lu
137 kstkesp %lu
138 kstkeip %lu
139 signal %lu
140 blocked %lu
141 sigignore %lu
142 sigcatch %lu
143 wchan %lu
144 nswap %lu
145 cnswap %lu
146 exit_signal %d
147 processor %d
148 rt_priority %lu (since kernel 2.5.19)
149 policy %lu (since kernel 2.5.19)
150 */
151  return vm;
152  }
153 
154 };
155 #else // __APPLE__: has no "/proc" pseudo-file system
156 
157 // Thanks to David O'Gwynn for providing this fix.
158 // This comes from a post by Michael Knight:
159 //
160 // http://miknight.blogspot.com/2005/11/resident-set-size-in-mac-os-x.html
161 
162 #include <unistd.h>
163 #include <stdio.h>
164 #include <stdlib.h>
165 #include <sys/types.h>
166 #include <sys/sysctl.h>
167 #include <mach/task.h>
168 #include <mach/mach_init.h>
169 
170 void getres(task_t task, unsigned long *rss, unsigned long *vs)
171 {
172  struct task_basic_info t_info;
173  mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
174 
175  task_info(task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
176  *rss = t_info.resident_size;
177  *vs = t_info.virtual_size;
178 }
179 
180 class MemoryInfo
181 {
182  public:
183  static size_t Usage(void)
184  {
185  unsigned long rss, vs;
186  task_t task = MACH_PORT_NULL;
187 
188  if (task_for_pid(current_task(), getpid(), &task) != KERN_SUCCESS)
189  abort();
190  getres(task, &rss, &vs);
191  return rss;
192  }
193 
194 };
195 
196 #endif // !__APPLE__
197 
198 #endif // WIN32
199 
200 #endif // MEMORY_USAGE_INCLUDE