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