There are no available options for this view.

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1.40 - (show annotations) (download) (as text)
Mon Mar 24 04:41:26 2008 UTC (5 years, 2 months ago) by albert
Branch: MAIN
Changes since 1.39: +16 -1 lines
File MIME type: text/x-chdr
new /proc/meminfo fields
1 // Copyright (C) 1992-1998 by Michael K. Johnson, johnsonm@redhat.com
2 // Copyright 1998-2003 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 // June 2003, Fabian Frederick, disk and slab info
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <locale.h>
17
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include "version.h"
21 #include "sysinfo.h" /* include self to verify prototypes */
22
23 #ifndef HZ
24 #include <netinet/in.h> /* htons */
25 #endif
26
27 long smp_num_cpus; /* number of CPUs */
28
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"
34
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;
45
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];
49
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.
53 */
54 #define FILE_TO_BUF(filename, fd) do{ \
55 static int local_n; \
56 if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \
57 fputs(BAD_OPEN_MESSAGE, stderr); \
58 fflush(NULL); \
59 _exit(102); \
60 } \
61 lseek(fd, 0L, SEEK_SET); \
62 if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \
63 perror(filename); \
64 fflush(NULL); \
65 _exit(103); \
66 } \
67 buf[local_n] = '\0'; \
68 }while(0)
69
70 /* evals 'x' twice */
71 #define SET_IF_DESIRED(x,y) do{ if(x) *(x) = (y); }while(0)
72
73
74 /***********************************************************************/
75 int uptime(double *restrict uptime_secs, double *restrict idle_secs) {
76 double up=0, idle=0;
77 char *restrict savelocale;
78
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);
85 return 0;
86 }
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 */
91 }
92
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.
101 *
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:
104 *
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...
108 *
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. :-)
112 *
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.
115 *
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.
122 */
123
124 unsigned long long Hertz;
125
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;
130 unsigned h;
131 char *restrict savelocale;
132
133 savelocale = setlocale(LC_NUMERIC, NULL);
134 setlocale(LC_NUMERIC, "C");
135 do{
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 */
148 switch(h){
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 */
165 default:
166 #ifdef HZ
167 Hertz = (unsigned long long)HZ; /* <asm/param.h> */
168 #else
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;
171 #endif
172 fprintf(stderr, "Unknown HZ value! (%d) Assume %Ld.\n", h, Hertz);
173 }
174 }
175
176 // same as: euid != uid || egid != gid
177 #ifndef AT_SECURE
178 #define AT_SECURE 23 // secure mode boolean (true if setuid, etc.)
179 #endif
180
181 #ifndef AT_CLKTCK
182 #define AT_CLKTCK 17 // frequency of times()
183 #endif
184
185 #define NOTE_NOT_FOUND 42
186
187 //extern char** environ;
188
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;
192 while(*ep++);
193 while(*ep){
194 if(ep[0]==findme) return ep[1];
195 ep+=2;
196 }
197 return NOTE_NOT_FOUND;
198 }
199
200 int have_privs;
201
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();
208 }
209 return !!rc;
210 }
211
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
217 //
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 */
224
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);
229 }
230 old_Hertz_hack();
231 }
232
233 #if 0
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.
239 */
240 #ifndef NAN
241 #define NAN (-0.0)
242 #endif
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 :-( */
250
251 tmp_w = 0.0;
252 new_w = 0;
253 tmp_x = 0.0;
254 new_x = 0;
255 tmp_y = 0.0;
256 new_y = 0;
257 tmp_z = 0.0;
258 new_z = 0;
259
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);
263 if(ticks_past){
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;
273 }else{
274 tmp_u = NAN;
275 tmp_n = NAN;
276 tmp_s = NAN;
277 tmp_i = NAN;
278 tmp_w = NAN;
279 tmp_x = NAN;
280 tmp_y = NAN;
281 tmp_z = NAN;
282 }
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);
291 old_u=new_u;
292 old_n=new_n;
293 old_s=new_s;
294 old_i=new_i;
295 old_w=new_w;
296 old_x=new_x;
297 old_y=new_y;
298 old_z=new_z;
299 }
300 #undef JT
301 #endif
302
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;
307
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);
313 exit(1);
314 }
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);
319 }
320
321 static char buff[BUFFSIZE]; /* used in the procedures */
322 /***********************************************************************/
323
324 static void crash(const char *filename) {
325 perror(filename);
326 exit(EXIT_FAILURE);
327 }
328
329 /***********************************************************************/
330
331 static void getrunners(unsigned int *restrict running, unsigned int *restrict blocked) {
332 struct direct *ent;
333 DIR *proc;
334
335 *running=0;
336 *blocked=0;
337
338 if((proc=opendir("/proc"))==NULL) crash("/proc");
339
340 while(( ent=readdir(proc) )) {
341 char tbuf[32];
342 char *cp;
343 int fd;
344 char c;
345
346 if (!isdigit(ent->d_name[0])) continue;
347 sprintf(tbuf, "/proc/%s/stat", ent->d_name);
348
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
353 close(fd);
354
355 cp = strrchr(tbuf, ')');
356 if(!cp) continue;
357 c = cp[2];
358
359 if (c=='R') {
360 (*running)++;
361 continue;
362 }
363 if (c=='D') {
364 (*blocked)++;
365 continue;
366 }
367 }
368 closedir(proc);
369 }
370
371 /***********************************************************************/
372
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) {
378 static int fd;
379 unsigned long long llbuf = 0;
380 int need_vmstat_file = 0;
381 int need_proc_scan = 0;
382 const char* b;
383 buff[BUFFSIZE-1] = 0; /* ensure null termination in buffer */
384
385 if(fd){
386 lseek(fd, 0L, SEEK_SET);
387 }else{
388 fd = open("/proc/stat", O_RDONLY, 0);
389 if(fd == -1) crash("/proc/stat");
390 }
391 read(fd,buff,BUFFSIZE-1);
392 *intr = 0;
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 */
397
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);
400
401 b = strstr(buff, "page ");
402 if(b) sscanf(b, "page %lu %lu", pin, pout);
403 else need_vmstat_file = 1;
404
405 b = strstr(buff, "swap ");
406 if(b) sscanf(b, "swap %lu %lu", s_in, sout);
407 else need_vmstat_file = 1;
408
409 b = strstr(buff, "intr ");
410 if(b) sscanf(b, "intr %Lu", &llbuf);
411 *intr = llbuf;
412
413 b = strstr(buff, "ctxt ");
414 if(b) sscanf(b, "ctxt %Lu", &llbuf);
415 *ctxt = llbuf;
416
417 b = strstr(buff, "btime ");
418 if(b) sscanf(b, "btime %u", btime);
419
420 b = strstr(buff, "processes ");
421 if(b) sscanf(b, "processes %u", processes);
422
423 b = strstr(buff, "procs_running ");
424 if(b) sscanf(b, "procs_running %u", running);
425 else need_proc_scan = 1;
426
427 b = strstr(buff, "procs_blocked ");
428 if(b) sscanf(b, "procs_blocked %u", blocked);
429 else need_proc_scan = 1;
430
431 if(need_proc_scan){ /* Linux 2.5.46 (approximately) and below */
432 getrunners(running, blocked);
433 }
434
435 (*running)--; // exclude vmstat itself
436
437 if(need_vmstat_file){ /* Linux 2.5.40-bk4 and above */
438 vminfo();
439 *pin = vm_pgpgin;
440 *pout = vm_pgpgout;
441 *s_in = vm_pswpin;
442 *sout = vm_pswpout;
443 }
444 }
445
446 /***********************************************************************/
447 /*
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.
456 */
457
458 typedef struct mem_table_struct {
459 const char *name; /* memory type name */
460 unsigned long *slot; /* slot in return struct */
461 } mem_table_struct;
462
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);
465 }
466
467 /* example data, following junk, with comments added:
468 *
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
479 * HighTotal: 0 kB
480 * HighFree: 0 kB
481 * LowTotal: 61768 kB
482 * LowFree: 1436 kB
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.??+
497 */
498
499 /* obsolete */
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;
513 /* 2.4.xx era */
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 */
520 /* derived values */
521 unsigned long kb_swap_used;
522 unsigned long kb_main_used;
523 /* 2.5.41+ */
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;
543
544 void meminfo(void){
545 char namebuf[16]; /* big enough to hold any row name */
546 mem_table_struct findme = { namebuf, NULL};
547 mem_table_struct *found;
548 char *head;
549 char *tail;
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
585 };
586 const int mem_table_count = sizeof(mem_table)/sizeof(mem_table_struct);
587
588 FILE_TO_BUF(MEMINFO_FILE,meminfo_fd);
589
590 kb_inactive = ~0UL;
591
592 head = buf;
593 for(;;){
594 tail = strchr(head, ':');
595 if(!tail) break;
596 *tail = '\0';
597 if(strlen(head) >= sizeof(namebuf)){
598 head = tail+1;
599 goto nextline;
600 }
601 strcpy(namebuf,head);
602 found = bsearch(&findme, mem_table, mem_table_count,
603 sizeof(mem_table_struct), compare_mem_table_structs
604 );
605 head = tail+1;
606 if(!found) goto nextline;
607 *(found->slot) = strtoul(head,&tail,10);
608 nextline:
609 tail = strchr(head, '\n');
610 if(!tail) break;
611 head = tail+1;
612 }
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;
616 }
617 if(kb_inactive==~0UL){
618 kb_inactive = kb_inact_dirty + kb_inact_clean + kb_inact_laundry;
619 }
620 kb_swap_used = kb_swap_total - kb_swap_free;
621 kb_main_used = kb_main_total - kb_main_free;
622 }
623
624 /*****************************************************************/
625
626 /* read /proc/vminfo only for 2.5.41 and above */
627
628 typedef struct vm_table_struct {
629 const char *name; /* VM statistic name */
630 unsigned long *slot; /* slot in return struct */
631 } vm_table_struct;
632
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);
635 }
636
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; //
684
685 void vminfo(void){
686 char namebuf[16]; /* big enough to hold any row name */
687 vm_table_struct findme = { namebuf, NULL};
688 vm_table_struct *found;
689 char *head;
690 char *tail;
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},
735 };
736 const int vm_table_count = sizeof(vm_table)/sizeof(vm_table_struct);
737
738 vm_pgalloc = 0;
739 vm_pgrefill = 0;
740 vm_pgscan = 0;
741 vm_pgsteal = 0;
742
743 FILE_TO_BUF(VMINFO_FILE,vminfo_fd);
744
745 head = buf;
746 for(;;){
747 tail = strchr(head, ' ');
748 if(!tail) break;
749 *tail = '\0';
750 if(strlen(head) >= sizeof(namebuf)){
751 head = tail+1;
752 goto nextline;
753 }
754 strcpy(namebuf,head);
755 found = bsearch(&findme, vm_table, vm_table_count,
756 sizeof(vm_table_struct), compare_vm_table_structs
757 );
758 head = tail+1;
759 if(!found) goto nextline;
760 *(found->slot) = strtoul(head,&tail,10);
761 nextline:
762
763 //if(found) fprintf(stderr,"%s=%d\n",found->name,*(found->slot));
764 //else fprintf(stderr,"%s not found\n",findme.name);
765
766 tail = strchr(head, '\n');
767 if(!tail) break;
768 head = tail+1;
769 }
770 if(!vm_pgalloc)
771 vm_pgalloc = vm_pgalloc_dma + vm_pgalloc_high + vm_pgalloc_normal;
772 if(!vm_pgrefill)
773 vm_pgrefill = vm_pgrefill_dma + vm_pgrefill_high + vm_pgrefill_normal;
774 if(!vm_pgscan)
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;
777 if(!vm_pgsteal)
778 vm_pgsteal = vm_pgsteal_dma + vm_pgsteal_high + vm_pgsteal_normal;
779 }
780
781 ///////////////////////////////////////////////////////////////////////
782 // based on Fabian Frederick's /proc/diskstats parser
783
784
785 unsigned int getpartitions_num(struct disk_stat *disks, int ndisks){
786 int i=0;
787 int partitions=0;
788
789 for (i=0;i<ndisks;i++){
790 partitions+=disks[i].partitions;
791 }
792 return partitions;
793
794 }
795
796 /////////////////////////////////////////////////////////////////////////////
797
798 unsigned int getdiskstat(struct disk_stat **disks, struct partition_stat **partitions){
799 FILE* fd;
800 int cDisk = 0;
801 int cPartition = 0;
802 int fields;
803 unsigned dummy;
804
805 *disks = NULL;
806 *partitions = NULL;
807 buff[BUFFSIZE-1] = 0;
808 fd = fopen("/proc/diskstats", "rb");
809 if(!fd) crash("/proc/diskstats");
810
811 for (;;) {
812 if (!fgets(buff,BUFFSIZE-1,fd)){
813 fclose(fd);
814 break;
815 }
816 fields = sscanf(buff, " %*d %*d %*s %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u", &dummy);
817 if (fields == 1){
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",
820 //&disk_major,
821 //&disk_minor,
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
834 );
835 (*disks)[cDisk].partitions=0;
836 cDisk++;
837 }else{
838 (*partitions) = realloc(*partitions, (cPartition+1)*sizeof(struct partition_stat));
839 fflush(stdout);
840 sscanf(buff, " %*d %*d %15s %u %llu %u %u",
841 //&part_major,
842 //&part_minor,
843 (*partitions)[cPartition].partition_name,
844 &(*partitions)[cPartition].reads,
845 &(*partitions)[cPartition].reads_sectors,
846 &(*partitions)[cPartition].writes,
847 &(*partitions)[cPartition].requested_writes
848 );
849 (*partitions)[cPartition++].parent_disk = cDisk-1;
850 (*disks)[cDisk-1].partitions++;
851 }
852 }
853
854 return cDisk;
855 }
856
857 /////////////////////////////////////////////////////////////////////////////
858 // based on Fabian Frederick's /proc/slabinfo parser
859
860 unsigned int getslabinfo (struct slab_cache **slab){
861 FILE* fd;
862 int cSlab = 0;
863 buff[BUFFSIZE-1] = 0;
864 *slab = NULL;
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
872 (*slab)[cSlab].name,
873 &(*slab)[cSlab].active_objs,
874 &(*slab)[cSlab].num_objs,
875 &(*slab)[cSlab].objsize,
876 &(*slab)[cSlab].objperslab
877 ) ;
878 cSlab++;
879 }
880 fclose(fd);
881 return cSlab;
882 }
883
884 ///////////////////////////////////////////////////////////////////////////
885
886 unsigned get_pid_digits(void){
887 char pidbuf[24];
888 char *endp;
889 long rc;
890 int fd;
891 static unsigned ret;
892
893 if(ret) goto out;
894 ret = 5;
895 fd = open("/proc/sys/kernel/pid_max", O_RDONLY);
896 if(fd==-1) goto out;
897 rc = read(fd, pidbuf, sizeof pidbuf);
898 close(fd);
899 if(rc<3) goto out;
900 pidbuf[rc] = '\0';
901 rc = strtol(pidbuf,&endp,10);
902 if(rc<42) goto out;
903 if(*endp && *endp!='\n') goto out;
904 rc--; // the pid_max value is really the max PID plus 1
905 ret = 0;
906 while(rc){
907 rc /= 10;
908 ret++;
909 }
910 out:
911 return ret;
912 }