klockstat(8)
๐ klockstat(8) ๐
System Manager's Manual, the vibe is immaculate ๐
๐ธ NAME ๐ธ
klockstat traces kernel mutex lock events and shows you lock stats. It's built on Linux eBPF/bcc. โจ
๐ฆ SYNOPSIS ๐ฆ
klockstat [-h] [-i] [-n] [-s] [-c] [-S FIELDS] [-p] [-t] [-d DURATION]
๐ DESCRIPTION ๐
okay so klockstat is literally a kernel mutex detective ๐ต๏ธโโ๏ธ it traces mutex lock events and then serves you the lock statistics, and the output looks like this:
Caller Avg Spin Count Max spin
Total spin
psi_avgs_work+0x2e 3675 5 5468
18379
flush_to_ldisc+0x22 2833 2 4210
5667
n_tty_write+0x30c 3914 1 3914
3914
isig+0x5d 2390 1 2390
2390
tty_buffer_flush+0x2a 1604 1 1604
1604
commit_echoes+0x22 1400 1 1400
1400
n_tty_receive_buf_common+0x3b9 1399 1 1399
1399
Caller Avg Hold Count Max hold
Total hold
flush_to_ldisc+0x22 42558 2 76135
85116
psi_avgs_work+0x2e 14821 5 20446
74106
n_tty_receive_buf_common+0x3b9 12300 1 12300
12300
n_tty_write+0x30c 10712 1 10712
10712
isig+0x5d 3362 1 3362
3362
tty_buffer_flush+0x2a 3078 1 3078
3078
commit_echoes+0x22 3017 1 3017
3017
Every caller to the kernel's mutex gets its own line, bestie. ๐ซถ
The first chunk of lines is the lock acquiring data, which tells you how long it took to actually grab a given lock. ๐
Callerโ the symbol grabbing the mutex ๐โโ๏ธAvg Spinโ the average time it took to acquire the mutex ๐Countโ how many times the mutex got acquired ๐ขMax spinโ the longest it took to acquire the mutex ๐ญTotal spinโ the grand total of time spent acquiring the mutex ๐งฎ
The second chunk is the lock holding data, which shows how long a lock was actually held. ๐ค
Callerโ the symbol holding the mutex ๐Avg Holdโ the average time the mutex was held โฑ๏ธCountโ how many times the mutex was held ๐Max holdโ the longest the mutex was held ๐ซTotal holdโ the total time spent holding the mutex ๐
Here's the tea โ this works by tracing mutex_lock/unlock kprobes, updating the lock stats in maps, and then processing all of that in the python part. it's giving clever engineering ๐ง
โ ๏ธ bestie, this uses BPF, so only the root user can run this tool. no sudo, no tool. that's the rule ๐ โโ๏ธ
๐งฉ REQUIREMENTS ๐งฉ
You'll need CONFIG_BPF and bcc, periodt. ๐ ๏ธ
โจ OPTIONS โจ
-hโ prints the usage message, in case you forget how to use it ๐-i SECโ prints a summary at this interval, measured in seconds โฐ-c CALLERโ only shows the locks taken by the caller you filter for, likepipe_๐-S FIELDSโ sorts the data by the columns you name in theFIELDSstring. By default it sorts by the Max columns ๐
The FIELDS string can hold 1 or 2 fields that describe which columns to sort on, for both the acquiring and holding data. These are the options you've got:
acq_maxโ for theMax spincolumn ๐acq_totalโ for theTotal spincolumn ๐งพacq_countโ for theCountcolumn ๐ขhld_maxโ for theMax holdcolumn ๐hld_totalโ for theTotal holdcolumn ๐งพhld_countโ for theCountcolumn ๐ข
See EXAMPLES for the vibes ๐
-n COUNTโ shows COUNT number of locks ๐ฏ-s COUNTโ shows COUNT number of stack entries ๐ฅ-p PIDโ traces only this process ID, filtered in-kernel ๐ง-t TIDโ traces only this thread ID, filtered in-kernel ๐งต-d DURATIONโ the total duration of the trace in seconds โณ--stack-storage-size STACK_STORAGE_SIZEโ changes how many unique stack traces can be stored and displayed. Go bigger if you need more receipts ๐ฆ
๐ EXAMPLES ๐
Sort lock acquired results on acquired count:
# klockstat -S acq_count
Sort lock held results on total held time:
# klockstat -S hld_total
Combo of the above:
# klockstat -S acq_count,hld_total
Trace the whole system:
# klockstat
Trace for just 5 seconds:
# klockstat -d 5
Display stats every 5 seconds:
# klockstat -i 5
Trace locks for PID 123:
# klockstat -p 123
Trace locks for TID 321:
# klockstat -t 321
Display stats only for lock callers with the pipe_ substring:
# klockstat -c pipe_
Display 3 locks:
# klockstat -n 3
Display 10 levels of stack for the most expensive lock:
# klockstat -n 1 -s 10
Here's what that looks like in action ๐ฌ
Tracing lock events... Hit Ctrl-C to end. ^C
Caller Avg Spin Count Max
spin Total spin
pipe_wait+0xa9 670 397691
17273 266775437
pipe_wait+0xa9
pipe_read+0x206
new_sync_read+0x12a
vfs_read+0x9d
ksys_read+0x5f
do_syscall_64+0x5b
entry_SYSCALL_64_after_hwframe+0x44
Caller Avg Hold Count Max
hold Total hold
flush_to_ldisc+0x22 28381 3
65484 85144
flush_to_ldisc+0x22
process_one_work+0x1b0
worker_thread+0x50
kthread+0xfb
ret_from_fork+0x35
๐ SOURCE ๐
This comes from bcc ๐ฆ you can find it at https://github.com/iovisor/bcc
Also, check the bcc distribution for a companion _examples.txt file. It has example usage, output, and commentary for this tool, and it's a whole mood ๐
๐ฅ๏ธ OS ๐ฅ๏ธ
Linux ๐ง
๐งช STABILITY ๐งช
Unstable, it's still in development ๐ง
๐ซถ CREDITS ๐ซถ
This tool is based on the work of David Valin <dvalin@redhat.com> and his script. Thank you, queen ๐
๐ AUTHOR ๐
Jiri Olsa ๐ง
USER COMMANDS โจ 2019-10-22 โจ klockstat(8) ๐๐ ttyl bestie!
๐ฆ where this page comes from
- centos-stream 10: bcc-tools 0.36.1-1.el10 ยท Apache-2.0
- centos-stream 9: bcc-tools 0.36.1-1.el9 ยท ASL 2.0
- fedora 43: bcc-tools 0.35.0-2.fc43 ยท Apache-2.0
- fedora 44: bcc-tools 0.35.0-5.fc44 ยท Apache-2.0
- ๐ girlified by anthropic/claude-haiku-5.5 on : a language model's rewrite of the original, under its terms