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> |
| 36 | #include "busybox.h" |
| 37 | |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 38 | //#define CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE /* + 2k */ |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 39 | |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 40 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 41 | #include <time.h> |
| 42 | #include <sys/time.h> |
| 43 | #include <fcntl.h> |
| 44 | #include <netinet/in.h> /* htons */ |
| 45 | #endif |
| 46 | |
| 47 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 48 | typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 49 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 50 | static procps_status_t *top; /* Hehe */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 51 | static int ntop; |
| 52 | |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 53 | #ifdef CONFIG_FEATURE_USE_TERMIOS |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 54 | static int pid_sort (procps_status_t *P, procps_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 55 | { |
Glenn L McGrath | 225be8c | 2004-09-14 18:56:52 +0000 | [diff] [blame] | 56 | return (Q->pid - P->pid); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 57 | } |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 58 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 59 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 60 | static int mem_sort (procps_status_t *P, procps_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 61 | { |
Glenn L McGrath | 225be8c | 2004-09-14 18:56:52 +0000 | [diff] [blame] | 62 | return (int)(Q->rss - P->rss); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 63 | } |
| 64 | |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 65 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 66 | |
| 67 | #define sort_depth 3 |
| 68 | static cmp_t sort_function[sort_depth]; |
| 69 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 70 | static int pcpu_sort (procps_status_t *P, procps_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 71 | { |
Glenn L McGrath | 225be8c | 2004-09-14 18:56:52 +0000 | [diff] [blame] | 72 | return (Q->pcpu - P->pcpu); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 75 | static int time_sort (procps_status_t *P, procps_status_t *Q) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 76 | { |
Glenn L McGrath | 225be8c | 2004-09-14 18:56:52 +0000 | [diff] [blame] | 77 | return (int)((Q->stime + Q->utime) - (P->stime + P->utime)); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 80 | static int mult_lvl_cmp(void* a, void* b) { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 81 | int i, cmp_val; |
| 82 | |
| 83 | 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; |
| 89 | } |
| 90 | |
| 91 | /* This structure stores some critical information from one frame to |
| 92 | the next. mostly used for sorting. Added cumulative and resident fields. */ |
| 93 | struct save_hist { |
| 94 | int ticks; |
| 95 | int pid; |
| 96 | int utime; |
| 97 | int stime; |
| 98 | }; |
| 99 | |
| 100 | /* |
| 101 | * Calculates percent cpu usage for each task. |
| 102 | */ |
| 103 | |
| 104 | static struct save_hist *save_history; |
| 105 | |
| 106 | static 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 Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 113 | * in /proc as being in architecture-dependent units that have no relation |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 114 | * 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 | |
| 133 | #define FILE_TO_BUF(filename, fd) do{ \ |
| 134 | if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 135 | bb_perror_msg_and_die("/proc not be mounted?"); \ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 136 | } \ |
| 137 | lseek(fd, 0L, SEEK_SET); \ |
| 138 | if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 139 | bb_perror_msg_and_die("%s", filename); \ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 140 | } \ |
| 141 | buf[local_n] = '\0'; \ |
| 142 | }while(0) |
| 143 | |
| 144 | #define FILE_TO_BUF2(filename, fd) do{ \ |
| 145 | lseek(fd, 0L, SEEK_SET); \ |
| 146 | if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 147 | bb_perror_msg_and_die("%s", filename); \ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 148 | } \ |
| 149 | buf[local_n] = '\0'; \ |
| 150 | }while(0) |
| 151 | |
| 152 | static void init_Hertz_value(void) { |
| 153 | unsigned long user_j, nice_j, sys_j, other_j; /* jiffies (clock ticks) */ |
| 154 | double up_1, up_2, seconds; |
| 155 | unsigned long jiffies, h; |
| 156 | char buf[80]; |
| 157 | int uptime_fd = -1; |
| 158 | int stat_fd = -1; |
| 159 | |
| 160 | long smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF); |
| 161 | |
| 162 | if(smp_num_cpus<1) smp_num_cpus=1; |
| 163 | do { |
| 164 | int local_n; |
| 165 | |
| 166 | FILE_TO_BUF("uptime", uptime_fd); |
| 167 | up_1 = strtod(buf, 0); |
| 168 | FILE_TO_BUF("stat", stat_fd); |
| 169 | sscanf(buf, "cpu %lu %lu %lu %lu", &user_j, &nice_j, &sys_j, &other_j); |
| 170 | FILE_TO_BUF2("uptime", uptime_fd); |
| 171 | up_2 = strtod(buf, 0); |
| 172 | } while((long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */ |
| 173 | |
| 174 | close(uptime_fd); |
| 175 | close(stat_fd); |
| 176 | |
| 177 | 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 | } |
| 205 | } |
| 206 | |
| 207 | static void do_stats(void) |
| 208 | { |
| 209 | struct timeval t; |
| 210 | static struct timeval oldtime; |
| 211 | struct timezone timez; |
| 212 | float elapsed_time; |
| 213 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 214 | procps_status_t *cur; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 215 | int total_time, i, n; |
| 216 | static int prev_count; |
| 217 | int systime, usrtime, pid; |
| 218 | |
| 219 | struct save_hist *New_save_hist; |
| 220 | |
| 221 | /* |
| 222 | * Finds the current time (in microseconds) and calculates the time |
| 223 | * elapsed since the last update. |
| 224 | */ |
| 225 | 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; |
| 230 | |
| 231 | 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; |
| 268 | |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Save cur frame's information. |
| 273 | */ |
| 274 | free(save_history); |
| 275 | save_history = memcpy(xmalloc(sizeof(struct save_hist)*n), New_save_hist, |
| 276 | sizeof(struct save_hist)*n); |
| 277 | prev_count = n; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 278 | qsort(top, n, sizeof(procps_status_t), (void*)mult_lvl_cmp); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 279 | } |
| 280 | #else |
| 281 | static cmp_t sort_function; |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 282 | #endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 283 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 284 | /* display generic info (meminfo / loadavg) */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 285 | static unsigned long display_generic(void) |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 286 | { |
| 287 | FILE *fp; |
| 288 | char buf[80]; |
| 289 | float avg1, avg2, avg3; |
| 290 | unsigned long total, used, mfree, shared, buffers, cached; |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 291 | unsigned int needs_conversion = 1; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 292 | |
| 293 | /* read memory info */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 294 | fp = bb_xfopen("meminfo", "r"); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 295 | |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 296 | /* |
| 297 | * Old kernels (such as 2.4.x) had a nice summary of memory info that |
| 298 | * we could parse, however this is gone entirely in 2.6. Try parsing |
| 299 | * the old way first, and if that fails, parse each field manually. |
| 300 | * |
| 301 | * First, we read in the first line. Old kernels will have bogus |
| 302 | * strings we don't care about, whereas new kernels will start right |
| 303 | * out with MemTotal: |
"Vladimir N. Oleynik" | 70678bc | 2005-11-29 12:32:33 +0000 | [diff] [blame] | 304 | * -- PFM. |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 305 | */ |
| 306 | if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) { |
"Vladimir N. Oleynik" | 70678bc | 2005-11-29 12:32:33 +0000 | [diff] [blame] | 307 | fgets(buf, sizeof(buf), fp); /* skip first line */ |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 308 | |
| 309 | fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu", |
| 310 | &total, &used, &mfree, &shared, &buffers, &cached); |
| 311 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 312 | /* |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 313 | * Revert to manual parsing, which incidentally already has the |
| 314 | * sizes in kilobytes. This should be safe for both 2.4 and |
| 315 | * 2.6. |
| 316 | */ |
| 317 | needs_conversion = 0; |
| 318 | |
| 319 | fscanf(fp, "MemFree: %lu %s\n", &mfree, buf); |
| 320 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 321 | /* |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 322 | * MemShared: is no longer present in 2.6. Report this as 0, |
| 323 | * to maintain consistent behavior with normal procps. |
| 324 | */ |
| 325 | if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2) |
| 326 | shared = 0; |
| 327 | |
| 328 | fscanf(fp, "Buffers: %lu %s\n", &buffers, buf); |
| 329 | fscanf(fp, "Cached: %lu %s\n", &cached, buf); |
| 330 | |
| 331 | used = total - mfree; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 332 | } |
| 333 | fclose(fp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 334 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 335 | /* read load average */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 336 | fp = bb_xfopen("loadavg", "r"); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 337 | if (fscanf(fp, "%f %f %f", &avg1, &avg2, &avg3) != 3) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 338 | bb_error_msg_and_die("failed to read '%s'", "loadavg"); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 339 | } |
| 340 | fclose(fp); |
| 341 | |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 342 | if (needs_conversion) { |
| 343 | /* convert to kilobytes */ |
| 344 | used /= 1024; |
| 345 | mfree /= 1024; |
| 346 | shared /= 1024; |
| 347 | buffers /= 1024; |
| 348 | cached /= 1024; |
| 349 | total /= 1024; |
| 350 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 351 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 352 | /* output memory info and load average */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 353 | /* clear screen & go to top */ |
| 354 | printf("\e[H\e[J" "Mem: " |
| 355 | "%ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached\n", |
| 356 | used, mfree, shared, buffers, cached); |
| 357 | printf("Load average: %.2f, %.2f, %.2f " |
| 358 | "(State: S=sleeping R=running, W=waiting)\n", |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 359 | avg1, avg2, avg3); |
Eric Andersen | 7857c03 | 2003-10-11 18:47:20 +0000 | [diff] [blame] | 360 | return total; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
| 364 | /* display process statuses */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 365 | static void display_status(int count, int col) |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 366 | { |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 367 | procps_status_t *s = top; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 368 | char rss_str_buf[8]; |
| 369 | unsigned long total_memory = display_generic(); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 370 | |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 371 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 372 | /* what info of the processes is shown */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 373 | printf("\n\e[7m PID USER STATUS RSS PPID %%CPU %%MEM COMMAND\e[0m\n"); |
| 374 | #else |
| 375 | printf("\n\e[7m PID USER STATUS RSS PPID %%MEM COMMAND\e[0m\n"); |
| 376 | #endif |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 377 | |
| 378 | while (count--) { |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 379 | char *namecmd = s->short_cmd; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 380 | int pmem; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 381 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 382 | pmem = 1000.0 * s->rss / total_memory; |
| 383 | if (pmem > 999) pmem = 999; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 384 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 385 | if(s->rss > 10*1024) |
| 386 | sprintf(rss_str_buf, "%6ldM", s->rss/1024); |
Manuel Novoa III | d499330 | 2002-09-18 19:27:10 +0000 | [diff] [blame] | 387 | else |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 388 | sprintf(rss_str_buf, "%7ld", s->rss); |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 389 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Glenn L McGrath | f6bba3d | 2003-12-08 20:31:25 +0000 | [diff] [blame] | 390 | printf("%5d %-8s %s %s %5d %2d.%d %2u.%u ", |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 391 | s->pid, s->user, s->state, rss_str_buf, s->ppid, |
Glenn L McGrath | f6bba3d | 2003-12-08 20:31:25 +0000 | [diff] [blame] | 392 | s->pcpu/10, s->pcpu%10, pmem/10, pmem%10); |
| 393 | #else |
| 394 | printf("%5d %-8s %s %s %5d %2u.%u ", |
| 395 | s->pid, s->user, s->state, rss_str_buf, s->ppid, |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 396 | pmem/10, pmem%10); |
Glenn L McGrath | f6bba3d | 2003-12-08 20:31:25 +0000 | [diff] [blame] | 397 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 398 | if(strlen(namecmd) > col) |
| 399 | namecmd[col] = 0; |
| 400 | printf("%s\n", namecmd); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 401 | s++; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 402 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static void clearmems(void) |
| 406 | { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 407 | free(top); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 408 | top = 0; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 409 | ntop = 0; |
| 410 | } |
| 411 | |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 412 | #ifdef CONFIG_FEATURE_USE_TERMIOS |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 413 | #include <termios.h> |
| 414 | #include <sys/time.h> |
| 415 | #include <signal.h> |
| 416 | |
| 417 | |
| 418 | static struct termios initial_settings; |
| 419 | |
| 420 | static void reset_term(void) |
| 421 | { |
| 422 | tcsetattr(0, TCSANOW, (void *) &initial_settings); |
| 423 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 424 | clearmems(); |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 425 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 426 | free(save_history); |
| 427 | #endif |
| 428 | #endif /* CONFIG_FEATURE_CLEAN_UP */ |
| 429 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 430 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 431 | static void sig_catcher (int sig) |
| 432 | { |
| 433 | reset_term(); |
| 434 | } |
| 435 | #endif /* CONFIG_FEATURE_USE_TERMIOS */ |
| 436 | |
| 437 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 438 | int top_main(int argc, char **argv) |
| 439 | { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 440 | int opt, interval, lines, col; |
"Vladimir N. Oleynik" | 70678bc | 2005-11-29 12:32:33 +0000 | [diff] [blame] | 441 | char *sinterval; |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 442 | #ifdef CONFIG_FEATURE_USE_TERMIOS |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 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 | /* do normal option parsing */ |
"Vladimir N. Oleynik" | 70678bc | 2005-11-29 12:32:33 +0000 | [diff] [blame] | 451 | opt = bb_getopt_ulflags(argc, argv, "d:", &sinterval); |
| 452 | if((opt & 1)) { |
| 453 | interval = atoi(sinterval); |
| 454 | } else { |
| 455 | /* Default update rate is 5 seconds */ |
| 456 | interval = 5; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 459 | /* Default to 25 lines - 5 lines for status */ |
| 460 | lines = 25 - 5; |
| 461 | /* Default CMD format size */ |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 462 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 463 | col = 35 - 6; |
| 464 | #else |
| 465 | col = 35; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 466 | #endif |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 467 | /* change to /proc */ |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 468 | if (chdir("/proc") < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 469 | bb_perror_msg_and_die("chdir('/proc')"); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 470 | } |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 471 | #ifdef CONFIG_FEATURE_USE_TERMIOS |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 472 | tcgetattr(0, (void *) &initial_settings); |
| 473 | memcpy(&new_settings, &initial_settings, sizeof(struct termios)); |
| 474 | new_settings.c_lflag &= ~(ISIG | ICANON); /* unbuffered input */ |
| 475 | /* Turn off echoing */ |
| 476 | new_settings.c_lflag &= ~(ECHO | ECHONL); |
| 477 | |
| 478 | signal (SIGTERM, sig_catcher); |
| 479 | sigaction (SIGTERM, (struct sigaction *) 0, &sa); |
| 480 | sa.sa_flags |= SA_RESTART; |
| 481 | sa.sa_flags &= ~SA_INTERRUPT; |
| 482 | sigaction (SIGTERM, &sa, (struct sigaction *) 0); |
| 483 | sigaction (SIGINT, &sa, (struct sigaction *) 0); |
| 484 | tcsetattr(0, TCSANOW, (void *) &new_settings); |
| 485 | atexit(reset_term); |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 486 | |
| 487 | get_terminal_width_height(0, &col, &lines); |
| 488 | if (lines > 4) { |
| 489 | lines -= 5; |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 490 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 491 | col = col - 80 + 35 - 6; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 492 | #else |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 493 | col = col - 80 + 35; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 494 | #endif |
| 495 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 496 | #endif /* CONFIG_FEATURE_USE_TERMIOS */ |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 497 | |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 498 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 499 | sort_function[0] = pcpu_sort; |
| 500 | sort_function[1] = mem_sort; |
| 501 | sort_function[2] = time_sort; |
| 502 | #else |
| 503 | sort_function = mem_sort; |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 504 | #endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */ |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 505 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 506 | while (1) { |
| 507 | /* read process IDs & status for all the processes */ |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 508 | procps_status_t * p; |
| 509 | |
| 510 | while ((p = procps_scan(0)) != 0) { |
| 511 | int n = ntop; |
| 512 | |
| 513 | top = xrealloc(top, (++ntop)*sizeof(procps_status_t)); |
| 514 | memcpy(top + n, p, sizeof(procps_status_t)); |
| 515 | } |
| 516 | if (ntop == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 517 | bb_perror_msg_and_die("scandir('/proc')"); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 518 | } |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 519 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 520 | if(!Hertz) { |
| 521 | init_Hertz_value(); |
| 522 | do_stats(); |
| 523 | sleep(1); |
| 524 | clearmems(); |
| 525 | continue; |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 526 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 527 | do_stats(); |
| 528 | #else |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 529 | qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function); |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 530 | #endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 531 | opt = lines; |
| 532 | if (opt > ntop) { |
| 533 | opt = ntop; |
| 534 | } |
| 535 | /* show status for each of the processes */ |
| 536 | display_status(opt, col); |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 537 | #ifdef CONFIG_FEATURE_USE_TERMIOS |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 538 | tv.tv_sec = interval; |
| 539 | tv.tv_usec = 0; |
| 540 | FD_ZERO (&readfds); |
| 541 | FD_SET (0, &readfds); |
| 542 | select (1, &readfds, NULL, NULL, &tv); |
| 543 | if (FD_ISSET (0, &readfds)) { |
| 544 | if (read (0, &c, 1) <= 0) { /* signal */ |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 545 | return EXIT_FAILURE; |
| 546 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 547 | if(c == 'q' || c == initial_settings.c_cc[VINTR]) |
| 548 | return EXIT_SUCCESS; |
| 549 | if(c == 'M') { |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 550 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 551 | sort_function[0] = mem_sort; |
| 552 | sort_function[1] = pcpu_sort; |
| 553 | sort_function[2] = time_sort; |
| 554 | #else |
| 555 | sort_function = mem_sort; |
| 556 | #endif |
| 557 | } |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 558 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 559 | if(c == 'P') { |
| 560 | sort_function[0] = pcpu_sort; |
| 561 | sort_function[1] = mem_sort; |
| 562 | sort_function[2] = time_sort; |
| 563 | } |
| 564 | if(c == 'T') { |
| 565 | sort_function[0] = time_sort; |
| 566 | sort_function[1] = mem_sort; |
| 567 | sort_function[2] = pcpu_sort; |
| 568 | } |
| 569 | #endif |
| 570 | if(c == 'N') { |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 571 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 572 | sort_function[0] = pid_sort; |
| 573 | #else |
| 574 | sort_function = pid_sort; |
| 575 | #endif |
| 576 | } |
| 577 | } |
| 578 | #else |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 579 | sleep(interval); |
Mike Frysinger | 223b887 | 2005-07-30 09:42:05 +0000 | [diff] [blame] | 580 | #endif /* CONFIG_FEATURE_USE_TERMIOS */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 581 | clearmems(); |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 582 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 583 | |
Eric Andersen | 420b208 | 2002-09-17 22:14:58 +0000 | [diff] [blame] | 584 | return EXIT_SUCCESS; |
| 585 | } |