Skip to content

Commit 582a7c5

Browse files
authored
contribute-nearcore: add info on ROCKSDB_LIB_DIR and other minor updates (#1076)
Most notably, add a section about compiling while using rocksdb library provided by the system. Based on my observation, this helps with OOMs. I think (though I haven’t tested it) that rust-rocksdb builds rocksdb library with LTO enabled which consumes bunch of memory. While at it, make some minor fixes to the document. Change ‘bash’ preformatted blocks to ‘console’ where the latter is more appropriate and remove mention of `start_localnet.py` script which no longer exists. Also add `env` to replace `time VAR=foo command` since the latter only works with some shells which provide `time` built-in. Using `env` to set the environment variables makes it more portable.
1 parent 0c5f073 commit 582a7c5

File tree

1 file changed

+71
-33
lines changed

1 file changed

+71
-33
lines changed

docs/community/contribute/nearcore.md

+71-33
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ cd nearcore
3838
Navigate to the root of the repository, and run:
3939

4040
```bash
41-
cargo run --package neard --bin neard -- init
42-
cargo run --package neard --bin neard -- run
41+
cargo run -p neard -- init
42+
cargo run -p neard -- run
4343
```
4444

4545
This will setup a local chain with `init` and will run the node.
@@ -63,10 +63,11 @@ If you are using CLion IDE you can configure it to run rustfmt automatically eve
6363

6464
## Testing {#testing}
6565

66-
To run NEARCore node in the testing mode, for example to test it or for development of `near-api-js` or `near-cli` you can use scripts that sets up special tests-only local testnet:
66+
To run NEARCore node in the testing mode, for example to test it or for development of `near-api-js` or `near-cli` you can use commands that sets up special tests-only local testnet:
6767

6868
```bash
69-
./scripts/start_localnet.py
69+
cargo run -p neard -- localnet -v1
70+
cargo run -p neard -- run
7071
```
7172

7273
This sets up a new single node testnet, with predetermined private key of the validator and turns on "fast" mode of block production to make development and testing easy.
@@ -76,7 +77,7 @@ This sets up a new single node testnet, with predetermined private key of the va
7677
Many times in development of the node it's useful to see detailed logs about what is happening. `neard` binary has `--verbose` mode to show more details about what is happening:
7778

7879
```bash
79-
cargo run --package neard --bin neard -- --verbose=true run
80+
cargo run -p neard -- --verbose= run
8081
```
8182

8283
You can also use the `RUST_LOG` environment variable, with `env_logger` [semantics](https://docs.rs/env_logger/0.6.0/env_logger/#enabling-logging) to override the log level for specific targets. `RUST_LOG` can also be used in integration tests which spawn runnable apps.
@@ -115,7 +116,7 @@ Official image is published at `nearprotocol/nearcore`
115116
> <a href="https://stackoverflow.com/questions/tagged/nearprotocol">
116117
> <h8>Ask it on StackOverflow!</h8></a>
117118
118-
# How to speed up rust compilation time
119+
## How to speed up rust compilation time
119120

120121
This describes on how to improve nearcore compilation time without having to
121122
modify Cargo.toml. It's possible to override Cargo.toml setting by setting
@@ -124,14 +125,14 @@ modify Cargo.toml. It's possible to override Cargo.toml setting by setting
124125

125126
### Default build {#default-build}
126127

127-
```bash
128-
# cargo clean
129-
# time RUSTFLAGS= RUSTC_WRAPPER= cargo build -p neard --release
128+
```console
129+
$ cargo clean
130+
$ time env RUSTFLAGS= RUSTC_WRAPPER= cargo build -p neard --release
130131
real 2m13.773s
131132
user 44m49.204s
132133
sys 1m22.583s
133-
# touch */*/*/*.rs
134-
# time RUSTFLAGS= RUSTC_WRAPPER= cargo build -p neard --release
134+
$ touch */*/*/*.rs
135+
$ time env RUSTFLAGS= RUSTC_WRAPPER= cargo build -p neard --release
135136
real 0m38.021s
136137
user 9m37.045s
137138
sys 0m5.302s
@@ -149,14 +150,14 @@ lto=off`.
149150

150151
Requires installing lld linker.
151152

152-
```bash
153-
# cargo clean
154-
# time RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld' RUSTC_WRAPPER= cargo build -p neard --release
153+
```console
154+
$ cargo clean
155+
$ time env RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld' RUSTC_WRAPPER= cargo build -p neard --release
155156
real 1m50.802s
156157
user 36m50.251s
157158
sys 1m19.069s
158-
# touch */*/*/*.rs
159-
# time RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld' RUSTC_WRAPPER= cargo build -p neard --release
159+
$ touch */*/*/*.rs
160+
$ time env RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld' RUSTC_WRAPPER= cargo build -p neard --release
160161
real 0m28.951s
161162
user 6m56.670s
162163
sys 0m4.307s
@@ -166,32 +167,69 @@ sys 0m4.307s
166167

167168
Works only with nightly compiler.
168169

169-
```bash
170-
# cargo clean
171-
# time RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld -Zshare-generics=y' RUSTC_WRAPPER= cargo build -p neard --release
170+
```console
171+
$ cargo clean
172+
$ time env RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld -Zshare-generics=y' RUSTC_WRAPPER= cargo build -p neard --release
172173
real 1m42.999s
173174
user 33m31.341s
174175
sys 1m25.773s
175-
# touch */*/*/*.rs
176-
# time RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld -Zshare-generics=y' RUSTC_WRAPPER= cargo build -p neard --release
176+
$ touch */*/*/*.rs
177+
$ time env RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld -Zshare-generics=y' RUSTC_WRAPPER= cargo build -p neard --release
177178
real 0m23.501s
178179
user 4m30.597s
179180
sys 0m3.804s
180181
```
181182

183+
### Use system-provided rocksdb
184+
185+
By default, building `neard` will result in compiling `librocksdb`
186+
which takes up some non-negligible amount of memory especially when
187+
building on a system with many cores. It’s possible to configure
188+
build process to provide system-provided library instead. This is
189+
done by setting `ROCKSDB_LIB_DIR` environment variable to location
190+
where `librocksdb.a` file is installed:
191+
192+
```console
193+
$ cargo clean
194+
$ time env ROCKSDB_LIB_DIR=/usr/lib/x86_64-linux-gnu cargo build -p neard --release
195+
real 1m31.014s
196+
user 30m46.544s
197+
sys 1m26.932s
198+
$ touch */*/*/*.rs
199+
$ time env ROCKSDB_LIB_DIR=/usr/lib/x86_64-linux-gnu cargo build -p neard --release
200+
real 0m35.061s
201+
user 9m7.968s
202+
sys 0m10.486s
203+
```
204+
205+
Note that the system must provide a recent version of the library
206+
which, depending on operating system you’re using, may require
207+
installing packages from testing branches. For example, on Debian it
208+
requires installing `librocksdb-dev` from `experimental` version:
209+
210+
```bash
211+
echo 'deb http://ftp.debian.org/debian experimental main contrib non-free' |
212+
sudo tee -a /etc/apt/sources.list
213+
sudo apt update
214+
sudo apt -t experimental install librocksdb-dev
215+
216+
ROCKSDB_LIB_DIR=/usr/lib/x86_64-linux-gnu
217+
export ROCKSDB_LIB_DIR
218+
```
219+
182220
### Cache results from previous compilations using sccache {#cache-results-from-previous-compilations-using-sccache}
183221

184222
Requires installing sscache. If you want to compile sccache with `cargo install sccache` make sure you use stable version of Rust.
185223

186-
```bash
187-
# cargo clean
188-
# rm -rf ~/.cache/sccache/
189-
# time RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld -Zshare-generics=y' RUSTC_WRAPPER=sccache cargo build -p neard --release
224+
```console
225+
$ cargo clean
226+
$ rm -rf ~/.cache/sccache/
227+
$ time env RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld -Zshare-generics=y' RUSTC_WRAPPER=sccache cargo build -p neard --release
190228
real 2m6.452s
191229
user 3m24.797s
192230
sys 0m30.919s
193-
# cargo clean
194-
# time RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld -Zshare-generics=y' RUSTC_WRAPPER=sccache cargo build -p neard --release
231+
$ cargo clean
232+
$ time env RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld -Zshare-generics=y' RUSTC_WRAPPER=sccache cargo build -p neard --release
195233
real 0m24.292s
196234
user 3m3.627s
197235
sys 0m27.619s
@@ -201,9 +239,9 @@ sys 0m27.619s
201239

202240
### Setting for building release binary with debug symbols and reduced inlining {#setting-for-building-release-binary-with-debug-symbols-and-reduced-inlining}
203241

204-
```bash
205-
# cargo clean
206-
# time RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld -Zshare-generics=y -C inline-threshold=25 -C debuginfo=2' RUSTC_WRAPPER=sccache cargo build -p neard --release
242+
```console
243+
$ cargo clean
244+
$ time env RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld -Zshare-generics=y -C inline-threshold=25 -C debuginfo=2' RUSTC_WRAPPER=sccache cargo build -p neard --release
207245
real 1m50.521s
208246
user 3m39.398s
209247
sys 0m32.069s
@@ -214,9 +252,9 @@ sys 0m32.069s
214252
Building this way cuts down build time, but the node will likely be too slow to run. This is useful in case you need
215253
to build package to run tests or do build within CLion/Intellij.
216254

217-
```bash
218-
# cargo clean
219-
# time RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld -Zshare-generics=y' RUSTC_WRAPPER=sccache cargo build -p neard
255+
```console
256+
$ cargo clean
257+
$ time env RUSTFLAGS='-C lto=off -C link-arg=-fuse-ld=lld -Zshare-generics=y' RUSTC_WRAPPER=sccache cargo build -p neard
220258
real 1m18.198s
221259
user 4m35.409s
222260
sys 0m32.220s

0 commit comments

Comments
 (0)