*
* This is written specifically for the linux /proc/<PID>/stat(m)
* files format.
-
+ *
* This reads the PIDs of all processes and their status and shows
* the status of processes (first ones that fit to screen) at given
* intervals.
* - CPU where Process was last seen running
* (to see effect of sched_setaffinity() etc)
* - CPU Time Split (idle/IO/wait etc) PER CPU
- */
-
-/* Original code Copyrights */
-/*
+ *
* Copyright (c) 1992 Branko Lankester
* Copyright (c) 1992 Roger Binns
* Copyright (C) 1994-1996 Charles L. Blake.
* Copyright (C) 1992-1998 Michael K. Johnson
- * May be distributed under the conditions of the
- * GNU Library General Public License
+ *
+ * Licensed under GPLv2, see file LICENSE in this tarball for details.
*/
#include "libbb.h"
} top_status_t;
typedef struct jiffy_counts_t {
- unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
+ /* Linux 2.4.x has only first four */
+ unsigned long long usr, nic, sys, idle;
+ unsigned long long iowait, irq, softirq, steal;
unsigned long long total;
unsigned long long busy;
} jiffy_counts_t;
return 0;
}
-/* NOINLINE so that complier doesn't unfold the call
- * causing multiple copies of the arithmatic instrns
- */
static NOINLINE int read_cpu_jiffy(FILE *fp, jiffy_counts_t *p_jif)
{
#if !ENABLE_FEATURE_TOP_SMP_CPU
- static const char fmt[] = "cpu %lld %lld %lld %lld %lld %lld %lld %lld";
+ static const char fmt[] = "cpu %llu %llu %llu %llu %llu %llu %llu %llu";
#else
- static const char fmt[] = "cp%*s %lld %lld %lld %lld %lld %lld %lld %lld";
+ static const char fmt[] = "cp%*s %llu %llu %llu %llu %llu %llu %llu %llu";
#endif
int ret;
&p_jif->usr, &p_jif->nic, &p_jif->sys, &p_jif->idle,
&p_jif->iowait, &p_jif->irq, &p_jif->softirq,
&p_jif->steal);
- if (ret > 4) {
+ if (ret >= 4) {
p_jif->total = p_jif->usr + p_jif->nic + p_jif->sys + p_jif->idle
+ p_jif->iowait + p_jif->irq + p_jif->softirq + p_jif->steal;
/* procps 2.x does not count iowait as busy time */
}
#if ENABLE_FEATURE_USE_TERMIOS
-#include <termios.h>
-#include <signal.h>
static void reset_term(void)
{
- tcsetattr(0, TCSANOW, &initial_settings);
+ tcsetattr_stdin_TCSANOW(&initial_settings);
if (ENABLE_FEATURE_CLEAN_UP) {
clearmems();
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
| PSSCAN_UTIME
| PSSCAN_STATE
| PSSCAN_COMM
-#if ENABLE_FEATURE_TOP_SMP_PROCESS
| PSSCAN_CPU
-#endif
| PSSCAN_UIDGID,
TOPMEM_MASK = 0
| PSSCAN_PID
unsigned lines, col;
int lines_rem;
unsigned interval;
- char *sinterval;
- SKIP_FEATURE_TOPMEM(const) unsigned scan_mask = TOP_MASK;
+ char *str_interval, *str_iterations;
+ IF_NOT_FEATURE_TOPMEM(const) unsigned scan_mask = TOP_MASK;
#if ENABLE_FEATURE_USE_TERMIOS
struct termios new_settings;
struct pollfd pfd[1];
#endif
/* all args are options; -n NUM */
- opt_complementary = "-:n+";
- if (getopt32(argv, "d:n:b", &sinterval, &iterations) & OPT_d) {
+ opt_complementary = "-";
+ col = getopt32(argv, "d:n:b", &str_interval, &str_iterations);
+ if (col & OPT_d) {
+ /* work around for "-d 1" -> "-d -1" done by getopt32 */
+ if (str_interval[0] == '-')
+ str_interval++;
/* Need to limit it to not overflow poll timeout */
- interval = xatou16(sinterval); /* -d */
+ interval = xatou16(str_interval);
+ }
+ if (col & OPT_n) {
+ if (str_iterations[0] == '-')
+ str_iterations++;
+ iterations = xatou(str_iterations);
}
/* change to /proc */
new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
bb_signals(BB_FATAL_SIGS, sig_catcher);
- tcsetattr(0, TCSANOW, (void *) &new_settings);
+ tcsetattr_stdin_TCSANOW(&new_settings);
#endif /* FEATURE_USE_TERMIOS */
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
if (c == 'q')
break;
if (c == 'n') {
- USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
+ IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
sort_function[0] = pid_sort;
}
if (c == 'm') {
- USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
+ IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
sort_function[0] = mem_sort;
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
sort_function[1] = pcpu_sort;
}
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
if (c == 'p') {
- USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
+ IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
sort_function[0] = pcpu_sort;
sort_function[1] = mem_sort;
sort_function[2] = time_sort;
}
if (c == 't') {
- USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
+ IF_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
sort_function[0] = time_sort;
sort_function[1] = mem_sort;
sort_function[2] = pcpu_sort;