Skip to content

Commit 4b24f6b

Browse files
committed
Merge bitcoin/bitcoin#23302: tracing: drop GetHash().ToString() argument from the validation:block_connected tracepoint
53c9fa9 tracing: drop block_connected hash.toString() arg (0xb10c) Pull request description: The tracepoint `validation:block_connected` was introduced in #22006. The first argument was the hash of the connected block as a pointer to a C-like String. The last argument passed the hash of the connected block as a pointer to 32 bytes. The hash was only passed as string to allow `bpftrace` scripts to print the hash. It was (incorrectly) assumed that `bpftrace` cannot hex-format and print the block hash given only the hash as bytes. The block hash can be printed in `bpftrace` by calling `printf("%02x")` for each byte of the hash in an `unroll () {...}`. By starting from the last byte of the hash, it can be printed in big-endian (the block-explorer format). ```C $p = $hash + 31; unroll(32) { $b = *(uint8*)$p; printf("%02x", $b); $p -= 1; } ``` See also: #22902 (comment) This is a breaking change to the block_connected tracepoint API, however this tracepoint has not yet been included in a release. ACKs for top commit: laanwj: Concept and code review ACK 53c9fa9 jb55: ACK 53c9fa9 Tree-SHA512: f1b9e4e0ee45aae892e8bf38e04b5ee5fbc643d6e7e27d011b829ed8701dacf966a99b7c877c46cca8666b894a375633e62582c552c8203614c6f2b9c4087585
2 parents 927c2c4 + 53c9fa9 commit 4b24f6b

File tree

4 files changed

+19
-26
lines changed

4 files changed

+19
-26
lines changed

contrib/tracing/README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,12 @@ third acts as a duration threshold in milliseconds. When the `ConnectBlock()`
176176
function takes longer than the threshold, information about the block, is
177177
printed. For more details, see the header comment in the script.
178178

179-
By default, `bpftrace` limits strings to 64 bytes due to the limited stack size
180-
in the kernel VM. Block hashes as zero-terminated hex strings are 65 bytes which
181-
exceed the string limit. The string size limit can be set to 65 bytes with the
182-
environment variable `BPFTRACE_STRLEN`.
183-
184179
The following command can be used to benchmark, for example, `ConnectBlock()`
185180
between height 20000 and 38000 on SigNet while logging all blocks that take
186181
longer than 25ms to connect.
187182

188183
```
189-
$ BPFTRACE_STRLEN=65 bpftrace contrib/tracing/connectblock_benchmark.bt 20000 38000 25
184+
$ bpftrace contrib/tracing/connectblock_benchmark.bt 20000 38000 25
190185
```
191186

192187
In a different terminal, starting Bitcoin Core in SigNet mode and with

contrib/tracing/connectblock_benchmark.bt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44

55
USAGE:
66

7-
BPFTRACE_STRLEN=65 bpftrace contrib/tracing/connectblock_benchmark.bt <start height> <end height> <logging threshold in ms>
7+
bpftrace contrib/tracing/connectblock_benchmark.bt <start height> <end height> <logging threshold in ms>
88

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.
129
- <start height> sets the height at which the benchmark should start. Setting
1310
the start height to 0 starts the benchmark immediately, even before the
1411
first block is connected.
@@ -23,15 +20,15 @@
2320

2421
EXAMPLES:
2522

26-
BPFTRACE_STRLEN=65 bpftrace contrib/tracing/connectblock_benchmark.bt 300000 680000 1000
23+
bpftrace contrib/tracing/connectblock_benchmark.bt 300000 680000 1000
2724

2825
When run together 'bitcoind -reindex', this benchmarks the time it takes to
2926
connect the blocks between height 300.000 and 680.000 (inclusive) and prints
3027
details about all blocks that take longer than 1000ms to connect. Prints a
3128
histogram with block connection times when the benchmark is finished.
3229

3330

34-
BPFTRACE_STRLEN=65 bpftrace contrib/tracing/connectblock_benchmark.bt 0 0 500
31+
bpftrace contrib/tracing/connectblock_benchmark.bt 0 0 500
3532

3633
When running together 'bitcoind', all newly connected blocks that
3734
take longer than 500ms to connect are logged. A histogram with block
@@ -107,14 +104,23 @@ usdt:./src/bitcoind:validation:block_connected /arg1 >= $1 && (arg1 <= $2 || $2
107104
*/
108105
usdt:./src/bitcoind:validation:block_connected / (uint64) arg5 / 1000> $3 /
109106
{
110-
$hash_str = str(arg0);
107+
$hash = arg0;
111108
$height = (int32) arg1;
112109
$transactions = (uint64) arg2;
113110
$inputs = (int32) arg3;
114111
$sigops = (int64) arg4;
115112
$duration = (int64) arg5;
116113

117-
printf("Block %d (%s) %4d tx %5d ins %5d sigops took %4d ms\n", $height, $hash_str, $transactions, $inputs, $sigops, (uint64) $duration / 1000);
114+
115+
printf("Block %d (", $height);
116+
/* Prints each byte of the block hash as hex in big-endian (the block-explorer format) */
117+
$p = $hash + 31;
118+
unroll(32) {
119+
$b = *(uint8*)$p;
120+
printf("%02x", $b);
121+
$p -= 1;
122+
}
123+
printf(") %4d tx %5d ins %5d sigops took %4d ms\n", $transactions, $inputs, $sigops, (uint64) $duration / 1000);
118124
}
119125

120126

doc/tracing.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,12 @@ Is called *after* a block is connected to the chain. Can, for example, be used
101101
to benchmark block connections together with `-reindex`.
102102

103103
Arguments passed:
104-
1. Block Header Hash as `pointer to C-style String` (64 characters)
104+
1. Block Header Hash as `pointer to unsigned chars` (i.e. 32 bytes in little-endian)
105105
2. Block Height as `int32`
106106
3. Transactions in the Block as `uint64`
107107
4. Inputs spend in the Block as `int32`
108108
5. SigOps in the Block (excluding coinbase SigOps) `uint64`
109109
6. Time it took to connect the Block in microseconds (µs) as `uint64`
110-
7. Block Header Hash as `pointer to unsigned chars` (i.e. 32 bytes in little-endian)
111-
112-
Note: The 7th argument can't be accessed by bpftrace and is purposefully chosen
113-
to be the block header hash as bytes. See [bpftrace argument limit] for more
114-
details.
115-
116-
[bpftrace argument limit]: #bpftrace-argument-limit
117110

118111
## Adding tracepoints to Bitcoin Core
119112

src/validation.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,14 +1877,13 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
18771877
int64_t nTime5 = GetTimeMicros(); nTimeIndex += nTime5 - nTime4;
18781878
LogPrint(BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime5 - nTime4), nTimeIndex * MICRO, nTimeIndex * MILLI / nBlocksTotal);
18791879

1880-
TRACE7(validation, block_connected,
1881-
block.GetHash().ToString().c_str(),
1880+
TRACE6(validation, block_connected,
1881+
block.GetHash().data(),
18821882
pindex->nHeight,
18831883
block.vtx.size(),
18841884
nInputs,
18851885
nSigOpsCost,
1886-
GetTimeMicros() - nTimeStart, // in microseconds (µs)
1887-
block.GetHash().data()
1886+
GetTimeMicros() - nTimeStart // in microseconds (µs)
18881887
);
18891888

18901889
return true;

0 commit comments

Comments
 (0)