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_H
25 #include <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"
56 #include "memusage.h"
57 #include "bfind.h"
58
59 #if defined(OS_LINUX) || defined(__FreeBSD__) || defined(OS_SOLARIS)
60 static int read_common_sizet(void *szp, char *strval)
61 {
62 char *end;
63
64 if (szp == NULL || strval == NULL) {
65 return -1;
66 }
67
68 end = strchr(strval, ' ');
69
70 if (end == NULL)
71 return (-1);
72
73 *end = '\0';
74
75 errno = 0;
76 *(size_t *)szp = strtoll(strval, NULL, 10);
77
78 if (errno == EINVAL ||
79 errno == ERANGE)
80 return (-1);
81
82 return (0);
83 }
84
85 struct stat_parser {
86 char *keyword;
87 int (*storval)(void *, char *);
88 ptrdiff_t offset;
89 };
90
91 static int cmpkey (const char *a, const struct stat_parser *b)
92 {
93 return strcmp(a, b->keyword);
94 }
95
96 static int read_status(const char *source, void *base, struct stat_parser *spt, size_t spt_size)
97 {
98 FILE *fp;
99 size_t processed;
100
101 #ifndef NDEBUG
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 = fopen(source, "r");
116
117 if (fp == NULL)
118 return -1;
119 else {
120 char linebuf[256];
121 char *strval;
122 struct stat_parser *sp;
123
124 processed = 0;
125
126 while (fgets(linebuf, sizeof linebuf - 1, fp) != NULL) {
127 strval = strchr(linebuf, ':');
128
129 if (strval == NULL) {
130 fclose (fp);
131 return (-1);
132 }
133
134 *strval++ = '\0';
135
136 while(isspace(*strval))
137 ++strval;
138
139 sp = oscap_bfind(spt, spt_size, sizeof(struct stat_parser),
140 linebuf, (int(*)(void *, void *))&cmpkey);
141
142 if (sp == NULL) {
143 /* drop end of unread line */
144 while (strchr(strval, '\n') == NULL) {
145 linebuf[0] = '\n';
146 if (fgets(linebuf, sizeof linebuf - 1, fp) == NULL) {
147 fclose(fp);
148 return (-1);
149 }
150 strval = linebuf;
151 }
152 continue;
153 }
154
155 /* line is too long, somthing is wrong */
156 if (strchr(strval, '\n') == NULL) {
157 dE("value for %s key is too long", linebuf);
158 fclose(fp);
159 return (-1);
160 }
161
162 if (sp->storval((void *)((uintptr_t)(base) + sp->offset), strval) != 0) {
163 fclose (fp);
164 return (-1);
165 }
166
167 ++processed;
168 }
169
170 fclose(fp);
171 }
172
173 return processed == spt_size ? 0 : 1;
174 }
175
176 #define stat_sizet_field(name, stype, sfield) \
177 { (name), &read_common_sizet, (ptrdiff_t)offsetof(stype, sfield) }
178
179 struct stat_parser __sys_stat_ptable[] = {
180 stat_sizet_field("Active", struct sys_memusage, mu_active),
181 stat_sizet_field("Buffers", struct sys_memusage, mu_buffers),
182 stat_sizet_field("Cached", struct sys_memusage, mu_cached),
183 stat_sizet_field("Inactive", struct sys_memusage, mu_inactive),
184 stat_sizet_field("MemFree", struct sys_memusage, mu_free),
185 stat_sizet_field("MemTotal", struct sys_memusage, mu_total)
186 };
187
188 struct stat_parser __proc_stat_ptable[] = {
189 stat_sizet_field("VmData", struct proc_memusage, mu_data),
190 stat_sizet_field("VmExe", struct proc_memusage, mu_text),
191 stat_sizet_field("VmHWM", struct proc_memusage, mu_hwm),
192 stat_sizet_field("VmLck", struct proc_memusage, mu_lock),
193 stat_sizet_field("VmLib", struct proc_memusage, mu_lib),
194 stat_sizet_field("VmRSS", struct proc_memusage, mu_rss),
195 stat_sizet_field("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_memusage(struct sys_memusage *mu)
293 {
294 if (mu == NULL)
295 return -1;
296 #if defined(OS_LINUX)
297 if (read_status(MEMUSAGE_LINUX_SYS_STATUS,
298 mu, __sys_stat_ptable,
299 (sizeof __sys_stat_ptable)/sizeof(struct stat_parser)) != 0)
300 {
301 return -1;
302 }
303
304 mu->mu_realfree = mu->mu_free + mu->mu_cached + mu->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_memusage(struct proc_memusage *mu)
316 {
317 if (mu == NULL)
318 return -1;
319 #if defined(OS_LINUX)
320 if (read_status(MEMUSAGE_LINUX_PROC_STATUS,
321 mu, __proc_stat_ptable,
322 (sizeof __proc_stat_ptable)/sizeof(struct 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 }