Skip to content

Commit

Permalink
libbpf tcptop: Fix PID column alignment when pids are large (iovisor#…
Browse files Browse the repository at this point in the history
…5004)

Fix PID column alignment when PIDs are large in libbpf-tools/tcptop:

Check the maximum width of the PIDs on the system and adjust the format to avoid column misalignment:

PID     COMM         LADDR                 RADDR                  RX_KB  TX_KB
1987802 wget         127.0.0.1:44960       127.0.0.1:443              6      0
1987815 wget         127.0.0.1:44968       127.0.0.1:443              6      0
2770    stunnel      127.0.0.1:51460       127.0.0.1:80               4      0
2770    stunnel      127.0.0.1:51452       127.0.0.1:80               4      0
1936412 sshd         10.71.56.137:22       10.71.8.14:45682           0      2
1987805 curl         127.0.0.1:44964       127.0.0.1:443              1      0
1987818 curl         127.0.0.1:44972       127.0.0.1:443              1      0
2770    stunnel      127.0.0.1:51456       127.0.0.1:80               0      0
2770    stunnel      127.0.0.1:51464       127.0.0.1:80               0      0
1918977 sshd         10.71.56.137:22       10.71.8.14:51046           0      0

    Also fix the alignment of the "TX_KB" label by moving the trailing "\n" character from it to the format string
    Also add libbpf-tools/tcptop_example.txt documenting the example.

Signed-off-by: Bernhard Kaindl <[email protected]>
  • Loading branch information
bernhardkaindl committed May 19, 2024
1 parent 5bc97bb commit b0b4239
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
24 changes: 16 additions & 8 deletions libbpf-tools/tcptop.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */

/*
* tcptop Trace sending and received operation over IP.
* tcptop: Summarize the top active TCP sessions - like top, but for TCP
* Copyright (c) 2022 Francis Laniel <[email protected]>
*
* Based on tcptop(8) from BCC by Brendan Gregg.
Expand Down Expand Up @@ -57,7 +57,7 @@ const char *argp_program_version = "tcptop 0.1";
const char *argp_program_bug_address =
"https://github.com/iovisor/bcc/tree/master/libbpf-tools";
const char argp_program_doc[] =
"Trace sending and received operation over IP.\n"
"Summarize the top active TCP sessions - like top, but for TCP\n"
"\n"
"USAGE: tcptop [-h] [-p PID] [interval] [count]\n"
"\n"
Expand Down Expand Up @@ -224,6 +224,12 @@ static int print_stat(struct tcptop_bpf *obj)
int fd = bpf_map__fd(obj->maps.ip_map);
int rows = 0;
bool ipv6_header_printed = false;
int pid_max_fd = open("/proc/sys/kernel/pid_max", O_RDONLY);
int pid_maxlen = read(pid_max_fd, buf, sizeof buf) - 1;

if (pid_maxlen < 6)
pid_maxlen = 6;
close(pid_max_fd);

if (!no_summary) {
f = fopen("/proc/loadavg", "r");
Expand Down Expand Up @@ -258,8 +264,9 @@ static int print_stat(struct tcptop_bpf *obj)
rows++;
}

printf("%-6s %-12s %-21s %-21s %6s %6s", "PID", "COMM", "LADDR", "RADDR",
"RX_KB", "TX_KB\n");
printf("%-*s %-12s %-21s %-21s %6s %6s\n",
pid_maxlen, "PID", "COMM", "LADDR", "RADDR",
"RX_KB", "TX_KB");

qsort(infos, rows, sizeof(struct info_t), sort_column);
rows = rows < output_rows ? rows : output_rows;
Expand All @@ -273,8 +280,9 @@ static int print_stat(struct tcptop_bpf *obj)
/* Width to fit IPv6 plus port. */
column_width = 51;
if (!ipv6_header_printed) {
printf("\n%-6s %-12s %-51s %-51s %6s %6s", "PID", "COMM", "LADDR6",
"RADDR6", "RX_KB", "TX_KB\n");
printf("\n%-*s %-12s %-51s %-51s %6s %6s\n",
pid_maxlen, "PID", "COMM", "LADDR6",
"RADDR6", "RX_KB", "TX_KB");
ipv6_header_printed = true;
}
}
Expand All @@ -298,8 +306,8 @@ static int print_stat(struct tcptop_bpf *obj)
snprintf(saddr_port, size, "%s:%d", saddr, key->lport);
snprintf(daddr_port, size, "%s:%d", daddr, key->dport);

printf("%-6d %-12.12s %-*s %-*s %6ld %6ld\n",
key->pid, key->name,
printf("%-*d %-12.12s %-*s %-*s %6ld %6ld\n",
pid_maxlen, key->pid, key->name,
column_width, saddr_port,
column_width, daddr_port,
value->received / 1024, value->sent / 1024);
Expand Down
65 changes: 65 additions & 0 deletions libbpf-tools/tcptop_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
Documentation of tcptop (the Linux BPF CO-RE version).
It is usually shipped as libbpf-tcptop.


Description:
Trace sending and received operation over IP.

Example output:

# ./tcptop
12:20:00 loadavg: 0.00 0.00 0.00 1/407 1987822

PID COMM LADDR RADDR RX_KB TX_KB
1987802 wget 127.0.0.1:44960 127.0.0.1:443 6 0
1987815 wget 127.0.0.1:44968 127.0.0.1:443 6 0
2770 stunnel 127.0.0.1:51460 127.0.0.1:80 4 0
2770 stunnel 127.0.0.1:51452 127.0.0.1:80 4 0
1936412 sshd 10.71.56.137:22 10.71.8.14:45682 0 2
1987805 curl 127.0.0.1:44964 127.0.0.1:443 1 0
1987818 curl 127.0.0.1:44972 127.0.0.1:443 1 0
2770 stunnel 127.0.0.1:51456 127.0.0.1:80 0 0
2770 stunnel 127.0.0.1:51464 127.0.0.1:80 0 0
1918977 sshd 10.71.56.137:22 10.71.8.14:51046 0 0

PID COMM LADDR6 RADDR6 RX_KB TX_KB
2770 stunnel ::ffff:127.0.0.1:443 ::ffff:127.0.0.1:44960 0 6
2770 stunnel ::ffff:127.0.0.1:443 ::ffff:127.0.0.1:44968 0 6
2158 server ::ffff:127.0.0.1:80 ::ffff:127.0.0.1:51452 0 4
2158 server ::ffff:127.0.0.1:80 ::ffff:127.0.0.1:51460 0 4
2770 stunnel ::ffff:127.0.0.1:443 ::ffff:127.0.0.1:44964 0 1
2770 stunnel ::ffff:127.0.0.1:443 ::ffff:127.0.0.1:44972 0 1
2158 server ::ffff:127.0.0.1:80 ::ffff:127.0.0.1:51464 0 0
2158 server ::ffff:127.0.0.1:80 ::ffff:127.0.0.1:51456 0 0

USAGE message:

# ./tcptop -h
Trace sending and received operation over IP.

USAGE: tcptop [-h] [-p PID] [interval] [count]

EXAMPLES:
tcptop # TCP top, refresh every 1s
tcptop -p 1216 # only trace PID 1216
tcptop -c path # only trace the given cgroup path
tcptop 5 10 # 5s summaries, 10 times

-4, --ipv4 trace IPv4 family only
-6, --ipv6 trace IPv6 family only
-c, --cgroup=/sys/fs/cgroup/unified
Trace process in cgroup path
-C, --noclear Don't clear the screen
-p, --pid=PID Process ID to trace
-r, --rows=ROWS Maximum rows to print, default 20
-s, --sort=SORT Sort columns, default all [all, sent, received]
-S, --nosummary Skip system summary line
-v, --verbose Verbose debug output
-?, --help Give this help list
--usage Give a short usage message
-V, --version Print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

Report bugs to https://github.com/iovisor/bcc/tree/master/libbpf-tools.

0 comments on commit b0b4239

Please sign in to comment.