Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/vminfo.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014 Davide Madrisan <[email protected]>
5 *
6 * A library for checking memory and swap usage on linux
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * This module is based on procps 3.2.8
22 */
23
24 #ifndef _GNU_SOURCE
25 # define _GNU_SOURCE /* activate extra prototypes for glibc */
26 #endif
27
28 #include <sys/types.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #include "system.h"
35 #include "getenv.h"
36 #include "messages.h"
37 #include "procparser.h"
38 #include "vminfo.h"
39
40 #define PROC_STAT "/proc/stat"
41
42 typedef struct proc_vmem_data
43 {
44 /* read /proc/vminfo only for 2.5.41 and above */
45 /* see include/linux/page-flags.h and mm/page_alloc.c */
46 unsigned long vm_nr_dirty; /* dirty writable pages */
47 unsigned long vm_nr_writeback; /* pages under writeback */
48 unsigned long vm_nr_pagecache; /* pages in pagecache -- gone in 2.5.66+ kernels */
49 unsigned long vm_nr_page_table_pages; /* pages used for pagetables */
50 unsigned long vm_nr_reverse_maps; /* includes PageDirect */
51 unsigned long vm_nr_mapped; /* mapped into pagetables */
52 unsigned long vm_nr_slab; /* in slab */
53 unsigned long vm_pgpgin; /* kB disk reads (same as 1st num on /proc/stat page line) */
54 unsigned long vm_pgpgout; /* kB disk writes (same as 2nd num on /proc/stat page line) */
55 unsigned long vm_pswpin; /* swap reads (same as 1st num on /proc/stat swap line) */
56 unsigned long vm_pswpout; /* swap writes (same as 2nd num on /proc/stat swap line) */
57 unsigned long vm_pgalloc; /* page allocations */
58 unsigned long vm_pgfree; /* page freeings */
59 unsigned long vm_pgactivate; /* pages moved inactive -> active */
60 unsigned long vm_pgdeactivate; /* pages moved active -> inactive */
61 unsigned long vm_pgfault; /* total faults (major+minor) */
62 unsigned long vm_pgmajfault; /* major faults */
63 unsigned long vm_pgscan; /* pages scanned by page reclaim */
64 unsigned long vm_pgrefill; /* inspected by refill_inactive_zone */
65 unsigned long vm_pgsteal; /* total pages reclaimed */
66 unsigned long vm_kswapd_steal; /* pages reclaimed by kswapd */
67 /* next 3 as defined by the 2.5.52 kernel */
68 unsigned long vm_pageoutrun; /* times kswapd ran page reclaim */
69 unsigned long vm_allocstall; /* times a page allocator ran direct reclaim */
70 unsigned long vm_pgrotated; /* pages rotated to the tail of the LRU for immediate reclaim */
71 unsigned long vm_pgalloc_dma;
72 unsigned long vm_pgalloc_dma32;
73 unsigned long vm_pgalloc_high;
74 unsigned long vm_pgalloc_normal;
75 unsigned long vm_pgrefill_dma;
76 unsigned long vm_pgrefill_dma32;
77 unsigned long vm_pgrefill_high;
78 unsigned long vm_pgrefill_normal;
79 unsigned long vm_pgscan_direct_dma;
80 unsigned long vm_pgscan_direct_dma32;
81 unsigned long vm_pgscan_direct_high;
82 unsigned long vm_pgscan_direct_normal;
83 unsigned long vm_pgscan_kswapd_dma;
84 unsigned long vm_pgscan_kswapd_dma32;
85 unsigned long vm_pgscan_kswapd_high;
86 unsigned long vm_pgscan_kswapd_normal;
87 unsigned long vm_pgsteal_dma;
88 unsigned long vm_pgsteal_dma32;
89 unsigned long vm_pgsteal_high;
90 unsigned long vm_pgsteal_normal;
91 unsigned long vm_pgsteal_direct_dma;
92 unsigned long vm_pgsteal_direct_dma32;
93 unsigned long vm_pgsteal_direct_high;
94 unsigned long vm_pgsteal_direct_normal;
95 /* seen on a 2.6.8-rc1 kernel */
96 unsigned long vm_kswapd_inodesteal;
97 unsigned long vm_nr_unstable;
98 unsigned long vm_pginodesteal;
99 unsigned long vm_slabs_scanned;
100 } proc_vmem_data_t;
101
102 typedef struct proc_vmem
103 {
104 int refcount;
105 struct proc_vmem_data *data;
106 } proc_vmem_t;
107
108 #ifndef NPL_TESTING
109
110 const char *
111 get_path_proc_vmstat ()
112 {
113 const char *env_procvmstat = secure_getenv ("NPL_TEST_PATH_PROCVMSTAT");
114 if (env_procvmstat)
115 return env_procvmstat;
116
117 return "/proc/vmstat";
118 }
119
120 /* get_vmem_pagesize - get memory page size */
121
122 inline long
123 get_vmem_pagesize (void)
124 {
125 return sysconf (_SC_PAGESIZE);
126 }
127
128 /* Allocates space for a new vmem object.
129 * Returns 0 if all went ok. Errors are returned as negative values. */
130
131 int
132 proc_vmem_new (struct proc_vmem **vmem)
133 {
134 struct proc_vmem *vm;
135
136 vm = calloc (1, sizeof (struct proc_vmem));
137 if (!vm)
138 return -ENOMEM;
139
140 vm->refcount = 1;
141 vm->data = calloc (1, sizeof (struct proc_vmem_data));
142 if (!vm->data)
143 {
144 free (vm);
145 return -ENOMEM;
146 }
147
148 *vmem = vm;
149 return 0;
150 }
151
152 /* Fill the proc_vmem structure pointed will the values found in the
153 * proc filesystem. */
154
155 void
156 proc_vmem_read (struct proc_vmem *vmem)
157 {
158 FILE *fp;
159 char *line = NULL;
160 size_t len = 0;
161 bool found_pgpg_data = false, found_pswp_data = false;
162
163 if (vmem == NULL)
164 return;
165
166 struct proc_vmem_data *data = vmem->data;
167
168 const proc_table_struct vmem_table[] = {
169 { "allocstall", &data->vm_allocstall },
170 { "kswapd_inodesteal", &data->vm_kswapd_inodesteal },
171 { "kswapd_steal", &data->vm_kswapd_steal },
172 { "nr_dirty", &data->vm_nr_dirty }, /* page version of meminfo Dirty */
173 { "nr_mapped", &data->vm_nr_mapped }, /* page version of meminfo Mapped */
174 { "nr_page_table_pages", &data->vm_nr_page_table_pages }, /* same as meminfo PageTables */
175 { "nr_pagecache", &data->vm_nr_pagecache }, /* gone in 2.5.66+ kernels */
176 { "nr_reverse_maps", &data->vm_nr_reverse_maps }, /* page version of meminfo ReverseMaps GONE */
177 { "nr_slab", &data->vm_nr_slab }, /* page version of meminfo Slab */
178 { "nr_unstable", &data->vm_nr_unstable },
179 { "nr_writeback", &data->vm_nr_writeback }, /* page version of meminfo Writeback */
180 { "pageoutrun", &data->vm_pageoutrun },
181 { "pgactivate", &data->vm_pgactivate },
182 { "pgalloc", &data->vm_pgalloc }, /* GONE (now separate dma,high,normal) */
183 { "pgalloc_dma", &data->vm_pgalloc_dma },
184 { "pgalloc_dma32", &data->vm_pgalloc_dma32 },
185 { "pgalloc_high", &data->vm_pgalloc_high },
186 { "pgalloc_normal", &data->vm_pgalloc_normal },
187 { "pgdeactivate", &data->vm_pgdeactivate },
188 { "pgfault", &data->vm_pgfault },
189 { "pgfree", &data->vm_pgfree },
190 { "pginodesteal", &data->vm_pginodesteal },
191 { "pgmajfault", &data->vm_pgmajfault },
192 { "pgpgin", &data->vm_pgpgin }, /* important */
193 { "pgpgout", &data->vm_pgpgout }, /* important */
194 { "pgrefill", &data->vm_pgrefill }, /* GONE (now separate dma,high,normal) */
195 { "pgrefill_dma", &data->vm_pgrefill_dma },
196 { "pgrefill_dma32", &data->vm_pgrefill_dma32 },
197 { "pgrefill_high", &data->vm_pgrefill_high },
198 { "pgrefill_normal", &data->vm_pgrefill_normal },
199 { "pgrotated", &data->vm_pgrotated },
200 { "pgscan", &data->vm_pgscan }, /* GONE (now separate direct,kswapd and dma,high,normal) */
201 { "pgscan_direct_dma", &data->vm_pgscan_direct_dma },
202 { "pgscan_direct_dma32", &data->vm_pgscan_direct_dma32 },
203 { "pgscan_direct_high", &data->vm_pgscan_direct_high },
204 { "pgscan_direct_normal", &data->vm_pgscan_direct_normal },
205 { "pgscan_kswapd_dma", &data->vm_pgscan_kswapd_dma },
206 { "pgscan_kswapd_dma32", &data->vm_pgscan_kswapd_dma32 },
207 { "pgscan_kswapd_high", &data->vm_pgscan_kswapd_high },
208 { "pgscan_kswapd_normal", &data->vm_pgscan_kswapd_normal },
209 { "pgsteal", &data->vm_pgsteal }, /* GONE (now separate dma,high,normal) */
210 { "pgsteal_dma", &data->vm_pgsteal_dma },
211 { "pgsteal_dma32", &data->vm_pgsteal_dma32 },
212 { "pgsteal_high", &data->vm_pgsteal_high },
213 { "pgsteal_normal", &data->vm_pgsteal_normal },
214 { "pgsteal_direct_dma", &data->vm_pgsteal_direct_dma },
215 { "pgsteal_direct_dma32", &data->vm_pgsteal_direct_dma32 },
216 { "pgsteal_direct_high", &data->vm_pgsteal_direct_high },
217 { "pgsteal_direct_normal", &data->vm_pgsteal_direct_normal },
218 { "pswpin", &data->vm_pswpin }, /* important */
219 { "pswpout", &data->vm_pswpout }, /* important */
220 { "slabs_scanned", &data->vm_slabs_scanned },
221 };
222 const int vmem_table_count =
223 sizeof (vmem_table) / sizeof (proc_table_struct);
224
225 data->vm_pgalloc = 0;
226 data->vm_pgrefill = 0;
227 data->vm_pgscan = 0;
228 data->vm_pgsteal = 0;
229
230 data->vm_pgpgin = data->vm_pgpgout = ~0UL;
231 data->vm_pswpin = data->vm_pswpout = ~0UL;
232
233 procparser (get_path_proc_vmstat (), vmem_table, vmem_table_count, ' ');
234
235 #define FOR_ALL_ZONES(x) x##_dma + x##_dma32 + x##_normal + x##_high
236 if (!data->vm_pgalloc)
237 data->vm_pgalloc = FOR_ALL_ZONES (data->vm_pgalloc);
238
239 if (!data->vm_pgrefill)
240 data->vm_pgrefill = FOR_ALL_ZONES (data->vm_pgrefill);
241
242 if (!data->vm_pgscan)
243 data->vm_pgscan = FOR_ALL_ZONES (data->vm_pgscan_direct)
244 + FOR_ALL_ZONES (data->vm_pgscan_kswapd);
245
246 if (!data->vm_pgsteal)
247 data->vm_pgsteal = FOR_ALL_ZONES (data->vm_pgsteal);
248 #undef FOR_ALL_ZONES
249
250 if (data->vm_pgpgin != ~0UL && data->vm_pswpin != ~0UL)
251 return;
252 else if (data->vm_pgpgin != ~0UL)
253 found_pgpg_data = true;
254 else if (data->vm_pswpin != ~0UL)
255 found_pswp_data = true;
256
257 /* Linux kernels < 2.5.40-bk4 */
258
259 if ((fp = fopen (PROC_STAT, "r")))
260 {
261 ssize_t nread;
262 while ((nread = getline (&line, &len, fp)) != -1)
263 {
264 if (2 == sscanf (line, "page %lu %lu",
265 &data->vm_pgpgin, &data->vm_pgpgout))
266 found_pgpg_data = true;
267 else if (2 == sscanf (line, "swap %lu %lu",
268 &data->vm_pswpin, &data->vm_pswpout))
269 found_pswp_data = true;
270
271 if (found_pgpg_data && found_pswp_data)
272 break;
273 }
274 fclose (fp);
275 free (line);
276 }
277
278 /* This should never occur */
279 if (!found_pgpg_data)
280 data->vm_pgpgin = data->vm_pgpgout = 0;
281 if (!found_pswp_data)
282 data->vm_pswpin = data->vm_pswpout = 0;
283 }
284
285 /* Drop a reference of the memory library context. If the refcount of
286 * reaches zero, the resources of the context will be released. */
287
288 struct proc_vmem *
289 proc_vmem_unref (struct proc_vmem *vmem)
290 {
291 if (vmem == NULL)
292 return NULL;
293
294 vmem->refcount--;
295 if (vmem->refcount > 0)
296 return vmem;
297
298 free (vmem->data);
299 free (vmem);
300 return NULL;
301 }
302
303 #define proc_vmem_get(arg) \
304 unsigned long proc_vmem_get_ ## arg (struct proc_vmem *p) \
305 { return (p == NULL) ? 0 : p->data->vm_ ## arg; }
306
307 proc_vmem_get (pgalloc)
308 proc_vmem_get (pgfault)
309 proc_vmem_get (pgfree)
310 proc_vmem_get (pgmajfault)
311 proc_vmem_get (pgpgin)
312 proc_vmem_get (pgpgout)
313 proc_vmem_get (pgrefill)
314 proc_vmem_get (pgscan)
315 proc_vmem_get (pgsteal)
316 proc_vmem_get (pswpin)
317 proc_vmem_get (pswpout)
318
319 #undef proc_vmem_get
320
321 unsigned long
322 proc_vmem_get_pgscand (struct proc_vmem *vmem)
323 {
324 return (vmem ==
325 NULL) ? 0 : vmem->data->vm_pgscan_direct_dma +
326 vmem->data->vm_pgscan_direct_high + vmem->data->vm_pgscan_direct_normal;
327 }
328
329 unsigned long
330 proc_vmem_get_pgscank (struct proc_vmem *vmem)
331 {
332 return (vmem ==
333 NULL) ? 0 : vmem->data->vm_pgscan_kswapd_dma +
334 vmem->data->vm_pgscan_kswapd_high + vmem->data->vm_pgscan_kswapd_normal;
335 }
336
337 #endif /* NPL_TESTING */