NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
meminfo.h
浏览该文件的文档.
1#if !defined(spp_memory_h_guard)
2#define spp_memory_h_guard
3
4#include <cstdint>
5#include <cstring>
6#include <cstdlib>
7
8#if defined(_WIN32) || defined( __CYGWIN__)
9 #define SPP_WIN
10#endif
11
12#ifdef SPP_WIN
13 #include <windows.h>
14 #include <Psapi.h>
15 #undef min
16 #undef max
17#elif defined(__linux__)
18 #include <sys/types.h>
19 #include <sys/sysinfo.h>
20#elif defined(__FreeBSD__)
21 #include <paths.h>
22 #include <fcntl.h>
23 #include <kvm.h>
24 #include <unistd.h>
25 #include <sys/sysctl.h>
26 #include <sys/user.h>
27#endif
28
29namespace spp
30{
31 uint64_t GetSystemMemory();
32 uint64_t GetTotalMemoryUsed();
33 uint64_t GetProcessMemoryUsed();
34 uint64_t GetPhysicalMemory();
35
36 uint64_t GetSystemMemory()
37 {
38#ifdef SPP_WIN
39 MEMORYSTATUSEX memInfo;
40 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
41 GlobalMemoryStatusEx(&memInfo);
42 return static_cast<uint64_t>(memInfo.ullTotalPageFile);
43#elif defined(__linux__)
44 struct sysinfo memInfo;
45 sysinfo (&memInfo);
46 auto totalVirtualMem = memInfo.totalram;
47
48 totalVirtualMem += memInfo.totalswap;
49 totalVirtualMem *= memInfo.mem_unit;
50 return static_cast<uint64_t>(totalVirtualMem);
51#elif defined(__FreeBSD__)
52 kvm_t *kd;
53 u_int pageCnt;
54 size_t pageCntLen = sizeof(pageCnt);
55 u_int pageSize;
56 struct kvm_swap kswap;
57 uint64_t totalVirtualMem;
58
59 pageSize = static_cast<u_int>(getpagesize());
60
61 sysctlbyname("vm.stats.vm.v_page_count", &pageCnt, &pageCntLen, NULL, 0);
62 totalVirtualMem = pageCnt * pageSize;
63
64 kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
65 kvm_getswapinfo(kd, &kswap, 1, 0);
66 kvm_close(kd);
67 totalVirtualMem += kswap.ksw_total * pageSize;
68
69 return totalVirtualMem;
70#else
71 return 0;
72#endif
73 }
74
76 {
77#ifdef SPP_WIN
78 MEMORYSTATUSEX memInfo;
79 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
80 GlobalMemoryStatusEx(&memInfo);
81 return static_cast<uint64_t>(memInfo.ullTotalPageFile - memInfo.ullAvailPageFile);
82#elif defined(__linux__)
83 struct sysinfo memInfo;
84 sysinfo(&memInfo);
85 auto virtualMemUsed = memInfo.totalram - memInfo.freeram;
86
87 virtualMemUsed += memInfo.totalswap - memInfo.freeswap;
88 virtualMemUsed *= memInfo.mem_unit;
89
90 return static_cast<uint64_t>(virtualMemUsed);
91#elif defined(__FreeBSD__)
92 kvm_t *kd;
93 u_int pageSize;
94 u_int pageCnt, freeCnt;
95 size_t pageCntLen = sizeof(pageCnt);
96 size_t freeCntLen = sizeof(freeCnt);
97 struct kvm_swap kswap;
98 uint64_t virtualMemUsed;
99
100 pageSize = static_cast<u_int>(getpagesize());
101
102 sysctlbyname("vm.stats.vm.v_page_count", &pageCnt, &pageCntLen, NULL, 0);
103 sysctlbyname("vm.stats.vm.v_free_count", &freeCnt, &freeCntLen, NULL, 0);
104 virtualMemUsed = (pageCnt - freeCnt) * pageSize;
105
106 kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
107 kvm_getswapinfo(kd, &kswap, 1, 0);
108 kvm_close(kd);
109 virtualMemUsed += kswap.ksw_used * pageSize;
110
111 return virtualMemUsed;
112#else
113 return 0;
114#endif
115 }
116
118 {
119#ifdef SPP_WIN
120 PROCESS_MEMORY_COUNTERS_EX pmc;
121 GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast<PPROCESS_MEMORY_COUNTERS>(&pmc), sizeof(pmc));
122 return static_cast<uint64_t>(pmc.PrivateUsage);
123#elif defined(__linux__)
124 auto parseLine =
125 [](char* line)->int
126 {
127 auto i = strlen(line);
128
129 while(*line < '0' || *line > '9')
130 {
131 line++;
132 }
133
134 line[i-3] = '\0';
135 i = atoi(line);
136 return i;
137 };
138
139 auto file = fopen("/proc/self/status", "r");
140 auto result = -1;
141 char line[128];
142
143 while(fgets(line, 128, file) != nullptr)
144 {
145 if(strncmp(line, "VmSize:", 7) == 0)
146 {
147 result = parseLine(line);
148 break;
149 }
150 }
151
152 fclose(file);
153 return static_cast<uint64_t>(result) * 1024;
154#elif defined(__FreeBSD__)
155 struct kinfo_proc info;
156 size_t infoLen = sizeof(info);
157 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
158
159 sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &infoLen, NULL, 0);
160 return static_cast<uint64_t>(info.ki_rssize * getpagesize());
161#else
162 return 0;
163#endif
164 }
165
167 {
168#ifdef SPP_WIN
169 MEMORYSTATUSEX memInfo;
170 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
171 GlobalMemoryStatusEx(&memInfo);
172 return static_cast<uint64_t>(memInfo.ullTotalPhys);
173#elif defined(__linux__)
174 struct sysinfo memInfo;
175 sysinfo(&memInfo);
176
177 auto totalPhysMem = memInfo.totalram;
178
179 totalPhysMem *= memInfo.mem_unit;
180 return static_cast<uint64_t>(totalPhysMem);
181#elif defined(__FreeBSD__)
182 u_long physMem;
183 size_t physMemLen = sizeof(physMem);
184 int mib[] = { CTL_HW, HW_PHYSMEM };
185
186 sysctl(mib, sizeof(mib) / sizeof(*mib), &physMem, &physMemLen, NULL, 0);
187 return physMem;
188#else
189 return 0;
190#endif
191 }
192
193}
194
195#endif // spp_memory_h_guard
Definition: meminfo.h:30
uint64_t GetTotalMemoryUsed()
Definition: meminfo.h:75
uint64_t GetSystemMemory()
Definition: meminfo.h:36
uint64_t GetProcessMemoryUsed()
Definition: meminfo.h:117
uint64_t GetPhysicalMemory()
Definition: meminfo.h:166