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]; |
| 43 | char comm[COMM_LEN]; |
| 44 | } top_status_t; |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 45 | |
| 46 | typedef struct jiffy_counts_t{ |
| 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; |
| 56 | unsigned 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 | |
| 61 | enum { SORT_DEPTH = 3 }; |
| 62 | |
| 63 | struct globals { |
| 64 | top_status_t *top; |
| 65 | int ntop; |
| 66 | #if ENABLE_FEATURE_USE_TERMIOS |
| 67 | struct termios initial_settings; |
| 68 | #endif |
| 69 | #if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
| 70 | cmp_funcp sort_function; |
| 71 | #else |
| 72 | cmp_funcp sort_function[SORT_DEPTH]; |
| 73 | struct save_hist *prev_hist; |
| 74 | int prev_hist_count; |
| 75 | jiffy_counts_t jif, prev_jif; |
| 76 | /* int hist_iterations; */ |
| 77 | unsigned total_pcpu; |
| 78 | /* unsigned long total_vsz; */ |
| 79 | #endif |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 80 | }; |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 81 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 82 | #define top (G.top ) |
| 83 | #define ntop (G.ntop ) |
| 84 | #if ENABLE_FEATURE_USE_TERMIOS |
| 85 | #define initial_settings (G. initial_settings ) |
| 86 | #endif |
| 87 | #define sort_function (G.sort_function ) |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 88 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 89 | #define prev_hist (G.prev_hist ) |
| 90 | #define prev_hist_count (G.prev_hist_count ) |
| 91 | #define jif (G.jif ) |
| 92 | #define prev_jif (G.prev_jif ) |
| 93 | #define total_pcpu (G.total_pcpu ) |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 94 | #endif |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 95 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 96 | #define OPT_BATCH_MODE (option_mask32 & 0x4) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 97 | |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 98 | |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 99 | #if ENABLE_FEATURE_USE_TERMIOS |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 100 | static int pid_sort(top_status_t *P, top_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 101 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 102 | /* Buggy wrt pids with high bit set */ |
| 103 | /* (linux pids are in [1..2^15-1]) */ |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 104 | return (Q->pid - P->pid); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 105 | } |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 106 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 107 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 108 | static int mem_sort(top_status_t *P, top_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 109 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 110 | /* We want to avoid unsigned->signed and truncation errors */ |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 111 | if (Q->vsz < P->vsz) return -1; |
| 112 | return Q->vsz != P->vsz; /* 0 if ==, 1 if > */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 115 | |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 116 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 117 | |
| 118 | static int pcpu_sort(top_status_t *P, top_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 119 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 120 | /* Buggy wrt ticks with high bit set */ |
| 121 | /* Affects only processes for which ticks overflow */ |
| 122 | return (int)Q->pcpu - (int)P->pcpu; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 125 | static int time_sort(top_status_t *P, top_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 126 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 127 | /* We want to avoid unsigned->signed and truncation errors */ |
| 128 | if (Q->ticks < P->ticks) return -1; |
| 129 | return Q->ticks != P->ticks; /* 0 if ==, 1 if > */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 132 | static int mult_lvl_cmp(void* a, void* b) |
| 133 | { |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 134 | int i, cmp_val; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 135 | |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 136 | for (i = 0; i < SORT_DEPTH; i++) { |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 137 | cmp_val = (*sort_function[i])(a, b); |
| 138 | if (cmp_val != 0) |
| 139 | return cmp_val; |
| 140 | } |
| 141 | return 0; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 144 | |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 145 | static void get_jiffy_counts(void) |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 146 | { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 147 | FILE* fp = xfopen("stat", "r"); |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 148 | prev_jif = jif; |
| 149 | if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld", |
| 150 | &jif.usr,&jif.nic,&jif.sys,&jif.idle, |
| 151 | &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) { |
Denis Vlasenko | 42dfcd2 | 2006-09-09 12:55:02 +0000 | [diff] [blame] | 152 | bb_error_msg_and_die("failed to read /proc/stat"); |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 153 | } |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 154 | fclose(fp); |
| 155 | jif.total = jif.usr + jif.nic + jif.sys + jif.idle |
| 156 | + jif.iowait + jif.irq + jif.softirq + jif.steal; |
| 157 | /* procps 2.x does not count iowait as busy time */ |
| 158 | jif.busy = jif.total - jif.idle - jif.iowait; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 161 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 162 | static void do_stats(void) |
| 163 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 164 | top_status_t *cur; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 165 | pid_t pid; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 166 | int i, last_i, n; |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 167 | struct save_hist *new_hist; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 168 | |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 169 | get_jiffy_counts(); |
| 170 | total_pcpu = 0; |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 171 | /* total_vsz = 0; */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 172 | new_hist = xmalloc(sizeof(struct save_hist)*ntop); |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 173 | /* |
| 174 | * Make a pass through the data to get stats. |
| 175 | */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 176 | /* hist_iterations = 0; */ |
| 177 | i = 0; |
| 178 | for (n = 0; n < ntop; n++) { |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 179 | cur = top + n; |
| 180 | |
| 181 | /* |
| 182 | * Calculate time in cur process. Time is sum of user time |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 183 | * and system time |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 184 | */ |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 185 | pid = cur->pid; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 186 | new_hist[n].ticks = cur->ticks; |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 187 | new_hist[n].pid = pid; |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 188 | |
| 189 | /* find matching entry from previous pass */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 190 | cur->pcpu = 0; |
| 191 | /* do not start at index 0, continue at last used one |
| 192 | * (brought hist_iterations from ~14000 down to 172) */ |
| 193 | last_i = i; |
| 194 | if (prev_hist_count) do { |
| 195 | if (prev_hist[i].pid == pid) { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 196 | cur->pcpu = cur->ticks - prev_hist[i].ticks; |
| 197 | total_pcpu += cur->pcpu; |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 198 | break; |
| 199 | } |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 200 | i = (i+1) % prev_hist_count; |
| 201 | /* hist_iterations++; */ |
| 202 | } while (i != last_i); |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 203 | /* total_vsz += cur->vsz; */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 207 | * Save cur frame's information. |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 208 | */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 209 | free(prev_hist); |
| 210 | prev_hist = new_hist; |
| 211 | prev_hist_count = ntop; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 212 | } |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 213 | #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 214 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 215 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 216 | /* display generic info (meminfo / loadavg) */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 217 | static unsigned long display_generic(int scr_width) |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 218 | { |
| 219 | FILE *fp; |
| 220 | char buf[80]; |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 221 | char scrbuf[80]; |
| 222 | char *end; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 223 | unsigned long total, used, mfree, shared, buffers, cached; |
| 224 | |
| 225 | /* read memory info */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 226 | fp = xfopen("meminfo", "r"); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 227 | |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 228 | /* |
| 229 | * Old kernels (such as 2.4.x) had a nice summary of memory info that |
| 230 | * we could parse, however this is gone entirely in 2.6. Try parsing |
| 231 | * the old way first, and if that fails, parse each field manually. |
| 232 | * |
| 233 | * First, we read in the first line. Old kernels will have bogus |
| 234 | * strings we don't care about, whereas new kernels will start right |
| 235 | * out with MemTotal: |
"Vladimir N. Oleynik" | 70678bc | 2005-11-29 12:32:33 +0000 | [diff] [blame] | 236 | * -- PFM. |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 237 | */ |
| 238 | if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) { |
"Vladimir N. Oleynik" | 70678bc | 2005-11-29 12:32:33 +0000 | [diff] [blame] | 239 | fgets(buf, sizeof(buf), fp); /* skip first line */ |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 240 | |
| 241 | fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu", |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 242 | &total, &used, &mfree, &shared, &buffers, &cached); |
| 243 | /* convert to kilobytes */ |
| 244 | used /= 1024; |
| 245 | mfree /= 1024; |
| 246 | shared /= 1024; |
| 247 | buffers /= 1024; |
| 248 | cached /= 1024; |
| 249 | total /= 1024; |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 250 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 251 | /* |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 252 | * Revert to manual parsing, which incidentally already has the |
| 253 | * sizes in kilobytes. This should be safe for both 2.4 and |
| 254 | * 2.6. |
| 255 | */ |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 256 | |
| 257 | fscanf(fp, "MemFree: %lu %s\n", &mfree, buf); |
| 258 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 259 | /* |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 260 | * MemShared: is no longer present in 2.6. Report this as 0, |
| 261 | * to maintain consistent behavior with normal procps. |
| 262 | */ |
| 263 | if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2) |
| 264 | shared = 0; |
| 265 | |
| 266 | fscanf(fp, "Buffers: %lu %s\n", &buffers, buf); |
| 267 | fscanf(fp, "Cached: %lu %s\n", &cached, buf); |
| 268 | |
| 269 | used = total - mfree; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 270 | } |
| 271 | fclose(fp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 272 | |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 273 | /* read load average as a string */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 274 | buf[0] = '\0'; |
Denis Vlasenko | 25d8062 | 2006-10-27 09:34:22 +0000 | [diff] [blame] | 275 | open_read_close("loadavg", buf, sizeof(buf)); |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 276 | end = strchr(buf, ' '); |
| 277 | if (end) end = strchr(end+1, ' '); |
| 278 | if (end) end = strchr(end+1, ' '); |
| 279 | if (end) *end = '\0'; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 280 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 281 | /* output memory info and load average */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 282 | /* clear screen & go to top */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 283 | if (scr_width > sizeof(scrbuf)) |
| 284 | scr_width = sizeof(scrbuf); |
| 285 | snprintf(scrbuf, scr_width, |
| 286 | "Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached", |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 287 | used, mfree, shared, buffers, cached); |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 288 | |
| 289 | 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] | 290 | |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 291 | if (ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS) { |
| 292 | /* |
| 293 | * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100% |
| 294 | */ |
Denis Vlasenko | b1e5add | 2007-06-10 18:04:54 +0000 | [diff] [blame] | 295 | /* using (unsigned) cast to make multiplication cheaper: */ |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 296 | #if ENABLE_FEATURE_TOP_DECIMALS |
| 297 | /* Generated code is approx +0.5k */ |
| 298 | #define CALC_STAT(xxx) div_t xxx = div(1000 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff, 10) |
| 299 | #define SHOW_STAT(xxx) xxx.quot, '0'+xxx.rem |
| 300 | #define FMT "%3u.%c%%" |
| 301 | #else |
Denis Vlasenko | b1e5add | 2007-06-10 18:04:54 +0000 | [diff] [blame] | 302 | #define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff |
| 303 | #define SHOW_STAT(xxx) xxx |
| 304 | #define FMT "%3u%%" |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 305 | #endif |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 306 | unsigned total_diff = (jif.total - prev_jif.total ? : 1); |
| 307 | CALC_STAT(usr); |
| 308 | CALC_STAT(sys); |
| 309 | CALC_STAT(nic); |
| 310 | CALC_STAT(idle); |
| 311 | CALC_STAT(iowait); |
| 312 | CALC_STAT(irq); |
| 313 | CALC_STAT(softirq); |
| 314 | //CALC_STAT(steal); |
| 315 | |
| 316 | snprintf(scrbuf, scr_width, |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 317 | /* Barely fits in 79 chars when in "decimals" mode. |
| 318 | * %3u in practice almost never displays "100" |
Denis Vlasenko | b1e5add | 2007-06-10 18:04:54 +0000 | [diff] [blame] | 319 | * and thus has implicit leading space: " 99" */ |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 320 | "CPU:"FMT" usr"FMT" sys"FMT" nice"FMT" idle"FMT" io"FMT" irq"FMT" softirq", |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 321 | SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle), |
| 322 | SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq) |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 323 | //, SHOW_STAT(steal) - what is this 'steal' thing? |
| 324 | // I doubt anyone wants to know it |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 325 | ); |
| 326 | puts(scrbuf); |
| 327 | #undef SHOW_STAT |
| 328 | #undef CALC_STAT |
Denis Vlasenko | b1e5add | 2007-06-10 18:04:54 +0000 | [diff] [blame] | 329 | #undef FMT |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Denis Vlasenko | 25d8062 | 2006-10-27 09:34:22 +0000 | [diff] [blame] | 332 | snprintf(scrbuf, scr_width, "Load average: %s", buf); |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 333 | puts(scrbuf); |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 334 | |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 335 | return total; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 338 | #if ENABLE_FEATURE_TOP_DECIMALS |
| 339 | #define UPSCALE 1000 |
| 340 | #define CALC_STAT(name, val) div_t name = div((val), 10) |
| 341 | #define SHOW_STAT(name) name.quot, '0'+name.rem |
| 342 | #define FMT "%3u.%c" |
| 343 | #else |
| 344 | #define UPSCALE 100 |
| 345 | #define CALC_STAT(name, val) unsigned name = (val) |
| 346 | #define SHOW_STAT(name) name |
| 347 | #define FMT " %3u%%" |
| 348 | #endif |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 349 | /* display process statuses */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 350 | static void display_status(int count, int scr_width) |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 351 | { |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 352 | enum { |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 353 | BITS_PER_INT = sizeof(int)*8 |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 354 | }; |
| 355 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 356 | top_status_t *s = top; |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 357 | char vsz_str_buf[8]; |
| 358 | unsigned long total_memory = display_generic(scr_width); /* or use total_vsz? */ |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 359 | /* xxx_shift and xxx_scale variables allow us to replace |
| 360 | * expensive divides with multiply and shift */ |
| 361 | unsigned pmem_shift, pmem_scale, pmem_half; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 362 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 363 | unsigned pcpu_shift, pcpu_scale, pcpu_half; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 364 | unsigned busy_jifs; |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 365 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 366 | /* what info of the processes is shown */ |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 367 | printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 368 | " PID PPID USER STAT VSZ %MEM %CPU COMMAND"); |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 369 | #define MIN_WIDTH \ |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 370 | sizeof( " PID PPID USER STAT VSZ %MEM %CPU C") |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 371 | #else |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 372 | |
| 373 | /* !CPU_USAGE_PERCENTAGE */ |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 374 | printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 375 | " PID PPID USER STAT VSZ %MEM COMMAND"); |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 376 | #define MIN_WIDTH \ |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 377 | sizeof( " PID PPID USER STAT VSZ %MEM C") |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 378 | #endif |
| 379 | |
| 380 | /* |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 381 | * MEM% = s->vsz/MemTotal |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 382 | */ |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 383 | pmem_shift = BITS_PER_INT-11; |
| 384 | pmem_scale = UPSCALE*(1U<<(BITS_PER_INT-11)) / total_memory; |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 385 | /* 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] | 386 | while (pmem_scale >= 512) { |
| 387 | pmem_scale /= 4; |
| 388 | pmem_shift -= 2; |
| 389 | } |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 390 | pmem_half = (1U << pmem_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 391 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
| 392 | busy_jifs = jif.busy - prev_jif.busy; |
| 393 | /* This happens if there were lots of short-lived processes |
| 394 | * between two top updates (e.g. compilation) */ |
| 395 | if (total_pcpu < busy_jifs) total_pcpu = busy_jifs; |
| 396 | |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 397 | /* |
| 398 | * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks |
| 399 | * (pcpu is delta of sys+user time between samples) |
| 400 | */ |
| 401 | /* (jif.xxx - prev_jif.xxx) and s->pcpu are |
| 402 | * in 0..~64000 range (HZ*update_interval). |
| 403 | * we assume that unsigned is at least 32-bit. |
| 404 | */ |
| 405 | pcpu_shift = 6; |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 406 | pcpu_scale = (UPSCALE*64*(uint16_t)busy_jifs ? : 1); |
| 407 | while (pcpu_scale < (1U<<(BITS_PER_INT-2))) { |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 408 | pcpu_scale *= 4; |
| 409 | pcpu_shift += 2; |
| 410 | } |
| 411 | pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1); |
| 412 | /* we want (s->pcpu * pcpu_scale) to never overflow */ |
| 413 | while (pcpu_scale >= 1024) { |
| 414 | pcpu_scale /= 4; |
| 415 | pcpu_shift -= 2; |
| 416 | } |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 417 | pcpu_half = (1U << pcpu_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2); |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 418 | /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 419 | #endif |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 420 | |
| 421 | /* Ok, all prelim data is ready, go thru the list */ |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 422 | while (count-- > 0) { |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 423 | int col = scr_width+1; |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 424 | CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift); |
| 425 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
| 426 | CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift); |
| 427 | #endif |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 428 | |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 429 | if (s->vsz >= 100*1024) |
| 430 | sprintf(vsz_str_buf, "%6ldM", s->vsz/1024); |
Manuel Novoa III | d499330 | 2002-09-18 19:27:10 +0000 | [diff] [blame] | 431 | else |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 432 | sprintf(vsz_str_buf, "%7ld", s->vsz); |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 433 | // PID PPID USER STAT VSZ %MEM [%CPU] COMMAND |
| 434 | col -= printf("\n" "%5u%6u %-8s %s%s" FMT |
| 435 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
| 436 | FMT |
| 437 | #endif |
| 438 | " ", |
| 439 | s->pid, s->ppid, get_cached_username(s->uid), |
| 440 | s->state, vsz_str_buf, |
| 441 | SHOW_STAT(pmem) |
| 442 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
| 443 | , SHOW_STAT(pcpu) |
| 444 | #endif |
| 445 | ); |
Denis Vlasenko | 25d8062 | 2006-10-27 09:34:22 +0000 | [diff] [blame] | 446 | if (col > 0) |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 447 | printf("%.*s", col, s->comm); |
Bernhard Reutner-Fischer | e2922e4 | 2006-05-19 12:48:56 +0000 | [diff] [blame] | 448 | /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu, |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 449 | jif.busy - prev_jif.busy, jif.total - prev_jif.total); */ |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 450 | s++; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 451 | } |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 452 | /* printf(" %d", hist_iterations); */ |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 453 | putchar(OPT_BATCH_MODE ? '\n' : '\r'); |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 454 | fflush(stdout); |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 455 | } |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 456 | #undef SHOW_STAT |
| 457 | #undef CALC_STAT |
| 458 | #undef FMT |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 459 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 460 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 461 | static void clearmems(void) |
| 462 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 463 | clear_username_cache(); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 464 | free(top); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 465 | top = 0; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 466 | ntop = 0; |
| 467 | } |
| 468 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 469 | |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 470 | #if ENABLE_FEATURE_USE_TERMIOS |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 471 | #include <termios.h> |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 472 | #include <signal.h> |
| 473 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 474 | static void reset_term(void) |
| 475 | { |
| 476 | tcsetattr(0, TCSANOW, (void *) &initial_settings); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 477 | #if ENABLE_FEATURE_CLEAN_UP |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 478 | clearmems(); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 479 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 480 | free(prev_hist); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 481 | #endif |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 482 | #endif /* FEATURE_CLEAN_UP */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 483 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 484 | |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 485 | static void sig_catcher(int sig ATTRIBUTE_UNUSED) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 486 | { |
| 487 | reset_term(); |
Rob Landley | db1ab1a | 2006-06-28 14:11:25 +0000 | [diff] [blame] | 488 | exit(1); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 489 | } |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 490 | #endif /* FEATURE_USE_TERMIOS */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 491 | |
| 492 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 493 | int top_main(int argc, char **argv); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 494 | int top_main(int argc, char **argv) |
| 495 | { |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 496 | int count, lines, col; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 497 | unsigned interval = 5; /* default update rate is 5 seconds */ |
| 498 | unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */ |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 499 | char *sinterval, *siterations; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 500 | #if ENABLE_FEATURE_USE_TERMIOS |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 501 | struct termios new_settings; |
| 502 | struct timeval tv; |
| 503 | fd_set readfds; |
| 504 | unsigned char c; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 505 | #endif /* FEATURE_USE_TERMIOS */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 506 | |
Denis Vlasenko | 42dfcd2 | 2006-09-09 12:55:02 +0000 | [diff] [blame] | 507 | interval = 5; |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 508 | |
| 509 | /* do normal option parsing */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 510 | opt_complementary = "-"; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 511 | getopt32(argc, argv, "d:n:b", &sinterval, &siterations); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 512 | if (option_mask32 & 0x1) interval = xatou(sinterval); // -d |
| 513 | if (option_mask32 & 0x2) iterations = xatou(siterations); // -n |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 514 | //if (option_mask32 & 0x4) // -b |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 515 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 516 | /* change to /proc */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 517 | xchdir("/proc"); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 518 | #if ENABLE_FEATURE_USE_TERMIOS |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 519 | tcgetattr(0, (void *) &initial_settings); |
| 520 | memcpy(&new_settings, &initial_settings, sizeof(struct termios)); |
Denis Vlasenko | 42dfcd2 | 2006-09-09 12:55:02 +0000 | [diff] [blame] | 521 | /* unbuffered input, turn off echo */ |
| 522 | new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 523 | |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 524 | signal(SIGTERM, sig_catcher); |
Rob Landley | db1ab1a | 2006-06-28 14:11:25 +0000 | [diff] [blame] | 525 | signal(SIGINT, sig_catcher); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 526 | tcsetattr(0, TCSANOW, (void *) &new_settings); |
| 527 | atexit(reset_term); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 528 | #endif /* FEATURE_USE_TERMIOS */ |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 529 | |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 530 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 531 | sort_function[0] = pcpu_sort; |
| 532 | sort_function[1] = mem_sort; |
| 533 | sort_function[2] = time_sort; |
| 534 | #else |
| 535 | sort_function = mem_sort; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 536 | #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 537 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 538 | while (1) { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 539 | procps_status_t *p = NULL; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 540 | |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 541 | /* Default to 25 lines - 5 lines for status */ |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 542 | lines = 24 - 3 USE_FEATURE_TOP_CPU_GLOBAL_PERCENTS( - 1); |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 543 | col = 79; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 544 | #if ENABLE_FEATURE_USE_TERMIOS |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 545 | get_terminal_width_height(0, &col, &lines); |
| 546 | if (lines < 5 || col < MIN_WIDTH) { |
| 547 | sleep(interval); |
| 548 | continue; |
| 549 | } |
Denis Vlasenko | 5a65447 | 2007-06-10 17:11:59 +0000 | [diff] [blame] | 550 | lines -= 3 USE_FEATURE_TOP_CPU_GLOBAL_PERCENTS( + 1); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 551 | #endif /* FEATURE_USE_TERMIOS */ |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 552 | |
| 553 | /* read process IDs & status for all the processes */ |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 554 | while ((p = procps_scan(p, 0 |
| 555 | | PSSCAN_PID |
| 556 | | PSSCAN_PPID |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 557 | | PSSCAN_VSZ |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 558 | | PSSCAN_STIME |
| 559 | | PSSCAN_UTIME |
| 560 | | PSSCAN_STATE |
| 561 | | PSSCAN_COMM |
| 562 | | PSSCAN_SID |
| 563 | | PSSCAN_UIDGID |
| 564 | ))) { |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 565 | int n = ntop; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 566 | top = xrealloc(top, (++ntop)*sizeof(top_status_t)); |
| 567 | top[n].pid = p->pid; |
| 568 | top[n].ppid = p->ppid; |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 569 | top[n].vsz = p->vsz; |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 570 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 571 | top[n].ticks = p->stime + p->utime; |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 572 | #endif |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 573 | top[n].uid = p->uid; |
| 574 | strcpy(top[n].state, p->state); |
| 575 | strcpy(top[n].comm, p->comm); |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 576 | } |
| 577 | if (ntop == 0) { |
Denis Vlasenko | 7451196 | 2007-06-11 16:31:55 +0000 | [diff] [blame] | 578 | bb_error_msg_and_die("no process info in /proc"); |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 579 | } |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 580 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 581 | if (!prev_hist_count) { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 582 | do_stats(); |
| 583 | sleep(1); |
| 584 | clearmems(); |
| 585 | continue; |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 586 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 587 | do_stats(); |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 588 | qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 589 | #else |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 590 | qsort(top, ntop, sizeof(top_status_t), (void*)sort_function); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 591 | #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 592 | count = lines; |
Denis Vlasenko | 25d8062 | 2006-10-27 09:34:22 +0000 | [diff] [blame] | 593 | if (OPT_BATCH_MODE || count > ntop) { |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 594 | count = ntop; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 595 | } |
| 596 | /* show status for each of the processes */ |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 597 | display_status(count, col); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 598 | #if ENABLE_FEATURE_USE_TERMIOS |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 599 | tv.tv_sec = interval; |
| 600 | tv.tv_usec = 0; |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 601 | FD_ZERO(&readfds); |
| 602 | FD_SET(0, &readfds); |
| 603 | select(1, &readfds, NULL, NULL, &tv); |
| 604 | if (FD_ISSET(0, &readfds)) { |
| 605 | if (read(0, &c, 1) <= 0) { /* signal */ |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 606 | return EXIT_FAILURE; |
| 607 | } |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 608 | if (c == 'q' || c == initial_settings.c_cc[VINTR]) |
"Vladimir N. Oleynik" | c218a29 | 2006-02-15 17:15:56 +0000 | [diff] [blame] | 609 | break; |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 610 | if (c == 'M') { |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 611 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 612 | sort_function[0] = mem_sort; |
| 613 | sort_function[1] = pcpu_sort; |
| 614 | sort_function[2] = time_sort; |
| 615 | #else |
| 616 | sort_function = mem_sort; |
| 617 | #endif |
| 618 | } |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 619 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 620 | if (c == 'P') { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 621 | sort_function[0] = pcpu_sort; |
| 622 | sort_function[1] = mem_sort; |
| 623 | sort_function[2] = time_sort; |
| 624 | } |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 625 | if (c == 'T') { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 626 | sort_function[0] = time_sort; |
| 627 | sort_function[1] = mem_sort; |
| 628 | sort_function[2] = pcpu_sort; |
| 629 | } |
| 630 | #endif |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 631 | if (c == 'N') { |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 632 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 633 | sort_function[0] = pid_sort; |
| 634 | #else |
| 635 | sort_function = pid_sort; |
| 636 | #endif |
| 637 | } |
| 638 | } |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 639 | if (!--iterations) |
| 640 | break; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 641 | #else |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 642 | sleep(interval); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 643 | #endif /* FEATURE_USE_TERMIOS */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 644 | clearmems(); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 645 | } |
Rob Landley | 997650b | 2006-04-24 23:13:46 +0000 | [diff] [blame] | 646 | if (ENABLE_FEATURE_CLEAN_UP) |
"Vladimir N. Oleynik" | c218a29 | 2006-02-15 17:15:56 +0000 | [diff] [blame] | 647 | clearmems(); |
| 648 | putchar('\n'); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 649 | return EXIT_SUCCESS; |
| 650 | } |