Skip to content

Commit 799968e

Browse files
0xB10Carnabsen1729
authored andcommitted
tracing: misc follow-ups to 22902
- mention 'Lost X events' workaround - clarify flush tracepoint docs - fix typo in tracepoint context - clarify flush for prune The documentation and examples for the `fFlushForPrune` argument of the utxocache flush tracepoint weren't clear without looking at the code. See these comments: bitcoin/bitcoin#22902 (comment) - doc: note that there can be temporary UTXO caches Bitcoin Core uses temporary clones of it's _main_ UTXO cache in some places. The utxocache:add and :spent tracepoints are triggered when temporary caches are changed too. This is documented.
1 parent 36a6584 commit 799968e

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

contrib/tracing/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,13 @@ uncached from the UTXO set. Based on the `utxocache:add`, `utxocache:spend` and
262262
$ bpftrace contrib/tracing/log_utxos.bt
263263
```
264264

265-
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
266272

267273
```bash
268274
Attaching 4 probes...

contrib/tracing/log_utxocache_flush.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
# a sandboxed Linux kernel VM.
1414
program = """
1515
# include <uapi/linux/ptrace.h>
16+
1617
struct data_t
1718
{
1819
u64 duration;
1920
u32 mode;
2021
u64 coins_count;
2122
u64 coins_mem_usage;
22-
bool is_flush_prune;
23+
bool is_flush_for_prune;
2324
};
2425
2526
// BPF perf buffer to push the data to user space.
@@ -31,7 +32,7 @@
3132
bpf_usdt_readarg(2, ctx, &data.mode);
3233
bpf_usdt_readarg(3, ctx, &data.coins_count);
3334
bpf_usdt_readarg(4, ctx, &data.coins_mem_usage);
34-
bpf_usdt_readarg(5, ctx, &data.is_flush_prune);
35+
bpf_usdt_readarg(5, ctx, &data.is_flush_for_prune);
3536
flush.perf_submit(ctx, &data, sizeof(data));
3637
return 0;
3738
}
@@ -52,7 +53,7 @@ class Data(ctypes.Structure):
5253
("mode", ctypes.c_uint32),
5354
("coins_count", ctypes.c_uint64),
5455
("coins_mem_usage", ctypes.c_uint64),
55-
("is_flush_prune", ctypes.c_bool),
56+
("is_flush_for_prune", ctypes.c_bool)
5657
]
5758

5859

@@ -62,7 +63,7 @@ def print_event(event):
6263
FLUSH_MODES[event.mode],
6364
event.coins_count,
6465
"%.2f kB" % (event.coins_mem_usage/1000),
65-
event.is_flush_prune,
66+
event.is_flush_for_prune
6667
))
6768

6869

@@ -85,7 +86,7 @@ def handle_flush(_, data, size):
8586
print("Logging utxocache flushes. Ctrl-C to end...")
8687
print("%-15s %-10s %-15s %-15s %-8s" % ("Duration (µs)", "Mode",
8788
"Coins Count", "Memory Usage",
88-
"Prune"))
89+
"Flush for Prune"))
8990

9091
while True:
9192
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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +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 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`
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`
125134

126135
#### Tracepoint `utxocache:add`
127136

128-
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.
129138

130139
Arguments passed:
131140
1. Transaction ID (hash) as `pointer to unsigned chars` (i.e. 32 bytes in little-endian)
@@ -136,7 +145,7 @@ Arguments passed:
136145

137146
#### Tracepoint `utxocache:spent`
138147

139-
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.
140149

141150
Arguments passed:
142151
1. Transaction ID (hash) as `pointer to unsigned chars` (i.e. 32 bytes in little-endian)
@@ -147,14 +156,17 @@ Arguments passed:
147156

148157
#### Tracepoint `utxocache:uncache`
149158

150-
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.
151163

152164
Arguments passed:
153165
1. Transaction ID (hash) as `pointer to unsigned chars` (i.e. 32 bytes in little-endian)
154166
2. Output index as `uint32`
155167
3. Block height the coin was uncached, as `uint32`
156168
4. Value of the coin as `int64`
157-
. If the coin is a coinbase as `bool`
169+
5. If the coin is a coinbase as `bool`
158170

159171
## Adding tracepoints to Bitcoin Core
160172

0 commit comments

Comments
 (0)