diff options
| author | Maksym Kryzhanovskyy <xmaks@email.cz> | 2010-06-06 08:24:39 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-06-06 08:24:39 +0200 |
| commit | 87496aa08180cdfffed88f7802e46ab25f659890 (patch) | |
| tree | f97ad876aef0b87dd8154a6605bf284fce40654f | |
| parent | f2177abe1d1da0f845147e29f5565a53d9cba09d (diff) | |
| download | busybox-w32-87496aa08180cdfffed88f7802e46ab25f659890.tar.gz busybox-w32-87496aa08180cdfffed88f7802e46ab25f659890.tar.bz2 busybox-w32-87496aa08180cdfffed88f7802e46ab25f659890.zip | |
top: code shrink
text data bss dec hex filename
853034 453 6820 860307 d2093 busybox_old
852726 453 6820 859999 d1f5f busybox_unstripped
Signed-off-by: Maksym Kryzhanovskyy <xmaks@email.cz>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | procps/top.c | 119 |
1 files changed, 42 insertions, 77 deletions
diff --git a/procps/top.c b/procps/top.c index e4afafc4c..51b167171 100644 --- a/procps/top.c +++ b/procps/top.c | |||
| @@ -696,111 +696,76 @@ static int topmem_sort(char *a, char *b) | |||
| 696 | return inverted ? -n : n; | 696 | return inverted ? -n : n; |
| 697 | } | 697 | } |
| 698 | 698 | ||
| 699 | /* Cut "NNNN" out of " NNNN kb" */ | ||
| 700 | static char *grab_number(char *str, const char *match, unsigned sz) | ||
| 701 | { | ||
| 702 | if (strncmp(str, match, sz) == 0) { | ||
| 703 | str = skip_whitespace(str + sz); | ||
| 704 | (skip_non_whitespace(str))[0] = '\0'; | ||
| 705 | return xstrdup(str); | ||
| 706 | } | ||
| 707 | return NULL; | ||
| 708 | } | ||
| 709 | |||
| 710 | /* display header info (meminfo / loadavg) */ | 699 | /* display header info (meminfo / loadavg) */ |
| 711 | static void display_topmem_header(int scr_width, int *lines_rem_p) | 700 | static void display_topmem_header(int scr_width, int *lines_rem_p) |
| 712 | { | 701 | { |
| 702 | enum { | ||
| 703 | TOTAL = 0, MFREE, BUF, CACHE, | ||
| 704 | SWAPTOTAL, SWAPFREE, DIRTY, | ||
| 705 | MWRITE, ANON, MAP, SLAB, | ||
| 706 | NUM_FIELDS | ||
| 707 | }; | ||
| 708 | static const char match[NUM_FIELDS][11] = { | ||
| 709 | "\x09" "MemTotal:", // TOTAL | ||
| 710 | "\x08" "MemFree:", // MFREE | ||
| 711 | "\x08" "Buffers:", // BUF | ||
| 712 | "\x07" "Cached:", // CACHE | ||
| 713 | "\x0a" "SwapTotal:", // SWAPTOTAL | ||
| 714 | "\x09" "SwapFree:", // SWAPFREE | ||
| 715 | "\x06" "Dirty:", // DIRTY | ||
| 716 | "\x0a" "Writeback:", // MWRITE | ||
| 717 | "\x0a" "AnonPages:", // ANON | ||
| 718 | "\x07" "Mapped:", // MAP | ||
| 719 | "\x05" "Slab:", // SLAB | ||
| 720 | }; | ||
| 721 | //TODO? Note that fields always appear in the above order. | ||
| 722 | //Thus, as each new line read from /proc/meminfo, we can compare it *once* | ||
| 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]; | ||
| 713 | char linebuf[128]; | 729 | char linebuf[128]; |
| 714 | unsigned i; | 730 | unsigned i; |
| 715 | FILE *fp; | 731 | FILE *fp; |
| 716 | union { | ||
| 717 | struct { | ||
| 718 | /* 1 */ char *total; | ||
| 719 | /* 2 */ char *mfree; | ||
| 720 | /* 3 */ char *buf; | ||
| 721 | /* 4 */ char *cache; | ||
| 722 | /* 5 */ char *swaptotal; | ||
| 723 | /* 6 */ char *swapfree; | ||
| 724 | /* 7 */ char *dirty; | ||
| 725 | /* 8 */ char *mwrite; | ||
| 726 | /* 9 */ char *anon; | ||
| 727 | /* 10 */ char *map; | ||
| 728 | /* 11 */ char *slab; | ||
| 729 | } u; | ||
| 730 | char *str[11]; | ||
| 731 | } Z; | ||
| 732 | #define total Z.u.total | ||
| 733 | #define mfree Z.u.mfree | ||
| 734 | #define buf Z.u.buf | ||
| 735 | #define cache Z.u.cache | ||
| 736 | #define swaptotal Z.u.swaptotal | ||
| 737 | #define swapfree Z.u.swapfree | ||
| 738 | #define dirty Z.u.dirty | ||
| 739 | #define mwrite Z.u.mwrite | ||
| 740 | #define anon Z.u.anon | ||
| 741 | #define map Z.u.map | ||
| 742 | #define slab Z.u.slab | ||
| 743 | #define str Z.str | ||
| 744 | 732 | ||
| 745 | memset(&Z, 0, sizeof(Z)); | 733 | memset(&Z, 0, sizeof(Z)); |
| 734 | for (i = 0; i < NUM_FIELDS; i++) | ||
| 735 | Z[i][0] = '?'; | ||
| 746 | 736 | ||
| 747 | /* read memory info */ | 737 | /* read memory info */ |
| 748 | fp = xfopen_for_read("meminfo"); | 738 | fp = xfopen_for_read("meminfo"); |
| 749 | while (fgets(linebuf, sizeof(linebuf), fp)) { | 739 | while (fgets(linebuf, sizeof(linebuf), fp)) { |
| 750 | char *p; | 740 | for (i = 0; i < NUM_FIELDS; i++) { |
| 751 | 741 | unsigned sz = (unsigned char)match[i][0]; | |
| 752 | #define SCAN(match, name) \ | 742 | if (strncmp(linebuf, match[i] + 1, sz) == 0) { |
| 753 | p = grab_number(linebuf, match, sizeof(match)-1); \ | 743 | /* Cut "NNNN" out of " NNNN kb" */ |
| 754 | if (p) { name = p; continue; } | 744 | char *s = skip_whitespace(linebuf + sz); |
| 755 | 745 | skip_non_whitespace(s)[0] = '\0'; | |
| 756 | SCAN("MemTotal:", total); | 746 | safe_strncpy(Z[i], s, sizeof(Z[i])); |
| 757 | SCAN("MemFree:", mfree); | 747 | break; |
| 758 | SCAN("Buffers:", buf); | 748 | } |
| 759 | SCAN("Cached:", cache); | 749 | } |
| 760 | SCAN("SwapTotal:", swaptotal); | ||
| 761 | SCAN("SwapFree:", swapfree); | ||
| 762 | SCAN("Dirty:", dirty); | ||
| 763 | SCAN("Writeback:", mwrite); | ||
| 764 | SCAN("AnonPages:", anon); | ||
| 765 | SCAN("Mapped:", map); | ||
| 766 | SCAN("Slab:", slab); | ||
| 767 | #undef SCAN | ||
| 768 | } | 750 | } |
| 769 | fclose(fp); | 751 | fclose(fp); |
| 770 | 752 | ||
| 771 | #define S(s) (s ? s : "0") | ||
| 772 | snprintf(linebuf, sizeof(linebuf), | 753 | snprintf(linebuf, sizeof(linebuf), |
| 773 | "Mem total:%s anon:%s map:%s free:%s", | 754 | "Mem total:%s anon:%s map:%s free:%s", |
| 774 | S(total), S(anon), S(map), S(mfree)); | 755 | Z[TOTAL], Z[ANON], Z[MAP], Z[MFREE]); |
| 775 | printf(OPT_BATCH_MODE ? "%.*s\n" : "\033[H\033[J%.*s\n", scr_width, linebuf); | 756 | printf(OPT_BATCH_MODE ? "%.*s\n" : "\033[H\033[J%.*s\n", scr_width, linebuf); |
| 776 | 757 | ||
| 777 | snprintf(linebuf, sizeof(linebuf), | 758 | snprintf(linebuf, sizeof(linebuf), |
| 778 | " slab:%s buf:%s cache:%s dirty:%s write:%s", | 759 | " slab:%s buf:%s cache:%s dirty:%s write:%s", |
| 779 | S(slab), S(buf), S(cache), S(dirty), S(mwrite)); | 760 | Z[SLAB], Z[BUF], Z[CACHE], Z[DIRTY], Z[MWRITE]); |
| 780 | printf("%.*s\n", scr_width, linebuf); | 761 | printf("%.*s\n", scr_width, linebuf); |
| 781 | 762 | ||
| 782 | snprintf(linebuf, sizeof(linebuf), | 763 | snprintf(linebuf, sizeof(linebuf), |
| 783 | "Swap total:%s free:%s", // TODO: % used? | 764 | "Swap total:%s free:%s", // TODO: % used? |
| 784 | S(swaptotal), S(swapfree)); | 765 | Z[SWAPTOTAL], Z[SWAPFREE]); |
| 785 | printf("%.*s\n", scr_width, linebuf); | 766 | printf("%.*s\n", scr_width, linebuf); |
| 786 | 767 | ||
| 787 | (*lines_rem_p) -= 3; | 768 | (*lines_rem_p) -= 3; |
| 788 | #undef S | ||
| 789 | |||
| 790 | for (i = 0; i < ARRAY_SIZE(str); i++) | ||
| 791 | free(str[i]); | ||
| 792 | #undef total | ||
| 793 | #undef free | ||
| 794 | #undef buf | ||
| 795 | #undef cache | ||
| 796 | #undef swaptotal | ||
| 797 | #undef swapfree | ||
| 798 | #undef dirty | ||
| 799 | #undef write | ||
| 800 | #undef anon | ||
| 801 | #undef map | ||
| 802 | #undef slab | ||
| 803 | #undef str | ||
| 804 | } | 769 | } |
| 805 | 770 | ||
| 806 | static void ulltoa6_and_space(unsigned long long ul, char buf[6]) | 771 | static void ulltoa6_and_space(unsigned long long ul, char buf[6]) |
