blob: 7d30936a8a5a4c12b268d6971d8cc4021ab24660 [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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000031#include "busybox.h"
Eric Andersen420b2082002-09-17 22:14:58 +000032
Eric Andersen08a72202002-09-30 20:52:10 +000033
Denis Vlasenko459e4d62006-11-05 00:43:51 +000034typedef struct {
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];
43 char comm[COMM_LEN];
44} top_status_t;
45static top_status_t *top;
Eric Andersen08a72202002-09-30 20:52:10 +000046static int ntop;
Denis Vlasenko459e4d62006-11-05 00:43:51 +000047/* This structure stores some critical information from one frame to
48 the next. Used for finding deltas. */
49struct save_hist {
50 unsigned long ticks;
51 unsigned pid;
52};
Denis Vlasenko3bba5452006-12-30 17:57:03 +000053#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenko459e4d62006-11-05 00:43:51 +000054static struct save_hist *prev_hist;
55static int prev_hist_count;
56/* static int hist_iterations; */
57static unsigned total_pcpu;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +000058/* static unsigned long total_vsz; */
Denis Vlasenko3bba5452006-12-30 17:57:03 +000059#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +000060
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000061#define OPT_BATCH_MODE (option_mask32 & 0x4)
Eric Andersen08a72202002-09-30 20:52:10 +000062
Denis Vlasenkofa076802006-11-05 00:38:51 +000063#if ENABLE_FEATURE_USE_TERMIOS
Denis Vlasenko459e4d62006-11-05 00:43:51 +000064static int pid_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000065{
Denis Vlasenko459e4d62006-11-05 00:43:51 +000066 /* Buggy wrt pids with high bit set */
67 /* (linux pids are in [1..2^15-1]) */
Rob Landleyb2804552006-02-13 22:04:27 +000068 return (Q->pid - P->pid);
Eric Andersen08a72202002-09-30 20:52:10 +000069}
Mike Frysinger223b8872005-07-30 09:42:05 +000070#endif
Eric Andersen08a72202002-09-30 20:52:10 +000071
Denis Vlasenko459e4d62006-11-05 00:43:51 +000072static int mem_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000073{
Denis Vlasenko459e4d62006-11-05 00:43:51 +000074 /* We want to avoid unsigned->signed and truncation errors */
Mike Frysinger0aa6ba52007-02-08 08:21:58 +000075 if (Q->vsz < P->vsz) return -1;
76 return Q->vsz != P->vsz; /* 0 if ==, 1 if > */
Eric Andersen08a72202002-09-30 20:52:10 +000077}
78
Denis Vlasenko459e4d62006-11-05 00:43:51 +000079
80typedef int (*cmp_funcp)(top_status_t *P, top_status_t *Q);
81
82#if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
83
84static cmp_funcp sort_function;
85
86#else
Eric Andersen08a72202002-09-30 20:52:10 +000087
Denis Vlasenkofa076802006-11-05 00:38:51 +000088enum { SORT_DEPTH = 3 };
Eric Andersen08a72202002-09-30 20:52:10 +000089
Denis Vlasenko459e4d62006-11-05 00:43:51 +000090static cmp_funcp sort_function[SORT_DEPTH];
91
92static int pcpu_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000093{
Denis Vlasenko459e4d62006-11-05 00:43:51 +000094 /* Buggy wrt ticks with high bit set */
95 /* Affects only processes for which ticks overflow */
96 return (int)Q->pcpu - (int)P->pcpu;
Eric Andersen08a72202002-09-30 20:52:10 +000097}
98
Denis Vlasenko459e4d62006-11-05 00:43:51 +000099static int time_sort(top_status_t *P, top_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +0000100{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000101 /* We want to avoid unsigned->signed and truncation errors */
102 if (Q->ticks < P->ticks) return -1;
103 return Q->ticks != P->ticks; /* 0 if ==, 1 if > */
Eric Andersen08a72202002-09-30 20:52:10 +0000104}
105
Denis Vlasenkoac678ec2007-04-16 22:32:04 +0000106static int mult_lvl_cmp(void* a, void* b)
107{
Rob Landleyb2804552006-02-13 22:04:27 +0000108 int i, cmp_val;
Eric Andersen08a72202002-09-30 20:52:10 +0000109
Denis Vlasenkofa076802006-11-05 00:38:51 +0000110 for (i = 0; i < SORT_DEPTH; i++) {
Rob Landleyb2804552006-02-13 22:04:27 +0000111 cmp_val = (*sort_function[i])(a, b);
112 if (cmp_val != 0)
113 return cmp_val;
114 }
115 return 0;
Eric Andersen08a72202002-09-30 20:52:10 +0000116}
117
Eric Andersen08a72202002-09-30 20:52:10 +0000118
Denis Vlasenkofa076802006-11-05 00:38:51 +0000119typedef struct {
Rob Landley997650b2006-04-24 23:13:46 +0000120 unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
121 unsigned long long total;
122 unsigned long long busy;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000123} jiffy_counts_t;
124static jiffy_counts_t jif, prev_jif;
Rob Landley997650b2006-04-24 23:13:46 +0000125static void get_jiffy_counts(void)
Rob Landleyb2804552006-02-13 22:04:27 +0000126{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000127 FILE* fp = xfopen("stat", "r");
Rob Landley997650b2006-04-24 23:13:46 +0000128 prev_jif = jif;
129 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
130 &jif.usr,&jif.nic,&jif.sys,&jif.idle,
131 &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
Denis Vlasenko42dfcd22006-09-09 12:55:02 +0000132 bb_error_msg_and_die("failed to read /proc/stat");
Rob Landleyb2804552006-02-13 22:04:27 +0000133 }
Rob Landley997650b2006-04-24 23:13:46 +0000134 fclose(fp);
135 jif.total = jif.usr + jif.nic + jif.sys + jif.idle
136 + jif.iowait + jif.irq + jif.softirq + jif.steal;
137 /* procps 2.x does not count iowait as busy time */
138 jif.busy = jif.total - jif.idle - jif.iowait;
Eric Andersen08a72202002-09-30 20:52:10 +0000139}
140
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000141
Eric Andersen08a72202002-09-30 20:52:10 +0000142static void do_stats(void)
143{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000144 top_status_t *cur;
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000145 pid_t pid;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000146 int i, last_i, n;
Rob Landley997650b2006-04-24 23:13:46 +0000147 struct save_hist *new_hist;
Eric Andersen08a72202002-09-30 20:52:10 +0000148
Rob Landley997650b2006-04-24 23:13:46 +0000149 get_jiffy_counts();
150 total_pcpu = 0;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000151 /* total_vsz = 0; */
Rob Landley997650b2006-04-24 23:13:46 +0000152 new_hist = xmalloc(sizeof(struct save_hist)*ntop);
Rob Landleyb2804552006-02-13 22:04:27 +0000153 /*
154 * Make a pass through the data to get stats.
155 */
Rob Landley997650b2006-04-24 23:13:46 +0000156 /* hist_iterations = 0; */
157 i = 0;
158 for (n = 0; n < ntop; n++) {
Rob Landleyb2804552006-02-13 22:04:27 +0000159 cur = top + n;
160
161 /*
162 * Calculate time in cur process. Time is sum of user time
Rob Landley997650b2006-04-24 23:13:46 +0000163 * and system time
Rob Landleyb2804552006-02-13 22:04:27 +0000164 */
Rob Landleyb2804552006-02-13 22:04:27 +0000165 pid = cur->pid;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000166 new_hist[n].ticks = cur->ticks;
Rob Landley997650b2006-04-24 23:13:46 +0000167 new_hist[n].pid = pid;
Rob Landleyb2804552006-02-13 22:04:27 +0000168
169 /* find matching entry from previous pass */
Rob Landley997650b2006-04-24 23:13:46 +0000170 cur->pcpu = 0;
171 /* do not start at index 0, continue at last used one
172 * (brought hist_iterations from ~14000 down to 172) */
173 last_i = i;
174 if (prev_hist_count) do {
175 if (prev_hist[i].pid == pid) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000176 cur->pcpu = cur->ticks - prev_hist[i].ticks;
177 total_pcpu += cur->pcpu;
Rob Landleyb2804552006-02-13 22:04:27 +0000178 break;
179 }
Rob Landley997650b2006-04-24 23:13:46 +0000180 i = (i+1) % prev_hist_count;
181 /* hist_iterations++; */
182 } while (i != last_i);
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000183 /* total_vsz += cur->vsz; */
Eric Andersen08a72202002-09-30 20:52:10 +0000184 }
185
186 /*
Rob Landleyb2804552006-02-13 22:04:27 +0000187 * Save cur frame's information.
Eric Andersen08a72202002-09-30 20:52:10 +0000188 */
Rob Landley997650b2006-04-24 23:13:46 +0000189 free(prev_hist);
190 prev_hist = new_hist;
191 prev_hist_count = ntop;
Eric Andersen08a72202002-09-30 20:52:10 +0000192}
Denis Vlasenkofa076802006-11-05 00:38:51 +0000193#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Eric Andersen08a72202002-09-30 20:52:10 +0000194
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000195
Eric Andersen420b2082002-09-17 22:14:58 +0000196/* display generic info (meminfo / loadavg) */
Rob Landley997650b2006-04-24 23:13:46 +0000197static unsigned long display_generic(int scr_width)
Eric Andersen420b2082002-09-17 22:14:58 +0000198{
199 FILE *fp;
200 char buf[80];
Rob Landley997650b2006-04-24 23:13:46 +0000201 char scrbuf[80];
202 char *end;
Eric Andersen420b2082002-09-17 22:14:58 +0000203 unsigned long total, used, mfree, shared, buffers, cached;
Eric Andersen7857c032003-10-11 18:47:20 +0000204 unsigned int needs_conversion = 1;
Eric Andersen420b2082002-09-17 22:14:58 +0000205
206 /* read memory info */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000207 fp = xfopen("meminfo", "r");
Eric Andersen420b2082002-09-17 22:14:58 +0000208
Eric Andersen7857c032003-10-11 18:47:20 +0000209 /*
210 * Old kernels (such as 2.4.x) had a nice summary of memory info that
211 * we could parse, however this is gone entirely in 2.6. Try parsing
212 * the old way first, and if that fails, parse each field manually.
213 *
214 * First, we read in the first line. Old kernels will have bogus
215 * strings we don't care about, whereas new kernels will start right
216 * out with MemTotal:
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000217 * -- PFM.
Eric Andersen7857c032003-10-11 18:47:20 +0000218 */
219 if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000220 fgets(buf, sizeof(buf), fp); /* skip first line */
Eric Andersen7857c032003-10-11 18:47:20 +0000221
222 fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
223 &total, &used, &mfree, &shared, &buffers, &cached);
224 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000225 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000226 * Revert to manual parsing, which incidentally already has the
227 * sizes in kilobytes. This should be safe for both 2.4 and
228 * 2.6.
229 */
230 needs_conversion = 0;
231
232 fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
233
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000234 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000235 * MemShared: is no longer present in 2.6. Report this as 0,
236 * to maintain consistent behavior with normal procps.
237 */
238 if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
239 shared = 0;
240
241 fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
242 fscanf(fp, "Cached: %lu %s\n", &cached, buf);
243
244 used = total - mfree;
Eric Andersen420b2082002-09-17 22:14:58 +0000245 }
246 fclose(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000247
Rob Landley997650b2006-04-24 23:13:46 +0000248 /* read load average as a string */
Rob Landley997650b2006-04-24 23:13:46 +0000249 buf[0] = '\0';
Denis Vlasenko25d80622006-10-27 09:34:22 +0000250 open_read_close("loadavg", buf, sizeof(buf));
Rob Landley997650b2006-04-24 23:13:46 +0000251 end = strchr(buf, ' ');
252 if (end) end = strchr(end+1, ' ');
253 if (end) end = strchr(end+1, ' ');
254 if (end) *end = '\0';
Eric Andersen420b2082002-09-17 22:14:58 +0000255
Eric Andersen7857c032003-10-11 18:47:20 +0000256 if (needs_conversion) {
257 /* convert to kilobytes */
258 used /= 1024;
259 mfree /= 1024;
260 shared /= 1024;
261 buffers /= 1024;
262 cached /= 1024;
263 total /= 1024;
264 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000265
Eric Andersen420b2082002-09-17 22:14:58 +0000266 /* output memory info and load average */
Eric Andersen08a72202002-09-30 20:52:10 +0000267 /* clear screen & go to top */
Rob Landley997650b2006-04-24 23:13:46 +0000268 if (scr_width > sizeof(scrbuf))
269 scr_width = sizeof(scrbuf);
270 snprintf(scrbuf, scr_width,
271 "Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached",
Rob Landleyb2804552006-02-13 22:04:27 +0000272 used, mfree, shared, buffers, cached);
Denis Vlasenko266bc172006-09-29 17:16:39 +0000273
274 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000275
Denis Vlasenko25d80622006-10-27 09:34:22 +0000276 snprintf(scrbuf, scr_width, "Load average: %s", buf);
Rob Landley997650b2006-04-24 23:13:46 +0000277 printf("%s\n", scrbuf);
278
Eric Andersen7857c032003-10-11 18:47:20 +0000279 return total;
Eric Andersen420b2082002-09-17 22:14:58 +0000280}
281
282
283/* display process statuses */
Rob Landley997650b2006-04-24 23:13:46 +0000284static void display_status(int count, int scr_width)
Eric Andersen420b2082002-09-17 22:14:58 +0000285{
Rob Landley997650b2006-04-24 23:13:46 +0000286 enum {
287 bits_per_int = sizeof(int)*8
288 };
289
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000290 top_status_t *s = top;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000291 char vsz_str_buf[8];
292 unsigned long total_memory = display_generic(scr_width); /* or use total_vsz? */
Rob Landley997650b2006-04-24 23:13:46 +0000293 unsigned pmem_shift, pmem_scale;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000294
Denis Vlasenkofa076802006-11-05 00:38:51 +0000295#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000296 unsigned pcpu_shift, pcpu_scale;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000297 unsigned busy_jifs;
Rob Landley997650b2006-04-24 23:13:46 +0000298
Eric Andersen420b2082002-09-17 22:14:58 +0000299 /* what info of the processes is shown */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000300 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000301 " PID USER STATUS VSZ PPID %CPU %MEM COMMAND");
Rob Landley997650b2006-04-24 23:13:46 +0000302#define MIN_WIDTH \
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000303 sizeof( " PID USER STATUS VSZ PPID %CPU %MEM C")
Eric Andersen08a72202002-09-30 20:52:10 +0000304#else
Denis Vlasenko266bc172006-09-29 17:16:39 +0000305 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000306 " PID USER STATUS VSZ PPID %MEM COMMAND");
Rob Landley997650b2006-04-24 23:13:46 +0000307#define MIN_WIDTH \
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000308 sizeof( " PID USER STATUS VSZ PPID %MEM C")
Rob Landley997650b2006-04-24 23:13:46 +0000309#endif
310
311 /*
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000312 * MEM% = s->vsz/MemTotal
Rob Landley997650b2006-04-24 23:13:46 +0000313 */
314 pmem_shift = bits_per_int-11;
315 pmem_scale = 1000*(1U<<(bits_per_int-11)) / total_memory;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000316 /* s->vsz is in kb. we want (s->vsz * pmem_scale) to never overflow */
Rob Landley997650b2006-04-24 23:13:46 +0000317 while (pmem_scale >= 512) {
318 pmem_scale /= 4;
319 pmem_shift -= 2;
320 }
Denis Vlasenkofa076802006-11-05 00:38:51 +0000321#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
322 busy_jifs = jif.busy - prev_jif.busy;
323 /* This happens if there were lots of short-lived processes
324 * between two top updates (e.g. compilation) */
325 if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
326
Rob Landley997650b2006-04-24 23:13:46 +0000327 /*
328 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
329 * (pcpu is delta of sys+user time between samples)
330 */
331 /* (jif.xxx - prev_jif.xxx) and s->pcpu are
332 * in 0..~64000 range (HZ*update_interval).
333 * we assume that unsigned is at least 32-bit.
334 */
335 pcpu_shift = 6;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000336 pcpu_scale = (1000*64*(uint16_t)busy_jifs ? : 1);
Rob Landley997650b2006-04-24 23:13:46 +0000337 while (pcpu_scale < (1U<<(bits_per_int-2))) {
338 pcpu_scale *= 4;
339 pcpu_shift += 2;
340 }
341 pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1);
342 /* we want (s->pcpu * pcpu_scale) to never overflow */
343 while (pcpu_scale >= 1024) {
344 pcpu_scale /= 4;
345 pcpu_shift -= 2;
346 }
347 /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
Eric Andersen08a72202002-09-30 20:52:10 +0000348#endif
Denis Vlasenko266bc172006-09-29 17:16:39 +0000349 while (count-- > 0) {
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000350 div_t pmem = div((s->vsz*pmem_scale) >> pmem_shift, 10);
Rob Landley997650b2006-04-24 23:13:46 +0000351 int col = scr_width+1;
Bernhard Reutner-Fischere2922e42006-05-19 12:48:56 +0000352 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(div_t pcpu;)
Eric Andersen420b2082002-09-17 22:14:58 +0000353
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000354 if (s->vsz >= 100*1024)
355 sprintf(vsz_str_buf, "%6ldM", s->vsz/1024);
Manuel Novoa III d4993302002-09-18 19:27:10 +0000356 else
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000357 sprintf(vsz_str_buf, "%7ld", s->vsz);
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000358 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(
359 pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);
360 )
361 col -= printf("\n%5u %-8s %s "
362 "%s%6u"
363 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c")
364 "%3u.%c ",
365 s->pid, get_cached_username(s->uid), s->state,
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000366 vsz_str_buf, s->ppid,
Rob Landley0c430462006-05-04 19:51:22 +0000367 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,)
368 pmem.quot, '0'+pmem.rem);
Denis Vlasenko25d80622006-10-27 09:34:22 +0000369 if (col > 0)
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000370 printf("%.*s", col, s->comm);
Bernhard Reutner-Fischere2922e42006-05-19 12:48:56 +0000371 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
Rob Landley997650b2006-04-24 23:13:46 +0000372 jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
Eric Andersen420b2082002-09-17 22:14:58 +0000373 s++;
Eric Andersen08a72202002-09-30 20:52:10 +0000374 }
Rob Landley997650b2006-04-24 23:13:46 +0000375 /* printf(" %d", hist_iterations); */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000376 putchar(OPT_BATCH_MODE ? '\n' : '\r');
Rob Landley997650b2006-04-24 23:13:46 +0000377 fflush(stdout);
Eric Andersen44608e92002-10-22 12:21:15 +0000378}
379
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000380
Eric Andersen44608e92002-10-22 12:21:15 +0000381static void clearmems(void)
382{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000383 clear_username_cache();
Eric Andersen08a72202002-09-30 20:52:10 +0000384 free(top);
Eric Andersen08a72202002-09-30 20:52:10 +0000385 top = 0;
Eric Andersen08a72202002-09-30 20:52:10 +0000386 ntop = 0;
387}
388
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000389
Denis Vlasenkofa076802006-11-05 00:38:51 +0000390#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000391#include <termios.h>
Eric Andersen08a72202002-09-30 20:52:10 +0000392#include <signal.h>
393
Eric Andersen08a72202002-09-30 20:52:10 +0000394static struct termios initial_settings;
395
396static void reset_term(void)
397{
398 tcsetattr(0, TCSANOW, (void *) &initial_settings);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000399#if ENABLE_FEATURE_CLEAN_UP
Eric Andersen08a72202002-09-30 20:52:10 +0000400 clearmems();
Denis Vlasenkofa076802006-11-05 00:38:51 +0000401#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000402 free(prev_hist);
Eric Andersen08a72202002-09-30 20:52:10 +0000403#endif
Denis Vlasenkofa076802006-11-05 00:38:51 +0000404#endif /* FEATURE_CLEAN_UP */
Eric Andersen08a72202002-09-30 20:52:10 +0000405}
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000406
Rob Landleyb2804552006-02-13 22:04:27 +0000407static void sig_catcher(int sig ATTRIBUTE_UNUSED)
Eric Andersen08a72202002-09-30 20:52:10 +0000408{
409 reset_term();
Rob Landleydb1ab1a2006-06-28 14:11:25 +0000410 exit(1);
Eric Andersen08a72202002-09-30 20:52:10 +0000411}
Denis Vlasenkofa076802006-11-05 00:38:51 +0000412#endif /* FEATURE_USE_TERMIOS */
Eric Andersen08a72202002-09-30 20:52:10 +0000413
414
Denis Vlasenko06af2162007-02-03 17:28:39 +0000415int top_main(int argc, char **argv);
Eric Andersen420b2082002-09-17 22:14:58 +0000416int top_main(int argc, char **argv)
417{
Denis Vlasenko266bc172006-09-29 17:16:39 +0000418 int count, lines, col;
Denis Vlasenko13858992006-10-08 12:49:22 +0000419 unsigned interval = 5; /* default update rate is 5 seconds */
420 unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000421 char *sinterval, *siterations;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000422#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000423 struct termios new_settings;
424 struct timeval tv;
425 fd_set readfds;
426 unsigned char c;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000427#endif /* FEATURE_USE_TERMIOS */
Eric Andersen08a72202002-09-30 20:52:10 +0000428
Eric Andersen420b2082002-09-17 22:14:58 +0000429 /* do normal option parsing */
Denis Vlasenko42dfcd22006-09-09 12:55:02 +0000430 interval = 5;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000431 opt_complementary = "-";
Denis Vlasenkofa076802006-11-05 00:38:51 +0000432 getopt32(argc, argv, "d:n:b", &sinterval, &siterations);
Denis Vlasenko13858992006-10-08 12:49:22 +0000433 if (option_mask32 & 0x1) interval = xatou(sinterval); // -d
434 if (option_mask32 & 0x2) iterations = xatou(siterations); // -n
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000435 //if (option_mask32 & 0x4) // -b
Eric Andersen420b2082002-09-17 22:14:58 +0000436
Eric Andersen44608e92002-10-22 12:21:15 +0000437 /* change to /proc */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000438 xchdir("/proc");
Denis Vlasenkofa076802006-11-05 00:38:51 +0000439#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000440 tcgetattr(0, (void *) &initial_settings);
441 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
Denis Vlasenko42dfcd22006-09-09 12:55:02 +0000442 /* unbuffered input, turn off echo */
443 new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
Eric Andersen08a72202002-09-30 20:52:10 +0000444
Rob Landley997650b2006-04-24 23:13:46 +0000445 signal(SIGTERM, sig_catcher);
Rob Landleydb1ab1a2006-06-28 14:11:25 +0000446 signal(SIGINT, sig_catcher);
Eric Andersen08a72202002-09-30 20:52:10 +0000447 tcsetattr(0, TCSANOW, (void *) &new_settings);
448 atexit(reset_term);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000449#endif /* FEATURE_USE_TERMIOS */
Mike Frysinger223b8872005-07-30 09:42:05 +0000450
Denis Vlasenkofa076802006-11-05 00:38:51 +0000451#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000452 sort_function[0] = pcpu_sort;
453 sort_function[1] = mem_sort;
454 sort_function[2] = time_sort;
455#else
456 sort_function = mem_sort;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000457#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Mike Frysinger223b8872005-07-30 09:42:05 +0000458
Eric Andersen08a72202002-09-30 20:52:10 +0000459 while (1) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000460 procps_status_t *p = NULL;
Eric Andersen44608e92002-10-22 12:21:15 +0000461
Rob Landley997650b2006-04-24 23:13:46 +0000462 /* Default to 25 lines - 5 lines for status */
463 lines = 24 - 3;
464 col = 79;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000465#if ENABLE_FEATURE_USE_TERMIOS
Rob Landley997650b2006-04-24 23:13:46 +0000466 get_terminal_width_height(0, &col, &lines);
467 if (lines < 5 || col < MIN_WIDTH) {
468 sleep(interval);
469 continue;
470 }
471 lines -= 3;
Denis Vlasenkofa076802006-11-05 00:38:51 +0000472#endif /* FEATURE_USE_TERMIOS */
Rob Landley997650b2006-04-24 23:13:46 +0000473
474 /* read process IDs & status for all the processes */
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000475 while ((p = procps_scan(p, 0
476 | PSSCAN_PID
477 | PSSCAN_PPID
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000478 | PSSCAN_VSZ
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000479 | PSSCAN_STIME
480 | PSSCAN_UTIME
481 | PSSCAN_STATE
482 | PSSCAN_COMM
483 | PSSCAN_SID
484 | PSSCAN_UIDGID
485 ))) {
Eric Andersen44608e92002-10-22 12:21:15 +0000486 int n = ntop;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000487 top = xrealloc(top, (++ntop)*sizeof(top_status_t));
488 top[n].pid = p->pid;
489 top[n].ppid = p->ppid;
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000490 top[n].vsz = p->vsz;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000491#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000492 top[n].ticks = p->stime + p->utime;
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000493#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000494 top[n].uid = p->uid;
495 strcpy(top[n].state, p->state);
496 strcpy(top[n].comm, p->comm);
Eric Andersen44608e92002-10-22 12:21:15 +0000497 }
498 if (ntop == 0) {
Denis Vlasenkoea620772006-10-14 02:23:43 +0000499 bb_error_msg_and_die("can't find process info in /proc");
Rob Landleyb2804552006-02-13 22:04:27 +0000500 }
Denis Vlasenkofa076802006-11-05 00:38:51 +0000501#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000502 if (!prev_hist_count) {
Eric Andersen08a72202002-09-30 20:52:10 +0000503 do_stats();
504 sleep(1);
505 clearmems();
506 continue;
Rob Landleyb2804552006-02-13 22:04:27 +0000507 }
Eric Andersen08a72202002-09-30 20:52:10 +0000508 do_stats();
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000509 qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
Eric Andersen08a72202002-09-30 20:52:10 +0000510#else
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000511 qsort(top, ntop, sizeof(top_status_t), (void*)sort_function);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000512#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000513 count = lines;
Denis Vlasenko25d80622006-10-27 09:34:22 +0000514 if (OPT_BATCH_MODE || count > ntop) {
Denis Vlasenko266bc172006-09-29 17:16:39 +0000515 count = ntop;
Eric Andersen08a72202002-09-30 20:52:10 +0000516 }
517 /* show status for each of the processes */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000518 display_status(count, col);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000519#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000520 tv.tv_sec = interval;
521 tv.tv_usec = 0;
Rob Landley997650b2006-04-24 23:13:46 +0000522 FD_ZERO(&readfds);
523 FD_SET(0, &readfds);
524 select(1, &readfds, NULL, NULL, &tv);
525 if (FD_ISSET(0, &readfds)) {
526 if (read(0, &c, 1) <= 0) { /* signal */
Mike Frysinger223b8872005-07-30 09:42:05 +0000527 return EXIT_FAILURE;
528 }
Rob Landley997650b2006-04-24 23:13:46 +0000529 if (c == 'q' || c == initial_settings.c_cc[VINTR])
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000530 break;
Rob Landley997650b2006-04-24 23:13:46 +0000531 if (c == 'M') {
Denis Vlasenkofa076802006-11-05 00:38:51 +0000532#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000533 sort_function[0] = mem_sort;
534 sort_function[1] = pcpu_sort;
535 sort_function[2] = time_sort;
536#else
537 sort_function = mem_sort;
538#endif
539 }
Denis Vlasenkofa076802006-11-05 00:38:51 +0000540#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000541 if (c == 'P') {
Eric Andersen08a72202002-09-30 20:52:10 +0000542 sort_function[0] = pcpu_sort;
543 sort_function[1] = mem_sort;
544 sort_function[2] = time_sort;
545 }
Rob Landley997650b2006-04-24 23:13:46 +0000546 if (c == 'T') {
Eric Andersen08a72202002-09-30 20:52:10 +0000547 sort_function[0] = time_sort;
548 sort_function[1] = mem_sort;
549 sort_function[2] = pcpu_sort;
550 }
551#endif
Rob Landley997650b2006-04-24 23:13:46 +0000552 if (c == 'N') {
Denis Vlasenkofa076802006-11-05 00:38:51 +0000553#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000554 sort_function[0] = pid_sort;
555#else
556 sort_function = pid_sort;
557#endif
558 }
559 }
Denis Vlasenko266bc172006-09-29 17:16:39 +0000560 if (!--iterations)
561 break;
Eric Andersen08a72202002-09-30 20:52:10 +0000562#else
Eric Andersen420b2082002-09-17 22:14:58 +0000563 sleep(interval);
Denis Vlasenkofa076802006-11-05 00:38:51 +0000564#endif /* FEATURE_USE_TERMIOS */
Eric Andersen08a72202002-09-30 20:52:10 +0000565 clearmems();
Eric Andersen420b2082002-09-17 22:14:58 +0000566 }
Rob Landley997650b2006-04-24 23:13:46 +0000567 if (ENABLE_FEATURE_CLEAN_UP)
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000568 clearmems();
569 putchar('\n');
Eric Andersen420b2082002-09-17 22:14:58 +0000570 return EXIT_SUCCESS;
571}