1 // Copyright (C) 1992-1998 by Michael K. Johnson, johnsonm@redhat.com
2 // Copyright 1998-2002 Albert Cahalan
3 //
4 // This file is placed under the conditions of the GNU Library
5 // General Public License, version 2, or any later version.
6 // See file COPYING for information on distribution conditions.
7
8 /* File for parsing top-level /proc entities. */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <locale.h>
15
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include "version.h"
19 #include "sysinfo.h" /* include self to verify prototypes */
20
21 #ifndef HZ
22 #include <netinet/in.h> /* htons */
23 #endif
24
25 long smp_num_cpus; /* number of CPUs */
26
27 #define BAD_OPEN_MESSAGE \
28 "Error: /proc must be mounted\n" \
29 " To mount /proc at boot you need an /etc/fstab line like:\n" \
30 " /proc /proc proc defaults\n" \
31 " In the meantime, mount /proc /proc -t proc\n"
32
33 #define STAT_FILE "/proc/stat"
34 static int stat_fd = -1;
35 #define UPTIME_FILE "/proc/uptime"
36 static int uptime_fd = -1;
37 #define LOADAVG_FILE "/proc/loadavg"
38 static int loadavg_fd = -1;
39 #define MEMINFO_FILE "/proc/meminfo"
40 static int meminfo_fd = -1;
41 #define VMINFO_FILE "/proc/vmstat"
42 static int vminfo_fd = -1;
43
44 static char buf[1024];
45
46 /* This macro opens filename only if necessary and seeks to 0 so
47 * that successive calls to the functions are more efficient.
48 * It also reads the current contents of the file into the global buf.
49 */
50 #define FILE_TO_BUF(filename, fd) do{ \
51 static int local_n; \
52 if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \
53 fprintf(stderr, BAD_OPEN_MESSAGE); \
54 fflush(NULL); \
55 _exit(102); \
56 } \
57 lseek(fd, 0L, SEEK_SET); \
58 if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \
59 perror(filename); \
60 fflush(NULL); \
61 _exit(103); \
62 } \
63 buf[local_n] = '\0'; \
64 }while (0)
65
66 /* evals 'x' twice */
67 #define SET_IF_DESIRED(x,y) do{ if (x) *(x) = (y); }while (0)
68
69 /***********************************************************************/
uptime(double * restrict uptime_secs,double * restrict idle_secs)70 int uptime(double *restrict uptime_secs, double *restrict idle_secs)
71 {
72 double up = 0, idle = 0;
73 char *restrict savelocale;
74
75 FILE_TO_BUF(UPTIME_FILE, uptime_fd);
76 savelocale = setlocale(LC_NUMERIC, NULL);
77 setlocale(LC_NUMERIC, "C");
78 if (sscanf(buf, "%lf %lf", &up, &idle) < 2) {
79 setlocale(LC_NUMERIC, savelocale);
80 fprintf(stderr, "bad data in " UPTIME_FILE "\n");
81 return 0;
82 }
83 setlocale(LC_NUMERIC, savelocale);
84 SET_IF_DESIRED(uptime_secs, up);
85 SET_IF_DESIRED(idle_secs, idle);
86 return up; /* assume never be zero seconds in practice */
87 }
88
89 /***********************************************************************
90 * Some values in /proc are expressed in units of 1/HZ seconds, where HZ
91 * is the kernel clock tick rate. One of these units is called a jiffy.
92 * The HZ value used in the kernel may vary according to hacker desire.
93 * According to Linus Torvalds, this is not true. He considers the values
94 * in /proc as being in architecture-dependant units that have no relation
95 * to the kernel clock tick rate. Examination of the kernel source code
96 * reveals that opinion as wishful thinking.
97 *
98 * In any case, we need the HZ constant as used in /proc. (the real HZ value
99 * may differ, but we don't care) There are several ways we could get HZ:
100 *
101 * 1. Include the kernel header file. If it changes, recompile this library.
102 * 2. Use the sysconf() function. When HZ changes, recompile the C library!
103 * 3. Ask the kernel. This is obviously correct...
104 *
105 * Linus Torvalds won't let us ask the kernel, because he thinks we should
106 * not know the HZ value. Oh well, we don't have to listen to him.
107 * Someone smuggled out the HZ value. :-)
108 *
109 * This code should work fine, even if Linus fixes the kernel to match his
110 * stated behavior. The code only fails in case of a partial conversion.
111 *
112 * Recent update: on some architectures, the 2.4 kernel provides an
113 * ELF note to indicate HZ. This may be for ARM or user-mode Linux
114 * support. This ought to be investigated. Note that sysconf() is still
115 * unreliable, because it doesn't return an error code when it is
116 * used with a kernel that doesn't support the ELF note. On some other
117 * architectures there may be a system call or sysctl() that will work.
118 */
119
120 unsigned long long Hertz;
121
old_Hertz_hack(void)122 static void old_Hertz_hack(void)
123 {
124 unsigned long long user_j, nice_j, sys_j, other_j; /* jiffies (clock ticks) */
125 double up_1, up_2, seconds;
126 unsigned long long jiffies;
127 unsigned h;
128 char *restrict savelocale;
129
130 savelocale = setlocale(LC_NUMERIC, NULL);
131 setlocale(LC_NUMERIC, "C");
132 do {
133 FILE_TO_BUF(UPTIME_FILE, uptime_fd);
134 sscanf(buf, "%lf", &up_1);
135 /* uptime(&up_1, NULL); */
136 FILE_TO_BUF(STAT_FILE, stat_fd);
137 sscanf(buf, "cpu %Lu %Lu %Lu %Lu", &user_j, &nice_j, &sys_j,
138 &other_j);
139 FILE_TO_BUF(UPTIME_FILE, uptime_fd);
140 sscanf(buf, "%lf", &up_2);
141 /* uptime(&up_2, NULL); */
142 } while ((long long)((up_2 - up_1) * 1000.0 / up_1)); /* want under 0.1% error */
143 setlocale(LC_NUMERIC, savelocale);
144 jiffies = user_j + nice_j + sys_j + other_j;
145 seconds = (up_1 + up_2) / 2;
146 h = (unsigned)((double)jiffies / seconds / smp_num_cpus);
147 /* actual values used by 2.4 kernels: 32 64 100 128 1000 1024 1200 */
148 switch (h) {
149 case 9 ... 11:
150 Hertz = 10;
151 break; /* S/390 (sometimes) */
152 case 18 ... 22:
153 Hertz = 20;
154 break; /* user-mode Linux */
155 case 30 ... 34:
156 Hertz = 32;
157 break; /* ia64 emulator */
158 case 48 ... 52:
159 Hertz = 50;
160 break;
161 case 58 ... 61:
162 Hertz = 60;
163 break;
164 case 62 ... 65:
165 Hertz = 64;
166 break; /* StrongARM /Shark */
167 case 95 ... 105:
168 Hertz = 100;
169 break; /* normal Linux */
170 case 124 ... 132:
171 Hertz = 128;
172 break; /* MIPS, ARM */
173 case 195 ... 204:
174 Hertz = 200;
175 break; /* normal << 1 */
176 case 253 ... 260:
177 Hertz = 256;
178 break;
179 case 393 ... 408:
180 Hertz = 400;
181 break; /* normal << 2 */
182 case 790 ... 808:
183 Hertz = 800;
184 break; /* normal << 3 */
185 case 990 ... 1010:
186 Hertz = 1000;
187 break; /* ARM */
188 case 1015 ... 1035:
189 Hertz = 1024;
190 break; /* Alpha, ia64 */
191 case 1180 ... 1220:
192 Hertz = 1200;
193 break; /* Alpha */
194 default:
195 #ifdef HZ
196 Hertz = (unsigned long long)HZ; /* <asm/param.h> */
197 #else
198 /* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
199 Hertz = (sizeof(long) == sizeof(int)
200 || htons(999) == 999) ? 100UL : 1024UL;
201 #endif
202 fprintf(stderr, "Unknown HZ value! (%d) Assume %Ld.\n", h,
203 Hertz);
204 }
205 }
206
207 #ifndef AT_CLKTCK
208 #define AT_CLKTCK 17 /* frequency of times() */
209 #endif
210
211 extern char **environ;
212
213 /* for ELF executables, notes are pushed before environment and args */
find_elf_note(unsigned long findme)214 static unsigned long find_elf_note(unsigned long findme)
215 {
216 unsigned long *ep = (unsigned long *)environ;
217 while (*ep++) ;
218 while (*ep) {
219 if (ep[0] == findme)
220 return ep[1];
221 ep += 2;
222 }
223 return 42;
224 }
225
226 static void init_libproc(void) __attribute__ ((constructor));
init_libproc(void)227 static void init_libproc(void)
228 {
229 /* ought to count CPUs in /proc/stat instead of relying
230 * on glibc, which foolishly tries to parse /proc/cpuinfo
231 */
232 smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF); // or _SC_NPROCESSORS_ONLN
233 if (smp_num_cpus < 1)
234 smp_num_cpus = 1; /* SPARC glibc is buggy */
235
236 if (linux_version_code > LINUX_VERSION(2, 4, 0)) {
237 Hertz = find_elf_note(AT_CLKTCK);
238 if (Hertz != 42)
239 return;
240 fprintf(stderr,
241 "2.4 kernel w/o ELF notes? -- report to albert@users.sf.net\n");
242 }
243 old_Hertz_hack();
244 }
245
246 /***********************************************************************
247 * The /proc filesystem calculates idle=jiffies-(user+nice+sys) and we
248 * recover jiffies by adding up the 4 or 5 numbers we are given. SMP kernels
249 * (as of pre-2.4 era) can report idle time going backwards, perhaps due
250 * to non-atomic reads and updates. There is no locking for these values.
251 */
252 #ifndef NAN
253 #define NAN (-0.0)
254 #endif
255 #define JT unsigned long long
five_cpu_numbers(double * restrict uret,double * restrict nret,double * restrict sret,double * restrict iret,double * restrict wret)256 void five_cpu_numbers(double *restrict uret, double *restrict nret,
257 double *restrict sret, double *restrict iret,
258 double *restrict wret)
259 {
260 double tmp_u, tmp_n, tmp_s, tmp_i, tmp_w;
261 double scale; /* scale values to % */
262 static JT old_u, old_n, old_s, old_i, old_w;
263 JT new_u, new_n, new_s, new_i, new_w;
264 JT ticks_past; /* avoid div-by-0 by not calling too often :-( */
265
266 tmp_w = 0.0;
267 new_w = 0;
268
269 FILE_TO_BUF(STAT_FILE, stat_fd);
270 sscanf(buf, "cpu %Lu %Lu %Lu %Lu %Lu", &new_u, &new_n, &new_s, &new_i,
271 &new_w);
272 ticks_past =
273 (new_u + new_n + new_s + new_i + new_w) - (old_u + old_n + old_s +
274 old_i + old_w);
275 if (ticks_past) {
276 scale = 100.0 / (double)ticks_past;
277 tmp_u = ((double)new_u - (double)old_u) * scale;
278 tmp_n = ((double)new_n - (double)old_n) * scale;
279 tmp_s = ((double)new_s - (double)old_s) * scale;
280 tmp_i = ((double)new_i - (double)old_i) * scale;
281 tmp_w = ((double)new_w - (double)old_w) * scale;
282 } else {
283 tmp_u = NAN;
284 tmp_n = NAN;
285 tmp_s = NAN;
286 tmp_i = NAN;
287 tmp_w = NAN;
288 }
289 SET_IF_DESIRED(uret, tmp_u);
290 SET_IF_DESIRED(nret, tmp_n);
291 SET_IF_DESIRED(sret, tmp_s);
292 SET_IF_DESIRED(iret, tmp_i);
293 SET_IF_DESIRED(wret, tmp_w);
294 old_u = new_u;
295 old_n = new_n;
296 old_s = new_s;
297 old_i = new_i;
298 old_w = new_w;
299 }
300
301 #undef JT
302
303 /***********************************************************************/
loadavg(double * restrict av1,double * restrict av5,double * restrict av15)304 void loadavg(double *restrict av1, double *restrict av5, double *restrict av15)
305 {
306 double avg_1 = 0, avg_5 = 0, avg_15 = 0;
307 char *restrict savelocale;
308
309 FILE_TO_BUF(LOADAVG_FILE, loadavg_fd);
310 savelocale = setlocale(LC_NUMERIC, NULL);
311 setlocale(LC_NUMERIC, "C");
312 if (sscanf(buf, "%lf %lf %lf", &avg_1, &avg_5, &avg_15) < 3) {
313 fprintf(stderr, "bad data in " LOADAVG_FILE "\n");
314 exit(1);
315 }
316 setlocale(LC_NUMERIC, savelocale);
317 SET_IF_DESIRED(av1, avg_1);
318 SET_IF_DESIRED(av5, avg_5);
319 SET_IF_DESIRED(av15, avg_15);
320 }
321
322 /***********************************************************************/
323 /*
324 * Copyright 1999 by Albert Cahalan; all rights reserved.
325 * This file may be used subject to the terms and conditions of the
326 * GNU Library General Public License Version 2, or any later version
327 * at your option, as published by the Free Software Foundation.
328 * This program is distributed in the hope that it will be useful,
329 * but WITHOUT ANY WARRANTY; without even the implied warranty of
330 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
331 * GNU Library General Public License for more details.
332 */
333
334 typedef struct mem_table_struct {
335 const char *name; /* memory type name */
336 unsigned *slot; /* slot in return struct */
337 } mem_table_struct;
338
compare_mem_table_structs(const void * a,const void * b)339 static int compare_mem_table_structs(const void *a, const void *b)
340 {
341 return strcmp(((const mem_table_struct *)a)->name,
342 ((const mem_table_struct *)b)->name);
343 }
344
345 /* example data, following junk, with comments added:
346 *
347 * MemTotal: 61768 kB old
348 * MemFree: 1436 kB old
349 * MemShared: 0 kB old (now always zero; not calculated)
350 * Buffers: 1312 kB old
351 * Cached: 20932 kB old
352 * Active: 12464 kB new
353 * Inact_dirty: 7772 kB new
354 * Inact_clean: 2008 kB new
355 * Inact_target: 0 kB new
356 * Inact_laundry: 0 kB new, and might be missing too
357 * HighTotal: 0 kB
358 * HighFree: 0 kB
359 * LowTotal: 61768 kB
360 * LowFree: 1436 kB
361 * SwapTotal: 122580 kB old
362 * SwapFree: 60352 kB old
363 * Inactive: 20420 kB 2.5.41+
364 * Dirty: 0 kB 2.5.41+
365 * Writeback: 0 kB 2.5.41+
366 * Mapped: 9792 kB 2.5.41+
367 * Slab: 4564 kB 2.5.41+
368 * Committed_AS: 8440 kB 2.5.41+
369 * PageTables: 304 kB 2.5.41+
370 * ReverseMaps: 5738 2.5.41+
371 */
372
373 /* obsolete */
374 unsigned kb_main_shared;
375 /* old but still kicking -- the important stuff */
376 unsigned kb_main_buffers;
377 unsigned kb_main_cached;
378 unsigned kb_main_free;
379 unsigned kb_main_total;
380 unsigned kb_swap_free;
381 unsigned kb_swap_total;
382 /* recently introduced */
383 unsigned kb_high_free;
384 unsigned kb_high_total;
385 unsigned kb_low_free;
386 unsigned kb_low_total;
387 /* 2.4.xx era */
388 unsigned kb_active;
389 unsigned kb_inact_laundry;
390 unsigned kb_inact_dirty;
391 unsigned kb_inact_clean;
392 unsigned kb_inact_target;
393 unsigned kb_swap_cached; /* late 2.4 only */
394 /* derived values */
395 unsigned kb_swap_used;
396 unsigned kb_main_used;
397 /* 2.5.41+ */
398 unsigned kb_writeback;
399 unsigned kb_slab;
400 unsigned nr_reversemaps;
401 unsigned kb_committed_as;
402 unsigned kb_dirty;
403 unsigned kb_inactive;
404 unsigned kb_mapped;
405 unsigned kb_pagetables;
406
meminfo(void)407 void meminfo(void)
408 {
409 char namebuf[16]; /* big enough to hold any row name */
410 mem_table_struct findme = { namebuf, NULL };
411 mem_table_struct *found;
412 char *head;
413 char *tail;
414 static const mem_table_struct mem_table[] = {
415 {"Active", &kb_active},
416 {"Buffers", &kb_main_buffers},
417 {"Cached", &kb_main_cached},
418 {"Committed_AS", &kb_committed_as},
419 {"Dirty", &kb_dirty},
420 {"HighFree", &kb_high_free},
421 {"HighTotal", &kb_high_total},
422 {"Inact_clean", &kb_inact_clean},
423 {"Inact_dirty", &kb_inact_dirty},
424 {"Inact_laundry", &kb_inact_laundry},
425 {"Inact_target", &kb_inact_target},
426 {"Inactive", &kb_inactive},
427 {"LowFree", &kb_low_free},
428 {"LowTotal", &kb_low_total},
429 {"Mapped", &kb_mapped},
430 {"MemFree", &kb_main_free},
431 {"MemShared", &kb_main_shared},
432 {"MemTotal", &kb_main_total},
433 {"PageTables", &kb_pagetables},
434 {"ReverseMaps", &nr_reversemaps},
435 {"Slab", &kb_slab},
436 {"SwapCached", &kb_swap_cached},
437 {"SwapFree", &kb_swap_free},
438 {"SwapTotal", &kb_swap_total},
439 {"Writeback", &kb_writeback}
440 };
441 const int mem_table_count =
442 sizeof(mem_table) / sizeof(mem_table_struct);
443
444 FILE_TO_BUF(MEMINFO_FILE, meminfo_fd);
445
446 kb_inactive = ~0U;
447
448 head = buf;
449 for (;;) {
450 tail = strchr(head, ':');
451 if (!tail)
452 break;
453 *tail = '\0';
454 if (strlen(head) >= sizeof(namebuf)) {
455 head = tail + 1;
456 goto nextline;
457 }
458 strcpy(namebuf, head);
459 found = bsearch(&findme, mem_table, mem_table_count,
460 sizeof(mem_table_struct),
461 compare_mem_table_structs);
462 head = tail + 1;
463 if (!found)
464 goto nextline;
465 *(found->slot) = strtoul(head, &tail, 10);
466 nextline:
467 tail = strchr(head, '\n');
468 if (!tail)
469 break;
470 head = tail + 1;
471 }
472 if (!kb_low_total) { /* low==main except with large-memory support */
473 kb_low_total = kb_main_total;
474 kb_low_free = kb_main_free;
475 }
476 if (kb_inactive == ~0U) {
477 kb_inactive =
478 kb_inact_dirty + kb_inact_clean + kb_inact_laundry;
479 }
480 kb_swap_used = kb_swap_total - kb_swap_free;
481 kb_main_used = kb_main_total - kb_main_free;
482 }
483
484 /*****************************************************************/
485
486 /* read /proc/vminfo only for 2.5.41 and above */
487
488 typedef struct vm_table_struct {
489 const char *name; /* VM statistic name */
490 unsigned *slot; /* slot in return struct */
491 } vm_table_struct;
492
compare_vm_table_structs(const void * a,const void * b)493 static int compare_vm_table_structs(const void *a, const void *b)
494 {
495 return strcmp(((const vm_table_struct *)a)->name,
496 ((const vm_table_struct *)b)->name);
497 }
498
499 unsigned vm_nr_dirty; // dirty writable pages
500 unsigned vm_nr_writeback; // pages under writeback
501 unsigned vm_nr_pagecache; // pages in pagecache
502 unsigned vm_nr_page_table_pages; // pages used for pagetables
503 unsigned vm_nr_reverse_maps; // includes PageDirect
504 unsigned vm_nr_mapped; // mapped into pagetables
505 unsigned vm_nr_slab; // in slab
506 unsigned vm_pgpgin; // disk reads (same as 1st num on /proc/stat page line)
507 unsigned vm_pgpgout; // disk writes (same as 2nd num on /proc/stat page line)
508 unsigned vm_pswpin; // swap reads (same as 1st num on /proc/stat swap line)
509 unsigned vm_pswpout; // swap writes (same as 2nd num on /proc/stat swap line)
510 unsigned vm_pgalloc; // page allocations
511 unsigned vm_pgfree; // page freeings
512 unsigned vm_pgactivate; // pages moved inactive -> active
513 unsigned vm_pgdeactivate; // pages moved active -> inactive
514 unsigned vm_pgfault; // total faults (major+minor)
515 unsigned vm_pgmajfault; // major faults
516 unsigned vm_pgscan; // pages scanned by page reclaim
517 unsigned vm_pgrefill; // inspected by refill_inactive_zone
518 unsigned vm_pgsteal; // total pages reclaimed
519 unsigned vm_kswapd_steal; // pages reclaimed by kswapd
520 // next 3 as defined by the 2.5.52 kernel
521 unsigned vm_pageoutrun; // times kswapd ran page reclaim
522 unsigned vm_allocstall; // times a page allocator ran direct reclaim
523 unsigned vm_pgrotated; // pages rotated to the tail of the LRU for immediate reclaim
524
vminfo(void)525 void vminfo(void)
526 {
527 char namebuf[16]; /* big enough to hold any row name */
528 vm_table_struct findme = { namebuf, NULL };
529 vm_table_struct *found;
530 char *head;
531 char *tail;
532 static const vm_table_struct vm_table[] = {
533 {"allocstall", &vm_allocstall},
534 {"kswapd_steal", &vm_kswapd_steal},
535 {"nr_dirty", &vm_nr_dirty},
536 {"nr_mapped", &vm_nr_mapped},
537 {"nr_page_table_pages", &vm_nr_page_table_pages},
538 {"nr_pagecache", &vm_nr_pagecache},
539 {"nr_reverse_maps", &vm_nr_reverse_maps},
540 {"nr_slab", &vm_nr_slab},
541 {"nr_writeback", &vm_nr_writeback},
542 {"pageoutrun", &vm_pageoutrun},
543 {"pgactivate", &vm_pgactivate},
544 {"pgalloc", &vm_pgalloc},
545 {"pgdeactivate", &vm_pgdeactivate},
546 {"pgfault", &vm_pgfault},
547 {"pgfree", &vm_pgfree},
548 {"pgmajfault", &vm_pgmajfault},
549 {"pgpgin", &vm_pgpgin},
550 {"pgpgout", &vm_pgpgout},
551 {"pgrefill", &vm_pgrefill},
552 {"pgrotated", &vm_pgrotated},
553 {"pgscan", &vm_pgscan},
554 {"pgsteal", &vm_pgsteal},
555 {"pswpin", &vm_pswpin},
556 {"pswpout", &vm_pswpout}
557 };
558 const int vm_table_count = sizeof(vm_table) / sizeof(vm_table_struct);
559
560 FILE_TO_BUF(VMINFO_FILE, vminfo_fd);
561
562 head = buf;
563 for (;;) {
564 tail = strchr(head, ' ');
565 if (!tail)
566 break;
567 *tail = '\0';
568 if (strlen(head) >= sizeof(namebuf)) {
569 head = tail + 1;
570 goto nextline;
571 }
572 strcpy(namebuf, head);
573 found = bsearch(&findme, vm_table, vm_table_count,
574 sizeof(vm_table_struct),
575 compare_vm_table_structs);
576 head = tail + 1;
577 if (!found)
578 goto nextline;
579 *(found->slot) = strtoul(head, &tail, 10);
580 nextline:
581
582 //if (found) fprintf(stderr,"%s=%d\n",found->name,*(found->slot));
583 //else fprintf(stderr,"%s not found\n",findme.name);
584
585 tail = strchr(head, '\n');
586 if (!tail)
587 break;
588 head = tail + 1;
589 }
590 }
591
592 /*****************************************************************/
593