Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * A tiny 'top' utility. |
| 3 | * |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 4 | * 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 Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 10 | * |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 11 | * NOTES: |
| 12 | * - At startup this changes to /proc, all the reads are then |
| 13 | * relative to that. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 14 | * |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 15 | * (C) Eero Tamminen <oak at welho dot com> |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 16 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 17 | * Rewritten by Vladimir Oleynik (C) 2002 <dzo@simtreas.ru> |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 18 | */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 19 | |
| 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 Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <unistd.h> |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 34 | #include <string.h> |
| 35 | #include <sys/ioctl.h> |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 36 | /* get page info */ |
| 37 | #include <asm/page.h> |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 38 | #include "busybox.h" |
| 39 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 40 | //#define FEATURE_CPU_USAGE_PERCENTAGE /* + 2k */ |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 41 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 42 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
| 43 | #include <time.h> |
| 44 | #include <sys/time.h> |
| 45 | #include <fcntl.h> |
| 46 | #include <netinet/in.h> /* htons */ |
| 47 | #endif |
| 48 | |
| 49 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 50 | typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 51 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 52 | static procps_status_t *top; /* Hehe */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 53 | static int ntop; |
| 54 | |
| 55 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 56 | static int pid_sort (procps_status_t *P, procps_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 57 | { |
Glenn L McGrath | 225be8c | 2004-09-14 18:56:52 +0000 | [diff] [blame] | 58 | return (Q->pid - P->pid); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 61 | static int mem_sort (procps_status_t *P, procps_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 62 | { |
Glenn L McGrath | 225be8c | 2004-09-14 18:56:52 +0000 | [diff] [blame] | 63 | return (int)(Q->rss - P->rss); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
| 67 | |
| 68 | #define sort_depth 3 |
| 69 | static cmp_t sort_function[sort_depth]; |
| 70 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 71 | static int pcpu_sort (procps_status_t *P, procps_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 72 | { |
Glenn L McGrath | 225be8c | 2004-09-14 18:56:52 +0000 | [diff] [blame] | 73 | return (Q->pcpu - P->pcpu); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 76 | static int time_sort (procps_status_t *P, procps_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 77 | { |
Glenn L McGrath | 225be8c | 2004-09-14 18:56:52 +0000 | [diff] [blame] | 78 | return (int)((Q->stime + Q->utime) - (P->stime + P->utime)); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame^] | 81 | static int mult_lvl_cmp(void* a, void* b) { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 82 | int i, cmp_val; |
| 83 | |
| 84 | for(i = 0; i < sort_depth; i++) { |
| 85 | cmp_val = (*sort_function[i])(a, b); |
| 86 | if (cmp_val != 0) |
| 87 | return cmp_val; |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | /* This structure stores some critical information from one frame to |
| 93 | the next. mostly used for sorting. Added cumulative and resident fields. */ |
| 94 | struct save_hist { |
| 95 | int ticks; |
| 96 | int pid; |
| 97 | int utime; |
| 98 | int stime; |
| 99 | }; |
| 100 | |
| 101 | /* |
| 102 | * Calculates percent cpu usage for each task. |
| 103 | */ |
| 104 | |
| 105 | static struct save_hist *save_history; |
| 106 | |
| 107 | static unsigned long Hertz; |
| 108 | |
| 109 | /*********************************************************************** |
| 110 | * Some values in /proc are expressed in units of 1/HZ seconds, where HZ |
| 111 | * is the kernel clock tick rate. One of these units is called a jiffy. |
| 112 | * The HZ value used in the kernel may vary according to hacker desire. |
| 113 | * According to Linus Torvalds, this is not true. He considers the values |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 114 | * in /proc as being in architecture-dependent units that have no relation |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 115 | * to the kernel clock tick rate. Examination of the kernel source code |
| 116 | * reveals that opinion as wishful thinking. |
| 117 | * |
| 118 | * In any case, we need the HZ constant as used in /proc. (the real HZ value |
| 119 | * may differ, but we don't care) There are several ways we could get HZ: |
| 120 | * |
| 121 | * 1. Include the kernel header file. If it changes, recompile this library. |
| 122 | * 2. Use the sysconf() function. When HZ changes, recompile the C library! |
| 123 | * 3. Ask the kernel. This is obviously correct... |
| 124 | * |
| 125 | * Linus Torvalds won't let us ask the kernel, because he thinks we should |
| 126 | * not know the HZ value. Oh well, we don't have to listen to him. |
| 127 | * Someone smuggled out the HZ value. :-) |
| 128 | * |
| 129 | * This code should work fine, even if Linus fixes the kernel to match his |
| 130 | * stated behavior. The code only fails in case of a partial conversion. |
| 131 | * |
| 132 | */ |
| 133 | |
| 134 | #define FILE_TO_BUF(filename, fd) do{ \ |
| 135 | if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 136 | bb_perror_msg_and_die("/proc not be mounted?"); \ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 137 | } \ |
| 138 | lseek(fd, 0L, SEEK_SET); \ |
| 139 | if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 140 | bb_perror_msg_and_die("%s", filename); \ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 141 | } \ |
| 142 | buf[local_n] = '\0'; \ |
| 143 | }while(0) |
| 144 | |
| 145 | #define FILE_TO_BUF2(filename, fd) do{ \ |
| 146 | lseek(fd, 0L, SEEK_SET); \ |
| 147 | if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 148 | bb_perror_msg_and_die("%s", filename); \ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 149 | } \ |
| 150 | buf[local_n] = '\0'; \ |
| 151 | }while(0) |
| 152 | |
| 153 | static void init_Hertz_value(void) { |
| 154 | unsigned long user_j, nice_j, sys_j, other_j; /* jiffies (clock ticks) */ |
| 155 | double up_1, up_2, seconds; |
| 156 | unsigned long jiffies, h; |
| 157 | char buf[80]; |
| 158 | int uptime_fd = -1; |
| 159 | int stat_fd = -1; |
| 160 | |
| 161 | long smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF); |
| 162 | |
| 163 | if(smp_num_cpus<1) smp_num_cpus=1; |
| 164 | do { |
| 165 | int local_n; |
| 166 | |
| 167 | FILE_TO_BUF("uptime", uptime_fd); |
| 168 | up_1 = strtod(buf, 0); |
| 169 | FILE_TO_BUF("stat", stat_fd); |
| 170 | sscanf(buf, "cpu %lu %lu %lu %lu", &user_j, &nice_j, &sys_j, &other_j); |
| 171 | FILE_TO_BUF2("uptime", uptime_fd); |
| 172 | up_2 = strtod(buf, 0); |
| 173 | } while((long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */ |
| 174 | |
| 175 | close(uptime_fd); |
| 176 | close(stat_fd); |
| 177 | |
| 178 | jiffies = user_j + nice_j + sys_j + other_j; |
| 179 | seconds = (up_1 + up_2) / 2; |
| 180 | h = (unsigned long)( (double)jiffies/seconds/smp_num_cpus ); |
| 181 | /* actual values used by 2.4 kernels: 32 64 100 128 1000 1024 1200 */ |
| 182 | switch(h){ |
| 183 | case 30 ... 34 : Hertz = 32; break; /* ia64 emulator */ |
| 184 | case 48 ... 52 : Hertz = 50; break; |
| 185 | case 58 ... 62 : Hertz = 60; break; |
| 186 | case 63 ... 65 : Hertz = 64; break; /* StrongARM /Shark */ |
| 187 | case 95 ... 105 : Hertz = 100; break; /* normal Linux */ |
| 188 | case 124 ... 132 : Hertz = 128; break; /* MIPS, ARM */ |
| 189 | case 195 ... 204 : Hertz = 200; break; /* normal << 1 */ |
| 190 | case 253 ... 260 : Hertz = 256; break; |
| 191 | case 295 ... 304 : Hertz = 300; break; /* 3 cpus */ |
| 192 | case 393 ... 408 : Hertz = 400; break; /* normal << 2 */ |
| 193 | case 495 ... 504 : Hertz = 500; break; /* 5 cpus */ |
| 194 | case 595 ... 604 : Hertz = 600; break; /* 6 cpus */ |
| 195 | case 695 ... 704 : Hertz = 700; break; /* 7 cpus */ |
| 196 | case 790 ... 808 : Hertz = 800; break; /* normal << 3 */ |
| 197 | case 895 ... 904 : Hertz = 900; break; /* 9 cpus */ |
| 198 | case 990 ... 1010 : Hertz = 1000; break; /* ARM */ |
| 199 | case 1015 ... 1035 : Hertz = 1024; break; /* Alpha, ia64 */ |
| 200 | case 1095 ... 1104 : Hertz = 1100; break; /* 11 cpus */ |
| 201 | case 1180 ... 1220 : Hertz = 1200; break; /* Alpha */ |
| 202 | default: |
| 203 | /* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */ |
| 204 | Hertz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static void do_stats(void) |
| 209 | { |
| 210 | struct timeval t; |
| 211 | static struct timeval oldtime; |
| 212 | struct timezone timez; |
| 213 | float elapsed_time; |
| 214 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 215 | procps_status_t *cur; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 216 | int total_time, i, n; |
| 217 | static int prev_count; |
| 218 | int systime, usrtime, pid; |
| 219 | |
| 220 | struct save_hist *New_save_hist; |
| 221 | |
| 222 | /* |
| 223 | * Finds the current time (in microseconds) and calculates the time |
| 224 | * elapsed since the last update. |
| 225 | */ |
| 226 | gettimeofday(&t, &timez); |
| 227 | elapsed_time = (t.tv_sec - oldtime.tv_sec) |
| 228 | + (float) (t.tv_usec - oldtime.tv_usec) / 1000000.0; |
| 229 | oldtime.tv_sec = t.tv_sec; |
| 230 | oldtime.tv_usec = t.tv_usec; |
| 231 | |
| 232 | New_save_hist = alloca(sizeof(struct save_hist)*ntop); |
| 233 | /* |
| 234 | * Make a pass through the data to get stats. |
| 235 | */ |
| 236 | for(n = 0; n < ntop; n++) { |
| 237 | cur = top + n; |
| 238 | |
| 239 | /* |
| 240 | * Calculate time in cur process. Time is sum of user time |
| 241 | * (usrtime) plus system time (systime). |
| 242 | */ |
| 243 | systime = cur->stime; |
| 244 | usrtime = cur->utime; |
| 245 | pid = cur->pid; |
| 246 | total_time = systime + usrtime; |
| 247 | New_save_hist[n].ticks = total_time; |
| 248 | New_save_hist[n].pid = pid; |
| 249 | New_save_hist[n].stime = systime; |
| 250 | New_save_hist[n].utime = usrtime; |
| 251 | |
| 252 | /* find matching entry from previous pass */ |
| 253 | for (i = 0; i < prev_count; i++) { |
| 254 | if (save_history[i].pid == pid) { |
| 255 | total_time -= save_history[i].ticks; |
| 256 | systime -= save_history[i].stime; |
| 257 | usrtime -= save_history[i].utime; |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Calculate percent cpu time for cur task. |
| 264 | */ |
| 265 | i = (total_time * 10 * 100/Hertz) / elapsed_time; |
| 266 | if (i > 999) |
| 267 | i = 999; |
| 268 | cur->pcpu = i; |
| 269 | |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Save cur frame's information. |
| 274 | */ |
| 275 | free(save_history); |
| 276 | save_history = memcpy(xmalloc(sizeof(struct save_hist)*n), New_save_hist, |
| 277 | sizeof(struct save_hist)*n); |
| 278 | prev_count = n; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 279 | qsort(top, n, sizeof(procps_status_t), (void*)mult_lvl_cmp); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 280 | } |
| 281 | #else |
| 282 | static cmp_t sort_function; |
| 283 | #endif /* FEATURE_CPU_USAGE_PERCENTAGE */ |
| 284 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 285 | /* display generic info (meminfo / loadavg) */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 286 | static unsigned long display_generic(void) |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 287 | { |
| 288 | FILE *fp; |
| 289 | char buf[80]; |
| 290 | float avg1, avg2, avg3; |
| 291 | unsigned long total, used, mfree, shared, buffers, cached; |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 292 | unsigned int needs_conversion = 1; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 293 | |
| 294 | /* read memory info */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 295 | fp = bb_xfopen("meminfo", "r"); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 296 | |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 297 | /* |
| 298 | * Old kernels (such as 2.4.x) had a nice summary of memory info that |
| 299 | * we could parse, however this is gone entirely in 2.6. Try parsing |
| 300 | * the old way first, and if that fails, parse each field manually. |
| 301 | * |
| 302 | * First, we read in the first line. Old kernels will have bogus |
| 303 | * strings we don't care about, whereas new kernels will start right |
| 304 | * out with MemTotal: |
| 305 | * -- PFM. |
| 306 | */ |
| 307 | if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) { |
| 308 | fgets(buf, sizeof(buf), fp); /* skip first line */ |
| 309 | |
| 310 | fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu", |
| 311 | &total, &used, &mfree, &shared, &buffers, &cached); |
| 312 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 313 | /* |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 314 | * Revert to manual parsing, which incidentally already has the |
| 315 | * sizes in kilobytes. This should be safe for both 2.4 and |
| 316 | * 2.6. |
| 317 | */ |
| 318 | needs_conversion = 0; |
| 319 | |
| 320 | fscanf(fp, "MemFree: %lu %s\n", &mfree, buf); |
| 321 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 322 | /* |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 323 | * MemShared: is no longer present in 2.6. Report this as 0, |
| 324 | * to maintain consistent behavior with normal procps. |
| 325 | */ |
| 326 | if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2) |
| 327 | shared = 0; |
| 328 | |
| 329 | fscanf(fp, "Buffers: %lu %s\n", &buffers, buf); |
| 330 | fscanf(fp, "Cached: %lu %s\n", &cached, buf); |
| 331 | |
| 332 | used = total - mfree; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 333 | } |
| 334 | fclose(fp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 335 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 336 | /* read load average */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 337 | fp = bb_xfopen("loadavg", "r"); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 338 | if (fscanf(fp, "%f %f %f", &avg1, &avg2, &avg3) != 3) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 339 | bb_error_msg_and_die("failed to read '%s'", "loadavg"); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 340 | } |
| 341 | fclose(fp); |
| 342 | |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 343 | if (needs_conversion) { |
| 344 | /* convert to kilobytes */ |
| 345 | used /= 1024; |
| 346 | mfree /= 1024; |
| 347 | shared /= 1024; |
| 348 | buffers /= 1024; |
| 349 | cached /= 1024; |
| 350 | total /= 1024; |
| 351 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 352 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 353 | /* output memory info and load average */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 354 | /* clear screen & go to top */ |
| 355 | printf("\e[H\e[J" "Mem: " |
| 356 | "%ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached\n", |
| 357 | used, mfree, shared, buffers, cached); |
| 358 | printf("Load average: %.2f, %.2f, %.2f " |
| 359 | "(State: S=sleeping R=running, W=waiting)\n", |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 360 | avg1, avg2, avg3); |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 361 | return total; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
| 365 | /* display process statuses */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 366 | static void display_status(int count, int col) |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 367 | { |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 368 | procps_status_t *s = top; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 369 | char rss_str_buf[8]; |
| 370 | unsigned long total_memory = display_generic(); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 371 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 372 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 373 | /* what info of the processes is shown */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 374 | printf("\n\e[7m PID USER STATUS RSS PPID %%CPU %%MEM COMMAND\e[0m\n"); |
| 375 | #else |
| 376 | printf("\n\e[7m PID USER STATUS RSS PPID %%MEM COMMAND\e[0m\n"); |
| 377 | #endif |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 378 | |
| 379 | while (count--) { |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 380 | char *namecmd = s->short_cmd; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 381 | int pmem; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 382 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 383 | pmem = 1000.0 * s->rss / total_memory; |
| 384 | if (pmem > 999) pmem = 999; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 385 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 386 | if(s->rss > 10*1024) |
| 387 | sprintf(rss_str_buf, "%6ldM", s->rss/1024); |
Manuel Novoa III | d499330 | 2002-09-18 19:27:10 +0000 | [diff] [blame] | 388 | else |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 389 | sprintf(rss_str_buf, "%7ld", s->rss); |
| 390 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
Glenn L McGrath | f6bba3d | 2003-12-08 20:31:25 +0000 | [diff] [blame] | 391 | printf("%5d %-8s %s %s %5d %2d.%d %2u.%u ", |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 392 | s->pid, s->user, s->state, rss_str_buf, s->ppid, |
Glenn L McGrath | f6bba3d | 2003-12-08 20:31:25 +0000 | [diff] [blame] | 393 | s->pcpu/10, s->pcpu%10, pmem/10, pmem%10); |
| 394 | #else |
| 395 | printf("%5d %-8s %s %s %5d %2u.%u ", |
| 396 | s->pid, s->user, s->state, rss_str_buf, s->ppid, |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 397 | pmem/10, pmem%10); |
Glenn L McGrath | f6bba3d | 2003-12-08 20:31:25 +0000 | [diff] [blame] | 398 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 399 | if(strlen(namecmd) > col) |
| 400 | namecmd[col] = 0; |
| 401 | printf("%s\n", namecmd); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 402 | s++; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 403 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | static void clearmems(void) |
| 407 | { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 408 | free(top); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 409 | top = 0; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 410 | ntop = 0; |
| 411 | } |
| 412 | |
| 413 | #if defined CONFIG_FEATURE_USE_TERMIOS |
| 414 | #include <termios.h> |
| 415 | #include <sys/time.h> |
| 416 | #include <signal.h> |
| 417 | |
| 418 | |
| 419 | static struct termios initial_settings; |
| 420 | |
| 421 | static void reset_term(void) |
| 422 | { |
| 423 | tcsetattr(0, TCSANOW, (void *) &initial_settings); |
| 424 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 425 | clearmems(); |
| 426 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
| 427 | free(save_history); |
| 428 | #endif |
| 429 | #endif /* CONFIG_FEATURE_CLEAN_UP */ |
| 430 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 431 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 432 | static void sig_catcher (int sig) |
| 433 | { |
| 434 | reset_term(); |
| 435 | } |
| 436 | #endif /* CONFIG_FEATURE_USE_TERMIOS */ |
| 437 | |
| 438 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 439 | int top_main(int argc, char **argv) |
| 440 | { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 441 | int opt, interval, lines, col; |
| 442 | #if defined CONFIG_FEATURE_USE_TERMIOS |
| 443 | struct termios new_settings; |
| 444 | struct timeval tv; |
| 445 | fd_set readfds; |
| 446 | unsigned char c; |
| 447 | struct sigaction sa; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 448 | #endif /* CONFIG_FEATURE_USE_TERMIOS */ |
| 449 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 450 | /* Default update rate is 5 seconds */ |
| 451 | interval = 5; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 452 | |
| 453 | /* do normal option parsing */ |
| 454 | while ((opt = getopt(argc, argv, "d:")) > 0) { |
| 455 | switch (opt) { |
| 456 | case 'd': |
| 457 | interval = atoi(optarg); |
| 458 | break; |
| 459 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 460 | bb_show_usage(); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 464 | /* Default to 25 lines - 5 lines for status */ |
| 465 | lines = 25 - 5; |
| 466 | /* Default CMD format size */ |
| 467 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
| 468 | col = 35 - 6; |
| 469 | #else |
| 470 | col = 35; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 471 | #endif |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 472 | /* change to /proc */ |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 473 | if (chdir("/proc") < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 474 | bb_perror_msg_and_die("chdir('/proc')"); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 475 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 476 | #if defined CONFIG_FEATURE_USE_TERMIOS |
| 477 | tcgetattr(0, (void *) &initial_settings); |
| 478 | memcpy(&new_settings, &initial_settings, sizeof(struct termios)); |
| 479 | new_settings.c_lflag &= ~(ISIG | ICANON); /* unbuffered input */ |
| 480 | /* Turn off echoing */ |
| 481 | new_settings.c_lflag &= ~(ECHO | ECHONL); |
| 482 | |
| 483 | signal (SIGTERM, sig_catcher); |
| 484 | sigaction (SIGTERM, (struct sigaction *) 0, &sa); |
| 485 | sa.sa_flags |= SA_RESTART; |
| 486 | sa.sa_flags &= ~SA_INTERRUPT; |
| 487 | sigaction (SIGTERM, &sa, (struct sigaction *) 0); |
| 488 | sigaction (SIGINT, &sa, (struct sigaction *) 0); |
| 489 | tcsetattr(0, TCSANOW, (void *) &new_settings); |
| 490 | atexit(reset_term); |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 491 | |
| 492 | get_terminal_width_height(0, &col, &lines); |
| 493 | if (lines > 4) { |
| 494 | lines -= 5; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 495 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 496 | col = col - 80 + 35 - 6; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 497 | #else |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 498 | col = col - 80 + 35; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 499 | #endif |
| 500 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 501 | #endif /* CONFIG_FEATURE_USE_TERMIOS */ |
| 502 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
| 503 | sort_function[0] = pcpu_sort; |
| 504 | sort_function[1] = mem_sort; |
| 505 | sort_function[2] = time_sort; |
| 506 | #else |
| 507 | sort_function = mem_sort; |
| 508 | #endif |
| 509 | while (1) { |
| 510 | /* read process IDs & status for all the processes */ |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 511 | procps_status_t * p; |
| 512 | |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 513 | #ifdef CONFIG_SELINUX |
| 514 | while ((p = procps_scan(0, 0, NULL) ) != 0) { |
| 515 | #else |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 516 | while ((p = procps_scan(0)) != 0) { |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 517 | #endif |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 518 | int n = ntop; |
| 519 | |
| 520 | top = xrealloc(top, (++ntop)*sizeof(procps_status_t)); |
| 521 | memcpy(top + n, p, sizeof(procps_status_t)); |
| 522 | } |
| 523 | if (ntop == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 524 | bb_perror_msg_and_die("scandir('/proc')"); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 525 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 526 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
| 527 | if(!Hertz) { |
| 528 | init_Hertz_value(); |
| 529 | do_stats(); |
| 530 | sleep(1); |
| 531 | clearmems(); |
| 532 | continue; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 533 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 534 | do_stats(); |
| 535 | #else |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 536 | qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 537 | #endif |
| 538 | opt = lines; |
| 539 | if (opt > ntop) { |
| 540 | opt = ntop; |
| 541 | } |
| 542 | /* show status for each of the processes */ |
| 543 | display_status(opt, col); |
| 544 | #if defined CONFIG_FEATURE_USE_TERMIOS |
| 545 | tv.tv_sec = interval; |
| 546 | tv.tv_usec = 0; |
| 547 | FD_ZERO (&readfds); |
| 548 | FD_SET (0, &readfds); |
| 549 | select (1, &readfds, NULL, NULL, &tv); |
| 550 | if (FD_ISSET (0, &readfds)) { |
| 551 | if (read (0, &c, 1) <= 0) { /* signal */ |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 552 | return EXIT_FAILURE; |
| 553 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 554 | if(c == 'q' || c == initial_settings.c_cc[VINTR]) |
| 555 | return EXIT_SUCCESS; |
| 556 | if(c == 'M') { |
| 557 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
| 558 | sort_function[0] = mem_sort; |
| 559 | sort_function[1] = pcpu_sort; |
| 560 | sort_function[2] = time_sort; |
| 561 | #else |
| 562 | sort_function = mem_sort; |
| 563 | #endif |
| 564 | } |
| 565 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
| 566 | if(c == 'P') { |
| 567 | sort_function[0] = pcpu_sort; |
| 568 | sort_function[1] = mem_sort; |
| 569 | sort_function[2] = time_sort; |
| 570 | } |
| 571 | if(c == 'T') { |
| 572 | sort_function[0] = time_sort; |
| 573 | sort_function[1] = mem_sort; |
| 574 | sort_function[2] = pcpu_sort; |
| 575 | } |
| 576 | #endif |
| 577 | if(c == 'N') { |
| 578 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
| 579 | sort_function[0] = pid_sort; |
| 580 | #else |
| 581 | sort_function = pid_sort; |
| 582 | #endif |
| 583 | } |
| 584 | } |
| 585 | #else |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 586 | sleep(interval); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 587 | #endif /* CONFIG_FEATURE_USE_TERMIOS */ |
| 588 | clearmems(); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 589 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 590 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 591 | return EXIT_SUCCESS; |
| 592 | } |