Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2017,2014 Davide Madrisan <[email protected]>
5 *
6 * A library for checking memory and swap usage on linux.
7 * This library is a front-end for `procps-newlib':
8 * https://gitlab.com/madrisan/procps/tree/newlib
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <libproc2/meminfo.h>
27
28 typedef struct proc_sysmem
29 {
30 int refcount;
31 struct meminfo_info *info; /* proc/meminfo.{h,c} */
32 } proc_sysmem_t;
33
34 int
35 proc_sysmem_new (struct proc_sysmem **sysmem)
36 {
37 struct proc_sysmem *m;
38
39 m = calloc (1, sizeof (struct proc_sysmem));
40 if (!m)
41 return -ENOMEM;
42
43 m->refcount = 1;
44
45 if (procps_meminfo_new (&m->info) < 0)
46 return -ENOMEM;
47
48 *sysmem = m;
49 return 0;
50 }
51
52 void
53 proc_sysmem_read (struct proc_sysmem *sysmem __attribute__ ((unused)))
54 {
55 }
56
57 struct proc_sysmem *
58 proc_sysmem_unref (struct proc_sysmem *sysmem)
59 {
60 if (sysmem == NULL)
61 return NULL;
62
63 sysmem->refcount--;
64 if (sysmem->refcount > 0)
65 return sysmem;
66
67 procps_meminfo_unref (&(sysmem->info));
68 free (sysmem);
69 return NULL;
70 }
71
72 #define proc_sysmem_get(arg, item) \
73 unsigned long proc_sysmem_get_ ## arg (struct proc_sysmem *p) \
74 { return (p == NULL) ? 0 : MEMINFO_GET (p->info, item, ul_int); }
75
76 /* Memory that has been used more recently and usually not reclaimed unless
77 * absolutely necessary. */
78 proc_sysmem_get(active, MEMINFO_MEM_ACTIVE)
79 /* Non-file backed pages mapped into user-space page tables.
80 * High AnonPages means too much memory been allocated (mostly by malloc call)
81 * but not released yet (by instance because of memory leaks - process(es)
82 * that allocate but never free memory. */
83 proc_sysmem_get(anon_pages, MEMINFO_MEM_ANON)
84 /* The amount of memory presently allocated on the system. */
85 proc_sysmem_get(committed_as, MEMINFO_MEM_COMMITTED_AS)
86 /* Memory which is waiting to get written back to the disk. */
87 proc_sysmem_get(dirty, MEMINFO_MEM_DIRTY)
88 /* Memory which has been less recently used.
89 * Inactive memory is the best candidate for reclaiming memory and so low
90 * inactive memory would mean that you are low on memory and the kernel may
91 + have to swap out process pages, or swap out the cache to disk or in the
92 * worst case if it runs out of swap space then begin killing processes. */
93 proc_sysmem_get(inactive, MEMINFO_MEM_INACTIVE)
94 proc_sysmem_get(main_available, MEMINFO_MEM_AVAILABLE)
95 proc_sysmem_get(main_buffers, MEMINFO_MEM_BUFFERS)
96 proc_sysmem_get(main_cached, MEMINFO_MEM_CACHED_ALL)
97 proc_sysmem_get(main_free, MEMINFO_MEM_FREE)
98 proc_sysmem_get(main_shared, MEMINFO_MEM_SHARED)
99 proc_sysmem_get(main_total, MEMINFO_MEM_TOTAL)
100 proc_sysmem_get(main_used, MEMINFO_MEM_USED)
101
102 proc_sysmem_get(swap_cached, MEMINFO_SWAP_CACHED)
103 proc_sysmem_get(swap_free, MEMINFO_SWAP_FREE)
104 proc_sysmem_get(swap_total, MEMINFO_SWAP_TOTAL)
105
106 unsigned long
107 proc_sysmem_get_swap_used (struct proc_sysmem *sysmem)
108 {
109 return (sysmem == NULL) ? 0 :
110 proc_sysmem_get_swap_total (sysmem) - proc_sysmem_get_swap_free (sysmem);
111 }
112
113 #undef proc_sysmem_get