2006-10-12 02:29:02 +05:30
|
|
|
/*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
|
|
|
*
|
|
|
|
* Based on nanotop.c from floppyfw project
|
|
|
|
*
|
2010-08-17 00:12:14 +05:30
|
|
|
* Contact me: vda.linux@googlemail.com
|
2010-08-16 23:44:46 +05:30
|
|
|
*/
|
2010-12-13 19:47:22 +05:30
|
|
|
//config:config NMETER
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "nmeter (11 kb)"
|
2010-12-13 19:47:22 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Prints selected system stats continuously, one line per update.
|
2010-12-13 19:47:22 +05:30
|
|
|
|
2011-01-18 18:28:01 +05:30
|
|
|
//applet:IF_NMETER(APPLET(nmeter, BB_DIR_USR_BIN, BB_SUID_DROP))
|
2010-12-13 19:47:22 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_NMETER) += nmeter.o
|
|
|
|
|
|
|
|
//usage:#define nmeter_trivial_usage
|
|
|
|
//usage: "[-d MSEC] FORMAT_STRING"
|
|
|
|
//usage:#define nmeter_full_usage "\n\n"
|
|
|
|
//usage: "Monitor system in real time"
|
|
|
|
//usage: "\n"
|
2016-03-15 22:24:17 +05:30
|
|
|
//usage: "\n -d MSEC Milliseconds between updates, default:1000, none:-1"
|
2010-12-13 19:47:22 +05:30
|
|
|
//usage: "\n"
|
|
|
|
//usage: "\nFormat specifiers:"
|
2017-01-21 08:16:35 +05:30
|
|
|
//usage: "\n %Nc or %[cN] CPU. N - bar size (default 10)"
|
2010-12-13 19:47:22 +05:30
|
|
|
//usage: "\n (displays: S:system U:user N:niced D:iowait I:irq i:softirq)"
|
2011-05-26 12:30:23 +05:30
|
|
|
//usage: "\n %[nINTERFACE] Network INTERFACE"
|
|
|
|
//usage: "\n %m Allocated memory"
|
|
|
|
//usage: "\n %[mf] Free memory"
|
|
|
|
//usage: "\n %[mt] Total memory"
|
|
|
|
//usage: "\n %s Allocated swap"
|
|
|
|
//usage: "\n %f Number of used file descriptors"
|
|
|
|
//usage: "\n %Ni Total/specific IRQ rate"
|
|
|
|
//usage: "\n %x Context switch rate"
|
|
|
|
//usage: "\n %p Forks"
|
|
|
|
//usage: "\n %[pn] # of processes"
|
|
|
|
//usage: "\n %b Block io"
|
|
|
|
//usage: "\n %Nt Time (with N decimal points)"
|
2010-12-13 19:47:22 +05:30
|
|
|
//usage: "\n %r Print <cr> instead of <lf> at EOL"
|
|
|
|
|
2006-10-12 02:29:02 +05:30
|
|
|
//TODO:
|
|
|
|
// simplify code
|
|
|
|
// /proc/locks
|
|
|
|
// /proc/stat:
|
|
|
|
// disk_io: (3,0):(22272,17897,410702,4375,54750)
|
|
|
|
// btime 1059401962
|
2007-06-17 06:05:15 +05:30
|
|
|
//TODO: use sysinfo libc call/syscall, if appropriate
|
|
|
|
// (faster than open/read/close):
|
|
|
|
// sysinfo({uptime=15017, loads=[5728, 15040, 16480]
|
|
|
|
// totalram=2107416576, freeram=211525632, sharedram=0, bufferram=157204480}
|
|
|
|
// totalswap=134209536, freeswap=134209536, procs=157})
|
2006-10-12 02:29:02 +05:30
|
|
|
|
2007-06-17 06:05:15 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2006-10-12 02:29:02 +05:30
|
|
|
|
|
|
|
typedef unsigned long long ullong;
|
|
|
|
|
2008-09-26 16:42:09 +05:30
|
|
|
enum { /* Preferably use powers of 2 */
|
|
|
|
PROC_MIN_FILE_SIZE = 256,
|
|
|
|
PROC_MAX_FILE_SIZE = 16 * 1024,
|
|
|
|
};
|
2006-10-12 02:29:02 +05:30
|
|
|
|
|
|
|
typedef struct proc_file {
|
|
|
|
char *file;
|
2008-09-26 16:42:09 +05:30
|
|
|
int file_sz;
|
2007-05-30 20:18:38 +05:30
|
|
|
smallint last_gen;
|
2006-10-12 02:29:02 +05:30
|
|
|
} proc_file;
|
|
|
|
|
2007-05-30 20:18:38 +05:30
|
|
|
static const char *const proc_name[] = {
|
|
|
|
"stat", // Must match the order of proc_file's!
|
|
|
|
"loadavg",
|
|
|
|
"net/dev",
|
|
|
|
"meminfo",
|
|
|
|
"diskstats",
|
2007-08-13 02:28:27 +05:30
|
|
|
"sys/fs/file-nr"
|
2007-05-30 20:18:38 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
struct globals {
|
|
|
|
// Sample generation flip-flop
|
|
|
|
smallint gen;
|
|
|
|
// Linux 2.6? (otherwise assumes 2.4)
|
|
|
|
smallint is26;
|
|
|
|
// 1 if sample delay is not an integer fraction of a second
|
|
|
|
smallint need_seconds;
|
2016-03-15 20:36:29 +05:30
|
|
|
char final_char;
|
2007-05-30 20:18:38 +05:30
|
|
|
char *cur_outbuf;
|
|
|
|
int delta;
|
2016-03-15 22:24:17 +05:30
|
|
|
unsigned deltanz;
|
2007-05-30 20:18:38 +05:30
|
|
|
struct timeval tv;
|
|
|
|
#define first_proc_file proc_stat
|
|
|
|
proc_file proc_stat; // Must match the order of proc_name's!
|
|
|
|
proc_file proc_loadavg;
|
|
|
|
proc_file proc_net_dev;
|
|
|
|
proc_file proc_meminfo;
|
|
|
|
proc_file proc_diskstats;
|
|
|
|
proc_file proc_sys_fs_filenr;
|
|
|
|
};
|
|
|
|
#define G (*ptr_to_globals)
|
|
|
|
#define gen (G.gen )
|
|
|
|
#define is26 (G.is26 )
|
|
|
|
#define need_seconds (G.need_seconds )
|
|
|
|
#define cur_outbuf (G.cur_outbuf )
|
|
|
|
#define tv (G.tv )
|
|
|
|
#define proc_stat (G.proc_stat )
|
|
|
|
#define proc_loadavg (G.proc_loadavg )
|
|
|
|
#define proc_net_dev (G.proc_net_dev )
|
|
|
|
#define proc_meminfo (G.proc_meminfo )
|
|
|
|
#define proc_diskstats (G.proc_diskstats )
|
|
|
|
#define proc_sys_fs_filenr (G.proc_sys_fs_filenr)
|
2016-04-21 22:08:51 +05:30
|
|
|
#define outbuf bb_common_bufsiz1
|
2007-05-30 20:18:38 +05:30
|
|
|
#define INIT_G() do { \
|
2016-04-21 22:08:51 +05:30
|
|
|
setup_common_bufsiz(); \
|
2008-02-28 00:11:59 +05:30
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
|
|
|
cur_outbuf = outbuf; \
|
2016-03-15 20:36:29 +05:30
|
|
|
G.final_char = '\n'; \
|
2016-03-15 22:24:17 +05:30
|
|
|
G.deltanz = G.delta = 1000000; \
|
2008-02-28 00:11:59 +05:30
|
|
|
} while (0)
|
2006-10-12 02:29:02 +05:30
|
|
|
|
|
|
|
static inline void reset_outbuf(void)
|
|
|
|
{
|
|
|
|
cur_outbuf = outbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int outbuf_count(void)
|
|
|
|
{
|
|
|
|
return cur_outbuf - outbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_outbuf(void)
|
|
|
|
{
|
|
|
|
int sz = cur_outbuf - outbuf;
|
|
|
|
if (sz > 0) {
|
2008-05-19 15:18:17 +05:30
|
|
|
xwrite(STDOUT_FILENO, outbuf, sz);
|
2006-10-12 02:29:02 +05:30
|
|
|
cur_outbuf = outbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put(const char *s)
|
|
|
|
{
|
2016-03-15 19:58:49 +05:30
|
|
|
char *p = cur_outbuf;
|
2016-04-21 22:08:51 +05:30
|
|
|
int sz = outbuf + COMMON_BUFSIZE - p;
|
2016-03-15 19:58:49 +05:30
|
|
|
while (*s && --sz >= 0)
|
|
|
|
*p++ = *s++;
|
|
|
|
cur_outbuf = p;
|
2006-10-12 02:29:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void put_c(char c)
|
|
|
|
{
|
2016-04-21 22:08:51 +05:30
|
|
|
if (cur_outbuf < outbuf + COMMON_BUFSIZE)
|
2006-10-12 02:29:02 +05:30
|
|
|
*cur_outbuf++ = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put_question_marks(int count)
|
|
|
|
{
|
|
|
|
while (count--)
|
|
|
|
put_c('?');
|
|
|
|
}
|
|
|
|
|
2008-09-26 16:42:09 +05:30
|
|
|
static void readfile_z(proc_file *pf, const char* fname)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
2007-05-30 20:18:38 +05:30
|
|
|
// open_read_close() will do two reads in order to be sure we are at EOF,
|
|
|
|
// and we don't need/want that.
|
2008-09-26 16:42:09 +05:30
|
|
|
int fd;
|
|
|
|
int sz, rdsz;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
sz = pf->file_sz;
|
|
|
|
buf = pf->file;
|
|
|
|
if (!buf) {
|
|
|
|
buf = xmalloc(PROC_MIN_FILE_SIZE);
|
|
|
|
sz = PROC_MIN_FILE_SIZE;
|
|
|
|
}
|
|
|
|
again:
|
|
|
|
fd = xopen(fname, O_RDONLY);
|
2007-05-30 20:18:38 +05:30
|
|
|
buf[0] = '\0';
|
2008-09-26 16:42:09 +05:30
|
|
|
rdsz = read(fd, buf, sz-1);
|
2008-06-30 13:03:52 +05:30
|
|
|
close(fd);
|
2008-09-26 16:42:09 +05:30
|
|
|
if (rdsz > 0) {
|
|
|
|
if (rdsz == sz-1 && sz < PROC_MAX_FILE_SIZE) {
|
|
|
|
sz *= 2;
|
|
|
|
buf = xrealloc(buf, sz);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
buf[rdsz] = '\0';
|
|
|
|
}
|
|
|
|
pf->file_sz = sz;
|
|
|
|
pf->file = buf;
|
2006-10-12 02:29:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static const char* get_file(proc_file *pf)
|
|
|
|
{
|
2007-05-30 20:18:38 +05:30
|
|
|
if (pf->last_gen != gen) {
|
|
|
|
pf->last_gen = gen;
|
2008-09-26 16:42:09 +05:30
|
|
|
readfile_z(pf, proc_name[pf - &first_proc_file]);
|
2006-10-12 02:29:02 +05:30
|
|
|
}
|
|
|
|
return pf->file;
|
|
|
|
}
|
|
|
|
|
2008-07-29 15:46:05 +05:30
|
|
|
static ullong read_after_slash(const char *p)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
p = strchr(p, '/');
|
|
|
|
if (!p) return 0;
|
|
|
|
return strtoull(p+1, NULL, 10);
|
|
|
|
}
|
|
|
|
|
2016-03-15 20:22:40 +05:30
|
|
|
enum conv_type {
|
|
|
|
conv_decimal = 0,
|
|
|
|
conv_slash = 1
|
|
|
|
};
|
2006-10-12 02:29:02 +05:30
|
|
|
|
|
|
|
// Reads decimal values from line. Values start after key, for example:
|
|
|
|
// "cpu 649369 0 341297 4336769..." - key is "cpu" here.
|
2016-03-15 20:22:40 +05:30
|
|
|
// Values are stored in vec[].
|
|
|
|
// posbits is a bit lit of positions we are interested in.
|
|
|
|
// for example: 00100110 - we want 1st, 2nd and 5th value.
|
|
|
|
// posbits.bit0 encodes conversion type.
|
|
|
|
static int rdval(const char* p, const char* key, ullong *vec, long posbits)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
2016-03-15 20:22:40 +05:30
|
|
|
unsigned curpos;
|
2006-10-12 02:29:02 +05:30
|
|
|
|
|
|
|
p = strstr(p, key);
|
|
|
|
if (!p) return 1;
|
|
|
|
|
|
|
|
p += strlen(key);
|
2016-03-15 20:22:40 +05:30
|
|
|
curpos = 1 << 1;
|
2006-10-12 02:29:02 +05:30
|
|
|
while (1) {
|
|
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
|
|
if (*p == '\n' || *p == '\0') break;
|
|
|
|
|
2016-03-15 20:22:40 +05:30
|
|
|
if (curpos & posbits) { // read this value
|
|
|
|
*vec++ = (posbits & 1) == conv_decimal ?
|
2006-10-12 02:29:02 +05:30
|
|
|
strtoull(p, NULL, 10) :
|
|
|
|
read_after_slash(p);
|
2016-03-15 20:22:40 +05:30
|
|
|
posbits -= curpos;
|
|
|
|
if (posbits <= 1)
|
2016-03-15 19:52:42 +05:30
|
|
|
return 0;
|
2006-10-12 02:29:02 +05:30
|
|
|
}
|
2016-03-15 20:22:40 +05:30
|
|
|
while (*p > ' ') // skip over the value
|
|
|
|
p++;
|
|
|
|
curpos <<= 1;
|
2006-10-12 02:29:02 +05:30
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses files with lines like "... ... ... 3/148 ...."
|
2016-03-15 20:22:40 +05:30
|
|
|
static int rdval_loadavg(const char* p, ullong *vec, long posbits)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
int result;
|
2016-03-15 20:22:40 +05:30
|
|
|
result = rdval(p, "", vec, posbits | conv_slash);
|
2006-10-12 02:29:02 +05:30
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses /proc/diskstats
|
2013-01-14 20:27:44 +05:30
|
|
|
// 1 2 3 4 5 6(rd) 7 8 9 10(wr) 11 12 13 14
|
2006-10-12 02:29:02 +05:30
|
|
|
// 3 0 hda 51292 14441 841783 926052 25717 79650 843256 3029804 0 148459 3956933
|
|
|
|
// 3 1 hda1 0 0 0 0 <- ignore if only 4 fields
|
2011-11-02 04:04:46 +05:30
|
|
|
// Linux 3.0 (maybe earlier) started printing full stats for hda1 too.
|
|
|
|
// Had to add code which skips such devices.
|
2006-10-12 02:29:02 +05:30
|
|
|
static int rdval_diskstats(const char* p, ullong *vec)
|
|
|
|
{
|
2011-11-02 04:04:46 +05:30
|
|
|
char devname[32];
|
|
|
|
unsigned devname_len = 0;
|
|
|
|
int value_idx = 0;
|
|
|
|
|
2006-10-12 02:29:02 +05:30
|
|
|
vec[0] = 0;
|
|
|
|
vec[1] = 0;
|
|
|
|
while (1) {
|
2011-11-02 04:04:46 +05:30
|
|
|
value_idx++;
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
2006-10-12 02:29:02 +05:30
|
|
|
if (*p == '\n') {
|
2011-11-02 04:04:46 +05:30
|
|
|
value_idx = 0;
|
2006-10-12 02:29:02 +05:30
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-02 04:04:46 +05:30
|
|
|
if (value_idx == 3) {
|
|
|
|
char *end = strchrnul(p, ' ');
|
|
|
|
/* If this a hda1-like device (same prefix as last one + digit)? */
|
|
|
|
if (devname_len && strncmp(devname, p, devname_len) == 0 && isdigit(p[devname_len])) {
|
|
|
|
p = end;
|
|
|
|
goto skip_line; /* skip entire line */
|
|
|
|
}
|
|
|
|
/* It is not. Remember the name for future checks */
|
|
|
|
devname_len = end - p;
|
|
|
|
if (devname_len > sizeof(devname)-1)
|
|
|
|
devname_len = sizeof(devname)-1;
|
|
|
|
strncpy(devname, p, devname_len);
|
|
|
|
/* devname[devname_len] = '\0'; - not really needed */
|
|
|
|
p = end;
|
|
|
|
} else
|
|
|
|
if (value_idx == 6) {
|
|
|
|
// TODO: *sectorsize (don't know how to find out sectorsize)
|
|
|
|
vec[0] += strtoull(p, NULL, 10);
|
|
|
|
} else
|
|
|
|
if (value_idx == 10) {
|
|
|
|
// TODO: *sectorsize (don't know how to find out sectorsize)
|
2006-10-12 02:29:02 +05:30
|
|
|
vec[1] += strtoull(p, NULL, 10);
|
2011-11-02 04:04:46 +05:30
|
|
|
skip_line:
|
|
|
|
while (*p != '\n' && *p != '\0')
|
|
|
|
p++;
|
2006-10-12 02:29:02 +05:30
|
|
|
continue;
|
|
|
|
}
|
2011-11-02 04:04:46 +05:30
|
|
|
while ((unsigned char)(*p) > ' ') // skip over value
|
|
|
|
p++;
|
2006-10-12 02:29:02 +05:30
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void scale(ullong ul)
|
|
|
|
{
|
|
|
|
char buf[5];
|
2008-01-06 08:56:53 +05:30
|
|
|
|
|
|
|
/* see http://en.wikipedia.org/wiki/Tera */
|
2013-09-06 16:23:14 +05:30
|
|
|
smart_ulltoa4(ul, buf, " kmgtpezy")[0] = '\0';
|
2006-10-12 02:29:02 +05:30
|
|
|
put(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define S_STAT(a) \
|
|
|
|
typedef struct a { \
|
|
|
|
struct s_stat *next; \
|
2009-06-05 15:36:05 +05:30
|
|
|
void (*collect)(struct a *s) FAST_FUNC; \
|
2006-10-12 02:29:02 +05:30
|
|
|
const char *label;
|
|
|
|
#define S_STAT_END(a) } a;
|
|
|
|
|
|
|
|
S_STAT(s_stat)
|
|
|
|
S_STAT_END(s_stat)
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static void FAST_FUNC collect_literal(s_stat *s UNUSED_PARAM)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static s_stat* init_literal(void)
|
|
|
|
{
|
2008-06-29 05:03:02 +05:30
|
|
|
s_stat *s = xzalloc(sizeof(*s));
|
2006-10-12 02:29:02 +05:30
|
|
|
s->collect = collect_literal;
|
|
|
|
return (s_stat*)s;
|
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static s_stat* init_cr(const char *param UNUSED_PARAM)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
2016-03-15 20:36:29 +05:30
|
|
|
G.final_char = '\r';
|
|
|
|
return NULL;
|
2006-10-12 02:29:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// user nice system idle iowait irq softirq (last 3 only in 2.6)
|
|
|
|
//cpu 649369 0 341297 4336769 11640 7122 1183
|
|
|
|
//cpuN 649369 0 341297 4336769 11640 7122 1183
|
|
|
|
enum { CPU_FIELDCNT = 7 };
|
|
|
|
S_STAT(cpu_stat)
|
|
|
|
ullong old[CPU_FIELDCNT];
|
2019-08-06 20:26:00 +05:30
|
|
|
unsigned bar_sz;
|
2016-03-15 20:36:29 +05:30
|
|
|
char bar[1];
|
2006-10-12 02:29:02 +05:30
|
|
|
S_STAT_END(cpu_stat)
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static void FAST_FUNC collect_cpu(cpu_stat *s)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
ullong data[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
unsigned frac[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
ullong all = 0;
|
2019-08-06 20:26:00 +05:30
|
|
|
unsigned norm_all = 0;
|
|
|
|
unsigned bar_sz = s->bar_sz;
|
2006-10-12 02:29:02 +05:30
|
|
|
char *bar = s->bar;
|
|
|
|
int i;
|
|
|
|
|
2016-03-15 20:22:40 +05:30
|
|
|
if (rdval(get_file(&proc_stat), "cpu ", data, 0
|
|
|
|
| (1 << 1)
|
|
|
|
| (1 << 2)
|
|
|
|
| (1 << 3)
|
|
|
|
| (1 << 4)
|
|
|
|
| (1 << 5)
|
|
|
|
| (1 << 6)
|
|
|
|
| (1 << 7))
|
|
|
|
) {
|
2006-10-12 02:29:02 +05:30
|
|
|
put_question_marks(bar_sz);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-05-30 20:18:38 +05:30
|
|
|
for (i = 0; i < CPU_FIELDCNT; i++) {
|
2006-10-12 02:29:02 +05:30
|
|
|
ullong old = s->old[i];
|
|
|
|
if (data[i] < old) old = data[i]; //sanitize
|
|
|
|
s->old[i] = data[i];
|
|
|
|
all += (data[i] -= old);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (all) {
|
2007-05-30 20:18:38 +05:30
|
|
|
for (i = 0; i < CPU_FIELDCNT; i++) {
|
2006-10-12 02:29:02 +05:30
|
|
|
ullong t = bar_sz * data[i];
|
|
|
|
norm_all += data[i] = t / all;
|
|
|
|
frac[i] = t % all;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (norm_all < bar_sz) {
|
|
|
|
unsigned max = frac[0];
|
|
|
|
int pos = 0;
|
2007-05-30 20:18:38 +05:30
|
|
|
for (i = 1; i < CPU_FIELDCNT; i++) {
|
2006-10-12 02:29:02 +05:30
|
|
|
if (frac[i] > max) max = frac[i], pos = i;
|
|
|
|
}
|
|
|
|
frac[pos] = 0; //avoid bumping up same value twice
|
|
|
|
data[pos]++;
|
|
|
|
norm_all++;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(bar, '.', bar_sz);
|
|
|
|
memset(bar, 'S', data[2]); bar += data[2]; //sys
|
|
|
|
memset(bar, 'U', data[0]); bar += data[0]; //usr
|
|
|
|
memset(bar, 'N', data[1]); bar += data[1]; //nice
|
|
|
|
memset(bar, 'D', data[4]); bar += data[4]; //iowait
|
|
|
|
memset(bar, 'I', data[5]); bar += data[5]; //irq
|
|
|
|
memset(bar, 'i', data[6]); bar += data[6]; //softirq
|
|
|
|
} else {
|
|
|
|
memset(bar, '?', bar_sz);
|
|
|
|
}
|
|
|
|
put(s->bar);
|
|
|
|
}
|
|
|
|
|
|
|
|
static s_stat* init_cpu(const char *param)
|
|
|
|
{
|
|
|
|
int sz;
|
2016-03-15 20:36:29 +05:30
|
|
|
cpu_stat *s;
|
2019-08-06 20:26:00 +05:30
|
|
|
sz = param[0] ? strtoul(param, NULL, 0) : 10;
|
|
|
|
if (sz <= 0) sz = 1;
|
2006-10-12 02:29:02 +05:30
|
|
|
if (sz > 1000) sz = 1000;
|
2016-03-15 20:36:29 +05:30
|
|
|
s = xzalloc(sizeof(*s) + sz);
|
2008-06-29 05:00:37 +05:30
|
|
|
/*s->bar[sz] = '\0'; - xzalloc did it */
|
2006-10-12 02:29:02 +05:30
|
|
|
s->bar_sz = sz;
|
2016-03-15 20:36:29 +05:30
|
|
|
s->collect = collect_cpu;
|
2006-10-12 02:29:02 +05:30
|
|
|
return (s_stat*)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
S_STAT(int_stat)
|
|
|
|
ullong old;
|
|
|
|
int no;
|
|
|
|
S_STAT_END(int_stat)
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static void FAST_FUNC collect_int(int_stat *s)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
ullong data[1];
|
|
|
|
ullong old;
|
|
|
|
|
2016-03-15 20:22:40 +05:30
|
|
|
if (rdval(get_file(&proc_stat), "intr", data, 1 << s->no)) {
|
2006-10-12 02:29:02 +05:30
|
|
|
put_question_marks(4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
old = s->old;
|
|
|
|
if (data[0] < old) old = data[0]; //sanitize
|
|
|
|
s->old = data[0];
|
|
|
|
scale(data[0] - old);
|
|
|
|
}
|
|
|
|
|
|
|
|
static s_stat* init_int(const char *param)
|
|
|
|
{
|
2008-06-29 05:00:37 +05:30
|
|
|
int_stat *s = xzalloc(sizeof(*s));
|
2006-10-12 02:29:02 +05:30
|
|
|
s->collect = collect_int;
|
2008-06-29 05:00:37 +05:30
|
|
|
if (param[0] == '\0') {
|
2006-10-12 02:29:02 +05:30
|
|
|
s->no = 1;
|
|
|
|
} else {
|
2010-08-12 17:44:45 +05:30
|
|
|
int n = xatoi_positive(param);
|
2008-06-29 05:00:37 +05:30
|
|
|
s->no = n + 2;
|
2006-10-12 02:29:02 +05:30
|
|
|
}
|
|
|
|
return (s_stat*)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
S_STAT(ctx_stat)
|
|
|
|
ullong old;
|
|
|
|
S_STAT_END(ctx_stat)
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static void FAST_FUNC collect_ctx(ctx_stat *s)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
ullong data[1];
|
|
|
|
ullong old;
|
|
|
|
|
2016-03-15 20:22:40 +05:30
|
|
|
if (rdval(get_file(&proc_stat), "ctxt", data, 1 << 1)) {
|
2006-10-12 02:29:02 +05:30
|
|
|
put_question_marks(4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
old = s->old;
|
|
|
|
if (data[0] < old) old = data[0]; //sanitize
|
|
|
|
s->old = data[0];
|
|
|
|
scale(data[0] - old);
|
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static s_stat* init_ctx(const char *param UNUSED_PARAM)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
2008-06-29 05:00:37 +05:30
|
|
|
ctx_stat *s = xzalloc(sizeof(*s));
|
2006-10-12 02:29:02 +05:30
|
|
|
s->collect = collect_ctx;
|
|
|
|
return (s_stat*)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
S_STAT(blk_stat)
|
|
|
|
const char* lookfor;
|
|
|
|
ullong old[2];
|
|
|
|
S_STAT_END(blk_stat)
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static void FAST_FUNC collect_blk(blk_stat *s)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
ullong data[2];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (is26) {
|
|
|
|
i = rdval_diskstats(get_file(&proc_diskstats), data);
|
|
|
|
} else {
|
2016-03-15 20:22:40 +05:30
|
|
|
i = rdval(get_file(&proc_stat), s->lookfor, data, 0
|
|
|
|
| (1 << 1)
|
|
|
|
| (1 << 2)
|
|
|
|
);
|
2006-10-12 02:29:02 +05:30
|
|
|
// Linux 2.4 reports bio in Kbytes, convert to sectors:
|
|
|
|
data[0] *= 2;
|
|
|
|
data[1] *= 2;
|
|
|
|
}
|
|
|
|
if (i) {
|
|
|
|
put_question_marks(9);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<2; i++) {
|
|
|
|
ullong old = s->old[i];
|
|
|
|
if (data[i] < old) old = data[i]; //sanitize
|
|
|
|
s->old[i] = data[i];
|
|
|
|
data[i] -= old;
|
|
|
|
}
|
|
|
|
scale(data[0]*512); // TODO: *sectorsize
|
|
|
|
put_c(' ');
|
|
|
|
scale(data[1]*512);
|
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static s_stat* init_blk(const char *param UNUSED_PARAM)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
2008-06-29 05:00:37 +05:30
|
|
|
blk_stat *s = xzalloc(sizeof(*s));
|
2006-10-12 02:29:02 +05:30
|
|
|
s->collect = collect_blk;
|
|
|
|
s->lookfor = "page";
|
|
|
|
return (s_stat*)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
S_STAT(fork_stat)
|
|
|
|
ullong old;
|
|
|
|
S_STAT_END(fork_stat)
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static void FAST_FUNC collect_thread_nr(fork_stat *s UNUSED_PARAM)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
ullong data[1];
|
|
|
|
|
2016-03-15 20:22:40 +05:30
|
|
|
if (rdval_loadavg(get_file(&proc_loadavg), data, 1 << 4)) {
|
2006-10-12 02:29:02 +05:30
|
|
|
put_question_marks(4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
scale(data[0]);
|
|
|
|
}
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static void FAST_FUNC collect_fork(fork_stat *s)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
ullong data[1];
|
|
|
|
ullong old;
|
|
|
|
|
2016-03-15 20:22:40 +05:30
|
|
|
if (rdval(get_file(&proc_stat), "processes", data, 1 << 1)) {
|
2006-10-12 02:29:02 +05:30
|
|
|
put_question_marks(4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
old = s->old;
|
|
|
|
if (data[0] < old) old = data[0]; //sanitize
|
|
|
|
s->old = data[0];
|
|
|
|
scale(data[0] - old);
|
|
|
|
}
|
|
|
|
|
|
|
|
static s_stat* init_fork(const char *param)
|
|
|
|
{
|
2008-06-29 05:00:37 +05:30
|
|
|
fork_stat *s = xzalloc(sizeof(*s));
|
2006-10-12 02:29:02 +05:30
|
|
|
if (*param == 'n') {
|
|
|
|
s->collect = collect_thread_nr;
|
|
|
|
} else {
|
|
|
|
s->collect = collect_fork;
|
|
|
|
}
|
|
|
|
return (s_stat*)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
S_STAT(if_stat)
|
|
|
|
ullong old[4];
|
|
|
|
const char *device;
|
|
|
|
char *device_colon;
|
|
|
|
S_STAT_END(if_stat)
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static void FAST_FUNC collect_if(if_stat *s)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
ullong data[4];
|
|
|
|
int i;
|
|
|
|
|
2016-03-15 20:22:40 +05:30
|
|
|
if (rdval(get_file(&proc_net_dev), s->device_colon, data, 0
|
|
|
|
| (1 << 1)
|
|
|
|
| (1 << 3)
|
|
|
|
| (1 << 9)
|
|
|
|
| (1 << 11))
|
|
|
|
) {
|
2006-10-12 02:29:02 +05:30
|
|
|
put_question_marks(10);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<4; i++) {
|
|
|
|
ullong old = s->old[i];
|
|
|
|
if (data[i] < old) old = data[i]; //sanitize
|
|
|
|
s->old[i] = data[i];
|
|
|
|
data[i] -= old;
|
|
|
|
}
|
|
|
|
put_c(data[1] ? '*' : ' ');
|
|
|
|
scale(data[0]);
|
|
|
|
put_c(data[3] ? '*' : ' ');
|
|
|
|
scale(data[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static s_stat* init_if(const char *device)
|
|
|
|
{
|
2008-06-29 05:00:37 +05:30
|
|
|
if_stat *s = xzalloc(sizeof(*s));
|
2006-10-12 02:29:02 +05:30
|
|
|
|
|
|
|
if (!device || !device[0])
|
|
|
|
bb_show_usage();
|
|
|
|
s->collect = collect_if;
|
|
|
|
|
|
|
|
s->device = device;
|
2008-06-29 05:00:37 +05:30
|
|
|
s->device_colon = xasprintf("%s:", device);
|
2006-10-12 02:29:02 +05:30
|
|
|
return (s_stat*)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
S_STAT(mem_stat)
|
|
|
|
char opt;
|
|
|
|
S_STAT_END(mem_stat)
|
|
|
|
|
|
|
|
// "Memory" value should not include any caches.
|
|
|
|
// IOW: neither "ls -laR /" nor heavy read/write activity
|
|
|
|
// should affect it. We'd like to also include any
|
|
|
|
// long-term allocated kernel-side mem, but it is hard
|
|
|
|
// to figure out. For now, bufs, cached & slab are
|
|
|
|
// counted as "free" memory
|
|
|
|
//2.6.16:
|
|
|
|
//MemTotal: 773280 kB
|
|
|
|
//MemFree: 25912 kB - genuinely free
|
|
|
|
//Buffers: 320672 kB - cache
|
|
|
|
//Cached: 146396 kB - cache
|
|
|
|
//SwapCached: 0 kB
|
|
|
|
//Active: 183064 kB
|
|
|
|
//Inactive: 356892 kB
|
|
|
|
//HighTotal: 0 kB
|
|
|
|
//HighFree: 0 kB
|
|
|
|
//LowTotal: 773280 kB
|
|
|
|
//LowFree: 25912 kB
|
|
|
|
//SwapTotal: 131064 kB
|
|
|
|
//SwapFree: 131064 kB
|
|
|
|
//Dirty: 48 kB
|
|
|
|
//Writeback: 0 kB
|
|
|
|
//Mapped: 96620 kB
|
|
|
|
//Slab: 200668 kB - takes 7 Mb on my box fresh after boot,
|
|
|
|
// but includes dentries and inodes
|
|
|
|
// (== can take arbitrary amount of mem)
|
|
|
|
//CommitLimit: 517704 kB
|
|
|
|
//Committed_AS: 236776 kB
|
|
|
|
//PageTables: 1248 kB
|
|
|
|
//VmallocTotal: 516052 kB
|
|
|
|
//VmallocUsed: 3852 kB
|
|
|
|
//VmallocChunk: 512096 kB
|
|
|
|
//HugePages_Total: 0
|
|
|
|
//HugePages_Free: 0
|
|
|
|
//Hugepagesize: 4096 kB
|
2009-06-05 15:36:05 +05:30
|
|
|
static void FAST_FUNC collect_mem(mem_stat *s)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
ullong m_total = 0;
|
|
|
|
ullong m_free = 0;
|
|
|
|
ullong m_bufs = 0;
|
|
|
|
ullong m_cached = 0;
|
|
|
|
ullong m_slab = 0;
|
|
|
|
|
2016-03-15 20:22:40 +05:30
|
|
|
if (rdval(get_file(&proc_meminfo), "MemTotal:", &m_total, 1 << 1)) {
|
2006-10-12 02:29:02 +05:30
|
|
|
put_question_marks(4);
|
|
|
|
return;
|
|
|
|
}
|
2008-02-29 13:16:19 +05:30
|
|
|
if (s->opt == 't') {
|
2006-10-12 02:29:02 +05:30
|
|
|
scale(m_total << 10);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-15 20:22:40 +05:30
|
|
|
if (rdval(proc_meminfo.file, "MemFree:", &m_free , 1 << 1)
|
|
|
|
|| rdval(proc_meminfo.file, "Buffers:", &m_bufs , 1 << 1)
|
|
|
|
|| rdval(proc_meminfo.file, "Cached:", &m_cached, 1 << 1)
|
|
|
|
|| rdval(proc_meminfo.file, "Slab:", &m_slab , 1 << 1)
|
2006-10-12 02:29:02 +05:30
|
|
|
) {
|
|
|
|
put_question_marks(4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_free += m_bufs + m_cached + m_slab;
|
2006-12-26 16:12:51 +05:30
|
|
|
switch (s->opt) {
|
2006-10-12 02:29:02 +05:30
|
|
|
case 'f':
|
|
|
|
scale(m_free << 10); break;
|
|
|
|
default:
|
|
|
|
scale((m_total - m_free) << 10); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static s_stat* init_mem(const char *param)
|
|
|
|
{
|
2008-06-29 05:00:37 +05:30
|
|
|
mem_stat *s = xzalloc(sizeof(*s));
|
2006-10-12 02:29:02 +05:30
|
|
|
s->collect = collect_mem;
|
|
|
|
s->opt = param[0];
|
|
|
|
return (s_stat*)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
S_STAT(swp_stat)
|
|
|
|
S_STAT_END(swp_stat)
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static void FAST_FUNC collect_swp(swp_stat *s UNUSED_PARAM)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
ullong s_total[1];
|
|
|
|
ullong s_free[1];
|
2016-03-15 20:22:40 +05:30
|
|
|
if (rdval(get_file(&proc_meminfo), "SwapTotal:", s_total, 1 << 1)
|
|
|
|
|| rdval(proc_meminfo.file, "SwapFree:" , s_free, 1 << 1)
|
2006-10-12 02:29:02 +05:30
|
|
|
) {
|
|
|
|
put_question_marks(4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
scale((s_total[0]-s_free[0]) << 10);
|
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static s_stat* init_swp(const char *param UNUSED_PARAM)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
2008-06-29 05:00:37 +05:30
|
|
|
swp_stat *s = xzalloc(sizeof(*s));
|
2006-10-12 02:29:02 +05:30
|
|
|
s->collect = collect_swp;
|
|
|
|
return (s_stat*)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
S_STAT(fd_stat)
|
|
|
|
S_STAT_END(fd_stat)
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static void FAST_FUNC collect_fd(fd_stat *s UNUSED_PARAM)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
ullong data[2];
|
|
|
|
|
2016-03-15 20:22:40 +05:30
|
|
|
if (rdval(get_file(&proc_sys_fs_filenr), "", data, 0
|
|
|
|
| (1 << 1)
|
|
|
|
| (1 << 2))
|
|
|
|
) {
|
2006-10-12 02:29:02 +05:30
|
|
|
put_question_marks(4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
scale(data[0] - data[1]);
|
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static s_stat* init_fd(const char *param UNUSED_PARAM)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
2008-06-29 05:00:37 +05:30
|
|
|
fd_stat *s = xzalloc(sizeof(*s));
|
2006-10-12 02:29:02 +05:30
|
|
|
s->collect = collect_fd;
|
|
|
|
return (s_stat*)s;
|
|
|
|
}
|
|
|
|
|
|
|
|
S_STAT(time_stat)
|
2016-03-15 20:36:29 +05:30
|
|
|
unsigned prec;
|
|
|
|
unsigned scale;
|
2006-10-12 02:29:02 +05:30
|
|
|
S_STAT_END(time_stat)
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static void FAST_FUNC collect_time(time_stat *s)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
char buf[sizeof("12:34:56.123456")];
|
|
|
|
struct tm* tm;
|
2016-03-15 20:36:29 +05:30
|
|
|
unsigned us = tv.tv_usec + s->scale/2;
|
2006-10-12 02:29:02 +05:30
|
|
|
time_t t = tv.tv_sec;
|
|
|
|
|
|
|
|
if (us >= 1000000) {
|
|
|
|
t++;
|
|
|
|
us -= 1000000;
|
|
|
|
}
|
|
|
|
tm = localtime(&t);
|
|
|
|
|
|
|
|
sprintf(buf, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
|
|
|
|
if (s->prec)
|
|
|
|
sprintf(buf+8, ".%0*d", s->prec, us / s->scale);
|
|
|
|
put(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static s_stat* init_time(const char *param)
|
|
|
|
{
|
|
|
|
int prec;
|
2008-06-29 05:00:37 +05:30
|
|
|
time_stat *s = xzalloc(sizeof(*s));
|
2006-10-12 02:29:02 +05:30
|
|
|
|
|
|
|
s->collect = collect_time;
|
2008-06-29 05:00:37 +05:30
|
|
|
prec = param[0] - '0';
|
2006-10-12 02:29:02 +05:30
|
|
|
if (prec < 0) prec = 0;
|
|
|
|
else if (prec > 6) prec = 6;
|
|
|
|
s->prec = prec;
|
|
|
|
s->scale = 1;
|
|
|
|
while (prec++ < 6)
|
|
|
|
s->scale *= 10;
|
|
|
|
return (s_stat*)s;
|
|
|
|
}
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static void FAST_FUNC collect_info(s_stat *s)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
2007-05-30 20:18:38 +05:30
|
|
|
gen ^= 1;
|
2006-10-12 02:29:02 +05:30
|
|
|
while (s) {
|
|
|
|
put(s->label);
|
|
|
|
s->collect(s);
|
|
|
|
s = s->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef s_stat* init_func(const char *param);
|
|
|
|
|
2016-03-15 20:43:23 +05:30
|
|
|
static const char options[] ALIGN1 = "ncmsfixptbr";
|
2007-05-30 20:18:38 +05:30
|
|
|
static init_func *const init_functions[] = {
|
2006-10-12 02:29:02 +05:30
|
|
|
init_if,
|
|
|
|
init_cpu,
|
|
|
|
init_mem,
|
|
|
|
init_swp,
|
|
|
|
init_fd,
|
|
|
|
init_int,
|
|
|
|
init_ctx,
|
|
|
|
init_fork,
|
|
|
|
init_time,
|
|
|
|
init_blk,
|
2007-08-13 02:28:27 +05:30
|
|
|
init_cr
|
2006-10-12 02:29:02 +05:30
|
|
|
};
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int nmeter_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2010-01-04 18:45:38 +05:30
|
|
|
int nmeter_main(int argc UNUSED_PARAM, char **argv)
|
2006-10-12 02:29:02 +05:30
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
s_stat *first = NULL;
|
|
|
|
s_stat *last = NULL;
|
|
|
|
s_stat *s;
|
2010-12-13 19:47:22 +05:30
|
|
|
char *opt_d;
|
2006-10-12 02:29:02 +05:30
|
|
|
char *cur, *prev;
|
|
|
|
|
2007-05-30 20:18:38 +05:30
|
|
|
INIT_G();
|
|
|
|
|
|
|
|
xchdir("/proc");
|
|
|
|
|
2008-08-15 03:27:43 +05:30
|
|
|
if (open_read_close("version", buf, sizeof(buf)-1) > 0) {
|
|
|
|
buf[sizeof(buf)-1] = '\0';
|
|
|
|
is26 = (strstr(buf, " 2.4.") == NULL);
|
|
|
|
}
|
2006-10-12 02:29:02 +05:30
|
|
|
|
2016-03-15 20:43:23 +05:30
|
|
|
if (getopt32(argv, "d:", &opt_d)) {
|
2016-03-15 22:24:17 +05:30
|
|
|
G.delta = xatoi(opt_d) * 1000;
|
|
|
|
G.deltanz = G.delta > 0 ? G.delta : 1;
|
|
|
|
need_seconds = (1000000 % G.deltanz) != 0;
|
2016-03-15 20:43:23 +05:30
|
|
|
}
|
2010-12-13 19:47:22 +05:30
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (!argv[0])
|
|
|
|
bb_show_usage();
|
|
|
|
|
|
|
|
// Can use argv[0] directly, but this will mess up
|
2006-10-12 02:29:02 +05:30
|
|
|
// parameters as seen by e.g. ps. Making a copy...
|
2010-12-13 19:47:22 +05:30
|
|
|
cur = xstrdup(argv[0]);
|
2006-10-12 02:29:02 +05:30
|
|
|
while (1) {
|
|
|
|
char *param, *p;
|
|
|
|
prev = cur;
|
2007-05-30 20:18:38 +05:30
|
|
|
again:
|
2006-10-12 02:29:02 +05:30
|
|
|
cur = strchr(cur, '%');
|
|
|
|
if (!cur)
|
|
|
|
break;
|
2007-05-30 20:18:38 +05:30
|
|
|
if (cur[1] == '%') { // %%
|
2008-07-23 01:46:55 +05:30
|
|
|
overlapping_strcpy(cur, cur + 1);
|
2006-10-12 02:29:02 +05:30
|
|
|
cur++;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
*cur++ = '\0'; // overwrite %
|
|
|
|
if (cur[0] == '[') {
|
|
|
|
// format: %[foptstring]
|
|
|
|
cur++;
|
|
|
|
p = strchr(options, cur[0]);
|
|
|
|
param = cur+1;
|
|
|
|
while (cur[0] != ']') {
|
|
|
|
if (!cur[0])
|
|
|
|
bb_show_usage();
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
*cur++ = '\0'; // overwrite [
|
|
|
|
} else {
|
|
|
|
// format: %NNNNNNf
|
|
|
|
param = cur;
|
|
|
|
while (cur[0] >= '0' && cur[0] <= '9')
|
|
|
|
cur++;
|
|
|
|
if (!cur[0])
|
|
|
|
bb_show_usage();
|
|
|
|
p = strchr(options, cur[0]);
|
|
|
|
*cur++ = '\0'; // overwrite format char
|
|
|
|
}
|
|
|
|
if (!p)
|
|
|
|
bb_show_usage();
|
|
|
|
s = init_functions[p-options](param);
|
|
|
|
if (s) {
|
|
|
|
s->label = prev;
|
2008-06-29 05:00:37 +05:30
|
|
|
/*s->next = NULL; - all initXXX funcs use xzalloc */
|
2006-10-12 02:29:02 +05:30
|
|
|
if (!first)
|
|
|
|
first = s;
|
|
|
|
else
|
|
|
|
last->next = s;
|
|
|
|
last = s;
|
|
|
|
} else {
|
2016-03-15 22:24:17 +05:30
|
|
|
// %r option. remove it from string
|
|
|
|
overlapping_strcpy(prev + strlen(prev), cur);
|
2006-10-12 02:29:02 +05:30
|
|
|
cur = prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (prev[0]) {
|
|
|
|
s = init_literal();
|
|
|
|
s->label = prev;
|
2008-06-29 05:00:37 +05:30
|
|
|
/*s->next = NULL; - all initXXX funcs use xzalloc */
|
2006-10-12 02:29:02 +05:30
|
|
|
if (!first)
|
|
|
|
first = s;
|
|
|
|
else
|
|
|
|
last->next = s;
|
|
|
|
last = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate first samples but do not print them, they're bogus
|
|
|
|
collect_info(first);
|
|
|
|
reset_outbuf();
|
2016-03-15 22:24:17 +05:30
|
|
|
if (G.delta >= 0) {
|
2007-05-30 20:18:38 +05:30
|
|
|
gettimeofday(&tv, NULL);
|
2016-03-15 22:24:17 +05:30
|
|
|
usleep(G.delta > 1000000 ? 1000000 : G.delta - tv.tv_usec % G.deltanz);
|
2006-10-12 02:29:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2007-05-30 20:18:38 +05:30
|
|
|
gettimeofday(&tv, NULL);
|
2006-10-12 02:29:02 +05:30
|
|
|
collect_info(first);
|
2016-03-15 20:36:29 +05:30
|
|
|
put_c(G.final_char);
|
2006-10-12 02:29:02 +05:30
|
|
|
print_outbuf();
|
|
|
|
|
|
|
|
// Negative delta -> no usleep at all
|
|
|
|
// This will hog the CPU but you can have REALLY GOOD
|
|
|
|
// time resolution ;)
|
|
|
|
// TODO: detect and avoid useless updates
|
|
|
|
// (like: nothing happens except time)
|
2016-03-15 22:24:17 +05:30
|
|
|
if (G.delta >= 0) {
|
2006-10-12 02:29:02 +05:30
|
|
|
int rem;
|
|
|
|
// can be commented out, will sacrifice sleep time precision a bit
|
2007-05-30 20:18:38 +05:30
|
|
|
gettimeofday(&tv, NULL);
|
2006-10-12 02:29:02 +05:30
|
|
|
if (need_seconds)
|
2016-03-15 22:24:17 +05:30
|
|
|
rem = G.delta - ((ullong)tv.tv_sec*1000000 + tv.tv_usec) % G.deltanz;
|
2006-10-12 02:29:02 +05:30
|
|
|
else
|
2016-03-15 22:24:17 +05:30
|
|
|
rem = G.delta - (unsigned)tv.tv_usec % G.deltanz;
|
2006-10-12 02:29:02 +05:30
|
|
|
// Sometimes kernel wakes us up just a tiny bit earlier than asked
|
|
|
|
// Do not go to very short sleep in this case
|
2016-03-15 22:24:17 +05:30
|
|
|
if (rem < (unsigned)G.delta / 128) {
|
|
|
|
rem += G.delta;
|
2006-10-12 02:29:02 +05:30
|
|
|
}
|
|
|
|
usleep(rem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-30 20:18:38 +05:30
|
|
|
/*return 0;*/
|
2006-10-12 02:29:02 +05:30
|
|
|
}
|