Brendan Gregg | 12c234f | 2020-09-16 14:34:29 -0700 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # tcpsynbl Show TCP SYN backlog. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
| 6 | # |
| 7 | # Copyright (c) 2019 Brendan Gregg. |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"). |
| 9 | # This was originally created for the BPF Performance Tools book |
| 10 | # published by Addison Wesley. ISBN-13: 9780136554820 |
| 11 | # When copying or porting, include this comment. |
| 12 | # |
| 13 | # 03-Jul-2019 Brendan Gregg Ported from bpftrace to BCC. |
| 14 | |
| 15 | from __future__ import print_function |
| 16 | from bcc import BPF |
| 17 | from time import sleep |
| 18 | |
| 19 | # load BPF program |
| 20 | b = BPF(text=""" |
| 21 | #include <net/sock.h> |
| 22 | |
| 23 | typedef struct backlog_key { |
| 24 | u32 backlog; |
| 25 | u64 slot; |
| 26 | } backlog_key_t; |
| 27 | |
| 28 | BPF_HISTOGRAM(dist, backlog_key_t); |
| 29 | |
| 30 | int do_entry(struct pt_regs *ctx) { |
| 31 | struct sock *sk = (struct sock *)PT_REGS_PARM1(ctx); |
| 32 | |
| 33 | backlog_key_t key = {}; |
| 34 | key.backlog = sk->sk_max_ack_backlog; |
| 35 | key.slot = bpf_log2l(sk->sk_ack_backlog); |
| 36 | dist.increment(key); |
| 37 | |
| 38 | return 0; |
| 39 | }; |
| 40 | """) |
| 41 | b.attach_kprobe(event="tcp_v4_syn_recv_sock", fn_name="do_entry") |
| 42 | b.attach_kprobe(event="tcp_v6_syn_recv_sock", fn_name="do_entry") |
| 43 | |
| 44 | print("Tracing SYN backlog size. Ctrl-C to end."); |
| 45 | |
| 46 | try: |
| 47 | sleep(99999999) |
| 48 | except KeyboardInterrupt: |
| 49 | print() |
| 50 | |
| 51 | dist = b.get_table("dist") |
| 52 | dist.print_log2_hist("backlog", "backlog_max") |