toolbox: Add scheduling policy display to top/ps

Signed-off-by: San Mehat <san@google.com>
diff --git a/toolbox/ps.c b/toolbox/ps.c
index 3b86fa2..bc50cfa 100644
--- a/toolbox/ps.c
+++ b/toolbox/ps.c
@@ -11,6 +11,8 @@
 
 #include <pwd.h>
 
+#include <cutils/sched_policy.h>
+
 
 static char *nexttoksep(char **strp, char *sep)
 {
@@ -24,6 +26,7 @@
 
 #define SHOW_PRIO 1
 #define SHOW_TIME 2
+#define SHOW_POLICY 4
 
 static int display_flags = 0;
 
@@ -138,12 +141,26 @@
     }
     
     if(!namefilter || !strncmp(name, namefilter, strlen(namefilter))) {
-        printf("%-8s %-5d %-5d %-5d %-5d", user, pid, ppid, vss / 1024, rss * 4);
+        printf("%-9s %-5d %-5d %-6d %-5d", user, pid, ppid, vss / 1024, rss * 4);
         if(display_flags&SHOW_PRIO)
             printf(" %-5d %-5d %-5d %-5d", prio, nice, rtprio, sched);
+        if (display_flags & SHOW_POLICY) {
+            SchedPolicy p;
+            if (get_sched_policy(pid, &p) < 0)
+                printf(" un ");
+            else {
+                if (p == SP_BACKGROUND)
+                    printf(" bg ");
+                else if (p == SP_FOREGROUND)
+                    printf(" fg ");
+                else
+                    printf(" er ");
+            }
+        }
         printf(" %08x %08x %s %s", wchan, eip, state, cmdline[0] ? cmdline : name);
         if(display_flags&SHOW_TIME)
             printf(" (u:%d, s:%d)", utime, stime);
+
         printf("\n");
     }
     return 0;
@@ -186,6 +203,8 @@
             threads = 1;
         } else if(!strcmp(argv[1],"-x")) {
             display_flags |= SHOW_TIME;
+        } else if(!strcmp(argv[1],"-P")) {
+            display_flags |= SHOW_POLICY;
         } else if(!strcmp(argv[1],"-p")) {
             display_flags |= SHOW_PRIO;
         }  else if(isdigit(argv[1][0])){
@@ -197,8 +216,9 @@
         argv++;
     }
 
-    printf("USER     PID   PPID  VSIZE RSS   %sWCHAN    PC         NAME\n", 
-           (display_flags&SHOW_PRIO)?"PRIO  NICE  RTPRI SCHED ":"");
+    printf("USER     PID   PPID  VSIZE  RSS   %s %s WCHAN    PC         NAME\n", 
+           (display_flags&SHOW_PRIO)?"PRIO  NICE  RTPRI SCHED ":"",
+           (display_flags&SHOW_POLICY)?"PCY " : "");
     while((de = readdir(d)) != 0){
         if(isdigit(de->d_name[0])){
             int pid = atoi(de->d_name);
diff --git a/toolbox/top.c b/toolbox/top.c
index dcc0843..daade6d 100644
--- a/toolbox/top.c
+++ b/toolbox/top.c
@@ -39,6 +39,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <cutils/sched_policy.h>
+
 struct cpu_info {
     long unsigned utime, ntime, stime, itime;
     long unsigned iowtime, irqtime, sirqtime;
@@ -64,6 +66,7 @@
     long vss;
     long rss;
     int num_threads;
+    char policy[32];
 };
 
 struct proc_list {
@@ -88,6 +91,7 @@
 static void free_proc(struct proc_info *proc);
 static void read_procs(void);
 static int read_stat(char *filename, struct proc_info *proc);
+static void read_policy(int pid, struct proc_info *proc);
 static void add_proc(int proc_num, struct proc_info *proc);
 static int read_cmdline(char *filename, struct proc_info *proc);
 static int read_status(char *filename, struct proc_info *proc);
@@ -260,6 +264,8 @@
             sprintf(filename, "/proc/%d/status", pid);
             read_status(filename, proc);
 
+            read_policy(pid, proc);
+
             proc->num_threads = 0;
         } else {
             sprintf(filename, "/proc/%d/cmdline", pid);
@@ -289,6 +295,8 @@
                 sprintf(filename, "/proc/%d/task/%d/stat", pid, tid);
                 read_stat(filename, proc);
 
+                read_policy(tid, proc);
+
                 strcpy(proc->name, cur_proc.name);
                 proc->uid = cur_proc.uid;
                 proc->gid = cur_proc.gid;
@@ -368,6 +376,20 @@
     return 0;
 }
 
+static void read_policy(int pid, struct proc_info *proc) {
+    SchedPolicy p;
+    if (get_sched_policy(pid, &p) < 0)
+        strcpy(proc->policy, "unk");
+    else {
+        if (p == SP_BACKGROUND)
+            strcpy(proc->policy, "bg");
+        else if (p == SP_FOREGROUND)
+            strcpy(proc->policy, "fg");
+        else
+            strcpy(proc->policy, "er");
+    }
+}
+
 static int read_status(char *filename, struct proc_info *proc) {
     FILE *file;
     char line[MAX_LINE];
@@ -432,9 +454,9 @@
             total_delta_time);
     printf("\n");
     if (!threads) 
-        printf("%5s %4s %1s %5s %7s %7s %-8s %s\n", "PID", "CPU%", "S", "#THR", "VSS", "RSS", "UID", "Name");
+        printf("%5s %4s %1s %5s %7s %7s %3s %-8s %s\n", "PID", "CPU%", "S", "#THR", "VSS", "RSS", "PCY", "UID", "Name");
     else
-        printf("%5s %5s %4s %1s %7s %7s %-8s %-15s %s\n", "PID", "TID", "CPU%", "S", "VSS", "RSS", "UID", "Thread", "Proc");
+        printf("%5s %5s %4s %1s %7s %7s %3s %-8s %-15s %s\n", "PID", "TID", "CPU%", "S", "VSS", "RSS", "PCY", "UID", "Thread", "Proc");
 
     for (i = 0; i < num_new_procs; i++) {
         proc = new_procs[i];
@@ -456,11 +478,11 @@
             group_str = group_buf;
         }
         if (!threads) 
-            printf("%5d %3ld%% %c %5d %6ldK %6ldK %-8.8s %s\n", proc->pid, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads,
-                proc->vss / 1024, proc->rss * getpagesize() / 1024, user_str, proc->name[0] != 0 ? proc->name : proc->tname);
+            printf("%5d %3ld%% %c %5d %6ldK %6ldK %3s %-8.8s %s\n", proc->pid, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads,
+                proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->name[0] != 0 ? proc->name : proc->tname);
         else
-            printf("%5d %5d %3ld%% %c %6ldK %6ldK %-8.8s %-15s %s\n", proc->pid, proc->tid, proc->delta_time * 100 / total_delta_time, proc->state,
-                proc->vss / 1024, proc->rss * getpagesize() / 1024, user_str, proc->tname, proc->name);
+            printf("%5d %5d %3ld%% %c %6ldK %6ldK %3s %-8.8s %-15s %s\n", proc->pid, proc->tid, proc->delta_time * 100 / total_delta_time, proc->state,
+                proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->tname, proc->name);
     }
 }