forked from soarpenguin/systemtap-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp-recv-queue
executable file
·184 lines (149 loc) · 4.38 KB
/
tcp-recv-queue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env perl
# Copyright (C) Yichun Zhang (agentzh)
use 5.006001;
use strict;
use warnings;
use Getopt::Long qw( GetOptions );
GetOptions("a=s", \(my $stap_args),
"d", \(my $dump_src),
"h", \(my $help),
"dport=i", \(my $dport),
"time=i", \(my $time))
or die usage();
if ($help) {
print usage();
exit;
}
if (!defined $dport) {
die "No destination port specified by the --dport option.\n";
}
if (!defined $stap_args) {
$stap_args = '';
}
if ($stap_args !~ /\b-D\s*MAXACTION=/) {
$stap_args .= " -DMAXACTION=100000";
}
my $ver = `stap --version 2>&1`;
if (!defined $ver) {
die "Systemtap not installed or its \"stap\" utility is not visible to the PATH environment: $!\n";
}
if ($ver =~ /version\s+(\d+\.\d+)/i) {
my $v = $1;
if ($v < 2.1) {
die "ERROR: at least systemtap 2.1 is required but found $v\n";
}
} else {
die "ERROR: unknown version of systemtap:\n$ver\n";
}
my $stap_src;
my $summary_code = <<_EOC_;
if (!found) {
println("\\nNo queued received packets found yet.")
} else {
println("\\n=== Distribution of First-In-First-Out Latency (us) in TCP Receive Queue ===")
printf("min/avg/max: %d/%d/%d\\n",
\@min(latency_stats), \@avg(latency_stats), \@max(latency_stats))
println(\@hist_log(latency_stats))
}
_EOC_
my $postamble = <<_EOC_;
probe end {
$summary_code
}
_EOC_
my $tip;
if (!defined $time) {
$tip = "Hit Ctrl-C to end."
} else {
$tip = "Sampling for $time seconds.";
$postamble .= <<_EOC_;
probe timer.s($time) {
exit()
}
_EOC_
}
$stap_src = <<_EOC_;
global latency_stats
global start_times
global found
probe begin {
warn("Tracing the TCP receive queues for packets to the port $dport...\\n$tip\\n")
}
/* TODO: take into account the TCP out_of_order queue (i.e., tcp_ofo_queue). */
probe kernel.function("tcp_queue_rcv")!,
kernel.function("tcp_data_queue")
{
tcphdr = __get_skb_tcphdr(\$skb)
dport = __tcp_skb_dport(tcphdr)
sport = __tcp_skb_sport(tcphdr)
if (dport == $dport && start_times[\$sk, sport] == 0) {
//printf("tcp_queue_rcv: queue=%p sk=%p sport=%d\\n",
//&\$sk->sk_receive_queue, \$sk, sport)
if (\$skb->len > 0) {
//println("probe func: ", probefunc())
if (\@cast(\$skb->cb, "tcp_skb_cb")->seq != \@cast(\$skb->cb, "tcp_skb_cb")->end_seq) {
start_times[\$sk, sport] = gettimeofday_us()
} else {
//println("found seq == end_seq")
}
}
}
}
probe kernel.function("tcp_recvmsg"), kernel.function("tcp_recv_skb") {
q = &\$sk->sk_receive_queue
skb = \$sk->sk_receive_queue->next
if (q != skb) {
/* queue is not empty */
tcphdr = __get_skb_tcphdr(skb)
dport = __tcp_skb_dport(tcphdr)
if (dport == $dport) {
sport = __tcp_skb_sport(tcphdr)
begin = start_times[\$sk, sport]
if (begin > 0) {
//printf("tcp recvmsg: port=$dport sk=%p\\n", \$sk)
latency_stats <<< (gettimeofday_us() - begin)
found = 1
delete start_times[\$sk, sport]
}
}
}
}
probe kernel.function("tcp_close"), kernel.function("tcp_disconnect") {
q = &\$sk->sk_receive_queue
skb = \$sk->sk_receive_queue->next
if (q != skb) {
/* queue is not empty */
tcphdr = __get_skb_tcphdr(skb)
dport = __tcp_skb_dport(tcphdr)
if (dport == $dport) {
sport = __tcp_skb_sport(tcphdr)
delete start_times[\$sk, sport]
}
}
}
$postamble
_EOC_
if ($dump_src) {
print $stap_src;
exit;
}
open my $in, "|stap --all-modules $stap_args -"
or die "Cannot run stap: $!\n";
print $in $stap_src;
close $in;
sub usage {
return <<'_EOC_';
Usage:
tcp-recv-queue [optoins]
Options:
-a <args> Pass extra arguments to the stap utility.
-d Dump out the systemtap script source.
-h Print this usage.
--dport=<port> Specify the destination port to be analyzed.
--time=<seconds> Time to wait before printing out the report and exiting
(Only meaningful with --distr)
Examples:
tcp-recv-queue --dport=11211
tcp-recv-queue --dport=6379 --time=10 -a '-DMAXACTION=100000'
_EOC_
}