Skip to content

Commit 2b0735d

Browse files
committed
Merge bitcoin/bitcoin#23907: tracing: utxocache tracepoints follow up for #22902
799968e tracing: misc follow-ups to 22902 (0xb10c) 36a6584 tracing: correctly scope utxocache:flush tracepoint (Arnab Sen) Pull request description: This PR is a follow-up to the [#22902](bitcoin/bitcoin#22902). Previously, the tracepoint `utxocache:flush` was called, even when it was not flushing. So, the tracepoint is now scoped to write only when coins cache to disk. ACKs for top commit: 0xB10C: ACK 799968e Tree-SHA512: ebb096cbf991c551c81e4339821f10d9768c14cf3d8cb14d0ad851acff5980962228a1c746914c6aba3bdb27e8be53b33349c41efe8bab5542f639916e437b5f
2 parents 98b9d60 + 799968e commit 2b0735d

File tree

5 files changed

+50
-50
lines changed

5 files changed

+50
-50
lines changed

contrib/tracing/README.md

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ Histogram of block connection times in milliseconds (ms).
237237

238238
### log_utxocache_flush.py
239239

240-
A BCC Python script to log the cache and index flushes. Based on the
240+
A BCC Python script to log the UTXO cache flushes. Based on the
241241
`utxocache:flush` tracepoint.
242242

243243
```bash
@@ -246,23 +246,10 @@ $ python3 contrib/tracing/log_utxocache_flush.py ./src/bitcoind
246246

247247
```
248248
Logging utxocache flushes. Ctrl-C to end...
249-
Duration (µs) Mode Coins Count Memory Usage Prune Full Flush
250-
0 PERIODIC 28484 3929.87 kB False False
251-
1 PERIODIC 28485 3930.00 kB False False
252-
0 PERIODIC 28489 3930.51 kB False False
253-
1 PERIODIC 28490 3930.64 kB False False
254-
0 PERIODIC 28491 3930.77 kB False False
255-
0 PERIODIC 28491 3930.77 kB False False
256-
0 PERIODIC 28496 3931.41 kB False False
257-
1 PERIODIC 28496 3931.41 kB False False
258-
0 PERIODIC 28497 3931.54 kB False False
259-
1 PERIODIC 28497 3931.54 kB False False
260-
1 PERIODIC 28499 3931.79 kB False False
261-
.
262-
.
263-
.
264-
53788 ALWAYS 30076 4136.27 kB False False
265-
7463 ALWAYS 0 245.84 kB False False
249+
Duration (µs) Mode Coins Count Memory Usage Prune
250+
730451 IF_NEEDED 22990 3323.54 kB True
251+
637657 ALWAYS 122320 17124.80 kB False
252+
81349 ALWAYS 0 1383.49 kB False
266253
```
267254

268255
### log_utxos.bt
@@ -275,7 +262,13 @@ uncached from the UTXO set. Based on the `utxocache:add`, `utxocache:spend` and
275262
$ bpftrace contrib/tracing/log_utxos.bt
276263
```
277264

278-
It should produce an output similar to the following.
265+
This should produce an output similar to the following. If you see bpftrace
266+
warnings like `Lost 24 events`, the eBPF perf ring-buffer is filled faster
267+
than it is being read. You can increase the ring-buffer size by setting the
268+
ENV variable `BPFTRACE_PERF_RB_PAGES` (default 64) at a cost of higher
269+
memory usage. See the [bpftrace reference guide] for more information.
270+
271+
[bpftrace reference guide]: https://github.com/iovisor/bpftrace/blob/master/docs/reference_guide.md#98-bpftrace_perf_rb_pages
279272

280273
```bash
281274
Attaching 4 probes...

contrib/tracing/log_utxocache_flush.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
# a sandboxed Linux kernel VM.
1717
program = """
1818
# include <uapi/linux/ptrace.h>
19+
1920
struct data_t
2021
{
2122
u64 duration;
2223
u32 mode;
2324
u64 coins_count;
2425
u64 coins_mem_usage;
25-
bool is_flush_prune;
26-
bool is_full_flush;
26+
bool is_flush_for_prune;
2727
};
2828
2929
// BPF perf buffer to push the data to user space.
@@ -35,8 +35,7 @@
3535
bpf_usdt_readarg(2, ctx, &data.mode);
3636
bpf_usdt_readarg(3, ctx, &data.coins_count);
3737
bpf_usdt_readarg(4, ctx, &data.coins_mem_usage);
38-
bpf_usdt_readarg(5, ctx, &data.is_flush_prune);
39-
bpf_usdt_readarg(5, ctx, &data.is_full_flush);
38+
bpf_usdt_readarg(5, ctx, &data.is_flush_for_prune);
4039
flush.perf_submit(ctx, &data, sizeof(data));
4140
return 0;
4241
}
@@ -57,19 +56,17 @@ class Data(ctypes.Structure):
5756
("mode", ctypes.c_uint32),
5857
("coins_count", ctypes.c_uint64),
5958
("coins_mem_usage", ctypes.c_uint64),
60-
("is_flush_prune", ctypes.c_bool),
61-
("is_full_flush", ctypes.c_bool)
59+
("is_flush_for_prune", ctypes.c_bool)
6260
]
6361

6462

6563
def print_event(event):
66-
print("%-15d %-10s %-15d %-15s %-8s %-8s" % (
64+
print("%-15d %-10s %-15d %-15s %-8s" % (
6765
event.duration,
6866
FLUSH_MODES[event.mode],
6967
event.coins_count,
7068
"%.2f kB" % (event.coins_mem_usage/1000),
71-
event.is_flush_prune,
72-
event.is_full_flush
69+
event.is_flush_for_prune
7370
))
7471

7572

@@ -90,9 +87,9 @@ def handle_flush(_, data, size):
9087

9188
b["flush"].open_perf_buffer(handle_flush)
9289
print("Logging utxocache flushes. Ctrl-C to end...")
93-
print("%-15s %-10s %-15s %-15s %-8s %-8s" % ("Duration (µs)", "Mode",
94-
"Coins Count", "Memory Usage",
95-
"Prune", "Full Flush"))
90+
print("%-15s %-10s %-15s %-15s %-8s" % ("Duration (µs)", "Mode",
91+
"Coins Count", "Memory Usage",
92+
"Flush for Prune"))
9693

9794
while True:
9895
try:

contrib/tracing/log_utxos.bt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
bpftrace contrib/tracing/log_utxos.bt
88

99
This script requires a 'bitcoind' binary compiled with eBPF support and the
10-
'utxochache' tracepoints. By default, it's assumed that 'bitcoind' is
10+
'utxocache' tracepoints. By default, it's assumed that 'bitcoind' is
1111
located in './src/bitcoind'. This can be modified in the script below.
1212

1313
NOTE: requires bpftrace v0.12.0 or above.

doc/tracing.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,31 @@ Arguments passed:
110110

111111
### Context `utxocache`
112112

113+
The following tracepoints cover the in-memory UTXO cache. UTXOs are, for example,
114+
added to and removed (spent) from the cache when we connect a new block.
115+
**Note**: Bitcoin Core uses temporary clones of the _main_ UTXO cache
116+
(`chainstate.CoinsTip()`). For example, the RPCs `generateblock` and
117+
`getblocktemplate` call `TestBlockValidity()`, which applies the UTXO set
118+
changes to a temporary cache. Similarly, mempool consistency checks, which are
119+
frequent on regtest, also apply the the UTXO set changes to a temporary cache.
120+
Changes to the _main_ UTXO cache and to temporary caches trigger the tracepoints.
121+
We can't tell if a temporary cache or the _main_ cache was changed.
122+
113123
#### Tracepoint `utxocache:flush`
114124

115-
Is called *after* the caches and indexes are flushed depending on the mode
116-
`CChainState::FlushStateToDisk` is called with.
125+
Is called *after* the in-memory UTXO cache is flushed.
117126

118127
Arguments passed:
119-
1. Duration in microseconds as `int64`
128+
1. Time it took to flush the cache microseconds as `int64`
120129
2. Flush state mode as `uint32`. It's an enumerator class with values `0`
121130
(`NONE`), `1` (`IF_NEEDED`), `2` (`PERIODIC`), `3` (`ALWAYS`)
122-
3. Number of coins flushed as `uint64`
123-
4. Memory usage in bytes as `uint64`
124-
5. If the flush was pruned as `bool`
125-
6. If it was full flush as `bool`
131+
3. Cache size (number of coins) before the flush as `uint64`
132+
4. Cache memory usage in bytes as `uint64`
133+
5. If pruning caused the flush as `bool`
126134

127135
#### Tracepoint `utxocache:add`
128136

129-
It is called when a new coin is added to the UTXO cache.
137+
Is called when a coin is added to a UTXO cache. This can be a temporary UTXO cache too.
130138

131139
Arguments passed:
132140
1. Transaction ID (hash) as `pointer to unsigned chars` (i.e. 32 bytes in little-endian)
@@ -137,7 +145,7 @@ Arguments passed:
137145

138146
#### Tracepoint `utxocache:spent`
139147

140-
It is called when a coin is spent from the UTXO cache.
148+
Is called when a coin is spent from a UTXO cache. This can be a temporary UTXO cache too.
141149

142150
Arguments passed:
143151
1. Transaction ID (hash) as `pointer to unsigned chars` (i.e. 32 bytes in little-endian)
@@ -148,14 +156,17 @@ Arguments passed:
148156

149157
#### Tracepoint `utxocache:uncache`
150158

151-
It is called when the UTXO with the given outpoint is removed from the cache.
159+
Is called when a coin is purposefully unloaded from a UTXO cache. This
160+
happens, for example, when we load an UTXO into a cache when trying to accept
161+
a transaction that turns out to be invalid. The loaded UTXO is uncached to avoid
162+
filling our UTXO cache up with irrelevant UTXOs.
152163

153164
Arguments passed:
154165
1. Transaction ID (hash) as `pointer to unsigned chars` (i.e. 32 bytes in little-endian)
155166
2. Output index as `uint32`
156167
3. Block height the coin was uncached, as `uint32`
157168
4. Value of the coin as `int64`
158-
. If the coin is a coinbase as `bool`
169+
5. If the coin is a coinbase as `bool`
159170

160171
## Adding tracepoints to Bitcoin Core
161172

src/validation.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,14 +2373,13 @@ bool CChainState::FlushStateToDisk(
23732373
return AbortNode(state, "Failed to write to coin database");
23742374
nLastFlush = nNow;
23752375
full_flush_completed = true;
2376+
TRACE5(utxocache, flush,
2377+
(int64_t)(GetTimeMicros() - nNow.count()), // in microseconds (µs)
2378+
(u_int32_t)mode,
2379+
(u_int64_t)coins_count,
2380+
(u_int64_t)coins_mem_usage,
2381+
(bool)fFlushForPrune);
23762382
}
2377-
TRACE6(utxocache, flush,
2378-
(int64_t)(GetTimeMicros() - nNow.count()), // in microseconds (µs)
2379-
(u_int32_t)mode,
2380-
(u_int64_t)coins_count,
2381-
(u_int64_t)coins_mem_usage,
2382-
(bool)fFlushForPrune,
2383-
(bool)fDoFullFlush);
23842383
}
23852384
if (full_flush_completed) {
23862385
// Update best block in wallet (so we can detect restored wallets).

0 commit comments

Comments
 (0)