Skip to content

Commit

Permalink
bpflist: Add to tests and use Python directory listing
Browse files Browse the repository at this point in the history
This commit adds bpflist to the smoke tests suite (omitted by mistake),
and switches to listing file descriptors using Python's `os` module
instead of relying on `ls` glob expansion, which is fragile when there
are many processes with many fds.
  • Loading branch information
goldshtn committed Feb 14, 2017
1 parent f387f5d commit 563af79
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
3 changes: 3 additions & 0 deletions tests/python/test_tools_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def test_biotop(self):
def test_bitesize(self):
self.run_with_int("biotop.py")

def test_bpflist(self):
self.run_with_duration("bpflist.py")

def test_btrfsdist(self):
# Will attempt to do anything meaningful only when btrfs is installed.
self.run_with_duration("btrfsdist.py 1 1")
Expand Down
26 changes: 16 additions & 10 deletions tools/bpflist.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from bcc import BPF, USDT
import argparse
import re
import os
import subprocess

examples = """examples:
Expand Down Expand Up @@ -54,16 +55,21 @@ def parse_probes(typ):
parse_probes("kprobe")
parse_probes("uprobe")

cmd = "ls -l /proc/*/fd/* | grep bpf"
p = subprocess.Popen(cmd, shell=True,
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
for line in p.stdout:
match = re.search('/proc/(\\d+)/fd/\\d+.*bpf-(\\w+)', line)
if match is None:
continue
pid = int(match.group(1))
t = match.group(2)
counts[(pid, t)] = counts.get((pid, t), 0) + 1
def find_bpf_fds(pid):
root = '/proc/%d/fd' % pid
for fd in os.listdir(root):
try:
link = os.readlink(os.path.join(root, fd))
except OSError:
continue
match = re.match('.*bpf-(\\w+)', link)
if match:
tup = (pid, match.group(1))
counts[tup] = counts.get(tup, 0) + 1

for pdir in os.listdir('/proc'):
if re.match('\\d+', pdir):
find_bpf_fds(int(pdir))

print("%-6s %-16s %-8s %s" % ("PID", "COMM", "TYPE", "COUNT"))
for (pid, typ), count in sorted(counts.items(), key=lambda t: t[0][0]):
Expand Down

0 comments on commit 563af79

Please sign in to comment.