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