blob: afd419ba55f55587925b05a7a6ab5e6f0f26bfb7 [file] [log] [blame]
Eric Andersen420b2082002-09-17 22:14:58 +00001/*
2 * A tiny 'top' utility.
3 *
Eric Andersen08a72202002-09-30 20:52:10 +00004 * This is written specifically for the linux /proc/<PID>/stat(m)
5 * files format.
6
7 * This reads the PIDs of all processes and their status and shows
8 * the status of processes (first ones that fit to screen) at given
9 * intervals.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000010 *
Eric Andersen420b2082002-09-17 22:14:58 +000011 * NOTES:
12 * - At startup this changes to /proc, all the reads are then
13 * relative to that.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000014 *
Eric Andersen420b2082002-09-17 22:14:58 +000015 * (C) Eero Tamminen <oak at welho dot com>
Eric Andersen08a72202002-09-30 20:52:10 +000016 *
Eric Andersenaff114c2004-04-14 17:51:38 +000017 * Rewritten by Vladimir Oleynik (C) 2002 <dzo@simtreas.ru>
Eric Andersen420b2082002-09-17 22:14:58 +000018 */
Eric Andersen08a72202002-09-30 20:52:10 +000019
20/* Original code Copyrights */
21/*
22 * Copyright (c) 1992 Branko Lankester
23 * Copyright (c) 1992 Roger Binns
24 * Copyright (C) 1994-1996 Charles L. Blake.
25 * Copyright (C) 1992-1998 Michael K. Johnson
26 * May be distributed under the conditions of the
27 * GNU Library General Public License
28 */
29
30#include <sys/types.h>
Eric Andersen420b2082002-09-17 22:14:58 +000031#include <stdio.h>
32#include <stdlib.h>
33#include <unistd.h>
Eric Andersen420b2082002-09-17 22:14:58 +000034#include <string.h>
35#include <sys/ioctl.h>
36#include "busybox.h"
37
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +000038//#define CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE /* + 2k */
Eric Andersen420b2082002-09-17 22:14:58 +000039
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +000040#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +000041#include <time.h>
42#include <sys/time.h>
43#include <fcntl.h>
44#include <netinet/in.h> /* htons */
45#endif
46
47
Eric Andersen44608e92002-10-22 12:21:15 +000048typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q);
Eric Andersen08a72202002-09-30 20:52:10 +000049
Eric Andersen44608e92002-10-22 12:21:15 +000050static procps_status_t *top; /* Hehe */
Eric Andersen08a72202002-09-30 20:52:10 +000051static int ntop;
52
Mike Frysinger223b8872005-07-30 09:42:05 +000053#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersen44608e92002-10-22 12:21:15 +000054static int pid_sort (procps_status_t *P, procps_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000055{
Rob Landleyb2804552006-02-13 22:04:27 +000056 return (Q->pid - P->pid);
Eric Andersen08a72202002-09-30 20:52:10 +000057}
Mike Frysinger223b8872005-07-30 09:42:05 +000058#endif
Eric Andersen08a72202002-09-30 20:52:10 +000059
Eric Andersen44608e92002-10-22 12:21:15 +000060static int mem_sort (procps_status_t *P, procps_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000061{
Rob Landleyb2804552006-02-13 22:04:27 +000062 return (int)(Q->rss - P->rss);
Eric Andersen08a72202002-09-30 20:52:10 +000063}
64
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +000065#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +000066
67#define sort_depth 3
68static cmp_t sort_function[sort_depth];
69
Eric Andersen44608e92002-10-22 12:21:15 +000070static int pcpu_sort (procps_status_t *P, procps_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000071{
Rob Landleyb2804552006-02-13 22:04:27 +000072 return (Q->pcpu - P->pcpu);
Eric Andersen08a72202002-09-30 20:52:10 +000073}
74
Eric Andersen44608e92002-10-22 12:21:15 +000075static int time_sort (procps_status_t *P, procps_status_t *Q)
Eric Andersen08a72202002-09-30 20:52:10 +000076{
Rob Landleyb2804552006-02-13 22:04:27 +000077 return (int)((Q->stime + Q->utime) - (P->stime + P->utime));
Eric Andersen08a72202002-09-30 20:52:10 +000078}
79
Eric Andersen14f5c8d2005-04-16 19:39:00 +000080static int mult_lvl_cmp(void* a, void* b) {
Rob Landleyb2804552006-02-13 22:04:27 +000081 int i, cmp_val;
Eric Andersen08a72202002-09-30 20:52:10 +000082
Rob Landleyb2804552006-02-13 22:04:27 +000083 for(i = 0; i < sort_depth; i++) {
84 cmp_val = (*sort_function[i])(a, b);
85 if (cmp_val != 0)
86 return cmp_val;
87 }
88 return 0;
Eric Andersen08a72202002-09-30 20:52:10 +000089}
90
91/* This structure stores some critical information from one frame to
92 the next. mostly used for sorting. Added cumulative and resident fields. */
93struct save_hist {
Rob Landleyb2804552006-02-13 22:04:27 +000094 int ticks;
95 int pid;
96 int utime;
97 int stime;
Eric Andersen08a72202002-09-30 20:52:10 +000098};
99
100/*
101 * Calculates percent cpu usage for each task.
102 */
103
104static struct save_hist *save_history;
105
106static unsigned long Hertz;
107
108/***********************************************************************
109 * Some values in /proc are expressed in units of 1/HZ seconds, where HZ
110 * is the kernel clock tick rate. One of these units is called a jiffy.
111 * The HZ value used in the kernel may vary according to hacker desire.
112 * According to Linus Torvalds, this is not true. He considers the values
Eric Andersenaff114c2004-04-14 17:51:38 +0000113 * in /proc as being in architecture-dependent units that have no relation
Eric Andersen08a72202002-09-30 20:52:10 +0000114 * to the kernel clock tick rate. Examination of the kernel source code
115 * reveals that opinion as wishful thinking.
116 *
117 * In any case, we need the HZ constant as used in /proc. (the real HZ value
118 * may differ, but we don't care) There are several ways we could get HZ:
119 *
120 * 1. Include the kernel header file. If it changes, recompile this library.
121 * 2. Use the sysconf() function. When HZ changes, recompile the C library!
122 * 3. Ask the kernel. This is obviously correct...
123 *
124 * Linus Torvalds won't let us ask the kernel, because he thinks we should
125 * not know the HZ value. Oh well, we don't have to listen to him.
126 * Someone smuggled out the HZ value. :-)
127 *
128 * This code should work fine, even if Linus fixes the kernel to match his
129 * stated behavior. The code only fails in case of a partial conversion.
130 *
131 */
132
"Vladimir N. Oleynik"3afdfec2006-02-14 10:36:38 +0000133#define file_to_buf_bufsize 80
134static inline int file_to_buf(char *buf, const char *filename, int fd)
Rob Landleyb2804552006-02-13 22:04:27 +0000135{
Rob Landleyb2804552006-02-13 22:04:27 +0000136 int sz;
Eric Andersen08a72202002-09-30 20:52:10 +0000137
Rob Landleyb2804552006-02-13 22:04:27 +0000138 if (fd == -1) {
139 fd = open(filename, O_RDONLY);
140 if(fd == -1)
141 bb_perror_msg_and_die("is /proc mounted?");
142 } else {
143 lseek(fd, 0L, SEEK_SET);
144 }
"Vladimir N. Oleynik"3afdfec2006-02-14 10:36:38 +0000145 sz = read(fd, buf, file_to_buf_bufsize - 1);
Rob Landleyb2804552006-02-13 22:04:27 +0000146 if (sz < 0) {
147 bb_perror_msg_and_die("%s", filename);
148 }
149 buf[sz] = '\0';
"Vladimir N. Oleynik"3afdfec2006-02-14 10:36:38 +0000150 return fd;
Rob Landleyb2804552006-02-13 22:04:27 +0000151}
Eric Andersen08a72202002-09-30 20:52:10 +0000152
Rob Landleyb2804552006-02-13 22:04:27 +0000153static void init_Hertz_value(void)
154{
155 unsigned long user_j, nice_j, sys_j, other_j; /* jiffies (clock ticks) */
156 double up_1, up_2, seconds;
157 unsigned long jiffies, h;
158 char buf[80];
159 int uptime_fd = -1;
160 int stat_fd = -1;
Eric Andersen08a72202002-09-30 20:52:10 +0000161
Rob Landleyb2804552006-02-13 22:04:27 +0000162 long smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF);
Eric Andersen08a72202002-09-30 20:52:10 +0000163
Rob Landleyb2804552006-02-13 22:04:27 +0000164 if(smp_num_cpus<1) smp_num_cpus=1;
Eric Andersen08a72202002-09-30 20:52:10 +0000165
Rob Landleyb2804552006-02-13 22:04:27 +0000166 do {
"Vladimir N. Oleynik"3afdfec2006-02-14 10:36:38 +0000167 uptime_fd = file_to_buf(buf, "uptime", uptime_fd);
Rob Landleyb2804552006-02-13 22:04:27 +0000168 up_1 = strtod(buf, 0);
"Vladimir N. Oleynik"3afdfec2006-02-14 10:36:38 +0000169 stat_fd = file_to_buf(buf, "stat", stat_fd);
Rob Landleyb2804552006-02-13 22:04:27 +0000170 sscanf(buf, "cpu %lu %lu %lu %lu", &user_j, &nice_j, &sys_j, &other_j);
"Vladimir N. Oleynik"3afdfec2006-02-14 10:36:38 +0000171 uptime_fd = file_to_buf(buf, "uptime", uptime_fd);
Rob Landleyb2804552006-02-13 22:04:27 +0000172 up_2 = strtod(buf, 0);
173 } while((long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */
174 close(uptime_fd);
175 close(stat_fd);
Eric Andersen08a72202002-09-30 20:52:10 +0000176
Rob Landleyb2804552006-02-13 22:04:27 +0000177 jiffies = user_j + nice_j + sys_j + other_j;
178 seconds = (up_1 + up_2) / 2;
179 h = (unsigned long)( (double)jiffies/seconds/smp_num_cpus );
180 /* actual values used by 2.4 kernels: 32 64 100 128 1000 1024 1200 */
181 switch(h) {
182 case 30 ... 34 : Hertz = 32; break; /* ia64 emulator */
183 case 48 ... 52 : Hertz = 50; break;
184 case 58 ... 62 : Hertz = 60; break;
185 case 63 ... 65 : Hertz = 64; break; /* StrongARM /Shark */
186 case 95 ... 105 : Hertz = 100; break; /* normal Linux */
187 case 124 ... 132 : Hertz = 128; break; /* MIPS, ARM */
188 case 195 ... 204 : Hertz = 200; break; /* normal << 1 */
189 case 253 ... 260 : Hertz = 256; break;
190 case 295 ... 304 : Hertz = 300; break; /* 3 cpus */
191 case 393 ... 408 : Hertz = 400; break; /* normal << 2 */
192 case 495 ... 504 : Hertz = 500; break; /* 5 cpus */
193 case 595 ... 604 : Hertz = 600; break; /* 6 cpus */
194 case 695 ... 704 : Hertz = 700; break; /* 7 cpus */
195 case 790 ... 808 : Hertz = 800; break; /* normal << 3 */
196 case 895 ... 904 : Hertz = 900; break; /* 9 cpus */
197 case 990 ... 1010 : Hertz = 1000; break; /* ARM */
198 case 1015 ... 1035 : Hertz = 1024; break; /* Alpha, ia64 */
199 case 1095 ... 1104 : Hertz = 1100; break; /* 11 cpus */
200 case 1180 ... 1220 : Hertz = 1200; break; /* Alpha */
201 default:
202 /* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
203 Hertz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL;
204 }
Eric Andersen08a72202002-09-30 20:52:10 +0000205}
206
207static void do_stats(void)
208{
Rob Landleyb2804552006-02-13 22:04:27 +0000209 struct timeval t;
210 static struct timeval oldtime;
211 struct timezone timez;
212 float elapsed_time;
Eric Andersen08a72202002-09-30 20:52:10 +0000213
Rob Landleyb2804552006-02-13 22:04:27 +0000214 procps_status_t *cur;
215 int total_time, i, n;
216 static int prev_count;
217 int systime, usrtime, pid;
Eric Andersen08a72202002-09-30 20:52:10 +0000218
Rob Landleyb2804552006-02-13 22:04:27 +0000219 struct save_hist *New_save_hist;
Eric Andersen08a72202002-09-30 20:52:10 +0000220
221 /*
Rob Landleyb2804552006-02-13 22:04:27 +0000222 * Finds the current time (in microseconds) and calculates the time
223 * elapsed since the last update.
Eric Andersen08a72202002-09-30 20:52:10 +0000224 */
Rob Landleyb2804552006-02-13 22:04:27 +0000225 gettimeofday(&t, &timez);
226 elapsed_time = (t.tv_sec - oldtime.tv_sec)
227 + (float) (t.tv_usec - oldtime.tv_usec) / 1000000.0;
228 oldtime.tv_sec = t.tv_sec;
229 oldtime.tv_usec = t.tv_usec;
Eric Andersen08a72202002-09-30 20:52:10 +0000230
Rob Landleyb2804552006-02-13 22:04:27 +0000231 New_save_hist = alloca(sizeof(struct save_hist)*ntop);
232 /*
233 * Make a pass through the data to get stats.
234 */
235 for(n = 0; n < ntop; n++) {
236 cur = top + n;
237
238 /*
239 * Calculate time in cur process. Time is sum of user time
240 * (usrtime) plus system time (systime).
241 */
242 systime = cur->stime;
243 usrtime = cur->utime;
244 pid = cur->pid;
245 total_time = systime + usrtime;
246 New_save_hist[n].ticks = total_time;
247 New_save_hist[n].pid = pid;
248 New_save_hist[n].stime = systime;
249 New_save_hist[n].utime = usrtime;
250
251 /* find matching entry from previous pass */
252 for (i = 0; i < prev_count; i++) {
253 if (save_history[i].pid == pid) {
254 total_time -= save_history[i].ticks;
255 systime -= save_history[i].stime;
256 usrtime -= save_history[i].utime;
257 break;
258 }
259 }
260
261 /*
262 * Calculate percent cpu time for cur task.
263 */
264 i = (total_time * 10 * 100/Hertz) / elapsed_time;
265 if (i > 999)
266 i = 999;
267 cur->pcpu = i;
Eric Andersen08a72202002-09-30 20:52:10 +0000268 }
269
270 /*
Rob Landleyb2804552006-02-13 22:04:27 +0000271 * Save cur frame's information.
Eric Andersen08a72202002-09-30 20:52:10 +0000272 */
Rob Landleyb2804552006-02-13 22:04:27 +0000273 free(save_history);
274 save_history = memcpy(xmalloc(sizeof(struct save_hist)*n), New_save_hist,
Eric Andersen08a72202002-09-30 20:52:10 +0000275 sizeof(struct save_hist)*n);
Rob Landleyb2804552006-02-13 22:04:27 +0000276 prev_count = n;
277 qsort(top, n, sizeof(procps_status_t), (void*)mult_lvl_cmp);
Eric Andersen08a72202002-09-30 20:52:10 +0000278}
279#else
280static cmp_t sort_function;
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000281#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Eric Andersen08a72202002-09-30 20:52:10 +0000282
Eric Andersen420b2082002-09-17 22:14:58 +0000283/* display generic info (meminfo / loadavg) */
Eric Andersen08a72202002-09-30 20:52:10 +0000284static unsigned long display_generic(void)
Eric Andersen420b2082002-09-17 22:14:58 +0000285{
286 FILE *fp;
287 char buf[80];
288 float avg1, avg2, avg3;
289 unsigned long total, used, mfree, shared, buffers, cached;
Eric Andersen7857c032003-10-11 18:47:20 +0000290 unsigned int needs_conversion = 1;
Eric Andersen420b2082002-09-17 22:14:58 +0000291
292 /* read memory info */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000293 fp = bb_xfopen("meminfo", "r");
Eric Andersen420b2082002-09-17 22:14:58 +0000294
Eric Andersen7857c032003-10-11 18:47:20 +0000295 /*
296 * Old kernels (such as 2.4.x) had a nice summary of memory info that
297 * we could parse, however this is gone entirely in 2.6. Try parsing
298 * the old way first, and if that fails, parse each field manually.
299 *
300 * First, we read in the first line. Old kernels will have bogus
301 * strings we don't care about, whereas new kernels will start right
302 * out with MemTotal:
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000303 * -- PFM.
Eric Andersen7857c032003-10-11 18:47:20 +0000304 */
305 if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000306 fgets(buf, sizeof(buf), fp); /* skip first line */
Eric Andersen7857c032003-10-11 18:47:20 +0000307
308 fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
309 &total, &used, &mfree, &shared, &buffers, &cached);
310 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000311 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000312 * Revert to manual parsing, which incidentally already has the
313 * sizes in kilobytes. This should be safe for both 2.4 and
314 * 2.6.
315 */
316 needs_conversion = 0;
317
318 fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
319
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000320 /*
Eric Andersen7857c032003-10-11 18:47:20 +0000321 * MemShared: is no longer present in 2.6. Report this as 0,
322 * to maintain consistent behavior with normal procps.
323 */
324 if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
325 shared = 0;
326
327 fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
328 fscanf(fp, "Cached: %lu %s\n", &cached, buf);
329
330 used = total - mfree;
Eric Andersen420b2082002-09-17 22:14:58 +0000331 }
332 fclose(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000333
Eric Andersen420b2082002-09-17 22:14:58 +0000334 /* read load average */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000335 fp = bb_xfopen("loadavg", "r");
Eric Andersen420b2082002-09-17 22:14:58 +0000336 if (fscanf(fp, "%f %f %f", &avg1, &avg2, &avg3) != 3) {
Rob Landleyb2804552006-02-13 22:04:27 +0000337 bb_error_msg_and_die("failed to read 'loadavg'");
Eric Andersen420b2082002-09-17 22:14:58 +0000338 }
339 fclose(fp);
340
Eric Andersen7857c032003-10-11 18:47:20 +0000341 if (needs_conversion) {
342 /* convert to kilobytes */
343 used /= 1024;
344 mfree /= 1024;
345 shared /= 1024;
346 buffers /= 1024;
347 cached /= 1024;
348 total /= 1024;
349 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000350
Eric Andersen420b2082002-09-17 22:14:58 +0000351 /* output memory info and load average */
Eric Andersen08a72202002-09-30 20:52:10 +0000352 /* clear screen & go to top */
353 printf("\e[H\e[J" "Mem: "
Rob Landleyb2804552006-02-13 22:04:27 +0000354 "%ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached\n",
355 used, mfree, shared, buffers, cached);
Eric Andersen08a72202002-09-30 20:52:10 +0000356 printf("Load average: %.2f, %.2f, %.2f "
Rob Landleyb2804552006-02-13 22:04:27 +0000357 "(State: S=sleeping R=running, W=waiting)\n",
358 avg1, avg2, avg3);
Eric Andersen7857c032003-10-11 18:47:20 +0000359 return total;
Eric Andersen420b2082002-09-17 22:14:58 +0000360}
361
362
363/* display process statuses */
Eric Andersen08a72202002-09-30 20:52:10 +0000364static void display_status(int count, int col)
Eric Andersen420b2082002-09-17 22:14:58 +0000365{
Eric Andersen44608e92002-10-22 12:21:15 +0000366 procps_status_t *s = top;
Eric Andersen08a72202002-09-30 20:52:10 +0000367 char rss_str_buf[8];
368 unsigned long total_memory = display_generic();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000369
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000370#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen420b2082002-09-17 22:14:58 +0000371 /* what info of the processes is shown */
Eric Andersen08a72202002-09-30 20:52:10 +0000372 printf("\n\e[7m PID USER STATUS RSS PPID %%CPU %%MEM COMMAND\e[0m\n");
373#else
374 printf("\n\e[7m PID USER STATUS RSS PPID %%MEM COMMAND\e[0m\n");
375#endif
Eric Andersen420b2082002-09-17 22:14:58 +0000376
377 while (count--) {
Eric Andersen44608e92002-10-22 12:21:15 +0000378 char *namecmd = s->short_cmd;
Eric Andersen08a72202002-09-30 20:52:10 +0000379 int pmem;
Eric Andersen420b2082002-09-17 22:14:58 +0000380
Eric Andersen08a72202002-09-30 20:52:10 +0000381 pmem = 1000.0 * s->rss / total_memory;
382 if (pmem > 999) pmem = 999;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000383
Eric Andersen08a72202002-09-30 20:52:10 +0000384 if(s->rss > 10*1024)
385 sprintf(rss_str_buf, "%6ldM", s->rss/1024);
Manuel Novoa III d4993302002-09-18 19:27:10 +0000386 else
Eric Andersen08a72202002-09-30 20:52:10 +0000387 sprintf(rss_str_buf, "%7ld", s->rss);
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000388#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Glenn L McGrathf6bba3d2003-12-08 20:31:25 +0000389 printf("%5d %-8s %s %s %5d %2d.%d %2u.%u ",
Eric Andersen08a72202002-09-30 20:52:10 +0000390 s->pid, s->user, s->state, rss_str_buf, s->ppid,
Glenn L McGrathf6bba3d2003-12-08 20:31:25 +0000391 s->pcpu/10, s->pcpu%10, pmem/10, pmem%10);
392#else
393 printf("%5d %-8s %s %s %5d %2u.%u ",
394 s->pid, s->user, s->state, rss_str_buf, s->ppid,
Eric Andersen08a72202002-09-30 20:52:10 +0000395 pmem/10, pmem%10);
Glenn L McGrathf6bba3d2003-12-08 20:31:25 +0000396#endif
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000397 if((int)strlen(namecmd) > col)
398 namecmd[col] = 0;
399 printf("%s", namecmd);
400 if(count)
401 putchar('\n');
402 else
403 fflush(stdout);
Eric Andersen420b2082002-09-17 22:14:58 +0000404 s++;
Eric Andersen08a72202002-09-30 20:52:10 +0000405 }
Eric Andersen44608e92002-10-22 12:21:15 +0000406}
407
408static void clearmems(void)
409{
Eric Andersen08a72202002-09-30 20:52:10 +0000410 free(top);
Eric Andersen08a72202002-09-30 20:52:10 +0000411 top = 0;
Eric Andersen08a72202002-09-30 20:52:10 +0000412 ntop = 0;
413}
414
Mike Frysinger223b8872005-07-30 09:42:05 +0000415#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000416#include <termios.h>
417#include <sys/time.h>
418#include <signal.h>
419
420
421static struct termios initial_settings;
422
423static void reset_term(void)
424{
425 tcsetattr(0, TCSANOW, (void *) &initial_settings);
426#ifdef CONFIG_FEATURE_CLEAN_UP
427 clearmems();
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000428#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000429 free(save_history);
430#endif
431#endif /* CONFIG_FEATURE_CLEAN_UP */
432}
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000433
Rob Landleyb2804552006-02-13 22:04:27 +0000434static void sig_catcher(int sig ATTRIBUTE_UNUSED)
Eric Andersen08a72202002-09-30 20:52:10 +0000435{
436 reset_term();
437}
438#endif /* CONFIG_FEATURE_USE_TERMIOS */
439
440
Eric Andersen420b2082002-09-17 22:14:58 +0000441int top_main(int argc, char **argv)
442{
Eric Andersen08a72202002-09-30 20:52:10 +0000443 int opt, interval, lines, col;
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000444 char *sinterval;
Mike Frysinger223b8872005-07-30 09:42:05 +0000445#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000446 struct termios new_settings;
447 struct timeval tv;
448 fd_set readfds;
449 unsigned char c;
450 struct sigaction sa;
Eric Andersen08a72202002-09-30 20:52:10 +0000451#endif /* CONFIG_FEATURE_USE_TERMIOS */
452
Eric Andersen420b2082002-09-17 22:14:58 +0000453 /* do normal option parsing */
"Vladimir N. Oleynik"70678bc2005-11-29 12:32:33 +0000454 opt = bb_getopt_ulflags(argc, argv, "d:", &sinterval);
455 if((opt & 1)) {
456 interval = atoi(sinterval);
457 } else {
458 /* Default update rate is 5 seconds */
459 interval = 5;
Eric Andersen420b2082002-09-17 22:14:58 +0000460 }
461
Eric Andersen08a72202002-09-30 20:52:10 +0000462 /* Default to 25 lines - 5 lines for status */
463 lines = 25 - 5;
464 /* Default CMD format size */
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000465#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000466 col = 35 - 6;
467#else
468 col = 35;
Eric Andersen420b2082002-09-17 22:14:58 +0000469#endif
Eric Andersen44608e92002-10-22 12:21:15 +0000470 /* change to /proc */
Eric Andersen420b2082002-09-17 22:14:58 +0000471 if (chdir("/proc") < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000472 bb_perror_msg_and_die("chdir('/proc')");
Eric Andersen420b2082002-09-17 22:14:58 +0000473 }
Mike Frysinger223b8872005-07-30 09:42:05 +0000474#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000475 tcgetattr(0, (void *) &initial_settings);
476 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
477 new_settings.c_lflag &= ~(ISIG | ICANON); /* unbuffered input */
478 /* Turn off echoing */
479 new_settings.c_lflag &= ~(ECHO | ECHONL);
480
481 signal (SIGTERM, sig_catcher);
482 sigaction (SIGTERM, (struct sigaction *) 0, &sa);
483 sa.sa_flags |= SA_RESTART;
484 sa.sa_flags &= ~SA_INTERRUPT;
485 sigaction (SIGTERM, &sa, (struct sigaction *) 0);
486 sigaction (SIGINT, &sa, (struct sigaction *) 0);
487 tcsetattr(0, TCSANOW, (void *) &new_settings);
488 atexit(reset_term);
Eric Andersen8efe9672003-09-15 08:33:45 +0000489
490 get_terminal_width_height(0, &col, &lines);
491 if (lines > 4) {
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000492 lines -= 4;
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000493#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Rob Landleyb2804552006-02-13 22:04:27 +0000494 col = col - 80 + 35 - 6;
Eric Andersen08a72202002-09-30 20:52:10 +0000495#else
Rob Landleyb2804552006-02-13 22:04:27 +0000496 col = col - 80 + 35;
Eric Andersen08a72202002-09-30 20:52:10 +0000497#endif
498 }
Eric Andersen08a72202002-09-30 20:52:10 +0000499#endif /* CONFIG_FEATURE_USE_TERMIOS */
Mike Frysinger223b8872005-07-30 09:42:05 +0000500
"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] = pcpu_sort;
503 sort_function[1] = mem_sort;
504 sort_function[2] = time_sort;
505#else
506 sort_function = mem_sort;
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000507#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Mike Frysinger223b8872005-07-30 09:42:05 +0000508
Eric Andersen08a72202002-09-30 20:52:10 +0000509 while (1) {
510 /* read process IDs & status for all the processes */
Eric Andersen44608e92002-10-22 12:21:15 +0000511 procps_status_t * p;
512
513 while ((p = procps_scan(0)) != 0) {
514 int n = ntop;
515
516 top = xrealloc(top, (++ntop)*sizeof(procps_status_t));
517 memcpy(top + n, p, sizeof(procps_status_t));
518 }
519 if (ntop == 0) {
Rob Landleyb2804552006-02-13 22:04:27 +0000520 bb_error_msg_and_die("Can't find process info in /proc");
521 }
"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 if(!Hertz) {
524 init_Hertz_value();
525 do_stats();
526 sleep(1);
527 clearmems();
528 continue;
Rob Landleyb2804552006-02-13 22:04:27 +0000529 }
Eric Andersen08a72202002-09-30 20:52:10 +0000530 do_stats();
531#else
Eric Andersen44608e92002-10-22 12:21:15 +0000532 qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function);
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000533#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
Eric Andersen08a72202002-09-30 20:52:10 +0000534 opt = lines;
535 if (opt > ntop) {
536 opt = ntop;
537 }
538 /* show status for each of the processes */
539 display_status(opt, col);
Mike Frysinger223b8872005-07-30 09:42:05 +0000540#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersen08a72202002-09-30 20:52:10 +0000541 tv.tv_sec = interval;
542 tv.tv_usec = 0;
543 FD_ZERO (&readfds);
544 FD_SET (0, &readfds);
545 select (1, &readfds, NULL, NULL, &tv);
546 if (FD_ISSET (0, &readfds)) {
547 if (read (0, &c, 1) <= 0) { /* signal */
Mike Frysinger223b8872005-07-30 09:42:05 +0000548 return EXIT_FAILURE;
549 }
Eric Andersen08a72202002-09-30 20:52:10 +0000550 if(c == 'q' || c == initial_settings.c_cc[VINTR])
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000551 break;
Eric Andersen08a72202002-09-30 20:52:10 +0000552 if(c == 'M') {
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000553#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000554 sort_function[0] = mem_sort;
555 sort_function[1] = pcpu_sort;
556 sort_function[2] = time_sort;
557#else
558 sort_function = mem_sort;
559#endif
560 }
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000561#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000562 if(c == 'P') {
563 sort_function[0] = pcpu_sort;
564 sort_function[1] = mem_sort;
565 sort_function[2] = time_sort;
566 }
567 if(c == 'T') {
568 sort_function[0] = time_sort;
569 sort_function[1] = mem_sort;
570 sort_function[2] = pcpu_sort;
571 }
572#endif
573 if(c == 'N') {
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000574#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen08a72202002-09-30 20:52:10 +0000575 sort_function[0] = pid_sort;
576#else
577 sort_function = pid_sort;
578#endif
579 }
580 }
581#else
Eric Andersen420b2082002-09-17 22:14:58 +0000582 sleep(interval);
Mike Frysinger223b8872005-07-30 09:42:05 +0000583#endif /* CONFIG_FEATURE_USE_TERMIOS */
Eric Andersen08a72202002-09-30 20:52:10 +0000584 clearmems();
Eric Andersen420b2082002-09-17 22:14:58 +0000585 }
"Vladimir N. Oleynik"c218a292006-02-15 17:15:56 +0000586 if(ENABLE_FEATURE_CLEAN_UP)
587 clearmems();
588 putchar('\n');
Eric Andersen420b2082002-09-17 22:14:58 +0000589 return EXIT_SUCCESS;
590}