blob: c523d7a3458aeef540816689c3181c92da4fe82a [file] [log] [blame]
Sasha Goldshteindd37d632017-02-09 05:44:43 -05001#!/usr/bin/python
2#
3# dbslower Trace MySQL and PostgreSQL queries slower than a threshold.
4#
Paul Chaignon46017922017-10-07 11:06:28 +02005# USAGE: dbslower [-v] [-p PID [PID ...]] [-b PATH_TO_BINARY] [-m THRESHOLD]
6# {mysql,postgres}
Sasha Goldshteindd37d632017-02-09 05:44:43 -05007#
8# By default, a threshold of 1ms is used. Set the threshold to 0 to trace all
Paul Chaignon46017922017-10-07 11:06:28 +02009# queries (verbose).
10#
11# Script works in two different modes:
12# 1) USDT probes, which means it needs MySQL and PostgreSQL built with
Igor Mazur5f7035e2017-07-18 10:06:34 +030013# USDT (DTrace) support.
Paul Chaignon46017922017-10-07 11:06:28 +020014# 2) uprobe and uretprobe on exported function of binary specified by
Igor Mazur5f7035e2017-07-18 10:06:34 +030015# PATH_TO_BINARY parameter. (At the moment only MySQL support)
Paul Chaignon46017922017-10-07 11:06:28 +020016#
Igor Mazur5f7035e2017-07-18 10:06:34 +030017# If no PID or PATH_TO_BINARY is provided, the script attempts to discover
18# all MySQL or PostgreSQL database processes and uses USDT probes.
Sasha Goldshteindd37d632017-02-09 05:44:43 -050019#
20# Strongly inspired by Brendan Gregg's work on the mysqld_qslower script.
21#
Paul Chaignon956ca1c2017-03-04 20:07:56 +010022# Copyright 2017, Sasha Goldshtein
Sasha Goldshteindd37d632017-02-09 05:44:43 -050023# Licensed under the Apache License, Version 2.0
24#
25# 15-Feb-2017 Sasha Goldshtein Created this.
26
27from bcc import BPF, USDT
28import argparse
Igor Mazur5f7035e2017-07-18 10:06:34 +030029import re
Sasha Goldshteindd37d632017-02-09 05:44:43 -050030import ctypes as ct
31import subprocess
32
33examples = """examples:
34 dbslower postgres # trace PostgreSQL queries slower than 1ms
35 dbslower postgres -p 188 322 # trace specific PostgreSQL processes
36 dbslower mysql -p 480 -m 30 # trace MySQL queries slower than 30ms
Paul Chaignon956ca1c2017-03-04 20:07:56 +010037 dbslower mysql -p 480 -v # trace MySQL queries & print the BPF program
Igor Mazur5f7035e2017-07-18 10:06:34 +030038 dbslower mysql -x $(which mysqld) # trace MySQL queries with uprobes
Sasha Goldshteindd37d632017-02-09 05:44:43 -050039"""
40parser = argparse.ArgumentParser(
41 description="",
42 formatter_class=argparse.RawDescriptionHelpFormatter,
43 epilog=examples)
44parser.add_argument("-v", "--verbose", action="store_true",
45 help="print the BPF program")
46parser.add_argument("db", choices=["mysql", "postgres"],
47 help="the database engine to use")
48parser.add_argument("-p", "--pid", type=int, nargs='*',
49 dest="pids", metavar="PID", help="the pid(s) to trace")
Igor Mazur5f7035e2017-07-18 10:06:34 +030050parser.add_argument("-x", "--exe", type=str,
51 dest="path", metavar="PATH", help="path to binary")
Sasha Goldshteindd37d632017-02-09 05:44:43 -050052parser.add_argument("-m", "--threshold", type=int, default=1,
53 help="trace queries slower than this threshold (ms)")
Nathan Scottcf0792f2018-02-02 16:56:50 +110054parser.add_argument("--ebpf", action="store_true",
55 help=argparse.SUPPRESS)
Sasha Goldshteindd37d632017-02-09 05:44:43 -050056args = parser.parse_args()
57
Sasha Goldshteindd37d632017-02-09 05:44:43 -050058threshold_ns = args.threshold * 1000000
59
Igor Mazur5f7035e2017-07-18 10:06:34 +030060mode = "USDT"
61if args.path and not args.pids:
62 if args.db == "mysql":
Paul Chaignon46017922017-10-07 11:06:28 +020063 regex = "\\w+dispatch_command\\w+"
64 symbols = BPF.get_user_functions_and_addresses(args.path, regex)
Igor Mazur5f7035e2017-07-18 10:06:34 +030065
66 if len(symbols) == 0:
67 print("Can't find function 'dispatch_command' in %s" % (args.path))
68 exit(1)
Paul Chaignon46017922017-10-07 11:06:28 +020069
Igor Mazur5f7035e2017-07-18 10:06:34 +030070 (mysql_func_name, addr) = symbols[0]
71
72 if mysql_func_name.find("COM_DATA") >= 0:
73 mode = "MYSQL57"
74 else:
75 mode = "MYSQL56"
76 else:
77 # Placeholder for PostrgeSQL
Paul Chaignon46017922017-10-07 11:06:28 +020078 # Look on functions initStringInfo, pgstat_report_activity, EndCommand,
79 # NullCommand
Igor Mazur5f7035e2017-07-18 10:06:34 +030080 print("Sorry at the moment PostgreSQL supports only USDT")
81 exit(1)
82
Sasha Goldshteindd37d632017-02-09 05:44:43 -050083program = """
Sasha Goldshteinaa124dd2017-03-31 17:16:45 -040084#include <uapi/linux/ptrace.h>
Sasha Goldshteindd37d632017-02-09 05:44:43 -050085
Igor Mazur5f7035e2017-07-18 10:06:34 +030086DEFINE_THRESHOLD
87DEFINE_USDT
88DEFINE_MYSQL56
89DEFINE_MYSQL57
90
Sasha Goldshteindd37d632017-02-09 05:44:43 -050091struct temp_t {
92 u64 timestamp;
Igor Mazur5f7035e2017-07-18 10:06:34 +030093#ifdef USDT
Sasha Goldshteindd37d632017-02-09 05:44:43 -050094 char *query;
Igor Mazur5f7035e2017-07-18 10:06:34 +030095#else
96 /*
97 MySQL clears query packet before uretprobe call - so copy query in advance
98 */
99 char query[256];
100#endif //USDT
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500101};
102
103struct data_t {
104 u64 pid;
105 u64 timestamp;
106 u64 duration;
107 char query[256];
108};
109
110BPF_HASH(temp, u64, struct temp_t);
111BPF_PERF_OUTPUT(events);
112
Igor Mazur5f7035e2017-07-18 10:06:34 +0300113int query_start(struct pt_regs *ctx) {
114
115#if defined(MYSQL56) || defined(MYSQL57)
116/*
Paul Chaignon46017922017-10-07 11:06:28 +0200117Trace only packets with enum_server_command == COM_QUERY
Igor Mazur5f7035e2017-07-18 10:06:34 +0300118*/
119 #ifdef MYSQL56
120 u64 command = (u64) PT_REGS_PARM1(ctx);
121 #else //MYSQL57
122 u64 command = (u64) PT_REGS_PARM3(ctx);
123 #endif
124 if (command != 3) return 0;
125#endif
126
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500127 struct temp_t tmp = {};
128 tmp.timestamp = bpf_ktime_get_ns();
Igor Mazur5f7035e2017-07-18 10:06:34 +0300129
130#if defined(MYSQL56)
131 bpf_probe_read(&tmp.query, sizeof(tmp.query), (void*) PT_REGS_PARM3(ctx));
132#elif defined(MYSQL57)
133 void* st = (void*) PT_REGS_PARM2(ctx);
134 char* query;
135 bpf_probe_read(&query, sizeof(query), st);
136 bpf_probe_read(&tmp.query, sizeof(tmp.query), query);
137#else //USDT
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500138 bpf_usdt_readarg(1, ctx, &tmp.query);
Igor Mazur5f7035e2017-07-18 10:06:34 +0300139#endif
140
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500141 u64 pid = bpf_get_current_pid_tgid();
142 temp.update(&pid, &tmp);
143 return 0;
144}
145
Igor Mazur5f7035e2017-07-18 10:06:34 +0300146int query_end(struct pt_regs *ctx) {
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500147 struct temp_t *tempp;
148 u64 pid = bpf_get_current_pid_tgid();
149 tempp = temp.lookup(&pid);
150 if (!tempp)
151 return 0;
152
153 u64 delta = bpf_ktime_get_ns() - tempp->timestamp;
Igor Mazur5f7035e2017-07-18 10:06:34 +0300154#ifdef THRESHOLD
155 if (delta >= THRESHOLD) {
Paul Chaignon46017922017-10-07 11:06:28 +0200156#endif //THRESHOLD
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500157 struct data_t data = {};
158 data.pid = pid >> 32; // only process id
159 data.timestamp = tempp->timestamp;
160 data.duration = delta;
161 bpf_probe_read(&data.query, sizeof(data.query), tempp->query);
162 events.perf_submit(ctx, &data, sizeof(data));
Igor Mazur5f7035e2017-07-18 10:06:34 +0300163#ifdef THRESHOLD
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500164 }
Igor Mazur5f7035e2017-07-18 10:06:34 +0300165#endif //THRESHOLD
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500166 temp.delete(&pid);
167 return 0;
Igor Mazur5f7035e2017-07-18 10:06:34 +0300168};
169""".replace("DEFINE_USDT", "#define USDT" if mode == "USDT" else "") \
170 .replace("DEFINE_MYSQL56", "#define MYSQL56" if mode == "MYSQL56" else "") \
171 .replace("DEFINE_MYSQL57", "#define MYSQL57" if mode == "MYSQL57" else "") \
Paul Chaignon46017922017-10-07 11:06:28 +0200172 .replace("DEFINE_THRESHOLD",
173 "#define THRESHOLD %d" % threshold_ns if threshold_ns > 0 else "")
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500174
Igor Mazur5f7035e2017-07-18 10:06:34 +0300175if mode.startswith("MYSQL"):
176 # Uprobes mode
177 bpf = BPF(text=program)
Paul Chaignon46017922017-10-07 11:06:28 +0200178 bpf.attach_uprobe(name=args.path, sym=mysql_func_name,
179 fn_name="query_start")
180 bpf.attach_uretprobe(name=args.path, sym=mysql_func_name,
181 fn_name="query_end")
Igor Mazur5f7035e2017-07-18 10:06:34 +0300182else:
183 # USDT mode
184 if not args.pids or len(args.pids) == 0:
185 if args.db == "mysql":
186 args.pids = map(int, subprocess.check_output(
187 "pidof mysqld".split()).split())
188 elif args.db == "postgres":
189 args.pids = map(int, subprocess.check_output(
190 "pidof postgres".split()).split())
191
192 usdts = map(lambda pid: USDT(pid=pid), args.pids)
193 for usdt in usdts:
194 usdt.enable_probe("query__start", "query_start")
195 usdt.enable_probe("query__done", "query_end")
196 if args.verbose:
197 print('\n'.join(map(lambda u: u.get_text(), usdts)))
198
199 bpf = BPF(text=program, usdt_contexts=usdts)
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500200
Nathan Scottcf0792f2018-02-02 16:56:50 +1100201if args.verbose or args.ebpf:
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500202 print(program)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100203 if args.ebpf:
204 exit()
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500205
206class Data(ct.Structure):
207 _fields_ = [
208 ("pid", ct.c_ulonglong),
209 ("timestamp", ct.c_ulonglong),
210 ("delta", ct.c_ulonglong),
211 ("query", ct.c_char * 256)
212 ]
213
214start = BPF.monotonic_time()
215
216def print_event(cpu, data, size):
217 event = ct.cast(data, ct.POINTER(Data)).contents
218 print("%-14.6f %-6d %8.3f %s" % (
219 float(event.timestamp - start) / 1000000000,
220 event.pid, float(event.delta) / 1000000, event.query))
221
Igor Mazur5f7035e2017-07-18 10:06:34 +0300222if mode.startswith("MYSQL"):
223 print("Tracing database queries for application %s slower than %d ms..." %
224 (args.path, args.threshold))
225else:
226 print("Tracing database queries for pids %s slower than %d ms..." %
227 (', '.join(map(str, args.pids)), args.threshold))
228
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500229print("%-14s %-6s %8s %s" % ("TIME(s)", "PID", "MS", "QUERY"))
230
Mark Drayton5f5687e2017-02-20 18:13:03 +0000231bpf["events"].open_perf_buffer(print_event, page_cnt=64)
Sasha Goldshteindd37d632017-02-09 05:44:43 -0500232while True:
Teng Qindbf00292018-02-28 21:47:50 -0800233 bpf.perf_buffer_poll()