123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266 |
- #include "libbb.h"
- #define ESC "\033"
- typedef struct top_status_t {
- unsigned long vsz;
- #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
- unsigned long ticks;
- unsigned pcpu;
- #endif
- unsigned pid, ppid;
- unsigned uid;
- char state[4];
- char comm[COMM_LEN];
- #if ENABLE_FEATURE_TOP_SMP_PROCESS
- int last_seen_on_cpu;
- #endif
- } top_status_t;
- typedef struct jiffy_counts_t {
-
- unsigned long long usr, nic, sys, idle;
- unsigned long long iowait, irq, softirq, steal;
- unsigned long long total;
- unsigned long long busy;
- } jiffy_counts_t;
- typedef struct save_hist {
- unsigned long ticks;
- pid_t pid;
- } save_hist;
- typedef int (*cmp_funcp)(top_status_t *P, top_status_t *Q);
- enum { SORT_DEPTH = 3 };
- enum { LINE_BUF_SIZE = 512 - 64 };
- struct globals {
- top_status_t *top;
- int ntop;
- smallint inverted;
- #if ENABLE_FEATURE_TOPMEM
- smallint sort_field;
- #endif
- #if ENABLE_FEATURE_TOP_SMP_CPU
- smallint smp_cpu_info;
- #endif
- unsigned lines;
- #if ENABLE_FEATURE_TOP_INTERACTIVE
- struct termios initial_settings;
- int scroll_ofs;
- #define G_scroll_ofs G.scroll_ofs
- #else
- #define G_scroll_ofs 0
- #endif
- #if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
- cmp_funcp sort_function[1];
- #else
- cmp_funcp sort_function[SORT_DEPTH];
- struct save_hist *prev_hist;
- int prev_hist_count;
- jiffy_counts_t cur_jif, prev_jif;
-
- unsigned total_pcpu;
-
- #endif
- #if ENABLE_FEATURE_TOP_SMP_CPU
-
- jiffy_counts_t *cpu_jif, *cpu_prev_jif;
- int num_cpus;
- #endif
- #if ENABLE_FEATURE_TOP_INTERACTIVE
- char kbd_input[KEYCODE_BUFFER_SIZE];
- #endif
- char line_buf[LINE_BUF_SIZE];
- };
- #define G (*ptr_to_globals)
- #define top (G.top )
- #define ntop (G.ntop )
- #define sort_field (G.sort_field )
- #define inverted (G.inverted )
- #define smp_cpu_info (G.smp_cpu_info )
- #define initial_settings (G.initial_settings )
- #define sort_function (G.sort_function )
- #define prev_hist (G.prev_hist )
- #define prev_hist_count (G.prev_hist_count )
- #define cur_jif (G.cur_jif )
- #define prev_jif (G.prev_jif )
- #define cpu_jif (G.cpu_jif )
- #define cpu_prev_jif (G.cpu_prev_jif )
- #define num_cpus (G.num_cpus )
- #define total_pcpu (G.total_pcpu )
- #define line_buf (G.line_buf )
- #define INIT_G() do { \
- SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
- BUILD_BUG_ON(LINE_BUF_SIZE <= 80); \
- } while (0)
- enum {
- OPT_d = (1 << 0),
- OPT_n = (1 << 1),
- OPT_b = (1 << 2),
- OPT_m = (1 << 3),
- OPT_EOF = (1 << 4),
- };
- #define OPT_BATCH_MODE (option_mask32 & OPT_b)
- #if ENABLE_FEATURE_TOP_INTERACTIVE
- static int pid_sort(top_status_t *P, top_status_t *Q)
- {
-
-
- return (Q->pid - P->pid);
- }
- #endif
- static int mem_sort(top_status_t *P, top_status_t *Q)
- {
-
- if (Q->vsz < P->vsz) return -1;
- return Q->vsz != P->vsz;
- }
- #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
- static int pcpu_sort(top_status_t *P, top_status_t *Q)
- {
-
-
- return (int)Q->pcpu - (int)P->pcpu;
- }
- static int time_sort(top_status_t *P, top_status_t *Q)
- {
-
- if (Q->ticks < P->ticks) return -1;
- return Q->ticks != P->ticks;
- }
- static int mult_lvl_cmp(void* a, void* b)
- {
- int i, cmp_val;
- for (i = 0; i < SORT_DEPTH; i++) {
- cmp_val = (*sort_function[i])(a, b);
- if (cmp_val != 0)
- break;
- }
- return inverted ? -cmp_val : cmp_val;
- }
- static NOINLINE int read_cpu_jiffy(FILE *fp, jiffy_counts_t *p_jif)
- {
- #if !ENABLE_FEATURE_TOP_SMP_CPU
- static const char fmt[] ALIGN1 = "cpu %llu %llu %llu %llu %llu %llu %llu %llu";
- #else
- static const char fmt[] ALIGN1 = "cp%*s %llu %llu %llu %llu %llu %llu %llu %llu";
- #endif
- int ret;
- if (!fgets(line_buf, LINE_BUF_SIZE, fp) || line_buf[0] != 'c' )
- return 0;
- ret = sscanf(line_buf, fmt,
- &p_jif->usr, &p_jif->nic, &p_jif->sys, &p_jif->idle,
- &p_jif->iowait, &p_jif->irq, &p_jif->softirq,
- &p_jif->steal);
- if (ret >= 4) {
- p_jif->total = p_jif->usr + p_jif->nic + p_jif->sys + p_jif->idle
- + p_jif->iowait + p_jif->irq + p_jif->softirq + p_jif->steal;
-
- p_jif->busy = p_jif->total - p_jif->idle - p_jif->iowait;
- }
- return ret;
- }
- static void get_jiffy_counts(void)
- {
- FILE* fp = xfopen_for_read("stat");
-
- prev_jif = cur_jif;
- if (read_cpu_jiffy(fp, &cur_jif) < 4)
- bb_error_msg_and_die("can't read '%s'", "/proc/stat");
- #if !ENABLE_FEATURE_TOP_SMP_CPU
- fclose(fp);
- return;
- #else
- if (!smp_cpu_info) {
- fclose(fp);
- return;
- }
- if (!num_cpus) {
-
- while (1) {
- cpu_jif = xrealloc_vector(cpu_jif, 1, num_cpus);
- if (read_cpu_jiffy(fp, &cpu_jif[num_cpus]) <= 4)
- break;
- num_cpus++;
- }
- if (num_cpus == 0)
- smp_cpu_info = 0;
- cpu_prev_jif = xzalloc(sizeof(cpu_prev_jif[0]) * num_cpus);
-
- usleep(50000);
- } else {
- jiffy_counts_t *tmp;
- int i;
-
- tmp = cpu_prev_jif;
- cpu_prev_jif = cpu_jif;
- cpu_jif = tmp;
-
- for (i = 0; i < num_cpus; i++)
- read_cpu_jiffy(fp, &cpu_jif[i]);
- }
- #endif
- fclose(fp);
- }
- static void do_stats(void)
- {
- top_status_t *cur;
- pid_t pid;
- int i, last_i, n;
- struct save_hist *new_hist;
- get_jiffy_counts();
- total_pcpu = 0;
-
- new_hist = xmalloc(sizeof(new_hist[0]) * ntop);
-
-
- i = 0;
- for (n = 0; n < ntop; n++) {
- cur = top + n;
-
- pid = cur->pid;
- new_hist[n].ticks = cur->ticks;
- new_hist[n].pid = pid;
-
- cur->pcpu = 0;
-
- last_i = i;
- if (prev_hist_count) do {
- if (prev_hist[i].pid == pid) {
- cur->pcpu = cur->ticks - prev_hist[i].ticks;
- total_pcpu += cur->pcpu;
- break;
- }
- i = (i+1) % prev_hist_count;
-
- } while (i != last_i);
-
- }
-
- free(prev_hist);
- prev_hist = new_hist;
- prev_hist_count = ntop;
- }
- #endif
- #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && ENABLE_FEATURE_TOP_DECIMALS
- static char *fmt_100percent_8(char pbuf[8], unsigned value, unsigned total)
- {
- unsigned t;
- if (value >= total) {
- strcpy(pbuf, " 100% ");
- return pbuf;
- }
-
- value = 1000 * value / total;
- t = value / 100;
- value = value % 100;
- pbuf[0] = ' ';
- pbuf[1] = t ? t + '0' : ' ';
- pbuf[2] = '0' + (value / 10);
- pbuf[3] = '.';
- pbuf[4] = '0' + (value % 10);
- pbuf[5] = '%';
- pbuf[6] = ' ';
- pbuf[7] = '\0';
- return pbuf;
- }
- #endif
- #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
- static void display_cpus(int scr_width, char *scrbuf, int *lines_rem_p)
- {
-
- unsigned total_diff;
- jiffy_counts_t *p_jif, *p_prev_jif;
- int i;
- # if ENABLE_FEATURE_TOP_SMP_CPU
- int n_cpu_lines;
- # endif
-
- # define CALC_TOTAL_DIFF do { \
- total_diff = (unsigned)(p_jif->total - p_prev_jif->total); \
- if (total_diff == 0) total_diff = 1; \
- } while (0)
- # if ENABLE_FEATURE_TOP_DECIMALS
- # define CALC_STAT(xxx) char xxx[8]
- # define SHOW_STAT(xxx) fmt_100percent_8(xxx, (unsigned)(p_jif->xxx - p_prev_jif->xxx), total_diff)
- # define FMT "%s"
- # else
- # define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(p_jif->xxx - p_prev_jif->xxx) / total_diff
- # define SHOW_STAT(xxx) xxx
- # define FMT "%4u%% "
- # endif
- # if !ENABLE_FEATURE_TOP_SMP_CPU
- {
- i = 1;
- p_jif = &cur_jif;
- p_prev_jif = &prev_jif;
- # else
-
- n_cpu_lines = smp_cpu_info ? num_cpus : 1;
- if (n_cpu_lines > *lines_rem_p)
- n_cpu_lines = *lines_rem_p;
- for (i = 0; i < n_cpu_lines; i++) {
- p_jif = &cpu_jif[i];
- p_prev_jif = &cpu_prev_jif[i];
- # endif
- CALC_TOTAL_DIFF;
- {
- CALC_STAT(usr);
- CALC_STAT(sys);
- CALC_STAT(nic);
- CALC_STAT(idle);
- CALC_STAT(iowait);
- CALC_STAT(irq);
- CALC_STAT(softirq);
-
- snprintf(scrbuf, scr_width,
-
- # if ENABLE_FEATURE_TOP_SMP_CPU
- "CPU%s:"FMT"usr"FMT"sys"FMT"nic"FMT"idle"FMT"io"FMT"irq"FMT"sirq",
- (smp_cpu_info ? utoa(i) : ""),
- # else
- "CPU:"FMT"usr"FMT"sys"FMT"nic"FMT"idle"FMT"io"FMT"irq"FMT"sirq",
- # endif
- SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
- SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
-
-
- );
- puts(scrbuf);
- }
- }
- # undef SHOW_STAT
- # undef CALC_STAT
- # undef FMT
- *lines_rem_p -= i;
- }
- #else
- # define display_cpus(scr_width, scrbuf, lines_rem) ((void)0)
- #endif
- enum {
- MI_MEMTOTAL,
- MI_MEMFREE,
- MI_MEMSHARED,
- MI_SHMEM,
- MI_BUFFERS,
- MI_CACHED,
- MI_SWAPTOTAL,
- MI_SWAPFREE,
- MI_DIRTY,
- MI_WRITEBACK,
- MI_ANONPAGES,
- MI_MAPPED,
- MI_SLAB,
- MI_MAX
- };
- static void parse_meminfo(unsigned long meminfo[MI_MAX])
- {
- static const char fields[] ALIGN1 =
- "MemTotal\0"
- "MemFree\0"
- "MemShared\0"
- "Shmem\0"
- "Buffers\0"
- "Cached\0"
- "SwapTotal\0"
- "SwapFree\0"
- "Dirty\0"
- "Writeback\0"
- "AnonPages\0"
- "Mapped\0"
- "Slab\0";
- char buf[60];
- FILE *f;
- int i;
- memset(meminfo, 0, sizeof(meminfo[0]) * MI_MAX);
- f = xfopen_for_read("meminfo");
- while (fgets(buf, sizeof(buf), f) != NULL) {
- char *c = strchr(buf, ':');
- if (!c)
- continue;
- *c = '\0';
- i = index_in_strings(fields, buf);
- if (i >= 0)
- meminfo[i] = strtoul(c+1, NULL, 10);
- }
- fclose(f);
- }
- static unsigned long display_header(int scr_width, int *lines_rem_p)
- {
- char scrbuf[100];
- char *buf;
- unsigned long meminfo[MI_MAX];
- parse_meminfo(meminfo);
-
- if (scr_width > (int)sizeof(scrbuf))
- scr_width = sizeof(scrbuf);
- snprintf(scrbuf, scr_width,
- "Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached",
- meminfo[MI_MEMTOTAL] - meminfo[MI_MEMFREE],
- meminfo[MI_MEMFREE],
- meminfo[MI_MEMSHARED] + meminfo[MI_SHMEM],
- meminfo[MI_BUFFERS],
- meminfo[MI_CACHED]);
-
- printf(OPT_BATCH_MODE ? "%s\n" : ESC"[H" ESC"[J" "%s\n", scrbuf);
- (*lines_rem_p)--;
-
- display_cpus(scr_width, scrbuf, lines_rem_p);
-
- buf = stpcpy(scrbuf, "Load average: ");
- open_read_close("loadavg", buf, sizeof(scrbuf) - sizeof("Load average: "));
- scrbuf[scr_width - 1] = '\0';
- strchrnul(buf, '\n')[0] = '\0';
- puts(scrbuf);
- (*lines_rem_p)--;
- return meminfo[MI_MEMTOTAL];
- }
- static NOINLINE void display_process_list(int lines_rem, int scr_width)
- {
- enum {
- BITS_PER_INT = sizeof(int) * 8
- };
- top_status_t *s;
- char vsz_str_buf[8];
- unsigned long total_memory = display_header(scr_width, &lines_rem);
-
- unsigned pmem_shift, pmem_scale, pmem_half;
- #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
- unsigned tmp_unsigned;
- unsigned pcpu_shift, pcpu_scale, pcpu_half;
- unsigned busy_jifs;
- #endif
-
- printf(OPT_BATCH_MODE ? "%.*s" : ESC"[7m" "%.*s" ESC"[m", scr_width,
- " PID PPID USER STAT VSZ %VSZ"
- IF_FEATURE_TOP_SMP_PROCESS(" CPU")
- IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE(" %CPU")
- " COMMAND");
- lines_rem--;
- #if ENABLE_FEATURE_TOP_DECIMALS
- # define UPSCALE 1000
- # define CALC_STAT(name, val) div_t name = div((val), 10)
- # define SHOW_STAT(name) name.quot, '0'+name.rem
- # define FMT "%3u.%c"
- #else
- # define UPSCALE 100
- # define CALC_STAT(name, val) unsigned name = (val)
- # define SHOW_STAT(name) name
- # define FMT "%4u%%"
- #endif
-
- pmem_shift = BITS_PER_INT-11;
- pmem_scale = UPSCALE*(1U<<(BITS_PER_INT-11)) / total_memory;
-
- while (pmem_scale >= 512) {
- pmem_scale /= 4;
- pmem_shift -= 2;
- }
- pmem_half = (1U << pmem_shift) / (ENABLE_FEATURE_TOP_DECIMALS ? 20 : 2);
- #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
- busy_jifs = cur_jif.busy - prev_jif.busy;
-
- if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
-
-
- pcpu_shift = 6;
- pcpu_scale = UPSCALE*64 * (uint16_t)busy_jifs;
- if (pcpu_scale == 0)
- pcpu_scale = 1;
- while (pcpu_scale < (1U << (BITS_PER_INT-2))) {
- pcpu_scale *= 4;
- pcpu_shift += 2;
- }
- tmp_unsigned = (uint16_t)(cur_jif.total - prev_jif.total) * total_pcpu;
- if (tmp_unsigned != 0)
- pcpu_scale /= tmp_unsigned;
-
- while (pcpu_scale >= 1024) {
- pcpu_scale /= 4;
- pcpu_shift -= 2;
- }
- pcpu_half = (1U << pcpu_shift) / (ENABLE_FEATURE_TOP_DECIMALS ? 20 : 2);
-
- #endif
-
- scr_width += 2;
- if (lines_rem > ntop - G_scroll_ofs)
- lines_rem = ntop - G_scroll_ofs;
- s = top + G_scroll_ofs;
- while (--lines_rem >= 0) {
- unsigned col;
- CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift);
- #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
- CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift);
- #endif
- if (s->vsz >= 100000)
- sprintf(vsz_str_buf, "%6ldm", s->vsz/1024);
- else
- sprintf(vsz_str_buf, "%7lu", s->vsz);
-
- col = snprintf(line_buf, scr_width,
- "\n" "%5u%6u %-8.8s %s%s" FMT
- IF_FEATURE_TOP_SMP_PROCESS(" %3d")
- IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE(FMT)
- " ",
- s->pid, s->ppid, get_cached_username(s->uid),
- s->state, vsz_str_buf,
- SHOW_STAT(pmem)
- IF_FEATURE_TOP_SMP_PROCESS(, s->last_seen_on_cpu)
- IF_FEATURE_TOP_CPU_USAGE_PERCENTAGE(, SHOW_STAT(pcpu))
- );
- if ((int)(col + 1) < scr_width)
- read_cmdline(line_buf + col, scr_width - col, s->pid, s->comm);
- fputs(line_buf, stdout);
-
- s++;
- }
-
- bb_putchar(OPT_BATCH_MODE ? '\n' : '\r');
- fflush_all();
- }
- #undef UPSCALE
- #undef SHOW_STAT
- #undef CALC_STAT
- #undef FMT
- static void clearmems(void)
- {
- clear_username_cache();
- free(top);
- top = NULL;
- }
- #if ENABLE_FEATURE_TOP_INTERACTIVE
- static void reset_term(void)
- {
- if (!OPT_BATCH_MODE)
- tcsetattr_stdin_TCSANOW(&initial_settings);
- }
- static void sig_catcher(int sig)
- {
- reset_term();
- kill_myself_with_sig(sig);
- }
- #endif
- typedef unsigned long mem_t;
- typedef struct topmem_status_t {
- unsigned pid;
- char comm[COMM_LEN];
-
- mem_t vsz ;
- mem_t vszrw ;
- mem_t rss ;
- mem_t rss_sh ;
- mem_t dirty ;
- mem_t dirty_sh;
- mem_t stack ;
- } topmem_status_t;
- enum { NUM_SORT_FIELD = 7 };
- #define topmem ((topmem_status_t*)top)
- #if ENABLE_FEATURE_TOPMEM
- static int topmem_sort(char *a, char *b)
- {
- int n;
- mem_t l, r;
- n = offsetof(topmem_status_t, vsz) + (sort_field * sizeof(mem_t));
- l = *(mem_t*)(a + n);
- r = *(mem_t*)(b + n);
- if (l == r) {
- l = ((topmem_status_t*)a)->dirty;
- r = ((topmem_status_t*)b)->dirty;
- }
-
-
- n = (l > r) ? -1 : (l != r);
- return inverted ? -n : n;
- }
- static void display_topmem_header(int scr_width, int *lines_rem_p)
- {
- unsigned long meminfo[MI_MAX];
- parse_meminfo(meminfo);
- snprintf(line_buf, LINE_BUF_SIZE,
- "Mem total:%lu anon:%lu map:%lu free:%lu",
- meminfo[MI_MEMTOTAL],
- meminfo[MI_ANONPAGES],
- meminfo[MI_MAPPED],
- meminfo[MI_MEMFREE]);
- printf(OPT_BATCH_MODE ? "%.*s\n" : ESC"[H" ESC"[J" "%.*s\n", scr_width, line_buf);
- snprintf(line_buf, LINE_BUF_SIZE,
- " slab:%lu buf:%lu cache:%lu dirty:%lu write:%lu",
- meminfo[MI_SLAB],
- meminfo[MI_BUFFERS],
- meminfo[MI_CACHED],
- meminfo[MI_DIRTY],
- meminfo[MI_WRITEBACK]);
- printf("%.*s\n", scr_width, line_buf);
- snprintf(line_buf, LINE_BUF_SIZE,
- "Swap total:%lu free:%lu",
- meminfo[MI_SWAPTOTAL],
- meminfo[MI_SWAPFREE]);
- printf("%.*s\n", scr_width, line_buf);
- (*lines_rem_p) -= 3;
- }
- static void ulltoa6_and_space(unsigned long long ul, char buf[6])
- {
-
- smart_ulltoa5(ul, buf, " mgtpezy")[0] = ' ';
- }
- static NOINLINE void display_topmem_process_list(int lines_rem, int scr_width)
- {
- #define HDR_STR " PID VSZ VSZRW RSS (SHR) DIRTY (SHR) STACK"
- #define MIN_WIDTH sizeof(HDR_STR)
- const topmem_status_t *s = topmem + G_scroll_ofs;
- char *cp, ch;
- display_topmem_header(scr_width, &lines_rem);
- strcpy(line_buf, HDR_STR " COMMAND");
-
- cp = &line_buf[5 + sort_field * 6];
- ch = "^_"[inverted];
- cp[6] = ch;
- do *cp++ = ch; while (*cp == ' ');
- printf(OPT_BATCH_MODE ? "%.*s" : ESC"[7m" "%.*s" ESC"[m", scr_width, line_buf);
- lines_rem--;
- if (lines_rem > ntop - G_scroll_ofs)
- lines_rem = ntop - G_scroll_ofs;
- while (--lines_rem >= 0) {
-
- ulltoa6_and_space(s->pid , &line_buf[0*6]);
- ulltoa6_and_space(s->vsz , &line_buf[1*6]);
- ulltoa6_and_space(s->vszrw , &line_buf[2*6]);
- ulltoa6_and_space(s->rss , &line_buf[3*6]);
- ulltoa6_and_space(s->rss_sh , &line_buf[4*6]);
- ulltoa6_and_space(s->dirty , &line_buf[5*6]);
- ulltoa6_and_space(s->dirty_sh, &line_buf[6*6]);
- ulltoa6_and_space(s->stack , &line_buf[7*6]);
- line_buf[8*6] = '\0';
- if (scr_width > (int)MIN_WIDTH) {
- read_cmdline(&line_buf[8*6], scr_width - MIN_WIDTH, s->pid, s->comm);
- }
- printf("\n""%.*s", scr_width, line_buf);
- s++;
- }
- bb_putchar(OPT_BATCH_MODE ? '\n' : '\r');
- fflush_all();
- #undef HDR_STR
- #undef MIN_WIDTH
- }
- #else
- void display_topmem_process_list(int lines_rem, int scr_width);
- int topmem_sort(char *a, char *b);
- #endif
- enum {
- TOP_MASK = 0
- | PSSCAN_PID
- | PSSCAN_PPID
- | PSSCAN_VSZ
- | PSSCAN_STIME
- | PSSCAN_UTIME
- | PSSCAN_STATE
- | PSSCAN_COMM
- | PSSCAN_CPU
- | PSSCAN_UIDGID,
- TOPMEM_MASK = 0
- | PSSCAN_PID
- | PSSCAN_SMAPS
- | PSSCAN_COMM,
- EXIT_MASK = (unsigned)-1,
- };
- #if ENABLE_FEATURE_TOP_INTERACTIVE
- static unsigned handle_input(unsigned scan_mask, unsigned interval)
- {
- if (option_mask32 & OPT_EOF) {
-
- sleep(interval);
- return scan_mask;
- }
- while (1) {
- int32_t c;
- c = read_key(STDIN_FILENO, G.kbd_input, interval * 1000);
- if (c == -1 && errno != EAGAIN) {
-
- option_mask32 |= OPT_EOF;
- break;
- }
- interval = 0;
- if (c == initial_settings.c_cc[VINTR])
- return EXIT_MASK;
- if (c == initial_settings.c_cc[VEOF])
- return EXIT_MASK;
- if (c == KEYCODE_UP) {
- G_scroll_ofs--;
- goto normalize_ofs;
- }
- if (c == KEYCODE_DOWN) {
- G_scroll_ofs++;
- goto normalize_ofs;
- }
- if (c == KEYCODE_HOME) {
- G_scroll_ofs = 0;
- break;
- }
- if (c == KEYCODE_END) {
- G_scroll_ofs = ntop - G.lines / 2;
- goto normalize_ofs;
- }
- if (c == KEYCODE_PAGEUP) {
- G_scroll_ofs -= G.lines / 2;
- goto normalize_ofs;
- }
- if (c == KEYCODE_PAGEDOWN) {
- G_scroll_ofs += G.lines / 2;
- normalize_ofs:
- if (G_scroll_ofs >= ntop)
- G_scroll_ofs = ntop - 1;
- if (G_scroll_ofs < 0)
- G_scroll_ofs = 0;
- break;
- }
- c |= 0x20;
- if (c == 'q')
- return EXIT_MASK;
- if (c == 'n') {
- IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
- sort_function[0] = pid_sort;
- continue;
- }
- if (c == 'm') {
- IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
- sort_function[0] = mem_sort;
- # if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
- sort_function[1] = pcpu_sort;
- sort_function[2] = time_sort;
- # endif
- continue;
- }
- # if ENABLE_FEATURE_SHOW_THREADS
- if (c == 'h'
- IF_FEATURE_TOPMEM(&& scan_mask != TOPMEM_MASK)
- ) {
- scan_mask ^= PSSCAN_TASKS;
- continue;
- }
- # endif
- # if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
- if (c == 'p') {
- IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
- sort_function[0] = pcpu_sort;
- sort_function[1] = mem_sort;
- sort_function[2] = time_sort;
- continue;
- }
- if (c == 't') {
- IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
- sort_function[0] = time_sort;
- sort_function[1] = mem_sort;
- sort_function[2] = pcpu_sort;
- continue;
- }
- # if ENABLE_FEATURE_TOPMEM
- if (c == 's') {
- scan_mask = TOPMEM_MASK;
- free(prev_hist);
- prev_hist = NULL;
- prev_hist_count = 0;
- sort_field = (sort_field + 1) % NUM_SORT_FIELD;
- continue;
- }
- # endif
- if (c == 'r') {
- inverted ^= 1;
- continue;
- }
- # if ENABLE_FEATURE_TOP_SMP_CPU
-
- if (c == 'c' || c == '1') {
-
- if (smp_cpu_info) {
- free(cpu_prev_jif);
- free(cpu_jif);
- cpu_jif = &cur_jif;
- cpu_prev_jif = &prev_jif;
- } else {
-
- cpu_jif = cpu_prev_jif = NULL;
- }
- num_cpus = 0;
- smp_cpu_info = !smp_cpu_info;
- get_jiffy_counts();
- continue;
- }
- # endif
- # endif
- break;
- }
- return scan_mask;
- }
- #endif
- int top_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int top_main(int argc UNUSED_PARAM, char **argv)
- {
- int iterations;
- unsigned col;
- unsigned interval;
- char *str_interval, *str_iterations;
- unsigned scan_mask = TOP_MASK;
- INIT_G();
- interval = 5;
- iterations = 0;
- #if ENABLE_FEATURE_TOP_SMP_CPU
-
-
- cpu_jif = &cur_jif;
- cpu_prev_jif = &prev_jif;
- #endif
-
- make_all_argv_opts(argv);
- col = getopt32(argv, "d:n:b"IF_FEATURE_TOPMEM("m"), &str_interval, &str_iterations);
- #if ENABLE_FEATURE_TOPMEM
- if (col & OPT_m)
- scan_mask = TOPMEM_MASK;
- #endif
- if (col & OPT_d) {
-
- if (str_interval[0] == '-')
- str_interval++;
-
- interval = xatou16(str_interval);
- }
- if (col & OPT_n) {
- if (str_iterations[0] == '-')
- str_iterations++;
- iterations = xatou(str_iterations);
- }
-
- xchdir("/proc");
- #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
- sort_function[0] = pcpu_sort;
- sort_function[1] = mem_sort;
- sort_function[2] = time_sort;
- #else
- sort_function[0] = mem_sort;
- #endif
- if (OPT_BATCH_MODE) {
- option_mask32 |= OPT_EOF;
- }
- #if ENABLE_FEATURE_TOP_INTERACTIVE
- else {
-
- set_termios_to_raw(STDIN_FILENO, &initial_settings, TERMIOS_CLEAR_ISIG);
- die_func = reset_term;
- }
- bb_signals(BB_FATAL_SIGS, sig_catcher);
-
- scan_mask = handle_input(scan_mask, 0);
- #endif
- while (scan_mask != EXIT_MASK) {
- procps_status_t *p = NULL;
- if (OPT_BATCH_MODE) {
- G.lines = INT_MAX;
- col = LINE_BUF_SIZE - 2;
- } else {
- G.lines = 24;
- col = 79;
-
- get_terminal_width_height(STDOUT_FILENO, &col, &G.lines);
- if (G.lines < 5 || col < 10) {
- sleep(interval);
- continue;
- }
- if (col > LINE_BUF_SIZE - 2)
- col = LINE_BUF_SIZE - 2;
- }
-
- ntop = 0;
- while ((p = procps_scan(p, scan_mask)) != NULL) {
- int n;
- IF_FEATURE_TOPMEM(if (scan_mask != TOPMEM_MASK)) {
- n = ntop;
- top = xrealloc_vector(top, 6, ntop++);
- top[n].pid = p->pid;
- top[n].ppid = p->ppid;
- top[n].vsz = p->vsz;
- #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
- top[n].ticks = p->stime + p->utime;
- #endif
- top[n].uid = p->uid;
- strcpy(top[n].state, p->state);
- strcpy(top[n].comm, p->comm);
- #if ENABLE_FEATURE_TOP_SMP_PROCESS
- top[n].last_seen_on_cpu = p->last_seen_on_cpu;
- #endif
- }
- #if ENABLE_FEATURE_TOPMEM
- else {
- if (!(p->smaps.mapped_ro | p->smaps.mapped_rw))
- continue;
- n = ntop;
-
- top = xrealloc_vector(topmem, 6, ntop++);
- strcpy(topmem[n].comm, p->comm);
- topmem[n].pid = p->pid;
- topmem[n].vsz = p->smaps.mapped_rw + p->smaps.mapped_ro;
- topmem[n].vszrw = p->smaps.mapped_rw;
- topmem[n].rss_sh = p->smaps.shared_clean + p->smaps.shared_dirty;
- topmem[n].rss = p->smaps.private_clean + p->smaps.private_dirty + topmem[n].rss_sh;
- topmem[n].dirty = p->smaps.private_dirty + p->smaps.shared_dirty;
- topmem[n].dirty_sh = p->smaps.shared_dirty;
- topmem[n].stack = p->smaps.stack;
- }
- #endif
- }
- if (ntop == 0) {
- bb_error_msg("no process info in /proc");
- break;
- }
- IF_FEATURE_TOPMEM(if (scan_mask != TOPMEM_MASK)) {
- #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
- if (!prev_hist_count) {
- do_stats();
- usleep(100000);
- clearmems();
- continue;
- }
- do_stats();
-
- qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
- #else
- qsort(top, ntop, sizeof(top_status_t), (void*)(sort_function[0]));
- #endif
- display_process_list(G.lines, col);
- }
- #if ENABLE_FEATURE_TOPMEM
- else {
- qsort(topmem, ntop, sizeof(topmem_status_t), (void*)topmem_sort);
- display_topmem_process_list(G.lines, col);
- }
- #endif
- clearmems();
- if (iterations >= 0 && !--iterations)
- break;
- #if !ENABLE_FEATURE_TOP_INTERACTIVE
- sleep(interval);
- #else
- scan_mask = handle_input(scan_mask, interval);
- #endif
- }
- bb_putchar('\n');
- #if ENABLE_FEATURE_TOP_INTERACTIVE
- reset_term();
- #endif
- if (ENABLE_FEATURE_CLEAN_UP) {
- clearmems();
- #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
- free(prev_hist);
- #endif
- }
- return EXIT_SUCCESS;
- }
|