blob: e9ccac0cdcc9417446324087b9a712c575dcb879 [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
61enum { SORT_DEPTH = 3 };
62
63struct globals {
64 top_status_t *top;
65 int ntop;
66#if ENABLE_FEATURE_USE_TERMIOS
67 struct termios initial_settings;
68#endif
69#if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
70 cmp_funcp sort_function;
71#else
72 cmp_funcp sort_function[SORT_DEPTH];
73 struct save_hist *prev_hist;
74 int prev_hist_count;
75 jiffy_counts_t jif, prev_jif;
76 /* int hist_iterations; */
77 unsigned total_pcpu;
78 /* unsigned long total_vsz; */
79#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +000080};
Denis Vlasenko85818632007-04-19 14:47:11 +000081#define G (*(struct globals*)&bb_common_bufsiz1)
82#define top (G.top )
83#define ntop (G.ntop )
84#if ENABLE_FEATURE_USE_TERMIOS
85#define initial_settings (G. initial_settings )
86#endif
87#define sort_function (G.sort_function )
Denis Vlasenko3bba5452006-12-30 17:57:03 +000088#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenko85818632007-04-19 14:47:11 +000089#define prev_hist (G.prev_hist )
90#define prev_hist_count (G.prev_hist_count )
91#define jif (G.jif )
92#define prev_jif (G.prev_jif )
93#define total_pcpu (G.total_pcpu )
Denis Vlasenko3bba5452006-12-30 17:57:03 +000094#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +000095
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000096#define OPT_BATCH_MODE (option_mask32 & 0x4)
Eric Andersen08a72202002-09-30 20:52:10 +000097
Denis Vlasenko85818632007-04-19 14:47:11 +000098
Denis Vlasenkofa076802006-11-05 00:38:51 +000099#if ENABLE_FEATURE_USE_TERMIOS
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000100static int pid_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +0000101{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000102 /* Buggy wrt pids with high bit set */
103 /* (linux pids are in [1..2^15-1]) */
Rob Landleyb2804552006-02-13 22:04:27 +0000104 return (Q->pid - P->pid);
Eric Andersen08a72202002-09-30 20:52:10 +0000105}
Mike Frysinger223b8872005-07-30 09:42:05 +0000106#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000107
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000108static int mem_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +0000109{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000110 /* We want to avoid unsigned->signed and truncation errors */
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000111 if (Q->vsz < P->vsz) return -1;
112 return Q->vsz != P->vsz; /* 0 if ==, 1 if > */
Eric Andersen08a72202002-09-30 20:52:10 +0000113}
114
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000115
Denis Vlasenko85818632007-04-19 14:47:11 +0000116#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000117
118static int pcpu_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +0000119{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000120 /* Buggy wrt ticks with high bit set */
121 /* Affects only processes for which ticks overflow */
122 return (int)Q->pcpu - (int)P->pcpu;
Eric Andersen08a72202002-09-30 20:52:10 +0000123}
124
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000125static int time_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +0000126{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000127 /* We want to avoid unsigned->signed and truncation errors */
128 if (Q->ticks < P->ticks) return -1;
129 return Q->ticks != P->ticks; /* 0 if ==, 1 if > */
Eric Andersen08a72202002-09-30 20:52:10 +0000130}
131
Denis Vlasenkoac678ec2007-04-16 22:32:04 +0000132static int mult_lvl_cmp(void* a, void* b)
133{
Rob Landleyb2804552006-02-13 22:04:27 +0000134 int i, cmp_val;
Eric Andersen08a72202002-09-30 20:52:10 +0000135
Denis Vlasenkofa076802006-11-05 00:38:51 +0000136 for (i = 0; i < SORT_DEPTH; i++) {
Rob Landleyb2804552006-02-13 22:04:27 +0000137 cmp_val = (*sort_function[i])(a, b);
138 if (cmp_val != 0)
139 return cmp_val;
140 }
141 return 0;
Eric Andersen08a72202002-09-30 20:52:10 +0000142}
143
Eric Andersen08a72202002-09-30 20:52:10 +0000144
Rob Landley997650b2006-04-24 23:13:46 +0000145static void get_jiffy_counts(void)
Rob Landleyb2804552006-02-13 22:04:27 +0000146{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000147 FILE* fp = xfopen("stat", "r");
Rob Landley997650b2006-04-24 23:13:46 +0000148 prev_jif = jif;
149 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
150 &jif.usr,&jif.nic,&jif.sys,&jif.idle,
151 &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
Denis Vlasenko42dfcd22006-09-09 12:55:02 +0000152 bb_error_msg_and_die("failed to read /proc/stat");
Rob Landleyb2804552006-02-13 22:04:27 +0000153 }
Rob Landley997650b2006-04-24 23:13:46 +0000154 fclose(fp);
155 jif.total = jif.usr + jif.nic + jif.sys + jif.idle
156 + jif.iowait + jif.irq + jif.softirq + jif.steal;
157 /* procps 2.x does not count iowait as busy time */
158 jif.busy = jif.total - jif.idle - jif.iowait;
Eric Andersen08a72202002-09-30 20:52:10 +0000159}
160
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000161
Eric Andersen08a72202002-09-30 20:52:10 +0000162static void do_stats(void)
163{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000164 top_status_t *cur;
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000165 pid_t pid;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000166 int i, last_i, n;
Rob Landley997650b2006-04-24 23:13:46 +0000167 struct save_hist *new_hist;
Eric Andersen08a72202002-09-30 20:52:10 +0000168
Rob Landley997650b2006-04-24 23:13:46 +0000169 get_jiffy_counts();
170 total_pcpu = 0;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000171 /* total_vsz = 0; */
Rob Landley997650b2006-04-24 23:13:46 +0000172 new_hist = xmalloc(sizeof(struct save_hist)*ntop);
Rob Landleyb2804552006-02-13 22:04:27 +0000173 /*
174 * Make a pass through the data to get stats.
175 */
Rob Landley997650b2006-04-24 23:13:46 +0000176 /* hist_iterations = 0; */
177 i = 0;
178 for (n = 0; n < ntop; n++) {
Rob Landleyb2804552006-02-13 22:04:27 +0000179 cur = top + n;
180
181 /*
182 * Calculate time in cur process. Time is sum of user time
Rob Landley997650b2006-04-24 23:13:46 +0000183 * and system time
Rob Landleyb2804552006-02-13 22:04:27 +0000184 */
Rob Landleyb2804552006-02-13 22:04:27 +0000185 pid = cur->pid;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000186 new_hist[n].ticks = cur->ticks;
Rob Landley997650b2006-04-24 23:13:46 +0000187 new_hist[n].pid = pid;
Rob Landleyb2804552006-02-13 22:04:27 +0000188
189 /* find matching entry from previous pass */
Rob Landley997650b2006-04-24 23:13:46 +0000190 cur->pcpu = 0;
191 /* do not start at index 0, continue at last used one
192 * (brought hist_iterations from ~14000 down to 172) */
193 last_i = i;
194 if (prev_hist_count) do {
195 if (prev_hist[i].pid == pid) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000196 cur->pcpu = cur->ticks - prev_hist[i].ticks;
197 total_pcpu += cur->pcpu;
Rob Landleyb2804552006-02-13 22:04:27 +0000198 break;
199 }
Rob Landley997650b2006-04-24 23:13:46 +0000200 i = (i+1) % prev_hist_count;
201 /* hist_iterations++; */
202 } while (i != last_i);
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000203 /* total_vsz += cur->vsz; */
Eric Andersen08a72202002-09-30 20:52:10 +0000204 }
205
206 /*
Rob Landleyb2804552006-02-13 22:04:27 +0000207 * Save cur frame's information.
Eric Andersen08a72202002-09-30 20:52:10 +0000208 */
Rob Landley997650b2006-04-24 23:13:46 +0000209 free(prev_hist);
210 prev_hist = new_hist;
211 prev_hist_count = ntop;
Eric Andersen08a72202002-09-30 20:52:10 +0000212}
Denis Vlasenkofa076802006-11-05 00:38:51 +0000213#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Eric Andersen08a72202002-09-30 20:52:10 +0000214
Eric Andersen420b2082002-09-17 22:14:58 +0000215/* display generic info (meminfo / loadavg) */
Rob Landley997650b2006-04-24 23:13:46 +0000216static unsigned long display_generic(int scr_width)
Eric Andersen420b2082002-09-17 22:14:58 +0000217{
218 FILE *fp;
219 char buf[80];
Rob Landley997650b2006-04-24 23:13:46 +0000220 char scrbuf[80];
Eric Andersen420b2082002-09-17 22:14:58 +0000221 unsigned long total, used, mfree, shared, buffers, cached;
Denis Vlasenko110967a2007-07-15 19:27:48 +0000222 unsigned total_diff;
223
224#if ENABLE_FEATURE_TOP_DECIMALS
225 /* formats 7 char string (8 with terminating NUL) */
226 /* using GCCism (nested function) - we need to access total_diff */
227 /* This produces more than 100 bytes smaller code */
228 char *fmt_100percent_8(char pbuf[8], unsigned value)
229 {
230 unsigned t;
231 if (value >= total_diff) { /* 100% ? */
232 strcpy(pbuf, " 100% ");
233 return pbuf;
234 }
235 /* else generate " [N/space]N.N% " string */
236 value = 1000 * value / total_diff;
237 t = value / 100;
238 value = value % 100;
239 pbuf[0] = ' ';
240 pbuf[1] = t ? t + '0' : ' ';
241 pbuf[2] = '0' + (value / 10);
242 pbuf[3] = '.';
243 pbuf[4] = '0' + (value % 10);
244 pbuf[5] = '%';
245 pbuf[6] = ' ';
246 pbuf[7] = '\0';
247 return pbuf;
248 }
249#endif
Eric Andersen420b2082002-09-17 22:14:58 +0000250
251 /* read memory info */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000252 fp = xfopen("meminfo", "r");
Eric Andersen420b2082002-09-17 22:14:58 +0000253
Eric Andersen7857c032003-10-11 18:47:20 +0000254 /*
255 * Old kernels (such as 2.4.x) had a nice summary of memory info that
256 * we could parse, however this is gone entirely in 2.6. Try parsing
257 * the old way first, and if that fails, parse each field manually.
258 *
259 * First, we read in the first line. Old kernels will have bogus
260 * strings we don't care about, whereas new kernels will start right
261 * out with MemTotal:
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000262 * -- PFM.
Eric Andersen7857c032003-10-11 18:47:20 +0000263 */
264 if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000265 fgets(buf, sizeof(buf), fp); /* skip first line */
Eric Andersen7857c032003-10-11 18:47:20 +0000266
267 fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
Denis Vlasenko5a654472007-06-10 17:11:59 +0000268 &total, &used, &mfree, &shared, &buffers, &cached);
269 /* convert to kilobytes */
270 used /= 1024;
271 mfree /= 1024;
272 shared /= 1024;
273 buffers /= 1024;
274 cached /= 1024;
275 total /= 1024;
Eric Andersen7857c032003-10-11 18:47:20 +0000276 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000277 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000278 * Revert to manual parsing, which incidentally already has the
279 * sizes in kilobytes. This should be safe for both 2.4 and
280 * 2.6.
281 */
Eric Andersen7857c032003-10-11 18:47:20 +0000282
283 fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
284
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000285 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000286 * MemShared: is no longer present in 2.6. Report this as 0,
287 * to maintain consistent behavior with normal procps.
288 */
289 if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
290 shared = 0;
291
292 fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
293 fscanf(fp, "Cached: %lu %s\n", &cached, buf);
294
295 used = total - mfree;
Eric Andersen420b2082002-09-17 22:14:58 +0000296 }
297 fclose(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000298
Denis Vlasenko110967a2007-07-15 19:27:48 +0000299 /* output memory info */
Rob Landley997650b2006-04-24 23:13:46 +0000300 if (scr_width > sizeof(scrbuf))
301 scr_width = sizeof(scrbuf);
302 snprintf(scrbuf, scr_width,
Denis Vlasenkoc1166c32007-07-15 19:23:38 +0000303 "Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached",
Rob Landleyb2804552006-02-13 22:04:27 +0000304 used, mfree, shared, buffers, cached);
Denis Vlasenko110967a2007-07-15 19:27:48 +0000305 /* clear screen & go to top */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000306 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000307
Denis Vlasenko5a654472007-06-10 17:11:59 +0000308 if (ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS) {
309 /*
310 * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
311 */
Denis Vlasenko110967a2007-07-15 19:27:48 +0000312 /* using (unsigned) casts to make operations cheaper */
313 total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1);
Denis Vlasenko74511962007-06-11 16:31:55 +0000314#if ENABLE_FEATURE_TOP_DECIMALS
Denis Vlasenko110967a2007-07-15 19:27:48 +0000315/* Generated code is approx +0.3k */
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000316#define CALC_STAT(xxx) char xxx[8]
Denis Vlasenko110967a2007-07-15 19:27:48 +0000317#define SHOW_STAT(xxx) fmt_100percent_8(xxx, (unsigned)(jif.xxx - prev_jif.xxx))
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000318#define FMT "%s"
Denis Vlasenko74511962007-06-11 16:31:55 +0000319#else
Denis Vlasenkob1e5add2007-06-10 18:04:54 +0000320#define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
321#define SHOW_STAT(xxx) xxx
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000322#define FMT "%4u%% "
Denis Vlasenko74511962007-06-11 16:31:55 +0000323#endif
Denis Vlasenko110967a2007-07-15 19:27:48 +0000324 { /* need block: CALC_STAT are declarations */
325 CALC_STAT(usr);
326 CALC_STAT(sys);
327 CALC_STAT(nic);
328 CALC_STAT(idle);
329 CALC_STAT(iowait);
330 CALC_STAT(irq);
331 CALC_STAT(softirq);
332 //CALC_STAT(steal);
Denis Vlasenko5a654472007-06-10 17:11:59 +0000333
Denis Vlasenko110967a2007-07-15 19:27:48 +0000334 snprintf(scrbuf, scr_width,
335 /* Barely fits in 79 chars when in "decimals" mode. */
336 "CPU:"FMT"usr"FMT"sys"FMT"nice"FMT"idle"FMT"io"FMT"irq"FMT"softirq",
337 SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
338 SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
339 //, SHOW_STAT(steal) - what is this 'steal' thing?
340 // I doubt anyone wants to know it
341 );
342 }
Denis Vlasenko5a654472007-06-10 17:11:59 +0000343 puts(scrbuf);
344#undef SHOW_STAT
345#undef CALC_STAT
Denis Vlasenkob1e5add2007-06-10 18:04:54 +0000346#undef FMT
Denis Vlasenko5a654472007-06-10 17:11:59 +0000347 }
348
Denis Vlasenko110967a2007-07-15 19:27:48 +0000349 /* read load average as a string */
350 buf[0] = '\0';
351 open_read_close("loadavg", buf, sizeof("N.NN N.NN N.NN")-1);
352 buf[sizeof("N.NN N.NN N.NN")-1] = '\0';
Denis Vlasenko25d80622006-10-27 09:34:22 +0000353 snprintf(scrbuf, scr_width, "Load average: %s", buf);
Denis Vlasenko5a654472007-06-10 17:11:59 +0000354 puts(scrbuf);
Rob Landley997650b2006-04-24 23:13:46 +0000355
Eric Andersen7857c032003-10-11 18:47:20 +0000356 return total;
Eric Andersen420b2082002-09-17 22:14:58 +0000357}
358
Eric Andersen420b2082002-09-17 22:14:58 +0000359/* display process statuses */
Rob Landley997650b2006-04-24 23:13:46 +0000360static void display_status(int count, int scr_width)
Eric Andersen420b2082002-09-17 22:14:58 +0000361{
Rob Landley997650b2006-04-24 23:13:46 +0000362 enum {
Denis Vlasenko74511962007-06-11 16:31:55 +0000363 BITS_PER_INT = sizeof(int)*8
Rob Landley997650b2006-04-24 23:13:46 +0000364 };
365
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000366 top_status_t *s = top;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000367 char vsz_str_buf[8];
368 unsigned long total_memory = display_generic(scr_width); /* or use total_vsz? */
Denis Vlasenko74511962007-06-11 16:31:55 +0000369 /* xxx_shift and xxx_scale variables allow us to replace
370 * expensive divides with multiply and shift */
371 unsigned pmem_shift, pmem_scale, pmem_half;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000372#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenko74511962007-06-11 16:31:55 +0000373 unsigned pcpu_shift, pcpu_scale, pcpu_half;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000374 unsigned busy_jifs;
Rob Landley997650b2006-04-24 23:13:46 +0000375
Eric Andersen420b2082002-09-17 22:14:58 +0000376 /* what info of the processes is shown */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000377 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
Denis Vlasenko74511962007-06-11 16:31:55 +0000378 " PID PPID USER STAT VSZ %MEM %CPU COMMAND");
Rob Landley997650b2006-04-24 23:13:46 +0000379#define MIN_WIDTH \
Denis Vlasenko74511962007-06-11 16:31:55 +0000380 sizeof( " PID PPID USER STAT VSZ %MEM %CPU C")
Eric Andersen08a72202002-09-30 20:52:10 +0000381#else
Denis Vlasenko74511962007-06-11 16:31:55 +0000382
383 /* !CPU_USAGE_PERCENTAGE */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000384 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
Denis Vlasenko74511962007-06-11 16:31:55 +0000385 " PID PPID USER STAT VSZ %MEM COMMAND");
Rob Landley997650b2006-04-24 23:13:46 +0000386#define MIN_WIDTH \
Denis Vlasenko74511962007-06-11 16:31:55 +0000387 sizeof( " PID PPID USER STAT VSZ %MEM C")
Rob Landley997650b2006-04-24 23:13:46 +0000388#endif
389
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000390#if ENABLE_FEATURE_TOP_DECIMALS
391#define UPSCALE 1000
392#define CALC_STAT(name, val) div_t name = div((val), 10)
393#define SHOW_STAT(name) name.quot, '0'+name.rem
394#define FMT "%3u.%c"
395#else
396#define UPSCALE 100
397#define CALC_STAT(name, val) unsigned name = (val)
398#define SHOW_STAT(name) name
399#define FMT "%4u%%"
400#endif
Rob Landley997650b2006-04-24 23:13:46 +0000401 /*
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000402 * MEM% = s->vsz/MemTotal
Rob Landley997650b2006-04-24 23:13:46 +0000403 */
Denis Vlasenko74511962007-06-11 16:31:55 +0000404 pmem_shift = BITS_PER_INT-11;
405 pmem_scale = UPSCALE*(1U<<(BITS_PER_INT-11)) / total_memory;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000406 /* s->vsz is in kb. we want (s->vsz * pmem_scale) to never overflow */
Rob Landley997650b2006-04-24 23:13:46 +0000407 while (pmem_scale >= 512) {
408 pmem_scale /= 4;
409 pmem_shift -= 2;
410 }
Denis Vlasenko74511962007-06-11 16:31:55 +0000411 pmem_half = (1U << pmem_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000412#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
413 busy_jifs = jif.busy - prev_jif.busy;
414 /* This happens if there were lots of short-lived processes
415 * between two top updates (e.g. compilation) */
416 if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
417
Rob Landley997650b2006-04-24 23:13:46 +0000418 /*
419 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
420 * (pcpu is delta of sys+user time between samples)
421 */
422 /* (jif.xxx - prev_jif.xxx) and s->pcpu are
423 * in 0..~64000 range (HZ*update_interval).
424 * we assume that unsigned is at least 32-bit.
425 */
426 pcpu_shift = 6;
Denis Vlasenko74511962007-06-11 16:31:55 +0000427 pcpu_scale = (UPSCALE*64*(uint16_t)busy_jifs ? : 1);
428 while (pcpu_scale < (1U<<(BITS_PER_INT-2))) {
Rob Landley997650b2006-04-24 23:13:46 +0000429 pcpu_scale *= 4;
430 pcpu_shift += 2;
431 }
432 pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1);
433 /* we want (s->pcpu * pcpu_scale) to never overflow */
434 while (pcpu_scale >= 1024) {
435 pcpu_scale /= 4;
436 pcpu_shift -= 2;
437 }
Denis Vlasenko74511962007-06-11 16:31:55 +0000438 pcpu_half = (1U << pcpu_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
Rob Landley997650b2006-04-24 23:13:46 +0000439 /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
Eric Andersen08a72202002-09-30 20:52:10 +0000440#endif
Denis Vlasenko74511962007-06-11 16:31:55 +0000441
442 /* Ok, all prelim data is ready, go thru the list */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000443 while (count-- > 0) {
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000444 int col = scr_width;
Denis Vlasenko74511962007-06-11 16:31:55 +0000445 CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift);
446#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
447 CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift);
448#endif
Eric Andersen420b2082002-09-17 22:14:58 +0000449
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000450 if (s->vsz >= 100*1024)
451 sprintf(vsz_str_buf, "%6ldM", s->vsz/1024);
Manuel Novoa III d4993302002-09-18 19:27:10 +0000452 else
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000453 sprintf(vsz_str_buf, "%7ld", s->vsz);
Denis Vlasenko74511962007-06-11 16:31:55 +0000454 // PID PPID USER STAT VSZ %MEM [%CPU] COMMAND
Denis Vlasenko9109fbe2007-07-15 17:07:58 +0000455 col -= printf("\n" "%5u%6u %-8.8s %s%s" FMT
Denis Vlasenko74511962007-06-11 16:31:55 +0000456#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
457 FMT
458#endif
459 " ",
460 s->pid, s->ppid, get_cached_username(s->uid),
461 s->state, vsz_str_buf,
462 SHOW_STAT(pmem)
463#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
464 , SHOW_STAT(pcpu)
465#endif
466 );
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000467 if (col > 0) {
468 char buf[col + 1];
469 read_cmdline(buf, col, s->pid, s->comm);
470 fputs(buf, stdout);
471 }
Bernhard Reutner-Fischere2922e42006-05-19 12:48:56 +0000472 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
Rob Landley997650b2006-04-24 23:13:46 +0000473 jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
Eric Andersen420b2082002-09-17 22:14:58 +0000474 s++;
Eric Andersen08a72202002-09-30 20:52:10 +0000475 }
Rob Landley997650b2006-04-24 23:13:46 +0000476 /* printf(" %d", hist_iterations); */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000477 putchar(OPT_BATCH_MODE ? '\n' : '\r');
Rob Landley997650b2006-04-24 23:13:46 +0000478 fflush(stdout);
Eric Andersen44608e92002-10-22 12:21:15 +0000479}
Denis Vlasenko24c5fba2007-07-15 19:25:01 +0000480#undef UPSCALE
Denis Vlasenko74511962007-06-11 16:31:55 +0000481#undef SHOW_STAT
482#undef CALC_STAT
483#undef FMT
Eric Andersen44608e92002-10-22 12:21:15 +0000484
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000485
Eric Andersen44608e92002-10-22 12:21:15 +0000486static void clearmems(void)
487{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000488 clear_username_cache();
Eric Andersen08a72202002-09-30 20:52:10 +0000489 free(top);
Eric Andersen08a72202002-09-30 20:52:10 +0000490 top = 0;
Eric Andersen08a72202002-09-30 20:52:10 +0000491 ntop = 0;
492}
493
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000494
Denis Vlasenkofa076802006-11-05 00:38:51 +0000495#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000496#include <termios.h>
Eric Andersen08a72202002-09-30 20:52:10 +0000497#include <signal.h>
498
Eric Andersen08a72202002-09-30 20:52:10 +0000499static void reset_term(void)
500{
501 tcsetattr(0, TCSANOW, (void *) &initial_settings);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000502#if ENABLE_FEATURE_CLEAN_UP
Eric Andersen08a72202002-09-30 20:52:10 +0000503 clearmems();
Denis Vlasenkofa076802006-11-05 00:38:51 +0000504#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000505 free(prev_hist);
Eric Andersen08a72202002-09-30 20:52:10 +0000506#endif
Denis Vlasenkofa076802006-11-05 00:38:51 +0000507#endif /* FEATURE_CLEAN_UP */
Eric Andersen08a72202002-09-30 20:52:10 +0000508}
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000509
Rob Landleyb2804552006-02-13 22:04:27 +0000510static void sig_catcher(int sig ATTRIBUTE_UNUSED)
Eric Andersen08a72202002-09-30 20:52:10 +0000511{
512 reset_term();
Rob Landleydb1ab1a2006-06-28 14:11:25 +0000513 exit(1);
Eric Andersen08a72202002-09-30 20:52:10 +0000514}
Denis Vlasenkofa076802006-11-05 00:38:51 +0000515#endif /* FEATURE_USE_TERMIOS */
Eric Andersen08a72202002-09-30 20:52:10 +0000516
517
Denis Vlasenko06af2162007-02-03 17:28:39 +0000518int top_main(int argc, char **argv);
Eric Andersen420b2082002-09-17 22:14:58 +0000519int top_main(int argc, char **argv)
520{
Denis Vlasenko266bc172006-09-29 17:16:39 +0000521 int count, lines, col;
Denis Vlasenko13858992006-10-08 12:49:22 +0000522 unsigned interval = 5; /* default update rate is 5 seconds */
523 unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000524 char *sinterval, *siterations;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000525#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000526 struct termios new_settings;
527 struct timeval tv;
528 fd_set readfds;
529 unsigned char c;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000530#endif /* FEATURE_USE_TERMIOS */
Eric Andersen08a72202002-09-30 20:52:10 +0000531
Denis Vlasenko42dfcd22006-09-09 12:55:02 +0000532 interval = 5;
Denis Vlasenko85818632007-04-19 14:47:11 +0000533
534 /* do normal option parsing */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000535 opt_complementary = "-";
Denis Vlasenkofa076802006-11-05 00:38:51 +0000536 getopt32(argc, argv, "d:n:b", &sinterval, &siterations);
Denis Vlasenko13858992006-10-08 12:49:22 +0000537 if (option_mask32 & 0x1) interval = xatou(sinterval); // -d
538 if (option_mask32 & 0x2) iterations = xatou(siterations); // -n
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000539 //if (option_mask32 & 0x4) // -b
Eric Andersen420b2082002-09-17 22:14:58 +0000540
Eric Andersen44608e92002-10-22 12:21:15 +0000541 /* change to /proc */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000542 xchdir("/proc");
Denis Vlasenkofa076802006-11-05 00:38:51 +0000543#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000544 tcgetattr(0, (void *) &initial_settings);
545 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
Denis Vlasenko42dfcd22006-09-09 12:55:02 +0000546 /* unbuffered input, turn off echo */
547 new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
Eric Andersen08a72202002-09-30 20:52:10 +0000548
Rob Landley997650b2006-04-24 23:13:46 +0000549 signal(SIGTERM, sig_catcher);
Rob Landleydb1ab1a2006-06-28 14:11:25 +0000550 signal(SIGINT, sig_catcher);
Eric Andersen08a72202002-09-30 20:52:10 +0000551 tcsetattr(0, TCSANOW, (void *) &new_settings);
552 atexit(reset_term);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000553#endif /* FEATURE_USE_TERMIOS */
Mike Frysinger223b8872005-07-30 09:42:05 +0000554
Denis Vlasenkofa076802006-11-05 00:38:51 +0000555#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000556 sort_function[0] = pcpu_sort;
557 sort_function[1] = mem_sort;
558 sort_function[2] = time_sort;
559#else
560 sort_function = mem_sort;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000561#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Mike Frysinger223b8872005-07-30 09:42:05 +0000562
Eric Andersen08a72202002-09-30 20:52:10 +0000563 while (1) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000564 procps_status_t *p = NULL;
Eric Andersen44608e92002-10-22 12:21:15 +0000565
Rob Landley997650b2006-04-24 23:13:46 +0000566 /* Default to 25 lines - 5 lines for status */
Denis Vlasenko5a654472007-06-10 17:11:59 +0000567 lines = 24 - 3 USE_FEATURE_TOP_CPU_GLOBAL_PERCENTS( - 1);
Rob Landley997650b2006-04-24 23:13:46 +0000568 col = 79;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000569#if ENABLE_FEATURE_USE_TERMIOS
Rob Landley997650b2006-04-24 23:13:46 +0000570 get_terminal_width_height(0, &col, &lines);
571 if (lines < 5 || col < MIN_WIDTH) {
572 sleep(interval);
573 continue;
574 }
Denis Vlasenko5a654472007-06-10 17:11:59 +0000575 lines -= 3 USE_FEATURE_TOP_CPU_GLOBAL_PERCENTS( + 1);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000576#endif /* FEATURE_USE_TERMIOS */
Rob Landley997650b2006-04-24 23:13:46 +0000577
578 /* read process IDs & status for all the processes */
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000579 while ((p = procps_scan(p, 0
580 | PSSCAN_PID
581 | PSSCAN_PPID
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000582 | PSSCAN_VSZ
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000583 | PSSCAN_STIME
584 | PSSCAN_UTIME
585 | PSSCAN_STATE
586 | PSSCAN_COMM
587 | PSSCAN_SID
588 | PSSCAN_UIDGID
589 ))) {
Eric Andersen44608e92002-10-22 12:21:15 +0000590 int n = ntop;
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000591 top = xrealloc(top, (++ntop) * sizeof(*top));
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000592 top[n].pid = p->pid;
593 top[n].ppid = p->ppid;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000594 top[n].vsz = p->vsz;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000595#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000596 top[n].ticks = p->stime + p->utime;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000597#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000598 top[n].uid = p->uid;
599 strcpy(top[n].state, p->state);
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000600 strcpy(top[n].comm, p->comm);
Eric Andersen44608e92002-10-22 12:21:15 +0000601 }
602 if (ntop == 0) {
Denis Vlasenko74511962007-06-11 16:31:55 +0000603 bb_error_msg_and_die("no process info in /proc");
Rob Landleyb2804552006-02-13 22:04:27 +0000604 }
Denis Vlasenkofa076802006-11-05 00:38:51 +0000605#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000606 if (!prev_hist_count) {
Eric Andersen08a72202002-09-30 20:52:10 +0000607 do_stats();
608 sleep(1);
609 clearmems();
610 continue;
Rob Landleyb2804552006-02-13 22:04:27 +0000611 }
Eric Andersen08a72202002-09-30 20:52:10 +0000612 do_stats();
Denis Vlasenkof7d07b12007-06-30 08:03:26 +0000613/* TODO: we don't need to sort all 10000 processes, we need to find top 24! */
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000614 qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
Eric Andersen08a72202002-09-30 20:52:10 +0000615#else
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000616 qsort(top, ntop, sizeof(top_status_t), (void*)sort_function);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000617#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000618 count = lines;
Denis Vlasenko25d80622006-10-27 09:34:22 +0000619 if (OPT_BATCH_MODE || count > ntop) {
Denis Vlasenko266bc172006-09-29 17:16:39 +0000620 count = ntop;
Eric Andersen08a72202002-09-30 20:52:10 +0000621 }
622 /* show status for each of the processes */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000623 display_status(count, col);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000624#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000625 tv.tv_sec = interval;
626 tv.tv_usec = 0;
Rob Landley997650b2006-04-24 23:13:46 +0000627 FD_ZERO(&readfds);
628 FD_SET(0, &readfds);
629 select(1, &readfds, NULL, NULL, &tv);
630 if (FD_ISSET(0, &readfds)) {
631 if (read(0, &c, 1) <= 0) { /* signal */
Mike Frysinger223b8872005-07-30 09:42:05 +0000632 return EXIT_FAILURE;
633 }
Rob Landley997650b2006-04-24 23:13:46 +0000634 if (c == 'q' || c == initial_settings.c_cc[VINTR])
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000635 break;
Rob Landley997650b2006-04-24 23:13:46 +0000636 if (c == 'M') {
Denis Vlasenkofa076802006-11-05 00:38:51 +0000637#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000638 sort_function[0] = mem_sort;
639 sort_function[1] = pcpu_sort;
640 sort_function[2] = time_sort;
641#else
642 sort_function = mem_sort;
643#endif
644 }
Denis Vlasenkofa076802006-11-05 00:38:51 +0000645#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000646 if (c == 'P') {
Eric Andersen08a72202002-09-30 20:52:10 +0000647 sort_function[0] = pcpu_sort;
648 sort_function[1] = mem_sort;
649 sort_function[2] = time_sort;
650 }
Rob Landley997650b2006-04-24 23:13:46 +0000651 if (c == 'T') {
Eric Andersen08a72202002-09-30 20:52:10 +0000652 sort_function[0] = time_sort;
653 sort_function[1] = mem_sort;
654 sort_function[2] = pcpu_sort;
655 }
656#endif
Rob Landley997650b2006-04-24 23:13:46 +0000657 if (c == 'N') {
Denis Vlasenkofa076802006-11-05 00:38:51 +0000658#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000659 sort_function[0] = pid_sort;
660#else
661 sort_function = pid_sort;
662#endif
663 }
664 }
Denis Vlasenko266bc172006-09-29 17:16:39 +0000665 if (!--iterations)
666 break;
Eric Andersen08a72202002-09-30 20:52:10 +0000667#else
Eric Andersen420b2082002-09-17 22:14:58 +0000668 sleep(interval);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000669#endif /* FEATURE_USE_TERMIOS */
Eric Andersen08a72202002-09-30 20:52:10 +0000670 clearmems();
Eric Andersen420b2082002-09-17 22:14:58 +0000671 }
Rob Landley997650b2006-04-24 23:13:46 +0000672 if (ENABLE_FEATURE_CLEAN_UP)
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000673 clearmems();
674 putchar('\n');
Eric Andersen420b2082002-09-17 22:14:58 +0000675 return EXIT_SUCCESS;
676}