blob: ca43376acc520a8f5ec777b5003dcc109ed4e87c [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;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +000056 pid_t 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 Vlasenkocf7cf622008-03-19 19:38:46 +0000111enum {
112 OPT_d = (1 << 0),
113 OPT_n = (1 << 1),
114 OPT_b = (1 << 2),
115 OPT_EOF = (1 << 3), /* pseudo: "we saw EOF in stdin" */
116};
117#define OPT_BATCH_MODE (option_mask32 & OPT_b)
Eric Andersen08a72202002-09-30 20:52:10 +0000118
Denis Vlasenko85818632007-04-19 14:47:11 +0000119
Denis Vlasenkofa076802006-11-05 00:38:51 +0000120#if ENABLE_FEATURE_USE_TERMIOS
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000121static int pid_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +0000122{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000123 /* Buggy wrt pids with high bit set */
124 /* (linux pids are in [1..2^15-1]) */
Rob Landleyb2804552006-02-13 22:04:27 +0000125 return (Q->pid - P->pid);
Eric Andersen08a72202002-09-30 20:52:10 +0000126}
Mike Frysinger223b8872005-07-30 09:42:05 +0000127#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000128
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000129static int mem_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +0000130{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000131 /* We want to avoid unsigned->signed and truncation errors */
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000132 if (Q->vsz < P->vsz) return -1;
133 return Q->vsz != P->vsz; /* 0 if ==, 1 if > */
Eric Andersen08a72202002-09-30 20:52:10 +0000134}
135
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000136
Denis Vlasenko85818632007-04-19 14:47:11 +0000137#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000138
139static int pcpu_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +0000140{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000141 /* Buggy wrt ticks with high bit set */
142 /* Affects only processes for which ticks overflow */
143 return (int)Q->pcpu - (int)P->pcpu;
Eric Andersen08a72202002-09-30 20:52:10 +0000144}
145
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000146static int time_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +0000147{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000148 /* We want to avoid unsigned->signed and truncation errors */
149 if (Q->ticks < P->ticks) return -1;
150 return Q->ticks != P->ticks; /* 0 if ==, 1 if > */
Eric Andersen08a72202002-09-30 20:52:10 +0000151}
152
Denis Vlasenkoac678ec2007-04-16 22:32:04 +0000153static int mult_lvl_cmp(void* a, void* b)
154{
Rob Landleyb2804552006-02-13 22:04:27 +0000155 int i, cmp_val;
Eric Andersen08a72202002-09-30 20:52:10 +0000156
Denis Vlasenkofa076802006-11-05 00:38:51 +0000157 for (i = 0; i < SORT_DEPTH; i++) {
Rob Landleyb2804552006-02-13 22:04:27 +0000158 cmp_val = (*sort_function[i])(a, b);
159 if (cmp_val != 0)
160 return cmp_val;
161 }
162 return 0;
Eric Andersen08a72202002-09-30 20:52:10 +0000163}
164
Eric Andersen08a72202002-09-30 20:52:10 +0000165
Rob Landley997650b2006-04-24 23:13:46 +0000166static void get_jiffy_counts(void)
Rob Landleyb2804552006-02-13 22:04:27 +0000167{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000168 FILE* fp = xfopen("stat", "r");
Rob Landley997650b2006-04-24 23:13:46 +0000169 prev_jif = jif;
170 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
171 &jif.usr,&jif.nic,&jif.sys,&jif.idle,
172 &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000173 bb_error_msg_and_die("can't read /proc/stat");
Rob Landleyb2804552006-02-13 22:04:27 +0000174 }
Rob Landley997650b2006-04-24 23:13:46 +0000175 fclose(fp);
176 jif.total = jif.usr + jif.nic + jif.sys + jif.idle
177 + jif.iowait + jif.irq + jif.softirq + jif.steal;
178 /* procps 2.x does not count iowait as busy time */
179 jif.busy = jif.total - jif.idle - jif.iowait;
Eric Andersen08a72202002-09-30 20:52:10 +0000180}
181
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000182
Eric Andersen08a72202002-09-30 20:52:10 +0000183static void do_stats(void)
184{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000185 top_status_t *cur;
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000186 pid_t pid;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000187 int i, last_i, n;
Rob Landley997650b2006-04-24 23:13:46 +0000188 struct save_hist *new_hist;
Eric Andersen08a72202002-09-30 20:52:10 +0000189
Rob Landley997650b2006-04-24 23:13:46 +0000190 get_jiffy_counts();
191 total_pcpu = 0;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000192 /* total_vsz = 0; */
Rob Landley997650b2006-04-24 23:13:46 +0000193 new_hist = xmalloc(sizeof(struct save_hist)*ntop);
Rob Landleyb2804552006-02-13 22:04:27 +0000194 /*
195 * Make a pass through the data to get stats.
196 */
Rob Landley997650b2006-04-24 23:13:46 +0000197 /* hist_iterations = 0; */
198 i = 0;
199 for (n = 0; n < ntop; n++) {
Rob Landleyb2804552006-02-13 22:04:27 +0000200 cur = top + n;
201
202 /*
203 * Calculate time in cur process. Time is sum of user time
Rob Landley997650b2006-04-24 23:13:46 +0000204 * and system time
Rob Landleyb2804552006-02-13 22:04:27 +0000205 */
Rob Landleyb2804552006-02-13 22:04:27 +0000206 pid = cur->pid;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000207 new_hist[n].ticks = cur->ticks;
Rob Landley997650b2006-04-24 23:13:46 +0000208 new_hist[n].pid = pid;
Rob Landleyb2804552006-02-13 22:04:27 +0000209
210 /* find matching entry from previous pass */
Rob Landley997650b2006-04-24 23:13:46 +0000211 cur->pcpu = 0;
212 /* do not start at index 0, continue at last used one
213 * (brought hist_iterations from ~14000 down to 172) */
214 last_i = i;
215 if (prev_hist_count) do {
216 if (prev_hist[i].pid == pid) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000217 cur->pcpu = cur->ticks - prev_hist[i].ticks;
218 total_pcpu += cur->pcpu;
Rob Landleyb2804552006-02-13 22:04:27 +0000219 break;
220 }
Rob Landley997650b2006-04-24 23:13:46 +0000221 i = (i+1) % prev_hist_count;
222 /* hist_iterations++; */
223 } while (i != last_i);
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000224 /* total_vsz += cur->vsz; */
Eric Andersen08a72202002-09-30 20:52:10 +0000225 }
226
227 /*
Rob Landleyb2804552006-02-13 22:04:27 +0000228 * Save cur frame's information.
Eric Andersen08a72202002-09-30 20:52:10 +0000229 */
Rob Landley997650b2006-04-24 23:13:46 +0000230 free(prev_hist);
231 prev_hist = new_hist;
232 prev_hist_count = ntop;
Eric Andersen08a72202002-09-30 20:52:10 +0000233}
Denis Vlasenkofa076802006-11-05 00:38:51 +0000234#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Eric Andersen08a72202002-09-30 20:52:10 +0000235
Denis Vlasenko6ee023c2007-08-23 10:52:52 +0000236#if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && ENABLE_FEATURE_TOP_DECIMALS
237/* formats 7 char string (8 with terminating NUL) */
238static char *fmt_100percent_8(char pbuf[8], unsigned value, unsigned total)
239{
240 unsigned t;
241 if (value >= total) { /* 100% ? */
242 strcpy(pbuf, " 100% ");
243 return pbuf;
244 }
245 /* else generate " [N/space]N.N% " string */
246 value = 1000 * value / total;
247 t = value / 100;
248 value = value % 100;
249 pbuf[0] = ' ';
250 pbuf[1] = t ? t + '0' : ' ';
251 pbuf[2] = '0' + (value / 10);
252 pbuf[3] = '.';
253 pbuf[4] = '0' + (value % 10);
254 pbuf[5] = '%';
255 pbuf[6] = ' ';
256 pbuf[7] = '\0';
257 return pbuf;
258}
259#endif
260
Denis Vlasenko05241802007-08-29 18:34:26 +0000261static unsigned long display_header(int scr_width)
Eric Andersen420b2082002-09-17 22:14:58 +0000262{
263 FILE *fp;
264 char buf[80];
Rob Landley997650b2006-04-24 23:13:46 +0000265 char scrbuf[80];
Eric Andersen420b2082002-09-17 22:14:58 +0000266 unsigned long total, used, mfree, shared, buffers, cached;
Denis Vlasenko6ee023c2007-08-23 10:52:52 +0000267#if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
Denis Vlasenko110967a2007-07-15 19:27:48 +0000268 unsigned total_diff;
Denis Vlasenko856be772007-08-17 08:29:48 +0000269#endif
Denis Vlasenko110967a2007-07-15 19:27:48 +0000270
Eric Andersen420b2082002-09-17 22:14:58 +0000271 /* read memory info */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000272 fp = xfopen("meminfo", "r");
Eric Andersen420b2082002-09-17 22:14:58 +0000273
Eric Andersen7857c032003-10-11 18:47:20 +0000274 /*
275 * Old kernels (such as 2.4.x) had a nice summary of memory info that
276 * we could parse, however this is gone entirely in 2.6. Try parsing
277 * the old way first, and if that fails, parse each field manually.
278 *
279 * First, we read in the first line. Old kernels will have bogus
280 * strings we don't care about, whereas new kernels will start right
281 * out with MemTotal:
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000282 * -- PFM.
Eric Andersen7857c032003-10-11 18:47:20 +0000283 */
284 if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000285 fgets(buf, sizeof(buf), fp); /* skip first line */
Eric Andersen7857c032003-10-11 18:47:20 +0000286
287 fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
Denis Vlasenko5a654472007-06-10 17:11:59 +0000288 &total, &used, &mfree, &shared, &buffers, &cached);
289 /* convert to kilobytes */
290 used /= 1024;
291 mfree /= 1024;
292 shared /= 1024;
293 buffers /= 1024;
294 cached /= 1024;
295 total /= 1024;
Eric Andersen7857c032003-10-11 18:47:20 +0000296 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000297 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000298 * Revert to manual parsing, which incidentally already has the
299 * sizes in kilobytes. This should be safe for both 2.4 and
300 * 2.6.
301 */
Eric Andersen7857c032003-10-11 18:47:20 +0000302
303 fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
304
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000305 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000306 * MemShared: is no longer present in 2.6. Report this as 0,
307 * to maintain consistent behavior with normal procps.
308 */
309 if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
310 shared = 0;
311
312 fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
313 fscanf(fp, "Cached: %lu %s\n", &cached, buf);
314
315 used = total - mfree;
Eric Andersen420b2082002-09-17 22:14:58 +0000316 }
317 fclose(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000318
Denis Vlasenko110967a2007-07-15 19:27:48 +0000319 /* output memory info */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000320 if (scr_width > (int)sizeof(scrbuf))
Rob Landley997650b2006-04-24 23:13:46 +0000321 scr_width = sizeof(scrbuf);
322 snprintf(scrbuf, scr_width,
Denis Vlasenkoc1166c32007-07-15 19:23:38 +0000323 "Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached",
Rob Landleyb2804552006-02-13 22:04:27 +0000324 used, mfree, shared, buffers, cached);
Denis Vlasenko110967a2007-07-15 19:27:48 +0000325 /* clear screen & go to top */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000326 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000327
Denis Vlasenko856be772007-08-17 08:29:48 +0000328#if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
329 /*
330 * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
331 */
332 /* using (unsigned) casts to make operations cheaper */
333 total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1);
Denis Vlasenko74511962007-06-11 16:31:55 +0000334#if ENABLE_FEATURE_TOP_DECIMALS
Denis Vlasenko110967a2007-07-15 19:27:48 +0000335/* Generated code is approx +0.3k */
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000336#define CALC_STAT(xxx) char xxx[8]
Denis Vlasenko6ee023c2007-08-23 10:52:52 +0000337#define SHOW_STAT(xxx) fmt_100percent_8(xxx, (unsigned)(jif.xxx - prev_jif.xxx), total_diff)
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000338#define FMT "%s"
Denis Vlasenko74511962007-06-11 16:31:55 +0000339#else
Denis Vlasenkob1e5add2007-06-10 18:04:54 +0000340#define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
341#define SHOW_STAT(xxx) xxx
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000342#define FMT "%4u%% "
Denis Vlasenko74511962007-06-11 16:31:55 +0000343#endif
Denis Vlasenko856be772007-08-17 08:29:48 +0000344 { /* need block: CALC_STAT are declarations */
345 CALC_STAT(usr);
346 CALC_STAT(sys);
347 CALC_STAT(nic);
348 CALC_STAT(idle);
349 CALC_STAT(iowait);
350 CALC_STAT(irq);
351 CALC_STAT(softirq);
352 //CALC_STAT(steal);
Denis Vlasenko5a654472007-06-10 17:11:59 +0000353
Denis Vlasenko856be772007-08-17 08:29:48 +0000354 snprintf(scrbuf, scr_width,
355 /* Barely fits in 79 chars when in "decimals" mode. */
356 "CPU:"FMT"usr"FMT"sys"FMT"nice"FMT"idle"FMT"io"FMT"irq"FMT"softirq",
357 SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
358 SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
359 //, SHOW_STAT(steal) - what is this 'steal' thing?
360 // I doubt anyone wants to know it
361 );
362 }
363 puts(scrbuf);
Denis Vlasenko5a654472007-06-10 17:11:59 +0000364#undef SHOW_STAT
365#undef CALC_STAT
Denis Vlasenkob1e5add2007-06-10 18:04:54 +0000366#undef FMT
Denis Vlasenko856be772007-08-17 08:29:48 +0000367#endif
Denis Vlasenko5a654472007-06-10 17:11:59 +0000368
Denis Vlasenko110967a2007-07-15 19:27:48 +0000369 /* read load average as a string */
370 buf[0] = '\0';
371 open_read_close("loadavg", buf, sizeof("N.NN N.NN N.NN")-1);
372 buf[sizeof("N.NN N.NN N.NN")-1] = '\0';
Denis Vlasenko25d80622006-10-27 09:34:22 +0000373 snprintf(scrbuf, scr_width, "Load average: %s", buf);
Denis Vlasenko5a654472007-06-10 17:11:59 +0000374 puts(scrbuf);
Rob Landley997650b2006-04-24 23:13:46 +0000375
Eric Andersen7857c032003-10-11 18:47:20 +0000376 return total;
Eric Andersen420b2082002-09-17 22:14:58 +0000377}
378
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000379static NOINLINE void display_process_list(int count, int scr_width)
Eric Andersen420b2082002-09-17 22:14:58 +0000380{
Rob Landley997650b2006-04-24 23:13:46 +0000381 enum {
Denis Vlasenko74511962007-06-11 16:31:55 +0000382 BITS_PER_INT = sizeof(int)*8
Rob Landley997650b2006-04-24 23:13:46 +0000383 };
384
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000385 top_status_t *s = top;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000386 char vsz_str_buf[8];
Denis Vlasenko05241802007-08-29 18:34:26 +0000387 unsigned long total_memory = display_header(scr_width); /* or use total_vsz? */
Denis Vlasenko74511962007-06-11 16:31:55 +0000388 /* xxx_shift and xxx_scale variables allow us to replace
389 * expensive divides with multiply and shift */
390 unsigned pmem_shift, pmem_scale, pmem_half;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000391#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenko74511962007-06-11 16:31:55 +0000392 unsigned pcpu_shift, pcpu_scale, pcpu_half;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000393 unsigned busy_jifs;
Rob Landley997650b2006-04-24 23:13:46 +0000394
Eric Andersen420b2082002-09-17 22:14:58 +0000395 /* what info of the processes is shown */
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 %CPU COMMAND");
Eric Andersen08a72202002-09-30 20:52:10 +0000398#else
Denis Vlasenko74511962007-06-11 16:31:55 +0000399
400 /* !CPU_USAGE_PERCENTAGE */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000401 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
Denis Vlasenko74511962007-06-11 16:31:55 +0000402 " PID PPID USER STAT VSZ %MEM COMMAND");
Rob Landley997650b2006-04-24 23:13:46 +0000403#endif
404
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000405#if ENABLE_FEATURE_TOP_DECIMALS
406#define UPSCALE 1000
407#define CALC_STAT(name, val) div_t name = div((val), 10)
408#define SHOW_STAT(name) name.quot, '0'+name.rem
409#define FMT "%3u.%c"
410#else
411#define UPSCALE 100
412#define CALC_STAT(name, val) unsigned name = (val)
413#define SHOW_STAT(name) name
414#define FMT "%4u%%"
415#endif
Rob Landley997650b2006-04-24 23:13:46 +0000416 /*
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000417 * MEM% = s->vsz/MemTotal
Rob Landley997650b2006-04-24 23:13:46 +0000418 */
Denis Vlasenko74511962007-06-11 16:31:55 +0000419 pmem_shift = BITS_PER_INT-11;
420 pmem_scale = UPSCALE*(1U<<(BITS_PER_INT-11)) / total_memory;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000421 /* s->vsz is in kb. we want (s->vsz * pmem_scale) to never overflow */
Rob Landley997650b2006-04-24 23:13:46 +0000422 while (pmem_scale >= 512) {
423 pmem_scale /= 4;
424 pmem_shift -= 2;
425 }
Denis Vlasenko74511962007-06-11 16:31:55 +0000426 pmem_half = (1U << pmem_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000427#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
428 busy_jifs = jif.busy - prev_jif.busy;
429 /* This happens if there were lots of short-lived processes
430 * between two top updates (e.g. compilation) */
431 if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
432
Rob Landley997650b2006-04-24 23:13:46 +0000433 /*
434 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
435 * (pcpu is delta of sys+user time between samples)
436 */
437 /* (jif.xxx - prev_jif.xxx) and s->pcpu are
438 * in 0..~64000 range (HZ*update_interval).
439 * we assume that unsigned is at least 32-bit.
440 */
441 pcpu_shift = 6;
Denis Vlasenko74511962007-06-11 16:31:55 +0000442 pcpu_scale = (UPSCALE*64*(uint16_t)busy_jifs ? : 1);
443 while (pcpu_scale < (1U<<(BITS_PER_INT-2))) {
Rob Landley997650b2006-04-24 23:13:46 +0000444 pcpu_scale *= 4;
445 pcpu_shift += 2;
446 }
447 pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1);
448 /* we want (s->pcpu * pcpu_scale) to never overflow */
449 while (pcpu_scale >= 1024) {
450 pcpu_scale /= 4;
451 pcpu_shift -= 2;
452 }
Denis Vlasenko74511962007-06-11 16:31:55 +0000453 pcpu_half = (1U << pcpu_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
Rob Landley997650b2006-04-24 23:13:46 +0000454 /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
Eric Andersen08a72202002-09-30 20:52:10 +0000455#endif
Denis Vlasenko74511962007-06-11 16:31:55 +0000456
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000457 scr_width += 2; /* account for leading '\n' and trailing NUL */
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +0000458 /* Ok, all preliminary data is ready, go through the list */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000459 while (count-- > 0) {
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000460 unsigned col;
Denis Vlasenko74511962007-06-11 16:31:55 +0000461 CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift);
462#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
463 CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift);
464#endif
Eric Andersen420b2082002-09-17 22:14:58 +0000465
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000466 if (s->vsz >= 100000)
Denis Vlasenkob308d812007-08-28 19:35:34 +0000467 sprintf(vsz_str_buf, "%6ldm", s->vsz/1024);
Manuel Novoa III d4993302002-09-18 19:27:10 +0000468 else
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000469 sprintf(vsz_str_buf, "%7ld", s->vsz);
Denis Vlasenko74511962007-06-11 16:31:55 +0000470 // PID PPID USER STAT VSZ %MEM [%CPU] COMMAND
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000471 col = snprintf(line_buf, scr_width,
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000472 "\n" "%5u%6u %-8.8s %s%s" FMT
Denis Vlasenko74511962007-06-11 16:31:55 +0000473#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
474 FMT
475#endif
476 " ",
477 s->pid, s->ppid, get_cached_username(s->uid),
478 s->state, vsz_str_buf,
479 SHOW_STAT(pmem)
480#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
481 , SHOW_STAT(pcpu)
482#endif
483 );
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000484 if ((int)(col + 1) < scr_width)
Denis Vlasenko4d7605a2007-09-08 17:42:00 +0000485 read_cmdline(line_buf + col, scr_width - col - 1, s->pid, s->comm);
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000486 fputs(line_buf, stdout);
Bernhard Reutner-Fischere2922e42006-05-19 12:48:56 +0000487 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
Rob Landley997650b2006-04-24 23:13:46 +0000488 jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
Eric Andersen420b2082002-09-17 22:14:58 +0000489 s++;
Eric Andersen08a72202002-09-30 20:52:10 +0000490 }
Rob Landley997650b2006-04-24 23:13:46 +0000491 /* printf(" %d", hist_iterations); */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000492 bb_putchar(OPT_BATCH_MODE ? '\n' : '\r');
Rob Landley997650b2006-04-24 23:13:46 +0000493 fflush(stdout);
Eric Andersen44608e92002-10-22 12:21:15 +0000494}
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000495#undef UPSCALE
Denis Vlasenko74511962007-06-11 16:31:55 +0000496#undef SHOW_STAT
497#undef CALC_STAT
498#undef FMT
Eric Andersen44608e92002-10-22 12:21:15 +0000499
500static void clearmems(void)
501{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000502 clear_username_cache();
Eric Andersen08a72202002-09-30 20:52:10 +0000503 free(top);
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000504 top = NULL;
Eric Andersen08a72202002-09-30 20:52:10 +0000505 ntop = 0;
506}
507
Denis Vlasenkofa076802006-11-05 00:38:51 +0000508#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000509#include <termios.h>
Eric Andersen08a72202002-09-30 20:52:10 +0000510#include <signal.h>
511
Eric Andersen08a72202002-09-30 20:52:10 +0000512static void reset_term(void)
513{
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000514 tcsetattr(0, TCSANOW, &initial_settings);
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000515 if (ENABLE_FEATURE_CLEAN_UP) {
516 clearmems();
Denis Vlasenkofa076802006-11-05 00:38:51 +0000517#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000518 free(prev_hist);
Eric Andersen08a72202002-09-30 20:52:10 +0000519#endif
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000520 }
Eric Andersen08a72202002-09-30 20:52:10 +0000521}
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000522
Rob Landleyb2804552006-02-13 22:04:27 +0000523static void sig_catcher(int sig ATTRIBUTE_UNUSED)
Eric Andersen08a72202002-09-30 20:52:10 +0000524{
525 reset_term();
Rob Landleydb1ab1a2006-06-28 14:11:25 +0000526 exit(1);
Eric Andersen08a72202002-09-30 20:52:10 +0000527}
Denis Vlasenkofa076802006-11-05 00:38:51 +0000528#endif /* FEATURE_USE_TERMIOS */
Eric Andersen08a72202002-09-30 20:52:10 +0000529
Denis Vlasenkoe7c1ad12007-09-08 17:21:01 +0000530/*
531 * TOPMEM support
532 */
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000533
534typedef unsigned long mem_t;
535
536typedef struct topmem_status_t {
537 unsigned pid;
538 char comm[COMM_LEN];
539 /* vsz doesn't count /dev/xxx mappings except /dev/zero */
540 mem_t vsz ;
541 mem_t vszrw ;
542 mem_t rss ;
543 mem_t rss_sh ;
544 mem_t dirty ;
545 mem_t dirty_sh;
546 mem_t stack ;
547} topmem_status_t;
548
549enum { NUM_SORT_FIELD = 7 };
550
551#define topmem ((topmem_status_t*)top)
552
553#if ENABLE_FEATURE_TOPMEM
554static int topmem_sort(char *a, char *b)
555{
556 int n;
557 mem_t l, r;
558
559 n = offsetof(topmem_status_t, vsz) + (sort_field * sizeof(mem_t));
560 l = *(mem_t*)(a + n);
561 r = *(mem_t*)(b + n);
562// if (l == r) {
563// l = a->mapped_rw;
564// r = b->mapped_rw;
565// }
566 /* We want to avoid unsigned->signed and truncation errors */
567 /* l>r: -1, l=r: 0, l<r: 1 */
568 n = (l > r) ? -1 : (l != r);
569 return inverted ? -n : n;
570}
571
572/* Cut "NNNN " out of " NNNN kb" */
573static char *grab_number(char *str, const char *match, unsigned sz)
574{
575 if (strncmp(str, match, sz) == 0) {
576 str = skip_whitespace(str + sz);
577 (skip_non_whitespace(str))[1] = '\0';
578 return xstrdup(str);
579 }
580 return NULL;
581}
582
583/* display header info (meminfo / loadavg) */
584static void display_topmem_header(int scr_width)
585{
586 char linebuf[128];
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000587 unsigned i;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000588 FILE *fp;
589 union {
590 struct {
591 /* 1 */ char *total;
592 /* 2 */ char *mfree;
593 /* 3 */ char *buf;
594 /* 4 */ char *cache;
595 /* 5 */ char *swaptotal;
596 /* 6 */ char *swapfree;
597 /* 7 */ char *dirty;
598 /* 8 */ char *mwrite;
599 /* 9 */ char *anon;
600 /* 10 */ char *map;
601 /* 11 */ char *slab;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000602 } u;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000603 char *str[11];
604 } Z;
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000605#define total Z.u.total
606#define mfree Z.u.mfree
607#define buf Z.u.buf
608#define cache Z.u.cache
609#define swaptotal Z.u.swaptotal
610#define swapfree Z.u.swapfree
611#define dirty Z.u.dirty
612#define mwrite Z.u.mwrite
613#define anon Z.u.anon
614#define map Z.u.map
615#define slab Z.u.slab
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000616#define str Z.str
617
618 memset(&Z, 0, sizeof(Z));
619
620 /* read memory info */
621 fp = xfopen("meminfo", "r");
622 while (fgets(linebuf, sizeof(linebuf), fp)) {
623 char *p;
624
625#define SCAN(match, name) \
626 p = grab_number(linebuf, match, sizeof(match)-1); \
627 if (p) { name = p; continue; }
628
629 SCAN("MemTotal:", total);
630 SCAN("MemFree:", mfree);
631 SCAN("Buffers:", buf);
632 SCAN("Cached:", cache);
633 SCAN("SwapTotal:", swaptotal);
634 SCAN("SwapFree:", swapfree);
635 SCAN("Dirty:", dirty);
636 SCAN("Writeback:", mwrite);
637 SCAN("AnonPages:", anon);
638 SCAN("Mapped:", map);
639 SCAN("Slab:", slab);
640#undef SCAN
641 }
642 fclose(fp);
643
Denis Vlasenkofcfb5c02007-12-24 12:16:24 +0000644#define S(s) (s ? s : "0 ")
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000645 snprintf(linebuf, sizeof(linebuf),
646 "Mem %stotal %sanon %smap %sfree",
647 S(total), S(anon), S(map), S(mfree));
648 printf(OPT_BATCH_MODE ? "%.*s\n" : "\e[H\e[J%.*s\n", scr_width, linebuf);
649
650 snprintf(linebuf, sizeof(linebuf),
651 " %sslab %sbuf %scache %sdirty %swrite",
652 S(slab), S(buf), S(cache), S(dirty), S(mwrite));
653 printf("%.*s\n", scr_width, linebuf);
654
655 snprintf(linebuf, sizeof(linebuf),
656 "Swap %stotal %sfree", // TODO: % used?
657 S(swaptotal), S(swapfree));
658 printf("%.*s\n", scr_width, linebuf);
659#undef S
660
661 for (i = 0; i < ARRAY_SIZE(str); i++)
662 free(str[i]);
663#undef total
664#undef free
665#undef buf
666#undef cache
667#undef swaptotal
668#undef swapfree
669#undef dirty
670#undef write
671#undef anon
672#undef map
673#undef slab
674#undef str
675}
676
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000677static void ulltoa6_and_space(unsigned long long ul, char buf[6])
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000678{
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000679 /* see http://en.wikipedia.org/wiki/Tera */
680 smart_ulltoa5(ul, buf, " mgtpezy");
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000681 buf[5] = ' ';
682}
683
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000684static NOINLINE void display_topmem_process_list(int count, int scr_width)
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000685{
686#define HDR_STR " PID VSZ VSZRW RSS (SHR) DIRTY (SHR) STACK"
687#define MIN_WIDTH sizeof(HDR_STR)
688 const topmem_status_t *s = topmem;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000689
690 display_topmem_header(scr_width);
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000691 strcpy(line_buf, HDR_STR " COMMAND");
692 line_buf[5 + sort_field * 6] = '*';
693 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, line_buf);
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000694
695 while (--count >= 0) {
696 // PID VSZ VSZRW RSS (SHR) DIRTY (SHR) COMMAND
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000697 ulltoa6_and_space(s->pid , &line_buf[0*6]);
698 ulltoa6_and_space(s->vsz , &line_buf[1*6]);
699 ulltoa6_and_space(s->vszrw , &line_buf[2*6]);
700 ulltoa6_and_space(s->rss , &line_buf[3*6]);
701 ulltoa6_and_space(s->rss_sh , &line_buf[4*6]);
702 ulltoa6_and_space(s->dirty , &line_buf[5*6]);
703 ulltoa6_and_space(s->dirty_sh, &line_buf[6*6]);
704 ulltoa6_and_space(s->stack , &line_buf[7*6]);
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000705 line_buf[8*6] = '\0';
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000706 if (scr_width > (int)MIN_WIDTH) {
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000707 read_cmdline(&line_buf[8*6], scr_width - MIN_WIDTH, s->pid, s->comm);
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000708 }
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000709 printf("\n""%.*s", scr_width, line_buf);
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000710 s++;
711 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000712 bb_putchar(OPT_BATCH_MODE ? '\n' : '\r');
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000713 fflush(stdout);
714#undef HDR_STR
715#undef MIN_WIDTH
716}
717#else
718void display_topmem_process_list(int count, int scr_width);
719int topmem_sort(char *a, char *b);
720#endif /* TOPMEM */
721
Denis Vlasenkoe7c1ad12007-09-08 17:21:01 +0000722/*
723 * end TOPMEM support
724 */
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000725
726enum {
727 TOP_MASK = 0
728 | PSSCAN_PID
729 | PSSCAN_PPID
730 | PSSCAN_VSZ
731 | PSSCAN_STIME
732 | PSSCAN_UTIME
733 | PSSCAN_STATE
734 | PSSCAN_COMM
735 | PSSCAN_UIDGID,
736 TOPMEM_MASK = 0
737 | PSSCAN_PID
738 | PSSCAN_SMAPS
739 | PSSCAN_COMM,
740};
741
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000742int top_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +0000743int top_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersen420b2082002-09-17 22:14:58 +0000744{
Denis Vlasenko266bc172006-09-29 17:16:39 +0000745 int count, lines, col;
Denis Vlasenkob308d812007-08-28 19:35:34 +0000746 unsigned interval;
Denis Vlasenkoe7c1ad12007-09-08 17:21:01 +0000747 int iterations;
Denis Vlasenko04e11c92008-02-10 19:44:20 +0000748 char *sinterval;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000749 SKIP_FEATURE_TOPMEM(const) unsigned scan_mask = TOP_MASK;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000750#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000751 struct termios new_settings;
Denis Vlasenkob308d812007-08-28 19:35:34 +0000752 struct pollfd pfd[1];
Eric Andersen08a72202002-09-30 20:52:10 +0000753 unsigned char c;
Denis Vlasenkob308d812007-08-28 19:35:34 +0000754
755 pfd[0].fd = 0;
756 pfd[0].events = POLLIN;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000757#endif /* FEATURE_USE_TERMIOS */
Eric Andersen08a72202002-09-30 20:52:10 +0000758
Denis Vlasenkoe7c1ad12007-09-08 17:21:01 +0000759 INIT_G();
760
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000761 interval = 5; /* default update interval is 5 seconds */
Denis Vlasenkoe7c1ad12007-09-08 17:21:01 +0000762 iterations = 0; /* infinite */
Denis Vlasenko85818632007-04-19 14:47:11 +0000763
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000764 /* all args are options; -n NUM */
Denis Vlasenko04e11c92008-02-10 19:44:20 +0000765 opt_complementary = "-:n+";
766 getopt32(argv, "d:n:b", &sinterval, &iterations);
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000767 if (option_mask32 & OPT_d) {
Denis Vlasenkob308d812007-08-28 19:35:34 +0000768 /* Need to limit it to not overflow poll timeout */
769 interval = xatou16(sinterval); // -d
770 }
Eric Andersen420b2082002-09-17 22:14:58 +0000771
Eric Andersen44608e92002-10-22 12:21:15 +0000772 /* change to /proc */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000773 xchdir("/proc");
Denis Vlasenkofa076802006-11-05 00:38:51 +0000774#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000775 tcgetattr(0, (void *) &initial_settings);
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000776 memcpy(&new_settings, &initial_settings, sizeof(new_settings));
Denis Vlasenko42dfcd22006-09-09 12:55:02 +0000777 /* unbuffered input, turn off echo */
778 new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
Eric Andersen08a72202002-09-30 20:52:10 +0000779
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000780 bb_signals(BB_FATAL_SIGS, sig_catcher);
Eric Andersen08a72202002-09-30 20:52:10 +0000781 tcsetattr(0, TCSANOW, (void *) &new_settings);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000782#endif /* FEATURE_USE_TERMIOS */
Mike Frysinger223b8872005-07-30 09:42:05 +0000783
Denis Vlasenkofa076802006-11-05 00:38:51 +0000784#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000785 sort_function[0] = pcpu_sort;
786 sort_function[1] = mem_sort;
787 sort_function[2] = time_sort;
788#else
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000789 sort_function[0] = mem_sort;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000790#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Mike Frysinger223b8872005-07-30 09:42:05 +0000791
Eric Andersen08a72202002-09-30 20:52:10 +0000792 while (1) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000793 procps_status_t *p = NULL;
Eric Andersen44608e92002-10-22 12:21:15 +0000794
Denis Vlasenkoe7c1ad12007-09-08 17:21:01 +0000795 lines = 24; /* default */
Rob Landley997650b2006-04-24 23:13:46 +0000796 col = 79;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000797#if ENABLE_FEATURE_USE_TERMIOS
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000798 /* We output to stdout, we need size of stdout (not stdin)! */
799 get_terminal_width_height(STDOUT_FILENO, &col, &lines);
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000800 if (lines < 5 || col < 10) {
Rob Landley997650b2006-04-24 23:13:46 +0000801 sleep(interval);
802 continue;
803 }
Denis Vlasenkofa076802006-11-05 00:38:51 +0000804#endif /* FEATURE_USE_TERMIOS */
Denis Vlasenko4c1d88d2007-09-08 17:34:05 +0000805 if (col > LINE_BUF_SIZE-2) /* +2 bytes for '\n', NUL, */
806 col = LINE_BUF_SIZE-2;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000807 if (!ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && scan_mask == TOP_MASK)
808 lines -= 3;
809 else
810 lines -= 4;
Rob Landley997650b2006-04-24 23:13:46 +0000811
812 /* read process IDs & status for all the processes */
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000813 while ((p = procps_scan(p, scan_mask)) != NULL) {
814 int n;
815 if (scan_mask == TOP_MASK) {
816 n = ntop;
817 top = xrealloc(top, (++ntop) * sizeof(*top));
818 top[n].pid = p->pid;
819 top[n].ppid = p->ppid;
820 top[n].vsz = p->vsz;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000821#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000822 top[n].ticks = p->stime + p->utime;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000823#endif
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000824 top[n].uid = p->uid;
825 strcpy(top[n].state, p->state);
826 strcpy(top[n].comm, p->comm);
827 } else { /* TOPMEM */
828#if ENABLE_FEATURE_TOPMEM
829 if (!(p->mapped_ro | p->mapped_rw))
830 continue; /* kernel threads are ignored */
831 n = ntop;
832 top = xrealloc(topmem, (++ntop) * sizeof(*topmem));
833 strcpy(topmem[n].comm, p->comm);
834 topmem[n].pid = p->pid;
835 topmem[n].vsz = p->mapped_rw + p->mapped_ro;
836 topmem[n].vszrw = p->mapped_rw;
837 topmem[n].rss_sh = p->shared_clean + p->shared_dirty;
838 topmem[n].rss = p->private_clean + p->private_dirty + topmem[n].rss_sh;
839 topmem[n].dirty = p->private_dirty + p->shared_dirty;
840 topmem[n].dirty_sh = p->shared_dirty;
841 topmem[n].stack = p->stack;
842#endif
843 }
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000844 } /* end of "while we read /proc" */
Eric Andersen44608e92002-10-22 12:21:15 +0000845 if (ntop == 0) {
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000846 bb_error_msg("no process info in /proc");
847 break;
Rob Landleyb2804552006-02-13 22:04:27 +0000848 }
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000849
850 if (scan_mask == TOP_MASK) {
Denis Vlasenkofa076802006-11-05 00:38:51 +0000851#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000852 if (!prev_hist_count) {
853 do_stats();
854 usleep(100000);
855 clearmems();
856 continue;
857 }
Eric Andersen08a72202002-09-30 20:52:10 +0000858 do_stats();
Denis Vlasenkof7d07b12007-06-30 08:03:26 +0000859/* TODO: we don't need to sort all 10000 processes, we need to find top 24! */
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000860 qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
Eric Andersen08a72202002-09-30 20:52:10 +0000861#else
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000862 qsort(top, ntop, sizeof(top_status_t), (void*)(sort_function[0]));
Denis Vlasenkofa076802006-11-05 00:38:51 +0000863#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Denis Vlasenkoe96dcb42008-04-17 18:04:38 +0000864 }
865#if ENABLE_FEATURE_TOPMEM
866 else { /* TOPMEM */
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000867 qsort(topmem, ntop, sizeof(topmem_status_t), (void*)topmem_sort);
868 }
Denis Vlasenkoe96dcb42008-04-17 18:04:38 +0000869#endif
Denis Vlasenko266bc172006-09-29 17:16:39 +0000870 count = lines;
Denis Vlasenko25d80622006-10-27 09:34:22 +0000871 if (OPT_BATCH_MODE || count > ntop) {
Denis Vlasenko266bc172006-09-29 17:16:39 +0000872 count = ntop;
Eric Andersen08a72202002-09-30 20:52:10 +0000873 }
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000874 if (scan_mask == TOP_MASK)
875 display_process_list(count, col);
Denis Vlasenkoe96dcb42008-04-17 18:04:38 +0000876#if ENABLE_FEATURE_TOPMEM
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000877 else
878 display_topmem_process_list(count, col);
Denis Vlasenkoe96dcb42008-04-17 18:04:38 +0000879#endif
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000880 clearmems();
881 if (iterations >= 0 && !--iterations)
882 break;
883#if !ENABLE_FEATURE_USE_TERMIOS
884 sleep(interval);
885#else
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000886 if (option_mask32 & (OPT_b|OPT_EOF))
887 /* batch mode, or EOF on stdin ("top </dev/null") */
888 sleep(interval);
889 else if (safe_poll(pfd, 1, interval * 1000) > 0) {
890 if (safe_read(0, &c, 1) != 1) { /* error/EOF? */
891 option_mask32 |= OPT_EOF;
892 continue;
893 }
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000894 if (c == initial_settings.c_cc[VINTR])
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000895 break;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000896 c |= 0x20; /* lowercase */
897 if (c == 'q')
898 break;
899 if (c == 'n') {
900 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000901 sort_function[0] = pid_sort;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000902 }
903 if (c == 'm') {
904 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
Eric Andersen08a72202002-09-30 20:52:10 +0000905 sort_function[0] = mem_sort;
Denis Vlasenko8bdba4d2007-08-29 18:18:08 +0000906#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000907 sort_function[1] = pcpu_sort;
908 sort_function[2] = time_sort;
Eric Andersen08a72202002-09-30 20:52:10 +0000909#endif
910 }
Denis Vlasenkofa076802006-11-05 00:38:51 +0000911#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000912 if (c == 'p') {
913 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
Eric Andersen08a72202002-09-30 20:52:10 +0000914 sort_function[0] = pcpu_sort;
915 sort_function[1] = mem_sort;
916 sort_function[2] = time_sort;
917 }
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000918 if (c == 't') {
919 USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
Eric Andersen08a72202002-09-30 20:52:10 +0000920 sort_function[0] = time_sort;
921 sort_function[1] = mem_sort;
922 sort_function[2] = pcpu_sort;
923 }
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000924#if ENABLE_FEATURE_TOPMEM
925 if (c == 's') {
926 scan_mask = TOPMEM_MASK;
927 free(prev_hist);
928 prev_hist = NULL;
929 prev_hist_count = 0;
930 sort_field = (sort_field + 1) % NUM_SORT_FIELD;
931 }
932 if (c == 'r')
933 inverted ^= 1;
934#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000935#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000936 }
Denis Vlasenkofa076802006-11-05 00:38:51 +0000937#endif /* FEATURE_USE_TERMIOS */
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000938 } /* end of "while (1)" */
939
Denis Vlasenko4daad902007-09-27 10:20:47 +0000940 bb_putchar('\n');
Paul Fox275b9292008-03-20 16:05:02 +0000941#if ENABLE_FEATURE_USE_TERMIOS
Denis Vlasenkocf7cf622008-03-19 19:38:46 +0000942 reset_term();
Paul Fox275b9292008-03-20 16:05:02 +0000943#endif
Eric Andersen420b2082002-09-17 22:14:58 +0000944 return EXIT_SUCCESS;
945}