1 // Copyright (C) 1992-1998 by Michael K. Johnson, johnsonm@redhat.com
2 // Copyright 1998-2003 Albert Cahalan
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.
8 // File for parsing top-level /proc entities. */
10 // June 2003, Fabian Frederick, disk and slab info
21 #include "sysinfo.h" /* include self to verify prototypes */
24 #include <netinet/in.h> /* htons */
27 long smp_num_cpus; /* number of CPUs */
29 #define BAD_OPEN_MESSAGE \
30 "Error: /proc must be mounted\n" \
31 " To mount /proc at boot you need an /etc/fstab line like:\n" \
32 " /proc /proc proc defaults\n" \
33 " In the meantime, run \"mount /proc /proc -t proc\"\n"
35 #define STAT_FILE "/proc/stat"
36 static int stat_fd = -1;
37 #define UPTIME_FILE "/proc/uptime"
38 static int uptime_fd = -1;
39 #define LOADAVG_FILE "/proc/loadavg"
40 static int loadavg_fd = -1;
41 #define MEMINFO_FILE "/proc/meminfo"
42 static int meminfo_fd = -1;
43 #define VMINFO_FILE "/proc/vmstat"
44 static int vminfo_fd = -1;
46 // As of 2.6.24 /proc/meminfo seems to need 888 on 64-bit,
47 // and would need 1258 if the obsolete fields were there.
48 static char buf[2048];
50 /* This macro opens filename only if necessary and seeks to 0 so
51 * that successive calls to the functions are more efficient.
52 * It also reads the current contents of the file into the global buf.
54 #define FILE_TO_BUF(filename, fd) do{ \
56 if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \
57 fputs(BAD_OPEN_MESSAGE, stderr); \
61 lseek(fd, 0L, SEEK_SET); \
62 if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \
67 buf[local_n] = '\0'; \
71 #define SET_IF_DESIRED(x,y) do{ if(x) *(x) = (y); }while(0)
74 /***********************************************************************/
75 int uptime(double *restrict uptime_secs, double *restrict idle_secs) {
77 char *restrict savelocale;
79 FILE_TO_BUF(UPTIME_FILE,uptime_fd);
80 savelocale = setlocale(LC_NUMERIC, NULL);
81 setlocale(LC_NUMERIC,"C");
82 if (sscanf(buf, "%lf %lf", &up, &idle) < 2) {
83 setlocale(LC_NUMERIC,savelocale);
84 fputs("bad data in " UPTIME_FILE "\n", stderr);
87 setlocale(LC_NUMERIC,savelocale);
88 SET_IF_DESIRED(uptime_secs, up);
89 SET_IF_DESIRED(idle_secs, idle);
90 return up; /* assume never be zero seconds in practice */
93 /***********************************************************************
94 * Some values in /proc are expressed in units of 1/HZ seconds, where HZ
95 * is the kernel clock tick rate. One of these units is called a jiffy.
96 * The HZ value used in the kernel may vary according to hacker desire.
97 * According to Linus Torvalds, this is not true. He considers the values
98 * in /proc as being in architecture-dependant units that have no relation
99 * to the kernel clock tick rate. Examination of the kernel source code
100 * reveals that opinion as wishful thinking.
102 * In any case, we need the HZ constant as used in /proc. (the real HZ value
103 * may differ, but we don't care) There are several ways we could get HZ:
105 * 1. Include the kernel header file. If it changes, recompile this library.
106 * 2. Use the sysconf() function. When HZ changes, recompile the C library!
107 * 3. Ask the kernel. This is obviously correct...
109 * Linus Torvalds won't let us ask the kernel, because he thinks we should
110 * not know the HZ value. Oh well, we don't have to listen to him.
111 * Someone smuggled out the HZ value. :-)
113 * This code should work fine, even if Linus fixes the kernel to match his
114 * stated behavior. The code only fails in case of a partial conversion.
116 * Recent update: on some architectures, the 2.4 kernel provides an
117 * ELF note to indicate HZ. This may be for ARM or user-mode Linux
118 * support. This ought to be investigated. Note that sysconf() is still
119 * unreliable, because it doesn't return an error code when it is
120 * used with a kernel that doesn't support the ELF note. On some other
121 * architectures there may be a system call or sysctl() that will work.
124 unsigned long long Hertz;
126 static void old_Hertz_hack(void){
127 unsigned long long user_j, nice_j, sys_j, other_j; /* jiffies (clock ticks) */
128 double up_1, up_2, seconds;
129 unsigned long long jiffies;
131 char *restrict savelocale;
133 savelocale = setlocale(LC_NUMERIC, NULL);
134 setlocale(LC_NUMERIC, "C");
136 FILE_TO_BUF(UPTIME_FILE,uptime_fd); sscanf(buf, "%lf", &up_1);
137 /* uptime(&up_1, NULL); */
138 FILE_TO_BUF(STAT_FILE,stat_fd);
139 sscanf(buf, "cpu %Lu %Lu %Lu %Lu", &user_j, &nice_j, &sys_j, &other_j);
140 FILE_TO_BUF(UPTIME_FILE,uptime_fd); 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 */
149 case 9 ... 11 : Hertz = 10; break; /* S/390 (sometimes) */
150 case 18 ... 22 : Hertz = 20; break; /* user-mode Linux */
151 case 30 ... 34 : Hertz = 32; break; /* ia64 emulator */
152 case 48 ... 52 : Hertz = 50; break;
153 case 58 ... 61 : Hertz = 60; break;
154 case 62 ... 65 : Hertz = 64; break; /* StrongARM /Shark */
155 case 95 ... 105 : Hertz = 100; break; /* normal Linux */
156 case 124 ... 132 : Hertz = 128; break; /* MIPS, ARM */
157 case 195 ... 204 : Hertz = 200; break; /* normal << 1 */
158 case 247 ... 252 : Hertz = 250; break;
159 case 253 ... 260 : Hertz = 256; break;
160 case 393 ... 408 : Hertz = 400; break; /* normal << 2 */
161 case 790 ... 808 : Hertz = 800; break; /* normal << 3 */
162 case 990 ... 1010 : Hertz = 1000; break; /* ARM */
163 case 1015 ... 1035 : Hertz = 1024; break; /* Alpha, ia64 */
164 case 1180 ... 1220 : Hertz = 1200; break; /* Alpha */
167 Hertz = (unsigned long long)HZ; /* <asm/param.h> */
169 /* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
170 Hertz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL;
172 fprintf(stderr, "Unknown HZ value! (%d) Assume %Ld.\n", h, Hertz);
176 // same as: euid != uid || egid != gid
178 #define AT_SECURE 23 // secure mode boolean (true if setuid, etc.)
182 #define AT_CLKTCK 17 // frequency of times()
185 #define NOTE_NOT_FOUND 42
187 //extern char** environ;
189 /* for ELF executables, notes are pushed before environment and args */
190 static unsigned long find_elf_note(unsigned long findme){
191 unsigned long *ep = (unsigned long *)environ;
194 if(ep[0]==findme) return ep[1];
197 return NOTE_NOT_FOUND;
202 static int check_for_privs(void){
203 unsigned long rc = find_elf_note(AT_SECURE);
204 if(rc==NOTE_NOT_FOUND){
205 // not valid to run this code after UID or GID change!
206 // (if needed, may use AT_UID and friends instead)
207 rc = geteuid() != getuid() || getegid() != getgid();
212 static void init_libproc(void) __attribute__((constructor));
213 static void init_libproc(void){
214 have_privs = check_for_privs();
215 // ought to count CPUs in /proc/stat instead of relying
216 // on glibc, which foolishly tries to parse /proc/cpuinfo
218 // SourceForge has an old Alpha running Linux 2.2.20 that
219 // appears to have a non-SMP kernel on a 2-way SMP box.
220 // _SC_NPROCESSORS_CONF returns 2, resulting in HZ=512
221 // _SC_NPROCESSORS_ONLN returns 1, which should work OK
222 smp_num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
223 if(smp_num_cpus<1) smp_num_cpus=1; /* SPARC glibc is buggy */
225 if(linux_version_code > LINUX_VERSION(2, 4, 0)){
226 Hertz = find_elf_note(AT_CLKTCK);
227 if(Hertz!=NOTE_NOT_FOUND) return;
228 fputs("2.4+ kernel w/o ELF notes? -- report this\n", stderr);
234 /***********************************************************************
235 * The /proc filesystem calculates idle=jiffies-(user+nice+sys) and we
236 * recover jiffies by adding up the 4 or 5 numbers we are given. SMP kernels
237 * (as of pre-2.4 era) can report idle time going backwards, perhaps due
238 * to non-atomic reads and updates. There is no locking for these values.
243 #define JT unsigned long long
244 void eight_cpu_numbers(double *restrict uret, double *restrict nret, double *restrict sret, double *restrict iret, double *restrict wret, double *restrict xret, double *restrict yret, double *restrict zret){
245 double tmp_u, tmp_n, tmp_s, tmp_i, tmp_w, tmp_x, tmp_y, tmp_z;
246 double scale; /* scale values to % */
247 static JT old_u, old_n, old_s, old_i, old_w, old_x, old_y, old_z;
248 JT new_u, new_n, new_s, new_i, new_w, new_x, new_y, new_z;
249 JT ticks_past; /* avoid div-by-0 by not calling too often :-( */
260 FILE_TO_BUF(STAT_FILE,stat_fd);
261 sscanf(buf, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu", &new_u, &new_n, &new_s, &new_i, &new_w, &new_x, &new_y, &new_z);
262 ticks_past = (new_u+new_n+new_s+new_i+new_w+new_x+new_y+new_z)-(old_u+old_n+old_s+old_i+old_w+old_x+old_y+old_z);
264 scale = 100.0 / (double)ticks_past;
265 tmp_u = ( (double)new_u - (double)old_u ) * scale;
266 tmp_n = ( (double)new_n - (double)old_n ) * scale;
267 tmp_s = ( (double)new_s - (double)old_s ) * scale;
268 tmp_i = ( (double)new_i - (double)old_i ) * scale;
269 tmp_w = ( (double)new_w - (double)old_w ) * scale;
270 tmp_x = ( (double)new_x - (double)old_x ) * scale;
271 tmp_y = ( (double)new_y - (double)old_y ) * scale;
272 tmp_z = ( (double)new_z - (double)old_z ) * scale;
283 SET_IF_DESIRED(uret, tmp_u);
284 SET_IF_DESIRED(nret, tmp_n);
285 SET_IF_DESIRED(sret, tmp_s);
286 SET_IF_DESIRED(iret, tmp_i);
287 SET_IF_DESIRED(wret, tmp_w);
288 SET_IF_DESIRED(xret, tmp_x);
289 SET_IF_DESIRED(yret, tmp_y);
290 SET_IF_DESIRED(zret, tmp_z);
303 /***********************************************************************/
304 void loadavg(double *restrict av1, double *restrict av5, double *restrict av15) {
305 double avg_1=0, avg_5=0, avg_15=0;
306 char *restrict savelocale;
308 FILE_TO_BUF(LOADAVG_FILE,loadavg_fd);
309 savelocale = setlocale(LC_NUMERIC, NULL);
310 setlocale(LC_NUMERIC, "C");
311 if (sscanf(buf, "%lf %lf %lf", &avg_1, &avg_5, &avg_15) < 3) {
312 fputs("bad data in " LOADAVG_FILE "\n", stderr);
315 setlocale(LC_NUMERIC, savelocale);
316 SET_IF_DESIRED(av1, avg_1);
317 SET_IF_DESIRED(av5, avg_5);
318 SET_IF_DESIRED(av15, avg_15);
321 static char buff[BUFFSIZE]; /* used in the procedures */
322 /***********************************************************************/
324 static void crash(const char *filename) {
329 /***********************************************************************/
331 static void getrunners(unsigned int *restrict running, unsigned int *restrict blocked) {
338 if((proc=opendir("/proc"))==NULL) crash("/proc");
340 while(( ent=readdir(proc) )) {
346 if (!isdigit(ent->d_name[0])) continue;
347 sprintf(tbuf, "/proc/%s/stat", ent->d_name);
349 fd = open(tbuf, O_RDONLY, 0);
350 if (fd == -1) continue;
351 memset(tbuf, '\0', sizeof tbuf); // didn't feel like checking read()
352 read(fd, tbuf, sizeof tbuf - 1); // need 32 byte buffer at most
355 cp = strrchr(tbuf, ')');
371 /***********************************************************************/
373 void getstat(jiff *restrict cuse, jiff *restrict cice, jiff *restrict csys, jiff *restrict cide, jiff *restrict ciow, jiff *restrict cxxx, jiff *restrict cyyy, jiff *restrict czzz,
374 unsigned long *restrict pin, unsigned long *restrict pout, unsigned long *restrict s_in, unsigned long *restrict sout,
375 unsigned *restrict intr, unsigned *restrict ctxt,
376 unsigned int *restrict running, unsigned int *restrict blocked,
377 unsigned int *restrict btime, unsigned int *restrict processes) {
379 unsigned long long llbuf = 0;
380 int need_vmstat_file = 0;
381 int need_proc_scan = 0;
383 buff[BUFFSIZE-1] = 0; /* ensure null termination in buffer */
386 lseek(fd, 0L, SEEK_SET);
388 fd = open("/proc/stat", O_RDONLY, 0);
389 if(fd == -1) crash("/proc/stat");
391 read(fd,buff,BUFFSIZE-1);
393 *ciow = 0; /* not separated out until the 2.5.41 kernel */
394 *cxxx = 0; /* not separated out until the 2.6.0-test4 kernel */
395 *cyyy = 0; /* not separated out until the 2.6.0-test4 kernel */
396 *czzz = 0; /* not separated out until the 2.6.11 kernel */
398 b = strstr(buff, "cpu ");
399 if(b) sscanf(b, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu", cuse, cice, csys, cide, ciow, cxxx, cyyy, czzz);
401 b = strstr(buff, "page ");
402 if(b) sscanf(b, "page %lu %lu", pin, pout);
403 else need_vmstat_file = 1;
405 b = strstr(buff, "swap ");
406 if(b) sscanf(b, "swap %lu %lu", s_in, sout);
407 else need_vmstat_file = 1;
409 b = strstr(buff, "intr ");
410 if(b) sscanf(b, "intr %Lu", &llbuf);
413 b = strstr(buff, "ctxt ");
414 if(b) sscanf(b, "ctxt %Lu", &llbuf);
417 b = strstr(buff, "btime ");
418 if(b) sscanf(b, "btime %u", btime);
420 b = strstr(buff, "processes ");
421 if(b) sscanf(b, "processes %u", processes);
423 b = strstr(buff, "procs_running ");
424 if(b) sscanf(b, "procs_running %u", running);
425 else need_proc_scan = 1;
427 b = strstr(buff, "procs_blocked ");
428 if(b) sscanf(b, "procs_blocked %u", blocked);
429 else need_proc_scan = 1;
431 if(need_proc_scan){ /* Linux 2.5.46 (approximately) and below */
432 getrunners(running, blocked);
435 (*running)--; // exclude vmstat itself
437 if(need_vmstat_file){ /* Linux 2.5.40-bk4 and above */
446 /***********************************************************************/
448 * Copyright 1999 by Albert Cahalan; all rights reserved.
449 * This file may be used subject to the terms and conditions of the
450 * GNU Library General Public License Version 2, or any later version
451 * at your option, as published by the Free Software Foundation.
452 * This program is distributed in the hope that it will be useful,
453 * but WITHOUT ANY WARRANTY; without even the implied warranty of
454 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
455 * GNU Library General Public License for more details.
458 typedef struct mem_table_struct {
459 const char *name; /* memory type name */
460 unsigned long *slot; /* slot in return struct */
463 static int compare_mem_table_structs(const void *a, const void *b){
464 return strcmp(((const mem_table_struct*)a)->name,((const mem_table_struct*)b)->name);
467 /* example data, following junk, with comments added:
469 * MemTotal: 61768 kB old
470 * MemFree: 1436 kB old
471 * MemShared: 0 kB old (now always zero; not calculated)
472 * Buffers: 1312 kB old
473 * Cached: 20932 kB old
474 * Active: 12464 kB new
475 * Inact_dirty: 7772 kB new
476 * Inact_clean: 2008 kB new
477 * Inact_target: 0 kB new
478 * Inact_laundry: 0 kB new, and might be missing too
483 * SwapTotal: 122580 kB old
484 * SwapFree: 60352 kB old
485 * Inactive: 20420 kB 2.5.41+
486 * Dirty: 0 kB 2.5.41+
487 * Writeback: 0 kB 2.5.41+
488 * Mapped: 9792 kB 2.5.41+
489 * Slab: 4564 kB 2.5.41+
490 * Committed_AS: 8440 kB 2.5.41+
491 * PageTables: 304 kB 2.5.41+
492 * ReverseMaps: 5738 2.5.41+
493 * SwapCached: 0 kB 2.5.??+
494 * HugePages_Total: 220 2.5.??+
495 * HugePages_Free: 138 2.5.??+
496 * Hugepagesize: 4096 kB 2.5.??+
500 unsigned long kb_main_shared;
501 /* old but still kicking -- the important stuff */
502 unsigned long kb_main_buffers;
503 unsigned long kb_main_cached;
504 unsigned long kb_main_free;
505 unsigned long kb_main_total;
506 unsigned long kb_swap_free;
507 unsigned long kb_swap_total;
508 /* recently introduced */
509 unsigned long kb_high_free;
510 unsigned long kb_high_total;
511 unsigned long kb_low_free;
512 unsigned long kb_low_total;
514 unsigned long kb_active;
515 unsigned long kb_inact_laundry;
516 unsigned long kb_inact_dirty;
517 unsigned long kb_inact_clean;
518 unsigned long kb_inact_target;
519 unsigned long kb_swap_cached; /* late 2.4 and 2.6+ only */
521 unsigned long kb_swap_used;
522 unsigned long kb_main_used;
524 unsigned long kb_writeback;
525 unsigned long kb_slab;
526 unsigned long nr_reversemaps;
527 unsigned long kb_committed_as;
528 unsigned long kb_dirty;
529 unsigned long kb_inactive;
530 unsigned long kb_mapped;
531 unsigned long kb_pagetables;
532 // seen on a 2.6.x kernel:
533 static unsigned long kb_vmalloc_chunk;
534 static unsigned long kb_vmalloc_total;
535 static unsigned long kb_vmalloc_used;
536 // seen on 2.6.24-rc6-git12
537 static unsigned long kb_anon_pages;
538 static unsigned long kb_bounce;
539 static unsigned long kb_commit_limit;
540 static unsigned long kb_nfs_unstable;
541 static unsigned long kb_swap_reclaimable;
542 static unsigned long kb_swap_unreclaimable;
545 char namebuf[16]; /* big enough to hold any row name */
546 mem_table_struct findme = { namebuf, NULL};
547 mem_table_struct *found;
550 static const mem_table_struct mem_table[] = {
551 {"Active", &kb_active}, // important
552 {"AnonPages", &kb_anon_pages},
553 {"Bounce", &kb_bounce},
554 {"Buffers", &kb_main_buffers}, // important
555 {"Cached", &kb_main_cached}, // important
556 {"CommitLimit", &kb_commit_limit},
557 {"Committed_AS", &kb_committed_as},
558 {"Dirty", &kb_dirty}, // kB version of vmstat nr_dirty
559 {"HighFree", &kb_high_free},
560 {"HighTotal", &kb_high_total},
561 {"Inact_clean", &kb_inact_clean},
562 {"Inact_dirty", &kb_inact_dirty},
563 {"Inact_laundry",&kb_inact_laundry},
564 {"Inact_target", &kb_inact_target},
565 {"Inactive", &kb_inactive}, // important
566 {"LowFree", &kb_low_free},
567 {"LowTotal", &kb_low_total},
568 {"Mapped", &kb_mapped}, // kB version of vmstat nr_mapped
569 {"MemFree", &kb_main_free}, // important
570 {"MemShared", &kb_main_shared}, // important, but now gone!
571 {"MemTotal", &kb_main_total}, // important
572 {"NFS_Unstable", &kb_nfs_unstable},
573 {"PageTables", &kb_pagetables}, // kB version of vmstat nr_page_table_pages
574 {"ReverseMaps", &nr_reversemaps}, // same as vmstat nr_page_table_pages
575 {"SReclaimable", &kb_swap_reclaimable}, // "swap reclaimable" (dentry and inode structures)
576 {"SUnreclaim", &kb_swap_unreclaimable},
577 {"Slab", &kb_slab}, // kB version of vmstat nr_slab
578 {"SwapCached", &kb_swap_cached},
579 {"SwapFree", &kb_swap_free}, // important
580 {"SwapTotal", &kb_swap_total}, // important
581 {"VmallocChunk", &kb_vmalloc_chunk},
582 {"VmallocTotal", &kb_vmalloc_total},
583 {"VmallocUsed", &kb_vmalloc_used},
584 {"Writeback", &kb_writeback}, // kB version of vmstat nr_writeback
586 const int mem_table_count = sizeof(mem_table)/sizeof(mem_table_struct);
588 FILE_TO_BUF(MEMINFO_FILE,meminfo_fd);
594 tail = strchr(head, ':');
597 if(strlen(head) >= sizeof(namebuf)){
601 strcpy(namebuf,head);
602 found = bsearch(&findme, mem_table, mem_table_count,
603 sizeof(mem_table_struct), compare_mem_table_structs
606 if(!found) goto nextline;
607 *(found->slot) = strtoul(head,&tail,10);
609 tail = strchr(head, '\n');
613 if(!kb_low_total){ /* low==main except with large-memory support */
614 kb_low_total = kb_main_total;
615 kb_low_free = kb_main_free;
617 if(kb_inactive==~0UL){
618 kb_inactive = kb_inact_dirty + kb_inact_clean + kb_inact_laundry;
620 kb_swap_used = kb_swap_total - kb_swap_free;
621 kb_main_used = kb_main_total - kb_main_free;
624 /*****************************************************************/
626 /* read /proc/vminfo only for 2.5.41 and above */
628 typedef struct vm_table_struct {
629 const char *name; /* VM statistic name */
630 unsigned long *slot; /* slot in return struct */
633 static int compare_vm_table_structs(const void *a, const void *b){
634 return strcmp(((const vm_table_struct*)a)->name,((const vm_table_struct*)b)->name);
637 // see include/linux/page-flags.h and mm/page_alloc.c
638 unsigned long vm_nr_dirty; // dirty writable pages
639 unsigned long vm_nr_writeback; // pages under writeback
640 unsigned long vm_nr_pagecache; // pages in pagecache -- gone in 2.5.66+ kernels
641 unsigned long vm_nr_page_table_pages;// pages used for pagetables
642 unsigned long vm_nr_reverse_maps; // includes PageDirect
643 unsigned long vm_nr_mapped; // mapped into pagetables
644 unsigned long vm_nr_slab; // in slab
645 unsigned long vm_pgpgin; // kB disk reads (same as 1st num on /proc/stat page line)
646 unsigned long vm_pgpgout; // kB disk writes (same as 2nd num on /proc/stat page line)
647 unsigned long vm_pswpin; // swap reads (same as 1st num on /proc/stat swap line)
648 unsigned long vm_pswpout; // swap writes (same as 2nd num on /proc/stat swap line)
649 unsigned long vm_pgalloc; // page allocations
650 unsigned long vm_pgfree; // page freeings
651 unsigned long vm_pgactivate; // pages moved inactive -> active
652 unsigned long vm_pgdeactivate; // pages moved active -> inactive
653 unsigned long vm_pgfault; // total faults (major+minor)
654 unsigned long vm_pgmajfault; // major faults
655 unsigned long vm_pgscan; // pages scanned by page reclaim
656 unsigned long vm_pgrefill; // inspected by refill_inactive_zone
657 unsigned long vm_pgsteal; // total pages reclaimed
658 unsigned long vm_kswapd_steal; // pages reclaimed by kswapd
659 // next 3 as defined by the 2.5.52 kernel
660 unsigned long vm_pageoutrun; // times kswapd ran page reclaim
661 unsigned long vm_allocstall; // times a page allocator ran direct reclaim
662 unsigned long vm_pgrotated; // pages rotated to the tail of the LRU for immediate reclaim
663 // seen on a 2.6.8-rc1 kernel, apparently replacing old fields
664 static unsigned long vm_pgalloc_dma; //
665 static unsigned long vm_pgalloc_high; //
666 static unsigned long vm_pgalloc_normal; //
667 static unsigned long vm_pgrefill_dma; //
668 static unsigned long vm_pgrefill_high; //
669 static unsigned long vm_pgrefill_normal; //
670 static unsigned long vm_pgscan_direct_dma; //
671 static unsigned long vm_pgscan_direct_high; //
672 static unsigned long vm_pgscan_direct_normal; //
673 static unsigned long vm_pgscan_kswapd_dma; //
674 static unsigned long vm_pgscan_kswapd_high; //
675 static unsigned long vm_pgscan_kswapd_normal; //
676 static unsigned long vm_pgsteal_dma; //
677 static unsigned long vm_pgsteal_high; //
678 static unsigned long vm_pgsteal_normal; //
679 // seen on a 2.6.8-rc1 kernel
680 static unsigned long vm_kswapd_inodesteal; //
681 static unsigned long vm_nr_unstable; //
682 static unsigned long vm_pginodesteal; //
683 static unsigned long vm_slabs_scanned; //
686 char namebuf[16]; /* big enough to hold any row name */
687 vm_table_struct findme = { namebuf, NULL};
688 vm_table_struct *found;
691 static const vm_table_struct vm_table[] = {
692 {"allocstall", &vm_allocstall},
693 {"kswapd_inodesteal", &vm_kswapd_inodesteal},
694 {"kswapd_steal", &vm_kswapd_steal},
695 {"nr_dirty", &vm_nr_dirty}, // page version of meminfo Dirty
696 {"nr_mapped", &vm_nr_mapped}, // page version of meminfo Mapped
697 {"nr_page_table_pages", &vm_nr_page_table_pages},// same as meminfo PageTables
698 {"nr_pagecache", &vm_nr_pagecache}, // gone in 2.5.66+ kernels
699 {"nr_reverse_maps", &vm_nr_reverse_maps}, // page version of meminfo ReverseMaps GONE
700 {"nr_slab", &vm_nr_slab}, // page version of meminfo Slab
701 {"nr_unstable", &vm_nr_unstable},
702 {"nr_writeback", &vm_nr_writeback}, // page version of meminfo Writeback
703 {"pageoutrun", &vm_pageoutrun},
704 {"pgactivate", &vm_pgactivate},
705 {"pgalloc", &vm_pgalloc}, // GONE (now separate dma,high,normal)
706 {"pgalloc_dma", &vm_pgalloc_dma},
707 {"pgalloc_high", &vm_pgalloc_high},
708 {"pgalloc_normal", &vm_pgalloc_normal},
709 {"pgdeactivate", &vm_pgdeactivate},
710 {"pgfault", &vm_pgfault},
711 {"pgfree", &vm_pgfree},
712 {"pginodesteal", &vm_pginodesteal},
713 {"pgmajfault", &vm_pgmajfault},
714 {"pgpgin", &vm_pgpgin}, // important
715 {"pgpgout", &vm_pgpgout}, // important
716 {"pgrefill", &vm_pgrefill}, // GONE (now separate dma,high,normal)
717 {"pgrefill_dma", &vm_pgrefill_dma},
718 {"pgrefill_high", &vm_pgrefill_high},
719 {"pgrefill_normal", &vm_pgrefill_normal},
720 {"pgrotated", &vm_pgrotated},
721 {"pgscan", &vm_pgscan}, // GONE (now separate direct,kswapd and dma,high,normal)
722 {"pgscan_direct_dma", &vm_pgscan_direct_dma},
723 {"pgscan_direct_high", &vm_pgscan_direct_high},
724 {"pgscan_direct_normal",&vm_pgscan_direct_normal},
725 {"pgscan_kswapd_dma", &vm_pgscan_kswapd_dma},
726 {"pgscan_kswapd_high", &vm_pgscan_kswapd_high},
727 {"pgscan_kswapd_normal",&vm_pgscan_kswapd_normal},
728 {"pgsteal", &vm_pgsteal}, // GONE (now separate dma,high,normal)
729 {"pgsteal_dma", &vm_pgsteal_dma},
730 {"pgsteal_high", &vm_pgsteal_high},
731 {"pgsteal_normal", &vm_pgsteal_normal},
732 {"pswpin", &vm_pswpin}, // important
733 {"pswpout", &vm_pswpout}, // important
734 {"slabs_scanned", &vm_slabs_scanned},
736 const int vm_table_count = sizeof(vm_table)/sizeof(vm_table_struct);
743 FILE_TO_BUF(VMINFO_FILE,vminfo_fd);
747 tail = strchr(head, ' ');
750 if(strlen(head) >= sizeof(namebuf)){
754 strcpy(namebuf,head);
755 found = bsearch(&findme, vm_table, vm_table_count,
756 sizeof(vm_table_struct), compare_vm_table_structs
759 if(!found) goto nextline;
760 *(found->slot) = strtoul(head,&tail,10);
763 //if(found) fprintf(stderr,"%s=%d\n",found->name,*(found->slot));
764 //else fprintf(stderr,"%s not found\n",findme.name);
766 tail = strchr(head, '\n');
771 vm_pgalloc = vm_pgalloc_dma + vm_pgalloc_high + vm_pgalloc_normal;
773 vm_pgrefill = vm_pgrefill_dma + vm_pgrefill_high + vm_pgrefill_normal;
775 vm_pgscan = vm_pgscan_direct_dma + vm_pgscan_direct_high + vm_pgscan_direct_normal
776 + vm_pgscan_kswapd_dma + vm_pgscan_kswapd_high + vm_pgscan_kswapd_normal;
778 vm_pgsteal = vm_pgsteal_dma + vm_pgsteal_high + vm_pgsteal_normal;
781 ///////////////////////////////////////////////////////////////////////
782 // based on Fabian Frederick's /proc/diskstats parser
785 unsigned int getpartitions_num(struct disk_stat *disks, int ndisks){
789 for (i=0;i<ndisks;i++){
790 partitions+=disks[i].partitions;
796 /////////////////////////////////////////////////////////////////////////////
798 unsigned int getdiskstat(struct disk_stat **disks, struct partition_stat **partitions){
807 buff[BUFFSIZE-1] = 0;
808 fd = fopen("/proc/diskstats", "rb");
809 if(!fd) crash("/proc/diskstats");
812 if (!fgets(buff,BUFFSIZE-1,fd)){
816 fields = sscanf(buff, " %*d %*d %*s %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u", &dummy);
818 (*disks) = realloc(*disks, (cDisk+1)*sizeof(struct disk_stat));
819 sscanf(buff, " %*d %*d %15s %u %u %llu %u %u %u %llu %u %u %u %u",
822 (*disks)[cDisk].disk_name,
823 &(*disks)[cDisk].reads,
824 &(*disks)[cDisk].merged_reads,
825 &(*disks)[cDisk].reads_sectors,
826 &(*disks)[cDisk].milli_reading,
827 &(*disks)[cDisk].writes,
828 &(*disks)[cDisk].merged_writes,
829 &(*disks)[cDisk].written_sectors,
830 &(*disks)[cDisk].milli_writing,
831 &(*disks)[cDisk].inprogress_IO,
832 &(*disks)[cDisk].milli_spent_IO,
833 &(*disks)[cDisk].weighted_milli_spent_IO
835 (*disks)[cDisk].partitions=0;
838 (*partitions) = realloc(*partitions, (cPartition+1)*sizeof(struct partition_stat));
840 sscanf(buff, " %*d %*d %15s %u %llu %u %u",
843 (*partitions)[cPartition].partition_name,
844 &(*partitions)[cPartition].reads,
845 &(*partitions)[cPartition].reads_sectors,
846 &(*partitions)[cPartition].writes,
847 &(*partitions)[cPartition].requested_writes
849 (*partitions)[cPartition++].parent_disk = cDisk-1;
850 (*disks)[cDisk-1].partitions++;
857 /////////////////////////////////////////////////////////////////////////////
858 // based on Fabian Frederick's /proc/slabinfo parser
860 unsigned int getslabinfo (struct slab_cache **slab){
863 buff[BUFFSIZE-1] = 0;
865 fd = fopen("/proc/slabinfo", "rb");
866 if(!fd) crash("/proc/slabinfo");
867 while (fgets(buff,BUFFSIZE-1,fd)){
868 if(!memcmp("slabinfo - version:",buff,19)) continue; // skip header
869 if(*buff == '#') continue; // skip comments
870 (*slab) = realloc(*slab, (cSlab+1)*sizeof(struct slab_cache));
871 sscanf(buff, "%47s %u %u %u %u", // allow 47; max seen is 24
873 &(*slab)[cSlab].active_objs,
874 &(*slab)[cSlab].num_objs,
875 &(*slab)[cSlab].objsize,
876 &(*slab)[cSlab].objperslab
884 ///////////////////////////////////////////////////////////////////////////
886 unsigned get_pid_digits(void){
895 fd = open("/proc/sys/kernel/pid_max", O_RDONLY);
897 rc = read(fd, pidbuf, sizeof pidbuf);
901 rc = strtol(pidbuf,&endp,10);
903 if(*endp && *endp!='\n') goto out;
904 rc--; // the pid_max value is really the max PID plus 1