Skip to content

Commit

Permalink
tools: killsnoop: auto detect the length of PID/TPID column
Browse files Browse the repository at this point in the history
There are more than 128CPUs on a modern server, also use a large PID
number, the fixed length 6 may be not enough:
 # ./killsnoop.py -T 1173199
 TIME      PID    COMM             SIG  TPID   RESULT
 15:31:51  3544588 bash             10   1173199 0

Auto detect the max bytes from '/proc/sys/kernel/pid_max' instead.

Signed-off-by: zhenwei pi <[email protected]>
  • Loading branch information
pizhenwei authored and yonghong-song committed Oct 8, 2022
1 parent d5b6d24 commit 8c6f846
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions tools/killsnoop.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,18 @@
b.attach_kprobe(event=kill_fnname, fn_name="syscall__kill")
b.attach_kretprobe(event=kill_fnname, fn_name="do_ret_sys_kill")

# detect the length of PID column
pid_bytes = 6
try:
with open("/proc/sys/kernel/pid_max", 'r') as f:
pid_bytes = len(f.read().strip()) + 1
f.close()
except:
pass # not fatal error, just use the default value

# header
print("%-9s %-6s %-16s %-4s %-6s %s" % (
"TIME", "PID", "COMM", "SIG", "TPID", "RESULT"))
print("%-9s %-*s %-16s %-4s %-*s %s" % (
"TIME", pid_bytes, "PID", "COMM", "SIG", pid_bytes, "TPID", "RESULT"))

# process event
def print_event(cpu, data, size):
Expand All @@ -153,8 +162,8 @@ def print_event(cpu, data, size):
if (args.failed and (event.ret >= 0)):
return

printb(b"%-9s %-6d %-16s %-4d %-6d %d" % (strftime("%H:%M:%S").encode('ascii'),
event.pid, event.comm, event.sig, event.tpid, event.ret))
printb(b"%-9s %-*d %-16s %-4d %-*d %d" % (strftime("%H:%M:%S").encode('ascii'),
pid_bytes, event.pid, event.comm, event.sig, pid_bytes, event.tpid, event.ret))

# loop with callback to print_event
b["events"].open_perf_buffer(print_event)
Expand Down

0 comments on commit 8c6f846

Please sign in to comment.