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