killall, pidof: use argv0 for process matching too
[platform/upstream/busybox.git] / procps / top.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * A tiny 'top' utility.
4  *
5  * 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.
11  *
12  * NOTES:
13  * - At startup this changes to /proc, all the reads are then
14  *   relative to that.
15  *
16  * (C) Eero Tamminen <oak at welho dot com>
17  *
18  * Rewritten by Vladimir Oleynik (C) 2002 <dzo@simtreas.ru>
19  */
20
21 /* Original code Copyrights */
22 /*
23  * Copyright (c) 1992 Branko Lankester
24  * Copyright (c) 1992 Roger Binns
25  * Copyright (C) 1994-1996 Charles L. Blake.
26  * Copyright (C) 1992-1998 Michael K. Johnson
27  * May be distributed under the conditions of the
28  * GNU Library General Public License
29  */
30
31 #include "libbb.h"
32
33
34 typedef struct top_status_t {
35         unsigned long vsz;
36 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
37         unsigned long ticks;
38         unsigned pcpu; /* delta of ticks */
39 #endif
40         unsigned pid, ppid;
41         unsigned uid;
42         char state[4];
43 /* TODO: read /proc/$PID/cmdline only for processes which are displayed */
44         char cmd[64];
45 } top_status_t;
46
47 typedef struct jiffy_counts_t{
48         unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
49         unsigned long long total;
50         unsigned long long busy;
51 } jiffy_counts_t;
52
53 /* This structure stores some critical information from one frame to
54    the next. Used for finding deltas. */
55 typedef struct save_hist {
56         unsigned long ticks;
57         unsigned pid;
58 } save_hist;
59
60 typedef int (*cmp_funcp)(top_status_t *P, top_status_t *Q);
61
62 enum { SORT_DEPTH = 3 };
63
64 struct globals {
65         top_status_t *top;
66         int ntop;
67 #if ENABLE_FEATURE_USE_TERMIOS
68         struct termios initial_settings;
69 #endif
70 #if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
71         cmp_funcp sort_function;
72 #else
73         cmp_funcp sort_function[SORT_DEPTH];
74         struct save_hist *prev_hist;
75         int prev_hist_count;
76         jiffy_counts_t jif, prev_jif;
77         /* int hist_iterations; */
78         unsigned total_pcpu;
79         /* unsigned long total_vsz; */
80 #endif
81 };
82 #define G (*(struct globals*)&bb_common_bufsiz1)
83 #define top              (G.top               )
84 #define ntop             (G.ntop              )
85 #if ENABLE_FEATURE_USE_TERMIOS
86 #define initial_settings (G. initial_settings )
87 #endif
88 #define sort_function    (G.sort_function     )
89 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
90 #define prev_hist        (G.prev_hist         )
91 #define prev_hist_count  (G.prev_hist_count   )
92 #define jif              (G.jif               )
93 #define prev_jif         (G.prev_jif          )
94 #define total_pcpu       (G.total_pcpu        )
95 #endif
96
97 #define OPT_BATCH_MODE (option_mask32 & 0x4)
98
99
100 #if ENABLE_FEATURE_USE_TERMIOS
101 static int pid_sort(top_status_t *P, top_status_t *Q)
102 {
103         /* Buggy wrt pids with high bit set */
104         /* (linux pids are in [1..2^15-1]) */
105         return (Q->pid - P->pid);
106 }
107 #endif
108
109 static int mem_sort(top_status_t *P, top_status_t *Q)
110 {
111         /* We want to avoid unsigned->signed and truncation errors */
112         if (Q->vsz < P->vsz) return -1;
113         return Q->vsz != P->vsz; /* 0 if ==, 1 if > */
114 }
115
116
117 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
118
119 static int pcpu_sort(top_status_t *P, top_status_t *Q)
120 {
121         /* Buggy wrt ticks with high bit set */
122         /* Affects only processes for which ticks overflow */
123         return (int)Q->pcpu - (int)P->pcpu;
124 }
125
126 static int time_sort(top_status_t *P, top_status_t *Q)
127 {
128         /* We want to avoid unsigned->signed and truncation errors */
129         if (Q->ticks < P->ticks) return -1;
130         return Q->ticks != P->ticks; /* 0 if ==, 1 if > */
131 }
132
133 static int mult_lvl_cmp(void* a, void* b)
134 {
135         int i, cmp_val;
136
137         for (i = 0; i < SORT_DEPTH; i++) {
138                 cmp_val = (*sort_function[i])(a, b);
139                 if (cmp_val != 0)
140                         return cmp_val;
141         }
142         return 0;
143 }
144
145
146 static void get_jiffy_counts(void)
147 {
148         FILE* fp = xfopen("stat", "r");
149         prev_jif = jif;
150         if (fscanf(fp, "cpu  %lld %lld %lld %lld %lld %lld %lld %lld",
151                         &jif.usr,&jif.nic,&jif.sys,&jif.idle,
152                         &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
153                 bb_error_msg_and_die("failed to read /proc/stat");
154         }
155         fclose(fp);
156         jif.total = jif.usr + jif.nic + jif.sys + jif.idle
157                         + jif.iowait + jif.irq + jif.softirq + jif.steal;
158         /* procps 2.x does not count iowait as busy time */
159         jif.busy = jif.total - jif.idle - jif.iowait;
160 }
161
162
163 static void do_stats(void)
164 {
165         top_status_t *cur;
166         pid_t pid;
167         int i, last_i, n;
168         struct save_hist *new_hist;
169
170         get_jiffy_counts();
171         total_pcpu = 0;
172         /* total_vsz = 0; */
173         new_hist = xmalloc(sizeof(struct save_hist)*ntop);
174         /*
175          * Make a pass through the data to get stats.
176          */
177         /* hist_iterations = 0; */
178         i = 0;
179         for (n = 0; n < ntop; n++) {
180                 cur = top + n;
181
182                 /*
183                  * Calculate time in cur process.  Time is sum of user time
184                  * and system time
185                  */
186                 pid = cur->pid;
187                 new_hist[n].ticks = cur->ticks;
188                 new_hist[n].pid = pid;
189
190                 /* find matching entry from previous pass */
191                 cur->pcpu = 0;
192                 /* do not start at index 0, continue at last used one
193                  * (brought hist_iterations from ~14000 down to 172) */
194                 last_i = i;
195                 if (prev_hist_count) do {
196                         if (prev_hist[i].pid == pid) {
197                                 cur->pcpu = cur->ticks - prev_hist[i].ticks;
198                                 total_pcpu += cur->pcpu;
199                                 break;
200                         }
201                         i = (i+1) % prev_hist_count;
202                         /* hist_iterations++; */
203                 } while (i != last_i);
204                 /* total_vsz += cur->vsz; */
205         }
206
207         /*
208          * Save cur frame's information.
209          */
210         free(prev_hist);
211         prev_hist = new_hist;
212         prev_hist_count = ntop;
213 }
214 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
215
216
217 /* display generic info (meminfo / loadavg) */
218 static unsigned long display_generic(int scr_width)
219 {
220         FILE *fp;
221         char buf[80];
222         char scrbuf[80];
223         char *end;
224         unsigned long total, used, mfree, shared, buffers, cached;
225
226         /* read memory info */
227         fp = xfopen("meminfo", "r");
228
229         /*
230          * Old kernels (such as 2.4.x) had a nice summary of memory info that
231          * we could parse, however this is gone entirely in 2.6. Try parsing
232          * the old way first, and if that fails, parse each field manually.
233          *
234          * First, we read in the first line. Old kernels will have bogus
235          * strings we don't care about, whereas new kernels will start right
236          * out with MemTotal:
237          *                              -- PFM.
238          */
239         if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
240                 fgets(buf, sizeof(buf), fp);    /* skip first line */
241
242                 fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
243                         &total, &used, &mfree, &shared, &buffers, &cached);
244                 /* convert to kilobytes */
245                 used /= 1024;
246                 mfree /= 1024;
247                 shared /= 1024;
248                 buffers /= 1024;
249                 cached /= 1024;
250                 total /= 1024;
251         } else {
252                 /*
253                  * Revert to manual parsing, which incidentally already has the
254                  * sizes in kilobytes. This should be safe for both 2.4 and
255                  * 2.6.
256                  */
257
258                 fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
259
260                 /*
261                  * MemShared: is no longer present in 2.6. Report this as 0,
262                  * to maintain consistent behavior with normal procps.
263                  */
264                 if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
265                         shared = 0;
266
267                 fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
268                 fscanf(fp, "Cached: %lu %s\n", &cached, buf);
269
270                 used = total - mfree;
271         }
272         fclose(fp);
273
274         /* read load average as a string */
275         buf[0] = '\0';
276         open_read_close("loadavg", buf, sizeof(buf));
277         end = strchr(buf, ' ');
278         if (end) end = strchr(end+1, ' ');
279         if (end) end = strchr(end+1, ' ');
280         if (end) *end = '\0';
281
282         /* output memory info and load average */
283         /* clear screen & go to top */
284         if (scr_width > sizeof(scrbuf))
285                 scr_width = sizeof(scrbuf);
286         snprintf(scrbuf, scr_width,
287                 "Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached",
288                 used, mfree, shared, buffers, cached);
289
290         printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
291
292         if (ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS) {
293                 /*
294                  * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
295                  */
296                 /* using (unsigned) cast to make multiplication cheaper: */
297 #if ENABLE_FEATURE_TOP_DECIMALS
298 /* Generated code is approx +0.5k */
299 #define CALC_STAT(xxx) div_t xxx = div(1000 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff, 10)
300 #define SHOW_STAT(xxx) xxx.quot, '0'+xxx.rem
301 #define FMT "%3u.%c%%"
302 #else
303 #define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
304 #define SHOW_STAT(xxx) xxx
305 #define FMT "%3u%%"
306 #endif
307                 unsigned total_diff = (jif.total - prev_jif.total ? : 1);
308                 CALC_STAT(usr);
309                 CALC_STAT(sys);
310                 CALC_STAT(nic);
311                 CALC_STAT(idle);
312                 CALC_STAT(iowait);
313                 CALC_STAT(irq);
314                 CALC_STAT(softirq);
315                 //CALC_STAT(steal);
316
317                 snprintf(scrbuf, scr_width,
318                         /* Barely fits in 79 chars when in "decimals" mode.
319                          * %3u in practice almost never displays "100"
320                          * and thus has implicit leading space:  " 99" */
321                         "CPU:"FMT" usr"FMT" sys"FMT" nice"FMT" idle"FMT" io"FMT" irq"FMT" softirq",
322                         SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
323                         SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
324                         //, SHOW_STAT(steal) - what is this 'steal' thing?
325                         // I doubt anyone wants to know it
326                 );
327                 puts(scrbuf);
328 #undef SHOW_STAT
329 #undef CALC_STAT
330 #undef FMT
331         }
332
333         snprintf(scrbuf, scr_width, "Load average: %s", buf);
334         puts(scrbuf);
335
336         return total;
337 }
338
339 #if ENABLE_FEATURE_TOP_DECIMALS
340 #define UPSCALE 1000
341 #define CALC_STAT(name, val) div_t name = div((val), 10)
342 #define SHOW_STAT(name) name.quot, '0'+name.rem
343 #define FMT "%3u.%c"
344 #else
345 #define UPSCALE 100
346 #define CALC_STAT(name, val) unsigned name = (val)
347 #define SHOW_STAT(name) name
348 #define FMT " %3u%%"
349 #endif
350 /* display process statuses */
351 static void display_status(int count, int scr_width)
352 {
353         enum {
354                 BITS_PER_INT = sizeof(int)*8
355         };
356
357         top_status_t *s = top;
358         char vsz_str_buf[8];
359         unsigned long total_memory = display_generic(scr_width); /* or use total_vsz? */
360         /* xxx_shift and xxx_scale variables allow us to replace
361          * expensive divides with multiply and shift */
362         unsigned pmem_shift, pmem_scale, pmem_half;
363 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
364         unsigned pcpu_shift, pcpu_scale, pcpu_half;
365         unsigned busy_jifs;
366
367         /* what info of the processes is shown */
368         printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
369                 "  PID  PPID USER     STAT   VSZ %MEM %CPU COMMAND");
370 #define MIN_WIDTH \
371         sizeof( "  PID  PPID USER     STAT   VSZ %MEM %CPU C")
372 #else
373
374         /* !CPU_USAGE_PERCENTAGE */
375         printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
376                 "  PID  PPID USER     STAT   VSZ %MEM COMMAND");
377 #define MIN_WIDTH \
378         sizeof( "  PID  PPID USER     STAT   VSZ %MEM C")
379 #endif
380
381         /*
382          * MEM% = s->vsz/MemTotal
383          */
384         pmem_shift = BITS_PER_INT-11;
385         pmem_scale = UPSCALE*(1U<<(BITS_PER_INT-11)) / total_memory;
386         /* s->vsz is in kb. we want (s->vsz * pmem_scale) to never overflow */
387         while (pmem_scale >= 512) {
388                 pmem_scale /= 4;
389                 pmem_shift -= 2;
390         }
391         pmem_half = (1U << pmem_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
392 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
393         busy_jifs = jif.busy - prev_jif.busy;
394         /* This happens if there were lots of short-lived processes
395          * between two top updates (e.g. compilation) */
396         if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
397
398         /*
399          * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
400          * (pcpu is delta of sys+user time between samples)
401          */
402         /* (jif.xxx - prev_jif.xxx) and s->pcpu are
403          * in 0..~64000 range (HZ*update_interval).
404          * we assume that unsigned is at least 32-bit.
405          */
406         pcpu_shift = 6;
407         pcpu_scale = (UPSCALE*64*(uint16_t)busy_jifs ? : 1);
408         while (pcpu_scale < (1U<<(BITS_PER_INT-2))) {
409                 pcpu_scale *= 4;
410                 pcpu_shift += 2;
411         }
412         pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1);
413         /* we want (s->pcpu * pcpu_scale) to never overflow */
414         while (pcpu_scale >= 1024) {
415                 pcpu_scale /= 4;
416                 pcpu_shift -= 2;
417         }
418         pcpu_half = (1U << pcpu_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
419         /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
420 #endif
421
422         /* Ok, all prelim data is ready, go thru the list */
423         while (count-- > 0) {
424                 int col = scr_width+1;
425                 CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift);
426 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
427                 CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift);
428 #endif
429
430                 if (s->vsz >= 100*1024)
431                         sprintf(vsz_str_buf, "%6ldM", s->vsz/1024);
432                 else
433                         sprintf(vsz_str_buf, "%7ld", s->vsz);
434                 // PID PPID USER STAT VSZ %MEM [%CPU] COMMAND
435                 col -= printf("\n" "%5u%6u %-8s %s%s" FMT
436 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
437                                 FMT
438 #endif
439                                 " ",
440                                 s->pid, s->ppid, get_cached_username(s->uid),
441                                 s->state, vsz_str_buf,
442                                 SHOW_STAT(pmem)
443 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
444                                 , SHOW_STAT(pcpu)
445 #endif
446                 );
447                 if (col > 0)
448                         printf("%.*s", col, s->cmd);
449                 /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
450                         jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
451                 s++;
452         }
453         /* printf(" %d", hist_iterations); */
454         putchar(OPT_BATCH_MODE ? '\n' : '\r');
455         fflush(stdout);
456 }
457 #undef SHOW_STAT
458 #undef CALC_STAT
459 #undef FMT
460
461
462 static void clearmems(void)
463 {
464         clear_username_cache();
465         free(top);
466         top = 0;
467         ntop = 0;
468 }
469
470
471 #if ENABLE_FEATURE_USE_TERMIOS
472 #include <termios.h>
473 #include <signal.h>
474
475 static void reset_term(void)
476 {
477         tcsetattr(0, TCSANOW, (void *) &initial_settings);
478 #if ENABLE_FEATURE_CLEAN_UP
479         clearmems();
480 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
481         free(prev_hist);
482 #endif
483 #endif /* FEATURE_CLEAN_UP */
484 }
485
486 static void sig_catcher(int sig ATTRIBUTE_UNUSED)
487 {
488         reset_term();
489         exit(1);
490 }
491 #endif /* FEATURE_USE_TERMIOS */
492
493
494 int top_main(int argc, char **argv);
495 int top_main(int argc, char **argv)
496 {
497         int count, lines, col;
498         unsigned interval = 5; /* default update rate is 5 seconds */
499         unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */
500         char *sinterval, *siterations;
501 #if ENABLE_FEATURE_USE_TERMIOS
502         struct termios new_settings;
503         struct timeval tv;
504         fd_set readfds;
505         unsigned char c;
506 #endif /* FEATURE_USE_TERMIOS */
507
508         interval = 5;
509
510         /* do normal option parsing */
511         opt_complementary = "-";
512         getopt32(argc, argv, "d:n:b", &sinterval, &siterations);
513         if (option_mask32 & 0x1) interval = xatou(sinterval); // -d
514         if (option_mask32 & 0x2) iterations = xatou(siterations); // -n
515         //if (option_mask32 & 0x4) // -b
516
517         /* change to /proc */
518         xchdir("/proc");
519 #if ENABLE_FEATURE_USE_TERMIOS
520         tcgetattr(0, (void *) &initial_settings);
521         memcpy(&new_settings, &initial_settings, sizeof(struct termios));
522         /* unbuffered input, turn off echo */
523         new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
524
525         signal(SIGTERM, sig_catcher);
526         signal(SIGINT, sig_catcher);
527         tcsetattr(0, TCSANOW, (void *) &new_settings);
528         atexit(reset_term);
529 #endif /* FEATURE_USE_TERMIOS */
530
531 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
532         sort_function[0] = pcpu_sort;
533         sort_function[1] = mem_sort;
534         sort_function[2] = time_sort;
535 #else
536         sort_function = mem_sort;
537 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
538
539         while (1) {
540                 procps_status_t *p = NULL;
541
542                 /* Default to 25 lines - 5 lines for status */
543                 lines = 24 - 3 USE_FEATURE_TOP_CPU_GLOBAL_PERCENTS( - 1);
544                 col = 79;
545 #if ENABLE_FEATURE_USE_TERMIOS
546                 get_terminal_width_height(0, &col, &lines);
547                 if (lines < 5 || col < MIN_WIDTH) {
548                         sleep(interval);
549                         continue;
550                 }
551                 lines -= 3 USE_FEATURE_TOP_CPU_GLOBAL_PERCENTS( + 1);
552 #endif /* FEATURE_USE_TERMIOS */
553
554                 /* read process IDs & status for all the processes */
555                 while ((p = procps_scan(p, 0
556                                 | PSSCAN_PID
557                                 | PSSCAN_PPID
558                                 | PSSCAN_VSZ
559                                 | PSSCAN_STIME
560                                 | PSSCAN_UTIME
561                                 | PSSCAN_STATE
562                                 | PSSCAN_COMM
563                                 | PSSCAN_CMD
564                                 | PSSCAN_SID
565                                 | PSSCAN_UIDGID
566                 ))) {
567                         int n = ntop;
568                         top = xrealloc(top, (++ntop)*sizeof(top_status_t));
569                         top[n].pid = p->pid;
570                         top[n].ppid = p->ppid;
571                         top[n].vsz = p->vsz;
572 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
573                         top[n].ticks = p->stime + p->utime;
574 #endif
575                         top[n].uid = p->uid;
576                         strcpy(top[n].state, p->state);
577                         if (p->cmd)
578                                 safe_strncpy(top[n].cmd, p->cmd, sizeof(top[n].cmd));
579                         else /* mimic ps */
580                                 sprintf(top[n].cmd, "[%s]", p->comm);
581                 }
582                 if (ntop == 0) {
583                         bb_error_msg_and_die("no process info in /proc");
584                 }
585 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
586                 if (!prev_hist_count) {
587                         do_stats();
588                         sleep(1);
589                         clearmems();
590                         continue;
591                 }
592                 do_stats();
593 /* TODO: we don't need to sort all 10000 processes, we need to find top 24! */
594                 qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
595 #else
596                 qsort(top, ntop, sizeof(top_status_t), (void*)sort_function);
597 #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
598                 count = lines;
599                 if (OPT_BATCH_MODE || count > ntop) {
600                         count = ntop;
601                 }
602                 /* show status for each of the processes */
603                 display_status(count, col);
604 #if ENABLE_FEATURE_USE_TERMIOS
605                 tv.tv_sec = interval;
606                 tv.tv_usec = 0;
607                 FD_ZERO(&readfds);
608                 FD_SET(0, &readfds);
609                 select(1, &readfds, NULL, NULL, &tv);
610                 if (FD_ISSET(0, &readfds)) {
611                         if (read(0, &c, 1) <= 0) {   /* signal */
612                                 return EXIT_FAILURE;
613                         }
614                         if (c == 'q' || c == initial_settings.c_cc[VINTR])
615                                 break;
616                         if (c == 'M') {
617 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
618                                 sort_function[0] = mem_sort;
619                                 sort_function[1] = pcpu_sort;
620                                 sort_function[2] = time_sort;
621 #else
622                                 sort_function = mem_sort;
623 #endif
624                         }
625 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
626                         if (c == 'P') {
627                                 sort_function[0] = pcpu_sort;
628                                 sort_function[1] = mem_sort;
629                                 sort_function[2] = time_sort;
630                         }
631                         if (c == 'T') {
632                                 sort_function[0] = time_sort;
633                                 sort_function[1] = mem_sort;
634                                 sort_function[2] = pcpu_sort;
635                         }
636 #endif
637                         if (c == 'N') {
638 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
639                                 sort_function[0] = pid_sort;
640 #else
641                                 sort_function = pid_sort;
642 #endif
643                         }
644                 }
645                 if (!--iterations)
646                         break;
647 #else
648                 sleep(interval);
649 #endif /* FEATURE_USE_TERMIOS */
650                 clearmems();
651         }
652         if (ENABLE_FEATURE_CLEAN_UP)
653                 clearmems();
654         putchar('\n');
655         return EXIT_SUCCESS;
656 }