~ubuntu-branches/ubuntu/saucy/procps/saucy

« back to all changes in this revision

Viewing changes to .pc/vmstat_truncate/proc/sysinfo.c

  • Committer: Package Import Robot
  • Author(s): Oliver Grawert
  • Date: 2012-06-20 13:12:40 UTC
  • mfrom: (2.1.21 sid)
  • Revision ID: package-import@ubuntu.com-20120620131240-923p0d8q88bmk3ac
Tags: 1:3.3.3-2ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - debian/sysctl.d (Ubuntu-specific):
    + 10-console-messages.conf: stop low-level kernel messages on console.
    + 10-kernel-hardening.conf: add the kptr_restrict setting
    + 10-keyboard.conf.powerpc: mouse button emulation on PowerPC.
    + 10-network-security.conf: enable rp_filter and SYN-flood protection.
    + 10-ptrace.conf: describe new PTRACE setting.
    + 10-zeropage.conf: safe mmap_min_addr value for graceful fall-back.
    + README: describe how this directory is supposed to work.
  - debian/upstart (Ubuntu-specific): upstart configuration to replace old
    style sysv init script

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
unsigned long getbtime(void) {
94
 
    static unsigned long btime = 0;
95
 
    FILE *f;
96
 
 
97
 
    if (btime)
98
 
        return btime;
99
 
 
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);
104
 
        fflush(NULL);
105
 
        _exit(102);
106
 
    }
107
 
 
108
 
    while ((fgets(buf, sizeof buf, f))) {
109
 
        if (sscanf(buf, "btime %lu", &btime) == 1)
110
 
            break;
111
 
    }
112
 
    fclose(f);
113
 
 
114
 
    if (!btime) {
115
 
        fputs("missing btime in " STAT_FILE "\n", stderr);
116
 
        exit(1);
117
 
    }
118
 
 
119
 
    return btime;
120
 
}
121
 
 
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.
130
 
 *
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:
133
 
 *
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...
137
 
 *
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. :-)
141
 
 *
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.
144
 
 *
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.
151
 
 */
152
 
 
153
 
unsigned long long Hertz;
154
 
 
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;
159
 
  unsigned h;
160
 
  char *restrict savelocale;
161
 
 
162
 
  wait_j = hirq_j = sirq_j = stol_j = 0;
163
 
  savelocale = setlocale(LC_NUMERIC, NULL);
164
 
  setlocale(LC_NUMERIC, "C");
165
 
  do{
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 */
178
 
  switch(h){
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 */
195
 
  default:
196
 
#ifdef HZ
197
 
    Hertz = (unsigned long long)HZ;    /* <asm/param.h> */
198
 
#else
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;
201
 
#endif
202
 
    fprintf(stderr, "Unknown HZ value! (%d) Assume %Ld.\n", h, Hertz);
203
 
  }
204
 
}
205
 
 
206
 
// same as:   euid != uid || egid != gid
207
 
#ifndef AT_SECURE
208
 
#define AT_SECURE      23     // secure mode boolean (true if setuid, etc.)
209
 
#endif
210
 
 
211
 
#ifndef AT_CLKTCK
212
 
#define AT_CLKTCK       17    // frequency of times()
213
 
#endif
214
 
 
215
 
#define NOTE_NOT_FOUND 42
216
 
 
217
 
//extern char** environ;
218
 
 
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;
222
 
  while(*ep++);
223
 
  while(*ep){
224
 
    if(ep[0]==findme) return ep[1];
225
 
    ep+=2;
226
 
  }
227
 
  return NOTE_NOT_FOUND;
228
 
}
229
 
 
230
 
int have_privs;
231
 
 
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();
238
 
  }
239
 
  return !!rc;
240
 
}
241
 
 
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
248
 
  //
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 */
255
 
#ifdef __linux__
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);
260
 
  }
261
 
#endif
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
266
 
   */
267
 
    Hertz = 100;
268
 
    return;
269
 
#endif
270
 
  old_Hertz_hack();
271
 
}
272
 
 
273
 
#if 0
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.
279
 
 */
280
 
#ifndef NAN
281
 
#define NAN (-0.0)
282
 
#endif
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 :-( */
290
 
 
291
 
    tmp_w = 0.0;
292
 
    new_w = 0;
293
 
    tmp_x = 0.0;
294
 
    new_x = 0;
295
 
    tmp_y = 0.0;
296
 
    new_y = 0;
297
 
    tmp_z = 0.0;
298
 
    new_z = 0;
299
 
 
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);
303
 
    if(ticks_past){
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;
313
 
    }else{
314
 
      tmp_u = NAN;
315
 
      tmp_n = NAN;
316
 
      tmp_s = NAN;
317
 
      tmp_i = NAN;
318
 
      tmp_w = NAN;
319
 
      tmp_x = NAN;
320
 
      tmp_y = NAN;
321
 
      tmp_z = NAN;
322
 
    }
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);
331
 
    old_u=new_u;
332
 
    old_n=new_n;
333
 
    old_s=new_s;
334
 
    old_i=new_i;
335
 
    old_w=new_w;
336
 
    old_x=new_x;
337
 
    old_y=new_y;
338
 
    old_z=new_z;
339
 
}
340
 
#undef JT
341
 
#endif
342
 
 
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;
347
 
    
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);
353
 
        exit(1);
354
 
    }
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);
359
 
}
360
 
 
361
 
  static char buff[BUFFSIZE]; /* used in the procedures */
362
 
/***********************************************************************/
363
 
 
364
 
static void crash(const char *filename) {
365
 
    perror(filename);
366
 
    exit(EXIT_FAILURE);
367
 
}
368
 
 
369
 
/***********************************************************************/
370
 
 
371
 
static void getrunners(unsigned int *restrict running, unsigned int *restrict blocked) {
372
 
  struct direct *ent;
373
 
  DIR *proc;
374
 
 
375
 
  *running=0;
376
 
  *blocked=0;
377
 
 
378
 
  if((proc=opendir("/proc"))==NULL) crash("/proc");
379
 
 
380
 
  while(( ent=readdir(proc) )) {
381
 
    char tbuf[32];
382
 
    char *cp;
383
 
    int fd;
384
 
    char c;
385
 
 
386
 
    if (!isdigit(ent->d_name[0])) continue;
387
 
    sprintf(tbuf, "/proc/%s/stat", ent->d_name);
388
 
 
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
393
 
    close(fd);
394
 
 
395
 
    cp = strrchr(tbuf, ')');
396
 
    if(!cp) continue;
397
 
    c = cp[2];
398
 
 
399
 
    if (c=='R') {
400
 
      (*running)++;
401
 
      continue;
402
 
    }
403
 
    if (c=='D') {
404
 
      (*blocked)++;
405
 
      continue;
406
 
    }
407
 
  }
408
 
  closedir(proc);
409
 
}
410
 
 
411
 
/***********************************************************************/
412
 
 
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) {
418
 
  static int fd;
419
 
  unsigned long long llbuf = 0;
420
 
  int need_vmstat_file = 0;
421
 
  int need_proc_scan = 0;
422
 
  const char* b;
423
 
  buff[BUFFSIZE-1] = 0;  /* ensure null termination in buffer */
424
 
 
425
 
  if(fd){
426
 
    lseek(fd, 0L, SEEK_SET);
427
 
  }else{
428
 
    fd = open("/proc/stat", O_RDONLY, 0);
429
 
    if(fd == -1) crash("/proc/stat");
430
 
  }
431
 
  read(fd,buff,BUFFSIZE-1);
432
 
  *intr = 0; 
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 */
437
 
 
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);
440
 
 
441
 
  b = strstr(buff, "page ");
442
 
  if(b) sscanf(b,  "page %lu %lu", pin, pout);
443
 
  else need_vmstat_file = 1;
444
 
 
445
 
  b = strstr(buff, "swap ");
446
 
  if(b) sscanf(b,  "swap %lu %lu", s_in, sout);
447
 
  else need_vmstat_file = 1;
448
 
 
449
 
  b = strstr(buff, "intr ");
450
 
  if(b) sscanf(b,  "intr %Lu", &llbuf);
451
 
  *intr = llbuf;
452
 
 
453
 
  b = strstr(buff, "ctxt ");
454
 
  if(b) sscanf(b,  "ctxt %Lu", &llbuf);
455
 
  *ctxt = llbuf;
456
 
 
457
 
  b = strstr(buff, "btime ");
458
 
  if(b) sscanf(b,  "btime %u", btime);
459
 
 
460
 
  b = strstr(buff, "processes ");
461
 
  if(b) sscanf(b,  "processes %u", processes);
462
 
 
463
 
  b = strstr(buff, "procs_running ");
464
 
  if(b) sscanf(b,  "procs_running %u", running);
465
 
  else need_proc_scan = 1;
466
 
 
467
 
  b = strstr(buff, "procs_blocked ");
468
 
  if(b) sscanf(b,  "procs_blocked %u", blocked);
469
 
  else need_proc_scan = 1;
470
 
 
471
 
  if(need_proc_scan){   /* Linux 2.5.46 (approximately) and below */
472
 
    getrunners(running, blocked);
473
 
  }
474
 
 
475
 
  (*running)--;   // exclude vmstat itself
476
 
 
477
 
  if(need_vmstat_file){  /* Linux 2.5.40-bk4 and above */
478
 
    vminfo();
479
 
    *pin  = vm_pgpgin;
480
 
    *pout = vm_pgpgout;
481
 
    *s_in = vm_pswpin;
482
 
    *sout = vm_pswpout;
483
 
  }
484
 
}
485
 
 
486
 
/***********************************************************************/
487
 
/*
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.
496
 
 */
497
 
 
498
 
typedef struct mem_table_struct {
499
 
  const char *name;     /* memory type name */
500
 
  unsigned long *slot; /* slot in return struct */
501
 
} mem_table_struct;
502
 
 
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);
505
 
}
506
 
 
507
 
/* example data, following junk, with comments added:
508
 
 *
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
519
 
 * HighTotal:           0 kB
520
 
 * HighFree:            0 kB
521
 
 * LowTotal:        61768 kB
522
 
 * LowFree:          1436 kB
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.??+
537
 
 */
538
 
 
539
 
/* obsolete */
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;
553
 
/* 2.4.xx era */
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 */
560
 
/* derived values */
561
 
unsigned long kb_swap_used;
562
 
unsigned long kb_main_used;
563
 
/* 2.5.41+ */
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;
583
 
 
584
 
void meminfo(void){
585
 
  char namebuf[16]; /* big enough to hold any row name */
586
 
  mem_table_struct findme = { namebuf, NULL};
587
 
  mem_table_struct *found;
588
 
  char *head;
589
 
  char *tail;
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
625
 
  };
626
 
  const int mem_table_count = sizeof(mem_table)/sizeof(mem_table_struct);
627
 
 
628
 
  FILE_TO_BUF(MEMINFO_FILE,meminfo_fd);
629
 
 
630
 
  kb_inactive = ~0UL;
631
 
 
632
 
  head = buf;
633
 
  for(;;){
634
 
    tail = strchr(head, ':');
635
 
    if(!tail) break;
636
 
    *tail = '\0';
637
 
    if(strlen(head) >= sizeof(namebuf)){
638
 
      head = tail+1;
639
 
      goto nextline;
640
 
    }
641
 
    strcpy(namebuf,head);
642
 
    found = bsearch(&findme, mem_table, mem_table_count,
643
 
        sizeof(mem_table_struct), compare_mem_table_structs
644
 
    );
645
 
    head = tail+1;
646
 
    if(!found) goto nextline;
647
 
    *(found->slot) = (unsigned long)strtoull(head,&tail,10);
648
 
nextline:
649
 
    tail = strchr(head, '\n');
650
 
    if(!tail) break;
651
 
    head = tail+1;
652
 
  }
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;
656
 
  }
657
 
  if(kb_inactive==~0UL){
658
 
    kb_inactive = kb_inact_dirty + kb_inact_clean + kb_inact_laundry;
659
 
  }
660
 
  kb_swap_used = kb_swap_total - kb_swap_free;
661
 
  kb_main_used = kb_main_total - kb_main_free;
662
 
}
663
 
 
664
 
/*****************************************************************/
665
 
 
666
 
/* read /proc/vminfo only for 2.5.41 and above */
667
 
 
668
 
typedef struct vm_table_struct {
669
 
  const char *name;     /* VM statistic name */
670
 
  unsigned long *slot;       /* slot in return struct */
671
 
} vm_table_struct;
672
 
 
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);
675
 
}
676
 
 
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;        //
724
 
 
725
 
void vminfo(void){
726
 
  char namebuf[16]; /* big enough to hold any row name */
727
 
  vm_table_struct findme = { namebuf, NULL};
728
 
  vm_table_struct *found;
729
 
  char *head;
730
 
  char *tail;
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},
775
 
  };
776
 
  const int vm_table_count = sizeof(vm_table)/sizeof(vm_table_struct);
777
 
 
778
 
  vm_pgalloc = 0;
779
 
  vm_pgrefill = 0;
780
 
  vm_pgscan = 0;
781
 
  vm_pgsteal = 0;
782
 
 
783
 
  FILE_TO_BUF(VMINFO_FILE,vminfo_fd);
784
 
 
785
 
  head = buf;
786
 
  for(;;){
787
 
    tail = strchr(head, ' ');
788
 
    if(!tail) break;
789
 
    *tail = '\0';
790
 
    if(strlen(head) >= sizeof(namebuf)){
791
 
      head = tail+1;
792
 
      goto nextline;
793
 
    }
794
 
    strcpy(namebuf,head);
795
 
    found = bsearch(&findme, vm_table, vm_table_count,
796
 
        sizeof(vm_table_struct), compare_vm_table_structs
797
 
    );
798
 
    head = tail+1;
799
 
    if(!found) goto nextline;
800
 
    *(found->slot) = strtoul(head,&tail,10);
801
 
nextline:
802
 
 
803
 
//if(found) fprintf(stderr,"%s=%d\n",found->name,*(found->slot));
804
 
//else      fprintf(stderr,"%s not found\n",findme.name);
805
 
 
806
 
    tail = strchr(head, '\n');
807
 
    if(!tail) break;
808
 
    head = tail+1;
809
 
  }
810
 
  if(!vm_pgalloc)
811
 
    vm_pgalloc  = vm_pgalloc_dma + vm_pgalloc_high + vm_pgalloc_normal;
812
 
  if(!vm_pgrefill)
813
 
    vm_pgrefill = vm_pgrefill_dma + vm_pgrefill_high + vm_pgrefill_normal;
814
 
  if(!vm_pgscan)
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;
817
 
  if(!vm_pgsteal)
818
 
    vm_pgsteal  = vm_pgsteal_dma + vm_pgsteal_high + vm_pgsteal_normal;
819
 
}
820
 
 
821
 
///////////////////////////////////////////////////////////////////////
822
 
// based on Fabian Frederick's /proc/diskstats parser
823
 
 
824
 
 
825
 
unsigned int getpartitions_num(struct disk_stat *disks, int ndisks){
826
 
  int i=0;
827
 
  int partitions=0;
828
 
 
829
 
  for (i=0;i<ndisks;i++){
830
 
        partitions+=disks[i].partitions;
831
 
  }
832
 
  return partitions;
833
 
 
834
 
}
835
 
 
836
 
/////////////////////////////////////////////////////////////////////////////
837
 
static int is_disk(char *dev)
838
 
{
839
 
  char syspath[32];
840
 
  char *slash;
841
 
 
842
 
  while ((slash = strchr(dev, '/')))
843
 
    *slash = '!';
844
 
  snprintf(syspath, sizeof(syspath), "/sys/block/%s", dev);
845
 
  return !(access(syspath, F_OK));
846
 
}
847
 
 
848
 
/////////////////////////////////////////////////////////////////////////////
849
 
 
850
 
unsigned int getdiskstat(struct disk_stat **disks, struct partition_stat **partitions){
851
 
  FILE* fd;
852
 
  int cDisk = 0;
853
 
  int cPartition = 0;
854
 
  int fields;
855
 
  unsigned dummy;
856
 
  char devname[32];
857
 
 
858
 
  *disks = NULL;
859
 
  *partitions = NULL;
860
 
  buff[BUFFSIZE-1] = 0; 
861
 
  fd = fopen("/proc/diskstats", "rb");
862
 
  if(!fd) crash("/proc/diskstats");
863
 
 
864
 
  for (;;) {
865
 
    if (!fgets(buff,BUFFSIZE-1,fd)){
866
 
      fclose(fd);
867
 
      break;
868
 
    }
869
 
    fields = sscanf(buff, " %*d %*d %15s %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u",
870
 
            &devname, &dummy);
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",
874
 
        //&disk_major,
875
 
        //&disk_minor,
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
888
 
      );
889
 
        (*disks)[cDisk].partitions=0;
890
 
      cDisk++;
891
 
    }else{
892
 
      (*partitions) = realloc(*partitions, (cPartition+1)*sizeof(struct partition_stat));
893
 
      fflush(stdout);
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",
897
 
        //&part_major,
898
 
        //&part_minor,
899
 
        (*partitions)[cPartition].partition_name,
900
 
        &(*partitions)[cPartition].reads,
901
 
        &(*partitions)[cPartition].reads_sectors,
902
 
        &(*partitions)[cPartition].writes,
903
 
        &(*partitions)[cPartition].requested_writes
904
 
      );
905
 
      (*partitions)[cPartition++].parent_disk = cDisk-1;
906
 
      (*disks)[cDisk-1].partitions++;   
907
 
    }
908
 
  }
909
 
 
910
 
  return cDisk;
911
 
}
912
 
 
913
 
/////////////////////////////////////////////////////////////////////////////
914
 
// based on Fabian Frederick's /proc/slabinfo parser
915
 
 
916
 
unsigned int getslabinfo (struct slab_cache **slab){
917
 
  FILE* fd;
918
 
  int cSlab = 0;
919
 
  buff[BUFFSIZE-1] = 0; 
920
 
  *slab = NULL;
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
928
 
      (*slab)[cSlab].name,
929
 
      &(*slab)[cSlab].active_objs,
930
 
      &(*slab)[cSlab].num_objs,
931
 
      &(*slab)[cSlab].objsize,
932
 
      &(*slab)[cSlab].objperslab
933
 
    ) ;
934
 
    cSlab++;
935
 
  }
936
 
  fclose(fd);
937
 
  return cSlab;
938
 
}
939
 
 
940
 
///////////////////////////////////////////////////////////////////////////
941
 
 
942
 
unsigned get_pid_digits(void){
943
 
  char pidbuf[24];
944
 
  char *endp;
945
 
  long rc;
946
 
  int fd;
947
 
  static unsigned ret;
948
 
 
949
 
  if(ret) goto out;
950
 
  ret = 5;
951
 
  fd = open("/proc/sys/kernel/pid_max", O_RDONLY);
952
 
  if(fd==-1) goto out;
953
 
  rc = read(fd, pidbuf, sizeof pidbuf);
954
 
  close(fd);
955
 
  if(rc<3) goto out;
956
 
  pidbuf[rc] = '\0';
957
 
  rc = strtol(pidbuf,&endp,10);
958
 
  if(rc<42) goto out;
959
 
  if(*endp && *endp!='\n') goto out;
960
 
  rc--;  // the pid_max value is really the max PID plus 1
961
 
  ret = 0;
962
 
  while(rc){
963
 
    rc /= 10;
964
 
    ret++;
965
 
  }
966
 
out:
967
 
  return ret;
968
 
}