Hello htop users and developers,

I've written a patch to add a -p option to htop, similar to top's
-p option.  For those unfamiliar with top's -p option, it allows you
to specify a set of pids to monitor, instead of every running process.

I've also attached a patch to tell MakeHeader.py to use Python 2; feel
free to disregard this if it breaks too many build environments.

Thanks,
Rob Hoelz
Index: ScreenManager.c
===================================================================
--- ScreenManager.c     (revision 287)
+++ ScreenManager.c     (working copy)
@@ -144,7 +144,7 @@
             this->lastScan = now;
          }
          Header_draw(this->header);
-         ProcessList_rebuildPanel(this->header->pl, false, false, false, 
false, false, NULL);
+         ProcessList_rebuildPanel(this->header->pl, false, false, false, 
false, false, NULL, NULL);
       }
       for (int i = 0; i < panels; i++) {
          Panel* panel = (Panel*) Vector_get(this->panels, i);
Index: ProcessList.c
===================================================================
--- ProcessList.c       (revision 287)
+++ ProcessList.c       (working copy)
@@ -114,6 +114,7 @@
    uid_t userId;
    bool filtering;
    const char* incFilter;
+   Hashtable *pidWhiteList;
 
    int cpuCount;
    int totalTasks;
@@ -244,6 +245,9 @@
    Hashtable_delete(this->processTable);
    Vector_delete(this->processes);
    Vector_delete(this->processes2);
+   if(this->pidWhiteList) {
+      Hashtable_delete(this->pidWhiteList);
+   }
    free(this->cpus);
    free(this->fields);
    free(this);
@@ -899,19 +903,21 @@
    }
 }
 
-void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, 
bool userOnly, uid_t userId, bool filtering, const char* incFilter) {
+void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, 
bool userOnly, uid_t userId, bool filtering, const char* incFilter, Hashtable 
*pidWhiteList) {
    if (!flags) {
       following = this->following;
       userOnly = this->userOnly;
       userId = this->userId;
       filtering = this->filtering;
       incFilter = this->incFilter;
+      pidWhiteList = this->pidWhiteList;
    } else {
       this->following = following;
       this->userOnly = userOnly;
       this->userId = userId;
       this->filtering = filtering;
       this->incFilter = incFilter;
+      this->pidWhiteList = pidWhiteList;
    }
 
    int currPos = Panel_getSelectedIndex(this->panel);
@@ -927,7 +933,8 @@
 
       if ( (!p->show)
          || (userOnly && (p->st_uid != userId))
-         || (filtering && !(String_contains_i(p->comm, incFilter))) )
+         || (filtering && !(String_contains_i(p->comm, incFilter)))
+         || (pidWhiteList && !Hashtable_get(pidWhiteList, p->pid)) )
          hidden = true;
 
       if (!hidden) {
Index: htop.c
===================================================================
--- htop.c      (revision 287)
+++ htop.c      (working copy)
@@ -51,11 +51,12 @@
 static void printHelpFlag() {
    fputs("htop " VERSION " - " COPYRIGHT "\n"
          "Released under the GNU GPL.\n\n"
-         "-C --no-color         Use a monochrome color scheme\n"
-         "-d --delay=DELAY      Set the delay between updates, in tenths of 
seconds\n"
-         "-h --help             Print this help screen\n"
-         "-s --sort-key=COLUMN  Sort by COLUMN (try --sort-key=help for a 
list)\n"
-         "-u --user=USERNAME    Show only processes of a given user\n"
+         "-C --no-color               Use a monochrome color scheme\n"
+         "-d --delay=DELAY            Set the delay between updates, in tenths 
of seconds\n"
+         "-h --help                   Print this help screen\n"
+         "-s --sort-key=COLUMN        Sort by COLUMN (try --sort-key=help for 
a list)\n"
+         "-u --user=USERNAME          Show only processes of a given user\n"
+         "-p --pid=PID,[,PID,PID...]  Show only the given pids\n"
          "-v --version          Print version info\n"
          "\n"
          "Long options may be passed with a single dash.\n\n"
@@ -260,6 +261,9 @@
    uid_t userId = 0;
    int usecolors = 1;
    TreeType treeType = TREE_TYPE_AUTO;
+   char *argCopy;
+   char *pid;
+   Hashtable *pidWhiteList = NULL;
 
    int opt, opti=0;
    static struct option long_opts[] =
@@ -271,6 +275,7 @@
       {"user",     required_argument,   0, 'u'},
       {"no-color", no_argument,         0, 'C'},
       {"no-colour",no_argument,         0, 'C'},
+      {"pid",      required_argument,   0, 'p'},
       {0,0,0,0}
    };
    int sortKey = 0;
@@ -284,7 +289,7 @@
       setlocale(LC_CTYPE, "");
 
    /* Parse arguments */
-   while ((opt = getopt_long(argc, argv, "hvCs:d:u:", long_opts, &opti))) {
+   while ((opt = getopt_long(argc, argv, "hvCs:d:u:p:", long_opts, &opti))) {
       if (opt == EOF) break;
       switch (opt) {
          case 'h':
@@ -324,6 +329,22 @@
          case 'C':
             usecolors=0;
             break;
+        case 'p':
+            argCopy = strdup(optarg);
+            pid      = strtok(argCopy, ",");
+
+            if( !pidWhiteList ) {
+               pidWhiteList = Hashtable_new(8, false);
+            }
+
+            while( pid ) {
+                unsigned int num_pid = atoi(pid);
+                Hashtable_put(pidWhiteList, num_pid, (void *) 1);
+                pid = strtok(NULL, ",");
+            }
+            free(argCopy);
+
+            break;
          default:
             exit(1);
       }
@@ -453,7 +474,7 @@
             ProcessList_sort(pl);
             refreshTimeout = 1;
          }
-         ProcessList_rebuildPanel(pl, true, following, userOnly, userId, 
filtering, incFilter.buffer);
+         ProcessList_rebuildPanel(pl, true, following, userOnly, userId, 
filtering, incFilter.buffer, pidWhiteList);
       }
       doRefresh = true;
       
Index: ProcessList.h
===================================================================
--- ProcessList.h       (revision 287)
+++ ProcessList.h       (working copy)
@@ -97,6 +97,7 @@
    uid_t userId;
    bool filtering;
    const char* incFilter;
+   Hashtable *pidWhiteList;
 
    int cpuCount;
    int totalTasks;
@@ -183,6 +184,6 @@
 
 void ProcessList_expandTree(ProcessList* this);
 
-void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, 
bool userOnly, uid_t userId, bool filtering, const char* incFilter);
+void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, 
bool userOnly, uid_t userId, bool filtering, const char* incFilter, Hashtable 
*pidWhiteList);
 
 #endif
Index: htop.1.in
===================================================================
--- htop.1.in   (revision 287)
+++ htop.1.in   (working copy)
@@ -35,6 +35,11 @@
 \fB\-s \-\-sort\-key COLUMN\fR
 Sort by this column (use \-\-sort\-key help for a column list)
 .TP
+\fB\-p \-\-pid=PID[,PID,PID...]
+Restrict the monitored processes to the given list of pids.  This option
+may be specified multiple times, and each invocation can specify a list
+of comma-separated pids.
+.TP
 \fB\-v \-\-version
 Output version information and exit
 .PP
Index: scripts/MakeHeader.py
===================================================================
--- scripts/MakeHeader.py       (revision 287)
+++ scripts/MakeHeader.py       (working copy)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python2
 
 import os, sys, string
 
------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here 
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
htop-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/htop-general

Reply via email to