From owner-freebsd-current@FreeBSD.ORG Thu Jan 17 21:22:15 2013 Return-Path: Delivered-To: current@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 6CBC65EB for ; Thu, 17 Jan 2013 21:22:15 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from bigwig.baldwin.cx (bigknife-pt.tunnel.tserv9.chi1.ipv6.he.net [IPv6:2001:470:1f10:75::2]) by mx1.freebsd.org (Postfix) with ESMTP id 457D9188 for ; Thu, 17 Jan 2013 21:22:15 +0000 (UTC) Received: from pakbsde14.localnet (unknown [38.105.238.108]) by bigwig.baldwin.cx (Postfix) with ESMTPSA id 8C072B953 for ; Thu, 17 Jan 2013 16:22:14 -0500 (EST) From: John Baldwin To: current@freebsd.org Subject: [PATCH] Adjust 'ps H' to include kthread names by default Date: Thu, 17 Jan 2013 16:22:09 -0500 User-Agent: KMail/1.13.5 (FreeBSD/8.2-CBSD-20110714-p22; KDE/4.5.5; amd64; ; ) MIME-Version: 1.0 Content-Type: Text/Plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Message-Id: <201301171622.09624.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.7 (bigwig.baldwin.cx); Thu, 17 Jan 2013 16:22:14 -0500 (EST) X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Jan 2013 21:22:15 -0000 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) @@ -141,7 +141,7 @@ 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 *); @@ -1163,11 +1163,12 @@ 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); } @@ -1195,7 +1196,7 @@ ki->ki_args = strdup(""); 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) @@ -1206,7 +1207,7 @@ 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) @@ -105,7 +105,7 @@ } 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; @@ -122,9 +122,14 @@ 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) @@ -51,7 +51,7 @@ 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