|
| 1 | +#!/usr/bin/env bpftrace |
| 2 | + |
| 3 | +/* |
| 4 | + |
| 5 | + USAGE: |
| 6 | + |
| 7 | + BPFTRACE_STRLEN=65 bpftrace contrib/tracing/connectblock_benchmark.bt <start height> <end height> <logging threshold in ms> |
| 8 | + |
| 9 | + - The environment variable BPFTRACE_STRLEN needs to be set to 65 chars as |
| 10 | + strings are limited to 64 chars by default. Hex strings with Bitcoin block |
| 11 | + hashes are 64 hex chars + 1 null-termination char. |
| 12 | + - <start height> sets the height at which the benchmark should start. Setting |
| 13 | + the start height to 0 starts the benchmark immediately, even before the |
| 14 | + first block is connected. |
| 15 | + - <end height> sets the height after which the benchmark should end. Setting |
| 16 | + the end height to 0 disables the benchmark. The script only logs blocks |
| 17 | + over <logging threshold in ms>. |
| 18 | + - Threshold <logging threshold in ms> |
| 19 | + |
| 20 | + This script requires a 'bitcoind' binary compiled with eBPF support and the |
| 21 | + 'validation:block_connected' USDT. By default, it's assumed that 'bitcoind' is |
| 22 | + located in './src/bitcoind'. This can be modified in the script below. |
| 23 | + |
| 24 | + EXAMPLES: |
| 25 | + |
| 26 | + BPFTRACE_STRLEN=65 bpftrace contrib/tracing/connectblock_benchmark.bt 300000 680000 1000 |
| 27 | + |
| 28 | + When run together 'bitcoind -reindex', this benchmarks the time it takes to |
| 29 | + connect the blocks between height 300.000 and 680.000 (inclusive) and prints |
| 30 | + details about all blocks that take longer than 1000ms to connect. Prints a |
| 31 | + histogram with block connection times when the benchmark is finished. |
| 32 | + |
| 33 | + |
| 34 | + BPFTRACE_STRLEN=65 bpftrace contrib/tracing/connectblock_benchmark.bt 0 0 500 |
| 35 | + |
| 36 | + When running together 'bitcoind', all newly connected blocks that |
| 37 | + take longer than 500ms to connect are logged. A histogram with block |
| 38 | + connection times is shown when the script is terminated. |
| 39 | + |
| 40 | +*/ |
| 41 | + |
| 42 | +BEGIN |
| 43 | +{ |
| 44 | + $start_height = $1; |
| 45 | + $end_height = $2; |
| 46 | + $logging_threshold_ms = $3; |
| 47 | + |
| 48 | + if ($end_height < $start_height) { |
| 49 | + printf("Error: start height (%d) larger than end height (%d)!\n", $start_height, $end_height); |
| 50 | + exit(); |
| 51 | + } |
| 52 | + |
| 53 | + if ($end_height > 0) { |
| 54 | + printf("ConnectBlock benchmark between height %d and %d inclusive\n", $start_height, $end_height); |
| 55 | + } else { |
| 56 | + printf("ConnectBlock logging starting at height %d\n", $start_height); |
| 57 | + } |
| 58 | + |
| 59 | + if ($logging_threshold_ms > 0) { |
| 60 | + printf("Logging blocks taking longer than %d ms to connect.\n", $3); |
| 61 | + } |
| 62 | + |
| 63 | + if ($start_height == 0) { |
| 64 | + @start = nsecs; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/* |
| 69 | + Attaches to the 'validation:block_connected' USDT and collects stats when the |
| 70 | + connected block is between the start and end height (or the end height is |
| 71 | + unset). |
| 72 | +*/ |
| 73 | +usdt:./src/bitcoind:validation:block_connected /arg1 >= $1 && (arg1 <= $2 || $2 == 0 )/ |
| 74 | +{ |
| 75 | + $height = arg1; |
| 76 | + $transactions = arg2; |
| 77 | + $inputs = arg3; |
| 78 | + $sigops = arg4; |
| 79 | + $duration = (uint64) arg5; |
| 80 | + |
| 81 | + @height = $height; |
| 82 | + |
| 83 | + @blocks = @blocks + 1; |
| 84 | + @transactions = @transactions + $transactions; |
| 85 | + @inputs = @inputs + $inputs; |
| 86 | + @sigops = @sigops + $sigops; |
| 87 | + |
| 88 | + @durations = hist($duration / 1000); |
| 89 | + |
| 90 | + if ($height == $1 && $height != 0) { |
| 91 | + @start = nsecs; |
| 92 | + printf("Starting Connect Block Benchmark between height %d and %d.\n", $1, $2); |
| 93 | + } |
| 94 | + |
| 95 | + if ($2 > 0 && $height >= $2) { |
| 96 | + @end = nsecs; |
| 97 | + $duration = @end - @start; |
| 98 | + printf("\nTook %d ms to connect the blocks between height %d and %d.\n", $duration / 1000000, $1, $2); |
| 99 | + exit(); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/* |
| 104 | + Attaches to the 'validation:block_connected' USDT and logs information about |
| 105 | + blocks where the time it took to connect the block is above the |
| 106 | + <logging threshold in ms>. |
| 107 | +*/ |
| 108 | +usdt:./src/bitcoind:validation:block_connected / (uint64) arg5 / 1000> $3 / |
| 109 | +{ |
| 110 | + $hash_str = str(arg0); |
| 111 | + $height = (int32) arg1; |
| 112 | + $transactions = (uint64) arg2; |
| 113 | + $inputs = (int32) arg3; |
| 114 | + $sigops = (int64) arg4; |
| 115 | + $duration = (int64) arg5; |
| 116 | + |
| 117 | + printf("Block %d (%s) %4d tx %5d ins %5d sigops took %4d ms\n", $height, $hash_str, $transactions, $inputs, $sigops, (uint64) $duration / 1000); |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +/* |
| 122 | + Prints stats about the blocks, transactions, inputs, and sigops processed in |
| 123 | + the last second (if any). |
| 124 | +*/ |
| 125 | +interval:s:1 { |
| 126 | + if (@blocks > 0) { |
| 127 | + printf("BENCH %4d blk/s %6d tx/s %7d inputs/s %8d sigops/s (height %d)\n", @blocks, @transactions, @inputs, @sigops, @height); |
| 128 | + |
| 129 | + zero(@blocks); |
| 130 | + zero(@transactions); |
| 131 | + zero(@inputs); |
| 132 | + zero(@sigops); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +END |
| 137 | +{ |
| 138 | + printf("\nHistogram of block connection times in milliseconds (ms).\n"); |
| 139 | + print(@durations); |
| 140 | + |
| 141 | + clear(@durations); |
| 142 | + clear(@blocks); |
| 143 | + clear(@transactions); |
| 144 | + clear(@inputs); |
| 145 | + clear(@sigops); |
| 146 | + clear(@height); |
| 147 | + clear(@start); |
| 148 | + clear(@end); |
| 149 | +} |
| 150 | + |
0 commit comments