annotate toys/vmstat.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents c97e338a4126
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
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 * vmstat.c - Report virtual memory statistics.
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 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
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 * Not in SUSv4.
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
8
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
9 USE_VMSTAT(NEWTOY(vmstat, ">2n", TOYFLAG_BIN))
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
10
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
11 config VMSTAT
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
12 bool "vmstat"
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
13 default y
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
14 help
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
15 usage: vmstat [-n] [delay [count]]
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
16 -n Display the header only once
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
17 delay The delay between updates in seconds, when not specified
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
18 the average since boot is displayed.
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
19 count Number of updates to display, the default is inifinite.
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
20 */
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
21
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
22 #include "toys.h"
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
23
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
24 void read_proc_stat(unsigned int * proc_running, unsigned int * proc_blocked,
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
25 uint64_t * sys_irq, uint64_t * sys_ctxt,
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
26 uint64_t * cpu_user, uint64_t * cpu_sys, uint64_t * cpu_idle, uint64_t * cpu_wait)
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
27 {
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
28 char * off;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
29 uint64_t c_user, c_nice, c_sys, c_irq, c_sirq;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
30 int fd = xopen("/proc/stat", O_RDONLY);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
31 size_t s = xread(fd, toybuf, sizeof(toybuf)-1);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
32 toybuf[s] = 0;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
33 if ( s == sizeof(toybuf)-1)
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
34 error_exit("/proc/stat is too large");
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
35
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
36 off = strstr(toybuf, "cpu ");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
37 // Ignoring steal and guest fields for now.
562
4d802d438983 Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
Rob Landley <rob@landley.net>
parents: 561
diff changeset
38 if (off) sscanf(off, "cpu %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64 \
4d802d438983 Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
Rob Landley <rob@landley.net>
parents: 561
diff changeset
39 " %"PRIu64" %"PRIu64" %"PRIu64, &c_user, &c_nice, &c_sys, cpu_idle,
4d802d438983 Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
Rob Landley <rob@landley.net>
parents: 561
diff changeset
40 cpu_wait, &c_irq, &c_sirq);
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
41 *cpu_user = c_user + c_nice;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
42 *cpu_sys = c_sys + c_irq + c_sirq;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
43 off = strstr(toybuf, "intr");
562
4d802d438983 Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
Rob Landley <rob@landley.net>
parents: 561
diff changeset
44 if (off) sscanf(off, "intr %"PRIu64, sys_irq);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
45
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
46 off = strstr(toybuf, "ctxt");
562
4d802d438983 Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
Rob Landley <rob@landley.net>
parents: 561
diff changeset
47 if (off) sscanf(off, "ctxt %"PRIu64, sys_ctxt);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
48
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
49 off = strstr(toybuf, "procs_running");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
50 if (off) sscanf(off, "procs_running %u", proc_running);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
51 (*proc_running)--; // look, i'm invisible.
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
52
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
53 off = strstr(toybuf, "procs_blocked");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
54 if (off) sscanf(off, "procs_blocked %u", proc_blocked);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
55
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
56 close(fd);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
57 }
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
58
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
59 void read_proc_meminfo(unsigned long * mem_swapped, unsigned long * mem_free,
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
60 unsigned long * mem_buff, unsigned long * mem_cache)
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
61 {
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
62 char * off;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
63 unsigned long swap_total, swap_free;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
64 int fd = xopen("/proc/meminfo", O_RDONLY);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
65 size_t s = xread(fd, toybuf, sizeof(toybuf)-1);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
66 toybuf[s] = 0;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
67 if ( s == sizeof(toybuf)-1)
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
68 error_exit("/proc/meminfo is too large");
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
69
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
70 off = strstr(toybuf, "MemFree");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
71 if (off) sscanf(off, "MemFree: %lu kB", mem_free);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
72
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
73 off = strstr(toybuf, "Buffers");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
74 if (off) sscanf(off, "Buffers: %lu kB", mem_buff);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
75
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
76 off = strstr(toybuf, "Cached");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
77 if (off) sscanf(off, "Cached: %lu kB", mem_cache);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
78
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
79 off = strstr(toybuf, "SwapFree");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
80 if (off) sscanf(off, "SwapFree: %lu kB", &swap_free);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
81
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
82 off = strstr(toybuf, "SwapTotal");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
83 if (off) sscanf(off, "SwapTotal: %lu kB", &swap_total);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
84 *mem_swapped = swap_total - swap_free;
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
85
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
86 close(fd);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
87 }
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
88
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
89 void read_proc_vmstat(unsigned long * io_pages_in, unsigned long * io_pages_out,
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
90 unsigned long * swap_bytes_in, unsigned long * swap_bytes_out)
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
91 {
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
92 char * off;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
93 unsigned long s_pages_in, s_pages_out;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
94 unsigned long pagesize_kb = sysconf(_SC_PAGESIZE) / 1024L;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
95 int fd = xopen("/proc/vmstat", O_RDONLY);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
96 size_t s = xread(fd, toybuf, sizeof(toybuf)-1);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
97 toybuf[s] = 0;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
98 if ( s == sizeof(toybuf)-1)
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
99 error_exit("/proc/vmstat is too large");
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
100
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
101 off = strstr(toybuf, "pgpgin");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
102 if (off) sscanf(off, "pgpgin %lu", io_pages_in);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
103
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
104 off = strstr(toybuf, "pgpgout");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
105 if (off) sscanf(off, "pgpgout %lu", io_pages_out);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
106
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
107 off = strstr(toybuf, "pswpin");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
108 if (off) sscanf(off, "pswpin %lu", &s_pages_in);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
109 *swap_bytes_in = s_pages_in * pagesize_kb;
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
110
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
111 off = strstr(toybuf, "pswpout");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
112 if (off) sscanf(off, "pswpout %lu", &s_pages_out);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
113 *swap_bytes_out = s_pages_out * pagesize_kb;
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
114
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
115 close(fd);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
116 }
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
117
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
118 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
119 {
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
120 const char fmt[] = "%2u %2u %6lu %6lu %6lu %6lu %4u %4u %5u %5u %4u %4u %2u %2u %2u %2u\n";
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
121 unsigned int loop_num = 0, loop_max_num = 0, loop_delay = 0;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
122 unsigned int running = 0, blocked = 0;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
123 unsigned long mem_swap = 0, mem_free = 0, mem_buff = 0, mem_cache = 0;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
124 unsigned long io_pages_in[2], io_pages_out[2], swap_bytes_in[2], swap_bytes_out[2];
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
125 uint64_t sys_irq[2], sys_ctxt[2], cpu_user[2], cpu_sys[2], cpu_idle[2], cpu_wait[2];
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
126 int first_run = 1;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
127 int no_header = toys.optflags & 0x1;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
128 unsigned num_rows = 22;
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
129
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
130 if (toys.optc >= 1)
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
131 loop_delay = atoi(toys.optargs[0]);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
132 if (toys.optc >= 2)
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
133 loop_max_num = atoi(toys.optargs[1]);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
134
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
135 if (loop_max_num < 0 || loop_delay < 0)
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
136 error_exit("Invalid arguments");
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
137
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
138 while(1) {
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
139 uint64_t total_jif;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
140 int idx = loop_num%2;
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
141
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
142 if(first_run || (!(loop_num % num_rows) && !no_header)) {
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
143 unsigned rows = 0, cols = 0;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
144 terminal_size(&cols, &rows);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
145 num_rows = (rows > 3)? rows - 3 : 22;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
146 printf("procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----\n");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
147 printf(" r b swpd free buff cache si so bi bo in cs us sy id wa\n");
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
148 }
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
149
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
150 read_proc_stat(&running, &blocked, &sys_irq[idx], &sys_ctxt[idx], &cpu_user[idx],
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
151 &cpu_sys[idx], &cpu_idle[idx], &cpu_wait[idx]);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
152 read_proc_meminfo(&mem_swap, &mem_free, &mem_buff, &mem_cache);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
153 read_proc_vmstat(&io_pages_in[idx], &io_pages_out[idx], &swap_bytes_in[idx], &swap_bytes_out[idx]);
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
154
561
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
155 if (first_run) {
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
156 struct sysinfo inf;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
157 sysinfo(&inf);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
158 first_run = 0;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
159 total_jif = cpu_user[idx] + cpu_idle[idx] + cpu_wait[idx];
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
160 printf(fmt, running, blocked, mem_swap, mem_free, mem_buff, mem_cache,
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
161 (unsigned) (swap_bytes_in[idx]/inf.uptime),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
162 (unsigned) (swap_bytes_out[idx]/inf.uptime),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
163 (unsigned) (io_pages_in[idx]/inf.uptime),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
164 (unsigned) (io_pages_out[idx]/inf.uptime),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
165 (unsigned) (sys_irq[idx]/inf.uptime),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
166 (unsigned) (sys_ctxt[idx]/inf.uptime),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
167 (unsigned) (100*cpu_user[idx]/total_jif),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
168 (unsigned) (100*cpu_sys[idx]/total_jif),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
169 (unsigned) (100*cpu_idle[idx]/total_jif),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
170 (unsigned) (100*cpu_wait[idx]/total_jif));
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
171 }else{
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
172 total_jif = cpu_user[idx] - cpu_user[!idx] + cpu_idle[idx] - cpu_idle[!idx] + cpu_wait[idx] - cpu_wait[!idx];
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
173 printf(fmt, running, blocked, mem_swap, mem_free, mem_buff, mem_cache,
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
174 (unsigned) ((swap_bytes_in[idx] - swap_bytes_in[!idx])/loop_delay),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
175 (unsigned) ((swap_bytes_out[idx] - swap_bytes_out[!idx])/loop_delay),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
176 (unsigned) ((io_pages_in[idx] - io_pages_in[!idx])/loop_delay),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
177 (unsigned) ((io_pages_out[idx] - io_pages_out[!idx])/loop_delay),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
178 (unsigned) ((sys_irq[idx] - sys_irq[!idx])/loop_delay),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
179 (unsigned) ((sys_ctxt[idx] - sys_ctxt[!idx])/loop_delay),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
180 (unsigned) (100*(cpu_user[idx] - cpu_user[!idx])/total_jif),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
181 (unsigned) (100*(cpu_sys[idx] - cpu_sys[!idx]) /total_jif),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
182 (unsigned) (100*(cpu_idle[idx] - cpu_idle[!idx])/total_jif),
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
183 (unsigned) (100*(cpu_wait[idx] - cpu_wait[!idx])/total_jif));
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
184 }
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
185
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
186 loop_num++;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
187 if (loop_delay == 0 || (loop_max_num != 0 && loop_num >= loop_max_num))
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
188 break;
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
189 sleep(loop_delay);
c97e338a4126 toybuf usage enhancement, tabs to spaces, teminal height refresh
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 559
diff changeset
190 }
559
120b3dde7cf4 Adding vmstat
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
191 }