aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2016-03-15 15:52:40 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2016-03-15 15:52:40 +0100
commit8a26fda98c46b1ffd98a1a9874a0899b061226d8 (patch)
tree5153235816a1ced9438159c2dc8e1fee1f4814d2
parent99c71c9e800d51aa52d7bd592e8071341013a628 (diff)
downloadbusybox-w32-8a26fda98c46b1ffd98a1a9874a0899b061226d8.tar.gz
busybox-w32-8a26fda98c46b1ffd98a1a9874a0899b061226d8.tar.bz2
busybox-w32-8a26fda98c46b1ffd98a1a9874a0899b061226d8.zip
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>
-rw-r--r--procps/nmeter.c106
1 files changed, 54 insertions, 52 deletions
diff --git a/procps/nmeter.c b/procps/nmeter.c
index 8fe39c43b..d6222af6b 100644
--- a/procps/nmeter.c
+++ b/procps/nmeter.c
@@ -208,67 +208,50 @@ static ullong read_after_slash(const char *p)
208 return strtoull(p+1, NULL, 10); 208 return strtoull(p+1, NULL, 10);
209} 209}
210 210
211enum conv_type { conv_decimal, conv_slash }; 211enum conv_type {
212 conv_decimal = 0,
213 conv_slash = 1
214};
212 215
213// Reads decimal values from line. Values start after key, for example: 216// Reads decimal values from line. Values start after key, for example:
214// "cpu 649369 0 341297 4336769..." - key is "cpu" here. 217// "cpu 649369 0 341297 4336769..." - key is "cpu" here.
215// Values are stored in vec[]. arg_ptr has list of positions 218// Values are stored in vec[].
216// we are interested in: for example: 1,2,5 - we want 1st, 2nd and 5th value. 219// posbits is a bit lit of positions we are interested in.
217static int vrdval(const char* p, const char* key, 220// for example: 00100110 - we want 1st, 2nd and 5th value.
218 enum conv_type conv, ullong *vec, va_list arg_ptr) 221// posbits.bit0 encodes conversion type.
222static int rdval(const char* p, const char* key, ullong *vec, long posbits)
219{ 223{
220 int indexline; 224 unsigned curpos;
221 int indexnext;
222 225
223 p = strstr(p, key); 226 p = strstr(p, key);
224 if (!p) return 1; 227 if (!p) return 1;
225 228
226 p += strlen(key); 229 p += strlen(key);
227 indexline = 1; 230 curpos = 1 << 1;
228 indexnext = va_arg(arg_ptr, int);
229 while (1) { 231 while (1) {
230 while (*p == ' ' || *p == '\t') p++; 232 while (*p == ' ' || *p == '\t') p++;
231 if (*p == '\n' || *p == '\0') break; 233 if (*p == '\n' || *p == '\0') break;
232 234
233 if (indexline == indexnext) { // read this value 235 if (curpos & posbits) { // read this value
234 *vec++ = conv==conv_decimal ? 236 *vec++ = (posbits & 1) == conv_decimal ?
235 strtoull(p, NULL, 10) : 237 strtoull(p, NULL, 10) :
236 read_after_slash(p); 238 read_after_slash(p);
237 indexnext = va_arg(arg_ptr, int); 239 posbits -= curpos;
238 if (!indexnext) 240 if (posbits <= 1)
239 return 0; 241 return 0;
240 } 242 }
241 while (*p > ' ') p++; // skip over value 243 while (*p > ' ') // skip over the value
242 indexline++; 244 p++;
245 curpos <<= 1;
243 } 246 }
244 return 0; 247 return 0;
245} 248}
246 249
247// Parses files with lines like "cpu0 21727 0 15718 1813856 9461 10485 0 0":
248// rdval(file_contents, "string_to_find", result_vector, value#, value#...)
249// value# start with 1
250static int rdval(const char* p, const char* key, ullong *vec, ...)
251{
252 va_list arg_ptr;
253 int result;
254
255 va_start(arg_ptr, vec);
256 result = vrdval(p, key, conv_decimal, vec, arg_ptr);
257 va_end(arg_ptr);
258
259 return result;
260}
261
262// Parses files with lines like "... ... ... 3/148 ...." 250// Parses files with lines like "... ... ... 3/148 ...."
263static int rdval_loadavg(const char* p, ullong *vec, ...) 251static int rdval_loadavg(const char* p, ullong *vec, long posbits)
264{ 252{
265 va_list arg_ptr;
266 int result; 253 int result;
267 254 result = rdval(p, "", vec, posbits | conv_slash);
268 va_start(arg_ptr, vec);
269 result = vrdval(p, "", conv_slash, vec, arg_ptr);
270 va_end(arg_ptr);
271
272 return result; 255 return result;
273} 256}
274 257
@@ -397,7 +380,15 @@ static void FAST_FUNC collect_cpu(cpu_stat *s)
397 char *bar = s->bar; 380 char *bar = s->bar;
398 int i; 381 int i;
399 382
400 if (rdval(get_file(&proc_stat), "cpu ", data, 1, 2, 3, 4, 5, 6, 7, 0)) { 383 if (rdval(get_file(&proc_stat), "cpu ", data, 0
384 | (1 << 1)
385 | (1 << 2)
386 | (1 << 3)
387 | (1 << 4)
388 | (1 << 5)
389 | (1 << 6)
390 | (1 << 7))
391 ) {
401 put_question_marks(bar_sz); 392 put_question_marks(bar_sz);
402 return; 393 return;
403 } 394 }
@@ -466,7 +457,7 @@ static void FAST_FUNC collect_int(int_stat *s)
466 ullong data[1]; 457 ullong data[1];
467 ullong old; 458 ullong old;
468 459
469 if (rdval(get_file(&proc_stat), "intr", data, s->no, 0)) { 460 if (rdval(get_file(&proc_stat), "intr", data, 1 << s->no)) {
470 put_question_marks(4); 461 put_question_marks(4);
471 return; 462 return;
472 } 463 }
@@ -500,7 +491,7 @@ static void FAST_FUNC collect_ctx(ctx_stat *s)
500 ullong data[1]; 491 ullong data[1];
501 ullong old; 492 ullong old;
502 493
503 if (rdval(get_file(&proc_stat), "ctxt", data, 1, 0)) { 494 if (rdval(get_file(&proc_stat), "ctxt", data, 1 << 1)) {
504 put_question_marks(4); 495 put_question_marks(4);
505 return; 496 return;
506 } 497 }
@@ -532,7 +523,10 @@ static void FAST_FUNC collect_blk(blk_stat *s)
532 if (is26) { 523 if (is26) {
533 i = rdval_diskstats(get_file(&proc_diskstats), data); 524 i = rdval_diskstats(get_file(&proc_diskstats), data);
534 } else { 525 } else {
535 i = rdval(get_file(&proc_stat), s->lookfor, data, 1, 2, 0); 526 i = rdval(get_file(&proc_stat), s->lookfor, data, 0
527 | (1 << 1)
528 | (1 << 2)
529 );
536 // Linux 2.4 reports bio in Kbytes, convert to sectors: 530 // Linux 2.4 reports bio in Kbytes, convert to sectors:
537 data[0] *= 2; 531 data[0] *= 2;
538 data[1] *= 2; 532 data[1] *= 2;
@@ -570,7 +564,7 @@ static void FAST_FUNC collect_thread_nr(fork_stat *s UNUSED_PARAM)
570{ 564{
571 ullong data[1]; 565 ullong data[1];
572 566
573 if (rdval_loadavg(get_file(&proc_loadavg), data, 4, 0)) { 567 if (rdval_loadavg(get_file(&proc_loadavg), data, 1 << 4)) {
574 put_question_marks(4); 568 put_question_marks(4);
575 return; 569 return;
576 } 570 }
@@ -582,7 +576,7 @@ static void FAST_FUNC collect_fork(fork_stat *s)
582 ullong data[1]; 576 ullong data[1];
583 ullong old; 577 ullong old;
584 578
585 if (rdval(get_file(&proc_stat), "processes", data, 1, 0)) { 579 if (rdval(get_file(&proc_stat), "processes", data, 1 << 1)) {
586 put_question_marks(4); 580 put_question_marks(4);
587 return; 581 return;
588 } 582 }
@@ -616,7 +610,12 @@ static void FAST_FUNC collect_if(if_stat *s)
616 ullong data[4]; 610 ullong data[4];
617 int i; 611 int i;
618 612
619 if (rdval(get_file(&proc_net_dev), s->device_colon, data, 1, 3, 9, 11, 0)) { 613 if (rdval(get_file(&proc_net_dev), s->device_colon, data, 0
614 | (1 << 1)
615 | (1 << 3)
616 | (1 << 9)
617 | (1 << 11))
618 ) {
620 put_question_marks(10); 619 put_question_marks(10);
621 return; 620 return;
622 } 621 }
@@ -694,7 +693,7 @@ static void FAST_FUNC collect_mem(mem_stat *s)
694 ullong m_cached = 0; 693 ullong m_cached = 0;
695 ullong m_slab = 0; 694 ullong m_slab = 0;
696 695
697 if (rdval(get_file(&proc_meminfo), "MemTotal:", &m_total, 1, 0)) { 696 if (rdval(get_file(&proc_meminfo), "MemTotal:", &m_total, 1 << 1)) {
698 put_question_marks(4); 697 put_question_marks(4);
699 return; 698 return;
700 } 699 }
@@ -703,10 +702,10 @@ static void FAST_FUNC collect_mem(mem_stat *s)
703 return; 702 return;
704 } 703 }
705 704
706 if (rdval(proc_meminfo.file, "MemFree:", &m_free , 1, 0) 705 if (rdval(proc_meminfo.file, "MemFree:", &m_free , 1 << 1)
707 || rdval(proc_meminfo.file, "Buffers:", &m_bufs , 1, 0) 706 || rdval(proc_meminfo.file, "Buffers:", &m_bufs , 1 << 1)
708 || rdval(proc_meminfo.file, "Cached:", &m_cached, 1, 0) 707 || rdval(proc_meminfo.file, "Cached:", &m_cached, 1 << 1)
709 || rdval(proc_meminfo.file, "Slab:", &m_slab , 1, 0) 708 || rdval(proc_meminfo.file, "Slab:", &m_slab , 1 << 1)
710 ) { 709 ) {
711 put_question_marks(4); 710 put_question_marks(4);
712 return; 711 return;
@@ -737,8 +736,8 @@ static void FAST_FUNC collect_swp(swp_stat *s UNUSED_PARAM)
737{ 736{
738 ullong s_total[1]; 737 ullong s_total[1];
739 ullong s_free[1]; 738 ullong s_free[1];
740 if (rdval(get_file(&proc_meminfo), "SwapTotal:", s_total, 1, 0) 739 if (rdval(get_file(&proc_meminfo), "SwapTotal:", s_total, 1 << 1)
741 || rdval(proc_meminfo.file, "SwapFree:" , s_free, 1, 0) 740 || rdval(proc_meminfo.file, "SwapFree:" , s_free, 1 << 1)
742 ) { 741 ) {
743 put_question_marks(4); 742 put_question_marks(4);
744 return; 743 return;
@@ -761,7 +760,10 @@ static void FAST_FUNC collect_fd(fd_stat *s UNUSED_PARAM)
761{ 760{
762 ullong data[2]; 761 ullong data[2];
763 762
764 if (rdval(get_file(&proc_sys_fs_filenr), "", data, 1, 2, 0)) { 763 if (rdval(get_file(&proc_sys_fs_filenr), "", data, 0
764 | (1 << 1)
765 | (1 << 2))
766 ) {
765 put_question_marks(4); 767 put_question_marks(4);
766 return; 768 return;
767 } 769 }