Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # tcptop Summarize TCP send/recv throughput by host. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
| 6 | # |
Hariharan Ananthakrishnan | 04893e3 | 2021-08-12 05:55:21 -0700 | [diff] [blame] | 7 | # USAGE: tcptop [-h] [-C] [-S] [-p PID] [interval [count]] [-4 | -6] |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 8 | # |
| 9 | # This uses dynamic tracing of kernel functions, and will need to be updated |
| 10 | # to match kernel changes. |
| 11 | # |
| 12 | # WARNING: This traces all send/receives at the TCP level, and while it |
| 13 | # summarizes data in-kernel to reduce overhead, there may still be some |
| 14 | # overhead at high TCP send/receive rates (eg, ~13% of one CPU at 100k TCP |
| 15 | # events/sec. This is not the same as packet rate: funccount can be used to |
| 16 | # count the kprobes below to find out the TCP rate). Test in a lab environment |
| 17 | # first. If your send/receive rate is low (eg, <1k/sec) then the overhead is |
| 18 | # expected to be negligible. |
| 19 | # |
| 20 | # ToDo: Fit output to screen size (top X only) in default (not -C) mode. |
| 21 | # |
| 22 | # Copyright 2016 Netflix, Inc. |
| 23 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 24 | # |
| 25 | # 02-Sep-2016 Brendan Gregg Created this. |
| 26 | |
| 27 | from __future__ import print_function |
| 28 | from bcc import BPF |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 29 | from bcc.containers import filter_by_containers |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 30 | import argparse |
| 31 | from socket import inet_ntop, AF_INET, AF_INET6 |
| 32 | from struct import pack |
| 33 | from time import sleep, strftime |
| 34 | from subprocess import call |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 35 | from collections import namedtuple, defaultdict |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 36 | |
| 37 | # arguments |
Benjamin Poirier | 8e86b9e | 2017-07-27 16:07:06 -0700 | [diff] [blame] | 38 | def range_check(string): |
| 39 | value = int(string) |
| 40 | if value < 1: |
| 41 | msg = "value must be stricly positive, got %d" % (value,) |
| 42 | raise argparse.ArgumentTypeError(msg) |
| 43 | return value |
| 44 | |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 45 | examples = """examples: |
| 46 | ./tcptop # trace TCP send/recv by host |
| 47 | ./tcptop -C # don't clear the screen |
| 48 | ./tcptop -p 181 # only trace PID 181 |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 49 | ./tcptop --cgroupmap mappath # only trace cgroups in this BPF map |
| 50 | ./tcptop --mntnsmap mappath # only trace mount namespaces in the map |
Hariharan Ananthakrishnan | 04893e3 | 2021-08-12 05:55:21 -0700 | [diff] [blame] | 51 | ./tcptop -4 # trace IPv4 family only |
| 52 | ./tcptop -6 # trace IPv6 family only |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 53 | """ |
| 54 | parser = argparse.ArgumentParser( |
| 55 | description="Summarize TCP send/recv throughput by host", |
| 56 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 57 | epilog=examples) |
| 58 | parser.add_argument("-C", "--noclear", action="store_true", |
| 59 | help="don't clear the screen") |
| 60 | parser.add_argument("-S", "--nosummary", action="store_true", |
| 61 | help="skip system summary line") |
| 62 | parser.add_argument("-p", "--pid", |
| 63 | help="trace this PID only") |
Benjamin Poirier | 8e86b9e | 2017-07-27 16:07:06 -0700 | [diff] [blame] | 64 | parser.add_argument("interval", nargs="?", default=1, type=range_check, |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 65 | help="output interval, in seconds (default 1)") |
Benjamin Poirier | 8e86b9e | 2017-07-27 16:07:06 -0700 | [diff] [blame] | 66 | parser.add_argument("count", nargs="?", default=-1, type=range_check, |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 67 | help="number of outputs") |
Alban Crequy | 7d62656 | 2020-03-08 16:41:34 +0100 | [diff] [blame] | 68 | parser.add_argument("--cgroupmap", |
| 69 | help="trace cgroups in this BPF map only") |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 70 | parser.add_argument("--mntnsmap", |
| 71 | help="trace mount namespaces in this BPF map only") |
Hariharan Ananthakrishnan | 04893e3 | 2021-08-12 05:55:21 -0700 | [diff] [blame] | 72 | group = parser.add_mutually_exclusive_group() |
| 73 | group.add_argument("-4", "--ipv4", action="store_true", |
| 74 | help="trace IPv4 family only") |
| 75 | group.add_argument("-6", "--ipv6", action="store_true", |
| 76 | help="trace IPv6 family only") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 77 | parser.add_argument("--ebpf", action="store_true", |
| 78 | help=argparse.SUPPRESS) |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 79 | args = parser.parse_args() |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 80 | debug = 0 |
| 81 | |
| 82 | # linux stats |
| 83 | loadavg = "/proc/loadavg" |
| 84 | |
| 85 | # define BPF program |
| 86 | bpf_text = """ |
| 87 | #include <uapi/linux/ptrace.h> |
| 88 | #include <net/sock.h> |
| 89 | #include <bcc/proto.h> |
| 90 | |
| 91 | struct ipv4_key_t { |
| 92 | u32 pid; |
eiffel-fl | aa6437e | 2021-12-18 19:21:13 +0100 | [diff] [blame] | 93 | char name[TASK_COMM_LEN]; |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 94 | u32 saddr; |
| 95 | u32 daddr; |
| 96 | u16 lport; |
| 97 | u16 dport; |
| 98 | }; |
| 99 | BPF_HASH(ipv4_send_bytes, struct ipv4_key_t); |
| 100 | BPF_HASH(ipv4_recv_bytes, struct ipv4_key_t); |
| 101 | |
| 102 | struct ipv6_key_t { |
Marko Myllynen | bfbf17e | 2018-09-11 21:49:58 +0300 | [diff] [blame] | 103 | unsigned __int128 saddr; |
| 104 | unsigned __int128 daddr; |
Jerome Marchand | 6b8a896 | 2020-03-19 14:40:15 +0100 | [diff] [blame] | 105 | u32 pid; |
eiffel-fl | aa6437e | 2021-12-18 19:21:13 +0100 | [diff] [blame] | 106 | char name[TASK_COMM_LEN]; |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 107 | u16 lport; |
| 108 | u16 dport; |
Jerome Marchand | 6b8a896 | 2020-03-19 14:40:15 +0100 | [diff] [blame] | 109 | u64 __pad__; |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 110 | }; |
| 111 | BPF_HASH(ipv6_send_bytes, struct ipv6_key_t); |
| 112 | BPF_HASH(ipv6_recv_bytes, struct ipv6_key_t); |
| 113 | |
| 114 | int kprobe__tcp_sendmsg(struct pt_regs *ctx, struct sock *sk, |
| 115 | struct msghdr *msg, size_t size) |
| 116 | { |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 117 | if (container_should_be_filtered()) { |
Alban Crequy | 7d62656 | 2020-03-08 16:41:34 +0100 | [diff] [blame] | 118 | return 0; |
| 119 | } |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 120 | |
| 121 | u32 pid = bpf_get_current_pid_tgid() >> 32; |
| 122 | FILTER_PID |
| 123 | |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 124 | u16 dport = 0, family = sk->__sk_common.skc_family; |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 125 | |
Hariharan Ananthakrishnan | 04893e3 | 2021-08-12 05:55:21 -0700 | [diff] [blame] | 126 | FILTER_FAMILY |
| 127 | |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 128 | if (family == AF_INET) { |
| 129 | struct ipv4_key_t ipv4_key = {.pid = pid}; |
eiffel-fl | aa6437e | 2021-12-18 19:21:13 +0100 | [diff] [blame] | 130 | bpf_get_current_comm(&ipv4_key.name, sizeof(ipv4_key.name)); |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 131 | ipv4_key.saddr = sk->__sk_common.skc_rcv_saddr; |
| 132 | ipv4_key.daddr = sk->__sk_common.skc_daddr; |
| 133 | ipv4_key.lport = sk->__sk_common.skc_num; |
| 134 | dport = sk->__sk_common.skc_dport; |
| 135 | ipv4_key.dport = ntohs(dport); |
Javier Honduvilla Coto | 64bf965 | 2018-08-01 06:50:19 +0200 | [diff] [blame] | 136 | ipv4_send_bytes.increment(ipv4_key, size); |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 137 | |
| 138 | } else if (family == AF_INET6) { |
| 139 | struct ipv6_key_t ipv6_key = {.pid = pid}; |
eiffel-fl | aa6437e | 2021-12-18 19:21:13 +0100 | [diff] [blame] | 140 | bpf_get_current_comm(&ipv6_key.name, sizeof(ipv6_key.name)); |
Sumanth Korikkar | 7f6066d | 2020-05-20 10:49:56 -0500 | [diff] [blame] | 141 | bpf_probe_read_kernel(&ipv6_key.saddr, sizeof(ipv6_key.saddr), |
Yonghong Song | a4834a6 | 2020-02-06 09:44:26 -0800 | [diff] [blame] | 142 | &sk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32); |
Sumanth Korikkar | 7f6066d | 2020-05-20 10:49:56 -0500 | [diff] [blame] | 143 | bpf_probe_read_kernel(&ipv6_key.daddr, sizeof(ipv6_key.daddr), |
Yonghong Song | a4834a6 | 2020-02-06 09:44:26 -0800 | [diff] [blame] | 144 | &sk->__sk_common.skc_v6_daddr.in6_u.u6_addr32); |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 145 | ipv6_key.lport = sk->__sk_common.skc_num; |
| 146 | dport = sk->__sk_common.skc_dport; |
| 147 | ipv6_key.dport = ntohs(dport); |
Javier Honduvilla Coto | 64bf965 | 2018-08-01 06:50:19 +0200 | [diff] [blame] | 148 | ipv6_send_bytes.increment(ipv6_key, size); |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 149 | } |
| 150 | // else drop |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * tcp_recvmsg() would be obvious to trace, but is less suitable because: |
| 157 | * - we'd need to trace both entry and return, to have both sock and size |
| 158 | * - misses tcp_read_sock() traffic |
| 159 | * we'd much prefer tracepoints once they are available. |
| 160 | */ |
| 161 | int kprobe__tcp_cleanup_rbuf(struct pt_regs *ctx, struct sock *sk, int copied) |
| 162 | { |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 163 | if (container_should_be_filtered()) { |
Alban Crequy | 7d62656 | 2020-03-08 16:41:34 +0100 | [diff] [blame] | 164 | return 0; |
| 165 | } |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 166 | |
| 167 | u32 pid = bpf_get_current_pid_tgid() >> 32; |
| 168 | FILTER_PID |
| 169 | |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 170 | u16 dport = 0, family = sk->__sk_common.skc_family; |
| 171 | u64 *val, zero = 0; |
| 172 | |
Benjamin Poirier | 81ad054 | 2017-07-28 13:25:14 -0700 | [diff] [blame] | 173 | if (copied <= 0) |
Paul Chaignon | 6d9b1b2 | 2017-10-07 11:06:41 +0200 | [diff] [blame] | 174 | return 0; |
Benjamin Poirier | 81ad054 | 2017-07-28 13:25:14 -0700 | [diff] [blame] | 175 | |
Hariharan Ananthakrishnan | 04893e3 | 2021-08-12 05:55:21 -0700 | [diff] [blame] | 176 | FILTER_FAMILY |
| 177 | |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 178 | if (family == AF_INET) { |
| 179 | struct ipv4_key_t ipv4_key = {.pid = pid}; |
eiffel-fl | aa6437e | 2021-12-18 19:21:13 +0100 | [diff] [blame] | 180 | bpf_get_current_comm(&ipv4_key.name, sizeof(ipv4_key.name)); |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 181 | ipv4_key.saddr = sk->__sk_common.skc_rcv_saddr; |
| 182 | ipv4_key.daddr = sk->__sk_common.skc_daddr; |
| 183 | ipv4_key.lport = sk->__sk_common.skc_num; |
| 184 | dport = sk->__sk_common.skc_dport; |
| 185 | ipv4_key.dport = ntohs(dport); |
Javier Honduvilla Coto | 64bf965 | 2018-08-01 06:50:19 +0200 | [diff] [blame] | 186 | ipv4_recv_bytes.increment(ipv4_key, copied); |
| 187 | |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 188 | } else if (family == AF_INET6) { |
| 189 | struct ipv6_key_t ipv6_key = {.pid = pid}; |
eiffel-fl | aa6437e | 2021-12-18 19:21:13 +0100 | [diff] [blame] | 190 | bpf_get_current_comm(&ipv6_key.name, sizeof(ipv6_key.name)); |
Sumanth Korikkar | 7f6066d | 2020-05-20 10:49:56 -0500 | [diff] [blame] | 191 | bpf_probe_read_kernel(&ipv6_key.saddr, sizeof(ipv6_key.saddr), |
Yonghong Song | a4834a6 | 2020-02-06 09:44:26 -0800 | [diff] [blame] | 192 | &sk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32); |
Sumanth Korikkar | 7f6066d | 2020-05-20 10:49:56 -0500 | [diff] [blame] | 193 | bpf_probe_read_kernel(&ipv6_key.daddr, sizeof(ipv6_key.daddr), |
Yonghong Song | a4834a6 | 2020-02-06 09:44:26 -0800 | [diff] [blame] | 194 | &sk->__sk_common.skc_v6_daddr.in6_u.u6_addr32); |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 195 | ipv6_key.lport = sk->__sk_common.skc_num; |
| 196 | dport = sk->__sk_common.skc_dport; |
| 197 | ipv6_key.dport = ntohs(dport); |
Javier Honduvilla Coto | 64bf965 | 2018-08-01 06:50:19 +0200 | [diff] [blame] | 198 | ipv6_recv_bytes.increment(ipv6_key, copied); |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 199 | } |
| 200 | // else drop |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | """ |
| 205 | |
| 206 | # code substitutions |
| 207 | if args.pid: |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 208 | bpf_text = bpf_text.replace('FILTER_PID', |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 209 | 'if (pid != %s) { return 0; }' % args.pid) |
| 210 | else: |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 211 | bpf_text = bpf_text.replace('FILTER_PID', '') |
Hariharan Ananthakrishnan | 04893e3 | 2021-08-12 05:55:21 -0700 | [diff] [blame] | 212 | if args.ipv4: |
| 213 | bpf_text = bpf_text.replace('FILTER_FAMILY', |
| 214 | 'if (family != AF_INET) { return 0; }') |
| 215 | elif args.ipv6: |
| 216 | bpf_text = bpf_text.replace('FILTER_FAMILY', |
| 217 | 'if (family != AF_INET6) { return 0; }') |
| 218 | bpf_text = bpf_text.replace('FILTER_FAMILY', '') |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 219 | bpf_text = filter_by_containers(args) + bpf_text |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 220 | if debug or args.ebpf: |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 221 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 222 | if args.ebpf: |
| 223 | exit() |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 224 | |
eiffel-fl | aa6437e | 2021-12-18 19:21:13 +0100 | [diff] [blame] | 225 | TCPSessionKey = namedtuple('TCPSession', ['pid', 'name', 'laddr', 'lport', 'daddr', 'dport']) |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 226 | |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 227 | def get_ipv4_session_key(k): |
| 228 | return TCPSessionKey(pid=k.pid, |
eiffel-fl | aa6437e | 2021-12-18 19:21:13 +0100 | [diff] [blame] | 229 | name=k.name, |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 230 | laddr=inet_ntop(AF_INET, pack("I", k.saddr)), |
| 231 | lport=k.lport, |
| 232 | daddr=inet_ntop(AF_INET, pack("I", k.daddr)), |
| 233 | dport=k.dport) |
| 234 | |
| 235 | def get_ipv6_session_key(k): |
| 236 | return TCPSessionKey(pid=k.pid, |
eiffel-fl | aa6437e | 2021-12-18 19:21:13 +0100 | [diff] [blame] | 237 | name=k.name, |
Marko Myllynen | bfbf17e | 2018-09-11 21:49:58 +0300 | [diff] [blame] | 238 | laddr=inet_ntop(AF_INET6, k.saddr), |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 239 | lport=k.lport, |
Marko Myllynen | bfbf17e | 2018-09-11 21:49:58 +0300 | [diff] [blame] | 240 | daddr=inet_ntop(AF_INET6, k.daddr), |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 241 | dport=k.dport) |
| 242 | |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 243 | # initialize BPF |
| 244 | b = BPF(text=bpf_text) |
| 245 | |
| 246 | ipv4_send_bytes = b["ipv4_send_bytes"] |
| 247 | ipv4_recv_bytes = b["ipv4_recv_bytes"] |
| 248 | ipv6_send_bytes = b["ipv6_send_bytes"] |
| 249 | ipv6_recv_bytes = b["ipv6_recv_bytes"] |
| 250 | |
| 251 | print('Tracing... Output every %s secs. Hit Ctrl-C to end' % args.interval) |
| 252 | |
| 253 | # output |
Benjamin Poirier | 8e86b9e | 2017-07-27 16:07:06 -0700 | [diff] [blame] | 254 | i = 0 |
| 255 | exiting = False |
| 256 | while i != args.count and not exiting: |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 257 | try: |
Benjamin Poirier | 8e86b9e | 2017-07-27 16:07:06 -0700 | [diff] [blame] | 258 | sleep(args.interval) |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 259 | except KeyboardInterrupt: |
Benjamin Poirier | 8e86b9e | 2017-07-27 16:07:06 -0700 | [diff] [blame] | 260 | exiting = True |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 261 | |
| 262 | # header |
| 263 | if args.noclear: |
| 264 | print() |
| 265 | else: |
| 266 | call("clear") |
| 267 | if not args.nosummary: |
| 268 | with open(loadavg) as stats: |
| 269 | print("%-8s loadavg: %s" % (strftime("%H:%M:%S"), stats.read())) |
| 270 | |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 271 | # IPv4: build dict of all seen keys |
| 272 | ipv4_throughput = defaultdict(lambda: [0, 0]) |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 273 | for k, v in ipv4_send_bytes.items(): |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 274 | key = get_ipv4_session_key(k) |
| 275 | ipv4_throughput[key][0] = v.value |
| 276 | ipv4_send_bytes.clear() |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 277 | |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 278 | for k, v in ipv4_recv_bytes.items(): |
| 279 | key = get_ipv4_session_key(k) |
| 280 | ipv4_throughput[key][1] = v.value |
| 281 | ipv4_recv_bytes.clear() |
| 282 | |
| 283 | if ipv4_throughput: |
xingfeng2510 | 03e4948 | 2022-03-17 13:07:16 +0800 | [diff] [blame] | 284 | print("%-7s %-12s %-21s %-21s %6s %6s" % ("PID", "COMM", |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 285 | "LADDR", "RADDR", "RX_KB", "TX_KB")) |
| 286 | |
| 287 | # output |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 288 | for k, (send_bytes, recv_bytes) in sorted(ipv4_throughput.items(), |
| 289 | key=lambda kv: sum(kv[1]), |
| 290 | reverse=True): |
xingfeng2510 | 03e4948 | 2022-03-17 13:07:16 +0800 | [diff] [blame] | 291 | print("%-7d %-12.12s %-21s %-21s %6d %6d" % (k.pid, |
eiffel-fl | aa6437e | 2021-12-18 19:21:13 +0100 | [diff] [blame] | 292 | k.name, |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 293 | k.laddr + ":" + str(k.lport), |
| 294 | k.daddr + ":" + str(k.dport), |
| 295 | int(recv_bytes / 1024), int(send_bytes / 1024))) |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 296 | |
| 297 | # IPv6: build dict of all seen keys |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 298 | ipv6_throughput = defaultdict(lambda: [0, 0]) |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 299 | for k, v in ipv6_send_bytes.items(): |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 300 | key = get_ipv6_session_key(k) |
| 301 | ipv6_throughput[key][0] = v.value |
| 302 | ipv6_send_bytes.clear() |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 303 | |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 304 | for k, v in ipv6_recv_bytes.items(): |
| 305 | key = get_ipv6_session_key(k) |
| 306 | ipv6_throughput[key][1] = v.value |
| 307 | ipv6_recv_bytes.clear() |
| 308 | |
| 309 | if ipv6_throughput: |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 310 | # more than 80 chars, sadly. |
xingfeng2510 | 03e4948 | 2022-03-17 13:07:16 +0800 | [diff] [blame] | 311 | print("\n%-7s %-12s %-32s %-32s %6s %6s" % ("PID", "COMM", |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 312 | "LADDR6", "RADDR6", "RX_KB", "TX_KB")) |
| 313 | |
| 314 | # output |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 315 | for k, (send_bytes, recv_bytes) in sorted(ipv6_throughput.items(), |
| 316 | key=lambda kv: sum(kv[1]), |
| 317 | reverse=True): |
xingfeng2510 | 03e4948 | 2022-03-17 13:07:16 +0800 | [diff] [blame] | 318 | print("%-7d %-12.12s %-32s %-32s %6d %6d" % (k.pid, |
eiffel-fl | aa6437e | 2021-12-18 19:21:13 +0100 | [diff] [blame] | 319 | k.name, |
Andreas Gerstmayr | c64f487 | 2018-07-06 14:59:07 +0200 | [diff] [blame] | 320 | k.laddr + ":" + str(k.lport), |
| 321 | k.daddr + ":" + str(k.dport), |
| 322 | int(recv_bytes / 1024), int(send_bytes / 1024))) |
Brendan Gregg | 60393ea | 2016-10-04 15:18:11 -0700 | [diff] [blame] | 323 | |
Benjamin Poirier | 8e86b9e | 2017-07-27 16:07:06 -0700 | [diff] [blame] | 324 | i += 1 |