Skip to content

Commit

Permalink
bpflist: use smarter print format
Browse files Browse the repository at this point in the history
BPF object type can be wider than 8 characters, e.g., for the "raw-tracepoint"
type.  Compute the printing format automatically based on the maximum length of
BPF objects to be listed.

Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
  • Loading branch information
aspsk authored and yonghong-song committed Jan 16, 2020
1 parent 304d95c commit 90937ce
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions tools/bpflist.py
Expand Up @@ -76,7 +76,12 @@ def find_bpf_fds(pid):
find_bpf_fds(int(pdir))
except OSError:
continue
print("%-6s %-16s %-8s %s" % ("PID", "COMM", "TYPE", "COUNT"))
for (pid, typ), count in sorted(counts.items(), key=lambda t: t[0][0]):

items = counts.items()
max_type_len = items and max(list(map(lambda t: len(t[0][1]), items))) or 0
print_format = "%%-6s %%-16s %%-%ss %%s" % (max_type_len + 1)

print(print_format % ("PID", "COMM", "TYPE", "COUNT"))
for (pid, typ), count in sorted(items, key=lambda t: t[0][0]):
comm = comm_for_pid(pid)
print("%-6d %-16s %-8s %-4d" % (pid, comm, typ, count))
print(print_format % (pid, comm, typ, count))

0 comments on commit 90937ce

Please sign in to comment.