blob: f08938654852ccd3af48a1bd2bd6d10599e1aa00 [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"
30//usage: "\n %[mf] Free memory"
31//usage: "\n %[mt] Total memory"
32//usage: "\n %s Allocated swap"
33//usage: "\n %f Number of used file descriptors"
34//usage: "\n %Ni Total/specific IRQ rate"
35//usage: "\n %x Context switch rate"
36//usage: "\n %p Forks"
37//usage: "\n %[pn] # of processes"
38//usage: "\n %b Block io"
39//usage: "\n %Nt Time (with N decimal points)"
Denys Vlasenko4789c7c2020-06-10 15:37:39 +020040//usage: "\n %NT Zero-based timestamp (with N decimal points)"
Denys Vlasenkoa6041862010-12-13 15:17:22 +010041//usage: "\n %r Print <cr> instead of <lf> at EOL"
42
Denis Vlasenko1db39b22006-10-11 20:59:02 +000043//TODO:
44// simplify code
45// /proc/locks
46// /proc/stat:
47// disk_io: (3,0):(22272,17897,410702,4375,54750)
48// btime 1059401962
Denis Vlasenkoa6163ca2007-06-17 00:35:15 +000049//TODO: use sysinfo libc call/syscall, if appropriate
50// (faster than open/read/close):
51// sysinfo({uptime=15017, loads=[5728, 15040, 16480]
52// totalram=2107416576, freeram=211525632, sharedram=0, bufferram=157204480}
53// totalswap=134209536, freeswap=134209536, procs=157})
Denis Vlasenko1db39b22006-10-11 20:59:02 +000054
Denis Vlasenkoa6163ca2007-06-17 00:35:15 +000055#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020056#include "common_bufsiz.h"
Denis Vlasenko1db39b22006-10-11 20:59:02 +000057
58typedef unsigned long long ullong;
Denis Vlasenko1db39b22006-10-11 20:59:02 +000059
Denis Vlasenkobfc93a32008-09-26 11:12:09 +000060enum { /* Preferably use powers of 2 */
61 PROC_MIN_FILE_SIZE = 256,
62 PROC_MAX_FILE_SIZE = 16 * 1024,
63};
Denis Vlasenko1db39b22006-10-11 20:59:02 +000064
65typedef struct proc_file {
Denis Vlasenko1db39b22006-10-11 20:59:02 +000066 char *file;
Denis Vlasenkobfc93a32008-09-26 11:12:09 +000067 int file_sz;
Denis Vlasenko199c0d52007-05-30 14:48:38 +000068 smallint last_gen;
Denis Vlasenko1db39b22006-10-11 20:59:02 +000069} proc_file;
70
Denis Vlasenko199c0d52007-05-30 14:48:38 +000071static const char *const proc_name[] = {
72 "stat", // Must match the order of proc_file's!
73 "loadavg",
74 "net/dev",
75 "meminfo",
76 "diskstats",
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000077 "sys/fs/file-nr"
Denis Vlasenko199c0d52007-05-30 14:48:38 +000078};
79
80struct globals {
81 // Sample generation flip-flop
82 smallint gen;
83 // Linux 2.6? (otherwise assumes 2.4)
84 smallint is26;
85 // 1 if sample delay is not an integer fraction of a second
86 smallint need_seconds;
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +010087 char final_char;
Denis Vlasenko199c0d52007-05-30 14:48:38 +000088 char *cur_outbuf;
Denis Vlasenko199c0d52007-05-30 14:48:38 +000089 int delta;
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +010090 unsigned deltanz;
Denis Vlasenko199c0d52007-05-30 14:48:38 +000091 struct timeval tv;
Denys Vlasenko4789c7c2020-06-10 15:37:39 +020092 struct timeval start;
Denis Vlasenko199c0d52007-05-30 14:48:38 +000093#define first_proc_file proc_stat
94 proc_file proc_stat; // Must match the order of proc_name's!
95 proc_file proc_loadavg;
96 proc_file proc_net_dev;
97 proc_file proc_meminfo;
98 proc_file proc_diskstats;
99 proc_file proc_sys_fs_filenr;
100};
101#define G (*ptr_to_globals)
102#define gen (G.gen )
103#define is26 (G.is26 )
104#define need_seconds (G.need_seconds )
105#define cur_outbuf (G.cur_outbuf )
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000106#define proc_stat (G.proc_stat )
107#define proc_loadavg (G.proc_loadavg )
108#define proc_net_dev (G.proc_net_dev )
109#define proc_meminfo (G.proc_meminfo )
110#define proc_diskstats (G.proc_diskstats )
111#define proc_sys_fs_filenr (G.proc_sys_fs_filenr)
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200112#define outbuf bb_common_bufsiz1
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000113#define INIT_G() do { \
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200114 setup_common_bufsiz(); \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000115 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
116 cur_outbuf = outbuf; \
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100117 G.final_char = '\n'; \
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +0100118 G.deltanz = G.delta = 1000000; \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000119} while (0)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000120
121static inline void reset_outbuf(void)
122{
123 cur_outbuf = outbuf;
124}
125
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000126static void print_outbuf(void)
127{
128 int sz = cur_outbuf - outbuf;
129 if (sz > 0) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000130 xwrite(STDOUT_FILENO, outbuf, sz);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000131 cur_outbuf = outbuf;
132 }
133}
134
135static void put(const char *s)
136{
Denys Vlasenko99c71c92016-03-15 15:28:49 +0100137 char *p = cur_outbuf;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200138 int sz = outbuf + COMMON_BUFSIZE - p;
Denys Vlasenko99c71c92016-03-15 15:28:49 +0100139 while (*s && --sz >= 0)
140 *p++ = *s++;
141 cur_outbuf = p;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000142}
143
144static void put_c(char c)
145{
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200146 if (cur_outbuf < outbuf + COMMON_BUFSIZE)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000147 *cur_outbuf++ = c;
148}
149
150static void put_question_marks(int count)
151{
152 while (count--)
153 put_c('?');
154}
155
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000156static void readfile_z(proc_file *pf, const char* fname)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000157{
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000158// open_read_close() will do two reads in order to be sure we are at EOF,
159// and we don't need/want that.
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000160 int fd;
161 int sz, rdsz;
162 char *buf;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000163
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000164 sz = pf->file_sz;
165 buf = pf->file;
166 if (!buf) {
167 buf = xmalloc(PROC_MIN_FILE_SIZE);
168 sz = PROC_MIN_FILE_SIZE;
169 }
170 again:
171 fd = xopen(fname, O_RDONLY);
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000172 buf[0] = '\0';
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000173 rdsz = read(fd, buf, sz-1);
Denis Vlasenkobc2fd372008-06-30 07:33:52 +0000174 close(fd);
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000175 if (rdsz > 0) {
176 if (rdsz == sz-1 && sz < PROC_MAX_FILE_SIZE) {
177 sz *= 2;
178 buf = xrealloc(buf, sz);
179 goto again;
180 }
181 buf[rdsz] = '\0';
182 }
183 pf->file_sz = sz;
184 pf->file = buf;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000185}
186
187static const char* get_file(proc_file *pf)
188{
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000189 if (pf->last_gen != gen) {
190 pf->last_gen = gen;
Denis Vlasenkobfc93a32008-09-26 11:12:09 +0000191 readfile_z(pf, proc_name[pf - &first_proc_file]);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000192 }
193 return pf->file;
194}
195
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000196static ullong read_after_slash(const char *p)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000197{
198 p = strchr(p, '/');
199 if (!p) return 0;
200 return strtoull(p+1, NULL, 10);
201}
202
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100203enum conv_type {
204 conv_decimal = 0,
205 conv_slash = 1
206};
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000207
208// Reads decimal values from line. Values start after key, for example:
209// "cpu 649369 0 341297 4336769..." - key is "cpu" here.
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100210// Values are stored in vec[].
211// posbits is a bit lit of positions we are interested in.
212// for example: 00100110 - we want 1st, 2nd and 5th value.
213// posbits.bit0 encodes conversion type.
214static int rdval(const char* p, const char* key, ullong *vec, long posbits)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000215{
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100216 unsigned curpos;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000217
218 p = strstr(p, key);
219 if (!p) return 1;
220
221 p += strlen(key);
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100222 curpos = 1 << 1;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000223 while (1) {
224 while (*p == ' ' || *p == '\t') p++;
225 if (*p == '\n' || *p == '\0') break;
226
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100227 if (curpos & posbits) { // read this value
228 *vec++ = (posbits & 1) == conv_decimal ?
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000229 strtoull(p, NULL, 10) :
230 read_after_slash(p);
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100231 posbits -= curpos;
232 if (posbits <= 1)
Denys Vlasenkoe4de8c62016-03-15 15:22:42 +0100233 return 0;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000234 }
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100235 while (*p > ' ') // skip over the value
236 p++;
237 curpos <<= 1;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000238 }
239 return 0;
240}
241
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000242// Parses files with lines like "... ... ... 3/148 ...."
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100243static int rdval_loadavg(const char* p, ullong *vec, long posbits)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000244{
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000245 int result;
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100246 result = rdval(p, "", vec, posbits | conv_slash);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000247 return result;
248}
249
250// Parses /proc/diskstats
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100251// 1 2 3 4 5 6(rd) 7 8 9 10(wr) 11 12 13 14
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000252// 3 0 hda 51292 14441 841783 926052 25717 79650 843256 3029804 0 148459 3956933
253// 3 1 hda1 0 0 0 0 <- ignore if only 4 fields
Denys Vlasenko3b160342011-11-01 23:34:46 +0100254// Linux 3.0 (maybe earlier) started printing full stats for hda1 too.
255// Had to add code which skips such devices.
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000256static int rdval_diskstats(const char* p, ullong *vec)
257{
Denys Vlasenko3b160342011-11-01 23:34:46 +0100258 char devname[32];
259 unsigned devname_len = 0;
260 int value_idx = 0;
261
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000262 vec[0] = 0;
263 vec[1] = 0;
264 while (1) {
Denys Vlasenko3b160342011-11-01 23:34:46 +0100265 value_idx++;
266 while (*p == ' ' || *p == '\t')
267 p++;
268 if (*p == '\0')
269 break;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000270 if (*p == '\n') {
Denys Vlasenko3b160342011-11-01 23:34:46 +0100271 value_idx = 0;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000272 p++;
273 continue;
274 }
Denys Vlasenko3b160342011-11-01 23:34:46 +0100275 if (value_idx == 3) {
276 char *end = strchrnul(p, ' ');
277 /* If this a hda1-like device (same prefix as last one + digit)? */
278 if (devname_len && strncmp(devname, p, devname_len) == 0 && isdigit(p[devname_len])) {
279 p = end;
280 goto skip_line; /* skip entire line */
281 }
282 /* It is not. Remember the name for future checks */
283 devname_len = end - p;
284 if (devname_len > sizeof(devname)-1)
285 devname_len = sizeof(devname)-1;
286 strncpy(devname, p, devname_len);
287 /* devname[devname_len] = '\0'; - not really needed */
288 p = end;
289 } else
290 if (value_idx == 6) {
291 // TODO: *sectorsize (don't know how to find out sectorsize)
292 vec[0] += strtoull(p, NULL, 10);
293 } else
294 if (value_idx == 10) {
295 // TODO: *sectorsize (don't know how to find out sectorsize)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000296 vec[1] += strtoull(p, NULL, 10);
Denys Vlasenko3b160342011-11-01 23:34:46 +0100297 skip_line:
298 while (*p != '\n' && *p != '\0')
299 p++;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000300 continue;
301 }
Denys Vlasenko3b160342011-11-01 23:34:46 +0100302 while ((unsigned char)(*p) > ' ') // skip over value
303 p++;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000304 }
305 return 0;
306}
307
308static void scale(ullong ul)
309{
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000310 char buf[5];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000311
312 /* see http://en.wikipedia.org/wiki/Tera */
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200313 smart_ulltoa4(ul, buf, " kmgtpezy")[0] = '\0';
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000314 put(buf);
315}
316
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000317#define S_STAT(a) \
318typedef struct a { \
319 struct s_stat *next; \
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200320 void (*collect)(struct a *s) FAST_FUNC; \
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000321 const char *label;
322#define S_STAT_END(a) } a;
323
324S_STAT(s_stat)
325S_STAT_END(s_stat)
326
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200327static void FAST_FUNC collect_literal(s_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000328{
329}
330
331static s_stat* init_literal(void)
332{
Denis Vlasenkobefd5ea2008-06-28 23:33:02 +0000333 s_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000334 s->collect = collect_literal;
335 return (s_stat*)s;
336}
337
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000338static s_stat* init_cr(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000339{
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100340 G.final_char = '\r';
341 return NULL;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000342}
343
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000344// user nice system idle iowait irq softirq (last 3 only in 2.6)
345//cpu 649369 0 341297 4336769 11640 7122 1183
346//cpuN 649369 0 341297 4336769 11640 7122 1183
347enum { CPU_FIELDCNT = 7 };
348S_STAT(cpu_stat)
349 ullong old[CPU_FIELDCNT];
Denys Vlasenko85b380f2019-08-06 16:56:00 +0200350 unsigned bar_sz;
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100351 char bar[1];
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000352S_STAT_END(cpu_stat)
353
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200354static void FAST_FUNC collect_cpu(cpu_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000355{
356 ullong data[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
357 unsigned frac[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
358 ullong all = 0;
Denys Vlasenko85b380f2019-08-06 16:56:00 +0200359 unsigned norm_all = 0;
360 unsigned bar_sz = s->bar_sz;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000361 char *bar = s->bar;
362 int i;
363
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100364 if (rdval(get_file(&proc_stat), "cpu ", data, 0
365 | (1 << 1)
366 | (1 << 2)
367 | (1 << 3)
368 | (1 << 4)
369 | (1 << 5)
370 | (1 << 6)
371 | (1 << 7))
372 ) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000373 put_question_marks(bar_sz);
374 return;
375 }
376
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000377 for (i = 0; i < CPU_FIELDCNT; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000378 ullong old = s->old[i];
379 if (data[i] < old) old = data[i]; //sanitize
380 s->old[i] = data[i];
381 all += (data[i] -= old);
382 }
383
384 if (all) {
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000385 for (i = 0; i < CPU_FIELDCNT; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000386 ullong t = bar_sz * data[i];
387 norm_all += data[i] = t / all;
388 frac[i] = t % all;
389 }
390
391 while (norm_all < bar_sz) {
392 unsigned max = frac[0];
393 int pos = 0;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000394 for (i = 1; i < CPU_FIELDCNT; i++) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000395 if (frac[i] > max) max = frac[i], pos = i;
396 }
397 frac[pos] = 0; //avoid bumping up same value twice
398 data[pos]++;
399 norm_all++;
400 }
401
402 memset(bar, '.', bar_sz);
403 memset(bar, 'S', data[2]); bar += data[2]; //sys
404 memset(bar, 'U', data[0]); bar += data[0]; //usr
405 memset(bar, 'N', data[1]); bar += data[1]; //nice
406 memset(bar, 'D', data[4]); bar += data[4]; //iowait
407 memset(bar, 'I', data[5]); bar += data[5]; //irq
408 memset(bar, 'i', data[6]); bar += data[6]; //softirq
409 } else {
410 memset(bar, '?', bar_sz);
411 }
412 put(s->bar);
413}
414
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000415static s_stat* init_cpu(const char *param)
416{
417 int sz;
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100418 cpu_stat *s;
Denys Vlasenko85b380f2019-08-06 16:56:00 +0200419 sz = param[0] ? strtoul(param, NULL, 0) : 10;
420 if (sz <= 0) sz = 1;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000421 if (sz > 1000) sz = 1000;
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100422 s = xzalloc(sizeof(*s) + sz);
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000423 /*s->bar[sz] = '\0'; - xzalloc did it */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000424 s->bar_sz = sz;
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100425 s->collect = collect_cpu;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000426 return (s_stat*)s;
427}
428
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000429S_STAT(int_stat)
430 ullong old;
431 int no;
432S_STAT_END(int_stat)
433
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200434static void FAST_FUNC collect_int(int_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000435{
436 ullong data[1];
437 ullong old;
438
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100439 if (rdval(get_file(&proc_stat), "intr", data, 1 << s->no)) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000440 put_question_marks(4);
441 return;
442 }
443
444 old = s->old;
445 if (data[0] < old) old = data[0]; //sanitize
446 s->old = data[0];
447 scale(data[0] - old);
448}
449
450static s_stat* init_int(const char *param)
451{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000452 int_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000453 s->collect = collect_int;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000454 if (param[0] == '\0') {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000455 s->no = 1;
456 } else {
Denys Vlasenko77832482010-08-12 14:14:45 +0200457 int n = xatoi_positive(param);
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000458 s->no = n + 2;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000459 }
460 return (s_stat*)s;
461}
462
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000463S_STAT(ctx_stat)
464 ullong old;
465S_STAT_END(ctx_stat)
466
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200467static void FAST_FUNC collect_ctx(ctx_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000468{
469 ullong data[1];
470 ullong old;
471
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100472 if (rdval(get_file(&proc_stat), "ctxt", data, 1 << 1)) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000473 put_question_marks(4);
474 return;
475 }
476
477 old = s->old;
478 if (data[0] < old) old = data[0]; //sanitize
479 s->old = data[0];
480 scale(data[0] - old);
481}
482
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000483static s_stat* init_ctx(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000484{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000485 ctx_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000486 s->collect = collect_ctx;
487 return (s_stat*)s;
488}
489
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000490S_STAT(blk_stat)
491 const char* lookfor;
492 ullong old[2];
493S_STAT_END(blk_stat)
494
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200495static void FAST_FUNC collect_blk(blk_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000496{
497 ullong data[2];
498 int i;
499
500 if (is26) {
501 i = rdval_diskstats(get_file(&proc_diskstats), data);
502 } else {
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100503 i = rdval(get_file(&proc_stat), s->lookfor, data, 0
504 | (1 << 1)
505 | (1 << 2)
506 );
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000507 // Linux 2.4 reports bio in Kbytes, convert to sectors:
508 data[0] *= 2;
509 data[1] *= 2;
510 }
511 if (i) {
512 put_question_marks(9);
513 return;
514 }
515
516 for (i=0; i<2; i++) {
517 ullong old = s->old[i];
518 if (data[i] < old) old = data[i]; //sanitize
519 s->old[i] = data[i];
520 data[i] -= old;
521 }
522 scale(data[0]*512); // TODO: *sectorsize
523 put_c(' ');
524 scale(data[1]*512);
525}
526
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000527static s_stat* init_blk(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000528{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000529 blk_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000530 s->collect = collect_blk;
531 s->lookfor = "page";
532 return (s_stat*)s;
533}
534
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000535S_STAT(fork_stat)
536 ullong old;
537S_STAT_END(fork_stat)
538
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200539static void FAST_FUNC collect_thread_nr(fork_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000540{
541 ullong data[1];
542
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100543 if (rdval_loadavg(get_file(&proc_loadavg), data, 1 << 4)) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000544 put_question_marks(4);
545 return;
546 }
547 scale(data[0]);
548}
549
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200550static void FAST_FUNC collect_fork(fork_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000551{
552 ullong data[1];
553 ullong old;
554
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100555 if (rdval(get_file(&proc_stat), "processes", data, 1 << 1)) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000556 put_question_marks(4);
557 return;
558 }
559
560 old = s->old;
561 if (data[0] < old) old = data[0]; //sanitize
562 s->old = data[0];
563 scale(data[0] - old);
564}
565
566static s_stat* init_fork(const char *param)
567{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000568 fork_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000569 if (*param == 'n') {
570 s->collect = collect_thread_nr;
571 } else {
572 s->collect = collect_fork;
573 }
574 return (s_stat*)s;
575}
576
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000577S_STAT(if_stat)
578 ullong old[4];
579 const char *device;
580 char *device_colon;
581S_STAT_END(if_stat)
582
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200583static void FAST_FUNC collect_if(if_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000584{
585 ullong data[4];
586 int i;
587
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100588 if (rdval(get_file(&proc_net_dev), s->device_colon, data, 0
589 | (1 << 1)
590 | (1 << 3)
591 | (1 << 9)
592 | (1 << 11))
593 ) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000594 put_question_marks(10);
595 return;
596 }
597
598 for (i=0; i<4; i++) {
599 ullong old = s->old[i];
600 if (data[i] < old) old = data[i]; //sanitize
601 s->old[i] = data[i];
602 data[i] -= old;
603 }
604 put_c(data[1] ? '*' : ' ');
605 scale(data[0]);
606 put_c(data[3] ? '*' : ' ');
607 scale(data[2]);
608}
609
610static s_stat* init_if(const char *device)
611{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000612 if_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000613
614 if (!device || !device[0])
615 bb_show_usage();
616 s->collect = collect_if;
617
618 s->device = device;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000619 s->device_colon = xasprintf("%s:", device);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000620 return (s_stat*)s;
621}
622
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000623S_STAT(mem_stat)
624 char opt;
625S_STAT_END(mem_stat)
626
627// "Memory" value should not include any caches.
628// IOW: neither "ls -laR /" nor heavy read/write activity
629// should affect it. We'd like to also include any
630// long-term allocated kernel-side mem, but it is hard
631// to figure out. For now, bufs, cached & slab are
632// counted as "free" memory
633//2.6.16:
634//MemTotal: 773280 kB
635//MemFree: 25912 kB - genuinely free
636//Buffers: 320672 kB - cache
637//Cached: 146396 kB - cache
638//SwapCached: 0 kB
639//Active: 183064 kB
640//Inactive: 356892 kB
641//HighTotal: 0 kB
642//HighFree: 0 kB
643//LowTotal: 773280 kB
644//LowFree: 25912 kB
645//SwapTotal: 131064 kB
646//SwapFree: 131064 kB
647//Dirty: 48 kB
648//Writeback: 0 kB
649//Mapped: 96620 kB
650//Slab: 200668 kB - takes 7 Mb on my box fresh after boot,
651// but includes dentries and inodes
652// (== can take arbitrary amount of mem)
653//CommitLimit: 517704 kB
654//Committed_AS: 236776 kB
655//PageTables: 1248 kB
656//VmallocTotal: 516052 kB
657//VmallocUsed: 3852 kB
658//VmallocChunk: 512096 kB
659//HugePages_Total: 0
660//HugePages_Free: 0
661//Hugepagesize: 4096 kB
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200662static void FAST_FUNC collect_mem(mem_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000663{
664 ullong m_total = 0;
665 ullong m_free = 0;
666 ullong m_bufs = 0;
667 ullong m_cached = 0;
668 ullong m_slab = 0;
669
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100670 if (rdval(get_file(&proc_meminfo), "MemTotal:", &m_total, 1 << 1)) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000671 put_question_marks(4);
672 return;
673 }
Denis Vlasenko8a2e4212008-02-29 07:46:19 +0000674 if (s->opt == 't') {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000675 scale(m_total << 10);
676 return;
677 }
678
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100679 if (rdval(proc_meminfo.file, "MemFree:", &m_free , 1 << 1)
680 || rdval(proc_meminfo.file, "Buffers:", &m_bufs , 1 << 1)
681 || rdval(proc_meminfo.file, "Cached:", &m_cached, 1 << 1)
682 || rdval(proc_meminfo.file, "Slab:", &m_slab , 1 << 1)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000683 ) {
684 put_question_marks(4);
685 return;
686 }
687
688 m_free += m_bufs + m_cached + m_slab;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000689 switch (s->opt) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000690 case 'f':
691 scale(m_free << 10); break;
692 default:
693 scale((m_total - m_free) << 10); break;
694 }
695}
696
697static s_stat* init_mem(const char *param)
698{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000699 mem_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000700 s->collect = collect_mem;
701 s->opt = param[0];
702 return (s_stat*)s;
703}
704
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000705S_STAT(swp_stat)
706S_STAT_END(swp_stat)
707
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200708static void FAST_FUNC collect_swp(swp_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000709{
710 ullong s_total[1];
711 ullong s_free[1];
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100712 if (rdval(get_file(&proc_meminfo), "SwapTotal:", s_total, 1 << 1)
713 || rdval(proc_meminfo.file, "SwapFree:" , s_free, 1 << 1)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000714 ) {
715 put_question_marks(4);
716 return;
717 }
718 scale((s_total[0]-s_free[0]) << 10);
719}
720
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000721static s_stat* init_swp(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000722{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000723 swp_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000724 s->collect = collect_swp;
725 return (s_stat*)s;
726}
727
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000728S_STAT(fd_stat)
729S_STAT_END(fd_stat)
730
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200731static void FAST_FUNC collect_fd(fd_stat *s UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000732{
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000733 ullong data[2];
734
Denys Vlasenko8a26fda2016-03-15 15:52:40 +0100735 if (rdval(get_file(&proc_sys_fs_filenr), "", data, 0
736 | (1 << 1)
737 | (1 << 2))
738 ) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000739 put_question_marks(4);
740 return;
741 }
742
743 scale(data[0] - data[1]);
744}
745
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000746static s_stat* init_fd(const char *param UNUSED_PARAM)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000747{
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000748 fd_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000749 s->collect = collect_fd;
750 return (s_stat*)s;
751}
752
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000753S_STAT(time_stat)
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100754 unsigned prec;
755 unsigned scale;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000756S_STAT_END(time_stat)
757
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200758static void FAST_FUNC collect_tv(time_stat *s, struct timeval *tv, int local)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000759{
760 char buf[sizeof("12:34:56.123456")];
761 struct tm* tm;
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200762 unsigned us = tv->tv_usec + s->scale/2;
763 time_t t = tv->tv_sec;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000764
765 if (us >= 1000000) {
766 t++;
767 us -= 1000000;
768 }
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200769 if (local)
770 tm = localtime(&t);
771 else
772 tm = gmtime(&t);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000773
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200774 sprintf(buf, "%02u:%02u:%02u", tm->tm_hour, tm->tm_min, tm->tm_sec);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000775 if (s->prec)
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200776 sprintf(buf+8, ".%0*u", s->prec, us / s->scale);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000777 put(buf);
778}
779
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200780static void FAST_FUNC collect_time(time_stat *s)
781{
782 collect_tv(s, &G.tv, /*local:*/ 1);
783}
784
785static void FAST_FUNC collect_monotonic(time_stat *s)
786{
787 struct timeval tv_mono;
788
789 tv_mono.tv_sec = G.tv.tv_sec - G.start.tv_sec;
790#if 0 /* Do we want this? */
791 if (tv_mono.tv_sec < 0) {
792 /* Time went backwards, reset start time to "now" */
793 tv_mono.tv_sec = 0;
794 G.start = G.tv;
795 }
796#endif
797 tv_mono.tv_usec = G.tv.tv_usec - G.start.tv_usec;
798 if ((int32_t)tv_mono.tv_usec < 0) {
799 tv_mono.tv_usec += 1000000;
800 tv_mono.tv_sec--;
801 }
802 collect_tv(s, &tv_mono, /*local:*/ 0);
803}
804
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000805static s_stat* init_time(const char *param)
806{
807 int prec;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000808 time_stat *s = xzalloc(sizeof(*s));
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000809
810 s->collect = collect_time;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000811 prec = param[0] - '0';
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000812 if (prec < 0) prec = 0;
813 else if (prec > 6) prec = 6;
814 s->prec = prec;
815 s->scale = 1;
816 while (prec++ < 6)
817 s->scale *= 10;
818 return (s_stat*)s;
819}
820
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200821static s_stat* init_monotonic(const char *param)
822{
823 time_stat *s = (void*)init_time(param);
824 s->collect = collect_monotonic;
825 return (s_stat*)s;
826}
827
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200828static void FAST_FUNC collect_info(s_stat *s)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000829{
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000830 gen ^= 1;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000831 while (s) {
832 put(s->label);
833 s->collect(s);
834 s = s->next;
835 }
836}
837
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000838typedef s_stat* init_func(const char *param);
839
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200840static const char options[] ALIGN1 = "ncmsfixptTbr";
Denys Vlasenko965b7952020-11-30 13:03:03 +0100841static init_func *const init_functions[] ALIGN_PTR = {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000842 init_if,
843 init_cpu,
844 init_mem,
845 init_swp,
846 init_fd,
847 init_int,
848 init_ctx,
849 init_fork,
850 init_time,
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200851 init_monotonic,
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000852 init_blk,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000853 init_cr
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000854};
855
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000856int nmeter_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100857int nmeter_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000858{
859 char buf[32];
860 s_stat *first = NULL;
861 s_stat *last = NULL;
862 s_stat *s;
Denys Vlasenkoa6041862010-12-13 15:17:22 +0100863 char *opt_d;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000864 char *cur, *prev;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000865
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000866 INIT_G();
867
868 xchdir("/proc");
869
Denis Vlasenkocd785fb2008-08-14 21:57:43 +0000870 if (open_read_close("version", buf, sizeof(buf)-1) > 0) {
871 buf[sizeof(buf)-1] = '\0';
872 is26 = (strstr(buf, " 2.4.") == NULL);
873 }
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000874
Denys Vlasenko41b1e2c2016-03-15 16:13:23 +0100875 if (getopt32(argv, "d:", &opt_d)) {
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +0100876 G.delta = xatoi(opt_d) * 1000;
877 G.deltanz = G.delta > 0 ? G.delta : 1;
878 need_seconds = (1000000 % G.deltanz) != 0;
Denys Vlasenko41b1e2c2016-03-15 16:13:23 +0100879 }
Denys Vlasenkoa6041862010-12-13 15:17:22 +0100880 argv += optind;
881
882 if (!argv[0])
883 bb_show_usage();
884
885 // Can use argv[0] directly, but this will mess up
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000886 // parameters as seen by e.g. ps. Making a copy...
Denys Vlasenkoa6041862010-12-13 15:17:22 +0100887 cur = xstrdup(argv[0]);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000888 while (1) {
889 char *param, *p;
890 prev = cur;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000891 again:
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000892 cur = strchr(cur, '%');
893 if (!cur)
894 break;
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000895 if (cur[1] == '%') { // %%
Denis Vlasenko0f293b92008-07-22 20:16:55 +0000896 overlapping_strcpy(cur, cur + 1);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000897 cur++;
898 goto again;
899 }
900 *cur++ = '\0'; // overwrite %
901 if (cur[0] == '[') {
902 // format: %[foptstring]
903 cur++;
904 p = strchr(options, cur[0]);
905 param = cur+1;
906 while (cur[0] != ']') {
907 if (!cur[0])
908 bb_show_usage();
909 cur++;
910 }
911 *cur++ = '\0'; // overwrite [
912 } else {
913 // format: %NNNNNNf
914 param = cur;
915 while (cur[0] >= '0' && cur[0] <= '9')
916 cur++;
917 if (!cur[0])
918 bb_show_usage();
919 p = strchr(options, cur[0]);
920 *cur++ = '\0'; // overwrite format char
921 }
922 if (!p)
923 bb_show_usage();
924 s = init_functions[p-options](param);
925 if (s) {
926 s->label = prev;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000927 /*s->next = NULL; - all initXXX funcs use xzalloc */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000928 if (!first)
929 first = s;
930 else
931 last->next = s;
932 last = s;
933 } else {
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +0100934 // %r option. remove it from string
935 overlapping_strcpy(prev + strlen(prev), cur);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000936 cur = prev;
937 }
938 }
939 if (prev[0]) {
940 s = init_literal();
941 s->label = prev;
Denis Vlasenko333aa0c2008-06-28 23:30:37 +0000942 /*s->next = NULL; - all initXXX funcs use xzalloc */
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000943 if (!first)
944 first = s;
945 else
946 last->next = s;
947 last = s;
948 }
949
950 // Generate first samples but do not print them, they're bogus
951 collect_info(first);
952 reset_outbuf();
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200953
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +0100954 if (G.delta >= 0) {
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100955 xgettimeofday(&G.tv);
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200956 usleep(G.delta > 1000000 ? 1000000 : G.delta - G.tv.tv_usec % G.deltanz);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000957 }
958
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100959 xgettimeofday(&G.start);
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200960 G.tv = G.start;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000961 while (1) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000962 collect_info(first);
Denys Vlasenkoa63e2a82016-03-15 16:06:29 +0100963 put_c(G.final_char);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000964 print_outbuf();
965
966 // Negative delta -> no usleep at all
967 // This will hog the CPU but you can have REALLY GOOD
968 // time resolution ;)
969 // TODO: detect and avoid useless updates
970 // (like: nothing happens except time)
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +0100971 if (G.delta >= 0) {
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000972 int rem;
973 // can be commented out, will sacrifice sleep time precision a bit
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100974 xgettimeofday(&G.tv);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000975 if (need_seconds)
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200976 rem = G.delta - ((ullong)G.tv.tv_sec*1000000 + G.tv.tv_usec) % G.deltanz;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000977 else
Denys Vlasenko4789c7c2020-06-10 15:37:39 +0200978 rem = G.delta - (unsigned)G.tv.tv_usec % G.deltanz;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000979 // Sometimes kernel wakes us up just a tiny bit earlier than asked
980 // Do not go to very short sleep in this case
Denys Vlasenko0d1b71e2016-03-15 17:54:17 +0100981 if (rem < (unsigned)G.delta / 128) {
982 rem += G.delta;
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000983 }
984 usleep(rem);
985 }
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100986 xgettimeofday(&G.tv);
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000987 }
988
Denis Vlasenko199c0d52007-05-30 14:48:38 +0000989 /*return 0;*/
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000990}