绝了!这个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:
[] 10 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[1] | |
[2, 4) | |
[4, 8) | |
[8, 16) | |
[16, 32) | |
[32, 64) | |
[64, 128) | |
[128, 256) 1 |@@@@@ |
[256, 512) 1 |@@@@@ |
[512, 1K) | |
[1K, 2K) 1 |@@@@@ |
Part3 结语
除了这两种探针,还可以使用 tracepoints 和 KProbe 来分析内核态,例如网络运行情况,磁盘 I/O 情况,当然这需要你对程序有一定熟悉,要不然不知道这些探针加到哪个地方好。
相关文章