Re: top: toggle routing tables

2020-08-25 Thread Remi Locherer
On Tue, Aug 25, 2020 at 09:34:55AM +0200, Klemens Nanni wrote:
> 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.

I like it!
ok remi@

> 
> 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 -  1.64
> +++ display.c 25 Aug 2020 07:33:14 -
> @@ -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 -  1.109
> +++ machine.c 25 Aug 2020 07:33:14 -
> @@ -75,8 +75,9 @@ struct handle {
>  static char header[] =
>   "  PID XPRI NICE  SIZE   RES STATE WAIT  TIMECPU 
> 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: 

Re: top: toggle routing tables

2020-08-25 Thread Klemens Nanni
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 -  1.64
+++ display.c   25 Aug 2020 07:33:14 -
@@ -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 -  1.109
+++ machine.c   25 Aug 2020 07:33:14 -
@@ -75,8 +75,9 @@ struct handle {
 static char header[] =
"  PID XPRI NICE  SIZE   RES STATE WAIT  TIMECPU 
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 -  1.30
+++ machine.h   25 Aug 2020 07:33:14 -
@@ -77,6 +77,7 @@ struct process_select {
uid_t   

top: toggle routing tables

2020-08-23 Thread Klemens Nanni
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?

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 -  1.64
+++ display.c   23 Aug 2020 22:39:47 -
@@ -824,6 +824,7 @@ show_help(void)
"r count pid  - renice process `pid' to nice value `count'\n"
"S- toggle the display of system processes\n"
"s time   - change delay between displays to `time' seconds\n"
+   "t- toggle the display of routing tables\n"
"T [-]rtable  - show processes associated with routing table 
`rtable'\n"
"   (T+ shows all, T -rtable hides rtable)\n"
"u [-]user- show processes for `user' (u+ shows all, u -user 
hides user)\n"
Index: machine.c
===
RCS file: /cvs/src/usr.bin/top/machine.c,v
retrieving revision 1.108
diff -u -p -r1.108 machine.c
--- machine.c   23 Aug 2020 21:11:55 -  1.108
+++ machine.c   23 Aug 2020 22:38:15 -
@@ -75,8 +75,9 @@ struct handle {
 static char header[] =
"  PID XPRI NICE  SIZE   RES STATE WAIT  TIMECPU 
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,20 @@ 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;
+   char *second_fieldp = second_field, *eighth_fieldp = eighth_field, *ptr;
 
-   field_name = second_field ? second_field : thread_field;
-
-   ptr = header + UNAME_START;
-   while (*field_name != '\0')
-   *ptr++ = *field_name++;
+   if (second_field != NULL) {
+   ptr = header + UNAME_START;
+   while (*second_fieldp != '\0')
+   *ptr++ = *second_fieldp++;
+   }
+   if (eighth_field != NULL) {
+   ptr = header + RTABLE_START;
+   while (*eighth_fieldp != '\0')
+   *ptr++ = *eighth_fieldp++;
+   }
return (header);
 }
 
@@ -414,7 +419,7 @@ get_process_info(struct system_info *si,
 int (*compare) (const void *, const void *))
 {
int show_idle, show_system, show_threads, show_uid, show_pid, show_cmd;
-   int show_rtable, hide_rtable, hide_uid;
+   int show_rtableid, hide_rtableid, hide_uid;
int total_procs, active_procs;
struct kinfo_proc **prefp, *pp;
int what = KERN_PROC_ALL;
@@ -446,8 +451,8 @@ get_process_info(struct system_info *si,
show_uid = sel->uid != (uid_t)-1;
hide_uid = sel->huid != (uid_t)-1;
show_pid = sel->pid != (pid_t)-1;
-   show_rtable = sel->rtableid != -1;
-   hide_rtable = sel->hrtableid != -1;
+   show_rtableid = sel->rtableid != -1;
+   hide_rtableid = sel->hrtableid != -1;
show_cmd = sel->command != NULL;
 
/* count up process states and get pointers to interesting procs */
@@ -476,8 +481,8 @@ get_process_info(struct system_info *si,
(!hide_uid || pp->p_ruid != sel->huid) &&
(!show_uid || pp->p_ruid == sel->uid) &&
(!show_pid || pp->p_pid == sel->pid) &&
-   (!hide_rtable || pp->p_rtableid != sel->hrtableid) 
&&
-   (!show_rtable || pp->p_rtableid == sel->rtableid) &&
+   (!hide_rtableid || pp->p_rtableid != 
sel->hrtableid) &&
+   (!show_rtableid || pp->p_rtableid == sel->rtableid) 
&&
(!show_cmd || cmd_matches(pp, sel->command))) {
*prefp++ = pp;