[PATCH] Adjust 'ps H' to include kthread names by default
Eric van Gyzen
eric at vangyzen.net
Thu Jan 17 21:47:49 UTC 2013
On 01/17/2013 15:22, John Baldwin wrote:
> 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.
Yes, please, and thank you.
The patch looks fine to me.
> 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("<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)
> @@ -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 *);
>
>
More information about the freebsd-current
mailing list