Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 6 Jul 2020 16:33:29 +0000 (UTC)
From:      Pawel Biernacki <kaktus@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r362967 - in head/sys: kern sys
Message-ID:  <202007061633.066GXTY9087195@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kaktus
Date: Mon Jul  6 16:33:28 2020
New Revision: 362967
URL: https://svnweb.freebsd.org/changeset/base/362967

Log:
  kern.tty_info_kstacks: add a compact format
  
  Add a more compact display format for kern.tty_info_kstacks inspired by
  procstat -kk. Set it as a default one.
  
  # sysctl kern.tty_info_kstacks=1
  kern.tty_info_kstacks: 0 -> 1
  # sleep 2
  ^T
  load: 0.17  cmd: sleep 623 [nanslp] 0.72r 0.00u 0.00s 0% 2124k
  #0 0xffffffff80c4443e at mi_switch+0xbe
  #1 0xffffffff80c98044 at sleepq_catch_signals+0x494
  #2 0xffffffff80c982c2 at sleepq_timedwait_sig+0x12
  #3 0xffffffff80c43af3 at _sleep+0x193
  #4 0xffffffff80c50e31 at kern_clock_nanosleep+0x1a1
  #5 0xffffffff80c5119b at sys_nanosleep+0x3b
  #6 0xffffffff810ffc69 at amd64_syscall+0x119
  #7 0xffffffff810d5520 at fast_syscall_common+0x101
  sleep: about 1 second(s) left out of the original 2
  ^C
  # sysctl kern.tty_info_kstacks=2
  kern.tty_info_kstacks: 1 -> 2
  # sleep 2
  ^T
  load: 0.24  cmd: sleep 625 [nanslp] 0.81r 0.00u 0.00s 0% 2124k
  mi_switch+0xbe sleepq_catch_signals+0x494 sleepq_timedwait_sig+0x12
  sleep+0x193 kern_clock_nanosleep+0x1a1 sys_nanosleep+0x3b
  amd64_syscall+0x119 fast_syscall_common+0x101
  sleep: about 1 second(s) left out of the original 2
  ^C
  
  Suggested by:	avg
  Reviewed by:	mjg
  Relnotes:	yes
  Sponsored by:	Mysterious Code Ltd.
  Differential Revision:	https://reviews.freebsd.org/D25487

Modified:
  head/sys/kern/subr_stack.c
  head/sys/kern/tty_info.c
  head/sys/sys/stack.h

Modified: head/sys/kern/subr_stack.c
==============================================================================
--- head/sys/kern/subr_stack.c	Mon Jul  6 16:33:21 2020	(r362966)
+++ head/sys/kern/subr_stack.c	Mon Jul  6 16:33:28 2020	(r362967)
@@ -170,7 +170,8 @@ stack_print_short_ddb(const struct stack *st)
  * flags - M_WAITOK or M_NOWAIT (EWOULDBLOCK).
  */
 int
-stack_sbuf_print_flags(struct sbuf *sb, const struct stack *st, int flags)
+stack_sbuf_print_flags(struct sbuf *sb, const struct stack *st, int flags,
+    enum stack_sbuf_fmt format)
 {
 	char namebuf[64];
 	long offset;
@@ -182,9 +183,19 @@ stack_sbuf_print_flags(struct sbuf *sb, const struct s
 		    &offset, flags);
 		if (error == EWOULDBLOCK)
 			return (error);
-		sbuf_printf(sb, "#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
-		    namebuf, offset);
+		switch (format) {
+		case STACK_SBUF_FMT_LONG:
+			sbuf_printf(sb, "#%d %p at %s+%#lx\n", i,
+			    (void *)st->pcs[i], namebuf, offset);
+			break;
+		case STACK_SBUF_FMT_COMPACT:
+			sbuf_printf(sb, "%s+%#lx ", namebuf, offset);
+			break;
+		default:
+			__assert_unreachable();
+		}
 	}
+	sbuf_nl_terminate(sb);
 	return (0);
 }
 
@@ -192,7 +203,7 @@ void
 stack_sbuf_print(struct sbuf *sb, const struct stack *st)
 {
 
-	(void)stack_sbuf_print_flags(sb, st, M_WAITOK);
+	(void)stack_sbuf_print_flags(sb, st, M_WAITOK, STACK_SBUF_FMT_LONG);
 }
 
 #if defined(DDB) || defined(WITNESS)

Modified: head/sys/kern/tty_info.c
==============================================================================
--- head/sys/kern/tty_info.c	Mon Jul  6 16:33:21 2020	(r362966)
+++ head/sys/kern/tty_info.c	Mon Jul  6 16:33:28 2020	(r362967)
@@ -239,10 +239,36 @@ sbuf_tty_drain(void *a, const char *d, int len)
 }
 
 #ifdef STACK
-static bool tty_info_kstacks = true;
-SYSCTL_BOOL(_kern, OID_AUTO, tty_info_kstacks, CTLFLAG_RWTUN,
-    &tty_info_kstacks, 0,
-    "Enable printing kernel stack(9) traces on ^T (tty info)");
+static int tty_info_kstacks = STACK_SBUF_FMT_LONG;
+
+static int
+sysctl_tty_info_kstacks(SYSCTL_HANDLER_ARGS)
+{
+	enum stack_sbuf_fmt val;
+	int error;
+
+	val = tty_info_kstacks;
+	error = sysctl_handle_int(oidp, &val, 0, req);
+	if (error != 0 || req->newptr == NULL)
+		return (error);
+
+	switch (val) {
+	case STACK_SBUF_FMT_NONE:
+	case STACK_SBUF_FMT_LONG:
+	case STACK_SBUF_FMT_COMPACT:
+		tty_info_kstacks = val;
+		break;
+	default:
+		error = EINVAL;
+	}
+
+	return (error);
+}
+SYSCTL_PROC(_kern, OID_AUTO, tty_info_kstacks,
+    CTLFLAG_RWTUN | CTLFLAG_MPSAFE | CTLTYPE_INT, NULL, 0,
+    sysctl_tty_info_kstacks, "I",
+    "Adjust format of kernel stack(9) traces on ^T (tty info): "
+    "0 - disabled; 1 - long; 2 - compact");
 #endif
 
 /*
@@ -254,7 +280,8 @@ tty_info(struct tty *tp)
 	struct timeval rtime, utime, stime;
 #ifdef STACK
 	struct stack stack;
-	int sterr;
+	int sterr, kstacks_val;
+	bool print_kstacks;
 #endif
 	struct proc *p, *ppick;
 	struct thread *td, *tdpick;
@@ -337,7 +364,10 @@ tty_info(struct tty *tp)
 		state = "unknown";
 	pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT;
 #ifdef STACK
-	if (tty_info_kstacks) {
+	kstacks_val = atomic_load_int(&tty_info_kstacks);
+	print_kstacks = (kstacks_val != STACK_SBUF_FMT_NONE);
+
+	if (print_kstacks) {
 		if (TD_IS_SWAPPED(td))
 			sterr = ENOENT;
 		else
@@ -366,8 +396,8 @@ tty_info(struct tty *tp)
 	    pctcpu / 100, rss);
 
 #ifdef STACK
-	if (tty_info_kstacks && sterr == 0)
-		stack_sbuf_print_flags(&sb, &stack, M_NOWAIT);
+	if (print_kstacks && sterr == 0)
+		stack_sbuf_print_flags(&sb, &stack, M_NOWAIT, kstacks_val);
 #endif
 
 out:

Modified: head/sys/sys/stack.h
==============================================================================
--- head/sys/sys/stack.h	Mon Jul  6 16:33:21 2020	(r362966)
+++ head/sys/sys/stack.h	Mon Jul  6 16:33:28 2020	(r362967)
@@ -39,6 +39,12 @@ MALLOC_DECLARE(M_STACK);
 
 struct sbuf;
 
+enum stack_sbuf_fmt {
+	STACK_SBUF_FMT_NONE	= 0,
+	STACK_SBUF_FMT_LONG	= 1,
+	STACK_SBUF_FMT_COMPACT	= 2,
+};
+
 /* MI Routines. */
 struct stack	*stack_create(int);
 void		 stack_destroy(struct stack *);
@@ -52,7 +58,7 @@ void		 stack_print_short_ddb(const struct stack *);
 void		 stack_sbuf_print(struct sbuf *, const struct stack *);
 void		 stack_sbuf_print_ddb(struct sbuf *, const struct stack *);
 int		 stack_sbuf_print_flags(struct sbuf *, const struct stack *,
-		 int);
+		     int, enum stack_sbuf_fmt);
 #ifdef KTR
 void		 stack_ktr(u_int, const char *, int, const struct stack *,
 		    u_int);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202007061633.066GXTY9087195>