1Demonstrations of dbstat, the Linux eBPF/bcc version. 2 3 4dbstat traces queries performed by a MySQL or PostgreSQL database process, and 5displays a histogram of query latencies. For example: 6 7# dbstat mysql 8Tracing database queries for pids 25776 slower than 0 ms... 9 query latency (ms) : count distribution 10 0 -> 1 : 990 |****************************************| 11 2 -> 3 : 7 | | 12 4 -> 7 : 0 | | 13 8 -> 15 : 0 | | 14 16 -> 31 : 0 | | 15 32 -> 63 : 0 | | 16 64 -> 127 : 0 | | 17 128 -> 255 : 0 | | 18 256 -> 511 : 0 | | 19 512 -> 1023 : 0 | | 20 1024 -> 2047 : 2 | | 21^C 22 23It's immediately evident that the vast majority of queries finish very quickly, 24in under 1ms, but there are some super-slow queries occasionally, in the 1-2 25seconds bucket. 26 27We can filter out the shorter queries with the -m switch: 28 29# dbstat mysql -m 1000 30Tracing database queries for pids 25776 slower than 1000 ms... 31 query latency (ms) : count distribution 32 0 -> 1 : 0 | | 33 2 -> 3 : 0 | | 34 4 -> 7 : 0 | | 35 8 -> 15 : 0 | | 36 16 -> 31 : 0 | | 37 32 -> 63 : 0 | | 38 64 -> 127 : 0 | | 39 128 -> 255 : 0 | | 40 256 -> 511 : 0 | | 41 512 -> 1023 : 0 | | 42 1024 -> 2047 : 8 |****************************************| 43^C 44 45By default, dbstat will try to detect mysqld and postgres processes, but if 46necessary, you can specify the process ids with the -p switch. Here, the -i 47switch is also used to request histograms at 3 second intervals: 48 49# dbstat mysql -p $(pidof mysql) -i 3 50Tracing database queries for pids 25776 slower than 0 ms... 51[06:14:36] 52 query latency (ms) : count distribution 53 0 -> 1 : 758 |****************************************| 54 2 -> 3 : 1 | | 55 4 -> 7 : 0 | | 56 8 -> 15 : 0 | | 57 16 -> 31 : 0 | | 58 32 -> 63 : 0 | | 59 64 -> 127 : 0 | | 60 128 -> 255 : 0 | | 61 256 -> 511 : 0 | | 62 512 -> 1023 : 0 | | 63 1024 -> 2047 : 1 | | 64 65[06:14:39] 66 query latency (ms) : count distribution 67 0 -> 1 : 436 |****************************************| 68 2 -> 3 : 2 | | 69 4 -> 7 : 0 | | 70 8 -> 15 : 0 | | 71 16 -> 31 : 0 | | 72 32 -> 63 : 0 | | 73 64 -> 127 : 0 | | 74 128 -> 255 : 0 | | 75 256 -> 511 : 0 | | 76 512 -> 1023 : 0 | | 77 1024 -> 2047 : 1 | | 78 79[06:14:42] 80 query latency (ms) : count distribution 81 0 -> 1 : 399 |****************************************| 82 2 -> 3 : 0 | | 83 4 -> 7 : 0 | | 84 8 -> 15 : 0 | | 85 16 -> 31 : 0 | | 86 32 -> 63 : 0 | | 87 64 -> 127 : 0 | | 88 128 -> 255 : 0 | | 89 256 -> 511 : 0 | | 90 512 -> 1023 : 0 | | 91 1024 -> 2047 : 1 | | 92^C 93 94 95USAGE: 96# dbstat -h 97usage: dbstat.py [-h] [-v] [-p [PID [PID ...]]] [-m THRESHOLD] [-u] 98 [-i INTERVAL] 99 {mysql,postgres} 100 101positional arguments: 102 {mysql,postgres} the database engine to use 103 104optional arguments: 105 -h, --help show this help message and exit 106 -v, --verbose print the BPF program 107 -p [PID [PID ...]], --pid [PID [PID ...]] 108 the pid(s) to trace 109 -m THRESHOLD, --threshold THRESHOLD 110 trace queries slower than this threshold (ms) 111 -u, --microseconds display query latencies in microseconds (default: 112 milliseconds) 113 -i INTERVAL, --interval INTERVAL 114 print summary at this interval (seconds) 115 116 dbstat postgres # display a histogram of PostgreSQL query latencies 117 dbstat mysql -v # display MySQL latencies and print the BPF program 118 dbstat mysql -u # display query latencies in microseconds (default: ms) 119 dbstat mysql -m 5 # trace only queries slower than 5ms 120 dbstat mysql -p 408 # trace queries in a specific process 121