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
unsigned long getbtime(void) {
94
static unsigned long btime = 0;
100
/* /proc/stat can get very large on multi-CPU systems so we
101
can't use FILE_TO_BUF */
102
if (!(f = fopen(STAT_FILE, "r"))) {
103
fputs(BAD_OPEN_MESSAGE, stderr);
108
while ((fgets(buf, sizeof buf, f))) {
109
if (sscanf(buf, "btime %lu", &btime) == 1)
115
fputs("missing btime in " STAT_FILE "\n", stderr);
122
/***********************************************************************
123
* Some values in /proc are expressed in units of 1/HZ seconds, where HZ
124
* is the kernel clock tick rate. One of these units is called a jiffy.
125
* The HZ value used in the kernel may vary according to hacker desire.
126
* According to Linus Torvalds, this is not true. He considers the values
127
* in /proc as being in architecture-dependant units that have no relation
128
* to the kernel clock tick rate. Examination of the kernel source code
129
* reveals that opinion as wishful thinking.
131
* In any case, we need the HZ constant as used in /proc. (the real HZ value
132
* may differ, but we don't care) There are several ways we could get HZ:
134
* 1. Include the kernel header file. If it changes, recompile this library.
135
* 2. Use the sysconf() function. When HZ changes, recompile the C library!
136
* 3. Ask the kernel. This is obviously correct...
138
* Linus Torvalds won't let us ask the kernel, because he thinks we should
139
* not know the HZ value. Oh well, we don't have to listen to him.
140
* Someone smuggled out the HZ value. :-)
142
* This code should work fine, even if Linus fixes the kernel to match his
143
* stated behavior. The code only fails in case of a partial conversion.
145
* Recent update: on some architectures, the 2.4 kernel provides an
146
* ELF note to indicate HZ. This may be for ARM or user-mode Linux
147
* support. This ought to be investigated. Note that sysconf() is still
148
* unreliable, because it doesn't return an error code when it is
149
* used with a kernel that doesn't support the ELF note. On some other
150
* architectures there may be a system call or sysctl() that will work.
153
unsigned long long Hertz;
155
static void old_Hertz_hack(void){
156
unsigned long long user_j, nice_j, sys_j, other_j, wait_j, hirq_j, sirq_j, stol_j; /* jiffies (clock ticks) */
157
double up_1, up_2, seconds;
158
unsigned long long jiffies;
160
char *restrict savelocale;
162
wait_j = hirq_j = sirq_j = stol_j = 0;
163
savelocale = setlocale(LC_NUMERIC, NULL);
164
setlocale(LC_NUMERIC, "C");
166
FILE_TO_BUF(UPTIME_FILE,uptime_fd); sscanf(buf, "%lf", &up_1);
167
/* uptime(&up_1, NULL); */
168
FILE_TO_BUF(STAT_FILE,stat_fd);
169
sscanf(buf, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu", &user_j, &nice_j, &sys_j, &other_j, &wait_j, &hirq_j, &sirq_j, &stol_j);
170
FILE_TO_BUF(UPTIME_FILE,uptime_fd); sscanf(buf, "%lf", &up_2);
171
/* uptime(&up_2, NULL); */
172
} while((long long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */
173
setlocale(LC_NUMERIC, savelocale);
174
jiffies = user_j + nice_j + sys_j + other_j + wait_j + hirq_j + sirq_j + stol_j ;
175
seconds = (up_1 + up_2) / 2;
176
h = (unsigned)( (double)jiffies/seconds/smp_num_cpus );
177
/* actual values used by 2.4 kernels: 32 64 100 128 1000 1024 1200 */
179
case 9 ... 11 : Hertz = 10; break; /* S/390 (sometimes) */
180
case 18 ... 22 : Hertz = 20; break; /* user-mode Linux */
181
case 30 ... 34 : Hertz = 32; break; /* ia64 emulator */
182
case 48 ... 52 : Hertz = 50; break;
183
case 58 ... 61 : Hertz = 60; break;
184
case 62 ... 65 : Hertz = 64; break; /* StrongARM /Shark */
185
case 95 ... 105 : Hertz = 100; break; /* normal Linux */
186
case 124 ... 132 : Hertz = 128; break; /* MIPS, ARM */
187
case 195 ... 204 : Hertz = 200; break; /* normal << 1 */
188
case 247 ... 252 : Hertz = 250; break;
189
case 253 ... 260 : Hertz = 256; break;
190
case 393 ... 408 : Hertz = 400; break; /* normal << 2 */
191
case 790 ... 808 : Hertz = 800; break; /* normal << 3 */
192
case 990 ... 1010 : Hertz = 1000; break; /* ARM */
193
case 1015 ... 1035 : Hertz = 1024; break; /* Alpha, ia64 */
194
case 1180 ... 1220 : Hertz = 1200; break; /* Alpha */
197
Hertz = (unsigned long long)HZ; /* <asm/param.h> */
199
/* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
200
Hertz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL;
202
fprintf(stderr, "Unknown HZ value! (%d) Assume %Ld.\n", h, Hertz);
206
// same as: euid != uid || egid != gid
208
#define AT_SECURE 23 // secure mode boolean (true if setuid, etc.)
212
#define AT_CLKTCK 17 // frequency of times()
215
#define NOTE_NOT_FOUND 42
217
//extern char** environ;
219
/* for ELF executables, notes are pushed before environment and args */
220
static unsigned long find_elf_note(unsigned long findme){
221
unsigned long *ep = (unsigned long *)environ;
224
if(ep[0]==findme) return ep[1];
227
return NOTE_NOT_FOUND;
232
static int check_for_privs(void){
233
unsigned long rc = find_elf_note(AT_SECURE);
234
if(rc==NOTE_NOT_FOUND){
235
// not valid to run this code after UID or GID change!
236
// (if needed, may use AT_UID and friends instead)
237
rc = geteuid() != getuid() || getegid() != getgid();
242
static void init_libproc(void) __attribute__((constructor));
243
static void init_libproc(void){
244
have_privs = check_for_privs();
245
init_Linux_version(); /* Must be called before we check code */
246
// ought to count CPUs in /proc/stat instead of relying
247
// on glibc, which foolishly tries to parse /proc/cpuinfo
249
// SourceForge has an old Alpha running Linux 2.2.20 that
250
// appears to have a non-SMP kernel on a 2-way SMP box.
251
// _SC_NPROCESSORS_CONF returns 2, resulting in HZ=512
252
// _SC_NPROCESSORS_ONLN returns 1, which should work OK
253
smp_num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
254
if(smp_num_cpus<1) smp_num_cpus=1; /* SPARC glibc is buggy */
256
if(linux_version_code > LINUX_VERSION(2, 4, 0)){
257
Hertz = find_elf_note(AT_CLKTCK);
258
if(Hertz!=NOTE_NOT_FOUND) return;
259
fputs("2.4+ kernel w/o ELF notes? -- report this\n", stderr);
262
#if defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
263
/* On FreeBSD the Hertz hack is unrelaible, there is no ELF note and
264
* Hertz isn't defined in asm/params.h
265
* See Debian Bug #460331
274
/***********************************************************************
275
* The /proc filesystem calculates idle=jiffies-(user+nice+sys) and we
276
* recover jiffies by adding up the 4 or 5 numbers we are given. SMP kernels
277
* (as of pre-2.4 era) can report idle time going backwards, perhaps due
278
* to non-atomic reads and updates. There is no locking for these values.
283
#define JT unsigned long long
284
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){
285
double tmp_u, tmp_n, tmp_s, tmp_i, tmp_w, tmp_x, tmp_y, tmp_z;
286
double scale; /* scale values to % */
287
static JT old_u, old_n, old_s, old_i, old_w, old_x, old_y, old_z;
288
JT new_u, new_n, new_s, new_i, new_w, new_x, new_y, new_z;
289
JT ticks_past; /* avoid div-by-0 by not calling too often :-( */
300
FILE_TO_BUF(STAT_FILE,stat_fd);
301
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);
302
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);
304
scale = 100.0 / (double)ticks_past;
305
tmp_u = ( (double)new_u - (double)old_u ) * scale;
306
tmp_n = ( (double)new_n - (double)old_n ) * scale;
307
tmp_s = ( (double)new_s - (double)old_s ) * scale;
308
tmp_i = ( (double)new_i - (double)old_i ) * scale;
309
tmp_w = ( (double)new_w - (double)old_w ) * scale;
310
tmp_x = ( (double)new_x - (double)old_x ) * scale;
311
tmp_y = ( (double)new_y - (double)old_y ) * scale;
312
tmp_z = ( (double)new_z - (double)old_z ) * scale;
323
SET_IF_DESIRED(uret, tmp_u);
324
SET_IF_DESIRED(nret, tmp_n);
325
SET_IF_DESIRED(sret, tmp_s);
326
SET_IF_DESIRED(iret, tmp_i);
327
SET_IF_DESIRED(wret, tmp_w);
328
SET_IF_DESIRED(xret, tmp_x);
329
SET_IF_DESIRED(yret, tmp_y);
330
SET_IF_DESIRED(zret, tmp_z);
343
/***********************************************************************/
344
void loadavg(double *restrict av1, double *restrict av5, double *restrict av15) {
345
double avg_1=0, avg_5=0, avg_15=0;
346
char *restrict savelocale;
348
FILE_TO_BUF(LOADAVG_FILE,loadavg_fd);
349
savelocale = setlocale(LC_NUMERIC, NULL);
350
setlocale(LC_NUMERIC, "C");
351
if (sscanf(buf, "%lf %lf %lf", &avg_1, &avg_5, &avg_15) < 3) {
352
fputs("bad data in " LOADAVG_FILE "\n", stderr);
355
setlocale(LC_NUMERIC, savelocale);
356
SET_IF_DESIRED(av1, avg_1);
357
SET_IF_DESIRED(av5, avg_5);
358
SET_IF_DESIRED(av15, avg_15);
361
static char buff[BUFFSIZE]; /* used in the procedures */
362
/***********************************************************************/
364
static void crash(const char *filename) {
369
/***********************************************************************/
371
static void getrunners(unsigned int *restrict running, unsigned int *restrict blocked) {
378
if((proc=opendir("/proc"))==NULL) crash("/proc");
380
while(( ent=readdir(proc) )) {
386
if (!isdigit(ent->d_name[0])) continue;
387
sprintf(tbuf, "/proc/%s/stat", ent->d_name);
389
fd = open(tbuf, O_RDONLY, 0);
390
if (fd == -1) continue;
391
memset(tbuf, '\0', sizeof tbuf); // didn't feel like checking read()
392
read(fd, tbuf, sizeof tbuf - 1); // need 32 byte buffer at most
395
cp = strrchr(tbuf, ')');
411
/***********************************************************************/
413
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,
414
unsigned long *restrict pin, unsigned long *restrict pout, unsigned long *restrict s_in, unsigned long *restrict sout,
415
unsigned *restrict intr, unsigned *restrict ctxt,
416
unsigned int *restrict running, unsigned int *restrict blocked,
417
unsigned int *restrict btime, unsigned int *restrict processes) {
419
unsigned long long llbuf = 0;
420
int need_vmstat_file = 0;
421
int need_proc_scan = 0;
423
buff[BUFFSIZE-1] = 0; /* ensure null termination in buffer */
426
lseek(fd, 0L, SEEK_SET);
428
fd = open("/proc/stat", O_RDONLY, 0);
429
if(fd == -1) crash("/proc/stat");
431
read(fd,buff,BUFFSIZE-1);
433
*ciow = 0; /* not separated out until the 2.5.41 kernel */
434
*cxxx = 0; /* not separated out until the 2.6.0-test4 kernel */
435
*cyyy = 0; /* not separated out until the 2.6.0-test4 kernel */
436
*czzz = 0; /* not separated out until the 2.6.11 kernel */
438
b = strstr(buff, "cpu ");
439
if(b) sscanf(b, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu", cuse, cice, csys, cide, ciow, cxxx, cyyy, czzz);
441
b = strstr(buff, "page ");
442
if(b) sscanf(b, "page %lu %lu", pin, pout);
443
else need_vmstat_file = 1;
445
b = strstr(buff, "swap ");
446
if(b) sscanf(b, "swap %lu %lu", s_in, sout);
447
else need_vmstat_file = 1;
449
b = strstr(buff, "intr ");
450
if(b) sscanf(b, "intr %Lu", &llbuf);
453
b = strstr(buff, "ctxt ");
454
if(b) sscanf(b, "ctxt %Lu", &llbuf);
457
b = strstr(buff, "btime ");
458
if(b) sscanf(b, "btime %u", btime);
460
b = strstr(buff, "processes ");
461
if(b) sscanf(b, "processes %u", processes);
463
b = strstr(buff, "procs_running ");
464
if(b) sscanf(b, "procs_running %u", running);
465
else need_proc_scan = 1;
467
b = strstr(buff, "procs_blocked ");
468
if(b) sscanf(b, "procs_blocked %u", blocked);
469
else need_proc_scan = 1;
471
if(need_proc_scan){ /* Linux 2.5.46 (approximately) and below */
472
getrunners(running, blocked);
475
(*running)--; // exclude vmstat itself
477
if(need_vmstat_file){ /* Linux 2.5.40-bk4 and above */
486
/***********************************************************************/
488
* Copyright 1999 by Albert Cahalan; all rights reserved.
489
* This file may be used subject to the terms and conditions of the
490
* GNU Library General Public License Version 2, or any later version
491
* at your option, as published by the Free Software Foundation.
492
* This program is distributed in the hope that it will be useful,
493
* but WITHOUT ANY WARRANTY; without even the implied warranty of
494
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
495
* GNU Library General Public License for more details.
498
typedef struct mem_table_struct {
499
const char *name; /* memory type name */
500
unsigned long *slot; /* slot in return struct */
503
static int compare_mem_table_structs(const void *a, const void *b){
504
return strcmp(((const mem_table_struct*)a)->name,((const mem_table_struct*)b)->name);
507
/* example data, following junk, with comments added:
509
* MemTotal: 61768 kB old
510
* MemFree: 1436 kB old
511
* MemShared: 0 kB old (now always zero; not calculated)
512
* Buffers: 1312 kB old
513
* Cached: 20932 kB old
514
* Active: 12464 kB new
515
* Inact_dirty: 7772 kB new
516
* Inact_clean: 2008 kB new
517
* Inact_target: 0 kB new
518
* Inact_laundry: 0 kB new, and might be missing too
523
* SwapTotal: 122580 kB old
524
* SwapFree: 60352 kB old
525
* Inactive: 20420 kB 2.5.41+
526
* Dirty: 0 kB 2.5.41+
527
* Writeback: 0 kB 2.5.41+
528
* Mapped: 9792 kB 2.5.41+
529
* Slab: 4564 kB 2.5.41+
530
* Committed_AS: 8440 kB 2.5.41+
531
* PageTables: 304 kB 2.5.41+
532
* ReverseMaps: 5738 2.5.41+
533
* SwapCached: 0 kB 2.5.??+
534
* HugePages_Total: 220 2.5.??+
535
* HugePages_Free: 138 2.5.??+
536
* Hugepagesize: 4096 kB 2.5.??+
540
unsigned long kb_main_shared;
541
/* old but still kicking -- the important stuff */
542
unsigned long kb_main_buffers;
543
unsigned long kb_main_cached;
544
unsigned long kb_main_free;
545
unsigned long kb_main_total;
546
unsigned long kb_swap_free;
547
unsigned long kb_swap_total;
548
/* recently introduced */
549
unsigned long kb_high_free;
550
unsigned long kb_high_total;
551
unsigned long kb_low_free;
552
unsigned long kb_low_total;
554
unsigned long kb_active;
555
unsigned long kb_inact_laundry;
556
unsigned long kb_inact_dirty;
557
unsigned long kb_inact_clean;
558
unsigned long kb_inact_target;
559
unsigned long kb_swap_cached; /* late 2.4 and 2.6+ only */
561
unsigned long kb_swap_used;
562
unsigned long kb_main_used;
564
unsigned long kb_writeback;
565
unsigned long kb_slab;
566
unsigned long nr_reversemaps;
567
unsigned long kb_committed_as;
568
unsigned long kb_dirty;
569
unsigned long kb_inactive;
570
unsigned long kb_mapped;
571
unsigned long kb_pagetables;
572
// seen on a 2.6.x kernel:
573
static unsigned long kb_vmalloc_chunk;
574
static unsigned long kb_vmalloc_total;
575
static unsigned long kb_vmalloc_used;
576
// seen on 2.6.24-rc6-git12
577
static unsigned long kb_anon_pages;
578
static unsigned long kb_bounce;
579
static unsigned long kb_commit_limit;
580
static unsigned long kb_nfs_unstable;
581
static unsigned long kb_swap_reclaimable;
582
static unsigned long kb_swap_unreclaimable;
585
char namebuf[16]; /* big enough to hold any row name */
586
mem_table_struct findme = { namebuf, NULL};
587
mem_table_struct *found;
590
static const mem_table_struct mem_table[] = {
591
{"Active", &kb_active}, // important
592
{"AnonPages", &kb_anon_pages},
593
{"Bounce", &kb_bounce},
594
{"Buffers", &kb_main_buffers}, // important
595
{"Cached", &kb_main_cached}, // important
596
{"CommitLimit", &kb_commit_limit},
597
{"Committed_AS", &kb_committed_as},
598
{"Dirty", &kb_dirty}, // kB version of vmstat nr_dirty
599
{"HighFree", &kb_high_free},
600
{"HighTotal", &kb_high_total},
601
{"Inact_clean", &kb_inact_clean},
602
{"Inact_dirty", &kb_inact_dirty},
603
{"Inact_laundry",&kb_inact_laundry},
604
{"Inact_target", &kb_inact_target},
605
{"Inactive", &kb_inactive}, // important
606
{"LowFree", &kb_low_free},
607
{"LowTotal", &kb_low_total},
608
{"Mapped", &kb_mapped}, // kB version of vmstat nr_mapped
609
{"MemFree", &kb_main_free}, // important
610
{"MemShared", &kb_main_shared}, // important, but now gone!
611
{"MemTotal", &kb_main_total}, // important
612
{"NFS_Unstable", &kb_nfs_unstable},
613
{"PageTables", &kb_pagetables}, // kB version of vmstat nr_page_table_pages
614
{"ReverseMaps", &nr_reversemaps}, // same as vmstat nr_page_table_pages
615
{"SReclaimable", &kb_swap_reclaimable}, // "swap reclaimable" (dentry and inode structures)
616
{"SUnreclaim", &kb_swap_unreclaimable},
617
{"Slab", &kb_slab}, // kB version of vmstat nr_slab
618
{"SwapCached", &kb_swap_cached},
619
{"SwapFree", &kb_swap_free}, // important
620
{"SwapTotal", &kb_swap_total}, // important
621
{"VmallocChunk", &kb_vmalloc_chunk},
622
{"VmallocTotal", &kb_vmalloc_total},
623
{"VmallocUsed", &kb_vmalloc_used},
624
{"Writeback", &kb_writeback}, // kB version of vmstat nr_writeback
626
const int mem_table_count = sizeof(mem_table)/sizeof(mem_table_struct);
628
FILE_TO_BUF(MEMINFO_FILE,meminfo_fd);
634
tail = strchr(head, ':');
637
if(strlen(head) >= sizeof(namebuf)){
641
strcpy(namebuf,head);
642
found = bsearch(&findme, mem_table, mem_table_count,
643
sizeof(mem_table_struct), compare_mem_table_structs
646
if(!found) goto nextline;
647
*(found->slot) = (unsigned long)strtoull(head,&tail,10);
649
tail = strchr(head, '\n');
653
if(!kb_low_total){ /* low==main except with large-memory support */
654
kb_low_total = kb_main_total;
655
kb_low_free = kb_main_free;
657
if(kb_inactive==~0UL){
658
kb_inactive = kb_inact_dirty + kb_inact_clean + kb_inact_laundry;
660
kb_swap_used = kb_swap_total - kb_swap_free;
661
kb_main_used = kb_main_total - kb_main_free;
664
/*****************************************************************/
666
/* read /proc/vminfo only for 2.5.41 and above */
668
typedef struct vm_table_struct {
669
const char *name; /* VM statistic name */
670
unsigned long *slot; /* slot in return struct */
673
static int compare_vm_table_structs(const void *a, const void *b){
674
return strcmp(((const vm_table_struct*)a)->name,((const vm_table_struct*)b)->name);
677
// see include/linux/page-flags.h and mm/page_alloc.c
678
unsigned long vm_nr_dirty; // dirty writable pages
679
unsigned long vm_nr_writeback; // pages under writeback
680
unsigned long vm_nr_pagecache; // pages in pagecache -- gone in 2.5.66+ kernels
681
unsigned long vm_nr_page_table_pages;// pages used for pagetables
682
unsigned long vm_nr_reverse_maps; // includes PageDirect
683
unsigned long vm_nr_mapped; // mapped into pagetables
684
unsigned long vm_nr_slab; // in slab
685
unsigned long vm_pgpgin; // kB disk reads (same as 1st num on /proc/stat page line)
686
unsigned long vm_pgpgout; // kB disk writes (same as 2nd num on /proc/stat page line)
687
unsigned long vm_pswpin; // swap reads (same as 1st num on /proc/stat swap line)
688
unsigned long vm_pswpout; // swap writes (same as 2nd num on /proc/stat swap line)
689
unsigned long vm_pgalloc; // page allocations
690
unsigned long vm_pgfree; // page freeings
691
unsigned long vm_pgactivate; // pages moved inactive -> active
692
unsigned long vm_pgdeactivate; // pages moved active -> inactive
693
unsigned long vm_pgfault; // total faults (major+minor)
694
unsigned long vm_pgmajfault; // major faults
695
unsigned long vm_pgscan; // pages scanned by page reclaim
696
unsigned long vm_pgrefill; // inspected by refill_inactive_zone
697
unsigned long vm_pgsteal; // total pages reclaimed
698
unsigned long vm_kswapd_steal; // pages reclaimed by kswapd
699
// next 3 as defined by the 2.5.52 kernel
700
unsigned long vm_pageoutrun; // times kswapd ran page reclaim
701
unsigned long vm_allocstall; // times a page allocator ran direct reclaim
702
unsigned long vm_pgrotated; // pages rotated to the tail of the LRU for immediate reclaim
703
// seen on a 2.6.8-rc1 kernel, apparently replacing old fields
704
static unsigned long vm_pgalloc_dma; //
705
static unsigned long vm_pgalloc_high; //
706
static unsigned long vm_pgalloc_normal; //
707
static unsigned long vm_pgrefill_dma; //
708
static unsigned long vm_pgrefill_high; //
709
static unsigned long vm_pgrefill_normal; //
710
static unsigned long vm_pgscan_direct_dma; //
711
static unsigned long vm_pgscan_direct_high; //
712
static unsigned long vm_pgscan_direct_normal; //
713
static unsigned long vm_pgscan_kswapd_dma; //
714
static unsigned long vm_pgscan_kswapd_high; //
715
static unsigned long vm_pgscan_kswapd_normal; //
716
static unsigned long vm_pgsteal_dma; //
717
static unsigned long vm_pgsteal_high; //
718
static unsigned long vm_pgsteal_normal; //
719
// seen on a 2.6.8-rc1 kernel
720
static unsigned long vm_kswapd_inodesteal; //
721
static unsigned long vm_nr_unstable; //
722
static unsigned long vm_pginodesteal; //
723
static unsigned long vm_slabs_scanned; //
726
char namebuf[16]; /* big enough to hold any row name */
727
vm_table_struct findme = { namebuf, NULL};
728
vm_table_struct *found;
731
static const vm_table_struct vm_table[] = {
732
{"allocstall", &vm_allocstall},
733
{"kswapd_inodesteal", &vm_kswapd_inodesteal},
734
{"kswapd_steal", &vm_kswapd_steal},
735
{"nr_dirty", &vm_nr_dirty}, // page version of meminfo Dirty
736
{"nr_mapped", &vm_nr_mapped}, // page version of meminfo Mapped
737
{"nr_page_table_pages", &vm_nr_page_table_pages},// same as meminfo PageTables
738
{"nr_pagecache", &vm_nr_pagecache}, // gone in 2.5.66+ kernels
739
{"nr_reverse_maps", &vm_nr_reverse_maps}, // page version of meminfo ReverseMaps GONE
740
{"nr_slab", &vm_nr_slab}, // page version of meminfo Slab
741
{"nr_unstable", &vm_nr_unstable},
742
{"nr_writeback", &vm_nr_writeback}, // page version of meminfo Writeback
743
{"pageoutrun", &vm_pageoutrun},
744
{"pgactivate", &vm_pgactivate},
745
{"pgalloc", &vm_pgalloc}, // GONE (now separate dma,high,normal)
746
{"pgalloc_dma", &vm_pgalloc_dma},
747
{"pgalloc_high", &vm_pgalloc_high},
748
{"pgalloc_normal", &vm_pgalloc_normal},
749
{"pgdeactivate", &vm_pgdeactivate},
750
{"pgfault", &vm_pgfault},
751
{"pgfree", &vm_pgfree},
752
{"pginodesteal", &vm_pginodesteal},
753
{"pgmajfault", &vm_pgmajfault},
754
{"pgpgin", &vm_pgpgin}, // important
755
{"pgpgout", &vm_pgpgout}, // important
756
{"pgrefill", &vm_pgrefill}, // GONE (now separate dma,high,normal)
757
{"pgrefill_dma", &vm_pgrefill_dma},
758
{"pgrefill_high", &vm_pgrefill_high},
759
{"pgrefill_normal", &vm_pgrefill_normal},
760
{"pgrotated", &vm_pgrotated},
761
{"pgscan", &vm_pgscan}, // GONE (now separate direct,kswapd and dma,high,normal)
762
{"pgscan_direct_dma", &vm_pgscan_direct_dma},
763
{"pgscan_direct_high", &vm_pgscan_direct_high},
764
{"pgscan_direct_normal",&vm_pgscan_direct_normal},
765
{"pgscan_kswapd_dma", &vm_pgscan_kswapd_dma},
766
{"pgscan_kswapd_high", &vm_pgscan_kswapd_high},
767
{"pgscan_kswapd_normal",&vm_pgscan_kswapd_normal},
768
{"pgsteal", &vm_pgsteal}, // GONE (now separate dma,high,normal)
769
{"pgsteal_dma", &vm_pgsteal_dma},
770
{"pgsteal_high", &vm_pgsteal_high},
771
{"pgsteal_normal", &vm_pgsteal_normal},
772
{"pswpin", &vm_pswpin}, // important
773
{"pswpout", &vm_pswpout}, // important
774
{"slabs_scanned", &vm_slabs_scanned},
776
const int vm_table_count = sizeof(vm_table)/sizeof(vm_table_struct);
783
FILE_TO_BUF(VMINFO_FILE,vminfo_fd);
787
tail = strchr(head, ' ');
790
if(strlen(head) >= sizeof(namebuf)){
794
strcpy(namebuf,head);
795
found = bsearch(&findme, vm_table, vm_table_count,
796
sizeof(vm_table_struct), compare_vm_table_structs
799
if(!found) goto nextline;
800
*(found->slot) = strtoul(head,&tail,10);
803
//if(found) fprintf(stderr,"%s=%d\n",found->name,*(found->slot));
804
//else fprintf(stderr,"%s not found\n",findme.name);
806
tail = strchr(head, '\n');
811
vm_pgalloc = vm_pgalloc_dma + vm_pgalloc_high + vm_pgalloc_normal;
813
vm_pgrefill = vm_pgrefill_dma + vm_pgrefill_high + vm_pgrefill_normal;
815
vm_pgscan = vm_pgscan_direct_dma + vm_pgscan_direct_high + vm_pgscan_direct_normal
816
+ vm_pgscan_kswapd_dma + vm_pgscan_kswapd_high + vm_pgscan_kswapd_normal;
818
vm_pgsteal = vm_pgsteal_dma + vm_pgsteal_high + vm_pgsteal_normal;
821
///////////////////////////////////////////////////////////////////////
822
// based on Fabian Frederick's /proc/diskstats parser
825
unsigned int getpartitions_num(struct disk_stat *disks, int ndisks){
829
for (i=0;i<ndisks;i++){
830
partitions+=disks[i].partitions;
836
/////////////////////////////////////////////////////////////////////////////
837
static int is_disk(char *dev)
842
while ((slash = strchr(dev, '/')))
844
snprintf(syspath, sizeof(syspath), "/sys/block/%s", dev);
845
return !(access(syspath, F_OK));
848
/////////////////////////////////////////////////////////////////////////////
850
unsigned int getdiskstat(struct disk_stat **disks, struct partition_stat **partitions){
860
buff[BUFFSIZE-1] = 0;
861
fd = fopen("/proc/diskstats", "rb");
862
if(!fd) crash("/proc/diskstats");
865
if (!fgets(buff,BUFFSIZE-1,fd)){
869
fields = sscanf(buff, " %*d %*d %15s %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u",
871
if (fields == 2 && is_disk(devname)){
872
(*disks) = realloc(*disks, (cDisk+1)*sizeof(struct disk_stat));
873
sscanf(buff, " %*d %*d %15s %u %u %llu %u %u %u %llu %u %u %u %u",
876
(*disks)[cDisk].disk_name,
877
&(*disks)[cDisk].reads,
878
&(*disks)[cDisk].merged_reads,
879
&(*disks)[cDisk].reads_sectors,
880
&(*disks)[cDisk].milli_reading,
881
&(*disks)[cDisk].writes,
882
&(*disks)[cDisk].merged_writes,
883
&(*disks)[cDisk].written_sectors,
884
&(*disks)[cDisk].milli_writing,
885
&(*disks)[cDisk].inprogress_IO,
886
&(*disks)[cDisk].milli_spent_IO,
887
&(*disks)[cDisk].weighted_milli_spent_IO
889
(*disks)[cDisk].partitions=0;
892
(*partitions) = realloc(*partitions, (cPartition+1)*sizeof(struct partition_stat));
894
sscanf(buff, (fields == 2)
895
? " %*d %*d %15s %u %*u %llu %*u %u %*u %llu %*u %*u %*u %*u"
896
: " %*d %*d %15s %u %llu %u %llu",
899
(*partitions)[cPartition].partition_name,
900
&(*partitions)[cPartition].reads,
901
&(*partitions)[cPartition].reads_sectors,
902
&(*partitions)[cPartition].writes,
903
&(*partitions)[cPartition].requested_writes
905
(*partitions)[cPartition++].parent_disk = cDisk-1;
906
(*disks)[cDisk-1].partitions++;
913
/////////////////////////////////////////////////////////////////////////////
914
// based on Fabian Frederick's /proc/slabinfo parser
916
unsigned int getslabinfo (struct slab_cache **slab){
919
buff[BUFFSIZE-1] = 0;
921
fd = fopen("/proc/slabinfo", "rb");
922
if(!fd) crash("/proc/slabinfo");
923
while (fgets(buff,BUFFSIZE-1,fd)){
924
if(!memcmp("slabinfo - version:",buff,19)) continue; // skip header
925
if(*buff == '#') continue; // skip comments
926
(*slab) = realloc(*slab, (cSlab+1)*sizeof(struct slab_cache));
927
sscanf(buff, "%47s %u %u %u %u", // allow 47; max seen is 24
929
&(*slab)[cSlab].active_objs,
930
&(*slab)[cSlab].num_objs,
931
&(*slab)[cSlab].objsize,
932
&(*slab)[cSlab].objperslab
940
///////////////////////////////////////////////////////////////////////////
942
unsigned get_pid_digits(void){
951
fd = open("/proc/sys/kernel/pid_max", O_RDONLY);
953
rc = read(fd, pidbuf, sizeof pidbuf);
957
rc = strtol(pidbuf,&endp,10);
959
if(*endp && *endp!='\n') goto out;
960
rc--; // the pid_max value is really the max PID plus 1