blob: 14a3870a1029b92e425d3737c437273bb13993b6 [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 Andersen44608e92002-10-22 12:21:15 +000033typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q);
Eric Andersen08a72202002-09-30 20:52:10 +000034
Eric Andersen44608e92002-10-22 12:21:15 +000035static procps_status_t *top; /* Hehe */
Eric Andersen08a72202002-09-30 20:52:10 +000036static int ntop;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000037#define OPT_BATCH_MODE (option_mask32 & 0x4)
Eric Andersen08a72202002-09-30 20:52:10 +000038
Mike Frysinger223b8872005-07-30 09:42:05 +000039#ifdef CONFIG_FEATURE_USE_TERMIOS
Rob Landley997650b2006-04-24 23:13:46 +000040static int pid_sort(procps_status_t *P, procps_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000041{
Rob Landleyb2804552006-02-13 22:04:27 +000042 return (Q->pid - P->pid);
Eric Andersen08a72202002-09-30 20:52:10 +000043}
Mike Frysinger223b8872005-07-30 09:42:05 +000044#endif
Eric Andersen08a72202002-09-30 20:52:10 +000045
Rob Landley997650b2006-04-24 23:13:46 +000046static int mem_sort(procps_status_t *P, procps_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000047{
Rob Landleyb2804552006-02-13 22:04:27 +000048 return (int)(Q->rss - P->rss);
Eric Andersen08a72202002-09-30 20:52:10 +000049}
50
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +000051#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +000052
53#define sort_depth 3
54static cmp_t sort_function[sort_depth];
55
Rob Landley997650b2006-04-24 23:13:46 +000056static int pcpu_sort(procps_status_t *P, procps_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000057{
Rob Landleyb2804552006-02-13 22:04:27 +000058 return (Q->pcpu - P->pcpu);
Eric Andersen08a72202002-09-30 20:52:10 +000059}
60
Rob Landley997650b2006-04-24 23:13:46 +000061static int time_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->stime + Q->utime) - (P->stime + P->utime));
Eric Andersen08a72202002-09-30 20:52:10 +000064}
65
Eric Andersen14f5c8d2005-04-16 19:39:00 +000066static int mult_lvl_cmp(void* a, void* b) {
Rob Landleyb2804552006-02-13 22:04:27 +000067 int i, cmp_val;
Eric Andersen08a72202002-09-30 20:52:10 +000068
Rob Landley997650b2006-04-24 23:13:46 +000069 for (i = 0; i < sort_depth; i++) {
Rob Landleyb2804552006-02-13 22:04:27 +000070 cmp_val = (*sort_function[i])(a, b);
71 if (cmp_val != 0)
72 return cmp_val;
73 }
74 return 0;
Eric Andersen08a72202002-09-30 20:52:10 +000075}
76
77/* This structure stores some critical information from one frame to
Denis Vlasenko42dfcd22006-09-09 12:55:02 +000078 the next. Mostly used for sorting. */
Eric Andersen08a72202002-09-30 20:52:10 +000079struct save_hist {
Rob Landleyb2804552006-02-13 22:04:27 +000080 int ticks;
81 int pid;
Eric Andersen08a72202002-09-30 20:52:10 +000082};
83
84/*
85 * Calculates percent cpu usage for each task.
86 */
87
Rob Landley997650b2006-04-24 23:13:46 +000088static struct save_hist *prev_hist;
89static int prev_hist_count;
90/* static int hist_iterations; */
Eric Andersen08a72202002-09-30 20:52:10 +000091
Eric Andersen08a72202002-09-30 20:52:10 +000092
Rob Landley997650b2006-04-24 23:13:46 +000093static unsigned total_pcpu;
94/* static unsigned long total_rss; */
Eric Andersen08a72202002-09-30 20:52:10 +000095
Rob Landley997650b2006-04-24 23:13:46 +000096struct jiffy_counts {
97 unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
98 unsigned long long total;
99 unsigned long long busy;
100};
101static struct jiffy_counts jif, prev_jif;
102
103static void get_jiffy_counts(void)
Rob Landleyb2804552006-02-13 22:04:27 +0000104{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000105 FILE* fp = xfopen("stat", "r");
Rob Landley997650b2006-04-24 23:13:46 +0000106 prev_jif = jif;
107 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
108 &jif.usr,&jif.nic,&jif.sys,&jif.idle,
109 &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
Denis Vlasenko42dfcd22006-09-09 12:55:02 +0000110 bb_error_msg_and_die("failed to read /proc/stat");
Rob Landleyb2804552006-02-13 22:04:27 +0000111 }
Rob Landley997650b2006-04-24 23:13:46 +0000112 fclose(fp);
113 jif.total = jif.usr + jif.nic + jif.sys + jif.idle
114 + jif.iowait + jif.irq + jif.softirq + jif.steal;
115 /* procps 2.x does not count iowait as busy time */
116 jif.busy = jif.total - jif.idle - jif.iowait;
Eric Andersen08a72202002-09-30 20:52:10 +0000117}
118
119static void do_stats(void)
120{
Rob Landleyb2804552006-02-13 22:04:27 +0000121 procps_status_t *cur;
Rob Landley997650b2006-04-24 23:13:46 +0000122 int pid, total_time, i, last_i, n;
123 struct save_hist *new_hist;
Eric Andersen08a72202002-09-30 20:52:10 +0000124
Rob Landley997650b2006-04-24 23:13:46 +0000125 get_jiffy_counts();
126 total_pcpu = 0;
127 /* total_rss = 0; */
128 new_hist = xmalloc(sizeof(struct save_hist)*ntop);
Rob Landleyb2804552006-02-13 22:04:27 +0000129 /*
130 * Make a pass through the data to get stats.
131 */
Rob Landley997650b2006-04-24 23:13:46 +0000132 /* hist_iterations = 0; */
133 i = 0;
134 for (n = 0; n < ntop; n++) {
Rob Landleyb2804552006-02-13 22:04:27 +0000135 cur = top + n;
136
137 /*
138 * Calculate time in cur process. Time is sum of user time
Rob Landley997650b2006-04-24 23:13:46 +0000139 * and system time
Rob Landleyb2804552006-02-13 22:04:27 +0000140 */
Rob Landleyb2804552006-02-13 22:04:27 +0000141 pid = cur->pid;
Rob Landley997650b2006-04-24 23:13:46 +0000142 total_time = cur->stime + cur->utime;
143 new_hist[n].ticks = total_time;
144 new_hist[n].pid = pid;
Rob Landleyb2804552006-02-13 22:04:27 +0000145
146 /* find matching entry from previous pass */
Rob Landley997650b2006-04-24 23:13:46 +0000147 cur->pcpu = 0;
148 /* do not start at index 0, continue at last used one
149 * (brought hist_iterations from ~14000 down to 172) */
150 last_i = i;
151 if (prev_hist_count) do {
152 if (prev_hist[i].pid == pid) {
153 cur->pcpu = total_time - prev_hist[i].ticks;
Rob Landleyb2804552006-02-13 22:04:27 +0000154 break;
155 }
Rob Landley997650b2006-04-24 23:13:46 +0000156 i = (i+1) % prev_hist_count;
157 /* hist_iterations++; */
158 } while (i != last_i);
159 total_pcpu += cur->pcpu;
160 /* total_rss += cur->rss; */
Eric Andersen08a72202002-09-30 20:52:10 +0000161 }
162
163 /*
Rob Landleyb2804552006-02-13 22:04:27 +0000164 * Save cur frame's information.
Eric Andersen08a72202002-09-30 20:52:10 +0000165 */
Rob Landley997650b2006-04-24 23:13:46 +0000166 free(prev_hist);
167 prev_hist = new_hist;
168 prev_hist_count = ntop;
Eric Andersen08a72202002-09-30 20:52:10 +0000169}
170#else
171static cmp_t sort_function;
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000172#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Eric Andersen08a72202002-09-30 20:52:10 +0000173
Eric Andersen420b2082002-09-17 22:14:58 +0000174/* display generic info (meminfo / loadavg) */
Rob Landley997650b2006-04-24 23:13:46 +0000175static unsigned long display_generic(int scr_width)
Eric Andersen420b2082002-09-17 22:14:58 +0000176{
177 FILE *fp;
178 char buf[80];
Rob Landley997650b2006-04-24 23:13:46 +0000179 char scrbuf[80];
180 char *end;
Eric Andersen420b2082002-09-17 22:14:58 +0000181 unsigned long total, used, mfree, shared, buffers, cached;
Eric Andersen7857c032003-10-11 18:47:20 +0000182 unsigned int needs_conversion = 1;
Eric Andersen420b2082002-09-17 22:14:58 +0000183
184 /* read memory info */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000185 fp = xfopen("meminfo", "r");
Eric Andersen420b2082002-09-17 22:14:58 +0000186
Eric Andersen7857c032003-10-11 18:47:20 +0000187 /*
188 * Old kernels (such as 2.4.x) had a nice summary of memory info that
189 * we could parse, however this is gone entirely in 2.6. Try parsing
190 * the old way first, and if that fails, parse each field manually.
191 *
192 * First, we read in the first line. Old kernels will have bogus
193 * strings we don't care about, whereas new kernels will start right
194 * out with MemTotal:
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000195 * -- PFM.
Eric Andersen7857c032003-10-11 18:47:20 +0000196 */
197 if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000198 fgets(buf, sizeof(buf), fp); /* skip first line */
Eric Andersen7857c032003-10-11 18:47:20 +0000199
200 fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
201 &total, &used, &mfree, &shared, &buffers, &cached);
202 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000203 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000204 * Revert to manual parsing, which incidentally already has the
205 * sizes in kilobytes. This should be safe for both 2.4 and
206 * 2.6.
207 */
208 needs_conversion = 0;
209
210 fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
211
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000212 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000213 * MemShared: is no longer present in 2.6. Report this as 0,
214 * to maintain consistent behavior with normal procps.
215 */
216 if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
217 shared = 0;
218
219 fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
220 fscanf(fp, "Cached: %lu %s\n", &cached, buf);
221
222 used = total - mfree;
Eric Andersen420b2082002-09-17 22:14:58 +0000223 }
224 fclose(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000225
Rob Landley997650b2006-04-24 23:13:46 +0000226 /* read load average as a string */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000227 fp = xfopen("loadavg", "r");
Rob Landley997650b2006-04-24 23:13:46 +0000228 buf[0] = '\0';
229 fgets(buf, sizeof(buf), fp);
230 end = strchr(buf, ' ');
231 if (end) end = strchr(end+1, ' ');
232 if (end) end = strchr(end+1, ' ');
233 if (end) *end = '\0';
Eric Andersen420b2082002-09-17 22:14:58 +0000234 fclose(fp);
235
Eric Andersen7857c032003-10-11 18:47:20 +0000236 if (needs_conversion) {
237 /* convert to kilobytes */
238 used /= 1024;
239 mfree /= 1024;
240 shared /= 1024;
241 buffers /= 1024;
242 cached /= 1024;
243 total /= 1024;
244 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000245
Eric Andersen420b2082002-09-17 22:14:58 +0000246 /* output memory info and load average */
Eric Andersen08a72202002-09-30 20:52:10 +0000247 /* clear screen & go to top */
Rob Landley997650b2006-04-24 23:13:46 +0000248 if (scr_width > sizeof(scrbuf))
249 scr_width = sizeof(scrbuf);
250 snprintf(scrbuf, scr_width,
251 "Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached",
Rob Landleyb2804552006-02-13 22:04:27 +0000252 used, mfree, shared, buffers, cached);
Denis Vlasenko266bc172006-09-29 17:16:39 +0000253
254 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
255
Rob Landley997650b2006-04-24 23:13:46 +0000256 snprintf(scrbuf, scr_width,
Denis Vlasenko266bc172006-09-29 17:16:39 +0000257 "Load average: %s", buf);
Rob Landley997650b2006-04-24 23:13:46 +0000258 printf("%s\n", scrbuf);
259
Eric Andersen7857c032003-10-11 18:47:20 +0000260 return total;
Eric Andersen420b2082002-09-17 22:14:58 +0000261}
262
263
264/* display process statuses */
Rob Landley997650b2006-04-24 23:13:46 +0000265static void display_status(int count, int scr_width)
Eric Andersen420b2082002-09-17 22:14:58 +0000266{
Rob Landley997650b2006-04-24 23:13:46 +0000267 enum {
268 bits_per_int = sizeof(int)*8
269 };
270
Eric Andersen44608e92002-10-22 12:21:15 +0000271 procps_status_t *s = top;
Eric Andersen08a72202002-09-30 20:52:10 +0000272 char rss_str_buf[8];
Rob Landley997650b2006-04-24 23:13:46 +0000273 unsigned long total_memory = display_generic(scr_width); /* or use total_rss? */
274 unsigned pmem_shift, pmem_scale;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000275
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000276#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000277 unsigned pcpu_shift, pcpu_scale;
278
Eric Andersen420b2082002-09-17 22:14:58 +0000279 /* what info of the processes is shown */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000280 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
Rob Landley997650b2006-04-24 23:13:46 +0000281 " PID USER STATUS RSS PPID %CPU %MEM COMMAND");
282#define MIN_WIDTH \
283 sizeof( " PID USER STATUS RSS PPID %CPU %MEM C")
Eric Andersen08a72202002-09-30 20:52:10 +0000284#else
Denis Vlasenko266bc172006-09-29 17:16:39 +0000285 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
Rob Landley997650b2006-04-24 23:13:46 +0000286 " PID USER STATUS RSS PPID %MEM COMMAND");
287#define MIN_WIDTH \
288 sizeof( " PID USER STATUS RSS PPID %MEM C")
289#endif
290
291 /*
292 * MEM% = s->rss/MemTotal
293 */
294 pmem_shift = bits_per_int-11;
295 pmem_scale = 1000*(1U<<(bits_per_int-11)) / total_memory;
296 /* s->rss is in kb. we want (s->rss * pmem_scale) to never overflow */
297 while (pmem_scale >= 512) {
298 pmem_scale /= 4;
299 pmem_shift -= 2;
300 }
301#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
302 /*
303 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
304 * (pcpu is delta of sys+user time between samples)
305 */
306 /* (jif.xxx - prev_jif.xxx) and s->pcpu are
307 * in 0..~64000 range (HZ*update_interval).
308 * we assume that unsigned is at least 32-bit.
309 */
310 pcpu_shift = 6;
311 pcpu_scale = (1000*64*(uint16_t)(jif.busy-prev_jif.busy) ? : 1);
312 while (pcpu_scale < (1U<<(bits_per_int-2))) {
313 pcpu_scale *= 4;
314 pcpu_shift += 2;
315 }
316 pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1);
317 /* we want (s->pcpu * pcpu_scale) to never overflow */
318 while (pcpu_scale >= 1024) {
319 pcpu_scale /= 4;
320 pcpu_shift -= 2;
321 }
322 /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
Eric Andersen08a72202002-09-30 20:52:10 +0000323#endif
Denis Vlasenko266bc172006-09-29 17:16:39 +0000324 if (OPT_BATCH_MODE) count--;
325 while (count-- > 0) {
326 div_t pmem = div((s->rss*pmem_scale) >> pmem_shift, 10);
Rob Landley997650b2006-04-24 23:13:46 +0000327 int col = scr_width+1;
Bernhard Reutner-Fischere2922e42006-05-19 12:48:56 +0000328 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(div_t pcpu;)
Eric Andersen420b2082002-09-17 22:14:58 +0000329
Rob Landley997650b2006-04-24 23:13:46 +0000330 if (s->rss >= 100*1024)
Eric Andersen08a72202002-09-30 20:52:10 +0000331 sprintf(rss_str_buf, "%6ldM", s->rss/1024);
Manuel Novoa III d4993302002-09-18 19:27:10 +0000332 else
Eric Andersen08a72202002-09-30 20:52:10 +0000333 sprintf(rss_str_buf, "%7ld", s->rss);
Bernhard Reutner-Fischere2922e42006-05-19 12:48:56 +0000334 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);)
Rob Landley0c430462006-05-04 19:51:22 +0000335 col -= printf("\n%5d %-8s %s %s%6d%3u.%c" \
Bernhard Reutner-Fischere2922e42006-05-19 12:48:56 +0000336 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c") " ",
Rob Landley997650b2006-04-24 23:13:46 +0000337 s->pid, s->user, s->state, rss_str_buf, s->ppid,
Rob Landley0c430462006-05-04 19:51:22 +0000338 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,)
339 pmem.quot, '0'+pmem.rem);
Rob Landley997650b2006-04-24 23:13:46 +0000340 if (col>0)
341 printf("%.*s", col, s->short_cmd);
Bernhard Reutner-Fischere2922e42006-05-19 12:48:56 +0000342 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
Rob Landley997650b2006-04-24 23:13:46 +0000343 jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
Eric Andersen420b2082002-09-17 22:14:58 +0000344 s++;
Eric Andersen08a72202002-09-30 20:52:10 +0000345 }
Rob Landley997650b2006-04-24 23:13:46 +0000346 /* printf(" %d", hist_iterations); */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000347 putchar(OPT_BATCH_MODE ? '\n' : '\r');
Rob Landley997650b2006-04-24 23:13:46 +0000348 fflush(stdout);
Eric Andersen44608e92002-10-22 12:21:15 +0000349}
350
351static void clearmems(void)
352{
Eric Andersen08a72202002-09-30 20:52:10 +0000353 free(top);
Eric Andersen08a72202002-09-30 20:52:10 +0000354 top = 0;
Eric Andersen08a72202002-09-30 20:52:10 +0000355 ntop = 0;
356}
357
Mike Frysinger223b8872005-07-30 09:42:05 +0000358#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000359#include <termios.h>
Eric Andersen08a72202002-09-30 20:52:10 +0000360#include <signal.h>
361
362
363static struct termios initial_settings;
364
365static void reset_term(void)
366{
367 tcsetattr(0, TCSANOW, (void *) &initial_settings);
368#ifdef CONFIG_FEATURE_CLEAN_UP
369 clearmems();
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000370#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000371 free(prev_hist);
Eric Andersen08a72202002-09-30 20:52:10 +0000372#endif
373#endif /* CONFIG_FEATURE_CLEAN_UP */
374}
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000375
Rob Landleyb2804552006-02-13 22:04:27 +0000376static void sig_catcher(int sig ATTRIBUTE_UNUSED)
Eric Andersen08a72202002-09-30 20:52:10 +0000377{
378 reset_term();
Rob Landleydb1ab1a2006-06-28 14:11:25 +0000379 exit(1);
Eric Andersen08a72202002-09-30 20:52:10 +0000380}
381#endif /* CONFIG_FEATURE_USE_TERMIOS */
382
383
Eric Andersen420b2082002-09-17 22:14:58 +0000384int top_main(int argc, char **argv)
385{
Denis Vlasenko266bc172006-09-29 17:16:39 +0000386 int count, lines, col;
Denis Vlasenko13858992006-10-08 12:49:22 +0000387 unsigned interval = 5; /* default update rate is 5 seconds */
388 unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000389 char *sinterval, *siterations;
Mike Frysinger223b8872005-07-30 09:42:05 +0000390#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000391 struct termios new_settings;
392 struct timeval tv;
393 fd_set readfds;
394 unsigned char c;
Eric Andersen08a72202002-09-30 20:52:10 +0000395#endif /* CONFIG_FEATURE_USE_TERMIOS */
396
Eric Andersen420b2082002-09-17 22:14:58 +0000397 /* do normal option parsing */
Denis Vlasenko42dfcd22006-09-09 12:55:02 +0000398 interval = 5;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000399 opt_complementary = "-";
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000400 getopt32(argc, argv, "d:n:b",
Denis Vlasenko266bc172006-09-29 17:16:39 +0000401 &sinterval, &siterations);
Denis Vlasenko13858992006-10-08 12:49:22 +0000402 if (option_mask32 & 0x1) interval = xatou(sinterval); // -d
403 if (option_mask32 & 0x2) iterations = xatou(siterations); // -n
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000404 //if (option_mask32 & 0x4) // -b
Eric Andersen420b2082002-09-17 22:14:58 +0000405
Eric Andersen44608e92002-10-22 12:21:15 +0000406 /* change to /proc */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000407 xchdir("/proc");
Mike Frysinger223b8872005-07-30 09:42:05 +0000408#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000409 tcgetattr(0, (void *) &initial_settings);
410 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
Denis Vlasenko42dfcd22006-09-09 12:55:02 +0000411 /* unbuffered input, turn off echo */
412 new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
Eric Andersen08a72202002-09-30 20:52:10 +0000413
Rob Landley997650b2006-04-24 23:13:46 +0000414 signal(SIGTERM, sig_catcher);
Rob Landleydb1ab1a2006-06-28 14:11:25 +0000415 signal(SIGINT, sig_catcher);
Eric Andersen08a72202002-09-30 20:52:10 +0000416 tcsetattr(0, TCSANOW, (void *) &new_settings);
417 atexit(reset_term);
Eric Andersen08a72202002-09-30 20:52:10 +0000418#endif /* CONFIG_FEATURE_USE_TERMIOS */
Mike Frysinger223b8872005-07-30 09:42:05 +0000419
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000420#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000421 sort_function[0] = pcpu_sort;
422 sort_function[1] = mem_sort;
423 sort_function[2] = time_sort;
424#else
425 sort_function = mem_sort;
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000426#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Mike Frysinger223b8872005-07-30 09:42:05 +0000427
Eric Andersen08a72202002-09-30 20:52:10 +0000428 while (1) {
Rob Landley997650b2006-04-24 23:13:46 +0000429 procps_status_t *p;
Eric Andersen44608e92002-10-22 12:21:15 +0000430
Rob Landley997650b2006-04-24 23:13:46 +0000431 /* Default to 25 lines - 5 lines for status */
432 lines = 24 - 3;
433 col = 79;
434#ifdef CONFIG_FEATURE_USE_TERMIOS
435 get_terminal_width_height(0, &col, &lines);
436 if (lines < 5 || col < MIN_WIDTH) {
437 sleep(interval);
438 continue;
439 }
440 lines -= 3;
441#endif /* CONFIG_FEATURE_USE_TERMIOS */
442
443 /* read process IDs & status for all the processes */
Eric Andersen44608e92002-10-22 12:21:15 +0000444 while ((p = procps_scan(0)) != 0) {
445 int n = ntop;
446
447 top = xrealloc(top, (++ntop)*sizeof(procps_status_t));
448 memcpy(top + n, p, sizeof(procps_status_t));
449 }
450 if (ntop == 0) {
Rob Landleyb2804552006-02-13 22:04:27 +0000451 bb_error_msg_and_die("Can't find process info in /proc");
452 }
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000453#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000454 if (!prev_hist_count) {
Eric Andersen08a72202002-09-30 20:52:10 +0000455 do_stats();
456 sleep(1);
457 clearmems();
458 continue;
Rob Landleyb2804552006-02-13 22:04:27 +0000459 }
Eric Andersen08a72202002-09-30 20:52:10 +0000460 do_stats();
Rob Landley997650b2006-04-24 23:13:46 +0000461 qsort(top, ntop, sizeof(procps_status_t), (void*)mult_lvl_cmp);
Eric Andersen08a72202002-09-30 20:52:10 +0000462#else
Eric Andersen44608e92002-10-22 12:21:15 +0000463 qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function);
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000464#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000465 count = lines;
466 if (count > ntop) {
467 count = ntop;
Eric Andersen08a72202002-09-30 20:52:10 +0000468 }
469 /* show status for each of the processes */
Denis Vlasenko266bc172006-09-29 17:16:39 +0000470 display_status(count, col);
Mike Frysinger223b8872005-07-30 09:42:05 +0000471#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000472 tv.tv_sec = interval;
473 tv.tv_usec = 0;
Rob Landley997650b2006-04-24 23:13:46 +0000474 FD_ZERO(&readfds);
475 FD_SET(0, &readfds);
476 select(1, &readfds, NULL, NULL, &tv);
477 if (FD_ISSET(0, &readfds)) {
478 if (read(0, &c, 1) <= 0) { /* signal */
Mike Frysinger223b8872005-07-30 09:42:05 +0000479 return EXIT_FAILURE;
480 }
Rob Landley997650b2006-04-24 23:13:46 +0000481 if (c == 'q' || c == initial_settings.c_cc[VINTR])
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000482 break;
Rob Landley997650b2006-04-24 23:13:46 +0000483 if (c == 'M') {
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000484#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000485 sort_function[0] = mem_sort;
486 sort_function[1] = pcpu_sort;
487 sort_function[2] = time_sort;
488#else
489 sort_function = mem_sort;
490#endif
491 }
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000492#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landley997650b2006-04-24 23:13:46 +0000493 if (c == 'P') {
Eric Andersen08a72202002-09-30 20:52:10 +0000494 sort_function[0] = pcpu_sort;
495 sort_function[1] = mem_sort;
496 sort_function[2] = time_sort;
497 }
Rob Landley997650b2006-04-24 23:13:46 +0000498 if (c == 'T') {
Eric Andersen08a72202002-09-30 20:52:10 +0000499 sort_function[0] = time_sort;
500 sort_function[1] = mem_sort;
501 sort_function[2] = pcpu_sort;
502 }
503#endif
Rob Landley997650b2006-04-24 23:13:46 +0000504 if (c == 'N') {
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000505#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000506 sort_function[0] = pid_sort;
507#else
508 sort_function = pid_sort;
509#endif
510 }
511 }
Denis Vlasenko266bc172006-09-29 17:16:39 +0000512 if (!--iterations)
513 break;
Eric Andersen08a72202002-09-30 20:52:10 +0000514#else
Eric Andersen420b2082002-09-17 22:14:58 +0000515 sleep(interval);
Mike Frysinger223b8872005-07-30 09:42:05 +0000516#endif /* CONFIG_FEATURE_USE_TERMIOS */
Eric Andersen08a72202002-09-30 20:52:10 +0000517 clearmems();
Eric Andersen420b2082002-09-17 22:14:58 +0000518 }
Rob Landley997650b2006-04-24 23:13:46 +0000519 if (ENABLE_FEATURE_CLEAN_UP)
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000520 clearmems();
521 putchar('\n');
Eric Andersen420b2082002-09-17 22:14:58 +0000522 return EXIT_SUCCESS;
523}