forked from openresty/openresty-systemtap-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx-active-reqs
executable file
·289 lines (208 loc) · 7.34 KB
/
ngx-active-reqs
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env perl
use 5.006001;
use strict;
use warnings;
use Getopt::Std qw( getopts );
my %opts;
getopts("a:dhp:m", \%opts)
or die usage();
if ($opts{h}) {
print usage();
exit;
}
my $pid = $opts{p}
or die "No nginx process pid specified by the -p option\n";
if ($pid !~ /^\d+$/) {
die "Bad -p option value \"$pid\": not look like a pid\n";
}
my $check_pools = $opts{m};
my $stap_args = $opts{a} || '';
if ($^O ne 'linux') {
die "Only linux is supported but I am on $^O.\n";
}
my $exec_file = "/proc/$pid/exe";
if (!-f $exec_file) {
die "Nginx process $pid is not running.\n";
}
my $nginx_path = readlink $exec_file;
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.0) {
die "ERROR: at least systemtap 2.0 is required but found $v\n";
}
} else {
die "ERROR: unknown version of systemtap:\n$ver\n";
}
my $stap_src;
my $preamble = <<_EOC_;
probe begin {
printf("Tracing %d ($nginx_path)...\\n\\n", target())
}
_EOC_
chop $preamble;
my ($check_pool_init_code, $check_pool_code, $fin_code);
if ($check_pools) {
$check_pool_init_code = <<_EOC_;
ngx_pool_t_size = &\@cast(0, "ngx_pool_t")[1]
size_t_size = &\@cast(0, "size_t")[1]
total_mem_for_reqs = 0
_EOC_
chop $check_pool_init_code;
$check_pool_code = <<_EOC_;
pool = \@cast(r, "ngx_http_request_t")->pool
if (!pool) {
printf(" pool is null\\n\\n")
continue
}
end = \@cast(pool, "ngx_pool_t")->d->end
printf(" pool chunk size: %d\\n", end - pool)
lim = \@cast(pool, "ngx_pool_t")->max + 1
/* analyze small blocks */
used = 0
unused = 0
p = pool
n = \@cast(pool, "ngx_pool_t")->d->next
for ( ;; ) {
last = \@cast(p, "ngx_pool_t")->d->last
end = \@cast(p, "ngx_pool_t")->d->end
used += last - p - ngx_pool_t_size
unused += end - last
/* printf("used: %d, unused %d\\n", last - p - ngx_pool_t_size, end - last) */
if (n == 0) {
break
}
p = n
n = \@cast(n, "ngx_pool_t")->d->next
}
printf(" small blocks (< %d): %d bytes used, %d bytes unused\\n",
lim, used, unused)
/* analyze large blocks */
total = 0
blocks = 0
for (l = \@cast(pool, "ngx_pool_t")->large;
l;
l = \@cast(l, "ngx_pool_large_t")->next)
{
ptr = \@cast(l, "ngx_pool_large_t")->alloc
if (ptr) {
blocks++
/* XXX specific to the glibc malloc implementation */
ptr -= size_t_size
block_size = \@cast(ptr, "size_t")[0] & ~(size_t_size - 1)
/* printf("large block size: %d %d\\n",
\@cast(ptr, "size_t")[0], block_size) */
total += block_size
}
}
printf(" large blocks (>= %d): %d blocks, %d bytes (used)\\n",
lim, blocks, total)
/* total summary */
printf(" total used: %d bytes\\n\\n", total + used)
total_mem_for_reqs += total + used
_EOC_
chop $check_pool_code;
$fin_code = <<_EOC_;
printf("total memory used for all %d active requests: %d bytes\\n",
nreqs, total_mem_for_reqs)
_EOC_
chop $fin_code;
} else {
$check_pool_init_code = '';
$check_pool_code = '';
$fin_code = <<_EOC_;
printf("\\nfound %d active requests.\\n", nreqs)
_EOC_
chop $fin_code;
}
$stap_src = <<_EOC_;
$preamble
probe process("$nginx_path").function("ngx_process_events_and_timers"),
process("$nginx_path").function("ngx_http_handler")
{
if (pid() == target()) {
begin = local_clock_us()
$check_pool_init_code
cached_sec = \@var("ngx_cached_time")->sec
cached_msec = \@var("ngx_cached_time")->msec
conns = \@var("ngx_cycle\@ngx_cycle.c")->connections
conn_n = \@var("ngx_cycle\@ngx_cycle.c")->connection_n
nreqs = 0
for (i = 0; i < conn_n; i++) {
c = &\@cast(conns, "ngx_connection_t")[i]
fd = \@cast(c, "ngx_connection_t")->fd
read = \@cast(c, "ngx_connection_t")->read
reqs = \@cast(c, "ngx_connection_t")->requests
destroyed = \@cast(c, "ngx_connection_t")->destroyed
if (reqs && fd != -1 && read && !destroyed) {
nreqs++;
r = \@cast(c, "ngx_connection_t")->data
/* get uri */
uri = &\@cast(r, "ngx_http_request_t")->uri
uri_data = \@cast(uri, "ngx_str_t")->data
uri_len = \@cast(uri, "ngx_str_t")->len
/* get uri args */
args = &\@cast(r, "ngx_http_request_t")->args
args_data = \@cast(args, "ngx_str_t")->data
args_len = \@cast(args, "ngx_str_t")->len
if (args_len == 0) {
args_str = ""
} else {
args_str = user_string_n(args_data, args_len)
}
/* get request time */
start_sec = \@cast(r, "ngx_http_request_t")->start_sec
start_msec = \@cast(r, "ngx_http_request_t")->start_msec
ms = (cached_sec - start_sec) * 1000 + (cached_msec - start_msec)
if (ms < 0) {
ms = 0
}
/* get method name */
method = &\@cast(r, "ngx_http_request_t")->method_name
method_data = \@cast(method, "ngx_str_t")->data
method_len = \@cast(method, "ngx_str_t")->len
/* get main request */
main = \@cast(r, "ngx_http_request_t")->main
printf("%s \\"%s %s\?%s\\", time %d.%03d sec, conn reqs %d, fd %d\\n",
main == r ? "req" : "subreq",
user_string_n(method_data, method_len),
user_string_n(uri_data, uri_len), args_str,
ms / 1000, ms % 1000,
reqs, fd)
$check_pool_code
}
}
$fin_code
elapsed = local_clock_us() - begin
printf("%d microseconds elapsed in the probe handler.\\n", elapsed)
exit()
} /* pid() == target() */
}
_EOC_
if ($opts{d}) {
print $stap_src;
exit;
}
open my $in, "|stap -x $pid $stap_args -"
or die "Cannot run stap: $!\n";
print $in $stap_src;
close $in;
sub usage {
return <<'_EOC_';
Usage:
ngx-active-reqs [optoins]
Options:
-a <args> Pass extra arguments to the stap utility.
-d Dump out the systemtap script source.
-h Print this usage.
-p <pid> Specify the nginx (worker) process pid.
-m Gather the information about request memory pools.
Examples:
ngx-active-reqs -p 12345
ngx-active-reqs -p 12345 -m
ngx-active-reqs -p 12345 -m -a '-DMAXACTION=100000'
_EOC_
}