-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathswoole-redis-watch
207 lines (166 loc) · 5.26 KB
/
swoole-redis-watch
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
#! /usr/bin/env perl
# Copyright (C) YangBingwu (detailyang)
use strict;
use warnings;
use Getopt::Std qw ( getopts );
sub usage();
my %opts;
getopts("a:t:hl:p:dt:", \%opts) or die usage();
if ($opts{h}) {
print usage();
exit;
}
my $time = $opts{t} || 20000;
if ($time !~ /^\d+$/) {
die "Bad -t option value \"$time\": not look like a time\n";
}
my $pid = $opts{p} || 0;
my $count = $opts{c} || 100;
if ($count !~ /^\d+$/) {
die "Bad -c option value \"$count\": not look like a count\n";
}
my $swoole_path = $opts{l} || "";
if (! -f $swoole_path) {
die "swoole.so path $swoole_path is not exist or ",
"you do not have enough permissions.\n";
}
my $preamble = <<_EOS_;
/*
#define REDIS_REPLY_STRING 1
#define REDIS_REPLY_ARRAY 2
#define REDIS_REPLY_INTEGER 3
#define REDIS_REPLY_NIL 4
#define REDIS_REPLY_STATUS 5
#define REDIS_REPLY_ERROR 6
*/
%{
struct sdshdr {
unsigned int len;
unsigned int free;
char buf[];
};
%}
function sdslen:long(s) %{
struct sdshdr *sh = (void*)(STAP_ARG_s-(sizeof(struct sdshdr)));
if (sh) {
STAP_RETVALUE = sh->len;
} else {
STAP_ERROR("sh is null");
}
%}
probe begin {
warn("Tracing swoole.so($swoole_path) for pid:$pid")
}
_EOS_
my $stap_args = $opts{a} || "";
my $stap_src = <<_EOS_;
$preamble
probe process("$swoole_path").function("swoole_redis_onWrite") {
ac = \@cast(\$event->socket->object, "swRedisClient")->context
len = sdslen(ac->c->obuf)
if (len < 64) {
printf("%s(%d) is writing for %s:%d to %s len:%d\\n", execname(), pid(), user_string(ac->c->tcp->host), ac->c->tcp->port, user_string(ac->c->obuf), len);
} else {
printf("%s(%d) is writing for %s:%d to buf-pre64:%s len:%d\\n", execname(), pid(), user_string(ac->c->tcp->host), ac->c->tcp->port, user_string_n(ac->c->obuf, 64), len);
}
}
function print_redis_reply_info(c, host, port) {
len = \@cast(c, "struct redisReply", "$swoole_path")->len
str = \@cast(c, "struct redisReply", "$swoole_path")->str
type = \@cast(c, "struct redisReply", "$swoole_path")->type
integer = \@cast(c, "struct redisReply", "$swoole_path")->integer
if (type == 1) {
if (len < 64) {
printf("%s:%d get reply: string:%s \\n",
user_string(host), port, user_string_n(str, len));
} else {
printf("%s:%d get reply: string-pre16:%s len:%d\\n",
user_string(host), port, user_string_n(str, 16), len);
}
} else if (type == 3) {
printf("%s:%d get reply: integer:%d \\n",
user_string(host), port, integer);
} else if (type == 4) {
printf("%s:%d get reply: nil \\n",
user_string(host), port);
} else if (type == 5) {
printf("%s:%d get reply: ok \\n",
user_string(host), port);
}
}
probe process("$swoole_path").function("swoole_redis_onResult") {
len = \@cast(\$r, "struct redisReply")->len
str = \@cast(\$r, "struct redisReply")->str
type = \@cast(\$r, "struct redisReply")->type
integer = \@cast(\$r, "struct redisReply")->integer
if (type == 1) {
if (len < 64) {
printf("%s:%d get reply: string:%s \\n",
user_string(\$c->c->tcp->host), \$c->c->tcp->port, user_string_n(str, len));
} else {
printf("%s:%d get reply: string-pre16:%s len:%d\\n",
user_string(\$c->c->tcp->host), \$c->c->tcp->port, user_string_n(str, 16), len);
}
} else if (type == 2) {
elements = \@cast(\$r, "struct redisReply")->elements
element = \@cast(\$r, "struct redisReply")->element
for (i = 0; i < elements; i ++) {
nr = &\@cast(element, "struct redisReply")[i];
print_redis_reply_info(nr, \$c->c->tcp->host, \$c->c->tcp->port);
}
} else if (type == 3) {
printf("%s:%d get reply: integer:%d \\n",
user_string(\$c->c->tcp->host), \$c->c->tcp->port, integer);
} else if (type == 4) {
printf("%s:%d get reply: nil \\n",
user_string(\$c->c->tcp->host), \$c->c->tcp->port);
} else if (type == 5) {
printf("%s:%d get reply: ok \\n",
user_string(\$c->c->tcp->host), \$c->c->tcp->port);
} else if (type == 6) {
printf("%s:%d get reply: error:%s \\n",
user_string(\$c->c->tcp->host), \$c->c->tcp->port, user_string(\$c->c->errstr));
}
}
%(
$time > 0
%?
probe timer.ms($time) {
warn("Time Ending....")
exit()
}
%:
probe end() {
exit()
}
%)
_EOS_
if ($opts{d}) {
print $stap_src;
exit;
}
my $in;
if ($pid > 0) {
open $in, "|stap --skip-badvars -x $pid $stap_args -g -"
or die "Cannot run stap: $!\n";
} else {
open $in, "|stap --skip-badvars $stap_args -g -"
or die "Cannot run stap: $!\n";
}
print $in $stap_src;
close $in;
sub usage() {
return <<'_EOS_';
Usage:
swoole-redis-watch [optoins]
Options:
-a <args> Pass extra arguments to the stap utility.
-d Dump out the systemtap script source.
-h Print this usage.
-l <path> Specify the swoole path to sampling.
-p <pid> Specify the process id to sampling.
-t <time>ms Specify the number of milliseconds for sampling.
Examples:
swoole-redis-watch -p 12345 -t 10000
_EOS_
}