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