Skip to content

Commit

Permalink
tools: add option --cgroupmap to tcptop
Browse files Browse the repository at this point in the history
  • Loading branch information
alban authored and yonghong-song committed Mar 9, 2020
1 parent 5e123df commit 7d62656
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
12 changes: 10 additions & 2 deletions man/man8/tcptop.8
@@ -1,8 +1,9 @@
.TH tcptop 8 "2016-09-13" "USER COMMANDS"
.TH tcptop 8 "2020-03-08" "USER COMMANDS"
.SH NAME
tcptop \- Summarize TCP send/recv throughput by host. Top for TCP.
.SH SYNOPSIS
.B tcptop [\-h] [\-C] [\-S] [\-p PID] [interval] [count]
.B tcptop [\-h] [\-C] [\-S] [\-p PID] [\-\-cgroupmap MAPPATH]
[interval] [count]
.SH DESCRIPTION
This is top for TCP sessions.

Expand Down Expand Up @@ -35,6 +36,9 @@ Don't print the system summary line (load averages).
\-p PID
Trace this PID only.
.TP
\-\-cgroupmap MAPPATH
Trace cgroups in this BPF map only (filtered in-kernel).
.TP
interval
Interval between updates, seconds (default 1).
.TP
Expand All @@ -53,6 +57,10 @@ Don't clear the screen (rolling output), and 5 second summaries:
Trace PID 181 only, and don't clear the screen:
#
.B tcptop \-Cp 181
.TP
Trace a set of cgroups only (see filtering_by_cgroups.md from bcc sources for more details):
#
.B tcptop \-\-cgroupmap /sys/fs/bpf/test01
.SH FIELDS
.TP
loadavg:
Expand Down
24 changes: 24 additions & 0 deletions tools/tcptop.py
Expand Up @@ -45,6 +45,7 @@ def range_check(string):
./tcptop # trace TCP send/recv by host
./tcptop -C # don't clear the screen
./tcptop -p 181 # only trace PID 181
./tcptop --cgroupmap ./mappath # only trace cgroups in this BPF map
"""
parser = argparse.ArgumentParser(
description="Summarize TCP send/recv throughput by host",
Expand All @@ -60,6 +61,8 @@ def range_check(string):
help="output interval, in seconds (default 1)")
parser.add_argument("count", nargs="?", default=-1, type=range_check,
help="number of outputs")
parser.add_argument("--cgroupmap",
help="trace cgroups in this BPF map only")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
Expand Down Expand Up @@ -94,11 +97,21 @@ def range_check(string):
BPF_HASH(ipv6_send_bytes, struct ipv6_key_t);
BPF_HASH(ipv6_recv_bytes, struct ipv6_key_t);
#if CGROUPSET
BPF_TABLE_PINNED("hash", u64, u64, cgroupset, 1024, "CGROUPPATH");
#endif
int kprobe__tcp_sendmsg(struct pt_regs *ctx, struct sock *sk,
struct msghdr *msg, size_t size)
{
u32 pid = bpf_get_current_pid_tgid() >> 32;
FILTER
#if CGROUPSET
u64 cgroupid = bpf_get_current_cgroup_id();
if (cgroupset.lookup(&cgroupid) == NULL) {
return 0;
}
#endif
u16 dport = 0, family = sk->__sk_common.skc_family;
if (family == AF_INET) {
Expand Down Expand Up @@ -136,6 +149,12 @@ def range_check(string):
{
u32 pid = bpf_get_current_pid_tgid() >> 32;
FILTER
#if CGROUPSET
u64 cgroupid = bpf_get_current_cgroup_id();
if (cgroupset.lookup(&cgroupid) == NULL) {
return 0;
}
#endif
u16 dport = 0, family = sk->__sk_common.skc_family;
u64 *val, zero = 0;
Expand Down Expand Up @@ -174,6 +193,11 @@ def range_check(string):
'if (pid != %s) { return 0; }' % args.pid)
else:
bpf_text = bpf_text.replace('FILTER', '')
if args.cgroupmap:
bpf_text = bpf_text.replace('CGROUPSET', '1')
bpf_text = bpf_text.replace('CGROUPPATH', args.cgroupmap)
else:
bpf_text = bpf_text.replace('CGROUPSET', '0')
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
Expand Down
25 changes: 18 additions & 7 deletions tools/tcptop_example.txt
Expand Up @@ -92,25 +92,36 @@ PID COMM LADDR RADDR RX_KB TX_KB

You can disable the loadavg summary line with -S if needed.

The --cgroupmap option filters based on a cgroup set. It is meant to be used
with an externally created map.

# tcptop --cgroupmap /sys/fs/bpf/test01

For more details, see docs/filtering_by_cgroups.md


USAGE:

# tcptop -h
usage: tcptop.py [-h] [-C] [-S] [-p PID] [interval] [count]
usage: tcptop.py [-h] [-C] [-S] [-p PID] [--cgroupmap CGROUPMAP]
[interval] [count]

Summarize TCP send/recv throughput by host

positional arguments:
interval output interval, in seconds (default 1)
count number of outputs
interval output interval, in seconds (default 1)
count number of outputs

optional arguments:
-h, --help show this help message and exit
-C, --noclear don't clear the screen
-S, --nosummary skip system summary line
-p PID, --pid PID trace this PID only
-h, --help show this help message and exit
-C, --noclear don't clear the screen
-S, --nosummary skip system summary line
-p PID, --pid PID trace this PID only
--cgroupmap CGROUPMAP
trace cgroups in this BPF map only

examples:
./tcptop # trace TCP send/recv by host
./tcptop -C # don't clear the screen
./tcptop -p 181 # only trace PID 181
./tcptop --cgroupmap ./mappath # only trace cgroups in this BPF map

0 comments on commit 7d62656

Please sign in to comment.