1
1
# Profiling Tools
2
2
3
3
This document describes several techniques to profile (i.e. find performance bottlenecks) the stacks-node mining loop, including:
4
+
4
5
- configuring debug logging,
5
6
- setting up a mock mining node,
6
7
- recording inbound transactions,
@@ -15,7 +16,7 @@ Note that all bash commands in this document are run from the stacks-blockchain
15
16
Validating the config file using ` stacks-node check-config ` :
16
17
17
18
```
18
- $ cargo run -r -p stacks-node --bin stacks-node check-config --config= testnet/stacks-node/conf/mainnet-mockminer-conf.toml
19
+ $ cargo run -r -p stacks-node --bin stacks-node check-config --config testnet/stacks-node/conf/mainnet-mockminer-conf.toml
19
20
INFO [1661276562.220137] [testnet/stacks-node/src/main.rs:82] [main] stacks-node 0.1.0 (tip-mine:c90476aa8a+, release build, macos [aarch64])
20
21
INFO [1661276562.220363] [testnet/stacks-node/src/main.rs:115] [main] Loading config at path testnet/stacks-node/conf/mainnet-mockminer-conf.toml
21
22
INFO [1661276562.233071] [testnet/stacks-node/src/main.rs:128] [main] Valid config!
@@ -24,7 +25,7 @@ INFO [1661276562.233071] [testnet/stacks-node/src/main.rs:128] [main] Valid conf
24
25
Enabling debug logging using environment variable ` STACKS_LOG_DEBUG=1 ` :
25
26
26
27
```
27
- $ STACKS_LOG_DEBUG=1 cargo run -r -p stacks-node --bin stacks-node check-config --config= testnet/stacks-node/conf/mainnet-mockminer-conf.toml
28
+ $ STACKS_LOG_DEBUG=1 cargo run -r -p stacks-node --bin stacks-node check-config --config testnet/stacks-node/conf/mainnet-mockminer-conf.toml
28
29
INFO [1661276562.220137] [testnet/stacks-node/src/main.rs:82] [main] stacks-node 0.1.0 (tip-mine:c90476aa8a+, release build, macos [aarch64])
29
30
INFO [1661276562.220363] [testnet/stacks-node/src/main.rs:115] [main] Loading config at path testnet/stacks-node/conf/mainnet-mockminer-conf.toml
30
31
DEBG [1661276562.222450] [testnet/stacks-node/src/main.rs:118] [main] Loaded config file: ConfigFile { burnchain: Some(BurnchainConfigFile { chain: Some("bitcoin"), burn_fee_cap: Some(1), mode: Some("mainnet"), commit_anchor_block_within: None, peer_host: Some("bitcoind.stacks.co"), peer_port: Some(8333), rpc_port: Some(8332), rpc_ssl: None, username: Some("blockstack"), password: Some("blockstacksystem"), timeout: None, magic_bytes: None, local_mining_public_key: None, process_exit_at_block_height: None, poll_time_secs: None, satoshis_per_byte: None, leader_key_tx_estimated_size: None, block_commit_tx_estimated_size: None, rbf_fee_increment: None, max_rbf: None, epochs: None }), node: Some(NodeConfigFile { name: None, seed: None, deny_nodes: None, working_dir: Some("/Users/igor/w/stacks-work/working_dir"), rpc_bind: Some("0.0.0.0:20443"), p2p_bind: Some("0.0.0.0:20444"), p2p_address: None, data_url: None, bootstrap_node: Some("02196f005965cebe6ddc3901b7b1cc1aa7a88f305bb8c5893456b8f9a605923893@seed.mainnet.hiro.so:20444"), local_peer_seed: None, miner: Some(true), mock_mining: Some(true), mine_microblocks: None, microblock_frequency: None, max_microblocks: None, wait_time_for_microblocks: None, prometheus_bind: None, marf_cache_strategy: None, marf_defer_hashing: None, pox_sync_sample_secs: None, use_test_genesis_chainstate: None }), ustx_balance: None, events_observer: Some([EventObserverConfigFile { endpoint: "localhost:3700", events_keys: ["*"] }]), connection_options: None, fee_estimation: None, miner: None }
@@ -34,7 +35,7 @@ INFO [1661276562.233071] [testnet/stacks-node/src/main.rs:128] [main] Valid conf
34
35
Enabling json logging using environment variable ` STACKS_LOG_JSON=1 ` and feature flag ` slog_json ` :
35
36
36
37
```
37
- $ STACKS_LOG_JSON=1 cargo run -F slog_json -r -p stacks-node --bin stacks-node check-config --config= testnet/stacks-node/conf/mainnet-mockminer-conf.toml
38
+ $ STACKS_LOG_JSON=1 cargo run -F slog_json -r -p stacks-node --bin stacks-node check-config --config testnet/stacks-node/conf/mainnet-mockminer-conf.toml
38
39
{"msg":"stacks-node 0.1.0 (tip-mine:c90476aa8a+, release build, macos [aarch64])","level":"INFO","ts":"2022-08-23T12:44:28.072462-05:00","thread":"main","line":82,"file":"testnet/stacks-node/src/main.rs"}
39
40
{"msg":"Loading config at path testnet/stacks-node/conf/mainnet-mockminer-conf.toml","level":"INFO","ts":"2022-08-23T12:44:28.074238-05:00","thread":"main","line":115,"file":"testnet/stacks-node/src/main.rs"}
40
41
{"msg":"Valid config!","level":"INFO","ts":"2022-08-23T12:44:28.089960-05:00","thread":"main","line":128,"file":"testnet/stacks-node/src/main.rs"}
@@ -53,6 +54,7 @@ $ export STACKS_SNAPSHOT_DIR=$STACKS_DIR/snapshot
53
54
## Setting up the mock mining node
54
55
55
56
Download and extract an archived snapshot of mainnet working directory, provided by Hiro.
57
+
56
58
```
57
59
$ wget -P $STACKS_DIR https://storage.googleapis.com/blockstack-publish/archiver-main/follower/mainnet-follower-latest.tar.gz
58
60
$ tar xzvf $STACKS_DIR/mainnet-follower-latest.tar.gz -C $STACKS_DIR
@@ -65,17 +67,19 @@ We'll be using the `stacks-node` config file available at:
65
67
Note that, for convenience, the ` stacks-node ` binary uses the environment variable ` $STACKS_WORKING_DIR ` to override the working directory location in the config file.
66
68
67
69
```
68
- $ cargo run -r -p stacks-node --bin stacks-node start --config= testnet/stacks-node/conf/mocknet-miner-conf.toml
70
+ $ cargo run -r -p stacks-node --bin stacks-node start --config testnet/stacks-node/conf/mocknet-miner-conf.toml
69
71
```
70
72
71
73
The ` stacks-node ` process will receive blocks starting from the latest block available in the Hiro archive.
72
74
73
75
Check the latest tip height of our node.
76
+
74
77
```
75
78
$ curl -s 127.0.0.1:20443/v2/info | jq .stacks_tip_height
76
79
```
77
80
78
81
Compare our node's tip height to a public node's tip height to check when our node is fully synchronized.
82
+
79
83
```
80
84
$ curl -s seed-0.mainnet.stacks.co:20443/v2/info | jq .stacks_tip_height
81
85
```
@@ -85,16 +89,19 @@ Once the node is synchronized, terminate the `stacks-node` process so we can set
85
89
## Recording blockchain events
86
90
87
91
Run ` stacks-events ` to receive and archive events:
92
+
88
93
```
89
94
$ cargo run -r -p stacks-node --bin stacks-events | tee $STACKS_DIR/events.log
90
95
```
91
96
92
97
Run ` stacks-node ` with an event observer:
98
+
93
99
```
94
- $ STACKS_EVENT_OBSERVER=localhost:3700 cargo run -r -p stacks-node --bin stacks-node start --config= testnet/stacks-node/conf/mocknet-miner-conf.toml
100
+ $ STACKS_EVENT_OBSERVER=localhost:3700 cargo run -r -p stacks-node --bin stacks-node start --config testnet/stacks-node/conf/mocknet-miner-conf.toml
95
101
```
96
102
97
103
You should see output from ` stacks-events ` in ` events.logs ` similar to:
104
+
98
105
```
99
106
$ tail -F $STACKS_DIR/events.log
100
107
{"path":"drop_mempool_tx","payload":{"dropped_txids":["0x6f78047f15ac3309153fc34be94ed8895111304336aec1ff106b7de051021e17, ..., "ts":"2022-08-12T05:03:08.577Z"}
@@ -103,21 +110,25 @@ $ tail -F $STACKS_DIR/events.log
103
110
## Historical Mining
104
111
105
112
Discover the first recorded block height:
113
+
106
114
```
107
115
$ cat $STACKS_DIR/events.log | egrep new_block | head -1 | jq .payload.block_height
108
116
```
109
117
110
118
Discover a lower bound number of recorded transactions. This is a lower bound because each line in the events file is a list of transactions.
119
+
111
120
```
112
121
$ cat $STACKS_DIR/events.log | egrep new_mempool_tx | wc -l
113
122
```
114
123
115
124
Make a snapshot of the working directory:
125
+
116
126
```
117
127
$ cp -r $STACKS_WORKING_DIR $STACKS_SNAPSHOT_DIR
118
128
```
119
129
120
130
Run the ` tip-mine ` benchmark:
131
+
121
132
```
122
133
$ export STACKS_TIP_MINE_BLOCK_HEIGHT=71294
123
134
$ export STACKS_TIP_MINE_NUM_TXS=100
@@ -137,9 +148,11 @@ INFO [1661274285.417171] [src/chainstate/stacks/miner.rs:1628] [main] Miner: min
137
148
Successfully mined block @ height = 71295 off of bd4fa09ece02e7fd53493c96bd69b89155058f7b28d4a659d87d89644208f41e (96cc06519e670eefb674aa2e9cfe0cfae103d4da/f0f0caa2afaae75417f14fe2fad1e3fd52b0169e66cb045b4954b9ab78611f31) in 7310ms.
138
149
Block 4a64e0a4012acb6748a08784876c23f6f61aba08b7c826db5b57832935278f33: 3227082 uSTX, 31587 bytes, cost ExecutionCost { write_length: 84090, write_count: 1170, read_length: 20381499, read_count: 7529, runtime: 103717315 }
139
150
```
151
+
140
152
In this run, ` tip-mine ` mined a block with 87 transactions.
141
153
142
154
Alternatively, you can run ` cargo build ` separately from the target binary ` stacks-inspect ` to avoid re-building and speed up profiling:
155
+
143
156
```
144
157
$ cargo build -F disable-costs -r --bin stacks-inspect
145
158
$ ./target/release/stacks-inspect tip-mine $STACKS_SNAPSHOT_DIR $STACKS_DIR/events.log $STACKS_TIP_MINE_BLOCK_HEIGHT $STACKS_TIP_MINE_NUM_TXS
@@ -158,16 +171,19 @@ $ cargo install flamegraph
158
171
flamegraph-rs uses [ dtrace] ( https://en.wikipedia.org/wiki/DTrace ) for profiling on Mac.
159
172
160
173
Build ` stacks-inspect ` using the feature ` disable-costs ` to disable the block cost limits:
174
+
161
175
```
162
176
$ cargo build -F disable-costs -r --bin stacks-inspect
163
177
```
164
178
165
179
Generate a flame graph:
180
+
166
181
```
167
182
$ flamegraph --root -o perf.svg -e cpu-clock --min-width 1 --deterministic -- ./target/release/stacks-inspect tip-mine $STACKS_SNAPSHOT_DIR $STACKS_DIR/events.log $STACKS_TIP_MINE_BLOCK_HEIGHT $STACKS_TIP_MINE_NUM_TXS
168
183
```
169
184
170
185
You can open the flame graph using a browser:
186
+
171
187
```
172
188
$ open perf.svg
173
189
```
@@ -183,40 +199,47 @@ The Linux performance tool `perf` has a performance bug which has been fixed. If
183
199
Background on the ` perf ` performance bug: https://eighty-twenty.org/2021/09/09/perf-addr2line-speed-improvement
184
200
185
201
Find out your kernel version:
202
+
186
203
```
187
204
$ uname -a
188
205
Linux localhost 5.15.0-25-generic #26~16.04.1-Ubuntu SMP Tue Oct 1 16:30:39 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
189
206
```
190
207
191
208
Install dependencies, clone the linux kernel source, checkout the version tag matching your kernel version and build perf:
209
+
192
210
```
193
- $ sudo apt install -y git libzstd-dev libunwind-dev libcap-dev libdw-dev libdwarf-dev libbfd-dev libelf-dev systemtap-sdt-dev binutils-dev libnuma-dev libiberty-dev bison flex
211
+ $ sudo apt install -y git libzstd-dev libunwind-dev libcap-dev libdw-dev libdwarf-dev libbfd-dev libelf-dev systemtap-sdt-dev binutils-dev libnuma-dev libiberty-dev bison flex
194
212
$ git clone https://github.com/torvalds/linux.git
195
213
$ git checkout v5.15
196
214
$ cd linux/tools/perf && make
197
215
```
198
216
199
217
#### Running perf
218
+
200
219
Grant kernel permissions to perf:
220
+
201
221
```
202
222
$ sudo sed -i "$ a kernel.perf_event_paranoid = -1" /etc/sysctl.conf
203
223
$ sudo sed -i "$ a kernel.kptr_restrict = 0" /etc/sysctl.conf
204
224
$ sysctl --system
205
225
```
206
226
207
227
Note that you need to uncomment the following in ` .cargo/config ` (see [ flamegraph-rs] ( https://github.com/flamegraph-rs/flamegraph ) for details)
228
+
208
229
```
209
230
[target.x86_64-unknown-linux-gnu]
210
231
linker = "/usr/bin/clang"
211
232
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Clink-arg=-Wl,--no-rosegment"]
212
233
```
213
234
214
235
Build ` stacks-inspect ` using the feature ` disable-costs ` to disable the block cost limits:
236
+
215
237
```
216
238
$ cargo build -F disable-costs -r --bin stacks-inspect
217
239
```
218
240
219
241
Generate a flame graph using the locally built ` perf ` binary:
242
+
220
243
```
221
244
$ PERF=~/linux/tools/perf/perf flamegraph --cmd "record -F 97 --call-graph dwarf,65528 -g -e cpu-clock" -o perf.svg --min-width 0.5 --deterministic -- ./target/release/stacks-inspect tip-mine $STACKS_SNAPSHOT_DIR $STACKS_DIR/events.log $STACKS_TIP_MINE_BLOCK_HEIGHT $STACKS_TIP_MINE_NUM_TXS
222
245
```
@@ -225,11 +248,11 @@ Output flame graph is in `perf.svg`.
225
248
226
249
## Profiling SQLite queries
227
250
228
- Set the environment variable ` STACKS_LOG_DEBUG=1 ` and use the cargo feature ` profile-sqlite ` :
251
+ Set the environment variable ` STACKS_LOG_DEBUG=1 ` and use the cargo feature ` profile-sqlite ` :
229
252
230
253
```
231
254
$ STACKS_LOG_DEBUG=1 cargo run -F profile-sqlite,disable-costs -r --bin stacks-inspect try-mine $STACKS_WORKING_DIR
232
255
...
233
256
DEBG [1661217664.809057] [src/util_lib/db.rs:666] [main] sqlite trace profile {"millis":1,"query":"SELECT value FROM data_table WHERE key = ?"}
234
257
...
235
- ```
258
+ ```
0 commit comments