On Mon, Aug 24, 2020 at 12:52:46AM +0200, Klemens Nanni wrote:
> Add `t' to swap the WAIT column with RTABLE (and vice versa);  WAIT
> is wide enough to fit RTABLE, somewhat adds additional value to STATE
> and seems therefore most appropiate to hide in favour of RTABLE.
> 
> Internally, I renamed the existing CMD_rtable command to filter routing
> tables into CMD_rtableid in order to use CMD_rtable for showing them as
> that seems in line with how CMD_threads is named to show threads, etc.
> 
> format_header() semantics are slightly reworked/improved now that there
> are two changing fields;  instead of conditionally changing, it now
> always updates it accordingly - i think that makes it clearer overall.
> 
> format_next_process() now uses strlcpy() instead of snprintf() for plain
> strings as I had to touch those lines anyway.
> 
> Filtering rtables with `T' does not toggle the column, just like
> filtering users with `u' does not toggle between user and thread id.
> 
> Feedback? OK?
New diff after feedback from jmc and a little cleanup I just committed
to avoid churn here.

Index: display.c
===================================================================
RCS file: /cvs/src/usr.bin/top/display.c,v
retrieving revision 1.64
diff -u -p -r1.64 display.c
--- display.c   23 Aug 2020 21:11:55 -0000      1.64
+++ display.c   25 Aug 2020 07:33:14 -0000
@@ -826,6 +826,7 @@ show_help(void)
            "s time       - change delay between displays to `time' seconds\n"
            "T [-]rtable  - show processes associated with routing table 
`rtable'\n"
            "               (T+ shows all, T -rtable hides rtable)\n"
+           "t            - toggle the display of routing tables\n"
            "u [-]user    - show processes for `user' (u+ shows all, u -user 
hides user)\n"
            "\n");
 
Index: machine.c
===================================================================
RCS file: /cvs/src/usr.bin/top/machine.c,v
retrieving revision 1.109
diff -u -p -r1.109 machine.c
--- machine.c   25 Aug 2020 07:27:34 -0000      1.109
+++ machine.c   25 Aug 2020 07:33:14 -0000
@@ -75,8 +75,9 @@ struct handle {
 static char header[] =
        "  PID X        PRI NICE  SIZE   RES STATE     WAIT      TIME    CPU 
COMMAND";
 
-/* 0123456   -- field to fill in starts at header+6 */
+/* offsets in the header line to start alternative columns */
 #define UNAME_START 6
+#define RTABLE_START 46
 
 #define Proc_format \
        "%5d %-8.8s %3d %4d %5s %5s %-9s %-7.7s %6s %5.2f%% %s"
@@ -226,16 +227,16 @@ machine_init(struct statics *statics)
 }
 
 char *
-format_header(char *second_field)
+format_header(char *second_field, char *eighth_field)
 {
-       char *field_name, *thread_field = "     TID";
-       char *ptr;
-
-       field_name = second_field ? second_field : thread_field;
+       char *second_fieldp = second_field, *eighth_fieldp = eighth_field, *ptr;
 
        ptr = header + UNAME_START;
-       while (*field_name != '\0')
-               *ptr++ = *field_name++;
+       while (*second_fieldp != '\0')
+               *ptr++ = *second_fieldp++;
+       ptr = header + RTABLE_START;
+       while (*eighth_fieldp != '\0')
+               *ptr++ = *eighth_fieldp++;
        return (header);
 }
 
@@ -544,13 +545,12 @@ skip_processes(struct handle *hndl, int 
 
 char *
 format_next_process(struct handle *hndl, const char *(*get_userid)(uid_t, int),
-    pid_t *pid)
+    int rtable, pid_t *pid)
 {
-       char *p_wait;
        struct kinfo_proc *pp;
        int cputime;
        double pct;
-       char second_buf[16];
+       char second_buf[16], eighth_buf[8];
 
        /* find and remember the next proc structure */
        pp = *(hndl->next_proc++);
@@ -566,7 +566,11 @@ format_next_process(struct handle *hndl,
                strlcpy(second_buf, (*get_userid)(pp->p_ruid, 0),
                    sizeof(second_buf));
 
-       p_wait = pp->p_wmesg[0] ? pp->p_wmesg : "-";
+       if (rtable)
+               snprintf(eighth_buf, sizeof(eighth_buf), "%7d", pp->p_rtableid);
+       else
+               strlcpy(eighth_buf, pp->p_wmesg[0] ? pp->p_wmesg : "-",
+                   sizeof(eighth_buf));
 
        /* format this entry */
        snprintf(fmt, sizeof(fmt), Proc_format, pp->p_pid, second_buf,
@@ -575,7 +579,7 @@ format_next_process(struct handle *hndl,
            format_k(pagetok(pp->p_vm_rssize)),
            (pp->p_stat == SSLEEP && pp->p_slptime > maxslp) ?
            "idle" : state_abbr(pp),
-           p_wait, format_time(cputime), 100.0 * pct,
+           eighth_buf, format_time(cputime), 100.0 * pct,
            printable(format_comm(pp)));
 
        *pid = pp->p_pid;
Index: machine.h
===================================================================
RCS file: /cvs/src/usr.bin/top/machine.h,v
retrieving revision 1.30
diff -u -p -r1.30 machine.h
--- machine.h   23 Aug 2020 21:11:55 -0000      1.30
+++ machine.h   25 Aug 2020 07:33:14 -0000
@@ -77,6 +77,7 @@ struct process_select {
        uid_t           uid;    /* only this uid (unless uid == -1) */
        uid_t           huid;   /* hide this uid (unless huid == -1) */
        pid_t           pid;    /* only this pid (unless pid == -1) */
+       int             rtable; /* show routing tables */
        int             rtableid;       /* only this rtable (unless rtableid == 
-1) */
        int             hrtableid;      /* hide this rtable (unless hrtableid 
== -1) */
        char           *command;/* only this command (unless == NULL) */
@@ -87,14 +88,14 @@ extern int      display_init(struct stat
 
 /* machine.c */
 extern int      machine_init(struct statics *);
-extern char    *format_header(char *);
+extern char    *format_header(char *, char *);
 extern void     get_system_info(struct system_info *);
 extern struct handle
 *get_process_info(struct system_info *, struct process_select *,
                 int (*) (const void *, const void *));
 extern void     skip_processes(struct handle *, int);
 extern char    *format_next_process(struct handle *,
-                const char *(*)(uid_t, int), pid_t *);
+                const char *(*)(uid_t, int), int, pid_t *);
 extern uid_t    proc_owner(pid_t);
 
 extern struct kinfo_proc       *getprocs(int, int, int *);
Index: top.1
===================================================================
RCS file: /cvs/src/usr.bin/top/top.1,v
retrieving revision 1.76
diff -u -p -r1.76 top.1
--- top.1       23 Aug 2020 21:11:55 -0000      1.76
+++ top.1       25 Aug 2020 07:33:16 -0000
@@ -388,6 +388,8 @@ shows processes associated with all rout
 The
 .Sq -
 prefix hides proccesses associated with a single routing table.
+.It t
+Toggle the display of routing tables.
 .It u Oo - Oc Ns Ar user
 Show only those processes owned by username or UID
 .Ar user .
@@ -463,6 +465,10 @@ number on which the process is bound.
 .It WAIT
 A description of the wait channel the process is sleeping on if it's
 asleep.
+.It RTABLE
+The routing table, used instead of WAIT if
+.Fl t
+is specified.
 .It TIME
 The number of system and user CPU seconds that the process has used.
 .It CPU
Index: top.c
===================================================================
RCS file: /cvs/src/usr.bin/top/top.c,v
retrieving revision 1.105
diff -u -p -r1.105 top.c
--- top.c       23 Aug 2020 21:11:55 -0000      1.105
+++ top.c       25 Aug 2020 07:33:16 -0000
@@ -133,7 +133,8 @@ struct statics  statics;
 #define CMD_pagedown   26
 #define CMD_pageup     27
 #define CMD_grep2      28
-#define CMD_rtable     29
+#define CMD_rtableid   29
+#define CMD_rtable     30
 
 static void
 usage(void)
@@ -141,7 +142,7 @@ usage(void)
        extern char *__progname;
 
        fprintf(stderr,
-           "usage: %s [-1bCHIinqSu] [-d count] [-g string] [-o [-]field] "
+           "usage: %s [-1bCHIinqStu] [-d count] [-g string] [-o [-]field] "
            "[-p pid] [-s time]\n\t[-T [-]rtable] [-U [-]user] [number]\n",
            __progname);
 }
@@ -241,7 +242,7 @@ parseargs(int ac, char **av)
        char *endp;
        int i;
 
-       while ((i = getopt(ac, av, "1SHICbinqus:d:p:U:o:g:T:")) != -1) {
+       while ((i = getopt(ac, av, "1SHICbinqus:d:p:U:o:g:T:t")) != -1) {
                switch (i) {
                case '1':
                        combine_cpus = 1;
@@ -339,6 +340,10 @@ parseargs(int ac, char **av)
                                    "%s: invalid routing table", optarg);
                        break;
 
+               case 't':
+                       ps.rtable = true;
+                       break;
+
                default:
                        usage();
                        exit(1);
@@ -370,7 +375,9 @@ parseargs(int ac, char **av)
 int
 main(int argc, char *argv[])
 {
-       char *uname_field = "USERNAME", *header_text, *env_top;
+       char *header_text, *env_top;
+       char *uname_field = "USERNAME", *thread_field = "     TID";
+       char *wait_field = "WAIT   ", *rtable_field = " RTABLE";
        const char *(*get_userid)(uid_t, int) = user_from_uid;
        char **preset_argv = NULL, **av = argv;
        int preset_argc = 0, ac = argc, active_procs, i, ncpuonline_now;
@@ -391,6 +398,7 @@ main(int argc, char *argv[])
        ps.uid = (uid_t)-1;
        ps.huid = (uid_t)-1;
        ps.pid = (pid_t)-1;
+       ps.rtable = false;
        ps.rtableid = -1;
        ps.hrtableid = -1;
        ps.command = NULL;
@@ -573,7 +581,9 @@ restart:
                i_message();
 
                /* get the string to use for the process area header */
-               header_text = format_header(ps.threads ? NULL : uname_field);
+               header_text = format_header(
+                   ps.threads ? thread_field : uname_field,
+                   ps.rtable ? rtable_field : wait_field);
 
                /* update the header area */
                i_header(header_text);
@@ -613,7 +623,8 @@ restart:
                                char * s;
 
                                s = format_next_process(processes,
-                                   ps.threads ? NULL : get_userid, &pid);
+                                   ps.threads ? NULL : get_userid, ps.rtable,
+                                   &pid);
                                i_process(i, s, pid == hlpid);
                        }
                }
@@ -668,7 +679,7 @@ rundisplay(void)
        char ch, *iptr;
        int change, i;
        struct pollfd pfd[1];
-       static char command_chars[] = "\f qh?en#sdkriIuSopCHg+P109)(/T";
+       static char command_chars[] = "\f qh?en#sdkriIuSopCHg+P109)(/Tt";
 
        /*
         * assume valid command unless told
@@ -1035,7 +1046,7 @@ rundisplay(void)
                        if (skip < 0)
                                skip = 0;
                        break;
-               case CMD_rtable:
+               case CMD_rtableid:
                        new_message(MT_standout,
                            "Routing table: ");
                        if (readline(tempbuf, sizeof(tempbuf)) > 0) {
@@ -1052,6 +1063,12 @@ rundisplay(void)
                                putr();
                        } else
                                clear_message();
+                       break;
+               case CMD_rtable:
+                       ps.rtable = !ps.rtable;
+                       new_message(MT_standout | MT_delayed,
+                           " %sisplaying routing tables.",
+                           ps.rtable ? "D" : "Not d");
                        break;
                default:
                        new_message(MT_standout, " BAD CASE IN SWITCH!");

Reply via email to