nmeter: convert field list to bit list

function                                             old     new   delta
rdval                                                 34     157    +123
collect_int                                          123     122      -1
collect_thread_nr                                     65      62      -3
collect_blk                                          559     552      -7
collect_if                                           207     199      -8
collect_fork                                         119     111      -8
collect_ctx                                          119     111      -8
collect_fd                                            81      71     -10
collect_swp                                          120     107     -13
collect_cpu                                          623     610     -13
collect_mem                                          371     339     -32
rdval_loadavg                                         38       -     -38
vrdval                                               170       -    -170
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 1/10 up/down: 123/-311)        Total: -188 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2016-03-15 15:52:40 +01:00
parent 99c71c9e80
commit 8a26fda98c

View File

@ -208,67 +208,50 @@ static ullong read_after_slash(const char *p)
return strtoull(p+1, NULL, 10); return strtoull(p+1, NULL, 10);
} }
enum conv_type { conv_decimal, conv_slash }; enum conv_type {
conv_decimal = 0,
conv_slash = 1
};
// Reads decimal values from line. Values start after key, for example: // Reads decimal values from line. Values start after key, for example:
// "cpu 649369 0 341297 4336769..." - key is "cpu" here. // "cpu 649369 0 341297 4336769..." - key is "cpu" here.
// Values are stored in vec[]. arg_ptr has list of positions // Values are stored in vec[].
// we are interested in: for example: 1,2,5 - we want 1st, 2nd and 5th value. // posbits is a bit lit of positions we are interested in.
static int vrdval(const char* p, const char* key, // for example: 00100110 - we want 1st, 2nd and 5th value.
enum conv_type conv, ullong *vec, va_list arg_ptr) // posbits.bit0 encodes conversion type.
static int rdval(const char* p, const char* key, ullong *vec, long posbits)
{ {
int indexline; unsigned curpos;
int indexnext;
p = strstr(p, key); p = strstr(p, key);
if (!p) return 1; if (!p) return 1;
p += strlen(key); p += strlen(key);
indexline = 1; curpos = 1 << 1;
indexnext = va_arg(arg_ptr, int);
while (1) { while (1) {
while (*p == ' ' || *p == '\t') p++; while (*p == ' ' || *p == '\t') p++;
if (*p == '\n' || *p == '\0') break; if (*p == '\n' || *p == '\0') break;
if (indexline == indexnext) { // read this value if (curpos & posbits) { // read this value
*vec++ = conv==conv_decimal ? *vec++ = (posbits & 1) == conv_decimal ?
strtoull(p, NULL, 10) : strtoull(p, NULL, 10) :
read_after_slash(p); read_after_slash(p);
indexnext = va_arg(arg_ptr, int); posbits -= curpos;
if (!indexnext) if (posbits <= 1)
return 0; return 0;
} }
while (*p > ' ') p++; // skip over value while (*p > ' ') // skip over the value
indexline++; p++;
curpos <<= 1;
} }
return 0; return 0;
} }
// Parses files with lines like "cpu0 21727 0 15718 1813856 9461 10485 0 0":
// rdval(file_contents, "string_to_find", result_vector, value#, value#...)
// value# start with 1
static int rdval(const char* p, const char* key, ullong *vec, ...)
{
va_list arg_ptr;
int result;
va_start(arg_ptr, vec);
result = vrdval(p, key, conv_decimal, vec, arg_ptr);
va_end(arg_ptr);
return result;
}
// Parses files with lines like "... ... ... 3/148 ...." // Parses files with lines like "... ... ... 3/148 ...."
static int rdval_loadavg(const char* p, ullong *vec, ...) static int rdval_loadavg(const char* p, ullong *vec, long posbits)
{ {
va_list arg_ptr;
int result; int result;
result = rdval(p, "", vec, posbits | conv_slash);
va_start(arg_ptr, vec);
result = vrdval(p, "", conv_slash, vec, arg_ptr);
va_end(arg_ptr);
return result; return result;
} }
@ -397,7 +380,15 @@ static void FAST_FUNC collect_cpu(cpu_stat *s)
char *bar = s->bar; char *bar = s->bar;
int i; int i;
if (rdval(get_file(&proc_stat), "cpu ", data, 1, 2, 3, 4, 5, 6, 7, 0)) { if (rdval(get_file(&proc_stat), "cpu ", data, 0
| (1 << 1)
| (1 << 2)
| (1 << 3)
| (1 << 4)
| (1 << 5)
| (1 << 6)
| (1 << 7))
) {
put_question_marks(bar_sz); put_question_marks(bar_sz);
return; return;
} }
@ -466,7 +457,7 @@ static void FAST_FUNC collect_int(int_stat *s)
ullong data[1]; ullong data[1];
ullong old; ullong old;
if (rdval(get_file(&proc_stat), "intr", data, s->no, 0)) { if (rdval(get_file(&proc_stat), "intr", data, 1 << s->no)) {
put_question_marks(4); put_question_marks(4);
return; return;
} }
@ -500,7 +491,7 @@ static void FAST_FUNC collect_ctx(ctx_stat *s)
ullong data[1]; ullong data[1];
ullong old; ullong old;
if (rdval(get_file(&proc_stat), "ctxt", data, 1, 0)) { if (rdval(get_file(&proc_stat), "ctxt", data, 1 << 1)) {
put_question_marks(4); put_question_marks(4);
return; return;
} }
@ -532,7 +523,10 @@ static void FAST_FUNC collect_blk(blk_stat *s)
if (is26) { if (is26) {
i = rdval_diskstats(get_file(&proc_diskstats), data); i = rdval_diskstats(get_file(&proc_diskstats), data);
} else { } else {
i = rdval(get_file(&proc_stat), s->lookfor, data, 1, 2, 0); i = rdval(get_file(&proc_stat), s->lookfor, data, 0
| (1 << 1)
| (1 << 2)
);
// Linux 2.4 reports bio in Kbytes, convert to sectors: // Linux 2.4 reports bio in Kbytes, convert to sectors:
data[0] *= 2; data[0] *= 2;
data[1] *= 2; data[1] *= 2;
@ -570,7 +564,7 @@ static void FAST_FUNC collect_thread_nr(fork_stat *s UNUSED_PARAM)
{ {
ullong data[1]; ullong data[1];
if (rdval_loadavg(get_file(&proc_loadavg), data, 4, 0)) { if (rdval_loadavg(get_file(&proc_loadavg), data, 1 << 4)) {
put_question_marks(4); put_question_marks(4);
return; return;
} }
@ -582,7 +576,7 @@ static void FAST_FUNC collect_fork(fork_stat *s)
ullong data[1]; ullong data[1];
ullong old; ullong old;
if (rdval(get_file(&proc_stat), "processes", data, 1, 0)) { if (rdval(get_file(&proc_stat), "processes", data, 1 << 1)) {
put_question_marks(4); put_question_marks(4);
return; return;
} }
@ -616,7 +610,12 @@ static void FAST_FUNC collect_if(if_stat *s)
ullong data[4]; ullong data[4];
int i; int i;
if (rdval(get_file(&proc_net_dev), s->device_colon, data, 1, 3, 9, 11, 0)) { if (rdval(get_file(&proc_net_dev), s->device_colon, data, 0
| (1 << 1)
| (1 << 3)
| (1 << 9)
| (1 << 11))
) {
put_question_marks(10); put_question_marks(10);
return; return;
} }
@ -694,7 +693,7 @@ static void FAST_FUNC collect_mem(mem_stat *s)
ullong m_cached = 0; ullong m_cached = 0;
ullong m_slab = 0; ullong m_slab = 0;
if (rdval(get_file(&proc_meminfo), "MemTotal:", &m_total, 1, 0)) { if (rdval(get_file(&proc_meminfo), "MemTotal:", &m_total, 1 << 1)) {
put_question_marks(4); put_question_marks(4);
return; return;
} }
@ -703,10 +702,10 @@ static void FAST_FUNC collect_mem(mem_stat *s)
return; return;
} }
if (rdval(proc_meminfo.file, "MemFree:", &m_free , 1, 0) if (rdval(proc_meminfo.file, "MemFree:", &m_free , 1 << 1)
|| rdval(proc_meminfo.file, "Buffers:", &m_bufs , 1, 0) || rdval(proc_meminfo.file, "Buffers:", &m_bufs , 1 << 1)
|| rdval(proc_meminfo.file, "Cached:", &m_cached, 1, 0) || rdval(proc_meminfo.file, "Cached:", &m_cached, 1 << 1)
|| rdval(proc_meminfo.file, "Slab:", &m_slab , 1, 0) || rdval(proc_meminfo.file, "Slab:", &m_slab , 1 << 1)
) { ) {
put_question_marks(4); put_question_marks(4);
return; return;
@ -737,8 +736,8 @@ static void FAST_FUNC collect_swp(swp_stat *s UNUSED_PARAM)
{ {
ullong s_total[1]; ullong s_total[1];
ullong s_free[1]; ullong s_free[1];
if (rdval(get_file(&proc_meminfo), "SwapTotal:", s_total, 1, 0) if (rdval(get_file(&proc_meminfo), "SwapTotal:", s_total, 1 << 1)
|| rdval(proc_meminfo.file, "SwapFree:" , s_free, 1, 0) || rdval(proc_meminfo.file, "SwapFree:" , s_free, 1 << 1)
) { ) {
put_question_marks(4); put_question_marks(4);
return; return;
@ -761,7 +760,10 @@ static void FAST_FUNC collect_fd(fd_stat *s UNUSED_PARAM)
{ {
ullong data[2]; ullong data[2];
if (rdval(get_file(&proc_sys_fs_filenr), "", data, 1, 2, 0)) { if (rdval(get_file(&proc_sys_fs_filenr), "", data, 0
| (1 << 1)
| (1 << 2))
) {
put_question_marks(4); put_question_marks(4);
return; return;
} }