blob: 0da742f0b1a146e34b908b3062b3d2307fcac543 [file] [log] [blame]
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +00001/* vi: set sw=4 ts=4: */
Eric Andersen420b2082002-09-17 22:14:58 +00002/*
3 * A tiny 'top' utility.
4 *
Eric Andersen08a72202002-09-30 20:52:10 +00005 * 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 Andersenc7bda1c2004-03-15 08:29:22 +000011 *
Eric Andersen420b2082002-09-17 22:14:58 +000012 * NOTES:
13 * - At startup this changes to /proc, all the reads are then
14 * relative to that.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000015 *
Eric Andersen420b2082002-09-17 22:14:58 +000016 * (C) Eero Tamminen <oak at welho dot com>
Eric Andersen08a72202002-09-30 20:52:10 +000017 *
Eric Andersenaff114c2004-04-14 17:51:38 +000018 * Rewritten by Vladimir Oleynik (C) 2002 <dzo@simtreas.ru>
Eric Andersen420b2082002-09-17 22:14:58 +000019 */
Eric Andersen08a72202002-09-30 20:52:10 +000020
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 Vlasenkob6adbf12007-05-26 19:00:18 +000031#include "libbb.h"
Eric Andersen420b2082002-09-17 22:14:58 +000032
Eric Andersen08a72202002-09-30 20:52:10 +000033
Denis Vlasenko85818632007-04-19 14:47:11 +000034typedef struct top_status_t {
Mike Frysinger0aa6ba52007-02-08 08:21:58 +000035 unsigned long vsz;
Denis Vlasenko459e4d62006-11-05 00:43:51 +000036#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 Vlasenko98ebab82007-06-30 14:47:41 +000043 char comm[COMM_LEN];
Denis Vlasenko459e4d62006-11-05 00:43:51 +000044} top_status_t;
Denis Vlasenko85818632007-04-19 14:47:11 +000045
Denis Vlasenko98ebab82007-06-30 14:47:41 +000046typedef struct jiffy_counts_t {
Denis Vlasenko85818632007-04-19 14:47:11 +000047 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 Vlasenko459e4d62006-11-05 00:43:51 +000052/* This structure stores some critical information from one frame to
53 the next. Used for finding deltas. */
Denis Vlasenko85818632007-04-19 14:47:11 +000054typedef struct save_hist {
Denis Vlasenko459e4d62006-11-05 00:43:51 +000055 unsigned long ticks;
56 unsigned pid;
Denis Vlasenko85818632007-04-19 14:47:11 +000057} save_hist;
58
59typedef int (*cmp_funcp)(top_status_t *P, top_status_t *Q);
60
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +000061
Denis Vlasenko85818632007-04-19 14:47:11 +000062enum { SORT_DEPTH = 3 };
63
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +000064
Denis Vlasenko85818632007-04-19 14:47:11 +000065struct globals {
66 top_status_t *top;
67 int ntop;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +000068#if ENABLE_FEATURE_TOPMEM
69 smallint sort_field;
70 smallint inverted;
71#endif
Denis Vlasenko85818632007-04-19 14:47:11 +000072#if ENABLE_FEATURE_USE_TERMIOS
73 struct termios initial_settings;
74#endif
75#if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +000076 cmp_funcp sort_function[1];
Denis Vlasenko85818632007-04-19 14:47:11 +000077#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 Vlasenko55761362007-10-16 22:53:05 +000086 char line_buf[80];
Denis Vlasenko459e4d62006-11-05 00:43:51 +000087};
Denis Vlasenko55761362007-10-16 22:53:05 +000088
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +000089enum { LINE_BUF_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line_buf) };
Denis Vlasenko55761362007-10-16 22:53:05 +000090
Denis Vlasenko85818632007-04-19 14:47:11 +000091#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenkoe7c1ad12007-09-08 17:21:01 +000092#define INIT_G() \
93 do { \
94 struct G_sizecheck { \
95 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
96 }; \
97 } while (0)
Denis Vlasenko85818632007-04-19 14:47:11 +000098#define top (G.top )
99#define ntop (G.ntop )
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000100#define sort_field (G.sort_field )
101#define inverted (G.inverted )
102#define initial_settings (G.initial_settings )
Denis Vlasenko85818632007-04-19 14:47:11 +0000103#define sort_function (G.sort_function )
Denis Vlasenko85818632007-04-19 14:47:11 +0000104#define prev_hist (G.prev_hist )
105#define prev_hist_count (G.prev_hist_count )
106#define jif (G.jif )
107#define prev_jif (G.prev_jif )
108#define total_pcpu (G.total_pcpu )
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000109#define line_buf (G.line_buf )
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000110
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000111
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000112#define OPT_BATCH_MODE (option_mask32 & 0x4)
Eric Andersen08a72202002-09-30 20:52:10 +0000113
Denis Vlasenko85818632007-04-19 14:47:11 +0000114
Denis Vlasenkofa076802006-11-05 00:38:51 +0000115#if ENABLE_FEATURE_USE_TERMIOS
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000116static int pid_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +0000117{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000118 /* Buggy wrt pids with high bit set */
119 /* (linux pids are in [1..2^15-1]) */
Rob Landleyb2804552006-02-13 22:04:27 +0000120 return (Q->pid - P->pid);
Eric Andersen08a72202002-09-30 20:52:10 +0000121}
Mike Frysinger223b8872005-07-30 09:42:05 +0000122#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000123
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000124static int mem_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +0000125{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000126 /* We want to avoid unsigned->signed and truncation errors */
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000127 if (Q->vsz < P->vsz) return -1;
128 return Q->vsz != P->vsz; /* 0 if ==, 1 if > */
Eric Andersen08a72202002-09-30 20:52:10 +0000129}
130
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000131
Denis Vlasenko85818632007-04-19 14:47:11 +0000132#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000133
134static int pcpu_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +0000135{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000136 /* Buggy wrt ticks with high bit set */
137 /* Affects only processes for which ticks overflow */
138 return (int)Q->pcpu - (int)P->pcpu;
Eric Andersen08a72202002-09-30 20:52:10 +0000139}
140
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000141static int time_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +0000142{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000143 /* We want to avoid unsigned->signed and truncation errors */
144 if (Q->ticks < P->ticks) return -1;
145 return Q->ticks != P->ticks; /* 0 if ==, 1 if > */
Eric Andersen08a72202002-09-30 20:52:10 +0000146}
147
Denis Vlasenkoac678ec2007-04-16 22:32:04 +0000148static int mult_lvl_cmp(void* a, void* b)
149{
Rob Landleyb2804552006-02-13 22:04:27 +0000150 int i, cmp_val;
Eric Andersen08a72202002-09-30 20:52:10 +0000151
Denis Vlasenkofa076802006-11-05 00:38:51 +0000152 for (i = 0; i < SORT_DEPTH; i++) {
Rob Landleyb2804552006-02-13 22:04:27 +0000153 cmp_val = (*sort_function[i])(a, b);
154 if (cmp_val != 0)
155 return cmp_val;
156 }
157 return 0;
Eric Andersen08a72202002-09-30 20:52:10 +0000158}
159
Eric Andersen08a72202002-09-30 20:52:10 +0000160
Rob Landley997650b2006-04-24 23:13:46 +0000161static void get_jiffy_counts(void)
Rob Landleyb2804552006-02-13 22:04:27 +0000162{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000163 FILE* fp = xfopen("stat", "r");
Rob Landley997650b2006-04-24 23:13:46 +0000164 prev_jif = jif;
165 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
166 &jif.usr,&jif.nic,&jif.sys,&jif.idle,
167 &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
Denis Vlasenko42dfcd22006-09-09 12:55:02 +0000168 bb_error_msg_and_die("failed to read /proc/stat");
Rob Landleyb2804552006-02-13 22:04:27 +0000169 }
Rob Landley997650b2006-04-24 23:13:46 +0000170 fclose(fp);
171 jif.total = jif.usr + jif.nic + jif.sys + jif.idle
172 + jif.iowait + jif.irq + jif.softirq + jif.steal;
173 /* procps 2.x does not count iowait as busy time */
174 jif.busy = jif.total - jif.idle - jif.iowait;
Eric Andersen08a72202002-09-30 20:52:10 +0000175}
176
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000177
Eric Andersen08a72202002-09-30 20:52:10 +0000178static void do_stats(void)
179{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000180 top_status_t *cur;
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000181 pid_t pid;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000182 int i, last_i, n;
Rob Landley997650b2006-04-24 23:13:46 +0000183 struct save_hist *new_hist;
Eric Andersen08a72202002-09-30 20:52:10 +0000184
Rob Landley997650b2006-04-24 23:13:46 +0000185 get_jiffy_counts();
186 total_pcpu = 0;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000187 /* total_vsz = 0; */
Rob Landley997650b2006-04-24 23:13:46 +0000188 new_hist = xmalloc(sizeof(struct save_hist)*ntop);
Rob Landleyb2804552006-02-13 22:04:27 +0000189 /*
190 * Make a pass through the data to get stats.
191 */
Rob Landley997650b2006-04-24 23:13:46 +0000192 /* hist_iterations = 0; */
193 i = 0;
194 for (n = 0; n < ntop; n++) {
Rob Landleyb2804552006-02-13 22:04:27 +0000195 cur = top + n;
196
197 /*
198 * Calculate time in cur process. Time is sum of user time
Rob Landley997650b2006-04-24 23:13:46 +0000199 * and system time
Rob Landleyb2804552006-02-13 22:04:27 +0000200 */
Rob Landleyb2804552006-02-13 22:04:27 +0000201 pid = cur->pid;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000202 new_hist[n].ticks = cur->ticks;
Rob Landley997650b2006-04-24 23:13:46 +0000203 new_hist[n].pid = pid;
Rob Landleyb2804552006-02-13 22:04:27 +0000204
205 /* find matching entry from previous pass */
Rob Landley997650b2006-04-24 23:13:46 +0000206 cur->pcpu = 0;
207 /* do not start at index 0, continue at last used one
208 * (brought hist_iterations from ~14000 down to 172) */
209 last_i = i;
210 if (prev_hist_count) do {
211 if (prev_hist[i].pid == pid) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000212 cur->pcpu = cur->ticks - prev_hist[i].ticks;
213 total_pcpu += cur->pcpu;
Rob Landleyb2804552006-02-13 22:04:27 +0000214 break;
215 }
Rob Landley997650b2006-04-24 23:13:46 +0000216 i = (i+1) % prev_hist_count;
217 /* hist_iterations++; */
218 } while (i != last_i);
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000219 /* total_vsz += cur->vsz; */
Eric Andersen08a72202002-09-30 20:52:10 +0000220 }
221
222 /*
Rob Landleyb2804552006-02-13 22:04:27 +0000223 * Save cur frame's information.
Eric Andersen08a72202002-09-30 20:52:10 +0000224 */
Rob Landley997650b2006-04-24 23:13:46 +0000225 free(prev_hist);
226 prev_hist = new_hist;
227 prev_hist_count = ntop;
Eric Andersen08a72202002-09-30 20:52:10 +0000228}
Denis Vlasenkofa076802006-11-05 00:38:51 +0000229#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Eric Andersen08a72202002-09-30 20:52:10 +0000230
Denis Vlasenko6ee023c2007-08-23 10:52:52 +0000231#if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && ENABLE_FEATURE_TOP_DECIMALS
232/* formats 7 char string (8 with terminating NUL) */
233static char *fmt_100percent_8(char pbuf[8], unsigned value, unsigned total)
234{
235 unsigned t;
236 if (value >= total) { /* 100% ? */
237 strcpy(pbuf, " 100% ");
238 return pbuf;
239 }
240 /* else generate " [N/space]N.N% " string */
241 value = 1000 * value / total;
242 t = value / 100;
243 value = value % 100;
244 pbuf[0] = ' ';
245 pbuf[1] = t ? t + '0' : ' ';
246 pbuf[2] = '0' + (value / 10);
247 pbuf[3] = '.';
248 pbuf[4] = '0' + (value % 10);
249 pbuf[5] = '%';
250 pbuf[6] = ' ';
251 pbuf[7] = '\0';
252 return pbuf;
253}
254#endif
255
Denis Vlasenko05241802007-08-29 18:34:26 +0000256static unsigned long display_header(int scr_width)
Eric Andersen420b2082002-09-17 22:14:58 +0000257{
258 FILE *fp;
259 char buf[80];
Rob Landley997650b2006-04-24 23:13:46 +0000260 char scrbuf[80];
Eric Andersen420b2082002-09-17 22:14:58 +0000261 unsigned long total, used, mfree, shared, buffers, cached;
Denis Vlasenko6ee023c2007-08-23 10:52:52 +0000262#if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
Denis Vlasenko110967a2007-07-15 19:27:48 +0000263 unsigned total_diff;
Denis Vlasenko856be772007-08-17 08:29:48 +0000264#endif
Denis Vlasenko110967a2007-07-15 19:27:48 +0000265
Eric Andersen420b2082002-09-17 22:14:58 +0000266 /* read memory info */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000267 fp = xfopen("meminfo", "r");
Eric Andersen420b2082002-09-17 22:14:58 +0000268
Eric Andersen7857c032003-10-11 18:47:20 +0000269 /*
270 * Old kernels (such as 2.4.x) had a nice summary of memory info that
271 * we could parse, however this is gone entirely in 2.6. Try parsing
272 * the old way first, and if that fails, parse each field manually.
273 *
274 * First, we read in the first line. Old kernels will have bogus
275 * strings we don't care about, whereas new kernels will start right
276 * out with MemTotal:
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000277 * -- PFM.
Eric Andersen7857c032003-10-11 18:47:20 +0000278 */
279 if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000280 fgets(buf, sizeof(buf), fp); /* skip first line */
Eric Andersen7857c032003-10-11 18:47:20 +0000281
282 fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
Denis Vlasenko5a654472007-06-10 17:11:59 +0000283 &total, &used, &mfree, &shared, &buffers, &cached);
284 /* convert to kilobytes */
285 used /= 1024;
286 mfree /= 1024;
287 shared /= 1024;
288 buffers /= 1024;
289 cached /= 1024;
290 total /= 1024;
Eric Andersen7857c032003-10-11 18:47:20 +0000291 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000292 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000293 * Revert to manual parsing, which incidentally already has the
294 * sizes in kilobytes. This should be safe for both 2.4 and
295 * 2.6.
296 */
Eric Andersen7857c032003-10-11 18:47:20 +0000297
298 fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
299
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000300 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000301 * MemShared: is no longer present in 2.6. Report this as 0,
302 * to maintain consistent behavior with normal procps.
303 */
304 if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
305 shared = 0;
306
307 fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
308 fscanf(fp, "Cached: %lu %s\n", &cached, buf);
309
310 used = total - mfree;
Eric Andersen420b2082002-09-17 22:14:58 +0000311 }
312 fclose(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000313
Denis Vlasenko110967a2007-07-15 19:27:48 +0000314 /* output memory info */
Rob Landley997650b2006-04-24 23:13:46 +0000315 if (scr_width > sizeof(scrbuf))
316 scr_width = sizeof(scrbuf);
317 snprintf(scrbuf, scr_width,
Denis Vlasenkoc1166c32007-07-15 19:23:38 +0000318 "Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached",
Rob Landleyb2804552006-02-13 22:04:27 +0000319 used, mfree, shared, buffers, cached);
Denis Vlasenko110967a2007-07-15 19:27:48 +0000320 /* clear screen & go to top */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000321 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000322
Denis Vlasenko856be772007-08-17 08:29:48 +0000323#if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
324 /*
325 * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
326 */
327 /* using (unsigned) casts to make operations cheaper */
328 total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1);
Denis Vlasenko74511962007-06-11 16:31:55 +0000329#if ENABLE_FEATURE_TOP_DECIMALS
Denis Vlasenko110967a2007-07-15 19:27:48 +0000330/* Generated code is approx +0.3k */
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000331#define CALC_STAT(xxx) char xxx[8]
Denis Vlasenko6ee023c2007-08-23 10:52:52 +0000332#define SHOW_STAT(xxx) fmt_100percent_8(xxx, (unsigned)(jif.xxx - prev_jif.xxx), total_diff)
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000333#define FMT "%s"
Denis Vlasenko74511962007-06-11 16:31:55 +0000334#else
Denis Vlasenkob1e5add2007-06-10 18:04:54 +0000335#define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
336#define SHOW_STAT(xxx) xxx
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000337#define FMT "%4u%% "
Denis Vlasenko74511962007-06-11 16:31:55 +0000338#endif
Denis Vlasenko856be772007-08-17 08:29:48 +0000339 { /* need block: CALC_STAT are declarations */
340 CALC_STAT(usr);
341 CALC_STAT(sys);
342 CALC_STAT(nic);
343 CALC_STAT(idle);
344 CALC_STAT(iowait);
345 CALC_STAT(irq);
346 CALC_STAT(softirq);
347 //CALC_STAT(steal);
Denis Vlasenko5a654472007-06-10 17:11:59 +0000348
Denis Vlasenko856be772007-08-17 08:29:48 +0000349 snprintf(scrbuf, scr_width,
350 /* Barely fits in 79 chars when in "decimals" mode. */
351 "CPU:"FMT"usr"FMT"sys"FMT"nice"FMT"idle"FMT"io"FMT"irq"FMT"softirq",
352 SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
353 SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
354 //, SHOW_STAT(steal) - what is this 'steal' thing?
355 // I doubt anyone wants to know it
356 );
357 }
358 puts(scrbuf);
Denis Vlasenko5a654472007-06-10 17:11:59 +0000359#undef SHOW_STAT
360#undef CALC_STAT
Denis Vlasenkob1e5add2007-06-10 18:04:54 +0000361#undef FMT
Denis Vlasenko856be772007-08-17 08:29:48 +0000362#endif
Denis Vlasenko5a654472007-06-10 17:11:59 +0000363
Denis Vlasenko110967a2007-07-15 19:27:48 +0000364 /* read load average as a string */
365 buf[0] = '\0';
366 open_read_close("loadavg", buf, sizeof("N.NN N.NN N.NN")-1);
367 buf[sizeof("N.NN N.NN N.NN")-1] = '\0';
Denis Vlasenko25d80622006-10-27 09:34:22 +0000368 snprintf(scrbuf, scr_width, "Load average: %s", buf);
Denis Vlasenko5a654472007-06-10 17:11:59 +0000369 puts(scrbuf);
Rob Landley997650b2006-04-24 23:13:46 +0000370
Eric Andersen7857c032003-10-11 18:47:20 +0000371 return total;
Eric Andersen420b2082002-09-17 22:14:58 +0000372}
373
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000374static NOINLINE void display_process_list(int count, int scr_width)
Eric Andersen420b2082002-09-17 22:14:58 +0000375{
Rob Landley997650b2006-04-24 23:13:46 +0000376 enum {
Denis Vlasenko74511962007-06-11 16:31:55 +0000377 BITS_PER_INT = sizeof(int)*8
Rob Landley997650b2006-04-24 23:13:46 +0000378 };
379
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000380 top_status_t *s = top;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000381 char vsz_str_buf[8];
Denis Vlasenko05241802007-08-29 18:34:26 +0000382 unsigned long total_memory = display_header(scr_width); /* or use total_vsz? */
Denis Vlasenko74511962007-06-11 16:31:55 +0000383 /* xxx_shift and xxx_scale variables allow us to replace
384 * expensive divides with multiply and shift */
385 unsigned pmem_shift, pmem_scale, pmem_half;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000386#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenko74511962007-06-11 16:31:55 +0000387 unsigned pcpu_shift, pcpu_scale, pcpu_half;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000388 unsigned busy_jifs;
Rob Landley997650b2006-04-24 23:13:46 +0000389
Eric Andersen420b2082002-09-17 22:14:58 +0000390 /* what info of the processes is shown */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000391 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
Denis Vlasenko74511962007-06-11 16:31:55 +0000392 " PID PPID USER STAT VSZ %MEM %CPU COMMAND");
Eric Andersen08a72202002-09-30 20:52:10 +0000393#else
Denis Vlasenko74511962007-06-11 16:31:55 +0000394
395 /* !CPU_USAGE_PERCENTAGE */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000396 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
Denis Vlasenko74511962007-06-11 16:31:55 +0000397 " PID PPID USER STAT VSZ %MEM COMMAND");
Rob Landley997650b2006-04-24 23:13:46 +0000398#endif
399
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000400#if ENABLE_FEATURE_TOP_DECIMALS
401#define UPSCALE 1000
402#define CALC_STAT(name, val) div_t name = div((val), 10)
403#define SHOW_STAT(name) name.quot, '0'+name.rem
404#define FMT "%3u.%c"
405#else
406#define UPSCALE 100
407#define CALC_STAT(name, val) unsigned name = (val)
408#define SHOW_STAT(name) name
409#define FMT "%4u%%"
410#endif
Rob Landley997650b2006-04-24 23:13:46 +0000411 /*
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000412 * MEM% = s->vsz/MemTotal
Rob Landley997650b2006-04-24 23:13:46 +0000413 */
Denis Vlasenko74511962007-06-11 16:31:55 +0000414 pmem_shift = BITS_PER_INT-11;
415 pmem_scale = UPSCALE*(1U<<(BITS_PER_INT-11)) / total_memory;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000416 /* s->vsz is in kb. we want (s->vsz * pmem_scale) to never overflow */
Rob Landley997650b2006-04-24 23:13:46 +0000417 while (pmem_scale >= 512) {
418 pmem_scale /= 4;
419 pmem_shift -= 2;
420 }
Denis Vlasenko74511962007-06-11 16:31:55 +0000421 pmem_half = (1U << pmem_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000422#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
423 busy_jifs = jif.busy - prev_jif.busy;
424 /* This happens if there were lots of short-lived processes
425 * between two top updates (e.g. compilation) */
426 if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
427
Rob Landley997650b2006-04-24 23:13:46 +0000428 /*
429 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
430 * (pcpu is delta of sys+user time between samples)
431 */
432 /* (jif.xxx - prev_jif.xxx) and s->pcpu are
433 * in 0..~64000 range (HZ*update_interval).
434 * we assume that unsigned is at least 32-bit.
435 */
436 pcpu_shift = 6;
Denis Vlasenko74511962007-06-11 16:31:55 +0000437 pcpu_scale = (UPSCALE*64*(uint16_t)busy_jifs ? : 1);
438 while (pcpu_scale < (1U<<(BITS_PER_INT-2))) {
Rob Landley997650b2006-04-24 23:13:46 +0000439 pcpu_scale *= 4;
440 pcpu_shift += 2;
441 }
442 pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1);
443 /* we want (s->pcpu * pcpu_scale) to never overflow */
444 while (pcpu_scale >= 1024) {
445 pcpu_scale /= 4;
446 pcpu_shift -= 2;
447 }
Denis Vlasenko74511962007-06-11 16:31:55 +0000448 pcpu_half = (1U << pcpu_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
Rob Landley997650b2006-04-24 23:13:46 +0000449 /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
Eric Andersen08a72202002-09-30 20:52:10 +0000450#endif
Denis Vlasenko74511962007-06-11 16:31:55 +0000451
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000452 scr_width += 2; /* account for leading '\n' and trailing NUL */
453 /* Ok, all preliminary data is ready, go thru the list */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000454 while (count-- > 0) {
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000455 unsigned col;
Denis Vlasenko74511962007-06-11 16:31:55 +0000456 CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift);
457#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
458 CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift);
459#endif
Eric Andersen420b2082002-09-17 22:14:58 +0000460
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000461 if (s->vsz >= 100000)
Denis Vlasenkob308d812007-08-28 19:35:34 +0000462 sprintf(vsz_str_buf, "%6ldm", s->vsz/1024);
Manuel Novoa III d4993302002-09-18 19:27:10 +0000463 else
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000464 sprintf(vsz_str_buf, "%7ld", s->vsz);
Denis Vlasenko74511962007-06-11 16:31:55 +0000465 // PID PPID USER STAT VSZ %MEM [%CPU] COMMAND
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000466 col = snprintf(line_buf, scr_width,
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000467 "\n" "%5u%6u %-8.8s %s%s" FMT
Denis Vlasenko74511962007-06-11 16:31:55 +0000468#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
469 FMT
470#endif
471 " ",
472 s->pid, s->ppid, get_cached_username(s->uid),
473 s->state, vsz_str_buf,
474 SHOW_STAT(pmem)
475#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
476 , SHOW_STAT(pcpu)
477#endif
478 );
Denis Vlasenko4d7605a2007-09-08 17:42:00 +0000479 if (col + 1 < scr_width)
480 read_cmdline(line_buf + col, scr_width - col - 1, s->pid, s->comm);
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000481 fputs(line_buf, stdout);
Bernhard Reutner-Fischere2922e42006-05-19 12:48:56 +0000482 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
Rob Landley997650b2006-04-24 23:13:46 +0000483 jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
Eric Andersen420b2082002-09-17 22:14:58 +0000484 s++;
Eric Andersen08a72202002-09-30 20:52:10 +0000485 }
Rob Landley997650b2006-04-24 23:13:46 +0000486 /* printf(" %d", hist_iterations); */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000487 bb_putchar(OPT_BATCH_MODE ? '\n' : '\r');
Rob Landley997650b2006-04-24 23:13:46 +0000488 fflush(stdout);
Eric Andersen44608e92002-10-22 12:21:15 +0000489}
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000490#undef UPSCALE
Denis Vlasenko74511962007-06-11 16:31:55 +0000491#undef SHOW_STAT
492#undef CALC_STAT
493#undef FMT
Eric Andersen44608e92002-10-22 12:21:15 +0000494
495static void clearmems(void)
496{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000497 clear_username_cache();
Eric Andersen08a72202002-09-30 20:52:10 +0000498 free(top);
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000499 top = NULL;
Eric Andersen08a72202002-09-30 20:52:10 +0000500 ntop = 0;
501}
502
Denis Vlasenkofa076802006-11-05 00:38:51 +0000503#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000504#include <termios.h>
Eric Andersen08a72202002-09-30 20:52:10 +0000505#include <signal.h>
506
Eric Andersen08a72202002-09-30 20:52:10 +0000507static void reset_term(void)
508{
509 tcsetattr(0, TCSANOW, (void *) &initial_settings);
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000510 if (ENABLE_FEATURE_CLEAN_UP) {
511 clearmems();
Denis Vlasenkofa076802006-11-05 00:38:51 +0000512#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000513 free(prev_hist);
Eric Andersen08a72202002-09-30 20:52:10 +0000514#endif
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000515 }
Eric Andersen08a72202002-09-30 20:52:10 +0000516}
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000517
Rob Landleyb2804552006-02-13 22:04:27 +0000518static void sig_catcher(int sig ATTRIBUTE_UNUSED)
Eric Andersen08a72202002-09-30 20:52:10 +0000519{
520 reset_term();
Rob Landleydb1ab1a2006-06-28 14:11:25 +0000521 exit(1);
Eric Andersen08a72202002-09-30 20:52:10 +0000522}
Denis Vlasenkofa076802006-11-05 00:38:51 +0000523#endif /* FEATURE_USE_TERMIOS */
Eric Andersen08a72202002-09-30 20:52:10 +0000524
Denis Vlasenkoe7c1ad12007-09-08 17:21:01 +0000525/*
526 * TOPMEM support
527 */
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000528
529typedef unsigned long mem_t;
530
531typedef struct topmem_status_t {
532 unsigned pid;
533 char comm[COMM_LEN];
534 /* vsz doesn't count /dev/xxx mappings except /dev/zero */
535 mem_t vsz ;
536 mem_t vszrw ;
537 mem_t rss ;
538 mem_t rss_sh ;
539 mem_t dirty ;
540 mem_t dirty_sh;
541 mem_t stack ;
542} topmem_status_t;
543
544enum { NUM_SORT_FIELD = 7 };
545
546#define topmem ((topmem_status_t*)top)
547
548#if ENABLE_FEATURE_TOPMEM
549static int topmem_sort(char *a, char *b)
550{
551 int n;
552 mem_t l, r;
553
554 n = offsetof(topmem_status_t, vsz) + (sort_field * sizeof(mem_t));
555 l = *(mem_t*)(a + n);
556 r = *(mem_t*)(b + n);
557// if (l == r) {
558// l = a->mapped_rw;
559// r = b->mapped_rw;
560// }
561 /* We want to avoid unsigned->signed and truncation errors */
562 /* l>r: -1, l=r: 0, l<r: 1 */
563 n = (l > r) ? -1 : (l != r);
564 return inverted ? -n : n;
565}
566
567/* Cut "NNNN " out of " NNNN kb" */
568static char *grab_number(char *str, const char *match, unsigned sz)
569{
570 if (strncmp(str, match, sz) == 0) {
571 str = skip_whitespace(str + sz);
572 (skip_non_whitespace(str))[1] = '\0';
573 return xstrdup(str);
574 }
575 return NULL;
576}
577
578/* display header info (meminfo / loadavg) */
579static void display_topmem_header(int scr_width)
580{
581 char linebuf[128];
582 int i;
583 FILE *fp;
584 union {
585 struct {
586 /* 1 */ char *total;
587 /* 2 */ char *mfree;
588 /* 3 */ char *buf;
589 /* 4 */ char *cache;
590 /* 5 */ char *swaptotal;
591 /* 6 */ char *swapfree;
592 /* 7 */ char *dirty;
593 /* 8 */ char *mwrite;
594 /* 9 */ char *anon;
595 /* 10 */ char *map;
596 /* 11 */ char *slab;
597 };
598 char *str[11];
599 } Z;
600#define total Z.total
601#define mfree Z.mfree
602#define buf Z.buf
603#define cache Z.cache
604#define swaptotal Z.swaptotal
605#define swapfree Z.swapfree
606#define dirty Z.dirty
607#define mwrite Z.mwrite
608#define anon Z.anon
609#define map Z.map
610#define slab Z.slab
611#define str Z.str
612
613 memset(&Z, 0, sizeof(Z));
614
615 /* read memory info */
616 fp = xfopen("meminfo", "r");
617 while (fgets(linebuf, sizeof(linebuf), fp)) {
618 char *p;
619
620#define SCAN(match, name) \
621 p = grab_number(linebuf, match, sizeof(match)-1); \
622 if (p) { name = p; continue; }
623
624 SCAN("MemTotal:", total);
625 SCAN("MemFree:", mfree);
626 SCAN("Buffers:", buf);
627 SCAN("Cached:", cache);
628 SCAN("SwapTotal:", swaptotal);
629 SCAN("SwapFree:", swapfree);
630 SCAN("Dirty:", dirty);
631 SCAN("Writeback:", mwrite);
632 SCAN("AnonPages:", anon);
633 SCAN("Mapped:", map);
634 SCAN("Slab:", slab);
635#undef SCAN
636 }
637 fclose(fp);
638
639#define S(s) (s ? s : "0")
640 snprintf(linebuf, sizeof(linebuf),
641 "Mem %stotal %sanon %smap %sfree",
642 S(total), S(anon), S(map), S(mfree));
643 printf(OPT_BATCH_MODE ? "%.*s\n" : "\e[H\e[J%.*s\n", scr_width, linebuf);
644
645 snprintf(linebuf, sizeof(linebuf),
646 " %sslab %sbuf %scache %sdirty %swrite",
647 S(slab), S(buf), S(cache), S(dirty), S(mwrite));
648 printf("%.*s\n", scr_width, linebuf);
649
650 snprintf(linebuf, sizeof(linebuf),
651 "Swap %stotal %sfree", // TODO: % used?
652 S(swaptotal), S(swapfree));
653 printf("%.*s\n", scr_width, linebuf);
654#undef S
655
656 for (i = 0; i < ARRAY_SIZE(str); i++)
657 free(str[i]);
658#undef total
659#undef free
660#undef buf
661#undef cache
662#undef swaptotal
663#undef swapfree
664#undef dirty
665#undef write
666#undef anon
667#undef map
668#undef slab
669#undef str
670}
671
672// Converts unsigned long long value into compact 5-char
673// representation. Sixth char is always ' '
674static void smart_ulltoa6(unsigned long long ul, char buf[6])
675{
676 const char *fmt;
677 char c;
678 unsigned v, u, idx = 0;
679
680 if (ul > 99999) { // do not scale if 99999 or less
681 ul *= 10;
682 do {
683 ul /= 1024;
684 idx++;
685 } while (ul >= 100000);
686 }
687 v = ul; // ullong divisions are expensive, avoid them
688
689 fmt = " 123456789";
690 u = v / 10;
691 v = v % 10;
692 if (!idx) {
693 // 99999 or less: use "12345" format
694 // u is value/10, v is last digit
695 c = buf[0] = " 123456789"[u/1000];
696 if (c != ' ') fmt = "0123456789";
697 c = buf[1] = fmt[u/100%10];
698 if (c != ' ') fmt = "0123456789";
699 c = buf[2] = fmt[u/10%10];
700 if (c != ' ') fmt = "0123456789";
701 buf[3] = fmt[u%10];
702 buf[4] = "0123456789"[v];
703 } else {
704 // value has been scaled into 0..9999.9 range
705 // u is value, v is 1/10ths (allows for 92.1M format)
706 if (u >= 100) {
707 // value is >= 100: use "1234M', " 123M" formats
708 c = buf[0] = " 123456789"[u/1000];
709 if (c != ' ') fmt = "0123456789";
710 c = buf[1] = fmt[u/100%10];
711 if (c != ' ') fmt = "0123456789";
712 v = u % 10;
713 u = u / 10;
714 buf[2] = fmt[u%10];
715 } else {
716 // value is < 100: use "92.1M" format
717 c = buf[0] = " 123456789"[u/10];
718 if (c != ' ') fmt = "0123456789";
719 buf[1] = fmt[u%10];
720 buf[2] = '.';
721 }
722 buf[3] = "0123456789"[v];
723 // see http://en.wikipedia.org/wiki/Tera
724 buf[4] = " mgtpezy"[idx];
725 }
726 buf[5] = ' ';
727}
728
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000729static NOINLINE void display_topmem_process_list(int count, int scr_width)
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000730{
731#define HDR_STR " PID VSZ VSZRW RSS (SHR) DIRTY (SHR) STACK"
732#define MIN_WIDTH sizeof(HDR_STR)
733 const topmem_status_t *s = topmem;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000734
735 display_topmem_header(scr_width);
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000736 strcpy(line_buf, HDR_STR " COMMAND");
737 line_buf[5 + sort_field * 6] = '*';
738 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, line_buf);
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000739
740 while (--count >= 0) {
741 // PID VSZ VSZRW RSS (SHR) DIRTY (SHR) COMMAND
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000742 smart_ulltoa6(s->pid , &line_buf[0*6]);
743 smart_ulltoa6(s->vsz , &line_buf[1*6]);
744 smart_ulltoa6(s->vszrw , &line_buf[2*6]);
745 smart_ulltoa6(s->rss , &line_buf[3*6]);
746 smart_ulltoa6(s->rss_sh , &line_buf[4*6]);
747 smart_ulltoa6(s->dirty , &line_buf[5*6]);
748 smart_ulltoa6(s->dirty_sh, &line_buf[6*6]);
749 smart_ulltoa6(s->stack , &line_buf[7*6]);
750 line_buf[8*6] = '\0';
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000751 if (scr_width > MIN_WIDTH) {
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000752 read_cmdline(&line_buf[8*6], scr_width - MIN_WIDTH, s->pid, s->comm);
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000753 }
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000754 printf("\n""%.*s", scr_width, line_buf);
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000755 s++;
756 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000757 bb_putchar(OPT_BATCH_MODE ? '\n' : '\r');
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000758 fflush(stdout);
759#undef HDR_STR
760#undef MIN_WIDTH
761}
762#else
763void display_topmem_process_list(int count, int scr_width);
764int topmem_sort(char *a, char *b);
765#endif /* TOPMEM */
766
Denis Vlasenkoe7c1ad12007-09-08 17:21:01 +0000767/*
768 * end TOPMEM support
769 */
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000770
771enum {
772 TOP_MASK = 0
773 | PSSCAN_PID
774 | PSSCAN_PPID
775 | PSSCAN_VSZ
776 | PSSCAN_STIME
777 | PSSCAN_UTIME
778 | PSSCAN_STATE
779 | PSSCAN_COMM
780 | PSSCAN_UIDGID,
781 TOPMEM_MASK = 0
782 | PSSCAN_PID
783 | PSSCAN_SMAPS
784 | PSSCAN_COMM,
785};
786
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000787int top_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Eric Andersen420b2082002-09-17 22:14:58 +0000788int top_main(int argc, char **argv)
789{
Denis Vlasenko266bc172006-09-29 17:16:39 +0000790 int count, lines, col;
Denis Vlasenkob308d812007-08-28 19:35:34 +0000791 unsigned interval;
Denis Vlasenkoe7c1ad12007-09-08 17:21:01 +0000792 int iterations;
Denis Vlasenko266bc172006-09-29 17:16:39 +0000793 char *sinterval, *siterations;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000794 SKIP_FEATURE_TOPMEM(const) unsigned scan_mask = TOP_MASK;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000795#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000796 struct termios new_settings;
Denis Vlasenkob308d812007-08-28 19:35:34 +0000797 struct pollfd pfd[1];
Eric Andersen08a72202002-09-30 20:52:10 +0000798 unsigned char c;
Denis Vlasenkob308d812007-08-28 19:35:34 +0000799
800 pfd[0].fd = 0;
801 pfd[0].events = POLLIN;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000802#endif /* FEATURE_USE_TERMIOS */
Eric Andersen08a72202002-09-30 20:52:10 +0000803
Denis Vlasenkoe7c1ad12007-09-08 17:21:01 +0000804 INIT_G();
805
Denis Vlasenkob308d812007-08-28 19:35:34 +0000806 interval = 5; /* default update rate is 5 seconds */
Denis Vlasenkoe7c1ad12007-09-08 17:21:01 +0000807 iterations = 0; /* infinite */
Denis Vlasenko85818632007-04-19 14:47:11 +0000808
809 /* do normal option parsing */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000810 opt_complementary = "-";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000811 getopt32(argv, "d:n:b", &sinterval, &siterations);
Denis Vlasenkob308d812007-08-28 19:35:34 +0000812 if (option_mask32 & 0x1) {
813 /* Need to limit it to not overflow poll timeout */
814 interval = xatou16(sinterval); // -d
815 }
816 if (option_mask32 & 0x2)
817 iterations = xatoi_u(siterations); // -n
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000818 //if (option_mask32 & 0x4) // -b
Eric Andersen420b2082002-09-17 22:14:58 +0000819
Eric Andersen44608e92002-10-22 12:21:15 +0000820 /* change to /proc */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000821 xchdir("/proc");
Denis Vlasenkofa076802006-11-05 00:38:51 +0000822#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000823 tcgetattr(0, (void *) &initial_settings);
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000824 memcpy(&new_settings, &initial_settings, sizeof(new_settings));
Denis Vlasenko42dfcd22006-09-09 12:55:02 +0000825 /* unbuffered input, turn off echo */
826 new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
Eric Andersen08a72202002-09-30 20:52:10 +0000827
Rob Landley997650b2006-04-24 23:13:46 +0000828 signal(SIGTERM, sig_catcher);
Rob Landleydb1ab1a2006-06-28 14:11:25 +0000829 signal(SIGINT, sig_catcher);
Eric Andersen08a72202002-09-30 20:52:10 +0000830 tcsetattr(0, TCSANOW, (void *) &new_settings);
831 atexit(reset_term);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000832#endif /* FEATURE_USE_TERMIOS */
Mike Frysinger223b8872005-07-30 09:42:05 +0000833
Denis Vlasenkofa076802006-11-05 00:38:51 +0000834#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000835 sort_function[0] = pcpu_sort;
836 sort_function[1] = mem_sort;
837 sort_function[2] = time_sort;
838#else
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000839 sort_function[0] = mem_sort;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000840#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Mike Frysinger223b8872005-07-30 09:42:05 +0000841
Eric Andersen08a72202002-09-30 20:52:10 +0000842 while (1) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000843 procps_status_t *p = NULL;
Eric Andersen44608e92002-10-22 12:21:15 +0000844
Denis Vlasenkoe7c1ad12007-09-08 17:21:01 +0000845 lines = 24; /* default */
Rob Landley997650b2006-04-24 23:13:46 +0000846 col = 79;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000847#if ENABLE_FEATURE_USE_TERMIOS
Rob Landley997650b2006-04-24 23:13:46 +0000848 get_terminal_width_height(0, &col, &lines);
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000849 if (lines < 5 || col < 10) {
Rob Landley997650b2006-04-24 23:13:46 +0000850 sleep(interval);
851 continue;
852 }
Denis Vlasenkofa076802006-11-05 00:38:51 +0000853#endif /* FEATURE_USE_TERMIOS */
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000854 if (col > LINE_BUF_SIZE-2) /* +2 bytes for '\n', NUL, */
855 col = LINE_BUF_SIZE-2;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000856 if (!ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && scan_mask == TOP_MASK)
857 lines -= 3;
858 else
859 lines -= 4;
Rob Landley997650b2006-04-24 23:13:46 +0000860
861 /* read process IDs & status for all the processes */
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000862 while ((p = procps_scan(p, scan_mask)) != NULL) {
863 int n;
864 if (scan_mask == TOP_MASK) {
865 n = ntop;
866 top = xrealloc(top, (++ntop) * sizeof(*top));
867 top[n].pid = p->pid;
868 top[n].ppid = p->ppid;
869 top[n].vsz = p->vsz;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000870#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000871 top[n].ticks = p->stime + p->utime;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000872#endif
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000873 top[n].uid = p->uid;
874 strcpy(top[n].state, p->state);
875 strcpy(top[n].comm, p->comm);
876 } else { /* TOPMEM */
877#if ENABLE_FEATURE_TOPMEM
878 if (!(p->mapped_ro | p->mapped_rw))
879 continue; /* kernel threads are ignored */
880 n = ntop;
881 top = xrealloc(topmem, (++ntop) * sizeof(*topmem));
882 strcpy(topmem[n].comm, p->comm);
883 topmem[n].pid = p->pid;
884 topmem[n].vsz = p->mapped_rw + p->mapped_ro;
885 topmem[n].vszrw = p->mapped_rw;
886 topmem[n].rss_sh = p->shared_clean + p->shared_dirty;
887 topmem[n].rss = p->private_clean + p->private_dirty + topmem[n].rss_sh;
888 topmem[n].dirty = p->private_dirty + p->shared_dirty;
889 topmem[n].dirty_sh = p->shared_dirty;
890 topmem[n].stack = p->stack;
891#endif
892 }
Eric Andersen44608e92002-10-22 12:21:15 +0000893 }
894 if (ntop == 0) {
Denis Vlasenko74511962007-06-11 16:31:55 +0000895 bb_error_msg_and_die("no process info in /proc");
Rob Landleyb2804552006-02-13 22:04:27 +0000896 }
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000897
898 if (scan_mask == TOP_MASK) {
Denis Vlasenkofa076802006-11-05 00:38:51 +0000899#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000900 if (!prev_hist_count) {
901 do_stats();
902 usleep(100000);
903 clearmems();
904 continue;
905 }
Eric Andersen08a72202002-09-30 20:52:10 +0000906 do_stats();
Denis Vlasenkof7d07b12007-06-30 08:03:26 +0000907/* TODO: we don't need to sort all 10000 processes, we need to find top 24! */
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000908 qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
Eric Andersen08a72202002-09-30 20:52:10 +0000909#else
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000910 qsort(top, ntop, sizeof(top_status_t), (void*)(sort_function[0]));
Denis Vlasenkofa076802006-11-05 00:38:51 +0000911#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000912 } else { /* TOPMEM */
913 qsort(topmem, ntop, sizeof(topmem_status_t), (void*)topmem_sort);
914 }
Denis Vlasenko266bc172006-09-29 17:16:39 +0000915 count = lines;
Denis Vlasenko25d80622006-10-27 09:34:22 +0000916 if (OPT_BATCH_MODE || count > ntop) {
Denis Vlasenko266bc172006-09-29 17:16:39 +0000917 count = ntop;
Eric Andersen08a72202002-09-30 20:52:10 +0000918 }
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000919 if (scan_mask == TOP_MASK)
920 display_process_list(count, col);
921 else
922 display_topmem_process_list(count, col);
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000923 clearmems();
924 if (iterations >= 0 && !--iterations)
925 break;
926#if !ENABLE_FEATURE_USE_TERMIOS
927 sleep(interval);
928#else
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000929 if (safe_poll(pfd, 1, interval * 1000) > 0) {
Denis Vlasenkob308d812007-08-28 19:35:34 +0000930 if (read(0, &c, 1) != 1) /* signal */
931 break;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000932 if (c == initial_settings.c_cc[VINTR])
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000933 break;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000934 c |= 0x20; /* lowercase */
935 if (c == 'q')
936 break;
937 if (c == 'n') {
938 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000939 sort_function[0] = pid_sort;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000940 }
941 if (c == 'm') {
942 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
Eric Andersen08a72202002-09-30 20:52:10 +0000943 sort_function[0] = mem_sort;
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000944#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000945 sort_function[1] = pcpu_sort;
946 sort_function[2] = time_sort;
Eric Andersen08a72202002-09-30 20:52:10 +0000947#endif
948 }
Denis Vlasenkofa076802006-11-05 00:38:51 +0000949#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000950 if (c == 'p') {
951 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
Eric Andersen08a72202002-09-30 20:52:10 +0000952 sort_function[0] = pcpu_sort;
953 sort_function[1] = mem_sort;
954 sort_function[2] = time_sort;
955 }
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000956 if (c == 't') {
957 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
Eric Andersen08a72202002-09-30 20:52:10 +0000958 sort_function[0] = time_sort;
959 sort_function[1] = mem_sort;
960 sort_function[2] = pcpu_sort;
961 }
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000962#if ENABLE_FEATURE_TOPMEM
963 if (c == 's') {
964 scan_mask = TOPMEM_MASK;
965 free(prev_hist);
966 prev_hist = NULL;
967 prev_hist_count = 0;
968 sort_field = (sort_field + 1) % NUM_SORT_FIELD;
969 }
970 if (c == 'r')
971 inverted ^= 1;
972#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000973#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000974 }
Denis Vlasenkofa076802006-11-05 00:38:51 +0000975#endif /* FEATURE_USE_TERMIOS */
Eric Andersen420b2082002-09-17 22:14:58 +0000976 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000977 bb_putchar('\n');
Eric Andersen420b2082002-09-17 22:14:58 +0000978 return EXIT_SUCCESS;
979}