-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathpdomysql-watch-query
executable file
·201 lines (166 loc) · 4.33 KB
/
pdomysql-watch-query
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
#! /usr/bin/env perl
# Copyright (C) YangBingwu (detailyang)
use 5.006001;
use strict;
use warnings;
use Getopt::Std qw( getopts );
sub usage();
my %opts;
getopts("ht:p:l:dt:s:c:T:P:m", \%opts) or die usage();
if ($opts{h}) {
print usage();
exit;
}
my $mysql_nd = $opts{m} || "";
my $mysql_nd_condition = "";
if ($mysql_nd) {
$mysql_nd_condition = <<_EOS_;
host = server->host ? server->host : server->host_info
host_len = 0
port = server->port
user = server->user
_EOS_
} else {
$mysql_nd_condition = <<_EOS_;
host_len = server->data->host_len
host = server->data->host
port = server->data->port
user = server->data->user
_EOS_
}
my $port = $opts{P} || "";
my $port_condition = "";
if ($port) {
$port_condition = <<_EOS_;
if (port != $port) {
next
}
_EOS_
}
my $count = $opts{c} || 100;
if ($count !~ /^\d+$/) {
die "Bad -t option value \"$count\": not look like count\n";
}
my $count_condition = "";
if ($count > 0) {
$count_condition = <<_EOS_;
if (++nreqs > $count) {
warn("up to max requests:$count")
exit();
}
_EOS_
}
my $sql = $opts{s} || "" ;
my $sql_condition = "";
if ($sql) {
$sql_condition = <<_EOS_;
if (sql !~ "$sql") {
next
}
_EOS_
}
my $pid = $opts{p} || 0;
if ($pid !~ /^\d+$/) {
die "Bad -p option value \"$pid\": not look like a pid\n";
}
my $php_path = "";
if ($pid) {
my $exec_file = "/proc/$pid/exe";
if (!-f $exec_file) {
die "PHP $pid is not running or ",
"you do not have enough permissions.\n";
}
$php_path = readlink $exec_file;
} else {
my $path = $opts{l} || "";
if (! -f $path) {
die "pdo_mysql.so path $path is not exist or ",
"you do not have enough permissions.\n";
}
$php_path = $path;
}
my $time = $opts{t} || 20000;
if ($time !~ /^\d+$/) {
die "Bad -t option value \"$time\": not look like time\n";
}
my $slowtime = $opts{T} || 0;
if ($slowtime !~ /^\d+$/) {
die "Bad -T option value \"$slowtime\": not look like time\n";
}
my $stap_args = $opts{a} || '';
my $preamble = <<_EOS_;
global tracet%
global trace%
global nreqs
probe begin {
warn("Tracing pdo-mysql ($pid)")
nreqs = 0
}
_EOS_
my $stap_src = <<_EOS_;
$preamble
probe process("$php_path").function("pdo_mysql_stmt_execute") {
qs = \@cast(\$stmt, "pdo_stmt_t")->active_query_string
qs_len = \@cast(\$stmt, "pdo_stmt_t")->active_query_stringlen
sql = user_string_n(qs, qs_len)
$sql_condition
S = \@cast(\$stmt, "struct _pdo_stmt_t")->driver_data
H = \@cast(S, "pdo_mysql_stmt")->H
server = \@cast(H, "pdo_mysql_db_handle")->server
$mysql_nd_condition
$port_condition
trace[tid()] = sprintf("%s:%d\@%s: %s", host_len ? user_string_n(host, host_len) : user_string(host), port, user_string(user), sql)
tracet[tid()] = gettimeofday_us()
}
probe process("$php_path").function("pdo_mysql_stmt_execute").return {
begin = tracet[tid()]
if (begin) {
elapsed = (gettimeofday_us() - begin) / 1000
if (elapsed < $slowtime) {
next
}
$count_condition
printf("%s(%d) %s RT:%d(ms) RTCODE:%d\\n", execname(), pid(), trace[tid()], elapsed, \$return)
}
}
%(
$time > 0
%?
probe timer.ms($time) {
warn("Time Ending....")
exit()
}
%:
probe end {
exit()
}
%)
_EOS_
if ($opts{d}) {
print $stap_src;
exit;
}
open my $in, "|stap --skip-badvars -x $pid $stap_args -"
or die "Cannot run stap: $!\n";
print $in $stap_src;
close $in;
sub usage() {
return <<'_EOC_';
Usage:
pdomysql-watch-query [optoins]
Options:
-a <args> Pass extra arguments to the stap utility.
-d Dump out the systemtap script source.
-h Print this usage.
-m Specify the pdo mysql for old struct.
-p <pid> Specify the pid to sampling.
-l <pdo_mysql.so> Specify the path of pdo_mysql.so to sampling.
-c <count> Specify the max requests default 100.
-t <time>ms Specify the number of mseconds for sampling.
-T <slow time>ms Specify the threshold of mseconds for sampling.
-s <sql> Specify the sql to sampling.
-P <port> Specify the port to sampling.
Examples:
pdomysql-watch-query -p 12345 -c 100 -T 1000
_EOC_
}