annotate toys/other/vmstat.c @ 1211:40e0f7b09b77 draft

Fix two bugs reported by Ashwini Sharma.
author Rob Landley <rob@landley.net>
date Fri, 28 Feb 2014 23:04:57 -0600
parents a7df29f72bc6
children 6eee35009294
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
1 /* vmstat.c - Report virtual memory statistics.
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
2 *
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
3 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
4
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
5 USE_VMSTAT(NEWTOY(vmstat, ">2n", TOYFLAG_BIN))
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
6
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
7 config VMSTAT
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
8 bool "vmstat"
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
9 default y
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
10 help
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
11 usage: vmstat [-n] [DELAY [COUNT]]
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
12
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
13 Print virtual memory statistics, repeating each DELAY seconds, COUNT times.
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
14 (With no DELAY, prints one line. With no COUNT, repeats until killed.)
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
15
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
16 Show processes running and blocked, kilobytes swapped, free, buffered, and
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
17 cached, kilobytes swapped in and out per second, file disk blocks input and
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
18 output per second, interrupts and context switches per second, percent
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
19 of CPU time spent running user code, system code, idle, and awaiting I/O.
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
20 First line is since system started, later lines are since last line.
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
21
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
22 -n Display the header only once
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
23 */
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
24
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
25 #define FOR_vmstat
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
26 #include "toys.h"
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
27
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
28 struct vmstat_proc {
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
29 // From /proc/stat (jiffies)
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
30 uint64_t user, nice, sys, idle, wait, irq, sirq, intr, ctxt, running, blocked;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
31 // From /proc/meminfo (units are kb)
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
32 uint64_t memfree, buffers, cached, swapfree, swaptotal;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
33 // From /proc/vmstat (units are pages)
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
34 uint64_t io_in, io_out, swap_in, swap_out;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
35 };
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
36
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
37 // All the elements of vmstat_proc are the same size, so we can populate it as
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
38 // a big array, then read the elements back out by name
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
39 void get_vmstat_proc(struct vmstat_proc *vmstat_proc)
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
40 {
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
41 char *vmstuff[] = { "/proc/stat", "cpu ", 0, 0, 0, 0, 0, 0,
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
42 "intr ", "ctxt ", "procs_running ", "procs_blocked ", "/proc/meminfo",
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
43 "MemFree: ", "Buffers: ", "Cached: ", "SwapFree: ", "SwapTotal: ",
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
44 "/proc/vmstat", "pgpgin ", "pgpgout ", "pswpin ", "pswpout " };
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
45 uint64_t *new = (uint64_t *)vmstat_proc;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
46 char *p = p, *name = name;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
47 int i, j;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
48
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
49 // We use vmstuff to fill out vmstat_proc as an array of uint64_t:
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
50 // Strings starting with / are the file to find next entries in
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
51 // Any other string is a key to search for, with decimal value right after
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
52 // 0 means parse another value on same line as last key
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
53
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
54 for (i = 0; i<sizeof(vmstuff)/sizeof(char *); i++) {
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
55 if (!vmstuff[i]) p++;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
56 else if (*vmstuff[i] == '/') {
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
57 xreadfile(name = vmstuff[i], toybuf, sizeof(toybuf));
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
58
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
59 continue;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
60 } else {
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
61 if (!(p = strstr(toybuf, vmstuff[i]))) goto error;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
62 p += strlen(vmstuff[i]);
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
63 }
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
64 if (1 != sscanf(p, "%"PRIu64"%n", new++, &j)) goto error;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
65 p += j;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
66 }
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
67
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
68 return;
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
69
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
70 error:
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
71 error_exit("No %sin %s\n", vmstuff[i], name);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
72 }
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
73
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
74 void vmstat_main(void)
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
75 {
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
76 struct vmstat_proc top[2];
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
77 int i, loop_delay = 0, loop_max = 0;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
78 unsigned loop, rows = (toys.optflags & FLAG_n) ? 0 : 25,
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
79 page_kb = sysconf(_SC_PAGESIZE)/1024;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
80 char *headers="r\0b\0swpd\0free\0buff\0cache\0si\0so\0bi\0bo\0in\0cs\0us\0"
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
81 "sy\0id\0wa", lengths[] = {2,2,6,6,6,6,4,4,5,5,4,4,2,2,2,2};
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
82
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
83 memset(top, 0, sizeof(top));
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
84 if (toys.optc) loop_delay = atolx_range(toys.optargs[0], 0, INT_MAX);
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
85 if (toys.optc > 1) loop_max = atolx_range(toys.optargs[1], 1, INT_MAX) - 1;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
86
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
87 for (loop = 0; loop <= loop_max; loop++) {
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
88 unsigned idx = loop&1, offset = 0, expected = 0;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
89 uint64_t units, total_hz, *ptr = (uint64_t *)(top+idx),
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
90 *oldptr = (uint64_t *)(top+!idx);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
91
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
92 // Print headers
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
93 if (rows>3 && !(loop % (rows-3))) {
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
94 if (isatty(1)) terminal_size(0, &rows);
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
95 else rows = 0;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
96
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
97 printf("procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----\n");
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
98
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
99 for (i=0; i<sizeof(lengths); i++) {
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
100 printf(" %*s"+!i, lengths[i], headers);
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
101 headers += strlen(headers)+1;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
102 }
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
103 xputc('\n');
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
104 }
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
105
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
106 // Read data and combine some fields we display as aggregates
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
107 get_vmstat_proc(top+idx);
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
108 top[idx].running--; // Don't include ourselves
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
109 top[idx].user += top[idx].nice;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
110 top[idx].sys += top[idx].irq + top[idx].sirq;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
111 top[idx].swaptotal -= top[idx].swapfree;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
112
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
113 // Collect unit adjustments (outside the inner loop to save time)
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
114
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
115 if (!loop) {
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
116 char *s = toybuf;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
117
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
118 xreadfile("/proc/uptime", toybuf, sizeof(toybuf)-1);
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
119 while (*(s++) > ' ');
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
120 sscanf(s, "%"PRIu64, &units);
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
121 } else units = loop_delay;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
122
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
123 // add up user, sys, idle, and wait time used since last time
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
124 // (Already appended nice to user)
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
125 total_hz = 0;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
126 for (i=0; i<4; i++) total_hz += ptr[i+!!i] - oldptr[i+!!i];
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
127
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
128 // Output values in order[]: running, blocked, swaptotal, memfree, buffers,
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
129 // cache, swap_in, swap_out, io_in, io_out, sirq, ctxt, user, sys, idle,wait
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
130
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
131 for (i=0; i<sizeof(lengths); i++) {
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
132 char order[] = {9, 10, 15, 11, 12, 13, 18, 19, 16, 17, 6, 8, 0, 2, 3, 4};
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
133 uint64_t out = ptr[order[i]];
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
134 int len;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
135
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
136 // Adjust rate and units
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
137 if (i>5) out -= oldptr[order[i]];
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
138 if (order[i]<7) out = ((out*100) + (total_hz/2)) / total_hz;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
139 else if (order[i]>15) out = ((out * page_kb)+(units-1))/units;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
140 else if (order[i]<9) out = (out+(units-1)) / units;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
141
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
142 // If a field was too big to fit in its slot, try to compensate later
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
143 expected += lengths[i] + !!i;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
144 len = expected - offset - !!i;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
145 if (len < 0) len = 0;
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
146 offset += printf(" %*"PRIu64+!i, len, out);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
147 }
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
148 xputc('\n');
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
149
1171
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
150 if (loop_delay) sleep(loop_delay);
a7df29f72bc6 Cleanup vmstat. Procs, memory, and cpu are reading right, the others not so much.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
151 else break;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
152 }
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
153 }