This patch adds the following real-time format specifiers to the "-o"
option for the ps command:
    psr     Processor that process is currently assigned to
    rtprio  Realtime priority
    sched   Scheduling policy  (0,  1,  2,  3, 4,   5)
    policy  Scheduling class   (TS, FF, RR, B, ISO, IDL, ?)

Usage is something like: ps -T -o pid,rtprio,psr,policy,args

function                                             old     new   delta
.rodata                                           156133  156361    +228
out_spec                                             512     640    +128
policy_name                                            -      56     +56
procps_scan                                         1099    1142     +43
func_policy                                            -      42     +42
func_sched                                             -      20     +20
func_rtprio                                            -      20     +20
func_psr                                               -      20     +20
find_out_spec                                        130     136      +6
------------------------------------------------------------------------------
(add/remove: 5/0 grow/shrink: 4/0 up/down: 563/0)             Total: 563 bytes

Signed-off-by: Stephen Agate <[email protected]>
---
 include/libbb.h   |  8 +++++++-
 libbb/procps.c    | 75 
+++++++++++++++++++++++++++++++--------------------------------------------
 procps/Config.src |  7 +++++++
 procps/ps.c       | 44 +++++++++++++++++++++++++++++++++++++++-----
 procps/top.c      |  2 +-
 5 files changed, 85 insertions(+), 51 deletions(-)

diff --git a/include/libbb.h b/include/libbb.h
index a21f420..80295e7 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1650,6 +1650,10 @@ typedef struct procps_status_t {
 #if ENABLE_FEATURE_TOP_SMP_PROCESS
        int last_seen_on_cpu;
 #endif
+#if ENABLE_FEATURE_PS_REALTIME
+       unsigned rtprio;
+       unsigned sched;
+#endif
 } procps_status_t;
 /* flag bits for procps_scan(xx, flags) calls */
 enum {
@@ -1678,10 +1682,12 @@ enum {
                                ),
        PSSCAN_CONTEXT  = (1 << 17) * ENABLE_SELINUX,
        PSSCAN_START_TIME = 1 << 18,
-       PSSCAN_CPU      = (1 << 19) * ENABLE_FEATURE_TOP_SMP_PROCESS,
+       PSSCAN_PSR      = (1 << 19) * ENABLE_FEATURE_TOP_SMP_PROCESS,
        PSSCAN_NICE     = (1 << 20) * ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS,
        PSSCAN_RUIDGID  = (1 << 21) * ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS,
        PSSCAN_TASKS    = (1 << 22) * ENABLE_FEATURE_SHOW_THREADS,
+       PSSCAN_RTPRIO   = (1 << 23) * ENABLE_FEATURE_PS_REALTIME,
+       PSSCAN_SCHED    = (1 << 24) * ENABLE_FEATURE_PS_REALTIME,
 };
 //procps_status_t* alloc_procps_scan(void) FAST_FUNC;
 void free_procps_scan(procps_status_t* sp) FAST_FUNC;
diff --git a/libbb/procps.c b/libbb/procps.c
index 4edc54d..f4f5888 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -368,7 +368,8 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, 
int flags)
                        | PSSCAN_VSZ | PSSCAN_RSS
                        | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_START_TIME
                        | PSSCAN_TTY | PSSCAN_NICE
-                       | PSSCAN_CPU)
+                       | PSSCAN_PSR
+                       | PSSCAN_RTPRIO | PSSCAN_SCHED)
                ) {
                        char *cp, *comm1;
                        int tty;
@@ -401,11 +402,18 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* 
sp, int flags)
                                "%lu "                 /* start_time */
                                "%lu "                 /* vsize */
                                "%lu "                 /* rss */
-# if ENABLE_FEATURE_TOP_SMP_PROCESS
+# if ENABLE_FEATURE_TOP_SMP_PROCESS || ENABLE_FEATURE_PS_REALTIME
                                "%*s %*s %*s %*s %*s %*s " /*rss_rlim, 
start_code, end_code, start_stack, kstk_esp, kstk_eip */
                                "%*s %*s %*s %*s "         /*signal, blocked, 
sigignore, sigcatch */
                                "%*s %*s %*s %*s "         /*wchan, nswap, 
cnswap, exit_signal */
-                               "%d"                       /*cpu last seen on*/
+#  if ENABLE_FEATURE_TOP_SMP_PROCESS
+                               "%d"                       /*cpu last seen on */
+#  else
+                               " %*s"                     /*cpu last seen on */
+#  endif
+#  if ENABLE_FEATURE_PS_REALTIME
+                               " %u %u"                   /* rt_priority, 
policy */
+#  endif
 # endif
                                ,
                                sp->state, &sp->ppid,
@@ -418,14 +426,23 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* 
sp, int flags)
 # if ENABLE_FEATURE_TOP_SMP_PROCESS
                                , &sp->last_seen_on_cpu
 # endif
+# if ENABLE_FEATURE_PS_REALTIME
+                               , &sp->rtprio, &sp->sched
+# endif
                                );

                        if (n < 11)
                                continue; /* bogus data, get next /proc/XXX */
 # if ENABLE_FEATURE_TOP_SMP_PROCESS
-                       if (n == 11)
+                       if (n < 12)
                                sp->last_seen_on_cpu = 0;
 # endif
+# if ENABLE_FEATURE_PS_REALTIME
+                       if (n < (ENABLE_FEATURE_TOP_SMP_PROCESS?14:13)) {
+                               sp->rtprio = 0;
+                               sp->sched = 0;
+                       }
+# endif

                        /* vsz is in bytes and we want kb */
                        sp->vsz = vsz >> 10;
@@ -455,13 +472,21 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* 
sp, int flags)
                        sp->vsz = fast_strtoul_10(&cp) >> 10;
                        /* vsz is in bytes but rss is in *PAGES*! Can you 
believe that? */
                        sp->rss = fast_strtoul_10(&cp) << sp->shift_pages_to_kb;
-# if ENABLE_FEATURE_TOP_SMP_PROCESS
+# if ENABLE_FEATURE_TOP_SMP_PROCESS || ENABLE_FEATURE_PS_REALTIME
                        /* (6): rss_rlim, start_code, end_code, start_stack, 
kstk_esp, kstk_eip */
                        /* (4): signal, blocked, sigignore, sigcatch */
                        /* (4): wchan, nswap, cnswap, exit_signal */
                        cp = skip_fields(cp, 14);
-//FIXME: is it safe to assume this field exists?
+//FIXME: is it safe to assume these fields exists (i.e. Linux > 2.5.19)?
+#  if ENABLE_FEATURE_TOP_SMP_PROCESS
                        sp->last_seen_on_cpu = fast_strtoul_10(&cp);
+#  else
+                       cp = skip_fields(cp, 1);
+#  endif
+#  if ENABLE_FEATURE_PS_REALTIME
+                       sp->rtprio = (unsigned)fast_strtoul_10(&cp);
+                       sp->sched = (unsigned)fast_strtoul_10(&cp);
+#  endif
 # endif
 #endif /* FEATURE_FAST_TOP */

@@ -617,41 +642,3 @@ void FAST_FUNC read_cmdline(char *buf, int col, unsigned 
pid, const char *comm)
                snprintf(buf, col, "[%s]", comm ? comm : "?");
        }
 }
-
-/* from kernel:
-       //             pid comm S ppid pgid sid tty_nr tty_pgrp flg
-       sprintf(buffer,"%d (%s) %c %d  %d   %d  %d     %d       %lu %lu \
-%lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
-%lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
-               task->pid,
-               tcomm,
-               state,
-               ppid,
-               pgid,
-               sid,
-               tty_nr,
-               tty_pgrp,
-               task->flags,
-               min_flt,
-               cmin_flt,
-               maj_flt,
-               cmaj_flt,
-               cputime_to_clock_t(utime),
-               cputime_to_clock_t(stime),
-               cputime_to_clock_t(cutime),
-               cputime_to_clock_t(cstime),
-               priority,
-               nice,
-               num_threads,
-               // 0,
-               start_time,
-               vsize,
-               mm ? get_mm_rss(mm) : 0,
-               rsslim,
-               mm ? mm->start_code : 0,
-               mm ? mm->end_code : 0,
-               mm ? mm->start_stack : 0,
-               esp,
-               eip,
-the rest is some obsolete cruft
-*/
diff --git a/procps/Config.src b/procps/Config.src
index 527d9ee..9535617 100644
--- a/procps/Config.src
+++ b/procps/Config.src
@@ -112,6 +112,13 @@ config FEATURE_PS_TIME
        help
          Support -o time and -o etime output specifiers.

+config FEATURE_PS_REALTIME
+       bool "Enable real-time scheduling priority and policy output"
+       default y
+       depends on PS && DESKTOP
+       help
+         Support -o rtprio, -o sched, -o policy output specifiers.
+
 config FEATURE_PS_ADDITIONAL_COLUMNS
        bool "Enable additional ps columns"
        default y
diff --git a/procps/ps.c b/procps/ps.c
index 08dfce1..19c661e 100644
--- a/procps/ps.c
+++ b/procps/ps.c
@@ -371,6 +371,37 @@ static void func_time(char *buf, int size, const 
procps_status_t *ps)

 #endif

+#if ENABLE_FEATURE_TOP_SMP_PROCESS
+
+static void func_psr(char *buf, int size, const procps_status_t *ps)
+{
+       sprintf(buf, "%*u", size, ps->last_seen_on_cpu);
+}
+
+#endif
+
+#if ENABLE_FEATURE_PS_REALTIME
+
+static void func_rtprio(char *buf, int size, const procps_status_t *ps)
+{
+       sprintf(buf, "%*u", size, ps->rtprio);
+}
+
+static void func_sched(char *buf, int size, const procps_status_t *ps)
+{
+       sprintf(buf, "%*u", size, ps->sched);
+}
+
+static const char *const policy_name[] = {"TS", "FF", "RR", "B", "ISO", "IDL", 
"?"};
+
+static void func_policy(char *buf, int size, const procps_status_t *ps)
+{
+       unsigned index = MIN(ps->sched, ARRAY_SIZE(policy_name)-1);
+       safe_strncpy(buf, policy_name[index], size+1);
+}
+
+#endif
+
 #if ENABLE_SELINUX
 static void func_label(char *buf, int size, const procps_status_t *ps)
 {
@@ -379,11 +410,6 @@ static void func_label(char *buf, int size, const 
procps_status_t *ps)
 #endif

 /*
-static void func_nice(char *buf, int size, const procps_status_t *ps)
-{
-       ps->???
-}
-
 static void func_pcpu(char *buf, int size, const procps_status_t *ps)
 {
 }
@@ -415,6 +441,14 @@ static const ps_out_t out_spec[] = {
 /* Not mandated, but useful: */
        { 4                  , "stat"  ,"STAT"   ,func_state ,PSSCAN_STATE   },
        { 4                  , "rss"   ,"RSS"    ,func_rss   ,PSSCAN_RSS     },
+#if ENABLE_FEATURE_TOP_SMP_PROCESS
+       { 3                  , "psr"   ,"PSR"    ,func_psr   ,PSSCAN_PSR     },
+#endif
+#if ENABLE_FEATURE_PS_REALTIME
+       { 6                  , "rtprio","RTPRIO" ,func_rtprio,PSSCAN_RTPRIO  },
+       { 3                  , "sched" ,"SCH"    ,func_sched ,PSSCAN_SCHED   },
+       { 3                  , "policy","POL"    ,func_policy,PSSCAN_SCHED   },
+#endif
 #if ENABLE_SELINUX
        { 35                 , "label" ,"LABEL"  ,func_label ,PSSCAN_CONTEXT },
 #endif
diff --git a/procps/top.c b/procps/top.c
index 73cd285..a7a736a 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -888,7 +888,7 @@ enum {
                | PSSCAN_UTIME
                | PSSCAN_STATE
                | PSSCAN_COMM
-               | PSSCAN_CPU
+               | PSSCAN_PSR
                | PSSCAN_UIDGID,
        TOPMEM_MASK = 0
                | PSSCAN_PID
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to