diff options
Diffstat (limited to 'src/testbed/gnunet-service-testbed_meminfo.c')
-rw-r--r-- | src/testbed/gnunet-service-testbed_meminfo.c | 185 |
1 files changed, 97 insertions, 88 deletions
diff --git a/src/testbed/gnunet-service-testbed_meminfo.c b/src/testbed/gnunet-service-testbed_meminfo.c index 7b42331b5..27778447c 100644 --- a/src/testbed/gnunet-service-testbed_meminfo.c +++ b/src/testbed/gnunet-service-testbed_meminfo.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 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/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | #include "platform.h" | 21 | #include "platform.h" |
22 | #include "gnunet_util_lib.h" | 22 | #include "gnunet_util_lib.h" |
@@ -42,11 +42,11 @@ | |||
42 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 42 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
43 | */ | 43 | */ |
44 | 44 | ||
45 | #define BAD_OPEN_MESSAGE \ | 45 | #define BAD_OPEN_MESSAGE \ |
46 | "Error: /proc must be mounted\n" \ | 46 | "Error: /proc must be mounted\n" \ |
47 | " To mount /proc at boot you need an /etc/fstab line like:\n" \ | 47 | " To mount /proc at boot you need an /etc/fstab line like:\n" \ |
48 | " proc /proc proc defaults\n" \ | 48 | " proc /proc proc defaults\n" \ |
49 | " In the meantime, run \"mount proc /proc -t proc\"\n" | 49 | " In the meantime, run \"mount proc /proc -t proc\"\n" |
50 | 50 | ||
51 | #define STAT_FILE "/proc/stat" | 51 | #define STAT_FILE "/proc/stat" |
52 | //static int stat_fd = -1; | 52 | //static int stat_fd = -1; |
@@ -67,21 +67,21 @@ static char buf[2048]; | |||
67 | * that successive calls to the functions are more efficient. | 67 | * that successive calls to the functions are more efficient. |
68 | * It also reads the current contents of the file into the global buf. | 68 | * It also reads the current contents of the file into the global buf. |
69 | */ | 69 | */ |
70 | #define FILE_TO_BUF(filename, fd) do{ \ | 70 | #define FILE_TO_BUF(filename, fd) do { \ |
71 | static int local_n; \ | 71 | static int local_n; \ |
72 | if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \ | 72 | if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \ |
73 | fputs(BAD_OPEN_MESSAGE, stderr); \ | 73 | fputs(BAD_OPEN_MESSAGE, stderr); \ |
74 | fflush(NULL); \ | 74 | fflush(NULL); \ |
75 | _exit(102); \ | 75 | _exit(102); \ |
76 | } \ | 76 | } \ |
77 | lseek(fd, 0L, SEEK_SET); \ | 77 | lseek(fd, 0L, SEEK_SET); \ |
78 | if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \ | 78 | if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \ |
79 | perror(filename); \ | 79 | perror(filename); \ |
80 | fflush(NULL); \ | 80 | fflush(NULL); \ |
81 | _exit(103); \ | 81 | _exit(103); \ |
82 | } \ | 82 | } \ |
83 | buf[local_n] = '\0'; \ | 83 | buf[local_n] = '\0'; \ |
84 | }while(0) | 84 | } while (0) |
85 | 85 | ||
86 | 86 | ||
87 | /***********************************************************************/ | 87 | /***********************************************************************/ |
@@ -101,8 +101,9 @@ typedef struct mem_table_struct { | |||
101 | unsigned long *slot; /* slot in return struct */ | 101 | unsigned long *slot; /* slot in return struct */ |
102 | } mem_table_struct; | 102 | } mem_table_struct; |
103 | 103 | ||
104 | static int compare_mem_table_structs(const void *a, const void *b){ | 104 | static int compare_mem_table_structs(const void *a, const void *b) |
105 | return strcmp(((const mem_table_struct*)a)->name,((const mem_table_struct*)b)->name); | 105 | { |
106 | return strcmp(((const mem_table_struct*)a)->name, ((const mem_table_struct*)b)->name); | ||
106 | } | 107 | } |
107 | 108 | ||
108 | /* example data, following junk, with comments added: | 109 | /* example data, following junk, with comments added: |
@@ -182,82 +183,90 @@ static unsigned long kb_nfs_unstable; | |||
182 | static unsigned long kb_swap_reclaimable; | 183 | static unsigned long kb_swap_reclaimable; |
183 | static unsigned long kb_swap_unreclaimable; | 184 | static unsigned long kb_swap_unreclaimable; |
184 | 185 | ||
185 | void meminfo(void){ | 186 | void meminfo(void) |
187 | { | ||
186 | char namebuf[16]; /* big enough to hold any row name */ | 188 | char namebuf[16]; /* big enough to hold any row name */ |
187 | mem_table_struct findme = { namebuf, NULL}; | 189 | mem_table_struct findme = { namebuf, NULL }; |
188 | mem_table_struct *found; | 190 | mem_table_struct *found; |
189 | char *head; | 191 | char *head; |
190 | char *tail; | 192 | char *tail; |
191 | static const mem_table_struct mem_table[] = { | 193 | static const mem_table_struct mem_table[] = { |
192 | {"Active", &kb_active}, // important | 194 | { "Active", &kb_active }, // important |
193 | {"AnonPages", &kb_anon_pages}, | 195 | { "AnonPages", &kb_anon_pages }, |
194 | {"Bounce", &kb_bounce}, | 196 | { "Bounce", &kb_bounce }, |
195 | {"Buffers", &kb_main_buffers}, // important | 197 | { "Buffers", &kb_main_buffers }, // important |
196 | {"Cached", &kb_main_cached}, // important | 198 | { "Cached", &kb_main_cached }, // important |
197 | {"CommitLimit", &kb_commit_limit}, | 199 | { "CommitLimit", &kb_commit_limit }, |
198 | {"Committed_AS", &kb_committed_as}, | 200 | { "Committed_AS", &kb_committed_as }, |
199 | {"Dirty", &kb_dirty}, // kB version of vmstat nr_dirty | 201 | { "Dirty", &kb_dirty }, // kB version of vmstat nr_dirty |
200 | {"HighFree", &kb_high_free}, | 202 | { "HighFree", &kb_high_free }, |
201 | {"HighTotal", &kb_high_total}, | 203 | { "HighTotal", &kb_high_total }, |
202 | {"Inact_clean", &kb_inact_clean}, | 204 | { "Inact_clean", &kb_inact_clean }, |
203 | {"Inact_dirty", &kb_inact_dirty}, | 205 | { "Inact_dirty", &kb_inact_dirty }, |
204 | {"Inact_laundry",&kb_inact_laundry}, | 206 | { "Inact_laundry", &kb_inact_laundry }, |
205 | {"Inact_target", &kb_inact_target}, | 207 | { "Inact_target", &kb_inact_target }, |
206 | {"Inactive", &kb_inactive}, // important | 208 | { "Inactive", &kb_inactive }, // important |
207 | {"LowFree", &kb_low_free}, | 209 | { "LowFree", &kb_low_free }, |
208 | {"LowTotal", &kb_low_total}, | 210 | { "LowTotal", &kb_low_total }, |
209 | {"Mapped", &kb_mapped}, // kB version of vmstat nr_mapped | 211 | { "Mapped", &kb_mapped }, // kB version of vmstat nr_mapped |
210 | {"MemFree", &kb_main_free}, // important | 212 | { "MemFree", &kb_main_free }, // important |
211 | {"MemShared", &kb_main_shared}, // important, but now gone! | 213 | { "MemShared", &kb_main_shared }, // important, but now gone! |
212 | {"MemTotal", &kb_main_total}, // important | 214 | { "MemTotal", &kb_main_total }, // important |
213 | {"NFS_Unstable", &kb_nfs_unstable}, | 215 | { "NFS_Unstable", &kb_nfs_unstable }, |
214 | {"PageTables", &kb_pagetables}, // kB version of vmstat nr_page_table_pages | 216 | { "PageTables", &kb_pagetables }, // kB version of vmstat nr_page_table_pages |
215 | {"ReverseMaps", &nr_reversemaps}, // same as vmstat nr_page_table_pages | 217 | { "ReverseMaps", &nr_reversemaps }, // same as vmstat nr_page_table_pages |
216 | {"SReclaimable", &kb_swap_reclaimable}, // "swap reclaimable" (dentry and inode structures) | 218 | { "SReclaimable", &kb_swap_reclaimable }, // "swap reclaimable" (dentry and inode structures) |
217 | {"SUnreclaim", &kb_swap_unreclaimable}, | 219 | { "SUnreclaim", &kb_swap_unreclaimable }, |
218 | {"Slab", &kb_slab}, // kB version of vmstat nr_slab | 220 | { "Slab", &kb_slab }, // kB version of vmstat nr_slab |
219 | {"SwapCached", &kb_swap_cached}, | 221 | { "SwapCached", &kb_swap_cached }, |
220 | {"SwapFree", &kb_swap_free}, // important | 222 | { "SwapFree", &kb_swap_free }, // important |
221 | {"SwapTotal", &kb_swap_total}, // important | 223 | { "SwapTotal", &kb_swap_total }, // important |
222 | {"VmallocChunk", &kb_vmalloc_chunk}, | 224 | { "VmallocChunk", &kb_vmalloc_chunk }, |
223 | {"VmallocTotal", &kb_vmalloc_total}, | 225 | { "VmallocTotal", &kb_vmalloc_total }, |
224 | {"VmallocUsed", &kb_vmalloc_used}, | 226 | { "VmallocUsed", &kb_vmalloc_used }, |
225 | {"Writeback", &kb_writeback}, // kB version of vmstat nr_writeback | 227 | { "Writeback", &kb_writeback }, // kB version of vmstat nr_writeback |
226 | }; | 228 | }; |
227 | const int mem_table_count = sizeof(mem_table)/sizeof(mem_table_struct); | 229 | const int mem_table_count = sizeof(mem_table) / sizeof(mem_table_struct); |
228 | 230 | ||
229 | FILE_TO_BUF(MEMINFO_FILE,meminfo_fd); | 231 | FILE_TO_BUF(MEMINFO_FILE, meminfo_fd); |
230 | 232 | ||
231 | kb_inactive = ~0UL; | 233 | kb_inactive = ~0UL; |
232 | 234 | ||
233 | head = buf; | 235 | head = buf; |
234 | for(;;){ | 236 | for (;;) |
235 | tail = strchr(head, ':'); | 237 | { |
236 | if(!tail) break; | 238 | tail = strchr(head, ':'); |
237 | *tail = '\0'; | 239 | if (!tail) |
238 | if(strlen(head) >= sizeof(namebuf)){ | 240 | break; |
239 | head = tail+1; | 241 | *tail = '\0'; |
240 | goto nextline; | 242 | if (strlen(head) >= sizeof(namebuf)) |
241 | } | 243 | { |
242 | strcpy(namebuf,head); | 244 | head = tail + 1; |
243 | found = bsearch(&findme, mem_table, mem_table_count, | 245 | goto nextline; |
244 | sizeof(mem_table_struct), compare_mem_table_structs | 246 | } |
245 | ); | 247 | strcpy(namebuf, head); |
246 | head = tail+1; | 248 | found = bsearch(&findme, mem_table, mem_table_count, |
247 | if(!found) goto nextline; | 249 | sizeof(mem_table_struct), compare_mem_table_structs |
248 | *(found->slot) = (unsigned long)strtoull(head,&tail,10); | 250 | ); |
251 | head = tail + 1; | ||
252 | if (!found) | ||
253 | goto nextline; | ||
254 | *(found->slot) = (unsigned long)strtoull(head, &tail, 10); | ||
249 | nextline: | 255 | nextline: |
250 | tail = strchr(head, '\n'); | 256 | tail = strchr(head, '\n'); |
251 | if(!tail) break; | 257 | if (!tail) |
252 | head = tail+1; | 258 | break; |
253 | } | 259 | head = tail + 1; |
254 | if(!kb_low_total){ /* low==main except with large-memory support */ | 260 | } |
255 | kb_low_total = kb_main_total; | 261 | if (!kb_low_total) /* low==main except with large-memory support */ |
256 | kb_low_free = kb_main_free; | 262 | { |
257 | } | 263 | kb_low_total = kb_main_total; |
258 | if(kb_inactive==~0UL){ | 264 | kb_low_free = kb_main_free; |
259 | kb_inactive = kb_inact_dirty + kb_inact_clean + kb_inact_laundry; | 265 | } |
260 | } | 266 | if (kb_inactive == ~0UL) |
267 | { | ||
268 | kb_inactive = kb_inact_dirty + kb_inact_clean + kb_inact_laundry; | ||
269 | } | ||
261 | kb_swap_used = kb_swap_total - kb_swap_free; | 270 | kb_swap_used = kb_swap_total - kb_swap_free; |
262 | kb_main_used = kb_main_total - kb_main_free; | 271 | kb_main_used = kb_main_total - kb_main_free; |
263 | } | 272 | } |