Bernhard Reutner-Fischer | d9cf7ac | 2006-04-12 18:39:58 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 2 | /* |
| 3 | * A tiny 'top' utility. |
| 4 | * |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 5 | * This is written specifically for the linux /proc/<PID>/stat(m) |
| 6 | * files format. |
| 7 | |
| 8 | * This reads the PIDs of all processes and their status and shows |
| 9 | * the status of processes (first ones that fit to screen) at given |
| 10 | * intervals. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 11 | * |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 12 | * NOTES: |
| 13 | * - At startup this changes to /proc, all the reads are then |
| 14 | * relative to that. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 15 | * |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 16 | * (C) Eero Tamminen <oak at welho dot com> |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 17 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 18 | * Rewritten by Vladimir Oleynik (C) 2002 <dzo@simtreas.ru> |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 19 | */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 20 | |
| 21 | /* Original code Copyrights */ |
| 22 | /* |
| 23 | * Copyright (c) 1992 Branko Lankester |
| 24 | * Copyright (c) 1992 Roger Binns |
| 25 | * Copyright (C) 1994-1996 Charles L. Blake. |
| 26 | * Copyright (C) 1992-1998 Michael K. Johnson |
| 27 | * May be distributed under the conditions of the |
| 28 | * GNU Library General Public License |
| 29 | */ |
| 30 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 31 | #include "libbb.h" |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 32 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 34 | typedef struct top_status_t { |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 35 | unsigned long vsz; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 36 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
| 37 | unsigned long ticks; |
| 38 | unsigned pcpu; /* delta of ticks */ |
| 39 | #endif |
| 40 | unsigned pid, ppid; |
| 41 | unsigned uid; |
| 42 | char state[4]; |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 43 | char comm[COMM_LEN]; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 44 | } top_status_t; |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 46 | typedef struct jiffy_counts_t { |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 47 | unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal; |
| 48 | unsigned long long total; |
| 49 | unsigned long long busy; |
| 50 | } jiffy_counts_t; |
| 51 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 52 | /* This structure stores some critical information from one frame to |
| 53 | the next. Used for finding deltas. */ |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 54 | typedef struct save_hist { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 55 | unsigned long ticks; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 56 | pid_t pid; |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 57 | } save_hist; |
| 58 | |
| 59 | typedef int (*cmp_funcp)(top_status_t *P, top_status_t *Q); |
| 60 | |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 62 | enum { SORT_DEPTH = 3 }; |
| 63 | |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 65 | struct globals { |
| 66 | top_status_t *top; |
| 67 | int ntop; |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 68 | #if ENABLE_FEATURE_TOPMEM |
| 69 | smallint sort_field; |
| 70 | smallint inverted; |
| 71 | #endif |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 72 | #if ENABLE_FEATURE_USE_TERMIOS |
| 73 | struct termios initial_settings; |
| 74 | #endif |
| 75 | #if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Denis Vlasenko | 8bdba4d | 2007-08-29 18:18:08 +0000 | [diff] [blame] | 76 | cmp_funcp sort_function[1]; |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 77 | #else |
| 78 | cmp_funcp sort_function[SORT_DEPTH]; |
| 79 | struct save_hist *prev_hist; |
| 80 | int prev_hist_count; |
| 81 | jiffy_counts_t jif, prev_jif; |
| 82 | /* int hist_iterations; */ |
| 83 | unsigned total_pcpu; |
| 84 | /* unsigned long total_vsz; */ |
| 85 | #endif |
Denis Vlasenko | 5576136 | 2007-10-16 22:53:05 +0000 | [diff] [blame] | 86 | char line_buf[80]; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 87 | }; |
Denis Vlasenko | 5576136 | 2007-10-16 22:53:05 +0000 | [diff] [blame] | 88 | |
Denis Vlasenko | 4c1d88d | 2007-09-08 17:34:05 +0000 | [diff] [blame] | 89 | enum { LINE_BUF_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line_buf) }; |
Denis Vlasenko | 5576136 | 2007-10-16 22:53:05 +0000 | [diff] [blame] | 90 | |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 91 | #define G (*(struct globals*)&bb_common_bufsiz1) |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 92 | #define INIT_G() do { \ |
| 93 | struct G_sizecheck { \ |
| 94 | char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \ |
| 95 | }; \ |
| 96 | } while (0) |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 97 | #define top (G.top ) |
| 98 | #define ntop (G.ntop ) |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 99 | #define sort_field (G.sort_field ) |
| 100 | #define inverted (G.inverted ) |
| 101 | #define initial_settings (G.initial_settings ) |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 102 | #define sort_function (G.sort_function ) |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 103 | #define prev_hist (G.prev_hist ) |
| 104 | #define prev_hist_count (G.prev_hist_count ) |
| 105 | #define jif (G.jif ) |
| 106 | #define prev_jif (G.prev_jif ) |
| 107 | #define total_pcpu (G.total_pcpu ) |
Denis Vlasenko | 4c1d88d | 2007-09-08 17:34:05 +0000 | [diff] [blame] | 108 | #define line_buf (G.line_buf ) |
Denis Vlasenko | 8bdba4d | 2007-08-29 18:18:08 +0000 | [diff] [blame] | 109 | |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 110 | enum { |
| 111 | OPT_d = (1 << 0), |
| 112 | OPT_n = (1 << 1), |
| 113 | OPT_b = (1 << 2), |
| 114 | OPT_EOF = (1 << 3), /* pseudo: "we saw EOF in stdin" */ |
| 115 | }; |
| 116 | #define OPT_BATCH_MODE (option_mask32 & OPT_b) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 117 | |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 118 | |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 119 | #if ENABLE_FEATURE_USE_TERMIOS |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 120 | static int pid_sort(top_status_t *P, top_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 121 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 122 | /* Buggy wrt pids with high bit set */ |
| 123 | /* (linux pids are in [1..2^15-1]) */ |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 124 | return (Q->pid - P->pid); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 125 | } |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 126 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 127 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 128 | static int mem_sort(top_status_t *P, top_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 129 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 130 | /* We want to avoid unsigned->signed and truncation errors */ |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 131 | if (Q->vsz < P->vsz) return -1; |
| 132 | return Q->vsz != P->vsz; /* 0 if ==, 1 if > */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 135 | |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 136 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 137 | |
| 138 | static int pcpu_sort(top_status_t *P, top_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 139 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 140 | /* Buggy wrt ticks with high bit set */ |
| 141 | /* Affects only processes for which ticks overflow */ |
| 142 | return (int)Q->pcpu - (int)P->pcpu; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 145 | static int time_sort(top_status_t *P, top_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 146 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 147 | /* We want to avoid unsigned->signed and truncation errors */ |
| 148 | if (Q->ticks < P->ticks) return -1; |
| 149 | return Q->ticks != P->ticks; /* 0 if ==, 1 if > */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 152 | static int mult_lvl_cmp(void* a, void* b) |
| 153 | { |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 154 | int i, cmp_val; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 155 | |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 156 | for (i = 0; i < SORT_DEPTH; i++) { |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 157 | cmp_val = (*sort_function[i])(a, b); |
| 158 | if (cmp_val != 0) |
| 159 | return cmp_val; |
| 160 | } |
| 161 | return 0; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 164 | |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 165 | static void get_jiffy_counts(void) |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 166 | { |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 167 | FILE* fp = xfopen_for_read("stat"); |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 168 | prev_jif = jif; |
| 169 | if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld", |
| 170 | &jif.usr,&jif.nic,&jif.sys,&jif.idle, |
| 171 | &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) { |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 172 | bb_error_msg_and_die("can't read /proc/stat"); |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 173 | } |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 174 | fclose(fp); |
| 175 | jif.total = jif.usr + jif.nic + jif.sys + jif.idle |
| 176 | + jif.iowait + jif.irq + jif.softirq + jif.steal; |
| 177 | /* procps 2.x does not count iowait as busy time */ |
| 178 | jif.busy = jif.total - jif.idle - jif.iowait; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 181 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 182 | static void do_stats(void) |
| 183 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 184 | top_status_t *cur; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 185 | pid_t pid; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 186 | int i, last_i, n; |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 187 | struct save_hist *new_hist; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 188 | |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 189 | get_jiffy_counts(); |
| 190 | total_pcpu = 0; |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 191 | /* total_vsz = 0; */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 192 | new_hist = xmalloc(sizeof(struct save_hist)*ntop); |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 193 | /* |
| 194 | * Make a pass through the data to get stats. |
| 195 | */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 196 | /* hist_iterations = 0; */ |
| 197 | i = 0; |
| 198 | for (n = 0; n < ntop; n++) { |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 199 | cur = top + n; |
| 200 | |
| 201 | /* |
| 202 | * Calculate time in cur process. Time is sum of user time |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 203 | * and system time |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 204 | */ |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 205 | pid = cur->pid; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 206 | new_hist[n].ticks = cur->ticks; |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 207 | new_hist[n].pid = pid; |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 208 | |
| 209 | /* find matching entry from previous pass */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 210 | cur->pcpu = 0; |
| 211 | /* do not start at index 0, continue at last used one |
| 212 | * (brought hist_iterations from ~14000 down to 172) */ |
| 213 | last_i = i; |
| 214 | if (prev_hist_count) do { |
| 215 | if (prev_hist[i].pid == pid) { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 216 | cur->pcpu = cur->ticks - prev_hist[i].ticks; |
| 217 | total_pcpu += cur->pcpu; |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 218 | break; |
| 219 | } |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 220 | i = (i+1) % prev_hist_count; |
| 221 | /* hist_iterations++; */ |
| 222 | } while (i != last_i); |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 223 | /* total_vsz += cur->vsz; */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 227 | * Save cur frame's information. |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 228 | */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 229 | free(prev_hist); |
| 230 | prev_hist = new_hist; |
| 231 | prev_hist_count = ntop; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 232 | } |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 233 | #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 234 | |
Denis Vlasenko | 6ee023c | 2007-08-23 10:52:52 +0000 | [diff] [blame] | 235 | #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && ENABLE_FEATURE_TOP_DECIMALS |
| 236 | /* formats 7 char string (8 with terminating NUL) */ |
| 237 | static char *fmt_100percent_8(char pbuf[8], unsigned value, unsigned total) |
| 238 | { |
| 239 | unsigned t; |
| 240 | if (value >= total) { /* 100% ? */ |
| 241 | strcpy(pbuf, " 100% "); |
| 242 | return pbuf; |
| 243 | } |
| 244 | /* else generate " [N/space]N.N% " string */ |
| 245 | value = 1000 * value / total; |
| 246 | t = value / 100; |
| 247 | value = value % 100; |
| 248 | pbuf[0] = ' '; |
| 249 | pbuf[1] = t ? t + '0' : ' '; |
| 250 | pbuf[2] = '0' + (value / 10); |
| 251 | pbuf[3] = '.'; |
| 252 | pbuf[4] = '0' + (value % 10); |
| 253 | pbuf[5] = '%'; |
| 254 | pbuf[6] = ' '; |
| 255 | pbuf[7] = '\0'; |
| 256 | return pbuf; |
| 257 | } |
| 258 | #endif |
| 259 | |
Denis Vlasenko | 0524180 | 2007-08-29 18:34:26 +0000 | [diff] [blame] | 260 | static unsigned long display_header(int scr_width) |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 261 | { |
| 262 | FILE *fp; |
| 263 | char buf[80]; |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 264 | char scrbuf[80]; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 265 | unsigned long total, used, mfree, shared, buffers, cached; |
Denis Vlasenko | 6ee023c | 2007-08-23 10:52:52 +0000 | [diff] [blame] | 266 | #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS |
Denis Vlasenko | 110967a | 2007-07-15 19:27:48 +0000 | [diff] [blame] | 267 | unsigned total_diff; |
Denis Vlasenko | 856be77 | 2007-08-17 08:29:48 +0000 | [diff] [blame] | 268 | #endif |
Denis Vlasenko | 110967a | 2007-07-15 19:27:48 +0000 | [diff] [blame] | 269 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 270 | /* read memory info */ |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 271 | fp = xfopen_for_read("meminfo"); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 272 | |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 273 | /* |
| 274 | * Old kernels (such as 2.4.x) had a nice summary of memory info that |
| 275 | * we could parse, however this is gone entirely in 2.6. Try parsing |
| 276 | * the old way first, and if that fails, parse each field manually. |
| 277 | * |
| 278 | * First, we read in the first line. Old kernels will have bogus |
| 279 | * strings we don't care about, whereas new kernels will start right |
| 280 | * out with MemTotal: |
"Vladimir N. Oleynik" | 70678bc | 2005-11-29 12:32:33 +0000 | [diff] [blame] | 281 | * -- PFM. |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 282 | */ |
| 283 | if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) { |
"Vladimir N. Oleynik" | 70678bc | 2005-11-29 12:32:33 +0000 | [diff] [blame] | 284 | fgets(buf, sizeof(buf), fp); /* skip first line */ |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 285 | |
| 286 | fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu", |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 287 | &total, &used, &mfree, &shared, &buffers, &cached); |
| 288 | /* convert to kilobytes */ |
| 289 | used /= 1024; |
| 290 | mfree /= 1024; |
| 291 | shared /= 1024; |
| 292 | buffers /= 1024; |
| 293 | cached /= 1024; |
| 294 | total /= 1024; |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 295 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 296 | /* |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 297 | * Revert to manual parsing, which incidentally already has the |
| 298 | * sizes in kilobytes. This should be safe for both 2.4 and |
| 299 | * 2.6. |
| 300 | */ |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 301 | |
| 302 | fscanf(fp, "MemFree: %lu %s\n", &mfree, buf); |
| 303 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 304 | /* |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 305 | * MemShared: is no longer present in 2.6. Report this as 0, |
| 306 | * to maintain consistent behavior with normal procps. |
| 307 | */ |
| 308 | if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2) |
| 309 | shared = 0; |
| 310 | |
| 311 | fscanf(fp, "Buffers: %lu %s\n", &buffers, buf); |
| 312 | fscanf(fp, "Cached: %lu %s\n", &cached, buf); |
| 313 | |
| 314 | used = total - mfree; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 315 | } |
| 316 | fclose(fp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 317 | |
Denis Vlasenko | 110967a | 2007-07-15 19:27:48 +0000 | [diff] [blame] | 318 | /* output memory info */ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 319 | if (scr_width > (int)sizeof(scrbuf)) |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 320 | scr_width = sizeof(scrbuf); |
| 321 | snprintf(scrbuf, scr_width, |
Denis Vlasenko | c1166c3 | 2007-07-15 19:23:38 +0000 | [diff] [blame] | 322 | "Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached", |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 323 | used, mfree, shared, buffers, cached); |
Denis Vlasenko | 110967a | 2007-07-15 19:27:48 +0000 | [diff] [blame] | 324 | /* clear screen & go to top */ |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 325 | printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf); |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 326 | |
Denis Vlasenko | 856be77 | 2007-08-17 08:29:48 +0000 | [diff] [blame] | 327 | #if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS |
| 328 | /* |
| 329 | * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100% |
| 330 | */ |
| 331 | /* using (unsigned) casts to make operations cheaper */ |
| 332 | total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1); |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 333 | #if ENABLE_FEATURE_TOP_DECIMALS |
Denis Vlasenko | 110967a | 2007-07-15 19:27:48 +0000 | [diff] [blame] | 334 | /* Generated code is approx +0.3k */ |
Denis Vlasenko | 24c5fba | 2007-07-15 19:25:01 +0000 | [diff] [blame] | 335 | #define CALC_STAT(xxx) char xxx[8] |
Denis Vlasenko | 6ee023c | 2007-08-23 10:52:52 +0000 | [diff] [blame] | 336 | #define SHOW_STAT(xxx) fmt_100percent_8(xxx, (unsigned)(jif.xxx - prev_jif.xxx), total_diff) |
Denis Vlasenko | 24c5fba | 2007-07-15 19:25:01 +0000 | [diff] [blame] | 337 | #define FMT "%s" |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 338 | #else |
Denis Vlasenko | b1e5add | 2007-06-10 18:04:54 +0000 | [diff] [blame] | 339 | #define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff |
| 340 | #define SHOW_STAT(xxx) xxx |
Denis Vlasenko | 24c5fba | 2007-07-15 19:25:01 +0000 | [diff] [blame] | 341 | #define FMT "%4u%% " |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 342 | #endif |
Denis Vlasenko | 856be77 | 2007-08-17 08:29:48 +0000 | [diff] [blame] | 343 | { /* need block: CALC_STAT are declarations */ |
| 344 | CALC_STAT(usr); |
| 345 | CALC_STAT(sys); |
| 346 | CALC_STAT(nic); |
| 347 | CALC_STAT(idle); |
| 348 | CALC_STAT(iowait); |
| 349 | CALC_STAT(irq); |
| 350 | CALC_STAT(softirq); |
| 351 | //CALC_STAT(steal); |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 352 | |
Denis Vlasenko | 856be77 | 2007-08-17 08:29:48 +0000 | [diff] [blame] | 353 | snprintf(scrbuf, scr_width, |
| 354 | /* Barely fits in 79 chars when in "decimals" mode. */ |
| 355 | "CPU:"FMT"usr"FMT"sys"FMT"nice"FMT"idle"FMT"io"FMT"irq"FMT"softirq", |
| 356 | SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle), |
| 357 | SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq) |
| 358 | //, SHOW_STAT(steal) - what is this 'steal' thing? |
| 359 | // I doubt anyone wants to know it |
| 360 | ); |
| 361 | } |
| 362 | puts(scrbuf); |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 363 | #undef SHOW_STAT |
| 364 | #undef CALC_STAT |
Denis Vlasenko | b1e5add | 2007-06-10 18:04:54 +0000 | [diff] [blame] | 365 | #undef FMT |
Denis Vlasenko | 856be77 | 2007-08-17 08:29:48 +0000 | [diff] [blame] | 366 | #endif |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 367 | |
Denis Vlasenko | 110967a | 2007-07-15 19:27:48 +0000 | [diff] [blame] | 368 | /* read load average as a string */ |
| 369 | buf[0] = '\0'; |
| 370 | open_read_close("loadavg", buf, sizeof("N.NN N.NN N.NN")-1); |
| 371 | buf[sizeof("N.NN N.NN N.NN")-1] = '\0'; |
Denis Vlasenko | 25d8062 | 2006-10-27 09:34:22 +0000 | [diff] [blame] | 372 | snprintf(scrbuf, scr_width, "Load average: %s", buf); |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 373 | puts(scrbuf); |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 374 | |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 375 | return total; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Denis Vlasenko | 4c1d88d | 2007-09-08 17:34:05 +0000 | [diff] [blame] | 378 | static NOINLINE void display_process_list(int count, int scr_width) |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 379 | { |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 380 | enum { |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 381 | BITS_PER_INT = sizeof(int)*8 |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 382 | }; |
| 383 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 384 | top_status_t *s = top; |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 385 | char vsz_str_buf[8]; |
Denis Vlasenko | 0524180 | 2007-08-29 18:34:26 +0000 | [diff] [blame] | 386 | unsigned long total_memory = display_header(scr_width); /* or use total_vsz? */ |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 387 | /* xxx_shift and xxx_scale variables allow us to replace |
| 388 | * expensive divides with multiply and shift */ |
| 389 | unsigned pmem_shift, pmem_scale, pmem_half; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 390 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 391 | unsigned pcpu_shift, pcpu_scale, pcpu_half; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 392 | unsigned busy_jifs; |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 393 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 394 | /* what info of the processes is shown */ |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 395 | printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 396 | " PID PPID USER STAT VSZ %MEM %CPU COMMAND"); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 397 | #else |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 398 | |
| 399 | /* !CPU_USAGE_PERCENTAGE */ |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 400 | printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 401 | " PID PPID USER STAT VSZ %MEM COMMAND"); |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 402 | #endif |
| 403 | |
Denis Vlasenko | 24c5fba | 2007-07-15 19:25:01 +0000 | [diff] [blame] | 404 | #if ENABLE_FEATURE_TOP_DECIMALS |
| 405 | #define UPSCALE 1000 |
| 406 | #define CALC_STAT(name, val) div_t name = div((val), 10) |
| 407 | #define SHOW_STAT(name) name.quot, '0'+name.rem |
| 408 | #define FMT "%3u.%c" |
| 409 | #else |
| 410 | #define UPSCALE 100 |
| 411 | #define CALC_STAT(name, val) unsigned name = (val) |
| 412 | #define SHOW_STAT(name) name |
| 413 | #define FMT "%4u%%" |
| 414 | #endif |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 415 | /* |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 416 | * MEM% = s->vsz/MemTotal |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 417 | */ |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 418 | pmem_shift = BITS_PER_INT-11; |
| 419 | pmem_scale = UPSCALE*(1U<<(BITS_PER_INT-11)) / total_memory; |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 420 | /* s->vsz is in kb. we want (s->vsz * pmem_scale) to never overflow */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 421 | while (pmem_scale >= 512) { |
| 422 | pmem_scale /= 4; |
| 423 | pmem_shift -= 2; |
| 424 | } |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 425 | pmem_half = (1U << pmem_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 426 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
| 427 | busy_jifs = jif.busy - prev_jif.busy; |
| 428 | /* This happens if there were lots of short-lived processes |
| 429 | * between two top updates (e.g. compilation) */ |
| 430 | if (total_pcpu < busy_jifs) total_pcpu = busy_jifs; |
| 431 | |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 432 | /* |
| 433 | * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks |
| 434 | * (pcpu is delta of sys+user time between samples) |
| 435 | */ |
| 436 | /* (jif.xxx - prev_jif.xxx) and s->pcpu are |
| 437 | * in 0..~64000 range (HZ*update_interval). |
| 438 | * we assume that unsigned is at least 32-bit. |
| 439 | */ |
| 440 | pcpu_shift = 6; |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 441 | pcpu_scale = (UPSCALE*64*(uint16_t)busy_jifs ? : 1); |
| 442 | while (pcpu_scale < (1U<<(BITS_PER_INT-2))) { |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 443 | pcpu_scale *= 4; |
| 444 | pcpu_shift += 2; |
| 445 | } |
| 446 | pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1); |
| 447 | /* we want (s->pcpu * pcpu_scale) to never overflow */ |
| 448 | while (pcpu_scale >= 1024) { |
| 449 | pcpu_scale /= 4; |
| 450 | pcpu_shift -= 2; |
| 451 | } |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 452 | pcpu_half = (1U << pcpu_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2); |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 453 | /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 454 | #endif |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 455 | |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 456 | scr_width += 2; /* account for leading '\n' and trailing NUL */ |
Bernhard Reutner-Fischer | a985d30 | 2008-02-11 11:44:38 +0000 | [diff] [blame] | 457 | /* Ok, all preliminary data is ready, go through the list */ |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 458 | while (count-- > 0) { |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 459 | unsigned col; |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 460 | CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift); |
| 461 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
| 462 | CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift); |
| 463 | #endif |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 464 | |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 465 | if (s->vsz >= 100000) |
Denis Vlasenko | b308d81 | 2007-08-28 19:35:34 +0000 | [diff] [blame] | 466 | sprintf(vsz_str_buf, "%6ldm", s->vsz/1024); |
Manuel Novoa III | d499330 | 2002-09-18 19:27:10 +0000 | [diff] [blame] | 467 | else |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 468 | sprintf(vsz_str_buf, "%7ld", s->vsz); |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 469 | // PID PPID USER STAT VSZ %MEM [%CPU] COMMAND |
Denis Vlasenko | 4c1d88d | 2007-09-08 17:34:05 +0000 | [diff] [blame] | 470 | col = snprintf(line_buf, scr_width, |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 471 | "\n" "%5u%6u %-8.8s %s%s" FMT |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 472 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
| 473 | FMT |
| 474 | #endif |
| 475 | " ", |
| 476 | s->pid, s->ppid, get_cached_username(s->uid), |
| 477 | s->state, vsz_str_buf, |
| 478 | SHOW_STAT(pmem) |
| 479 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
| 480 | , SHOW_STAT(pcpu) |
| 481 | #endif |
| 482 | ); |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 483 | if ((int)(col + 1) < scr_width) |
Denis Vlasenko | 4d7605a | 2007-09-08 17:42:00 +0000 | [diff] [blame] | 484 | read_cmdline(line_buf + col, scr_width - col - 1, s->pid, s->comm); |
Denis Vlasenko | 4c1d88d | 2007-09-08 17:34:05 +0000 | [diff] [blame] | 485 | fputs(line_buf, stdout); |
Bernhard Reutner-Fischer | e2922e4 | 2006-05-19 12:48:56 +0000 | [diff] [blame] | 486 | /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu, |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 487 | jif.busy - prev_jif.busy, jif.total - prev_jif.total); */ |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 488 | s++; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 489 | } |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 490 | /* printf(" %d", hist_iterations); */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 491 | bb_putchar(OPT_BATCH_MODE ? '\n' : '\r'); |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 492 | fflush(stdout); |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 493 | } |
Denis Vlasenko | 24c5fba | 2007-07-15 19:25:01 +0000 | [diff] [blame] | 494 | #undef UPSCALE |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 495 | #undef SHOW_STAT |
| 496 | #undef CALC_STAT |
| 497 | #undef FMT |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 498 | |
| 499 | static void clearmems(void) |
| 500 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 501 | clear_username_cache(); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 502 | free(top); |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 503 | top = NULL; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 504 | ntop = 0; |
| 505 | } |
| 506 | |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 507 | #if ENABLE_FEATURE_USE_TERMIOS |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 508 | #include <termios.h> |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 509 | #include <signal.h> |
| 510 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 511 | static void reset_term(void) |
| 512 | { |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 513 | tcsetattr(0, TCSANOW, &initial_settings); |
Denis Vlasenko | 8bdba4d | 2007-08-29 18:18:08 +0000 | [diff] [blame] | 514 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 515 | clearmems(); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 516 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Denis Vlasenko | 8bdba4d | 2007-08-29 18:18:08 +0000 | [diff] [blame] | 517 | free(prev_hist); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 518 | #endif |
Denis Vlasenko | 8bdba4d | 2007-08-29 18:18:08 +0000 | [diff] [blame] | 519 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 520 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 521 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 522 | static void sig_catcher(int sig UNUSED_PARAM) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 523 | { |
| 524 | reset_term(); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 525 | exit(EXIT_FAILURE); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 526 | } |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 527 | #endif /* FEATURE_USE_TERMIOS */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 528 | |
Denis Vlasenko | e7c1ad1 | 2007-09-08 17:21:01 +0000 | [diff] [blame] | 529 | /* |
| 530 | * TOPMEM support |
| 531 | */ |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 532 | |
| 533 | typedef unsigned long mem_t; |
| 534 | |
| 535 | typedef struct topmem_status_t { |
| 536 | unsigned pid; |
| 537 | char comm[COMM_LEN]; |
| 538 | /* vsz doesn't count /dev/xxx mappings except /dev/zero */ |
| 539 | mem_t vsz ; |
| 540 | mem_t vszrw ; |
| 541 | mem_t rss ; |
| 542 | mem_t rss_sh ; |
| 543 | mem_t dirty ; |
| 544 | mem_t dirty_sh; |
| 545 | mem_t stack ; |
| 546 | } topmem_status_t; |
| 547 | |
| 548 | enum { NUM_SORT_FIELD = 7 }; |
| 549 | |
| 550 | #define topmem ((topmem_status_t*)top) |
| 551 | |
| 552 | #if ENABLE_FEATURE_TOPMEM |
| 553 | static int topmem_sort(char *a, char *b) |
| 554 | { |
| 555 | int n; |
| 556 | mem_t l, r; |
| 557 | |
| 558 | n = offsetof(topmem_status_t, vsz) + (sort_field * sizeof(mem_t)); |
| 559 | l = *(mem_t*)(a + n); |
| 560 | r = *(mem_t*)(b + n); |
| 561 | // if (l == r) { |
| 562 | // l = a->mapped_rw; |
| 563 | // r = b->mapped_rw; |
| 564 | // } |
| 565 | /* We want to avoid unsigned->signed and truncation errors */ |
| 566 | /* l>r: -1, l=r: 0, l<r: 1 */ |
| 567 | n = (l > r) ? -1 : (l != r); |
| 568 | return inverted ? -n : n; |
| 569 | } |
| 570 | |
| 571 | /* Cut "NNNN " out of " NNNN kb" */ |
| 572 | static char *grab_number(char *str, const char *match, unsigned sz) |
| 573 | { |
| 574 | if (strncmp(str, match, sz) == 0) { |
| 575 | str = skip_whitespace(str + sz); |
| 576 | (skip_non_whitespace(str))[1] = '\0'; |
| 577 | return xstrdup(str); |
| 578 | } |
| 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | /* display header info (meminfo / loadavg) */ |
| 583 | static void display_topmem_header(int scr_width) |
| 584 | { |
| 585 | char linebuf[128]; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 586 | unsigned i; |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 587 | FILE *fp; |
| 588 | union { |
| 589 | struct { |
| 590 | /* 1 */ char *total; |
| 591 | /* 2 */ char *mfree; |
| 592 | /* 3 */ char *buf; |
| 593 | /* 4 */ char *cache; |
| 594 | /* 5 */ char *swaptotal; |
| 595 | /* 6 */ char *swapfree; |
| 596 | /* 7 */ char *dirty; |
| 597 | /* 8 */ char *mwrite; |
| 598 | /* 9 */ char *anon; |
| 599 | /* 10 */ char *map; |
| 600 | /* 11 */ char *slab; |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 601 | } u; |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 602 | char *str[11]; |
| 603 | } Z; |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 604 | #define total Z.u.total |
| 605 | #define mfree Z.u.mfree |
| 606 | #define buf Z.u.buf |
| 607 | #define cache Z.u.cache |
| 608 | #define swaptotal Z.u.swaptotal |
| 609 | #define swapfree Z.u.swapfree |
| 610 | #define dirty Z.u.dirty |
| 611 | #define mwrite Z.u.mwrite |
| 612 | #define anon Z.u.anon |
| 613 | #define map Z.u.map |
| 614 | #define slab Z.u.slab |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 615 | #define str Z.str |
| 616 | |
| 617 | memset(&Z, 0, sizeof(Z)); |
| 618 | |
| 619 | /* read memory info */ |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 620 | fp = xfopen_for_read("meminfo"); |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 621 | while (fgets(linebuf, sizeof(linebuf), fp)) { |
| 622 | char *p; |
| 623 | |
| 624 | #define SCAN(match, name) \ |
| 625 | p = grab_number(linebuf, match, sizeof(match)-1); \ |
| 626 | if (p) { name = p; continue; } |
| 627 | |
| 628 | SCAN("MemTotal:", total); |
| 629 | SCAN("MemFree:", mfree); |
| 630 | SCAN("Buffers:", buf); |
| 631 | SCAN("Cached:", cache); |
| 632 | SCAN("SwapTotal:", swaptotal); |
| 633 | SCAN("SwapFree:", swapfree); |
| 634 | SCAN("Dirty:", dirty); |
| 635 | SCAN("Writeback:", mwrite); |
| 636 | SCAN("AnonPages:", anon); |
| 637 | SCAN("Mapped:", map); |
| 638 | SCAN("Slab:", slab); |
| 639 | #undef SCAN |
| 640 | } |
| 641 | fclose(fp); |
| 642 | |
Denis Vlasenko | fcfb5c0 | 2007-12-24 12:16:24 +0000 | [diff] [blame] | 643 | #define S(s) (s ? s : "0 ") |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 644 | snprintf(linebuf, sizeof(linebuf), |
| 645 | "Mem %stotal %sanon %smap %sfree", |
| 646 | S(total), S(anon), S(map), S(mfree)); |
| 647 | printf(OPT_BATCH_MODE ? "%.*s\n" : "\e[H\e[J%.*s\n", scr_width, linebuf); |
| 648 | |
| 649 | snprintf(linebuf, sizeof(linebuf), |
| 650 | " %sslab %sbuf %scache %sdirty %swrite", |
| 651 | S(slab), S(buf), S(cache), S(dirty), S(mwrite)); |
| 652 | printf("%.*s\n", scr_width, linebuf); |
| 653 | |
| 654 | snprintf(linebuf, sizeof(linebuf), |
| 655 | "Swap %stotal %sfree", // TODO: % used? |
| 656 | S(swaptotal), S(swapfree)); |
| 657 | printf("%.*s\n", scr_width, linebuf); |
| 658 | #undef S |
| 659 | |
| 660 | for (i = 0; i < ARRAY_SIZE(str); i++) |
| 661 | free(str[i]); |
| 662 | #undef total |
| 663 | #undef free |
| 664 | #undef buf |
| 665 | #undef cache |
| 666 | #undef swaptotal |
| 667 | #undef swapfree |
| 668 | #undef dirty |
| 669 | #undef write |
| 670 | #undef anon |
| 671 | #undef map |
| 672 | #undef slab |
| 673 | #undef str |
| 674 | } |
| 675 | |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 676 | static void ulltoa6_and_space(unsigned long long ul, char buf[6]) |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 677 | { |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 678 | /* see http://en.wikipedia.org/wiki/Tera */ |
| 679 | smart_ulltoa5(ul, buf, " mgtpezy"); |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 680 | buf[5] = ' '; |
| 681 | } |
| 682 | |
Denis Vlasenko | 4c1d88d | 2007-09-08 17:34:05 +0000 | [diff] [blame] | 683 | static NOINLINE void display_topmem_process_list(int count, int scr_width) |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 684 | { |
| 685 | #define HDR_STR " PID VSZ VSZRW RSS (SHR) DIRTY (SHR) STACK" |
| 686 | #define MIN_WIDTH sizeof(HDR_STR) |
| 687 | const topmem_status_t *s = topmem; |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 688 | |
| 689 | display_topmem_header(scr_width); |
Denis Vlasenko | 4c1d88d | 2007-09-08 17:34:05 +0000 | [diff] [blame] | 690 | strcpy(line_buf, HDR_STR " COMMAND"); |
| 691 | line_buf[5 + sort_field * 6] = '*'; |
| 692 | printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, line_buf); |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 693 | |
| 694 | while (--count >= 0) { |
| 695 | // PID VSZ VSZRW RSS (SHR) DIRTY (SHR) COMMAND |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 696 | ulltoa6_and_space(s->pid , &line_buf[0*6]); |
| 697 | ulltoa6_and_space(s->vsz , &line_buf[1*6]); |
| 698 | ulltoa6_and_space(s->vszrw , &line_buf[2*6]); |
| 699 | ulltoa6_and_space(s->rss , &line_buf[3*6]); |
| 700 | ulltoa6_and_space(s->rss_sh , &line_buf[4*6]); |
| 701 | ulltoa6_and_space(s->dirty , &line_buf[5*6]); |
| 702 | ulltoa6_and_space(s->dirty_sh, &line_buf[6*6]); |
| 703 | ulltoa6_and_space(s->stack , &line_buf[7*6]); |
Denis Vlasenko | 4c1d88d | 2007-09-08 17:34:05 +0000 | [diff] [blame] | 704 | line_buf[8*6] = '\0'; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 705 | if (scr_width > (int)MIN_WIDTH) { |
Denis Vlasenko | 4c1d88d | 2007-09-08 17:34:05 +0000 | [diff] [blame] | 706 | read_cmdline(&line_buf[8*6], scr_width - MIN_WIDTH, s->pid, s->comm); |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 707 | } |
Denis Vlasenko | 4c1d88d | 2007-09-08 17:34:05 +0000 | [diff] [blame] | 708 | printf("\n""%.*s", scr_width, line_buf); |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 709 | s++; |
| 710 | } |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 711 | bb_putchar(OPT_BATCH_MODE ? '\n' : '\r'); |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 712 | fflush(stdout); |
| 713 | #undef HDR_STR |
| 714 | #undef MIN_WIDTH |
| 715 | } |
| 716 | #else |
| 717 | void display_topmem_process_list(int count, int scr_width); |
| 718 | int topmem_sort(char *a, char *b); |
| 719 | #endif /* TOPMEM */ |
| 720 | |
Denis Vlasenko | e7c1ad1 | 2007-09-08 17:21:01 +0000 | [diff] [blame] | 721 | /* |
| 722 | * end TOPMEM support |
| 723 | */ |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 724 | |
| 725 | enum { |
| 726 | TOP_MASK = 0 |
| 727 | | PSSCAN_PID |
| 728 | | PSSCAN_PPID |
| 729 | | PSSCAN_VSZ |
| 730 | | PSSCAN_STIME |
| 731 | | PSSCAN_UTIME |
| 732 | | PSSCAN_STATE |
| 733 | | PSSCAN_COMM |
| 734 | | PSSCAN_UIDGID, |
| 735 | TOPMEM_MASK = 0 |
| 736 | | PSSCAN_PID |
| 737 | | PSSCAN_SMAPS |
| 738 | | PSSCAN_COMM, |
| 739 | }; |
| 740 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 741 | int top_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 742 | int top_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 743 | { |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 744 | int count; |
Denis Vlasenko | e7c1ad1 | 2007-09-08 17:21:01 +0000 | [diff] [blame] | 745 | int iterations; |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 746 | unsigned lines, col; |
| 747 | unsigned interval; |
Denis Vlasenko | 04e11c9 | 2008-02-10 19:44:20 +0000 | [diff] [blame] | 748 | char *sinterval; |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 749 | SKIP_FEATURE_TOPMEM(const) unsigned scan_mask = TOP_MASK; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 750 | #if ENABLE_FEATURE_USE_TERMIOS |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 751 | struct termios new_settings; |
Denis Vlasenko | b308d81 | 2007-08-28 19:35:34 +0000 | [diff] [blame] | 752 | struct pollfd pfd[1]; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 753 | unsigned char c; |
Denis Vlasenko | b308d81 | 2007-08-28 19:35:34 +0000 | [diff] [blame] | 754 | |
| 755 | pfd[0].fd = 0; |
| 756 | pfd[0].events = POLLIN; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 757 | #endif /* FEATURE_USE_TERMIOS */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 758 | |
Denis Vlasenko | e7c1ad1 | 2007-09-08 17:21:01 +0000 | [diff] [blame] | 759 | INIT_G(); |
| 760 | |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 761 | interval = 5; /* default update interval is 5 seconds */ |
Denis Vlasenko | e7c1ad1 | 2007-09-08 17:21:01 +0000 | [diff] [blame] | 762 | iterations = 0; /* infinite */ |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 763 | |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 764 | /* all args are options; -n NUM */ |
Denis Vlasenko | 04e11c9 | 2008-02-10 19:44:20 +0000 | [diff] [blame] | 765 | opt_complementary = "-:n+"; |
Denis Vlasenko | 1c45a50 | 2008-08-20 00:12:22 +0000 | [diff] [blame] | 766 | if (getopt32(argv, "d:n:b", &sinterval, &iterations) & OPT_d) { |
Denis Vlasenko | b308d81 | 2007-08-28 19:35:34 +0000 | [diff] [blame] | 767 | /* Need to limit it to not overflow poll timeout */ |
| 768 | interval = xatou16(sinterval); // -d |
| 769 | } |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 770 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 771 | /* change to /proc */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 772 | xchdir("/proc"); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 773 | #if ENABLE_FEATURE_USE_TERMIOS |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 774 | tcgetattr(0, (void *) &initial_settings); |
Denis Vlasenko | 8bdba4d | 2007-08-29 18:18:08 +0000 | [diff] [blame] | 775 | memcpy(&new_settings, &initial_settings, sizeof(new_settings)); |
Denis Vlasenko | 42dfcd2 | 2006-09-09 12:55:02 +0000 | [diff] [blame] | 776 | /* unbuffered input, turn off echo */ |
| 777 | new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 778 | |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 779 | bb_signals(BB_FATAL_SIGS, sig_catcher); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 780 | tcsetattr(0, TCSANOW, (void *) &new_settings); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 781 | #endif /* FEATURE_USE_TERMIOS */ |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 782 | |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 783 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 784 | sort_function[0] = pcpu_sort; |
| 785 | sort_function[1] = mem_sort; |
| 786 | sort_function[2] = time_sort; |
| 787 | #else |
Denis Vlasenko | 8bdba4d | 2007-08-29 18:18:08 +0000 | [diff] [blame] | 788 | sort_function[0] = mem_sort; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 789 | #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 790 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 791 | while (1) { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 792 | procps_status_t *p = NULL; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 793 | |
Denis Vlasenko | e7c1ad1 | 2007-09-08 17:21:01 +0000 | [diff] [blame] | 794 | lines = 24; /* default */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 795 | col = 79; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 796 | #if ENABLE_FEATURE_USE_TERMIOS |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 797 | /* We output to stdout, we need size of stdout (not stdin)! */ |
| 798 | get_terminal_width_height(STDOUT_FILENO, &col, &lines); |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 799 | if (lines < 5 || col < 10) { |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 800 | sleep(interval); |
| 801 | continue; |
| 802 | } |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 803 | #endif /* FEATURE_USE_TERMIOS */ |
Denis Vlasenko | 4c1d88d | 2007-09-08 17:34:05 +0000 | [diff] [blame] | 804 | if (col > LINE_BUF_SIZE-2) /* +2 bytes for '\n', NUL, */ |
| 805 | col = LINE_BUF_SIZE-2; |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 806 | if (!ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && scan_mask == TOP_MASK) |
| 807 | lines -= 3; |
| 808 | else |
| 809 | lines -= 4; |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 810 | |
| 811 | /* read process IDs & status for all the processes */ |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 812 | while ((p = procps_scan(p, scan_mask)) != NULL) { |
| 813 | int n; |
| 814 | if (scan_mask == TOP_MASK) { |
| 815 | n = ntop; |
Denis Vlasenko | 2784228 | 2008-08-04 13:20:36 +0000 | [diff] [blame] | 816 | top = xrealloc_vector(top, 6, ntop++); |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 817 | top[n].pid = p->pid; |
| 818 | top[n].ppid = p->ppid; |
| 819 | top[n].vsz = p->vsz; |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 820 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 821 | top[n].ticks = p->stime + p->utime; |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 822 | #endif |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 823 | top[n].uid = p->uid; |
| 824 | strcpy(top[n].state, p->state); |
| 825 | strcpy(top[n].comm, p->comm); |
| 826 | } else { /* TOPMEM */ |
| 827 | #if ENABLE_FEATURE_TOPMEM |
| 828 | if (!(p->mapped_ro | p->mapped_rw)) |
| 829 | continue; /* kernel threads are ignored */ |
| 830 | n = ntop; |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 831 | /* No bug here - top and topmem are the same */ |
Denis Vlasenko | 2784228 | 2008-08-04 13:20:36 +0000 | [diff] [blame] | 832 | top = xrealloc_vector(topmem, 6, ntop++); |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 833 | strcpy(topmem[n].comm, p->comm); |
| 834 | topmem[n].pid = p->pid; |
| 835 | topmem[n].vsz = p->mapped_rw + p->mapped_ro; |
| 836 | topmem[n].vszrw = p->mapped_rw; |
| 837 | topmem[n].rss_sh = p->shared_clean + p->shared_dirty; |
| 838 | topmem[n].rss = p->private_clean + p->private_dirty + topmem[n].rss_sh; |
| 839 | topmem[n].dirty = p->private_dirty + p->shared_dirty; |
| 840 | topmem[n].dirty_sh = p->shared_dirty; |
| 841 | topmem[n].stack = p->stack; |
| 842 | #endif |
| 843 | } |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 844 | } /* end of "while we read /proc" */ |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 845 | if (ntop == 0) { |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 846 | bb_error_msg("no process info in /proc"); |
| 847 | break; |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 848 | } |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 849 | |
| 850 | if (scan_mask == TOP_MASK) { |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 851 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 852 | if (!prev_hist_count) { |
| 853 | do_stats(); |
| 854 | usleep(100000); |
| 855 | clearmems(); |
| 856 | continue; |
| 857 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 858 | do_stats(); |
Denis Vlasenko | f7d07b1 | 2007-06-30 08:03:26 +0000 | [diff] [blame] | 859 | /* TODO: we don't need to sort all 10000 processes, we need to find top 24! */ |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 860 | qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 861 | #else |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 862 | qsort(top, ntop, sizeof(top_status_t), (void*)(sort_function[0])); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 863 | #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ |
Denis Vlasenko | e96dcb4 | 2008-04-17 18:04:38 +0000 | [diff] [blame] | 864 | } |
| 865 | #if ENABLE_FEATURE_TOPMEM |
| 866 | else { /* TOPMEM */ |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 867 | qsort(topmem, ntop, sizeof(topmem_status_t), (void*)topmem_sort); |
| 868 | } |
Denis Vlasenko | e96dcb4 | 2008-04-17 18:04:38 +0000 | [diff] [blame] | 869 | #endif |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 870 | count = lines; |
Denis Vlasenko | 25d8062 | 2006-10-27 09:34:22 +0000 | [diff] [blame] | 871 | if (OPT_BATCH_MODE || count > ntop) { |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 872 | count = ntop; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 873 | } |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 874 | if (scan_mask == TOP_MASK) |
| 875 | display_process_list(count, col); |
Denis Vlasenko | e96dcb4 | 2008-04-17 18:04:38 +0000 | [diff] [blame] | 876 | #if ENABLE_FEATURE_TOPMEM |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 877 | else |
| 878 | display_topmem_process_list(count, col); |
Denis Vlasenko | e96dcb4 | 2008-04-17 18:04:38 +0000 | [diff] [blame] | 879 | #endif |
Denis Vlasenko | 8bdba4d | 2007-08-29 18:18:08 +0000 | [diff] [blame] | 880 | clearmems(); |
| 881 | if (iterations >= 0 && !--iterations) |
| 882 | break; |
| 883 | #if !ENABLE_FEATURE_USE_TERMIOS |
| 884 | sleep(interval); |
| 885 | #else |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 886 | if (option_mask32 & (OPT_b|OPT_EOF)) |
| 887 | /* batch mode, or EOF on stdin ("top </dev/null") */ |
| 888 | sleep(interval); |
| 889 | else if (safe_poll(pfd, 1, interval * 1000) > 0) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 890 | if (safe_read(STDIN_FILENO, &c, 1) != 1) { /* error/EOF? */ |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 891 | option_mask32 |= OPT_EOF; |
| 892 | continue; |
| 893 | } |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 894 | if (c == initial_settings.c_cc[VINTR]) |
"Vladimir N. Oleynik" | c218a29 | 2006-02-15 17:15:56 +0000 | [diff] [blame] | 895 | break; |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 896 | c |= 0x20; /* lowercase */ |
| 897 | if (c == 'q') |
| 898 | break; |
| 899 | if (c == 'n') { |
| 900 | USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;) |
Denis Vlasenko | 8bdba4d | 2007-08-29 18:18:08 +0000 | [diff] [blame] | 901 | sort_function[0] = pid_sort; |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 902 | } |
| 903 | if (c == 'm') { |
| 904 | USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 905 | sort_function[0] = mem_sort; |
Denis Vlasenko | 8bdba4d | 2007-08-29 18:18:08 +0000 | [diff] [blame] | 906 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 907 | sort_function[1] = pcpu_sort; |
| 908 | sort_function[2] = time_sort; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 909 | #endif |
| 910 | } |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 911 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 912 | if (c == 'p') { |
| 913 | USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 914 | sort_function[0] = pcpu_sort; |
| 915 | sort_function[1] = mem_sort; |
| 916 | sort_function[2] = time_sort; |
| 917 | } |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 918 | if (c == 't') { |
| 919 | USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 920 | sort_function[0] = time_sort; |
| 921 | sort_function[1] = mem_sort; |
| 922 | sort_function[2] = pcpu_sort; |
| 923 | } |
Denis Vlasenko | ff6e8e2 | 2007-09-08 16:51:19 +0000 | [diff] [blame] | 924 | #if ENABLE_FEATURE_TOPMEM |
| 925 | if (c == 's') { |
| 926 | scan_mask = TOPMEM_MASK; |
| 927 | free(prev_hist); |
| 928 | prev_hist = NULL; |
| 929 | prev_hist_count = 0; |
| 930 | sort_field = (sort_field + 1) % NUM_SORT_FIELD; |
| 931 | } |
| 932 | if (c == 'r') |
| 933 | inverted ^= 1; |
| 934 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 935 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 936 | } |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 937 | #endif /* FEATURE_USE_TERMIOS */ |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 938 | } /* end of "while (1)" */ |
| 939 | |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 940 | bb_putchar('\n'); |
Paul Fox | 275b929 | 2008-03-20 16:05:02 +0000 | [diff] [blame] | 941 | #if ENABLE_FEATURE_USE_TERMIOS |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 942 | reset_term(); |
Paul Fox | 275b929 | 2008-03-20 16:05:02 +0000 | [diff] [blame] | 943 | #endif |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 944 | return EXIT_SUCCESS; |
| 945 | } |