summaryrefslogtreecommitdiff
path: root/src/testbed/gnunet-service-testbed_meminfo.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testbed/gnunet-service-testbed_meminfo.c')
-rw-r--r--src/testbed/gnunet-service-testbed_meminfo.c107
1 files changed, 55 insertions, 52 deletions
diff --git a/src/testbed/gnunet-service-testbed_meminfo.c b/src/testbed/gnunet-service-testbed_meminfo.c
index 27778447c..5afce288b 100644
--- a/src/testbed/gnunet-service-testbed_meminfo.c
+++ b/src/testbed/gnunet-service-testbed_meminfo.c
@@ -49,15 +49,15 @@
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;
53#define UPTIME_FILE "/proc/uptime" 53#define UPTIME_FILE "/proc/uptime"
54//static int uptime_fd = -1; 54// static int uptime_fd = -1;
55#define LOADAVG_FILE "/proc/loadavg" 55#define LOADAVG_FILE "/proc/loadavg"
56//static int loadavg_fd = -1; 56// static int loadavg_fd = -1;
57#define MEMINFO_FILE "/proc/meminfo" 57#define MEMINFO_FILE "/proc/meminfo"
58static int meminfo_fd = -1; 58static int meminfo_fd = -1;
59#define VMINFO_FILE "/proc/vmstat" 59#define VMINFO_FILE "/proc/vmstat"
60//static int vminfo_fd = -1; 60// static int vminfo_fd = -1;
61 61
62// As of 2.6.24 /proc/meminfo seems to need 888 on 64-bit, 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. 63// and would need 1258 if the obsolete fields were there.
@@ -68,20 +68,20 @@ static char buf[2048];
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/***********************************************************************/
@@ -96,14 +96,17 @@ static char buf[2048];
96 * GNU Library General Public License for more details. 96 * GNU Library General Public License for more details.
97 */ 97 */
98 98
99typedef struct mem_table_struct { 99typedef struct mem_table_struct
100{
100 const char *name; /* memory type name */ 101 const char *name; /* memory type name */
101 unsigned long *slot; /* slot in return struct */ 102 unsigned long *slot; /* slot in return struct */
102} mem_table_struct; 103} mem_table_struct;
103 104
104static int compare_mem_table_structs(const void *a, const void *b) 105static int compare_mem_table_structs (const void *a, const void *b)
105{ 106{
106 return strcmp(((const mem_table_struct*)a)->name, ((const mem_table_struct*)b)->name); 107 return strcmp (((const mem_table_struct*) a)->name, ((const
108 mem_table_struct*) b)->
109 name);
107} 110}
108 111
109/* example data, following junk, with comments added: 112/* example data, following junk, with comments added:
@@ -183,7 +186,7 @@ static unsigned long kb_nfs_unstable;
183static unsigned long kb_swap_reclaimable; 186static unsigned long kb_swap_reclaimable;
184static unsigned long kb_swap_unreclaimable; 187static unsigned long kb_swap_unreclaimable;
185 188
186void meminfo(void) 189void meminfo (void)
187{ 190{
188 char namebuf[16]; /* big enough to hold any row name */ 191 char namebuf[16]; /* big enough to hold any row name */
189 mem_table_struct findme = { namebuf, NULL }; 192 mem_table_struct findme = { namebuf, NULL };
@@ -228,45 +231,45 @@ void meminfo(void)
228 }; 231 };
229 const int mem_table_count = sizeof(mem_table) / sizeof(mem_table_struct); 232 const int mem_table_count = sizeof(mem_table) / sizeof(mem_table_struct);
230 233
231 FILE_TO_BUF(MEMINFO_FILE, meminfo_fd); 234 FILE_TO_BUF (MEMINFO_FILE, meminfo_fd);
232 235
233 kb_inactive = ~0UL; 236 kb_inactive = ~0UL;
234 237
235 head = buf; 238 head = buf;
236 for (;;) 239 for (;;)
240 {
241 tail = strchr (head, ':');
242 if (! tail)
243 break;
244 *tail = '\0';
245 if (strlen (head) >= sizeof(namebuf))
237 { 246 {
238 tail = strchr(head, ':');
239 if (!tail)
240 break;
241 *tail = '\0';
242 if (strlen(head) >= sizeof(namebuf))
243 {
244 head = tail + 1;
245 goto nextline;
246 }
247 strcpy(namebuf, head);
248 found = bsearch(&findme, mem_table, mem_table_count,
249 sizeof(mem_table_struct), compare_mem_table_structs
250 );
251 head = tail + 1;
252 if (!found)
253 goto nextline;
254 *(found->slot) = (unsigned long)strtoull(head, &tail, 10);
255nextline:
256 tail = strchr(head, '\n');
257 if (!tail)
258 break;
259 head = tail + 1; 247 head = tail + 1;
248 goto nextline;
260 } 249 }
261 if (!kb_low_total) /* low==main except with large-memory support */ 250 strcpy (namebuf, head);
262 { 251 found = bsearch (&findme, mem_table, mem_table_count,
263 kb_low_total = kb_main_total; 252 sizeof(mem_table_struct), compare_mem_table_structs
264 kb_low_free = kb_main_free; 253 );
265 } 254 head = tail + 1;
255 if (! found)
256 goto nextline;
257 *(found->slot) = (unsigned long) strtoull (head, &tail, 10);
258nextline:
259 tail = strchr (head, '\n');
260 if (! tail)
261 break;
262 head = tail + 1;
263 }
264 if (! kb_low_total) /* low==main except with large-memory support */
265 {
266 kb_low_total = kb_main_total;
267 kb_low_free = kb_main_free;
268 }
266 if (kb_inactive == ~0UL) 269 if (kb_inactive == ~0UL)
267 { 270 {
268 kb_inactive = kb_inact_dirty + kb_inact_clean + kb_inact_laundry; 271 kb_inactive = kb_inact_dirty + kb_inact_clean + kb_inact_laundry;
269 } 272 }
270 kb_swap_used = kb_swap_total - kb_swap_free; 273 kb_swap_used = kb_swap_total - kb_swap_free;
271 kb_main_used = kb_main_total - kb_main_free; 274 kb_main_used = kb_main_total - kb_main_free;
272} 275}