tighten formatting rules
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed_meminfo.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2008--2013 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23
24 /*
25  * File for parsing top-level /proc entities.
26  * Copyright Copyright (C) 1992-1998 by Michael K. Johnson, johnsonm@redhat.com
27  * Copyright 1998-2003 Albert Cahalan
28  * June 2003, Fabian Frederick, disk and slab info
29  *
30  * This library is free software; you can redistribute it and/or
31  * modify it under the terms of the GNU Lesser General Public
32  * License as published by the Free Software Foundation; either
33  * version 2.1 of the License, or (at your option) any later version.
34  *
35  * This library is distributed in the hope that it will be useful,
36  * but WITHOUT ANY WARRANTY; without even the implied warranty of
37  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
38  * Lesser General Public License for more details.
39  *
40  * You should have received a copy of the GNU Lesser General Public
41  * License along with this library; if not, write to the Free Software
42  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
43  */
44
45 #define BAD_OPEN_MESSAGE                                        \
46   "Error: /proc must be mounted\n"                                \
47   "  To mount /proc at boot you need an /etc/fstab line like:\n"  \
48   "      proc   /proc   proc    defaults\n"                       \
49   "  In the meantime, run \"mount proc /proc -t proc\"\n"
50
51 #define STAT_FILE    "/proc/stat"
52 // static int stat_fd = -1;
53 #define UPTIME_FILE  "/proc/uptime"
54 // static int uptime_fd = -1;
55 #define LOADAVG_FILE "/proc/loadavg"
56 // static int loadavg_fd = -1;
57 #define MEMINFO_FILE "/proc/meminfo"
58 static int meminfo_fd = -1;
59 #define VMINFO_FILE "/proc/vmstat"
60 // static int vminfo_fd = -1;
61
62 // As of 2.6.24 /proc/meminfo seems to need 888 on 64-bit,
63 // and would need 1258 if the obsolete fields were there.
64 static char buf[2048];
65
66 /* This macro opens filename only if necessary and seeks to 0 so
67  * that successive calls to the functions are more efficient.
68  * It also reads the current contents of the file into the global buf.
69  */
70 #define FILE_TO_BUF(filename, fd) do {                           \
71     static int local_n;                                         \
72     if (fd == -1 && (fd = open (filename, O_RDONLY)) == -1) {    \
73       fputs (BAD_OPEN_MESSAGE, stderr);                        \
74       fflush (NULL);                                           \
75       _exit (102);                                             \
76     }                                                           \
77     lseek (fd, 0L, SEEK_SET);                                    \
78     if ((local_n = read (fd, buf, sizeof buf - 1)) < 0) {        \
79       perror (filename);                                       \
80       fflush (NULL);                                           \
81       _exit (103);                                             \
82     }                                                           \
83     buf[local_n] = '\0';                                        \
84 } while (0)
85
86
87 /***********************************************************************/
88 /*
89  * Copyright 1999 by Albert Cahalan; all rights reserved.
90  * This file may be used subject to the terms and conditions of the
91  * GNU Library General Public License Version 2, or any later version
92  * at your option, as published by the Free Software Foundation.
93  * This program is distributed in the hope that it will be useful,
94  * but WITHOUT ANY WARRANTY; without even the implied warranty of
95  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
96  * GNU Library General Public License for more details.
97  */
98
99 typedef struct mem_table_struct
100 {
101   const char *name;     /* memory type name */
102   unsigned long *slot; /* slot in return struct */
103 } mem_table_struct;
104
105 static int
106 compare_mem_table_structs (const void *a, const void *b)
107 {
108   return strcmp (((const mem_table_struct*) a)->name, ((const
109                                                         mem_table_struct*) b)->
110                  name);
111 }
112
113
114 /* example data, following junk, with comments added:
115  *
116  * MemTotal:        61768 kB    old
117  * MemFree:          1436 kB    old
118  * MemShared:           0 kB    old (now always zero; not calculated)
119  * Buffers:          1312 kB    old
120  * Cached:          20932 kB    old
121  * Active:          12464 kB    new
122  * Inact_dirty:      7772 kB    new
123  * Inact_clean:      2008 kB    new
124  * Inact_target:        0 kB    new
125  * Inact_laundry:       0 kB    new, and might be missing too
126  * HighTotal:           0 kB
127  * HighFree:            0 kB
128  * LowTotal:        61768 kB
129  * LowFree:          1436 kB
130  * SwapTotal:      122580 kB    old
131  * SwapFree:        60352 kB    old
132  * Inactive:        20420 kB    2.5.41+
133  * Dirty:               0 kB    2.5.41+
134  * Writeback:           0 kB    2.5.41+
135  * Mapped:           9792 kB    2.5.41+
136  * Slab:             4564 kB    2.5.41+
137  * Committed_AS:     8440 kB    2.5.41+
138  * PageTables:        304 kB    2.5.41+
139  * ReverseMaps:      5738       2.5.41+
140  * SwapCached:          0 kB    2.5.??+
141  * HugePages_Total:   220       2.5.??+
142  * HugePages_Free:    138       2.5.??+
143  * Hugepagesize:     4096 kB    2.5.??+
144  */
145
146 /* obsolete */
147 unsigned long kb_main_shared;
148 /* old but still kicking -- the important stuff */
149 unsigned long kb_main_buffers;
150 unsigned long kb_main_cached;
151 unsigned long kb_main_free;
152 unsigned long kb_main_total;
153 unsigned long kb_swap_free;
154 unsigned long kb_swap_total;
155 /* recently introduced */
156 unsigned long kb_high_free;
157 unsigned long kb_high_total;
158 unsigned long kb_low_free;
159 unsigned long kb_low_total;
160 /* 2.4.xx era */
161 unsigned long kb_active;
162 unsigned long kb_inact_laundry;
163 unsigned long kb_inact_dirty;
164 unsigned long kb_inact_clean;
165 unsigned long kb_inact_target;
166 unsigned long kb_swap_cached;  /* late 2.4 and 2.6+ only */
167 /* derived values */
168 unsigned long kb_swap_used;
169 unsigned long kb_main_used;
170 /* 2.5.41+ */
171 unsigned long kb_writeback;
172 unsigned long kb_slab;
173 unsigned long nr_reversemaps;
174 unsigned long kb_committed_as;
175 unsigned long kb_dirty;
176 unsigned long kb_inactive;
177 unsigned long kb_mapped;
178 unsigned long kb_pagetables;
179 // seen on a 2.6.x kernel:
180 static unsigned long kb_vmalloc_chunk;
181 static unsigned long kb_vmalloc_total;
182 static unsigned long kb_vmalloc_used;
183 // seen on 2.6.24-rc6-git12
184 static unsigned long kb_anon_pages;
185 static unsigned long kb_bounce;
186 static unsigned long kb_commit_limit;
187 static unsigned long kb_nfs_unstable;
188 static unsigned long kb_swap_reclaimable;
189 static unsigned long kb_swap_unreclaimable;
190
191 void
192 meminfo (void)
193 {
194   char namebuf[16]; /* big enough to hold any row name */
195   mem_table_struct findme = { namebuf, NULL };
196   mem_table_struct *found;
197   char *head;
198   char *tail;
199   static const mem_table_struct mem_table[] = {
200     { "Active", &kb_active },         // important
201     { "AnonPages", &kb_anon_pages },
202     { "Bounce", &kb_bounce },
203     { "Buffers", &kb_main_buffers },  // important
204     { "Cached", &kb_main_cached },    // important
205     { "CommitLimit", &kb_commit_limit },
206     { "Committed_AS", &kb_committed_as },
207     { "Dirty", &kb_dirty },           // kB version of vmstat nr_dirty
208     { "HighFree", &kb_high_free },
209     { "HighTotal", &kb_high_total },
210     { "Inact_clean", &kb_inact_clean },
211     { "Inact_dirty", &kb_inact_dirty },
212     { "Inact_laundry", &kb_inact_laundry },
213     { "Inact_target", &kb_inact_target },
214     { "Inactive", &kb_inactive },     // important
215     { "LowFree", &kb_low_free },
216     { "LowTotal", &kb_low_total },
217     { "Mapped", &kb_mapped },         // kB version of vmstat nr_mapped
218     { "MemFree", &kb_main_free },     // important
219     { "MemShared", &kb_main_shared }, // important, but now gone!
220     { "MemTotal", &kb_main_total },   // important
221     { "NFS_Unstable", &kb_nfs_unstable },
222     { "PageTables", &kb_pagetables }, // kB version of vmstat nr_page_table_pages
223     { "ReverseMaps", &nr_reversemaps }, // same as vmstat nr_page_table_pages
224     { "SReclaimable", &kb_swap_reclaimable }, // "swap reclaimable" (dentry and inode structures)
225     { "SUnreclaim", &kb_swap_unreclaimable },
226     { "Slab", &kb_slab },             // kB version of vmstat nr_slab
227     { "SwapCached", &kb_swap_cached },
228     { "SwapFree", &kb_swap_free },    // important
229     { "SwapTotal", &kb_swap_total },  // important
230     { "VmallocChunk", &kb_vmalloc_chunk },
231     { "VmallocTotal", &kb_vmalloc_total },
232     { "VmallocUsed", &kb_vmalloc_used },
233     { "Writeback", &kb_writeback },   // kB version of vmstat nr_writeback
234   };
235   const int mem_table_count = sizeof(mem_table) / sizeof(mem_table_struct);
236
237   FILE_TO_BUF (MEMINFO_FILE, meminfo_fd);
238
239   kb_inactive = ~0UL;
240
241   head = buf;
242   for (;;)
243   {
244     tail = strchr (head, ':');
245     if (! tail)
246       break;
247     *tail = '\0';
248     if (strlen (head) >= sizeof(namebuf))
249     {
250       head = tail + 1;
251       goto nextline;
252     }
253     strcpy (namebuf, head);
254     found = bsearch (&findme, mem_table, mem_table_count,
255                      sizeof(mem_table_struct), compare_mem_table_structs
256                      );
257     head = tail + 1;
258     if (! found)
259       goto nextline;
260     *(found->slot) = (unsigned long) strtoull (head, &tail, 10);
261 nextline:
262     tail = strchr (head, '\n');
263     if (! tail)
264       break;
265     head = tail + 1;
266   }
267   if (! kb_low_total)   /* low==main except with large-memory support */
268   {
269     kb_low_total = kb_main_total;
270     kb_low_free = kb_main_free;
271   }
272   if (kb_inactive == ~0UL)
273   {
274     kb_inactive = kb_inact_dirty + kb_inact_clean + kb_inact_laundry;
275   }
276   kb_swap_used = kb_swap_total - kb_swap_free;
277   kb_main_used = kb_main_total - kb_main_free;
278 }