[PATCH] Adjust 'ps H' to include kthread names by default

From: John Baldwin <jhb_at_freebsd.org>
Date: Thu, 17 Jan 2013 16:22:09 -0500
Running 'ps axH' on a current system results in a lot of kthreads with not 
very useful names (unless you add -c):

  PID  TT  STAT      TIME COMMAND
    0  ??  DLs    1:09.52 [kernel]
    0  ??  DLs    0:00.00 [kernel]
    0  ??  DLs    0:00.00 [kernel]
    0  ??  DLs    0:00.00 [kernel]
    0  ??  DLs    0:00.00 [kernel]
    0  ??  DLs    0:00.00 [kernel]
    0  ??  DLs    0:00.00 [kernel]
    0  ??  DLs    0:03.82 [kernel]
    0  ??  DLs    1:15.14 [kernel]
    0  ??  DLs    0:00.00 [kernel]
    0  ??  DLs   39:24.55 [kernel]
    0  ??  DLs    0:00.04 [kernel]

This patch changes this to:

  PID  TT  STAT      TIME COMMAND
    0  ??  DLs    1:09.53 [kernel/swapper]
    0  ??  DLs    0:00.00 [kernel/firmware tas]
    0  ??  DLs    0:00.00 [kernel/ffs_trim tas]
    0  ??  DLs    0:00.00 [kernel/acpi_task_0]
    0  ??  DLs    0:00.00 [kernel/acpi_task_1]
    0  ??  DLs    0:00.00 [kernel/acpi_task_2]
    0  ??  DLs    0:00.00 [kernel/aiod_bio tas]
    0  ??  DLs    0:03.82 [kernel/thread taskq]
    0  ??  DLs    1:15.19 [kernel/nvidia taskq]
    0  ??  DLs    0:00.00 [kernel/kqueue taskq]
    0  ??  DLs   39:26.82 [kernel/em0 taskq]
    0  ??  DLs    0:00.04 [kernel/mca taskq]

In theory this will affect any process for which an argv can't be fetched, but 
in practice it mostly helps with kthreads.

Index: ps.c
===================================================================
--- ps.c	(revision 245225)
+++ ps.c	(working copy)
_at__at_ -141,7 +141,7 _at__at_
 static void	*expand_list(struct listinfo *);
 static const char *
 		 fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int),
-		    KINFO *, char *, int);
+		    KINFO *, char *, char *, int);
 static void	 free_list(struct listinfo *);
 static void	 init_list(struct listinfo *, addelem_rtn, int, const char *);
 static char	*kludge_oldps_options(const char *, char *, const char *);
_at__at_ -1163,11 +1163,12 _at__at_
 
 static const char *
 fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki,
-    char *comm, int maxlen)
+    char *comm, char *thread, int maxlen)
 {
 	const char *s;
 
-	s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen);
+	s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm,
+	    ki->ki_p->ki_numthreads > 1 ? thread : NULL, maxlen);
 	return (s);
 }
 
_at__at_ -1195,7 +1196,7 _at__at_
 			ki->ki_args = strdup("<defunct>");
 		else if (UREADOK(ki) || (ki->ki_p->ki_args != NULL))
 			ki->ki_args = strdup(fmt(kvm_getargv, ki,
-			    ki->ki_p->ki_comm, MAXCOMLEN));
+			    ki->ki_p->ki_comm, ki->ki_p->ki_tdname, MAXCOMLEN));
 		else
 			asprintf(&ki->ki_args, "(%s)", ki->ki_p->ki_comm);
 		if (ki->ki_args == NULL)
_at__at_ -1206,7 +1207,7 _at__at_
 	if (needenv) {
 		if (UREADOK(ki))
 			ki->ki_env = strdup(fmt(kvm_getenvv, ki,
-			    (char *)NULL, 0));
+			    (char *)NULL, (char *)NULL, 0));
 		else
 			ki->ki_env = strdup("()");
 		if (ki->ki_env == NULL)
Index: fmt.c
===================================================================
--- fmt.c	(revision 245225)
+++ fmt.c	(working copy)
_at__at_ -105,7 +105,7 _at__at_
 }
 
 const char *
-fmt_argv(char **argv, char *cmd, size_t maxlen)
+fmt_argv(char **argv, char *cmd, char *thread, size_t maxlen)
 {
 	size_t len;
 	char *ap, *cp;
_at__at_ -122,9 +122,14 _at__at_
 	cp = malloc(len);
 	if (cp == NULL)
 		errx(1, "malloc failed");
-	if (ap == NULL)
-		sprintf(cp, "[%.*s]", (int)maxlen, cmd);
-	else if (strncmp(cmdpart(argv[0]), cmd, maxlen) != 0)
+	if (ap == NULL) {
+		if (showthreads && thread != NULL) {
+			asprintf(&ap, "%s/%s", cmd, thread);
+			sprintf(cp, "[%.*s]", (int)maxlen, ap);
+			free(ap);
+		} else
+			sprintf(cp, "[%.*s]", (int)maxlen, cmd);
+	} else if (strncmp(cmdpart(argv[0]), cmd, maxlen) != 0)
 		sprintf(cp, "%s (%.*s)", ap, (int)maxlen, cmd);
 	else
 		strcpy(cp, ap);
Index: extern.h
===================================================================
--- extern.h	(revision 245225)
+++ extern.h	(working copy)
_at__at_ -51,7 +51,7 _at__at_
 char	 *elapseds(KINFO *, VARENT *);
 char	 *emulname(KINFO *, VARENT *);
 VARENT	*find_varentry(VAR *);
-const	 char *fmt_argv(char **, char *, size_t);
+const	 char *fmt_argv(char **, char *, char *, size_t);
 double	 getpcpu(const KINFO *);
 char	 *kvar(KINFO *, VARENT *);
 char	 *label(KINFO *, VARENT *);


-- 
John Baldwin
Received on Thu Jan 17 2013 - 20:22:15 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:40:34 UTC