Skip to content

Commit 58788d2

Browse files
committed
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
1 parent 5d8ca75 commit 58788d2

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

sys/kern/subr_stack.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ stack_print_short_ddb(const struct stack *st)
170170
* flags - M_WAITOK or M_NOWAIT (EWOULDBLOCK).
171171
*/
172172
int
173-
stack_sbuf_print_flags(struct sbuf *sb, const struct stack *st, int flags)
173+
stack_sbuf_print_flags(struct sbuf *sb, const struct stack *st, int flags,
174+
enum stack_sbuf_fmt format)
174175
{
175176
char namebuf[64];
176177
long offset;
@@ -182,17 +183,27 @@ stack_sbuf_print_flags(struct sbuf *sb, const struct stack *st, int flags)
182183
&offset, flags);
183184
if (error == EWOULDBLOCK)
184185
return (error);
185-
sbuf_printf(sb, "#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
186-
namebuf, offset);
186+
switch (format) {
187+
case STACK_SBUF_FMT_LONG:
188+
sbuf_printf(sb, "#%d %p at %s+%#lx\n", i,
189+
(void *)st->pcs[i], namebuf, offset);
190+
break;
191+
case STACK_SBUF_FMT_COMPACT:
192+
sbuf_printf(sb, "%s+%#lx ", namebuf, offset);
193+
break;
194+
default:
195+
__assert_unreachable();
196+
}
187197
}
198+
sbuf_nl_terminate(sb);
188199
return (0);
189200
}
190201

191202
void
192203
stack_sbuf_print(struct sbuf *sb, const struct stack *st)
193204
{
194205

195-
(void)stack_sbuf_print_flags(sb, st, M_WAITOK);
206+
(void)stack_sbuf_print_flags(sb, st, M_WAITOK, STACK_SBUF_FMT_LONG);
196207
}
197208

198209
#if defined(DDB) || defined(WITNESS)

sys/kern/tty_info.c

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,36 @@ sbuf_tty_drain(void *a, const char *d, int len)
239239
}
240240

241241
#ifdef STACK
242-
static bool tty_info_kstacks = true;
243-
SYSCTL_BOOL(_kern, OID_AUTO, tty_info_kstacks, CTLFLAG_RWTUN,
244-
&tty_info_kstacks, 0,
245-
"Enable printing kernel stack(9) traces on ^T (tty info)");
242+
static int tty_info_kstacks = STACK_SBUF_FMT_LONG;
243+
244+
static int
245+
sysctl_tty_info_kstacks(SYSCTL_HANDLER_ARGS)
246+
{
247+
enum stack_sbuf_fmt val;
248+
int error;
249+
250+
val = tty_info_kstacks;
251+
error = sysctl_handle_int(oidp, &val, 0, req);
252+
if (error != 0 || req->newptr == NULL)
253+
return (error);
254+
255+
switch (val) {
256+
case STACK_SBUF_FMT_NONE:
257+
case STACK_SBUF_FMT_LONG:
258+
case STACK_SBUF_FMT_COMPACT:
259+
tty_info_kstacks = val;
260+
break;
261+
default:
262+
error = EINVAL;
263+
}
264+
265+
return (error);
266+
}
267+
SYSCTL_PROC(_kern, OID_AUTO, tty_info_kstacks,
268+
CTLFLAG_RWTUN | CTLFLAG_MPSAFE | CTLTYPE_INT, NULL, 0,
269+
sysctl_tty_info_kstacks, "I",
270+
"Adjust format of kernel stack(9) traces on ^T (tty info): "
271+
"0 - disabled; 1 - long; 2 - compact");
246272
#endif
247273

248274
/*
@@ -254,7 +280,8 @@ tty_info(struct tty *tp)
254280
struct timeval rtime, utime, stime;
255281
#ifdef STACK
256282
struct stack stack;
257-
int sterr;
283+
int sterr, kstacks_val;
284+
bool print_kstacks;
258285
#endif
259286
struct proc *p, *ppick;
260287
struct thread *td, *tdpick;
@@ -337,7 +364,10 @@ tty_info(struct tty *tp)
337364
state = "unknown";
338365
pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT;
339366
#ifdef STACK
340-
if (tty_info_kstacks) {
367+
kstacks_val = atomic_load_int(&tty_info_kstacks);
368+
print_kstacks = (kstacks_val != STACK_SBUF_FMT_NONE);
369+
370+
if (print_kstacks) {
341371
if (TD_IS_SWAPPED(td))
342372
sterr = ENOENT;
343373
else
@@ -366,8 +396,8 @@ tty_info(struct tty *tp)
366396
pctcpu / 100, rss);
367397

368398
#ifdef STACK
369-
if (tty_info_kstacks && sterr == 0)
370-
stack_sbuf_print_flags(&sb, &stack, M_NOWAIT);
399+
if (print_kstacks && sterr == 0)
400+
stack_sbuf_print_flags(&sb, &stack, M_NOWAIT, kstacks_val);
371401
#endif
372402

373403
out:

sys/sys/stack.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ MALLOC_DECLARE(M_STACK);
3939

4040
struct sbuf;
4141

42+
enum stack_sbuf_fmt {
43+
STACK_SBUF_FMT_NONE = 0,
44+
STACK_SBUF_FMT_LONG = 1,
45+
STACK_SBUF_FMT_COMPACT = 2,
46+
};
47+
4248
/* MI Routines. */
4349
struct stack *stack_create(int);
4450
void stack_destroy(struct stack *);
@@ -52,7 +58,7 @@ void stack_print_short_ddb(const struct stack *);
5258
void stack_sbuf_print(struct sbuf *, const struct stack *);
5359
void stack_sbuf_print_ddb(struct sbuf *, const struct stack *);
5460
int stack_sbuf_print_flags(struct sbuf *, const struct stack *,
55-
int);
61+
int, enum stack_sbuf_fmt);
5662
#ifdef KTR
5763
void stack_ktr(u_int, const char *, int, const struct stack *,
5864
u_int);

0 commit comments

Comments
 (0)