Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # dbslower Trace MySQL and PostgreSQL queries slower than a threshold. |
| 4 | # |
Paul Chaignon | 4601792 | 2017-10-07 11:06:28 +0200 | [diff] [blame] | 5 | # USAGE: dbslower [-v] [-p PID [PID ...]] [-b PATH_TO_BINARY] [-m THRESHOLD] |
| 6 | # {mysql,postgres} |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 7 | # |
| 8 | # By default, a threshold of 1ms is used. Set the threshold to 0 to trace all |
Paul Chaignon | 4601792 | 2017-10-07 11:06:28 +0200 | [diff] [blame] | 9 | # queries (verbose). |
| 10 | # |
| 11 | # Script works in two different modes: |
| 12 | # 1) USDT probes, which means it needs MySQL and PostgreSQL built with |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 13 | # USDT (DTrace) support. |
Paul Chaignon | 4601792 | 2017-10-07 11:06:28 +0200 | [diff] [blame] | 14 | # 2) uprobe and uretprobe on exported function of binary specified by |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 15 | # PATH_TO_BINARY parameter. (At the moment only MySQL support) |
Paul Chaignon | 4601792 | 2017-10-07 11:06:28 +0200 | [diff] [blame] | 16 | # |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 17 | # 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 Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 19 | # |
| 20 | # Strongly inspired by Brendan Gregg's work on the mysqld_qslower script. |
| 21 | # |
Paul Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 22 | # Copyright 2017, Sasha Goldshtein |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 23 | # Licensed under the Apache License, Version 2.0 |
| 24 | # |
| 25 | # 15-Feb-2017 Sasha Goldshtein Created this. |
| 26 | |
| 27 | from bcc import BPF, USDT |
| 28 | import argparse |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 29 | import re |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 30 | import ctypes as ct |
| 31 | import subprocess |
| 32 | |
| 33 | examples = """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 Chaignon | 956ca1c | 2017-03-04 20:07:56 +0100 | [diff] [blame] | 37 | dbslower mysql -p 480 -v # trace MySQL queries & print the BPF program |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 38 | dbslower mysql -x $(which mysqld) # trace MySQL queries with uprobes |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 39 | """ |
| 40 | parser = argparse.ArgumentParser( |
| 41 | description="", |
| 42 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 43 | epilog=examples) |
| 44 | parser.add_argument("-v", "--verbose", action="store_true", |
| 45 | help="print the BPF program") |
| 46 | parser.add_argument("db", choices=["mysql", "postgres"], |
| 47 | help="the database engine to use") |
| 48 | parser.add_argument("-p", "--pid", type=int, nargs='*', |
| 49 | dest="pids", metavar="PID", help="the pid(s) to trace") |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 50 | parser.add_argument("-x", "--exe", type=str, |
| 51 | dest="path", metavar="PATH", help="path to binary") |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 52 | parser.add_argument("-m", "--threshold", type=int, default=1, |
| 53 | help="trace queries slower than this threshold (ms)") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 54 | parser.add_argument("--ebpf", action="store_true", |
| 55 | help=argparse.SUPPRESS) |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 56 | args = parser.parse_args() |
| 57 | |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 58 | threshold_ns = args.threshold * 1000000 |
| 59 | |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 60 | mode = "USDT" |
| 61 | if args.path and not args.pids: |
| 62 | if args.db == "mysql": |
Paul Chaignon | 4601792 | 2017-10-07 11:06:28 +0200 | [diff] [blame] | 63 | regex = "\\w+dispatch_command\\w+" |
| 64 | symbols = BPF.get_user_functions_and_addresses(args.path, regex) |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 65 | |
| 66 | if len(symbols) == 0: |
| 67 | print("Can't find function 'dispatch_command' in %s" % (args.path)) |
| 68 | exit(1) |
Paul Chaignon | 4601792 | 2017-10-07 11:06:28 +0200 | [diff] [blame] | 69 | |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 70 | (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 Chaignon | 4601792 | 2017-10-07 11:06:28 +0200 | [diff] [blame] | 78 | # Look on functions initStringInfo, pgstat_report_activity, EndCommand, |
| 79 | # NullCommand |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 80 | print("Sorry at the moment PostgreSQL supports only USDT") |
| 81 | exit(1) |
| 82 | |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 83 | program = """ |
Sasha Goldshtein | aa124dd | 2017-03-31 17:16:45 -0400 | [diff] [blame] | 84 | #include <uapi/linux/ptrace.h> |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 85 | |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 86 | DEFINE_THRESHOLD |
| 87 | DEFINE_USDT |
| 88 | DEFINE_MYSQL56 |
| 89 | DEFINE_MYSQL57 |
| 90 | |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 91 | struct temp_t { |
| 92 | u64 timestamp; |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 93 | #ifdef USDT |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 94 | char *query; |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 95 | #else |
| 96 | /* |
| 97 | MySQL clears query packet before uretprobe call - so copy query in advance |
| 98 | */ |
| 99 | char query[256]; |
| 100 | #endif //USDT |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | struct data_t { |
| 104 | u64 pid; |
| 105 | u64 timestamp; |
| 106 | u64 duration; |
| 107 | char query[256]; |
| 108 | }; |
| 109 | |
| 110 | BPF_HASH(temp, u64, struct temp_t); |
| 111 | BPF_PERF_OUTPUT(events); |
| 112 | |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 113 | int query_start(struct pt_regs *ctx) { |
| 114 | |
| 115 | #if defined(MYSQL56) || defined(MYSQL57) |
| 116 | /* |
Paul Chaignon | 4601792 | 2017-10-07 11:06:28 +0200 | [diff] [blame] | 117 | Trace only packets with enum_server_command == COM_QUERY |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 118 | */ |
| 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 Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 127 | struct temp_t tmp = {}; |
| 128 | tmp.timestamp = bpf_ktime_get_ns(); |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 129 | |
| 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 Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 138 | bpf_usdt_readarg(1, ctx, &tmp.query); |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 139 | #endif |
| 140 | |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 141 | u64 pid = bpf_get_current_pid_tgid(); |
| 142 | temp.update(&pid, &tmp); |
| 143 | return 0; |
| 144 | } |
| 145 | |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 146 | int query_end(struct pt_regs *ctx) { |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 147 | 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 Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 154 | #ifdef THRESHOLD |
| 155 | if (delta >= THRESHOLD) { |
Paul Chaignon | 4601792 | 2017-10-07 11:06:28 +0200 | [diff] [blame] | 156 | #endif //THRESHOLD |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 157 | 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 Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 163 | #ifdef THRESHOLD |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 164 | } |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 165 | #endif //THRESHOLD |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 166 | temp.delete(&pid); |
| 167 | return 0; |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 168 | }; |
| 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 Chaignon | 4601792 | 2017-10-07 11:06:28 +0200 | [diff] [blame] | 172 | .replace("DEFINE_THRESHOLD", |
| 173 | "#define THRESHOLD %d" % threshold_ns if threshold_ns > 0 else "") |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 174 | |
Igor Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 175 | if mode.startswith("MYSQL"): |
| 176 | # Uprobes mode |
| 177 | bpf = BPF(text=program) |
Paul Chaignon | 4601792 | 2017-10-07 11:06:28 +0200 | [diff] [blame] | 178 | 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 Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 182 | else: |
| 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 Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 200 | |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 201 | if args.verbose or args.ebpf: |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 202 | print(program) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 203 | if args.ebpf: |
| 204 | exit() |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 205 | |
| 206 | class 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 | |
| 214 | start = BPF.monotonic_time() |
| 215 | |
| 216 | def 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 Mazur | 5f7035e | 2017-07-18 10:06:34 +0300 | [diff] [blame] | 222 | if mode.startswith("MYSQL"): |
| 223 | print("Tracing database queries for application %s slower than %d ms..." % |
| 224 | (args.path, args.threshold)) |
| 225 | else: |
| 226 | print("Tracing database queries for pids %s slower than %d ms..." % |
| 227 | (', '.join(map(str, args.pids)), args.threshold)) |
| 228 | |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 229 | print("%-14s %-6s %8s %s" % ("TIME(s)", "PID", "MS", "QUERY")) |
| 230 | |
Mark Drayton | 5f5687e | 2017-02-20 18:13:03 +0000 | [diff] [blame] | 231 | bpf["events"].open_perf_buffer(print_event, page_cnt=64) |
Sasha Goldshtein | dd37d63 | 2017-02-09 05:44:43 -0500 | [diff] [blame] | 232 | while True: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame] | 233 | bpf.perf_buffer_poll() |