Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/procps/nmeter.c


Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/procps/nmeter.c

    r1765 r2725  
    11/*
    2 ** Licensed under the GPL v2, see the file LICENSE in this tarball
    3 **
    4 ** Based on nanotop.c from floppyfw project
    5 **
    6 ** Contact me: vda.linux@googlemail.com */
     2 * Licensed under GPLv2, see file LICENSE in this source tree.
     3 *
     4 * Based on nanotop.c from floppyfw project
     5 *
     6 * Contact me: vda.linux@googlemail.com
     7 */
    78
    89//TODO:
     
    1819//  totalswap=134209536, freeswap=134209536, procs=157})
    1920
    20 #include <time.h>
    2121#include "libbb.h"
    2222
    2323typedef unsigned long long ullong;
    2424
    25 enum { PROC_FILE_SIZE = 4096 };
     25enum {  /* Preferably use powers of 2 */
     26    PROC_MIN_FILE_SIZE = 256,
     27    PROC_MAX_FILE_SIZE = 16 * 1024,
     28};
    2629
    2730typedef struct proc_file {
    2831    char *file;
    29     //const char *name;
     32    int file_sz;
    3033    smallint last_gen;
    3134} proc_file;
     
    7679#define proc_sys_fs_filenr (G.proc_sys_fs_filenr)
    7780#define INIT_G() do { \
    78         PTR_TO_GLOBALS = xzalloc(sizeof(G)); \
    79         cur_outbuf = outbuf; \
    80         final_str = "\n"; \
    81         deltanz = delta = 1000000; \
    82     } while (0)
     81    SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
     82    cur_outbuf = outbuf; \
     83    final_str = "\n"; \
     84    deltanz = delta = 1000000; \
     85} while (0)
    8386
    8487// We depend on this being a char[], not char* - we take sizeof() of it
     
    99102    int sz = cur_outbuf - outbuf;
    100103    if (sz > 0) {
    101         xwrite(1, outbuf, sz);
     104        xwrite(STDOUT_FILENO, outbuf, sz);
    102105        cur_outbuf = outbuf;
    103106    }
     
    125128}
    126129
    127 static void readfile_z(char *buf, int sz, const char* fname)
     130static void readfile_z(proc_file *pf, const char* fname)
    128131{
    129132// open_read_close() will do two reads in order to be sure we are at EOF,
    130133// and we don't need/want that.
    131 //  sz = open_read_close(fname, buf, sz-1);
    132 
    133     int fd = xopen(fname, O_RDONLY);
     134    int fd;
     135    int sz, rdsz;
     136    char *buf;
     137
     138    sz = pf->file_sz;
     139    buf = pf->file;
     140    if (!buf) {
     141        buf = xmalloc(PROC_MIN_FILE_SIZE);
     142        sz = PROC_MIN_FILE_SIZE;
     143    }
     144 again:
     145    fd = xopen(fname, O_RDONLY);
    134146    buf[0] = '\0';
    135     if (fd >= 0) {
    136         sz = read(fd, buf, sz-1);
    137         if (sz > 0) buf[sz] = '\0';
    138         close(fd);
    139     }
     147    rdsz = read(fd, buf, sz-1);
     148    close(fd);
     149    if (rdsz > 0) {
     150        if (rdsz == sz-1 && sz < PROC_MAX_FILE_SIZE) {
     151            sz *= 2;
     152            buf = xrealloc(buf, sz);
     153            goto again;
     154        }
     155        buf[rdsz] = '\0';
     156    }
     157    pf->file_sz = sz;
     158    pf->file = buf;
    140159}
    141160
     
    144163    if (pf->last_gen != gen) {
    145164        pf->last_gen = gen;
    146         // We allocate PROC_FILE_SIZE bytes. This wastes memory,
    147         // but allows us to allocate only once (at first sample)
    148         // per proc file, and reuse buffer for each sample
    149         if (!pf->file)
    150             pf->file = xmalloc(PROC_FILE_SIZE);
    151         readfile_z(pf->file, PROC_FILE_SIZE, proc_name[pf - &first_proc_file]);
     165        readfile_z(pf, proc_name[pf - &first_proc_file]);
    152166    }
    153167    return pf->file;
    154168}
    155169
    156 static inline ullong read_after_slash(const char *p)
     170static ullong read_after_slash(const char *p)
    157171{
    158172    p = strchr(p, '/');
     
    229243static int rdval_diskstats(const char* p, ullong *vec)
    230244{
    231     ullong rd = 0; // to avoid "warning: 'rd' might be used uninitialized"
     245    ullong rd = rd; // for compiler
    232246    int indexline = 0;
    233247    vec[0] = 0;
     
    258272{
    259273    char buf[5];
    260     smart_ulltoa5(ul, buf);
     274
     275    /* see http://en.wikipedia.org/wiki/Tera */
     276    smart_ulltoa4(ul, buf, " kmgtpezy");
     277    buf[4] = '\0';
    261278    put(buf);
    262279}
     
    266283typedef struct a { \
    267284    struct s_stat *next; \
    268     void (*collect)(struct a *s); \
     285    void (*collect)(struct a *s) FAST_FUNC; \
    269286    const char *label;
    270287#define S_STAT_END(a) } a;
     
    273290S_STAT_END(s_stat)
    274291
    275 static void collect_literal(s_stat *s)
     292static void FAST_FUNC collect_literal(s_stat *s UNUSED_PARAM)
    276293{
    277294}
     
    279296static s_stat* init_literal(void)
    280297{
    281     s_stat *s = xmalloc(sizeof(s_stat));
     298    s_stat *s = xzalloc(sizeof(*s));
    282299    s->collect = collect_literal;
    283300    return (s_stat*)s;
     
    286303static s_stat* init_delay(const char *param)
    287304{
    288     delta = bb_strtoi(param, NULL, 0) * 1000;
     305    delta = strtoul(param, NULL, 0) * 1000; /* param can be "" */
    289306    deltanz = delta > 0 ? delta : 1;
    290307    need_seconds = (1000000%deltanz) != 0;
     
    292309}
    293310
    294 static s_stat* init_cr(const char *param)
     311static s_stat* init_cr(const char *param UNUSED_PARAM)
    295312{
    296313    final_str = "\r";
     
    310327
    311328
    312 static void collect_cpu(cpu_stat *s)
     329static void FAST_FUNC collect_cpu(cpu_stat *s)
    313330{
    314331    ullong data[CPU_FIELDCNT] = { 0, 0, 0, 0, 0, 0, 0 };
     
    367384{
    368385    int sz;
    369     cpu_stat *s = xmalloc(sizeof(cpu_stat));
     386    cpu_stat *s = xzalloc(sizeof(*s));
    370387    s->collect = collect_cpu;
    371     sz = strtol(param, NULL, 0);
     388    sz = strtoul(param, NULL, 0); /* param can be "" */
    372389    if (sz < 10) sz = 10;
    373390    if (sz > 1000) sz = 1000;
    374     s->bar = xmalloc(sz+1);
    375     s->bar[sz] = '\0';
     391    s->bar = xzalloc(sz+1);
     392    /*s->bar[sz] = '\0'; - xzalloc did it */
    376393    s->bar_sz = sz;
    377394    return (s_stat*)s;
     
    384401S_STAT_END(int_stat)
    385402
    386 static void collect_int(int_stat *s)
     403static void FAST_FUNC collect_int(int_stat *s)
    387404{
    388405    ullong data[1];
     
    402419static s_stat* init_int(const char *param)
    403420{
    404     int_stat *s = xmalloc(sizeof(int_stat));
     421    int_stat *s = xzalloc(sizeof(*s));
    405422    s->collect = collect_int;
    406     if (param[0]=='\0') {
     423    if (param[0] == '\0') {
    407424        s->no = 1;
    408425    } else {
    409         int n = strtoul(param, NULL, 0);
    410         s->no = n+2;
     426        int n = xatoi_positive(param);
     427        s->no = n + 2;
    411428    }
    412429    return (s_stat*)s;
     
    418435S_STAT_END(ctx_stat)
    419436
    420 static void collect_ctx(ctx_stat *s)
     437static void FAST_FUNC collect_ctx(ctx_stat *s)
    421438{
    422439    ullong data[1];
     
    434451}
    435452
    436 static s_stat* init_ctx(const char *param)
    437 {
    438     ctx_stat *s = xmalloc(sizeof(ctx_stat));
     453static s_stat* init_ctx(const char *param UNUSED_PARAM)
     454{
     455    ctx_stat *s = xzalloc(sizeof(*s));
    439456    s->collect = collect_ctx;
    440457    return (s_stat*)s;
     
    447464S_STAT_END(blk_stat)
    448465
    449 static void collect_blk(blk_stat *s)
     466static void FAST_FUNC collect_blk(blk_stat *s)
    450467{
    451468    ullong data[2];
     
    476493}
    477494
    478 static s_stat* init_blk(const char *param)
    479 {
    480     blk_stat *s = xmalloc(sizeof(blk_stat));
     495static s_stat* init_blk(const char *param UNUSED_PARAM)
     496{
     497    blk_stat *s = xzalloc(sizeof(*s));
    481498    s->collect = collect_blk;
    482499    s->lookfor = "page";
     
    489506S_STAT_END(fork_stat)
    490507
    491 static void collect_thread_nr(fork_stat *s)
     508static void FAST_FUNC collect_thread_nr(fork_stat *s UNUSED_PARAM)
    492509{
    493510    ullong data[1];
     
    500517}
    501518
    502 static void collect_fork(fork_stat *s)
     519static void FAST_FUNC collect_fork(fork_stat *s)
    503520{
    504521    ullong data[1];
     
    518535static s_stat* init_fork(const char *param)
    519536{
    520     fork_stat *s = xmalloc(sizeof(fork_stat));
     537    fork_stat *s = xzalloc(sizeof(*s));
    521538    if (*param == 'n') {
    522539        s->collect = collect_thread_nr;
     
    534551S_STAT_END(if_stat)
    535552
    536 static void collect_if(if_stat *s)
     553static void FAST_FUNC collect_if(if_stat *s)
    537554{
    538555    ullong data[4];
     
    558575static s_stat* init_if(const char *device)
    559576{
    560     if_stat *s = xmalloc(sizeof(if_stat));
     577    if_stat *s = xzalloc(sizeof(*s));
    561578
    562579    if (!device || !device[0])
     
    565582
    566583    s->device = device;
    567     s->device_colon = xmalloc(strlen(device)+2);
    568     strcpy(s->device_colon, device);
    569     strcat(s->device_colon, ":");
     584    s->device_colon = xasprintf("%s:", device);
    570585    return (s_stat*)s;
    571586}
     
    611626//HugePages_Free:      0
    612627//Hugepagesize:     4096 kB
    613 static void collect_mem(mem_stat *s)
     628static void FAST_FUNC collect_mem(mem_stat *s)
    614629{
    615630    ullong m_total = 0;
     
    623638        return;
    624639    }
    625     if (s->opt == 'f') {
     640    if (s->opt == 't') {
    626641        scale(m_total << 10);
    627642        return;
     
    648663static s_stat* init_mem(const char *param)
    649664{
    650     mem_stat *s = xmalloc(sizeof(mem_stat));
     665    mem_stat *s = xzalloc(sizeof(*s));
    651666    s->collect = collect_mem;
    652667    s->opt = param[0];
     
    658673S_STAT_END(swp_stat)
    659674
    660 static void collect_swp(swp_stat *s)
     675static void FAST_FUNC collect_swp(swp_stat *s UNUSED_PARAM)
    661676{
    662677    ullong s_total[1];
     
    671686}
    672687
    673 static s_stat* init_swp(const char *param)
    674 {
    675     swp_stat *s = xmalloc(sizeof(swp_stat));
     688static s_stat* init_swp(const char *param UNUSED_PARAM)
     689{
     690    swp_stat *s = xzalloc(sizeof(*s));
    676691    s->collect = collect_swp;
    677692    return (s_stat*)s;
     
    682697S_STAT_END(fd_stat)
    683698
    684 static void collect_fd(fd_stat *s)
     699static void FAST_FUNC collect_fd(fd_stat *s UNUSED_PARAM)
    685700{
    686701    ullong data[2];
     
    694709}
    695710
    696 static s_stat* init_fd(const char *param)
    697 {
    698     fd_stat *s = xmalloc(sizeof(fd_stat));
     711static s_stat* init_fd(const char *param UNUSED_PARAM)
     712{
     713    fd_stat *s = xzalloc(sizeof(*s));
    699714    s->collect = collect_fd;
    700715    return (s_stat*)s;
     
    707722S_STAT_END(time_stat)
    708723
    709 static void collect_time(time_stat *s)
     724static void FAST_FUNC collect_time(time_stat *s)
    710725{
    711726    char buf[sizeof("12:34:56.123456")];
     
    729744{
    730745    int prec;
    731     time_stat *s = xmalloc(sizeof(time_stat));
     746    time_stat *s = xzalloc(sizeof(*s));
    732747
    733748    s->collect = collect_time;
    734     prec = param[0]-'0';
     749    prec = param[0] - '0';
    735750    if (prec < 0) prec = 0;
    736751    else if (prec > 6) prec = 6;
     
    742757}
    743758
    744 static void collect_info(s_stat *s)
     759static void FAST_FUNC collect_info(s_stat *s)
    745760{
    746761    gen ^= 1;
     
    771786};
    772787
    773 int nmeter_main(int argc, char **argv);
    774 int nmeter_main(int argc, char **argv)
     788int nmeter_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     789int nmeter_main(int argc UNUSED_PARAM, char **argv)
    775790{
    776791    char buf[32];
     
    784799    xchdir("/proc");
    785800
    786     if (argc != 2)
     801    if (!argv[1])
    787802        bb_show_usage();
    788803
    789     if (open_read_close("version", buf, sizeof(buf)) > 0)
    790         is26 = (strstr(buf, " 2.4.")==NULL);
     804    if (open_read_close("version", buf, sizeof(buf)-1) > 0) {
     805        buf[sizeof(buf)-1] = '\0';
     806        is26 = (strstr(buf, " 2.4.") == NULL);
     807    }
    791808
    792809    // Can use argv[1] directly, but this will mess up
     
    801818            break;
    802819        if (cur[1] == '%') {    // %%
    803             strcpy(cur, cur+1);
     820            overlapping_strcpy(cur, cur + 1);
    804821            cur++;
    805822            goto again;
     
    832849        if (s) {
    833850            s->label = prev;
    834             s->next = 0;
     851            /*s->next = NULL; - all initXXX funcs use xzalloc */
    835852            if (!first)
    836853                first = s;
     
    847864        s = init_literal();
    848865        s->label = prev;
    849         s->next = 0;
     866        /*s->next = NULL; - all initXXX funcs use xzalloc */
    850867        if (!first)
    851868            first = s;
Note: See TracChangeset for help on using the changeset viewer.