forked from openresty/openresty-systemtap-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx-shm
executable file
·259 lines (196 loc) · 6.79 KB
/
ngx-shm
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
#!/usr/bin/env perl
use 5.006001;
use strict;
use warnings;
use Getopt::Std qw( getopts );
my %opts;
getopts("a:dhp:n:", \%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 $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 $zone = $opts{n};
my $preamble = <<_EOC_;
probe begin {
printf("Tracing %d ($nginx_path)...\\n\\n", target())
}
_EOC_
chop $preamble;
if ($zone) {
my $quoted_zone = quote_str($zone);
my $zone_name_len = length $zone;
$stap_src = <<_EOC_;
$preamble
probe process("$nginx_path").function("ngx_process_events_and_timers"),
process("$nginx_path").function("ngx_http_init_request")
{
if (pid() == target()) {
found = 0
begin = local_clock_us()
pagesize = \@var("ngx_pagesize\@ngx_alloc.c")
part = &\@var("ngx_cycle\@ngx_cycle.c")->shared_memory->part
zone = \@cast(part, "ngx_list_part_t")->elts
for (i = 0; ; i++) {
if (i >= \@cast(part, "ngx_list_part_t")->nelts) {
if (\@cast(part, "ngx_list_part_t")->next == 0) {
break
}
part = \@cast(part, "ngx_list_part_t")->next
zone = \@cast(part, "ngx_list_part_t")->elts
i = 0
}
shm = &\@cast(zone, "ngx_shm_zone_t")[i]->shm
name = &\@cast(shm, "ngx_shm_t")->name
if (\@cast(name, "ngx_str_t")->len != $zone_name_len) {
continue;
}
zone_name = user_string_n(\@cast(name, "ngx_str_t")->data, $zone_name_len)
if (zone_name != $quoted_zone) {
continue;
}
init = \@cast(zone, "ngx_shm_zone_t")[i]->init
addr = \@cast(shm, "ngx_shm_t")->addr
if (addr != \@cast(addr, "ngx_slab_pool_t")->addr) {
error("shm zone \\"" . zone_name . "\\" is corrupted: "
. "pool != pool->addr")
exit()
}
init_name = usymname(init)
owner = str_replace(str_replace(init_name, "_init", ""), "_zone", "")
printf("shm zone \\"%s\\"\\n", zone_name)
printf(" owner: %s\\n", owner)
printf(" total size: %d KB\\n", \@cast(shm, "ngx_shm_t")->size / 1024)
pages = 0
free = &\@cast(addr, "ngx_slab_pool_t")->free
blocks = 0
for (page = \@cast(free, "ngx_slab_page_t")->next;
page != free;
page = \@cast(page, "ngx_slab_page_t")->next)
{
pages += \@cast(page, "ngx_slab_page_t")->slab
if (++blocks % 1000 == 0) {
printf("\\r free pages: %d KB (%d pages, %d blocks)",
pages * pagesize / 1024, pages, blocks)
}
}
printf("\\r free pages: %d KB (%d pages, %d blocks)\\n",
pages * pagesize / 1024, pages, blocks)
found = 1
break
}
if (!found) {
printf("shm zone \\"%s\\" not found.\\n", $quoted_zone)
}
elapsed = local_clock_us() - begin
printf("\\n%d microseconds elapsed in the probe handler.\\n", elapsed)
exit()
} /* pid() == target() */
}
_EOC_
} else {
# no zone name specified
$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()
part = &\@var("ngx_cycle\@ngx_cycle.c")->shared_memory->part
zone = \@cast(part, "ngx_list_part_t")->elts
for (i = 0; ; i++) {
if (i >= \@cast(part, "ngx_list_part_t")->nelts) {
if (\@cast(part, "ngx_list_part_t")->next == 0) {
break
}
part = \@cast(part, "ngx_list_part_t")->next
zone = \@cast(part, "ngx_list_part_t")->elts
i = 0
}
shm = &\@cast(zone, "ngx_shm_zone_t")[i]->shm
addr = \@cast(shm, "ngx_shm_t")->addr
init = \@cast(zone, "ngx_shm_zone_t")[i]->init
name = &\@cast(shm, "ngx_shm_t")->name
zone_name = user_string_n(\@cast(name, "ngx_str_t")->data,
\@cast(name, "ngx_str_t")->len)
if (addr != \@cast(addr, "ngx_slab_pool_t")->addr) {
printf("shm zone \\"%s\\" is corrupted: pool != pool->addr\\n",
zone_name)
continue;
}
init_name = usymname(init)
owner = str_replace(str_replace(init_name, "_init", ""), "_zone", "")
printf("shm zone \\"%s\\"\\n owner: %s\\n total size: %d KB\\n\\n",
user_string_n(\@cast(name, "ngx_str_t")->data,
\@cast(name, "ngx_str_t")->len),
owner, \@cast(shm, "ngx_shm_t")->size / 1024)
}
println("Use the -n <zone> option to see more details about each zone.")
elapsed = local_clock_us() - begin
printf("%d microseconds elapsed in the probe.\\n", elapsed)
exit()
} /* pid() == target() */
}
_EOC_
}
if ($opts{d}) {
print $stap_src;
exit;
}
open my $in, "|stap $stap_args -x $pid -"
or die "Cannot run stap: $!\n";
print $in $stap_src;
close $in;
sub usage {
return <<'_EOC_';
Usage:
ngx-shm [optoins]
Options:
-a <args> Pass extra arguments to the stap utility.
-d Dump out the systemtap script source.
-h Print this usage.
-n <zone> Show details about the shm zone named <zone>.
-p <pid> Specify the nginx worker process pid.
Examples:
ngx-shm -p 12345
ngx-shm -p 12345 -n dogs
ngx-shm -p 12345 -n my_zone_name -a '-DMAXACTION=100000'
_EOC_
}
sub quote_str {
my $s = shift;
$s =~ s/\\/\\\\/g;
$s =~ s/"/\\"/g;
$s =~ s/\n/\\n/g;
$s =~ s/\t/\\t/g;
$s =~ s/\r/\\r/g;
return qq{"$s"};
}