Debian Codegraph @

src/common/memusage.c

raw symbols SCIP
1 /*
2 * Copyright 2010 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Daniel Kopecek <[email protected]>
21 */
22
23
24 #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`!
25 #include <config.h>cxx . . $ `<file>/build/config.h`/
26 #endif
27
28 #include <stdio.h>
29 #include <stddef.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <stdint.h>
34
35 #if defined(OS_FREEBSD)
36 #include <fcntl.h>
37 #include <kvm.h>
38 #include <limits.h>
39 #include <paths.h>
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/sysctl.h>
43 #include <sys/user.h>
44 #include <unistd.h>
45
46 #define GET_VM_PAGE_SIZE "vm.stats.vm.v_page_size"
47 #define GET_VM_TOTAL_PAGE_COUNT "vm.stats.vm.v_page_count"
48 #define GET_VM_FREE_PAGE_COUNT "vm.stats.vm.v_free_count"
49 #define GET_VM_INACT_PAGE_COUNT "vm.stats.vm.v_inactive_count"
50 #define GET_VM_ACT_PAGE_COUNT "vm.stats.vm.v_active_count"
51
52 #define BYTES_TO_KIB(x) (x >> 10)
53 #endif
54
55 #include "debug_priv.h"cxx . . $ `<file>/src/common/debug_priv.h`/
56 #include "memusage.h"cxx . . $ `<file>/src/common/memusage.h`/
57 #include "bfind.h"cxx . . $ `<file>/src/common/bfind.h`/
58
59 #if defined(OS_LINUXcxx . . $ `compat/oscap_platforms.h:38:10`!) || defined(__FreeBSD__) || defined(OS_SOLARIS)
60 static int read_common_sizetcxx . . $ read_common_sizet(e256373c99a31857).(void *szp, char *strval)
61 {
62 char *end;
63
64 if (szp == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || strval == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
65 return -1;
66 }
67
68 end = strchrcxx . . $ strchr(2050848944e00d54).(strval, ' ');
69
70 if (end == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
71 return (-1);
72
73 *end = '\0';
74
75 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0;
76 *(size_t *)szp = strtollcxx . . $ strtoll(c50972e7488cbac6).(strval, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 10);
77
78 if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`! ||
79 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`!)
80 return (-1);
81
82 return (0);
83 }
84
85 struct stat_parsercxx . . $ stat_parser# {
86 char *keywordcxx . . $ stat_parser#keyword.;
87 int (*storvalcxx . . $ stat_parser#storval.)(void *, char *);
88 ptrdiff_t offsetcxx . . $ stat_parser#offset.;
89 };
90
91 static int cmpkeycxx . . $ cmpkey(b4edf5655cd8d0d7). (const char *a, const struct stat_parsercxx . . $ stat_parser# *b)
92 {
93 return strcmpcxx . . $ strcmp(5ae29d726ae57f88).(a, b->keywordcxx . . $ stat_parser#keyword.);
94 }
95
96 static int read_statuscxx . . $ read_status(92762a994a8b9b75).(const char *source, void *base, struct stat_parsercxx . . $ stat_parser# *spt, size_t spt_size)
97 {
98 FILEcxx . . $ FILE# *fp;
99 size_t processed;
100
101 #ifndef NDEBUGcxx . . $ `<built-in>:425:9`!
102 /* check whether spt is sorted */
103 {
104 register size_t i;
105
106 for (i = 0; i < spt_size - 1; ++i) {
107 if (cmpkey(spt[i].keyword, spt + i+1) > 0) {
108 dE("spt not sorted! %s > %s",
109 spt[i].keyword, spt[i+1].keyword);
110 abort();
111 }
112 }
113 }
114 #endif
115 fp = fopencxx . . $ fopen(82ec0787cd37e0a3).(source, "r");
116
117 if (fp == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
118 return -1;
119 else {
120 char linebuf[256];
121 char *strval;
122 struct stat_parsercxx . . $ stat_parser# *sp;
123
124 processed = 0;
125
126 while (fgetscxx . . $ fgets(efb0aaba7ac866ae).(linebuf, sizeof linebuf - 1, fp) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
127 strval = strchrcxx . . $ strchr(2050848944e00d54).(linebuf, ':');
128
129 if (strval == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
130 fclosecxx . . $ fclose(89bc1efcdac0ca22). (fp);
131 return (-1);
132 }
133
134 *strval++ = '\0';
135
136 while(isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(*strval))
137 ++strval;
138
139 sp = oscap_bfindcxx . . $ oscap_bfind(55e9dbee426f048c).(spt, spt_size, sizeof(struct stat_parsercxx . . $ stat_parser#),
140 linebuf, (int(*)(void *, void *))&cmpkeycxx . . $ cmpkey(b4edf5655cd8d0d7).);
141
142 if (sp == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
143 /* drop end of unread line */
144 while (strchrcxx . . $ strchr(2050848944e00d54).(strval, '\n') == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
145 linebuf[0] = '\n';
146 if (fgetscxx . . $ fgets(efb0aaba7ac866ae).(linebuf, sizeof linebuf - 1, fp) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
147 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fp);
148 return (-1);
149 }
150 strval = linebuf;
151 }
152 continue;
153 }
154
155 /* line is too long, somthing is wrong */
156 if (strchrcxx . . $ strchr(2050848944e00d54).(strval, '\n') == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
157 dEcxx . . $ `src/common/debug_priv.h:63:9`!("value for %s key is too long", linebuf);
158 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fp);
159 return (-1);
160 }
161
162 if (sp->storvalcxx . . $ stat_parser#storval.((void *)((uintptr_tcxx . . $ uintptr_t#)(base) + sp->offsetcxx . . $ stat_parser#offset.), strval) != 0) {
163 fclosecxx . . $ fclose(89bc1efcdac0ca22). (fp);
164 return (-1);
165 }
166
167 ++processed;
168 }
169
170 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fp);
171 }
172
173 return processed == spt_size ? 0 : 1;
174 }
175
176 #define stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!(name, stype, sfield) \
177 { (name), &read_common_sizet, (ptrdiff_t)offsetofcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:431:9`!(stype, sfield) }
178
179 struct stat_parsercxx . . $ stat_parser# __sys_stat_ptablecxx . . $ __sys_stat_ptable.[] = {
180 stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!("Active", struct sys_memusage, mu_active),
181 stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!("Buffers", struct sys_memusage, mu_buffers),
182 stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!("Cached", struct sys_memusage, mu_cached),
183 stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!("Inactive", struct sys_memusage, mu_inactive),
184 stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!("MemFree", struct sys_memusage, mu_free),
185 stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!("MemTotal", struct sys_memusage, mu_total)
186 };
187
188 struct stat_parsercxx . . $ stat_parser# __proc_stat_ptablecxx . . $ __proc_stat_ptable.[] = {
189 stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!("VmData", struct proc_memusage, mu_data),
190 stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!("VmExe", struct proc_memusage, mu_text),
191 stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!("VmHWM", struct proc_memusage, mu_hwm),
192 stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!("VmLck", struct proc_memusage, mu_lock),
193 stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!("VmLib", struct proc_memusage, mu_lib),
194 stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!("VmRSS", struct proc_memusage, mu_rss),
195 stat_sizet_fieldcxx . . $ `src/common/memusage.c:176:9`!("VmStk", struct proc_memusage, mu_stack),
196 };
197
198 #endif /* OS_LINUX */
199
200 #if defined(OS_FREEBSD)
201 static int freebsd_sys_memusage(struct sys_memusage *mu)
202 {
203 size_t size;
204 size_t page_size = 0;
205 unsigned int total_page_count = 0;
206 unsigned int free_page_count = 0;
207 unsigned int active_page_count = 0;
208 unsigned int inactive_page_count = 0;
209
210 /* Page size */
211 size = sizeof(page_size);
212 if (sysctlbyname(GET_VM_PAGE_SIZE, &page_size, &size, NULL, 0) < 0) {
213 return -1;
214 }
215
216 /* Total pages */
217 size = sizeof(total_page_count);
218 if (sysctlbyname(GET_VM_TOTAL_PAGE_COUNT, &total_page_count, &size, NULL, 0) < 0) {
219 return -1;
220 }
221
222 /* Total free pages */
223 size = sizeof(free_page_count);
224 if (sysctlbyname(GET_VM_FREE_PAGE_COUNT, &free_page_count, &size, NULL, 0) < 0) {
225 return -1;
226 }
227
228 /* Total active pages */
229 size = sizeof(active_page_count);
230 if (sysctlbyname(GET_VM_ACT_PAGE_COUNT, &active_page_count , &size, NULL, 0) < 0) {
231 return -1;
232 }
233
234 /* Total inactive pages */
235 size = sizeof(inactive_page_count);
236 if (sysctlbyname(GET_VM_INACT_PAGE_COUNT, &inactive_page_count , &size, NULL, 0) < 0) {
237 return -1;
238 }
239
240 mu->mu_total = BYTES_TO_KIB(total_page_count * page_size);
241 mu->mu_free = BYTES_TO_KIB(free_page_count * page_size);
242 mu->mu_active = BYTES_TO_KIB(active_page_count * page_size);
243 mu->mu_inactive = BYTES_TO_KIB(inactive_page_count * page_size);
244 mu->mu_realfree = mu->mu_free + mu->mu_inactive;
245
246 /* FreeBSD does not report these values */
247 mu->mu_buffers = 0;
248 mu->mu_cached = 0;
249
250 return 0;
251 }
252
253 static int freebsd_proc_memusage(struct proc_memusage *mu)
254 {
255 int count;
256 kvm_t *kd;
257 pid_t mypid;
258 char errbuf[LINE_MAX];
259 struct kinfo_proc *procinfo;
260
261 mypid = getpid();
262 kd = kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, errbuf);
263
264 if (!kd)
265 return -1;
266
267 procinfo = kvm_getprocs(kd, KERN_PROC_PID, mypid, &count);
268
269 if (!procinfo)
270 return -1;
271
272 mu->mu_rss = procinfo->ki_rssize;
273 mu->mu_text = procinfo->ki_tsize;
274 mu->mu_data = procinfo->ki_dsize;
275 mu->mu_stack = procinfo->ki_ssize;
276
277 /* ki_swrss is the resident set size before last swap, this
278 * is the closest approximation to Linux's "VmHWM" which is the
279 * peak resident set size of the process.
280 */
281 mu->mu_hwm = procinfo->ki_swrss;
282
283 /* Not exposed on FreeBSD */
284 mu->mu_lib = 0;
285 mu->mu_lock = 0;
286
287 return 0;
288 }
289
290 #endif /* OS_FREEBSD */
291
292 int oscap_sys_memusagecxx . . $ oscap_sys_memusage(99e7a5fcafeb1462).(struct sys_memusagecxx . . $ sys_memusage# *mu)
293 {
294 if (mu == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
295 return -1;
296 #if defined(OS_LINUXcxx . . $ `compat/oscap_platforms.h:38:10`!)
297 if (read_statuscxx . . $ read_status(92762a994a8b9b75).(MEMUSAGE_LINUX_SYS_STATUScxx . . $ `src/common/memusage.h:9:10`!,
298 mu, __sys_stat_ptablecxx . . $ __sys_stat_ptable.,
299 (sizeof __sys_stat_ptablecxx . . $ __sys_stat_ptable.)/sizeof(struct stat_parsercxx . . $ stat_parser#)) != 0)
300 {
301 return -1;
302 }
303
304 mu->mu_realfreecxx . . $ sys_memusage#mu_realfree. = mu->mu_freecxx . . $ sys_memusage#mu_free. + mu->mu_cachedcxx . . $ sys_memusage#mu_cached. + mu->mu_bufferscxx . . $ sys_memusage#mu_buffers.;
305 #elif defined(OS_FREEBSD)
306 if (freebsd_sys_memusage(mu))
307 return -1;
308 #else
309 errno = EOPNOTSUPP;
310 return -1;
311 #endif
312 return 0;
313 }
314
315 int oscap_proc_memusagecxx . . $ oscap_proc_memusage(bd134701dab08304).(struct proc_memusagecxx . . $ proc_memusage# *mu)
316 {
317 if (mu == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
318 return -1;
319 #if defined(OS_LINUXcxx . . $ `compat/oscap_platforms.h:38:10`!)
320 if (read_statuscxx . . $ read_status(92762a994a8b9b75).(MEMUSAGE_LINUX_PROC_STATUScxx . . $ `src/common/memusage.h:7:10`!,
321 mu, __proc_stat_ptablecxx . . $ __proc_stat_ptable.,
322 (sizeof __proc_stat_ptablecxx . . $ __proc_stat_ptable.)/sizeof(struct stat_parsercxx . . $ stat_parser#)) != 0)
323 {
324 return -1;
325 }
326 #elif defined(OS_FREEBSD)
327 if (freebsd_proc_memusage(mu))
328 return -1;
329 #else
330 errno = EOPNOTSUPP;
331 return -1;
332 #endif
333 return 0;
334 }