Skip to content

Commit

Permalink
Adding a couple of optional parameters to tcpconnlat.
Browse files Browse the repository at this point in the history
1. Adding `-m` and `-u` parameters for filtering tcp connects with greater the specified minimum latency.
2. Adding -v option to print out the bpf program. This is helpful for debugging and is a bit more consistent with some of the other bpf tools.

The latency check is only added if `-u` or `-m` have been set to greater than 0.
  • Loading branch information
dpayne committed Jan 17, 2018
1 parent 7c91cf6 commit 3aed4fb
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion tools/tcpconnlat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
# arguments
examples = """examples:
./tcpconnlat # trace all TCP connect()s
./tcpconnlat -m 1 # only show results slower than 1 ms
./tcpconnlat -u 100 # only show results slower than 100 microseconds
./tcpconnlat -t # include timestamps
./tcpconnlat -p 181 # only trace PID 181
"""
Expand All @@ -35,7 +37,21 @@
help="include timestamp on output")
parser.add_argument("-p", "--pid",
help="trace this PID only")
parser.add_argument("-m", "--min-ms", type=float, dest="min_ms",
help="minimum latency filter (ms)")
parser.add_argument("-u", "--min-us", type=float, dest="min_us",
help="minimum latency filter (us)")
parser.add_argument("-v", "--verbose", action="store_true",
help="print the BPF program for debugging purposes")
args = parser.parse_args()

if args.min_ms:
duration_us = int(args.min_ms * 1000)
elif args.min_us:
duration_us = int(args.min_us)
else:
duration_us = 0 # default is show all

debug = 0

# define BPF program
Expand Down Expand Up @@ -104,9 +120,18 @@
if (infop == 0) {
return 0; // missed entry or filtered
}
u64 ts = infop->ts;
u64 now = bpf_ktime_get_ns();
u64 delta_us = (now - ts) / 1000ul;
#ifdef MIN_LATENCY
if ( delta_us < DURATION_US ) {
return 0; // connect latency is below latency filter minimum
}
#endif
// pull in details
u16 family = 0, dport = 0;
family = skp->__sk_common.skc_family;
Expand Down Expand Up @@ -142,13 +167,17 @@
}
"""

if duration_us > 0:
bpf_text = "#define MIN_LATENCY\n" + bpf_text
bpf_text = bpf_text.replace('DURATION_US', str(duration_us))

# code substitutions
if args.pid:
bpf_text = bpf_text.replace('FILTER',
'if (pid != %s) { return 0; }' % args.pid)
else:
bpf_text = bpf_text.replace('FILTER', '')
if debug:
if debug or args.verbose:
print(bpf_text)

# initialize BPF
Expand Down

0 comments on commit 3aed4fb

Please sign in to comment.