Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/tests/tslibvminfo.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2017 Davide Madrisan <[email protected]>
5 *
6 * Unit test for lib/vminfo.c
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 */
22
23 #include "common.h"
24 #include "testutils.h"
25
26 #ifndef HAVE_LIBPROCPS
27
28 # define NPL_TESTING
29 # include "../lib/vminfo.c"
30 # undef NPL_TESTING
31
32 proc_vmem_t *vmem = NULL;
33
34 static int
35 test_memory_init ()
36 {
37 int ret = 0;
38 const char *env_variable = "NPL_TEST_PATH_PROCVMSTAT";
39
40 ret = proc_vmem_new (&vmem);
41 if (ret < 0)
42 return EXIT_AM_HARDFAIL;
43
44 ret = setenv (env_variable, NPL_TEST_PATH_PROCVMSTAT, 1);
45 if (ret < 0)
46 return EXIT_AM_HARDFAIL;
47
48 proc_vmem_read (vmem);
49 unsetenv (env_variable);
50
51 return 0;
52 }
53
54 static void
55 test_memory_release ()
56 {
57 proc_vmem_unref (vmem);
58 }
59
60 typedef struct test_data
61 {
62 unsigned long value;
63 unsigned long expect_value;
64 } test_data;
65
66 static int
67 test_procvminfo (const void *tdata)
68 {
69 const struct test_data *data = tdata;
70 int ret = 0;
71
72 TEST_ASSERT_EQUAL_NUMERIC (data->value, data->expect_value);
73 return ret;
74 }
75
76 # define test_memory_label(arg, value) \
77 static int test_memory_ ## arg (const void *tdata) \
78 { \
79 int err, ret = 0; \
80 unsigned long kb_value; \
81 const char *env_variable = "NPL_TEST_PATH_PROCVMSTAT"; \
82 \
83 /* read the data from (a static copy of) /proc/vmstat */ \
84 err = setenv (env_variable, NPL_TEST_PATH_PROCVMSTAT, 1); \
85 if (err < 0) \
86 return EXIT_AM_HARDFAIL; \
87 \
88 kb_value = proc_vmem_get_ ## arg (vmem); \
89 TEST_ASSERT_EQUAL_NUMERIC (kb_value, value); \
90 \
91 return ret; \
92 }
93
94 test_memory_label (pgpgin, 2581510UL);
95 test_memory_label (pgpgout, 34298109UL);
96 test_memory_label (pswpin, 10402UL);
97 test_memory_label (pswpout, 12250UL);
98 test_memory_label (pgfault, 91270548UL);
99 test_memory_label (pgmajfault, 9363UL);
100 test_memory_label (pgfree, 91315814UL);
101 /*test_memory_label (pgsteal, 0UL);*/
102 /*test_memory_label (pgscand, 0UL);*/
103 /*test_memory_label (pgscank, 0UL);*/
104
105 static int
106 mymain (void)
107 {
108 int err, ret = 0;
109
110 if ((err = test_memory_init ()) != 0)
111 return err;
112
113 proc_vmem_data_t *procdata = vmem->data;
114
115 # define DO_TEST_PROC(MEMTYPE, VALUE, EXPECT_VALUE) \
116 do \
117 { \
118 test_data data = { \
119 .value = VALUE, \
120 .expect_value = EXPECT_VALUE, \
121 }; \
122 if (test_run("check " PROC_STAT " parser for " MEMTYPE " memory", \
123 test_procvminfo, (&data)) < 0) \
124 ret = -1; \
125 } \
126 while (0)
127
128 /* test the proc parser */
129
130 DO_TEST_PROC ("allocstall", procdata->vm_allocstall, 0L);
131 DO_TEST_PROC ("kswapd_inodesteal", procdata->vm_kswapd_inodesteal, 0L);
132 /* kswapd_steal */
133 DO_TEST_PROC ("nr_dirty", procdata->vm_nr_dirty, 20L);
134 DO_TEST_PROC ("nr_mapped", procdata->vm_nr_mapped, 314943L);
135 DO_TEST_PROC ("nr_page_table_pages",
136 procdata->vm_nr_page_table_pages, 10601L);
137 /* nr_pagecache */
138 /* nr_reverse_maps */
139 /* nr_slab */
140 DO_TEST_PROC ("nr_unstable", procdata->vm_nr_unstable, 0L);
141 DO_TEST_PROC ("nr_writeback", procdata->vm_nr_writeback, 0L);
142 DO_TEST_PROC ("pageoutrun", procdata->vm_pageoutrun, 1L);
143 DO_TEST_PROC ("pgactivate", procdata->vm_pgactivate, 764549L);
144 /* pgalloc */
145 DO_TEST_PROC ("pgalloc_dma", procdata->vm_pgalloc_dma, 6L);
146 /* pgalloc_high */
147 DO_TEST_PROC ("pgalloc_normal", procdata->vm_pgalloc_normal, 70896125L);
148 DO_TEST_PROC ("pgdeactivate", procdata->vm_pgdeactivate, 0L);
149 DO_TEST_PROC ("pgfault", procdata->vm_pgfault, 91270548L);
150 DO_TEST_PROC ("pgfree", procdata->vm_pgfree, 91315814L);
151 DO_TEST_PROC ("pginodesteal", procdata->vm_pginodesteal, 0L);
152 DO_TEST_PROC ("pgmajfault", procdata->vm_pgmajfault, 9363L);
153 DO_TEST_PROC ("pgpgin", procdata->vm_pgpgin, 2581510L);
154 DO_TEST_PROC ("pgpgout", procdata->vm_pgpgout, 34298109L);
155 DO_TEST_PROC ("pgrefill", procdata->vm_pgrefill, 0L);
156 DO_TEST_PROC ("pgrefill_dma", procdata->vm_pgrefill_dma, 0L);
157 /* pgrefill_high */
158 DO_TEST_PROC ("pgrefill_normal", procdata->vm_pgrefill_normal, 0L);
159 DO_TEST_PROC ("pgrotated", procdata->vm_pgrotated, 407L);
160 DO_TEST_PROC ("pgscan", procdata->vm_pgscan, 0L);
161 DO_TEST_PROC ("pgscan_direct_dma", procdata->vm_pgscan_direct_dma, 0L);
162 /* pgscan_direct_high */
163 DO_TEST_PROC ("pgscan_direct_normal", procdata->vm_pgscan_direct_normal, 0L);
164 DO_TEST_PROC ("pgscan_kswapd_dma", procdata->vm_pgscan_kswapd_dma, 0L);
165 /* pgscan_kswapd_high */
166 DO_TEST_PROC ("pgscan_kswapd_normal", procdata->vm_pgscan_kswapd_normal, 0L);
167 DO_TEST_PROC ("pgsteal", procdata->vm_pgsteal, 0L);
168 /* pgsteal_dma */
169 /* pgsteal_high */
170 /* pgsteal_normal */
171 DO_TEST_PROC ("pswpin", procdata->vm_pswpin, 10402L);
172 DO_TEST_PROC ("pswpout", procdata->vm_pswpout, 12250L);
173 DO_TEST_PROC ("slabs_scanned", procdata->vm_slabs_scanned, 0L);
174
175 /* test the public interface of the library */
176
177 # define DO_TEST(MSG, FUNC, DATA) \
178 do { if (test_run (MSG, FUNC, DATA) < 0) ret = -1; } while (0)
179
180 DO_TEST ("check pgpgin virtual memory stat", test_memory_pgpgin, NULL);
181 DO_TEST ("check pgpgout virtual memory stat", test_memory_pgpgout, NULL);
182
183 /* used by check_swap, check_paging */
184 DO_TEST ("check pswpin virtual memory stat", test_memory_pswpin, NULL);
185 DO_TEST ("check pswpout virtual memory stat", test_memory_pswpout, NULL);
186
187 /* used by check_paging */
188 DO_TEST ("check pgfault virtual memory stat", test_memory_pgfault, NULL);
189 DO_TEST ("check pgmajfault virtual memory stat", test_memory_pgmajfault, NULL);
190 DO_TEST ("check pgfree virtual memory stat", test_memory_pgfree, NULL);
191 /*DO_TEST ("check pgsteal virtual memory stat", test_memory_pgsteal, NULL);*/
192 /*DO_TEST ("check pgscand virtual memory stat", test_memory_pgscand, NULL);*/
193 /*DO_TEST ("check pgscank virtual memory stat", test_memory_pgscank, NULL);*/
194
195 test_memory_release ();
196
197 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
198 }
199
200 TEST_MAIN (mymain)
201
202 #else
203
204 int
205 main (void)
206 {
207 return EXIT_AM_SKIP;
208 }
209
210 #endif /* HAVE_LIBPROCPS */