From: chendotjs Date: Tue, 30 Nov 2021 03:56:03 +0000 (+0000) Subject: libbpf-tools: add option to include 'LPORT' in tcpconnlat X-Git-Tag: v0.24.0~56^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=64a30f1b9ebd055ec0f1f0a6179d10936c06c0b3;p=platform%2Fupstream%2Fbcc.git libbpf-tools: add option to include 'LPORT' in tcpconnlat Signed-off-by: chendotjs --- diff --git a/libbpf-tools/tcpconnlat.bpf.c b/libbpf-tools/tcpconnlat.bpf.c index 7e0940d4..56d37414 100644 --- a/libbpf-tools/tcpconnlat.bpf.c +++ b/libbpf-tools/tcpconnlat.bpf.c @@ -36,7 +36,7 @@ static __always_inline int trace_connect(struct sock *sk) u32 tgid = bpf_get_current_pid_tgid() >> 32; struct piddata piddata = {}; - if (targ_tgid && targ_tgid != tgid) + if (targ_tgid && targ_tgid != tgid) return 0; bpf_get_current_comm(&piddata.comm, sizeof(piddata.comm)); @@ -85,6 +85,7 @@ int BPF_PROG(tcp_rcv_state_process, struct sock *sk) sizeof(event.comm)); event.ts_us = ts / 1000; event.tgid = piddatap->tgid; + event.lport = sk->__sk_common.skc_num; event.dport = sk->__sk_common.skc_dport; event.af = sk->__sk_common.skc_family; if (event.af == AF_INET) { diff --git a/libbpf-tools/tcpconnlat.c b/libbpf-tools/tcpconnlat.c index 3e06447f..870fa436 100644 --- a/libbpf-tools/tcpconnlat.c +++ b/libbpf-tools/tcpconnlat.c @@ -24,6 +24,7 @@ static struct env { __u64 min_us; pid_t pid; bool timestamp; + bool lport; bool verbose; } env; @@ -33,18 +34,20 @@ const char *argp_program_bug_address = const char argp_program_doc[] = "\nTrace TCP connects and show connection latency.\n" "\n" -"USAGE: tcpconnlat [--help] [-t] [-p PID]\n" +"USAGE: tcpconnlat [--help] [-t] [-p PID] [-L]\n" "\n" "EXAMPLES:\n" -" tcpconnlat # summarize on-CPU time as a histogram" -" tcpconnlat 1 # trace connection latency slower than 1 ms" -" tcpconnlat 0.1 # trace connection latency slower than 100 us" -" tcpconnlat -t # 1s summaries, milliseconds, and timestamps" -" tcpconnlat -p 185 # trace PID 185 only"; +" tcpconnlat # summarize on-CPU time as a histogram\n" +" tcpconnlat 1 # trace connection latency slower than 1 ms\n" +" tcpconnlat 0.1 # trace connection latency slower than 100 us\n" +" tcpconnlat -t # 1s summaries, milliseconds, and timestamps\n" +" tcpconnlat -p 185 # trace PID 185 only\n" +" tcpconnlat -L # include LPORT while printing outputs\n"; static const struct argp_option opts[] = { { "timestamp", 't', NULL, 0, "Include timestamp on output" }, { "pid", 'p', "PID", 0, "Trace this PID only" }, + { "lport", 'L', NULL, 0, "Include LPORT on output" }, { "verbose", 'v', NULL, 0, "Verbose debug output" }, { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" }, {}, @@ -72,6 +75,9 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) case 't': env.timestamp = true; break; + case 'L': + env.lport = true; + break; case ARGP_KEY_ARG: if (pos_args++) { fprintf(stderr, @@ -131,10 +137,17 @@ void handle_event(void *ctx, int cpu, void *data, __u32 data_sz) return; } - printf("%-6d %-12.12s %-2d %-16s %-16s %-5d %.2f\n", e->tgid, e->comm, - e->af == AF_INET ? 4 : 6, inet_ntop(e->af, &s, src, sizeof(src)), - inet_ntop(e->af, &d, dst, sizeof(dst)), ntohs(e->dport), - e->delta_us / 1000.0); + if (env.lport) { + printf("%-6d %-12.12s %-2d %-16s %-6d %-16s %-5d %.2f\n", e->tgid, e->comm, + e->af == AF_INET ? 4 : 6, inet_ntop(e->af, &s, src, sizeof(src)), e->lport, + inet_ntop(e->af, &d, dst, sizeof(dst)), ntohs(e->dport), + e->delta_us / 1000.0); + } else { + printf("%-6d %-12.12s %-2d %-16s %-16s %-5d %.2f\n", e->tgid, e->comm, + e->af == AF_INET ? 4 : 6, inet_ntop(e->af, &s, src, sizeof(src)), + inet_ntop(e->af, &d, dst, sizeof(dst)), ntohs(e->dport), + e->delta_us / 1000.0); + } } void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt) @@ -199,10 +212,17 @@ int main(int argc, char **argv) goto cleanup; } + /* print header */ if (env.timestamp) printf("%-9s ", ("TIME(s)")); - printf("%-6s %-12s %-2s %-16s %-16s %-5s %s\n", - "PID", "COMM", "IP", "SADDR", "DADDR", "DPORT", "LAT(ms)"); + if (env.lport) { + printf("%-6s %-12s %-2s %-16s %-6s %-16s %-5s %s\n", + "PID", "COMM", "IP", "SADDR", "LPORT", "DADDR", "DPORT", "LAT(ms)"); + } else { + printf("%-6s %-12s %-2s %-16s %-16s %-5s %s\n", + "PID", "COMM", "IP", "SADDR", "DADDR", "DPORT", "LAT(ms)"); + } + if (signal(SIGINT, sig_int) == SIG_ERR) { fprintf(stderr, "can't set signal handler: %s\n", strerror(errno)); diff --git a/libbpf-tools/tcpconnlat.h b/libbpf-tools/tcpconnlat.h index 208a71d1..9dbddfc6 100644 --- a/libbpf-tools/tcpconnlat.h +++ b/libbpf-tools/tcpconnlat.h @@ -18,6 +18,7 @@ struct event { __u64 ts_us; __u32 tgid; int af; + __u16 lport; __u16 dport; };