blob: 691b4ea7e924c66b6f007bbed1a90e8388eeef82 [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
31#include <sys/types.h>
Eric Andersen420b2082002-09-17 22:14:58 +000032#include <stdio.h>
33#include <stdlib.h>
34#include <unistd.h>
Eric Andersen420b2082002-09-17 22:14:58 +000035#include <string.h>
36#include <sys/ioctl.h>
37#include "busybox.h"
38
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +000039//#define CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE /* + 2k */
Eric Andersen420b2082002-09-17 22:14:58 +000040
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +000041#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +000042#include <time.h>
43#include <sys/time.h>
44#include <fcntl.h>
45#include <netinet/in.h> /* htons */
46#endif
47
48
Eric Andersen44608e92002-10-22 12:21:15 +000049typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q);
Eric Andersen08a72202002-09-30 20:52:10 +000050
Eric Andersen44608e92002-10-22 12:21:15 +000051static procps_status_t *top; /* Hehe */
Eric Andersen08a72202002-09-30 20:52:10 +000052static int ntop;
53
Mike Frysinger223b8872005-07-30 09:42:05 +000054#ifdef CONFIG_FEATURE_USE_TERMIOS
Rob Landley997650b2006-04-24 23:13:46 +000055static int pid_sort(procps_status_t *P, procps_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000056{
Rob Landleyb2804552006-02-13 22:04:27 +000057 return (Q->pid - P->pid);
Eric Andersen08a72202002-09-30 20:52:10 +000058}
Mike Frysinger223b8872005-07-30 09:42:05 +000059#endif
Eric Andersen08a72202002-09-30 20:52:10 +000060
Rob Landley997650b2006-04-24 23:13:46 +000061static int mem_sort(procps_status_t *P, procps_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000062{
Rob Landleyb2804552006-02-13 22:04:27 +000063 return (int)(Q->rss - P->rss);
Eric Andersen08a72202002-09-30 20:52:10 +000064}
65
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +000066#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +000067
68#define sort_depth 3
69static cmp_t sort_function[sort_depth];
70
Rob Landley997650b2006-04-24 23:13:46 +000071static int pcpu_sort(procps_status_t *P, procps_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000072{
Rob Landleyb2804552006-02-13 22:04:27 +000073 return (Q->pcpu - P->pcpu);
Eric Andersen08a72202002-09-30 20:52:10 +000074}
75
Rob Landley997650b2006-04-24 23:13:46 +000076static int time_sort(procps_status_t *P, procps_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000077{
Rob Landleyb2804552006-02-13 22:04:27 +000078 return (int)((Q->stime + Q->utime) - (P->stime + P->utime));
Eric Andersen08a72202002-09-30 20:52:10 +000079}
80
Eric Andersen14f5c8d2005-04-16 19:39:00 +000081static int mult_lvl_cmp(void* a, void* b) {
Rob Landleyb2804552006-02-13 22:04:27 +000082 int i, cmp_val;
Eric Andersen08a72202002-09-30 20:52:10 +000083
Rob Landley997650b2006-04-24 23:13:46 +000084 for (i = 0; i < sort_depth; i++) {
Rob Landleyb2804552006-02-13 22:04:27 +000085 cmp_val = (*sort_function[i])(a, b);
86 if (cmp_val != 0)
87 return cmp_val;
88 }
89 return 0;
Eric Andersen08a72202002-09-30 20:52:10 +000090}
91
92/* This structure stores some critical information from one frame to
93 the next. mostly used for sorting. Added cumulative and resident fields. */
94struct save_hist {
Rob Landleyb2804552006-02-13 22:04:27 +000095 int ticks;
96 int pid;
Eric Andersen08a72202002-09-30 20:52:10 +000097};
98
99/*
100 * Calculates percent cpu usage for each task.
101 */
102
Rob Landley997650b2006-04-24 23:13:46 +0000103static struct save_hist *prev_hist;
104static int prev_hist_count;
105/* static int hist_iterations; */
Eric Andersen08a72202002-09-30 20:52:10 +0000106
Eric Andersen08a72202002-09-30 20:52:10 +0000107
Rob Landley997650b2006-04-24 23:13:46 +0000108static unsigned total_pcpu;
109/* static unsigned long total_rss; */
Eric Andersen08a72202002-09-30 20:52:10 +0000110
Rob Landley997650b2006-04-24 23:13:46 +0000111struct jiffy_counts {
112 unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
113 unsigned long long total;
114 unsigned long long busy;
115};
116static struct jiffy_counts jif, prev_jif;
117
118static void get_jiffy_counts(void)
Rob Landleyb2804552006-02-13 22:04:27 +0000119{
Rob Landley997650b2006-04-24 23:13:46 +0000120 FILE* fp = bb_xfopen("stat", "r");
121 prev_jif = jif;
122 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
123 &jif.usr,&jif.nic,&jif.sys,&jif.idle,
124 &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
125 bb_error_msg_and_die("failed to read 'stat'");
Rob Landleyb2804552006-02-13 22:04:27 +0000126 }
Rob Landley997650b2006-04-24 23:13:46 +0000127 fclose(fp);
128 jif.total = jif.usr + jif.nic + jif.sys + jif.idle
129 + jif.iowait + jif.irq + jif.softirq + jif.steal;
130 /* procps 2.x does not count iowait as busy time */
131 jif.busy = jif.total - jif.idle - jif.iowait;
Eric Andersen08a72202002-09-30 20:52:10 +0000132}
133
134static void do_stats(void)
135{
Rob Landleyb2804552006-02-13 22:04:27 +0000136 procps_status_t *cur;
Rob Landley997650b2006-04-24 23:13:46 +0000137 int pid, total_time, i, last_i, n;
138 struct save_hist *new_hist;
Eric Andersen08a72202002-09-30 20:52:10 +0000139
Rob Landley997650b2006-04-24 23:13:46 +0000140 get_jiffy_counts();
141 total_pcpu = 0;
142 /* total_rss = 0; */
143 new_hist = xmalloc(sizeof(struct save_hist)*ntop);
Rob Landleyb2804552006-02-13 22:04:27 +0000144 /*
145 * Make a pass through the data to get stats.
146 */
Rob Landley997650b2006-04-24 23:13:46 +0000147 /* hist_iterations = 0; */
148 i = 0;
149 for (n = 0; n < ntop; n++) {
Rob Landleyb2804552006-02-13 22:04:27 +0000150 cur = top + n;
151
152 /*
153 * Calculate time in cur process. Time is sum of user time
Rob Landley997650b2006-04-24 23:13:46 +0000154 * and system time
Rob Landleyb2804552006-02-13 22:04:27 +0000155 */
Rob Landleyb2804552006-02-13 22:04:27 +0000156 pid = cur->pid;
Rob Landley997650b2006-04-24 23:13:46 +0000157 total_time = cur->stime + cur->utime;
158 new_hist[n].ticks = total_time;
159 new_hist[n].pid = pid;
Rob Landleyb2804552006-02-13 22:04:27 +0000160
161 /* find matching entry from previous pass */
Rob Landley997650b2006-04-24 23:13:46 +0000162 cur->pcpu = 0;
163 /* do not start at index 0, continue at last used one
164 * (brought hist_iterations from ~14000 down to 172) */
165 last_i = i;
166 if (prev_hist_count) do {
167 if (prev_hist[i].pid == pid) {
168 cur->pcpu = total_time - prev_hist[i].ticks;
Rob Landleyb2804552006-02-13 22:04:27 +0000169 break;
170 }
Rob Landley997650b2006-04-24 23:13:46 +0000171 i = (i+1) % prev_hist_count;
172 /* hist_iterations++; */
173 } while (i != last_i);
174 total_pcpu += cur->pcpu;
175 /* total_rss += cur->rss; */
Eric Andersen08a72202002-09-30 20:52:10 +0000176 }
177
178 /*
Rob Landleyb2804552006-02-13 22:04:27 +0000179 * Save cur frame's information.
Eric Andersen08a72202002-09-30 20:52:10 +0000180 */
Rob Landley997650b2006-04-24 23:13:46 +0000181 free(prev_hist);
182 prev_hist = new_hist;
183 prev_hist_count = ntop;
Eric Andersen08a72202002-09-30 20:52:10 +0000184}
185#else
186static cmp_t sort_function;
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000187#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Eric Andersen08a72202002-09-30 20:52:10 +0000188
Eric Andersen420b2082002-09-17 22:14:58 +0000189/* display generic info (meminfo / loadavg) */
Rob Landley997650b2006-04-24 23:13:46 +0000190static unsigned long display_generic(int scr_width)
Eric Andersen420b2082002-09-17 22:14:58 +0000191{
192 FILE *fp;
193 char buf[80];
Rob Landley997650b2006-04-24 23:13:46 +0000194 char scrbuf[80];
195 char *end;
Eric Andersen420b2082002-09-17 22:14:58 +0000196 unsigned long total, used, mfree, shared, buffers, cached;
Eric Andersen7857c032003-10-11 18:47:20 +0000197 unsigned int needs_conversion = 1;
Eric Andersen420b2082002-09-17 22:14:58 +0000198
199 /* read memory info */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000200 fp = bb_xfopen("meminfo", "r");
Eric Andersen420b2082002-09-17 22:14:58 +0000201
Eric Andersen7857c032003-10-11 18:47:20 +0000202 /*
203 * Old kernels (such as 2.4.x) had a nice summary of memory info that
204 * we could parse, however this is gone entirely in 2.6. Try parsing
205 * the old way first, and if that fails, parse each field manually.
206 *
207 * First, we read in the first line. Old kernels will have bogus
208 * strings we don't care about, whereas new kernels will start right
209 * out with MemTotal:
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000210 * -- PFM.
Eric Andersen7857c032003-10-11 18:47:20 +0000211 */
212 if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000213 fgets(buf, sizeof(buf), fp); /* skip first line */
Eric Andersen7857c032003-10-11 18:47:20 +0000214
215 fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
216 &total, &used, &mfree, &shared, &buffers, &cached);
217 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000218 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000219 * Revert to manual parsing, which incidentally already has the
220 * sizes in kilobytes. This should be safe for both 2.4 and
221 * 2.6.
222 */
223 needs_conversion = 0;
224
225 fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
226
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000227 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000228 * MemShared: is no longer present in 2.6. Report this as 0,
229 * to maintain consistent behavior with normal procps.
230 */
231 if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
232 shared = 0;
233
234 fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
235 fscanf(fp, "Cached: %lu %s\n", &cached, buf);
236
237 used = total - mfree;
Eric Andersen420b2082002-09-17 22:14:58 +0000238 }
239 fclose(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000240
Rob Landley997650b2006-04-24 23:13:46 +0000241 /* read load average as a string */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000242 fp = bb_xfopen("loadavg", "r");
Rob Landley997650b2006-04-24 23:13:46 +0000243 buf[0] = '\0';
244 fgets(buf, sizeof(buf), fp);
245 end = strchr(buf, ' ');
246 if (end) end = strchr(end+1, ' ');
247 if (end) end = strchr(end+1, ' ');
248 if (end) *end = '\0';
Eric Andersen420b2082002-09-17 22:14:58 +0000249 fclose(fp);
250
Eric Andersen7857c032003-10-11 18:47:20 +0000251 if (needs_conversion) {
252 /* convert to kilobytes */
253 used /= 1024;
254 mfree /= 1024;
255 shared /= 1024;
256 buffers /= 1024;
257 cached /= 1024;
258 total /= 1024;
259 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000260
Eric Andersen420b2082002-09-17 22:14:58 +0000261 /* output memory info and load average */
Eric Andersen08a72202002-09-30 20:52:10 +0000262 /* clear screen & go to top */
Rob Landley997650b2006-04-24 23:13:46 +0000263 if (scr_width > sizeof(scrbuf))
264 scr_width = sizeof(scrbuf);
265 snprintf(scrbuf, scr_width,
266 "Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached",
Rob Landleyb2804552006-02-13 22:04:27 +0000267 used, mfree, shared, buffers, cached);
Rob Landley997650b2006-04-24 23:13:46 +0000268 printf("\e[H\e[J%s\n", scrbuf);
269 snprintf(scrbuf, scr_width,
270 "Load average: %s (Status: S=sleeping R=running, W=waiting)", buf);
271 printf("%s\n", scrbuf);
272
Eric Andersen7857c032003-10-11 18:47:20 +0000273 return total;
Eric Andersen420b2082002-09-17 22:14:58 +0000274}
275
276
277/* display process statuses */
Rob Landley997650b2006-04-24 23:13:46 +0000278static void display_status(int count, int scr_width)
Eric Andersen420b2082002-09-17 22:14:58 +0000279{
Rob Landley997650b2006-04-24 23:13:46 +0000280 enum {
281 bits_per_int = sizeof(int)*8
282 };
283
Eric Andersen44608e92002-10-22 12:21:15 +0000284 procps_status_t *s = top;
Eric Andersen08a72202002-09-30 20:52:10 +0000285 char rss_str_buf[8];
Rob Landley997650b2006-04-24 23:13:46 +0000286 unsigned long total_memory = display_generic(scr_width); /* or use total_rss? */
287 unsigned pmem_shift, pmem_scale;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000288
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000289#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000290 unsigned pcpu_shift, pcpu_scale;
291
Eric Andersen420b2082002-09-17 22:14:58 +0000292 /* what info of the processes is shown */
Rob Landley997650b2006-04-24 23:13:46 +0000293 printf("\e[7m%.*s\e[0m", scr_width,
294 " PID USER STATUS RSS PPID %CPU %MEM COMMAND");
295#define MIN_WIDTH \
296 sizeof( " PID USER STATUS RSS PPID %CPU %MEM C")
Eric Andersen08a72202002-09-30 20:52:10 +0000297#else
Rob Landley997650b2006-04-24 23:13:46 +0000298 printf("\e[7m%.*s\e[0m", scr_width,
299 " PID USER STATUS RSS PPID %MEM COMMAND");
300#define MIN_WIDTH \
301 sizeof( " PID USER STATUS RSS PPID %MEM C")
302#endif
303
304 /*
305 * MEM% = s->rss/MemTotal
306 */
307 pmem_shift = bits_per_int-11;
308 pmem_scale = 1000*(1U<<(bits_per_int-11)) / total_memory;
309 /* s->rss is in kb. we want (s->rss * pmem_scale) to never overflow */
310 while (pmem_scale >= 512) {
311 pmem_scale /= 4;
312 pmem_shift -= 2;
313 }
314#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
315 /*
316 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
317 * (pcpu is delta of sys+user time between samples)
318 */
319 /* (jif.xxx - prev_jif.xxx) and s->pcpu are
320 * in 0..~64000 range (HZ*update_interval).
321 * we assume that unsigned is at least 32-bit.
322 */
323 pcpu_shift = 6;
324 pcpu_scale = (1000*64*(uint16_t)(jif.busy-prev_jif.busy) ? : 1);
325 while (pcpu_scale < (1U<<(bits_per_int-2))) {
326 pcpu_scale *= 4;
327 pcpu_shift += 2;
328 }
329 pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1);
330 /* we want (s->pcpu * pcpu_scale) to never overflow */
331 while (pcpu_scale >= 1024) {
332 pcpu_scale /= 4;
333 pcpu_shift -= 2;
334 }
335 /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
Eric Andersen08a72202002-09-30 20:52:10 +0000336#endif
Eric Andersen420b2082002-09-17 22:14:58 +0000337
338 while (count--) {
Rob Landley997650b2006-04-24 23:13:46 +0000339 div_t pmem = div( (s->rss*pmem_scale) >> pmem_shift, 10);
340 int col = scr_width+1;
Bernhard Reutner-Fischere2922e42006-05-19 12:48:56 +0000341 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(div_t pcpu;)
Eric Andersen420b2082002-09-17 22:14:58 +0000342
Rob Landley997650b2006-04-24 23:13:46 +0000343 if (s->rss >= 100*1024)
Eric Andersen08a72202002-09-30 20:52:10 +0000344 sprintf(rss_str_buf, "%6ldM", s->rss/1024);
Manuel Novoa III d4993302002-09-18 19:27:10 +0000345 else
Eric Andersen08a72202002-09-30 20:52:10 +0000346 sprintf(rss_str_buf, "%7ld", s->rss);
Bernhard Reutner-Fischere2922e42006-05-19 12:48:56 +0000347 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);)
Rob Landley0c430462006-05-04 19:51:22 +0000348 col -= printf("\n%5d %-8s %s %s%6d%3u.%c" \
Bernhard Reutner-Fischere2922e42006-05-19 12:48:56 +0000349 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c") " ",
Rob Landley997650b2006-04-24 23:13:46 +0000350 s->pid, s->user, s->state, rss_str_buf, s->ppid,
Rob Landley0c430462006-05-04 19:51:22 +0000351 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,)
352 pmem.quot, '0'+pmem.rem);
Rob Landley997650b2006-04-24 23:13:46 +0000353 if (col>0)
354 printf("%.*s", col, s->short_cmd);
Bernhard Reutner-Fischere2922e42006-05-19 12:48:56 +0000355 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
Rob Landley997650b2006-04-24 23:13:46 +0000356 jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
Eric Andersen420b2082002-09-17 22:14:58 +0000357 s++;
Eric Andersen08a72202002-09-30 20:52:10 +0000358 }
Rob Landley997650b2006-04-24 23:13:46 +0000359 /* printf(" %d", hist_iterations); */
360 putchar('\r');
361 fflush(stdout);
Eric Andersen44608e92002-10-22 12:21:15 +0000362}
363
364static void clearmems(void)
365{
Eric Andersen08a72202002-09-30 20:52:10 +0000366 free(top);
Eric Andersen08a72202002-09-30 20:52:10 +0000367 top = 0;
Eric Andersen08a72202002-09-30 20:52:10 +0000368 ntop = 0;
369}
370
Mike Frysinger223b8872005-07-30 09:42:05 +0000371#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000372#include <termios.h>
373#include <sys/time.h>
374#include <signal.h>
375
376
377static struct termios initial_settings;
378
379static void reset_term(void)
380{
381 tcsetattr(0, TCSANOW, (void *) &initial_settings);
382#ifdef CONFIG_FEATURE_CLEAN_UP
383 clearmems();
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000384#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000385 free(prev_hist);
Eric Andersen08a72202002-09-30 20:52:10 +0000386#endif
387#endif /* CONFIG_FEATURE_CLEAN_UP */
388}
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000389
Rob Landleyb2804552006-02-13 22:04:27 +0000390static void sig_catcher(int sig ATTRIBUTE_UNUSED)
Eric Andersen08a72202002-09-30 20:52:10 +0000391{
392 reset_term();
393}
394#endif /* CONFIG_FEATURE_USE_TERMIOS */
395
396
Eric Andersen420b2082002-09-17 22:14:58 +0000397int top_main(int argc, char **argv)
398{
Eric Andersen08a72202002-09-30 20:52:10 +0000399 int opt, interval, lines, col;
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000400 char *sinterval;
Mike Frysinger223b8872005-07-30 09:42:05 +0000401#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000402 struct termios new_settings;
403 struct timeval tv;
404 fd_set readfds;
405 unsigned char c;
406 struct sigaction sa;
Eric Andersen08a72202002-09-30 20:52:10 +0000407#endif /* CONFIG_FEATURE_USE_TERMIOS */
408
Eric Andersen420b2082002-09-17 22:14:58 +0000409 /* do normal option parsing */
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000410 opt = bb_getopt_ulflags(argc, argv, "d:", &sinterval);
Rob Landley997650b2006-04-24 23:13:46 +0000411 if ((opt & 1)) {
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000412 interval = atoi(sinterval);
413 } else {
414 /* Default update rate is 5 seconds */
415 interval = 5;
Eric Andersen420b2082002-09-17 22:14:58 +0000416 }
417
Eric Andersen44608e92002-10-22 12:21:15 +0000418 /* change to /proc */
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +0000419 bb_xchdir("/proc");
Mike Frysinger223b8872005-07-30 09:42:05 +0000420#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000421 tcgetattr(0, (void *) &initial_settings);
422 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
423 new_settings.c_lflag &= ~(ISIG | ICANON); /* unbuffered input */
424 /* Turn off echoing */
425 new_settings.c_lflag &= ~(ECHO | ECHONL);
426
Rob Landley997650b2006-04-24 23:13:46 +0000427 signal(SIGTERM, sig_catcher);
428 sigaction(SIGTERM, (struct sigaction *) 0, &sa);
Eric Andersen08a72202002-09-30 20:52:10 +0000429 sa.sa_flags |= SA_RESTART;
430 sa.sa_flags &= ~SA_INTERRUPT;
Rob Landley997650b2006-04-24 23:13:46 +0000431 sigaction(SIGTERM, &sa, (struct sigaction *) 0);
432 sigaction(SIGINT, &sa, (struct sigaction *) 0);
Eric Andersen08a72202002-09-30 20:52:10 +0000433 tcsetattr(0, TCSANOW, (void *) &new_settings);
434 atexit(reset_term);
Eric Andersen08a72202002-09-30 20:52:10 +0000435#endif /* CONFIG_FEATURE_USE_TERMIOS */
Mike Frysinger223b8872005-07-30 09:42:05 +0000436
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000437#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000438 sort_function[0] = pcpu_sort;
439 sort_function[1] = mem_sort;
440 sort_function[2] = time_sort;
441#else
442 sort_function = mem_sort;
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000443#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Mike Frysinger223b8872005-07-30 09:42:05 +0000444
Eric Andersen08a72202002-09-30 20:52:10 +0000445 while (1) {
Rob Landley997650b2006-04-24 23:13:46 +0000446 procps_status_t *p;
Eric Andersen44608e92002-10-22 12:21:15 +0000447
Rob Landley997650b2006-04-24 23:13:46 +0000448 /* Default to 25 lines - 5 lines for status */
449 lines = 24 - 3;
450 col = 79;
451#ifdef CONFIG_FEATURE_USE_TERMIOS
452 get_terminal_width_height(0, &col, &lines);
453 if (lines < 5 || col < MIN_WIDTH) {
454 sleep(interval);
455 continue;
456 }
457 lines -= 3;
458#endif /* CONFIG_FEATURE_USE_TERMIOS */
459
460 /* read process IDs & status for all the processes */
Eric Andersen44608e92002-10-22 12:21:15 +0000461 while ((p = procps_scan(0)) != 0) {
462 int n = ntop;
463
464 top = xrealloc(top, (++ntop)*sizeof(procps_status_t));
465 memcpy(top + n, p, sizeof(procps_status_t));
466 }
467 if (ntop == 0) {
Rob Landleyb2804552006-02-13 22:04:27 +0000468 bb_error_msg_and_die("Can't find process info in /proc");
469 }
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000470#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000471 if (!prev_hist_count) {
Eric Andersen08a72202002-09-30 20:52:10 +0000472 do_stats();
473 sleep(1);
474 clearmems();
475 continue;
Rob Landleyb2804552006-02-13 22:04:27 +0000476 }
Eric Andersen08a72202002-09-30 20:52:10 +0000477 do_stats();
Rob Landley997650b2006-04-24 23:13:46 +0000478 qsort(top, ntop, sizeof(procps_status_t), (void*)mult_lvl_cmp);
Eric Andersen08a72202002-09-30 20:52:10 +0000479#else
Eric Andersen44608e92002-10-22 12:21:15 +0000480 qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function);
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000481#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Eric Andersen08a72202002-09-30 20:52:10 +0000482 opt = lines;
483 if (opt > ntop) {
484 opt = ntop;
485 }
486 /* show status for each of the processes */
487 display_status(opt, col);
Mike Frysinger223b8872005-07-30 09:42:05 +0000488#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000489 tv.tv_sec = interval;
490 tv.tv_usec = 0;
Rob Landley997650b2006-04-24 23:13:46 +0000491 FD_ZERO(&readfds);
492 FD_SET(0, &readfds);
493 select(1, &readfds, NULL, NULL, &tv);
494 if (FD_ISSET(0, &readfds)) {
495 if (read(0, &c, 1) <= 0) { /* signal */
Mike Frysinger223b8872005-07-30 09:42:05 +0000496 return EXIT_FAILURE;
497 }
Rob Landley997650b2006-04-24 23:13:46 +0000498 if (c == 'q' || c == initial_settings.c_cc[VINTR])
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000499 break;
Rob Landley997650b2006-04-24 23:13:46 +0000500 if (c == 'M') {
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000501#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000502 sort_function[0] = mem_sort;
503 sort_function[1] = pcpu_sort;
504 sort_function[2] = time_sort;
505#else
506 sort_function = mem_sort;
507#endif
508 }
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000509#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000510 if (c == 'P') {
Eric Andersen08a72202002-09-30 20:52:10 +0000511 sort_function[0] = pcpu_sort;
512 sort_function[1] = mem_sort;
513 sort_function[2] = time_sort;
514 }
Rob Landley997650b2006-04-24 23:13:46 +0000515 if (c == 'T') {
Eric Andersen08a72202002-09-30 20:52:10 +0000516 sort_function[0] = time_sort;
517 sort_function[1] = mem_sort;
518 sort_function[2] = pcpu_sort;
519 }
520#endif
Rob Landley997650b2006-04-24 23:13:46 +0000521 if (c == 'N') {
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000522#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000523 sort_function[0] = pid_sort;
524#else
525 sort_function = pid_sort;
526#endif
527 }
528 }
529#else
Eric Andersen420b2082002-09-17 22:14:58 +0000530 sleep(interval);
Mike Frysinger223b8872005-07-30 09:42:05 +0000531#endif /* CONFIG_FEATURE_USE_TERMIOS */
Eric Andersen08a72202002-09-30 20:52:10 +0000532 clearmems();
Eric Andersen420b2082002-09-17 22:14:58 +0000533 }
Rob Landley997650b2006-04-24 23:13:46 +0000534 if (ENABLE_FEATURE_CLEAN_UP)
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000535 clearmems();
536 putchar('\n');
Eric Andersen420b2082002-09-17 22:14:58 +0000537 return EXIT_SUCCESS;
538}