Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env bpftrace3/*5USAGE:7bpftrace contrib/tracing/connectblock_benchmark.bt <start height> <end height> <logging threshold in ms>9- <start height> sets the height at which the benchmark should start. Setting10the start height to 0 starts the benchmark immediately, even before the11first block is connected.12- <end height> sets the height after which the benchmark should end. Setting13the end height to 0 disables the benchmark. The script only logs blocks14over <logging threshold in ms>.15- Threshold <logging threshold in ms>17This script requires a 'bitcoind' binary compiled with eBPF support and the18'validation:block_connected' USDT. By default, it's assumed that 'bitcoind' is19located in './build/bin/bitcoind'. This can be modified in the script below.21EXAMPLES:23bpftrace contrib/tracing/connectblock_benchmark.bt 300000 680000 100025When run together 'bitcoind -reindex', this benchmarks the time it takes to26connect the blocks between height 300.000 and 680.000 (inclusive) and prints27details about all blocks that take longer than 1000ms to connect. Prints a28histogram with block connection times when the benchmark is finished.31bpftrace contrib/tracing/connectblock_benchmark.bt 0 0 50033When running together 'bitcoind', all newly connected blocks that34take longer than 500ms to connect are logged. A histogram with block35connection times is shown when the script is terminated.37*/39BEGIN40{41$start_height = $1;42$end_height = $2;43$logging_threshold_ms = $3;45if ($end_height < $start_height) {46printf("Error: start height (%d) larger than end height (%d)!\n", $start_height, $end_height);47exit();48}50if ($end_height > 0) {51printf("ConnectBlock benchmark between height %d and %d inclusive\n", $start_height, $end_height);52} else {53printf("ConnectBlock logging starting at height %d\n", $start_height);54}56if ($logging_threshold_ms > 0) {57printf("Logging blocks taking longer than %d ms to connect.\n", $3);58}60if ($start_height == 0) {61@start = nsecs;62}63}65/*66Attaches to the 'validation:block_connected' USDT and collects stats when the67connected block is between the start and end height (or the end height is68unset).69*/70usdt:./build/bin/bitcoind:validation:block_connected /arg1 >= $1 && (arg1 <= $2 || $2 == 0 )/71{72$height = arg1;73$transactions = arg2;74$inputs = arg3;75$sigops = arg4;76$duration = (uint64) arg5;78@height = $height;80@blocks = @blocks + 1;81@transactions = @transactions + $transactions;82@inputs = @inputs + $inputs;83@sigops = @sigops + $sigops;85@durations = hist($duration / 1e6);87if ($height == $1 && $height != 0) {88@start = nsecs;89printf("Starting Connect Block Benchmark between height %d and %d.\n", $1, $2);90}92if ($2 > 0 && $height >= $2) {93@end = nsecs;94$duration = @end - @start;95printf("\nTook %d ms to connect the blocks between height %d and %d.\n", $duration / 1e9, $1, $2);96exit();97}98}100/*101Attaches to the 'validation:block_connected' USDT and logs information about102blocks where the time it took to connect the block is above the103<logging threshold in ms>.104*/105usdt:./build/bin/bitcoind:validation:block_connected / (uint64) arg5 / 1e6 > $3 /106{107$hash = arg0;108$height = (int32) arg1;109$transactions = (uint64) arg2;110$inputs = (int32) arg3;111$sigops = (int64) arg4;112$duration = (int64) arg5;115printf("Block %d (", $height);116/* Prints each byte of the block hash as hex in big-endian (the block-explorer format) */117$p = $hash + 31;118unroll(32) {119$b = *(uint8*)$p;120printf("%02x", $b);121$p -= 1;122}123printf(") %4d tx %5d ins %5d sigops took %4d ms\n", $transactions, $inputs, $sigops, (uint64) $duration / 1e6);124}127/*128Prints stats about the blocks, transactions, inputs, and sigops processed in129the last second (if any).130*/131interval:s:1 {132if (@blocks > 0) {133printf("BENCH %4d blk/s %6d tx/s %7d inputs/s %8d sigops/s (height %d)\n", @blocks, @transactions, @inputs, @sigops, @height);135zero(@blocks);136zero(@transactions);137zero(@inputs);138zero(@sigops);139}140}142END143{144printf("\nHistogram of block connection times in milliseconds (ms).\n");145print(@durations);147clear(@durations);148clear(@blocks);149clear(@transactions);150clear(@inputs);151clear(@sigops);152clear(@height);153clear(@start);154clear(@end);155}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.