Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014-2015 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 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE /* activate extra prototypes for glibc */
23 #endif
24
25 #include <linux/version.h>
26 #include <sys/types.h>
27 #include <errno.h>
28 #include <stdlib.h>
29
30 #include "getenv.h"
31 #include "kernelver.h"
32 #include "logging.h"
33 #include "messages.h"
34 #include "meminfo.h"
35 #include "procparser.h"
36 #include "sysfsparser.h"
37 #include "system.h"
38 #include "units.h"
39
40 #define MEMINFO_UNSET ~0UL
41
42 #define PATH_PROC_SYS "/proc/sys"
43 #define PATH_VM_MIN_FREE_KB PATH_PROC_SYS "/vm/min_free_kbytes"
44
45 typedef struct proc_sysmem_data
46 {
47 /* old but still kicking -- the important stuff */
48 unsigned long kb_main_buffers;
49 unsigned long kb_page_cache;
50 unsigned long kb_main_free;
51 unsigned long kb_main_total;
52 unsigned long kb_swap_free;
53 unsigned long kb_swap_total;
54 /* Shmem in 2.6.32+ */
55 unsigned long kb_main_shared;
56 /* recently introduced */
57 unsigned long kb_high_total;
58 unsigned long kb_low_free;
59 unsigned long kb_low_total;
60 /* 2.4.xx era */
61 unsigned long kb_active;
62 unsigned long kb_inact_laundry;
63 unsigned long kb_inact_dirty;
64 unsigned long kb_inact_clean;
65 unsigned long kb_swap_cached; /* late 2.4 and 2.6+ only */
66 /* 2.5.41+ */
67 unsigned long kb_slab;
68 unsigned long kb_committed_as;
69 unsigned long kb_dirty;
70 unsigned long kb_inactive;
71 // 2.6.19+
72 unsigned long kb_slab_reclaimable;
73 /* seen on 2.6.24-rc6-git12 */
74 unsigned long kb_anon_pages;
75 // 2.6.27+
76 unsigned long kb_active_file;
77 unsigned long kb_inactive_file;
78 /* 3.14+
79 * MemAvailable provides an estimate of how much memory is available for
80 * starting new applications, without swapping.
81 * However, unlike the data provided by the Cache or Free fields,
82 * MemAvailable takes into account page cache and also that not all
83 * reclaimable memory slabs will be reclaimable due to items being in
84 * use.
85 * See: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/\
86 * commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
87 */
88 unsigned long kb_main_available;
89 /* derived values */
90 unsigned long kb_main_cached;
91 unsigned long kb_main_used;
92 } proc_sysmem_data_t;
93
94 typedef struct proc_sysmem
95 {
96 int refcount;
97 struct proc_sysmem_data *data;
98 } proc_sysmem_t;
99
100 #ifndef NPL_TESTING
101
102 const char *
103 get_path_proc_meminfo ()
104 {
105 const char *env_procmeminfo = secure_getenv ("NPL_TEST_PATH_PROCMEMINFO");
106 if (env_procmeminfo)
107 return env_procmeminfo;
108
109 return PROC_MEMINFO;
110 }
111
112 /* Allocates space for a new sysmem object.
113 * Returns 0 if all went ok. Errors are returned as negative values. */
114
115 int
116 proc_sysmem_new (struct proc_sysmem **sysmem)
117 {
118 struct proc_sysmem *m;
119
120 m = calloc (1, sizeof (struct proc_sysmem));
121 if (!m)
122 return -ENOMEM;
123
124 m->refcount = 1;
125 m->data = calloc (1, sizeof (struct proc_sysmem_data));
126 if (!m->data)
127 {
128 free (m);
129 return -ENOMEM;
130 }
131
132 *sysmem = m;
133 return 0;
134 }
135
136 /* Fill the proc_sysmem structure pointed will the values found in the
137 * proc filesystem. */
138
139 void proc_sysmem_read (struct proc_sysmem *sysmem)
140 {
141 if (sysmem == NULL)
142 return;
143
144 struct proc_sysmem_data *data = sysmem->data;
145
146 const struct proc_table_struct sysmem_table[] = {
147 { "Active", &data->kb_active }, /* important */
148 { "Active(file)", &data->kb_active_file },
149 { "AnonPages", &data->kb_anon_pages },
150 { "Buffers", &data->kb_main_buffers }, /* important */
151 { "Cached", &data->kb_page_cache }, /* important */
152 { "Committed_AS", &data->kb_committed_as },
153 { "Dirty", &data->kb_dirty }, /* kB version of vmstat nr_dirty */
154 { "HighTotal", &data->kb_high_total },
155 { "Inact_clean", &data->kb_inact_clean },
156 { "Inact_dirty", &data->kb_inact_dirty },
157 { "Inact_laundry", &data->kb_inact_laundry },
158 { "Inactive", &data->kb_inactive }, /* important */
159 { "Inactive(file)", &data->kb_inactive_file },
160 { "LowFree", &data->kb_low_free },
161 { "LowTotal", &data->kb_low_total },
162 { "MemAvailable", &data->kb_main_available }, /* kernel 3.14 and later */
163 { "MemFree", &data->kb_main_free }, /* important */
164 { "MemTotal", &data->kb_main_total }, /* important */
165 { "SReclaimable", &data->kb_slab_reclaimable }, /* dentry and inode structures */
166 { "Shmem", &data->kb_main_shared }, /* kernel 2.6.32 and later */
167 { "Slab", &data->kb_slab }, /* kB version of vmstat nr_slab */
168 { "SwapCached", &data->kb_swap_cached }, /* late 2.4 and 2.6+ only */
169 { "SwapFree", &data->kb_swap_free }, /* important */
170 { "SwapTotal", &data->kb_swap_total }, /* important */
171 };
172 const int sysmem_table_count = sizeof (sysmem_table) / sizeof (proc_table_struct);
173
174 data->kb_inactive = MEMINFO_UNSET;
175 data->kb_low_total = MEMINFO_UNSET;
176 data->kb_main_available = MEMINFO_UNSET;
177
178 procparser (get_path_proc_meminfo (), sysmem_table, sysmem_table_count, ':');
179
180 if (!data->kb_low_total)
181 { /* low==main except with large-memory support */
182 data->kb_low_total = data->kb_main_total;
183 data->kb_low_free = data->kb_main_free;
184 }
185
186 if (data->kb_inactive == MEMINFO_UNSET)
187 {
188 data->kb_inactive =
189 data->kb_inact_dirty + data->kb_inact_clean +
190 data->kb_inact_laundry;
191 }
192
193 /* zero? might need fallback for 2.6.27 <= kernel < 3.14 */
194 if (data->kb_main_available == MEMINFO_UNSET)
195 {
196 dbg ("MemAvailable is not provided by /proc/meminfo (kernel %u)...",
197 linux_version ());
198 if (linux_version () < KERNEL_VERSION (2, 6, 27))
199 {
200 dbg ("...falling back to MemFree\n");
201 data->kb_main_available = data->kb_main_free;
202 }
203 else
204 {
205 dbg ("...let's calculate the value...\n");
206 unsigned long long kb_min_free;
207 sysfsparser_getvalue (&kb_min_free, PATH_VM_MIN_FREE_KB);
208 /* should be equal to sum of all 'low' fields in /proc/zoneinfo */
209 unsigned long watermark_low = kb_min_free * 5 / 4;
210
211 signed long mem_available =
212 (signed long) data->kb_main_free - watermark_low
213 + data->kb_inactive_file + data->kb_active_file
214 #define MIN(x,y) ((x) < (y) ? (x) : (y))
215 - MIN((data->kb_inactive_file + data->kb_active_file) / 2,
216 watermark_low) + data->kb_slab_reclaimable
217 - MIN(data->kb_slab_reclaimable / 2, watermark_low);
218 #undef MIN
219 if (mem_available < 0)
220 mem_available = 0;
221 data->kb_main_available = (unsigned long) mem_available;
222 }
223 }
224
225 /* derived values */
226 data->kb_main_cached = data->kb_page_cache + data->kb_slab_reclaimable;
227 data->kb_main_used =
228 data->kb_main_total - data->kb_main_free - data->kb_main_cached -
229 data->kb_main_buffers;
230 }
231
232 /* Drop a reference of the memory library context. If the refcount of
233 * reaches zero, the resources of the context will be released. */
234
235 struct proc_sysmem *proc_sysmem_unref (struct proc_sysmem *sysmem)
236 {
237 if (sysmem == NULL)
238 return NULL;
239
240 sysmem->refcount--;
241 if (sysmem->refcount > 0)
242 return sysmem;
243
244 free (sysmem->data);
245 free (sysmem);
246 return NULL;
247 }
248
249 #define proc_sysmem_get(arg) \
250 unsigned long proc_sysmem_get_ ## arg (struct proc_sysmem *p) \
251 { return (p == NULL) ? 0 : p->data->kb_ ## arg; }
252
253 /* Memory that has been used more recently and usually not reclaimed unless
254 * absolutely necessary. */
255 proc_sysmem_get(active)
256 /* Non-file backed pages mapped into user-space page tables.
257 * High AnonPages means too much memory been allocated (mostly by malloc call)
258 * but not released yet (by instance because of memory leaks - process(es)
259 * that allocate but never free memory. */
260 proc_sysmem_get(anon_pages)
261 /* The amount of memory presently allocated on the system. */
262 proc_sysmem_get(committed_as)
263 /* Memory which is waiting to get written back to the disk. */
264 proc_sysmem_get(dirty)
265 /* Memory which has been less recently used.
266 * Inactive memory is the best candidate for reclaiming memory and so low
267 * inactive memory would mean that you are low on memory and the kernel may
268 + have to swap out process pages, or swap out the cache to disk or in the
269 * worst case if it runs out of swap space then begin killing processes. */
270 proc_sysmem_get(inactive)
271 proc_sysmem_get(main_available)
272 proc_sysmem_get(main_buffers)
273 proc_sysmem_get(main_cached)
274 proc_sysmem_get(main_free)
275 proc_sysmem_get(main_shared)
276 proc_sysmem_get(main_total)
277 proc_sysmem_get(main_used)
278
279 proc_sysmem_get(swap_cached)
280 proc_sysmem_get(swap_free)
281 proc_sysmem_get(swap_total)
282
283 #undef proc_sysmem_get
284
285 unsigned long
286 proc_sysmem_get_swap_used (struct proc_sysmem *sysmem)
287 {
288 return (sysmem == NULL) ? 0 :
289 sysmem->data->kb_swap_total - sysmem->data->kb_swap_free;
290 }
291
292 #endif /* NPL_TESTING */