Changeset 3621 in MondoRescue for branches/3.3/mindi-busybox/procps/free.c


Ignore:
Timestamp:
Dec 20, 2016, 4:07:32 PM (7 years ago)
Author:
Bruno Cornec
Message:

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

Location:
branches/3.3
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/3.3/mindi-busybox/procps/free.c

    r3232 r3621  
    2323
    2424#include "libbb.h"
     25#include "common_bufsiz.h"
    2526#ifdef __linux__
    2627# include <sys/sysinfo.h>
     
    3637#endif
    3738} FIX_ALIASING;
    38 #define G (*(struct globals*)&bb_common_bufsiz1)
    39 #define INIT_G() do { } while (0)
     39#define G (*(struct globals*)bb_common_bufsiz1)
     40#define INIT_G() do { setup_common_bufsiz(); } while (0)
    4041
    4142
     
    4546}
    4647
     48static unsigned long parse_cached_kb(void)
     49{
     50    char buf[60]; /* actual lines we expect are ~30 chars or less */
     51    FILE *fp;
     52    unsigned long cached = 0;
     53
     54    fp = xfopen_for_read("/proc/meminfo");
     55    while (fgets(buf, sizeof(buf), fp) != NULL) {
     56        if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1)
     57            break;
     58    }
     59    if (ENABLE_FEATURE_CLEAN_UP)
     60        fclose(fp);
     61
     62    return cached;
     63}
    4764
    4865int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     
    5067{
    5168    struct sysinfo info;
     69    unsigned long long cached;
    5270
    5371    INIT_G();
     
    7492    }
    7593#endif
    76 
    77     sysinfo(&info);
    78 
    79     /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
    80     G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
    81 
    82     printf("     %13s%13s%13s%13s%13s\n",
     94    printf("       %11s%11s%11s%11s%11s%11s\n"
     95    "Mem:   ",
    8396        "total",
    8497        "used",
    8598        "free",
    86         "shared", "buffers" /* swap and total don't have these columns */
    87         /* procps version 3.2.8 also shows "cached" column, but
    88          * sysinfo() does not provide this value, need to parse
    89          * /proc/meminfo instead and get "Cached: NNN kB" from there.
    90          */
     99        "shared", "buffers", "cached" /* swap and total don't have these columns */
    91100    );
    92101
    93 #define FIELDS_5 "%13llu%13llu%13llu%13llu%13llu\n"
    94 #define FIELDS_3 (FIELDS_5 + 2*6)
    95 #define FIELDS_2 (FIELDS_5 + 3*6)
     102    sysinfo(&info);
     103    /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
     104    G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
     105    /* Extract cached from /proc/meminfo and convert to mem_units */
     106    cached = ((unsigned long long) parse_cached_kb() * 1024) / G.mem_unit;
    96107
    97     printf("Mem: ");
    98     printf(FIELDS_5,
    99         scale(info.totalram),
    100         scale(info.totalram - info.freeram),
    101         scale(info.freeram),
    102         scale(info.sharedram),
    103         scale(info.bufferram)
     108#define FIELDS_6 "%11llu%11llu%11llu%11llu%11llu%11llu\n"
     109#define FIELDS_3 (FIELDS_6 + 3*6)
     110#define FIELDS_2 (FIELDS_6 + 4*6)
     111
     112    printf(FIELDS_6,
     113        scale(info.totalram),                //total
     114        scale(info.totalram - info.freeram), //used
     115        scale(info.freeram),                 //free
     116        scale(info.sharedram),               //shared
     117        scale(info.bufferram),               //buffers
     118        scale(cached)                        //cached
    104119    );
    105120    /* Show alternate, more meaningful busy/free numbers by counting
    106      * buffer cache as free memory (make it "-/+ buffers/cache"
    107      * if/when we add support for "cached" column): */
    108     printf("-/+ buffers:      ");
     121     * buffer cache as free memory. */
     122    printf("-/+ buffers/cache:");
     123    cached += info.freeram;
     124    cached += info.bufferram;
    109125    printf(FIELDS_2,
    110         scale(info.totalram - info.freeram - info.bufferram),
    111         scale(info.freeram + info.bufferram)
     126        scale(info.totalram - cached), //used
     127        scale(cached)                  //free
    112128    );
    113129#if BB_MMU
    114     printf("Swap:");
     130    printf("Swap:  ");
    115131    printf(FIELDS_3,
    116         scale(info.totalswap),
    117         scale(info.totalswap - info.freeswap),
    118         scale(info.freeswap)
     132        scale(info.totalswap),                 //total
     133        scale(info.totalswap - info.freeswap), //used
     134        scale(info.freeswap)                   //free
    119135    );
    120136#endif
Note: See TracChangeset for help on using the changeset viewer.