blob: 68e6f3325ee44fca56b69597244f53437343d732 [file] [log] [blame]
Denis Vlasenko1db39b22006-10-11 20:59:02 +00001/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02002 * Licensed under GPLv2, see file LICENSE in this source tree.
3 *
4 * Based on nanotop.c from floppyfw project
5 *
Denys Vlasenkobdaea462010-08-16 20:42:14 +02006 * Contact me: vda.linux@googlemail.com
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 */
Denys Vlasenkoa6041862010-12-13 15:17:22 +01008//config:config NMETER
Denys Vlasenkob097a842018-12-28 03:20:17 +01009//config: bool "nmeter (11 kb)"
Denys Vlasenkoa6041862010-12-13 15:17:22 +010010//config: default y
11//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020012//config: Prints selected system stats continuously, one line per update.
Denys Vlasenkoa6041862010-12-13 15:17:22 +010013
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +010014//applet:IF_NMETER(APPLET(nmeter, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenkoa6041862010-12-13 15:17:22 +010015
16//kbuild:lib-$(CONFIG_NMETER) += nmeter.o
17
18//usage:#define nmeter_trivial_usage
19//usage: "[-d MSEC] FORMAT_STRING"
20//usage:#define nmeter_full_usage "\n\n"
21//usage: "Monitor system in real time"
22//usage: "\n"
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +010023//usage: "\n -d MSEC Milliseconds between updates, default:1000, none:-1"
Denys Vlasenkoa6041862010-12-13 15:17:22 +010024//usage: "\n"
25//usage: "\nFormat specifiers:"
Denys Vlasenkof3d705f2017-01-21 03:46:35 +010026//usage: "\n %Nc or %[cN] CPU. N - bar size (default 10)"
Denys Vlasenkoa6041862010-12-13 15:17:22 +010027//usage: "\n (displays: S:system U:user N:niced D:iowait I:irq i:softirq)"
Denys Vlasenkoabb09522011-05-26 09:00:23 +020028//usage: "\n %[nINTERFACE] Network INTERFACE"
29//usage: "\n %m Allocated memory"
Denys Vlasenko524fecf2022-01-11 19:32:16 +010030//usage: "\n %[md] Dirty file-backed memory"
31//usage: "\n %[mw] Memory being written to storage"
Denys Vlasenkoabb09522011-05-26 09:00:23 +020032//usage: "\n %[mf] Free memory"
33//usage: "\n %[mt] Total memory"
34//usage: "\n %s Allocated swap"
35//usage: "\n %f Number of used file descriptors"
36//usage: "\n %Ni Total/specific IRQ rate"
37//usage: "\n %x Context switch rate"
38//usage: "\n %p Forks"
39//usage: "\n %[pn] # of processes"
40//usage: "\n %b Block io"
41//usage: "\n %Nt Time (with N decimal points)"
Denys Vlasenko4789c7c2020-06-10 15:37:39 +020042//usage: "\n %NT Zero-based timestamp (with N decimal points)"
Denys Vlasenkoa6041862010-12-13 15:17:22 +010043//usage: "\n %r Print <cr> instead of <lf> at EOL"
44
Denis Vlasenko1db39b22006-10-11 20:59:02 +000045//TODO:
46// simplify code
47// /proc/locks
48// /proc/stat:
49// disk_io: (3,0):(22272,17897,410702,4375,54750)
50// btime 1059401962
Denis Vlasenkoa6163ca2007-06-17 00:35:15 +000051//TODO: use sysinfo libc call/syscall, if appropriate
52// (faster than open/read/close):
53// sysinfo({uptime=15017, loads=[5728, 15040, 16480]
54// totalram=2107416576, freeram=211525632, sharedram=0, bufferram=157204480}
55// totalswap=134209536, freeswap=134209536, procs=157})
Denis Vlasenko1db39b22006-10-11 20:59:02 +000056
Denis Vlasenkoa6163ca2007-06-17 00:35:15 +000057#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020058#include "common_bufsiz.h"
Denis Vlasenko1db39b22006-10-11 20:59:02 +000059
60typedef unsigned long long ullong;
Denis Vlasenko1db39b22006-10-11 20:59:02 +000061
Denis Vlasenkobfc93a32008-09-26 11:12:09 +000062enum { /* Preferably use powers of 2 */
63 PROC_MIN_FILE_SIZE = 256,
64 PROC_MAX_FILE_SIZE = 16 * 1024,
65};
Denis Vlasenko1db39b22006-10-11 20:59:02 +000066
67typedef struct proc_file {
Denis Vlasenko1db39b22006-10-11 20:59:02 +000068 char *file;
Denis Vlasenkobfc93a32008-09-26 11:12:09 +000069 int file_sz;
Denis Vlasenko199c0d52007-05-30 14:48:38 +000070 smallint last_gen;
Denis Vlasenko1db39b22006-10-11 20:59:02 +000071} proc_file;
72
Denys Vlasenko987be932022-02-06 20:07:12 +010073static const char *const proc_name[] ALIGN_PTR = {
Denis Vlasenko199c0d52007-05-30 14:48:38 +000074 "stat", // Must match the order of proc_file's!
75 "loadavg",
76 "net/dev",
77 "meminfo",
78 "diskstats",
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000079 "sys/fs/file-nr"
Denis Vlasenko199c0d52007-05-30 14:48:38 +000080};
81
82struct globals {
83 // Sample generation flip-flop
84 smallint gen;
85 // Linux 2.6? (otherwise assumes 2.4)
86 smallint is26;
87 // 1 if sample delay is not an integer fraction of a second
88 smallint need_seconds;
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +010089 char final_char;
Denis Vlasenko199c0d52007-05-30 14:48:38 +000090 char *cur_outbuf;
Denis Vlasenko199c0d52007-05-30 14:48:38 +000091 int delta;
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +010092 unsigned deltanz;
Denis Vlasenko199c0d52007-05-30 14:48:38 +000093 struct timeval tv;
Denys Vlasenko4789c7c2020-06-10 15:37:39 +020094 struct timeval start;
Denis Vlasenko199c0d52007-05-30 14:48:38 +000095#define first_proc_file proc_stat
96 proc_file proc_stat; // Must match the order of proc_name's!
97 proc_file proc_loadavg;
98 proc_file proc_net_dev;
99 proc_file proc_meminfo;
100 proc_file proc_diskstats;
101 proc_file proc_sys_fs_filenr;
102};
103#define G (*ptr_to_globals)
104#define gen (G.gen )
105#define is26 (G.is26 )
106#define need_seconds (G.need_seconds )
107#define cur_outbuf (G.cur_outbuf )
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000108#define proc_stat (G.proc_stat )
109#define proc_loadavg (G.proc_loadavg )
110#define proc_net_dev (G.proc_net_dev )
111#define proc_meminfo (G.proc_meminfo )
112#define proc_diskstats (G.proc_diskstats )
113#define proc_sys_fs_filenr (G.proc_sys_fs_filenr)
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200114#define outbuf bb_common_bufsiz1
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000115#define INIT_G() do { \
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200116 setup_common_bufsiz(); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000117 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
118 cur_outbuf = outbuf; \
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100119 G.final_char = '\n'; \
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +0100120 G.deltanz = G.delta = 1000000; \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000121} while (0)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000122
123static inline void reset_outbuf(void)
124{
125 cur_outbuf = outbuf;
126}
127
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000128static void print_outbuf(void)
129{
130 int sz = cur_outbuf - outbuf;
131 if (sz > 0) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000132 xwrite(STDOUT_FILENO, outbuf, sz);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000133 cur_outbuf = outbuf;
134 }
135}
136
137static void put(const char *s)
138{
Denys Vlasenko99c71c92016-03-15 15:28:49 +0100139 char *p = cur_outbuf;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200140 int sz = outbuf + COMMON_BUFSIZE - p;
Denys Vlasenko99c71c92016-03-15 15:28:49 +0100141 while (*s && --sz >= 0)
142 *p++ = *s++;
143 cur_outbuf = p;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000144}
145
146static void put_c(char c)
147{
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200148 if (cur_outbuf < outbuf + COMMON_BUFSIZE)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000149 *cur_outbuf++ = c;
150}
151
152static void put_question_marks(int count)
153{
154 while (count--)
155 put_c('?');
156}
157
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000158static void readfile_z(proc_file *pf, const char* fname)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000159{
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000160// open_read_close() will do two reads in order to be sure we are at EOF,
161// and we don't need/want that.
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000162 int fd;
163 int sz, rdsz;
164 char *buf;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000165
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000166 sz = pf->file_sz;
167 buf = pf->file;
168 if (!buf) {
169 buf = xmalloc(PROC_MIN_FILE_SIZE);
170 sz = PROC_MIN_FILE_SIZE;
171 }
172 again:
173 fd = xopen(fname, O_RDONLY);
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000174 buf[0] = '\0';
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000175 rdsz = read(fd, buf, sz-1);
Denis Vlasenkobc2fd372008-06-30 07:33:52 +0000176 close(fd);
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000177 if (rdsz > 0) {
178 if (rdsz == sz-1 && sz < PROC_MAX_FILE_SIZE) {
179 sz *= 2;
180 buf = xrealloc(buf, sz);
181 goto again;
182 }
183 buf[rdsz] = '\0';
184 }
185 pf->file_sz = sz;
186 pf->file = buf;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000187}
188
189static const char* get_file(proc_file *pf)
190{
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000191 if (pf->last_gen != gen) {
192 pf->last_gen = gen;
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000193 readfile_z(pf, proc_name[pf - &first_proc_file]);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000194 }
195 return pf->file;
196}
197
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000198static ullong read_after_slash(const char *p)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000199{
200 p = strchr(p, '/');
201 if (!p) return 0;
202 return strtoull(p+1, NULL, 10);
203}
204
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100205enum conv_type {
206 conv_decimal = 0,
207 conv_slash = 1
208};
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000209
210// Reads decimal values from line. Values start after key, for example:
211// "cpu 649369 0 341297 4336769..." - key is "cpu" here.
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100212// Values are stored in vec[].
Denys Vlasenko524fecf2022-01-11 19:32:16 +0100213// posbits is a bit list of positions we are interested in.
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100214// for example: 00100110 - we want 1st, 2nd and 5th value.
215// posbits.bit0 encodes conversion type.
216static int rdval(const char* p, const char* key, ullong *vec, long posbits)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000217{
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100218 unsigned curpos;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000219
220 p = strstr(p, key);
221 if (!p) return 1;
222
223 p += strlen(key);
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100224 curpos = 1 << 1;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000225 while (1) {
226 while (*p == ' ' || *p == '\t') p++;
227 if (*p == '\n' || *p == '\0') break;
228
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100229 if (curpos & posbits) { // read this value
230 *vec++ = (posbits & 1) == conv_decimal ?
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000231 strtoull(p, NULL, 10) :
232 read_after_slash(p);
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100233 posbits -= curpos;
234 if (posbits <= 1)
Denys Vlasenkoe4de8c62016-03-15 15:22:42 +0100235 return 0;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000236 }
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100237 while (*p > ' ') // skip over the value
238 p++;
239 curpos <<= 1;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000240 }
241 return 0;
242}
243
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000244// Parses files with lines like "... ... ... 3/148 ...."
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100245static int rdval_loadavg(const char* p, ullong *vec, long posbits)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000246{
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000247 int result;
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100248 result = rdval(p, "", vec, posbits | conv_slash);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000249 return result;
250}
251
252// Parses /proc/diskstats
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100253// 1 2 3 4 5 6(rd) 7 8 9 10(wr) 11 12 13 14
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000254// 3 0 hda 51292 14441 841783 926052 25717 79650 843256 3029804 0 148459 3956933
255// 3 1 hda1 0 0 0 0 <- ignore if only 4 fields
Denys Vlasenko3b160342011-11-01 23:34:46 +0100256// Linux 3.0 (maybe earlier) started printing full stats for hda1 too.
257// Had to add code which skips such devices.
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000258static int rdval_diskstats(const char* p, ullong *vec)
259{
Denys Vlasenko3b160342011-11-01 23:34:46 +0100260 char devname[32];
261 unsigned devname_len = 0;
262 int value_idx = 0;
263
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000264 vec[0] = 0;
265 vec[1] = 0;
266 while (1) {
Denys Vlasenko3b160342011-11-01 23:34:46 +0100267 value_idx++;
268 while (*p == ' ' || *p == '\t')
269 p++;
270 if (*p == '\0')
271 break;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000272 if (*p == '\n') {
Denys Vlasenko3b160342011-11-01 23:34:46 +0100273 value_idx = 0;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000274 p++;
275 continue;
276 }
Denys Vlasenko3b160342011-11-01 23:34:46 +0100277 if (value_idx == 3) {
278 char *end = strchrnul(p, ' ');
279 /* If this a hda1-like device (same prefix as last one + digit)? */
280 if (devname_len && strncmp(devname, p, devname_len) == 0 && isdigit(p[devname_len])) {
281 p = end;
282 goto skip_line; /* skip entire line */
283 }
284 /* It is not. Remember the name for future checks */
285 devname_len = end - p;
286 if (devname_len > sizeof(devname)-1)
287 devname_len = sizeof(devname)-1;
288 strncpy(devname, p, devname_len);
289 /* devname[devname_len] = '\0'; - not really needed */
290 p = end;
291 } else
292 if (value_idx == 6) {
293 // TODO: *sectorsize (don't know how to find out sectorsize)
294 vec[0] += strtoull(p, NULL, 10);
295 } else
296 if (value_idx == 10) {
297 // TODO: *sectorsize (don't know how to find out sectorsize)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000298 vec[1] += strtoull(p, NULL, 10);
Denys Vlasenko3b160342011-11-01 23:34:46 +0100299 skip_line:
300 while (*p != '\n' && *p != '\0')
301 p++;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000302 continue;
303 }
Denys Vlasenko3b160342011-11-01 23:34:46 +0100304 while ((unsigned char)(*p) > ' ') // skip over value
305 p++;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000306 }
307 return 0;
308}
309
310static void scale(ullong ul)
311{
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000312 char buf[5];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000313
314 /* see http://en.wikipedia.org/wiki/Tera */
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200315 smart_ulltoa4(ul, buf, " kmgtpezy")[0] = '\0';
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000316 put(buf);
317}
318
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000319#define S_STAT(a) \
320typedef struct a { \
321 struct s_stat *next; \
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200322 void (*collect)(struct a *s) FAST_FUNC; \
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000323 const char *label;
324#define S_STAT_END(a) } a;
325
326S_STAT(s_stat)
327S_STAT_END(s_stat)
328
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200329static void FAST_FUNC collect_literal(s_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000330{
331}
332
333static s_stat* init_literal(void)
334{
Denis Vlasenkobefd5ea2008-06-28 23:33:02 +0000335 s_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000336 s->collect = collect_literal;
337 return (s_stat*)s;
338}
339
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000340static s_stat* init_cr(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000341{
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100342 G.final_char = '\r';
343 return NULL;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000344}
345
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000346// user nice system idle iowait irq softirq (last 3 only in 2.6)
347//cpu 649369 0 341297 4336769 11640 7122 1183
348//cpuN 649369 0 341297 4336769 11640 7122 1183
349enum { CPU_FIELDCNT = 7 };
350S_STAT(cpu_stat)
351 ullong old[CPU_FIELDCNT];
Denys Vlasenko85b380f2019-08-06 16:56:00 +0200352 unsigned bar_sz;
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100353 char bar[1];
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000354S_STAT_END(cpu_stat)
355
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200356static void FAST_FUNC collect_cpu(cpu_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000357{
358 ullong data[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
359 unsigned frac[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
360 ullong all = 0;
Denys Vlasenko85b380f2019-08-06 16:56:00 +0200361 unsigned norm_all = 0;
362 unsigned bar_sz = s->bar_sz;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000363 char *bar = s->bar;
364 int i;
365
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100366 if (rdval(get_file(&proc_stat), "cpu ", data, 0
367 | (1 << 1)
368 | (1 << 2)
369 | (1 << 3)
370 | (1 << 4)
371 | (1 << 5)
372 | (1 << 6)
373 | (1 << 7))
374 ) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000375 put_question_marks(bar_sz);
376 return;
377 }
378
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000379 for (i = 0; i < CPU_FIELDCNT; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000380 ullong old = s->old[i];
381 if (data[i] < old) old = data[i]; //sanitize
382 s->old[i] = data[i];
383 all += (data[i] -= old);
384 }
385
386 if (all) {
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000387 for (i = 0; i < CPU_FIELDCNT; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000388 ullong t = bar_sz * data[i];
389 norm_all += data[i] = t / all;
390 frac[i] = t % all;
391 }
392
393 while (norm_all < bar_sz) {
394 unsigned max = frac[0];
395 int pos = 0;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000396 for (i = 1; i < CPU_FIELDCNT; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000397 if (frac[i] > max) max = frac[i], pos = i;
398 }
399 frac[pos] = 0; //avoid bumping up same value twice
400 data[pos]++;
401 norm_all++;
402 }
403
404 memset(bar, '.', bar_sz);
405 memset(bar, 'S', data[2]); bar += data[2]; //sys
406 memset(bar, 'U', data[0]); bar += data[0]; //usr
407 memset(bar, 'N', data[1]); bar += data[1]; //nice
408 memset(bar, 'D', data[4]); bar += data[4]; //iowait
409 memset(bar, 'I', data[5]); bar += data[5]; //irq
410 memset(bar, 'i', data[6]); bar += data[6]; //softirq
411 } else {
412 memset(bar, '?', bar_sz);
413 }
414 put(s->bar);
415}
416
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000417static s_stat* init_cpu(const char *param)
418{
419 int sz;
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100420 cpu_stat *s;
Denys Vlasenko85b380f2019-08-06 16:56:00 +0200421 sz = param[0] ? strtoul(param, NULL, 0) : 10;
422 if (sz <= 0) sz = 1;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000423 if (sz > 1000) sz = 1000;
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100424 s = xzalloc(sizeof(*s) + sz);
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000425 /*s->bar[sz] = '\0'; - xzalloc did it */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000426 s->bar_sz = sz;
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100427 s->collect = collect_cpu;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000428 return (s_stat*)s;
429}
430
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000431S_STAT(int_stat)
432 ullong old;
433 int no;
434S_STAT_END(int_stat)
435
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200436static void FAST_FUNC collect_int(int_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000437{
438 ullong data[1];
439 ullong old;
440
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100441 if (rdval(get_file(&proc_stat), "intr", data, 1 << s->no)) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000442 put_question_marks(4);
443 return;
444 }
445
446 old = s->old;
447 if (data[0] < old) old = data[0]; //sanitize
448 s->old = data[0];
449 scale(data[0] - old);
450}
451
452static s_stat* init_int(const char *param)
453{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000454 int_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000455 s->collect = collect_int;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000456 if (param[0] == '\0') {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000457 s->no = 1;
458 } else {
Denys Vlasenko77832482010-08-12 14:14:45 +0200459 int n = xatoi_positive(param);
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000460 s->no = n + 2;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000461 }
462 return (s_stat*)s;
463}
464
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000465S_STAT(ctx_stat)
466 ullong old;
467S_STAT_END(ctx_stat)
468
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200469static void FAST_FUNC collect_ctx(ctx_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000470{
471 ullong data[1];
472 ullong old;
473
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100474 if (rdval(get_file(&proc_stat), "ctxt", data, 1 << 1)) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000475 put_question_marks(4);
476 return;
477 }
478
479 old = s->old;
480 if (data[0] < old) old = data[0]; //sanitize
481 s->old = data[0];
482 scale(data[0] - old);
483}
484
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000485static s_stat* init_ctx(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000486{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000487 ctx_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000488 s->collect = collect_ctx;
489 return (s_stat*)s;
490}
491
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000492S_STAT(blk_stat)
493 const char* lookfor;
494 ullong old[2];
495S_STAT_END(blk_stat)
496
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200497static void FAST_FUNC collect_blk(blk_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000498{
499 ullong data[2];
500 int i;
501
502 if (is26) {
503 i = rdval_diskstats(get_file(&proc_diskstats), data);
504 } else {
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100505 i = rdval(get_file(&proc_stat), s->lookfor, data, 0
506 | (1 << 1)
507 | (1 << 2)
508 );
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000509 // Linux 2.4 reports bio in Kbytes, convert to sectors:
510 data[0] *= 2;
511 data[1] *= 2;
512 }
513 if (i) {
514 put_question_marks(9);
515 return;
516 }
517
Denys Vlasenkofe73c8d2022-08-30 16:41:17 +0200518 for (i = 0; i < 2; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000519 ullong old = s->old[i];
520 if (data[i] < old) old = data[i]; //sanitize
521 s->old[i] = data[i];
522 data[i] -= old;
523 }
524 scale(data[0]*512); // TODO: *sectorsize
525 put_c(' ');
526 scale(data[1]*512);
527}
528
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000529static s_stat* init_blk(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000530{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000531 blk_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000532 s->collect = collect_blk;
533 s->lookfor = "page";
534 return (s_stat*)s;
535}
536
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000537S_STAT(fork_stat)
538 ullong old;
539S_STAT_END(fork_stat)
540
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200541static void FAST_FUNC collect_thread_nr(fork_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000542{
543 ullong data[1];
544
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100545 if (rdval_loadavg(get_file(&proc_loadavg), data, 1 << 4)) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000546 put_question_marks(4);
547 return;
548 }
549 scale(data[0]);
550}
551
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200552static void FAST_FUNC collect_fork(fork_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000553{
554 ullong data[1];
555 ullong old;
556
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100557 if (rdval(get_file(&proc_stat), "processes", data, 1 << 1)) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000558 put_question_marks(4);
559 return;
560 }
561
562 old = s->old;
563 if (data[0] < old) old = data[0]; //sanitize
564 s->old = data[0];
565 scale(data[0] - old);
566}
567
568static s_stat* init_fork(const char *param)
569{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000570 fork_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000571 if (*param == 'n') {
572 s->collect = collect_thread_nr;
573 } else {
574 s->collect = collect_fork;
575 }
576 return (s_stat*)s;
577}
578
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000579S_STAT(if_stat)
580 ullong old[4];
581 const char *device;
582 char *device_colon;
583S_STAT_END(if_stat)
584
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200585static void FAST_FUNC collect_if(if_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000586{
587 ullong data[4];
588 int i;
589
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100590 if (rdval(get_file(&proc_net_dev), s->device_colon, data, 0
591 | (1 << 1)
592 | (1 << 3)
593 | (1 << 9)
594 | (1 << 11))
595 ) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000596 put_question_marks(10);
597 return;
598 }
599
Denys Vlasenkofe73c8d2022-08-30 16:41:17 +0200600 for (i = 0; i < 4; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000601 ullong old = s->old[i];
602 if (data[i] < old) old = data[i]; //sanitize
603 s->old[i] = data[i];
604 data[i] -= old;
605 }
606 put_c(data[1] ? '*' : ' ');
607 scale(data[0]);
608 put_c(data[3] ? '*' : ' ');
609 scale(data[2]);
610}
611
612static s_stat* init_if(const char *device)
613{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000614 if_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000615
616 if (!device || !device[0])
617 bb_show_usage();
618 s->collect = collect_if;
619
620 s->device = device;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000621 s->device_colon = xasprintf("%s:", device);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000622 return (s_stat*)s;
623}
624
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000625S_STAT(mem_stat)
626 char opt;
627S_STAT_END(mem_stat)
628
629// "Memory" value should not include any caches.
630// IOW: neither "ls -laR /" nor heavy read/write activity
631// should affect it. We'd like to also include any
632// long-term allocated kernel-side mem, but it is hard
633// to figure out. For now, bufs, cached & slab are
634// counted as "free" memory
635//2.6.16:
636//MemTotal: 773280 kB
637//MemFree: 25912 kB - genuinely free
638//Buffers: 320672 kB - cache
639//Cached: 146396 kB - cache
640//SwapCached: 0 kB
641//Active: 183064 kB
642//Inactive: 356892 kB
643//HighTotal: 0 kB
644//HighFree: 0 kB
645//LowTotal: 773280 kB
646//LowFree: 25912 kB
647//SwapTotal: 131064 kB
648//SwapFree: 131064 kB
649//Dirty: 48 kB
650//Writeback: 0 kB
651//Mapped: 96620 kB
652//Slab: 200668 kB - takes 7 Mb on my box fresh after boot,
653// but includes dentries and inodes
654// (== can take arbitrary amount of mem)
655//CommitLimit: 517704 kB
656//Committed_AS: 236776 kB
657//PageTables: 1248 kB
658//VmallocTotal: 516052 kB
659//VmallocUsed: 3852 kB
660//VmallocChunk: 512096 kB
661//HugePages_Total: 0
662//HugePages_Free: 0
663//Hugepagesize: 4096 kB
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200664static void FAST_FUNC collect_mem(mem_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000665{
Denys Vlasenko524fecf2022-01-11 19:32:16 +0100666 ullong m_total;
667 ullong m_free;
668 ullong m_bufs;
669 ullong m_cached;
670 ullong m_slab;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000671
Denys Vlasenko524fecf2022-01-11 19:32:16 +0100672 const char *meminfo = get_file(&proc_meminfo);
673
674 if (s->opt == 'd' /* dirty page cache */
675 || s->opt == 'w' /* under writeback */
676 ) {
677 m_total = 0; /* temporary reuse m_total */
678 if (rdval(meminfo,
679 (s->opt == 'd' ? "Dirty:" : "Writeback:"),
680 &m_total, 1 << 1)
681 ) {
682 put_question_marks(4);
683 return;
684 }
685 scale(m_total << 10);
686 return;
687 }
688
689 m_total = 0;
690 if (rdval(meminfo, "MemTotal:", &m_total, 1 << 1)) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000691 put_question_marks(4);
692 return;
693 }
Denis Vlasenko8a2e4212008-02-29 07:46:19 +0000694 if (s->opt == 't') {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000695 scale(m_total << 10);
696 return;
697 }
698
Denys Vlasenko524fecf2022-01-11 19:32:16 +0100699 m_free = 0;
700 m_bufs = 0;
701 m_cached = 0;
702 m_slab = 0;
703 if (rdval(meminfo, "MemFree:", &m_free , 1 << 1)
704 || rdval(meminfo, "Buffers:", &m_bufs , 1 << 1)
705 || rdval(meminfo, "Cached:", &m_cached, 1 << 1)
706 || rdval(meminfo, "Slab:", &m_slab , 1 << 1)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000707 ) {
708 put_question_marks(4);
709 return;
710 }
711
712 m_free += m_bufs + m_cached + m_slab;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000713 switch (s->opt) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000714 case 'f':
715 scale(m_free << 10); break;
716 default:
717 scale((m_total - m_free) << 10); break;
718 }
719}
720
721static s_stat* init_mem(const char *param)
722{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000723 mem_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000724 s->collect = collect_mem;
725 s->opt = param[0];
726 return (s_stat*)s;
727}
728
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000729S_STAT(swp_stat)
730S_STAT_END(swp_stat)
731
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200732static void FAST_FUNC collect_swp(swp_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000733{
734 ullong s_total[1];
735 ullong s_free[1];
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100736 if (rdval(get_file(&proc_meminfo), "SwapTotal:", s_total, 1 << 1)
737 || rdval(proc_meminfo.file, "SwapFree:" , s_free, 1 << 1)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000738 ) {
739 put_question_marks(4);
740 return;
741 }
742 scale((s_total[0]-s_free[0]) << 10);
743}
744
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000745static s_stat* init_swp(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000746{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000747 swp_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000748 s->collect = collect_swp;
749 return (s_stat*)s;
750}
751
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000752S_STAT(fd_stat)
753S_STAT_END(fd_stat)
754
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200755static void FAST_FUNC collect_fd(fd_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000756{
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000757 ullong data[2];
758
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100759 if (rdval(get_file(&proc_sys_fs_filenr), "", data, 0
760 | (1 << 1)
761 | (1 << 2))
762 ) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000763 put_question_marks(4);
764 return;
765 }
766
767 scale(data[0] - data[1]);
768}
769
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000770static s_stat* init_fd(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000771{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000772 fd_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000773 s->collect = collect_fd;
774 return (s_stat*)s;
775}
776
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000777S_STAT(time_stat)
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100778 unsigned prec;
779 unsigned scale;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000780S_STAT_END(time_stat)
781
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200782static void FAST_FUNC collect_tv(time_stat *s, struct timeval *tv, int local)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000783{
784 char buf[sizeof("12:34:56.123456")];
785 struct tm* tm;
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200786 unsigned us = tv->tv_usec + s->scale/2;
787 time_t t = tv->tv_sec;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000788
789 if (us >= 1000000) {
790 t++;
791 us -= 1000000;
792 }
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200793 if (local)
794 tm = localtime(&t);
795 else
796 tm = gmtime(&t);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000797
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200798 sprintf(buf, "%02u:%02u:%02u", tm->tm_hour, tm->tm_min, tm->tm_sec);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000799 if (s->prec)
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200800 sprintf(buf+8, ".%0*u", s->prec, us / s->scale);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000801 put(buf);
802}
803
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200804static void FAST_FUNC collect_time(time_stat *s)
805{
806 collect_tv(s, &G.tv, /*local:*/ 1);
807}
808
809static void FAST_FUNC collect_monotonic(time_stat *s)
810{
811 struct timeval tv_mono;
812
813 tv_mono.tv_sec = G.tv.tv_sec - G.start.tv_sec;
814#if 0 /* Do we want this? */
815 if (tv_mono.tv_sec < 0) {
816 /* Time went backwards, reset start time to "now" */
817 tv_mono.tv_sec = 0;
818 G.start = G.tv;
819 }
820#endif
821 tv_mono.tv_usec = G.tv.tv_usec - G.start.tv_usec;
822 if ((int32_t)tv_mono.tv_usec < 0) {
823 tv_mono.tv_usec += 1000000;
824 tv_mono.tv_sec--;
825 }
826 collect_tv(s, &tv_mono, /*local:*/ 0);
827}
828
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000829static s_stat* init_time(const char *param)
830{
831 int prec;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000832 time_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000833
834 s->collect = collect_time;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000835 prec = param[0] - '0';
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000836 if (prec < 0) prec = 0;
837 else if (prec > 6) prec = 6;
838 s->prec = prec;
839 s->scale = 1;
840 while (prec++ < 6)
841 s->scale *= 10;
842 return (s_stat*)s;
843}
844
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200845static s_stat* init_monotonic(const char *param)
846{
847 time_stat *s = (void*)init_time(param);
848 s->collect = collect_monotonic;
849 return (s_stat*)s;
850}
851
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200852static void FAST_FUNC collect_info(s_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000853{
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000854 gen ^= 1;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000855 while (s) {
856 put(s->label);
857 s->collect(s);
858 s = s->next;
859 }
860}
861
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000862typedef s_stat* init_func(const char *param);
863
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200864static const char options[] ALIGN1 = "ncmsfixptTbr";
Denys Vlasenko965b7952020-11-30 13:03:03 +0100865static init_func *const init_functions[] ALIGN_PTR = {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000866 init_if,
867 init_cpu,
868 init_mem,
869 init_swp,
870 init_fd,
871 init_int,
872 init_ctx,
873 init_fork,
874 init_time,
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200875 init_monotonic,
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000876 init_blk,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000877 init_cr
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000878};
879
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000880int nmeter_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100881int nmeter_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000882{
883 char buf[32];
884 s_stat *first = NULL;
885 s_stat *last = NULL;
886 s_stat *s;
Denys Vlasenkoa6041862010-12-13 15:17:22 +0100887 char *opt_d;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000888 char *cur, *prev;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000889
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000890 INIT_G();
891
892 xchdir("/proc");
893
Denis Vlasenkocd785fb2008-08-14 21:57:43 +0000894 if (open_read_close("version", buf, sizeof(buf)-1) > 0) {
895 buf[sizeof(buf)-1] = '\0';
896 is26 = (strstr(buf, " 2.4.") == NULL);
897 }
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000898
Denys Vlasenko41b1e2c2016-03-15 16:13:23 +0100899 if (getopt32(argv, "d:", &opt_d)) {
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +0100900 G.delta = xatoi(opt_d) * 1000;
901 G.deltanz = G.delta > 0 ? G.delta : 1;
902 need_seconds = (1000000 % G.deltanz) != 0;
Denys Vlasenko41b1e2c2016-03-15 16:13:23 +0100903 }
Denys Vlasenkoa6041862010-12-13 15:17:22 +0100904 argv += optind;
905
906 if (!argv[0])
907 bb_show_usage();
908
909 // Can use argv[0] directly, but this will mess up
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000910 // parameters as seen by e.g. ps. Making a copy...
Denys Vlasenkoa6041862010-12-13 15:17:22 +0100911 cur = xstrdup(argv[0]);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000912 while (1) {
913 char *param, *p;
914 prev = cur;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000915 again:
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000916 cur = strchr(cur, '%');
917 if (!cur)
918 break;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000919 if (cur[1] == '%') { // %%
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000920 overlapping_strcpy(cur, cur + 1);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000921 cur++;
922 goto again;
923 }
924 *cur++ = '\0'; // overwrite %
925 if (cur[0] == '[') {
926 // format: %[foptstring]
927 cur++;
928 p = strchr(options, cur[0]);
929 param = cur+1;
930 while (cur[0] != ']') {
931 if (!cur[0])
932 bb_show_usage();
933 cur++;
934 }
935 *cur++ = '\0'; // overwrite [
936 } else {
937 // format: %NNNNNNf
938 param = cur;
939 while (cur[0] >= '0' && cur[0] <= '9')
940 cur++;
941 if (!cur[0])
942 bb_show_usage();
943 p = strchr(options, cur[0]);
944 *cur++ = '\0'; // overwrite format char
945 }
946 if (!p)
947 bb_show_usage();
948 s = init_functions[p-options](param);
949 if (s) {
950 s->label = prev;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000951 /*s->next = NULL; - all initXXX funcs use xzalloc */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000952 if (!first)
953 first = s;
954 else
955 last->next = s;
956 last = s;
957 } else {
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +0100958 // %r option. remove it from string
959 overlapping_strcpy(prev + strlen(prev), cur);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000960 cur = prev;
961 }
962 }
963 if (prev[0]) {
964 s = init_literal();
965 s->label = prev;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000966 /*s->next = NULL; - all initXXX funcs use xzalloc */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000967 if (!first)
968 first = s;
969 else
970 last->next = s;
971 last = s;
972 }
973
974 // Generate first samples but do not print them, they're bogus
975 collect_info(first);
976 reset_outbuf();
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200977
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +0100978 if (G.delta >= 0) {
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100979 xgettimeofday(&G.tv);
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200980 usleep(G.delta > 1000000 ? 1000000 : G.delta - G.tv.tv_usec % G.deltanz);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000981 }
982
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100983 xgettimeofday(&G.start);
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200984 G.tv = G.start;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000985 while (1) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000986 collect_info(first);
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100987 put_c(G.final_char);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000988 print_outbuf();
989
990 // Negative delta -> no usleep at all
991 // This will hog the CPU but you can have REALLY GOOD
992 // time resolution ;)
993 // TODO: detect and avoid useless updates
994 // (like: nothing happens except time)
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +0100995 if (G.delta >= 0) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000996 int rem;
997 // can be commented out, will sacrifice sleep time precision a bit
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100998 xgettimeofday(&G.tv);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000999 if (need_seconds)
Denys Vlasenko4789c7c2020-06-10 15:37:39 +02001000 rem = G.delta - ((ullong)G.tv.tv_sec*1000000 + G.tv.tv_usec) % G.deltanz;
Denis Vlasenko1db39b22006-10-11 20:59:02 +00001001 else
Denys Vlasenko4789c7c2020-06-10 15:37:39 +02001002 rem = G.delta - (unsigned)G.tv.tv_usec % G.deltanz;
Denis Vlasenko1db39b22006-10-11 20:59:02 +00001003 // Sometimes kernel wakes us up just a tiny bit earlier than asked
1004 // Do not go to very short sleep in this case
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +01001005 if (rem < (unsigned)G.delta / 128) {
1006 rem += G.delta;
Denis Vlasenko1db39b22006-10-11 20:59:02 +00001007 }
1008 usleep(rem);
1009 }
Denys Vlasenko3c13da32020-12-30 23:48:01 +01001010 xgettimeofday(&G.tv);
Denis Vlasenko1db39b22006-10-11 20:59:02 +00001011 }
1012
Denis Vlasenko199c0d52007-05-30 14:48:38 +00001013 /*return 0;*/
Denis Vlasenko1db39b22006-10-11 20:59:02 +00001014}