Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2017,2024 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/vmstat.h>
27
28 typedef struct proc_vmem
29 {
30 int refcount;
31 struct vmstat_info *info; /* proc/vmstat.h */
32 } proc_vmem_t;
33
34 int
35 proc_vmem_new (struct proc_vmem **vmem)
36 {
37 struct proc_vmem *vm;
38
39 vm = calloc (1, sizeof (struct proc_vmem));
40 if (!vm)
41 return -ENOMEM;
42
43 vm->refcount = 1;
44
45 if (procps_vmstat_new (&vm->info) < 0)
46 return -ENOMEM;
47
48 *vmem = vm;
49 return 0;
50 }
51
52 void
53 proc_vmem_read (struct proc_vmem *vmem __attribute__ ((unused)))
54 {
55 }
56
57 struct proc_vmem *
58 proc_vmem_unref (struct proc_vmem *vmem)
59 {
60 if (vmem == NULL)
61 return NULL;
62
63 vmem->refcount--;
64 if (vmem->refcount > 0)
65 return vmem;
66
67 procps_vmstat_unref (&(vmem->info));
68 free (vmem);
69 return NULL;
70 }
71
72 #define proc_vmem_get(arg, item) \
73 unsigned long proc_vmem_get_ ## arg (struct proc_vmem *p) \
74 { return (p == NULL) ? 0 : VMSTAT_GET(p->info, item, ul_int); }
75
76 proc_vmem_get (pgfault, VMSTAT_PGFAULT)
77 proc_vmem_get (pgfree, VMSTAT_PGFREE)
78 proc_vmem_get (pgmajfault, VMSTAT_PGMAJFAULT)
79 proc_vmem_get (pgpgin, VMSTAT_PGPGIN) proc_vmem_get (pgpgout, VMSTAT_PGPGOUT)
80 proc_vmem_get (pswpin, VMSTAT_PSWPIN)
81 proc_vmem_get (pswpout, VMSTAT_PSWPOUT)
82
83 #undef proc_vmem_get
84
85 /*
86 * DMA memory zone:
87 * - low 16MB of memory
88 * - exists for historical reasons, sometime there was hardware that
89 * could only do DMA in this area
90 * DMA32 memory zone:
91 * - only in 64-bit linux
92 * - low ~4GBytes of memory
93 * - today, there is hardware that can do DMA to 4GBytes
94 * Normal memory zone:
95 * different on 32-bit and 64-bit machines
96 * - 32-bit: Memory from 16MB to 896MB
97 * - 64-bit: Memory above ~4GB
98 * HighMem memory zone:
99 * - only on 32-bit Linux
100 * - all Memory above ~896 MB
101 * - is not permanently or automatically mapped into the kernel’s
102 * address space
103 *
104 * cat /proc/pagetypeinfo
105 */
106
107 #define GET_DATA(item) VMSTAT_GET(vmem->info, item, ul_int)
108
109 unsigned long
110 proc_vmem_get_pgsteal (struct proc_vmem *vmem)
111 {
112 if (vmem == NULL)
113 return 0;
114
115 return GET_DATA (VMSTAT_PGSTEAL_DIRECT);
116 }
117
118 unsigned long
119 proc_vmem_get_pgscand (struct proc_vmem *vmem)
120 {
121 if (vmem == NULL)
122 return 0;
123
124 return GET_DATA (VMSTAT_PGSCAN_DIRECT);
125 }
126
127 unsigned long
128 proc_vmem_get_pgscank (struct proc_vmem *vmem)
129 {
130 if (vmem == NULL)
131 return 0;
132
133 return GET_DATA (VMSTAT_PGSCAN_KSWAPD);
134 }
135
136 #undef GET_DATA