diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-06-06 17:40:54 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-06-06 17:40:54 +0200 |
commit | d94332f2b6615e30a8a4d3420407dbd938885b7b (patch) | |
tree | 7202ce9d82c1aafe9ecec90a4f5ee906ff150253 | |
parent | 87496aa08180cdfffed88f7802e46ab25f659890 (diff) | |
download | busybox-w32-d94332f2b6615e30a8a4d3420407dbd938885b7b.tar.gz busybox-w32-d94332f2b6615e30a8a4d3420407dbd938885b7b.tar.bz2 busybox-w32-d94332f2b6615e30a8a4d3420407dbd938885b7b.zip |
top: code shrink, -26 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | procps/top.c | 60 |
1 files changed, 27 insertions, 33 deletions
diff --git a/procps/top.c b/procps/top.c index 51b167171..04dd82633 100644 --- a/procps/top.c +++ b/procps/top.c | |||
@@ -103,11 +103,11 @@ struct globals { | |||
103 | }; //FIX_ALIASING; - large code growth | 103 | }; //FIX_ALIASING; - large code growth |
104 | enum { LINE_BUF_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line_buf) }; | 104 | enum { LINE_BUF_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line_buf) }; |
105 | #define G (*(struct globals*)&bb_common_bufsiz1) | 105 | #define G (*(struct globals*)&bb_common_bufsiz1) |
106 | #define INIT_G() do { \ | 106 | struct BUG_bad_size { |
107 | struct G_sizecheck { \ | 107 | char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1]; |
108 | char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \ | 108 | char BUG_line_buf_too_small[LINE_BUF_SIZE > 80 ? 1 : -1]; |
109 | }; \ | 109 | }; |
110 | } while (0) | 110 | #define INIT_G() do { } while (0) |
111 | #define top (G.top ) | 111 | #define top (G.top ) |
112 | #define ntop (G.ntop ) | 112 | #define ntop (G.ntop ) |
113 | #define sort_field (G.sort_field ) | 113 | #define sort_field (G.sort_field ) |
@@ -705,7 +705,7 @@ static void display_topmem_header(int scr_width, int *lines_rem_p) | |||
705 | MWRITE, ANON, MAP, SLAB, | 705 | MWRITE, ANON, MAP, SLAB, |
706 | NUM_FIELDS | 706 | NUM_FIELDS |
707 | }; | 707 | }; |
708 | static const char match[NUM_FIELDS][11] = { | 708 | static const char match[NUM_FIELDS][12] = { |
709 | "\x09" "MemTotal:", // TOTAL | 709 | "\x09" "MemTotal:", // TOTAL |
710 | "\x08" "MemFree:", // MFREE | 710 | "\x08" "MemFree:", // MFREE |
711 | "\x08" "Buffers:", // BUF | 711 | "\x08" "Buffers:", // BUF |
@@ -718,52 +718,46 @@ static void display_topmem_header(int scr_width, int *lines_rem_p) | |||
718 | "\x07" "Mapped:", // MAP | 718 | "\x07" "Mapped:", // MAP |
719 | "\x05" "Slab:", // SLAB | 719 | "\x05" "Slab:", // SLAB |
720 | }; | 720 | }; |
721 | //TODO? Note that fields always appear in the above order. | 721 | char meminfo_buf[4 * 1024]; |
722 | //Thus, as each new line read from /proc/meminfo, we can compare it *once* | 722 | const char *Z[NUM_FIELDS]; |
723 | //with match[last_matched+1], instead of looping thru all match[i]'s. | ||
724 | //If it matches, memorize its data and last_matched++ (and if == NUM_FIELDS, | ||
725 | //we're done with reading /proc/meminfo!); otherwise fgets next line. | ||
726 | //The code below is slower, but is robust against a case when /proc/meminfo | ||
727 | //gets reordered in the future. | ||
728 | char Z[NUM_FIELDS][sizeof(long long)*3]; | ||
729 | char linebuf[128]; | ||
730 | unsigned i; | 723 | unsigned i; |
731 | FILE *fp; | 724 | int sz; |
732 | 725 | ||
733 | memset(&Z, 0, sizeof(Z)); | ||
734 | for (i = 0; i < NUM_FIELDS; i++) | 726 | for (i = 0; i < NUM_FIELDS; i++) |
735 | Z[i][0] = '?'; | 727 | Z[i] = "?"; |
736 | 728 | ||
737 | /* read memory info */ | 729 | /* read memory info */ |
738 | fp = xfopen_for_read("meminfo"); | 730 | sz = open_read_close("meminfo", meminfo_buf, sizeof(meminfo_buf) - 1); |
739 | while (fgets(linebuf, sizeof(linebuf), fp)) { | 731 | if (sz >= 0) { |
732 | char *p = meminfo_buf; | ||
733 | meminfo_buf[sz] = '\0'; | ||
734 | /* Note that fields always appear in the match[] order */ | ||
740 | for (i = 0; i < NUM_FIELDS; i++) { | 735 | for (i = 0; i < NUM_FIELDS; i++) { |
741 | unsigned sz = (unsigned char)match[i][0]; | 736 | char *found = strstr(p, match[i] + 1); |
742 | if (strncmp(linebuf, match[i] + 1, sz) == 0) { | 737 | if (found) { |
743 | /* Cut "NNNN" out of " NNNN kb" */ | 738 | /* Cut "NNNN" out of " NNNN kb" */ |
744 | char *s = skip_whitespace(linebuf + sz); | 739 | char *s = skip_whitespace(found + match[i][0]); |
745 | skip_non_whitespace(s)[0] = '\0'; | 740 | p = skip_non_whitespace(s); |
746 | safe_strncpy(Z[i], s, sizeof(Z[i])); | 741 | *p++ = '\0'; |
747 | break; | 742 | Z[i] = s; |
748 | } | 743 | } |
749 | } | 744 | } |
750 | } | 745 | } |
751 | fclose(fp); | ||
752 | 746 | ||
753 | snprintf(linebuf, sizeof(linebuf), | 747 | snprintf(line_buf, LINE_BUF_SIZE, |
754 | "Mem total:%s anon:%s map:%s free:%s", | 748 | "Mem total:%s anon:%s map:%s free:%s", |
755 | Z[TOTAL], Z[ANON], Z[MAP], Z[MFREE]); | 749 | Z[TOTAL], Z[ANON], Z[MAP], Z[MFREE]); |
756 | printf(OPT_BATCH_MODE ? "%.*s\n" : "\033[H\033[J%.*s\n", scr_width, linebuf); | 750 | printf(OPT_BATCH_MODE ? "%.*s\n" : "\033[H\033[J%.*s\n", scr_width, line_buf); |
757 | 751 | ||
758 | snprintf(linebuf, sizeof(linebuf), | 752 | snprintf(line_buf, LINE_BUF_SIZE, |
759 | " slab:%s buf:%s cache:%s dirty:%s write:%s", | 753 | " slab:%s buf:%s cache:%s dirty:%s write:%s", |
760 | Z[SLAB], Z[BUF], Z[CACHE], Z[DIRTY], Z[MWRITE]); | 754 | Z[SLAB], Z[BUF], Z[CACHE], Z[DIRTY], Z[MWRITE]); |
761 | printf("%.*s\n", scr_width, linebuf); | 755 | printf("%.*s\n", scr_width, line_buf); |
762 | 756 | ||
763 | snprintf(linebuf, sizeof(linebuf), | 757 | snprintf(line_buf, LINE_BUF_SIZE, |
764 | "Swap total:%s free:%s", // TODO: % used? | 758 | "Swap total:%s free:%s", // TODO: % used? |
765 | Z[SWAPTOTAL], Z[SWAPFREE]); | 759 | Z[SWAPTOTAL], Z[SWAPFREE]); |
766 | printf("%.*s\n", scr_width, linebuf); | 760 | printf("%.*s\n", scr_width, line_buf); |
767 | 761 | ||
768 | (*lines_rem_p) -= 3; | 762 | (*lines_rem_p) -= 3; |
769 | } | 763 | } |