其他
绝了!这个MySQL故障定位方法太好用了
Part1 探针的原理
KProbes 是 Linux 内核探针,可以用于监视生产系统中的事件。您可以使用它来解决性能瓶颈,记录特定事件,跟踪问题等。
Part2 使用探针观测MySQL
#!/usr/bin/bpftrace
BEGIN
{
printf("Tracing mysqld queries slower than %d ms. Ctrl-C to end.\n",
$1);
printf("%-10s %-6s %6s %s\n", "TIME(ms)", "PID", "MS", "QUERY");
}
usdt:/usr/sbin/mysqld:mysql:query__start
{
@query[tid] = str(arg0);
@start[tid] = nsecs;
}
usdt:/usr/sbin/mysqld:mysql:query__done
/@start[tid]/
{
$dur = (nsecs - @start[tid]) / 1000000;
if ($dur > $1) {
printf("%-10u %-6d %6d %s\n", elapsed / 1000000,
pid, $dur, @query[tid]);
}
delete(@query[tid]);
delete(@start[tid]);
}
#!/usr/bin/bpftrace
BEGIN
{
printf("Tracing mysqld queries slower than %d ms. Ctrl-C to end.\n",
$1);
printf("%-10s %-6s %6s %s\n", "TIME(ms)", "PID", "MS", "QUERY");
}
uprobe:/usr/sbin/mysqld:*dispatch_command*
{
$COM_QUERY = 3;
if (arg2 == $COM_QUERY) {
@query[tid] = str(*arg1);
@start[tid] = nsecs;
}
}
uretprobe:/usr/sbin/mysqld:*dispatch_command*
/@start[tid]/
{
$dur = (nsecs - @start[tid]) / 1000000;
if ($dur > $1) {
printf("%-10u %-6d %6d %s\n", elapsed / 1000000,
pid, $dur, @query[tid]);
}
delete(@query[tid]);
delete(@start[tid]);
}
sudo ./mysql_uprobe_slow.bt 10
Attaching 3 probes...
Tracing mysqld queries slower than 10 ms. Ctrl-C to end.
TIME(ms) PID MS QUERY
35976 1083 742 select employees.first_name, employees.last_name, titles.title
93145 1083 224 select * from employees
125348 1083 1727 select * from salaries
#!/usr/bin/bpftrace
BEGIN
{
printf("Tracing MySQL query... Hit Ctrl-C to end.\n");
}
uprobe:/usr/sbin/mysqld:*dispatch_command*
{
@start[tid] = nsecs;
}
uretprobe:/usr/sbin/mysqld:*dispatch_command*
/@start[tid]/
{
@usecs = hist((nsecs - @start[tid]) / 1000000);
delete(@start[tid]);
}
END
{
clear(@start);
}
sudo ./histo.bt
Attaching 4 probes...
Tracing MySQL query... Hit Ctrl-C to end.
^C
@usecs:
[0] 10 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[1] 0 | |
[2, 4) 0 | |
[4, 8) 0 | |
[8, 16) 0 | |
[16, 32) 0 | |
[32, 64) 0 | |
[64, 128) 0 | |
[128, 256) 1 |@@@@@ |
[256, 512) 1 |@@@@@ |
[512, 1K) 0 | |
[1K, 2K) 1 |@@@@@ |
Part3 结语
- End -
直播预告