Created
August 15, 2017 04:49
-
-
Save dermoth/b5462462d9edc69f6de79abcfcd017d5 to your computer and use it in GitHub Desktop.
Script performing read-ahead on bitcoin's blockfiles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# blk_ra.sh - Script performing read-ahead on bitcoin's blockfiles | |
# | |
# In order to speed up data loading, this script performs read-ahead | |
# operations on the next block to be read by the bitcoin node. | |
# | |
# The expected setup for this speed-up to be effective is: | |
# | |
# - Blockchain index and chainstate database on SSD | |
# - Blocks on slow spindle storage | |
# - At least a coupls GB'sof available RAM for OS cache | |
# | |
# When doing initial data loading using a bootstrap file (after bootstrap.dat | |
# has been copied to blk*****.dat) or reindex operation, the bitcoin node | |
# will read each block in order. This script will ensure the next block | |
# is already cached in OS RAM then the node comes to read it. | |
# | |
trap 'echo "Error $? on line $LINENO"; exit 1' ERR | |
# process to look for... usually bitcoin-qt or bitcoind | |
PROC=bitcoin-qt | |
# Sleep time between runs - should be tuned as high as possible to avoid | |
# incurring extra load, but too high will miss some blocks | |
SLEEP=20 | |
# Print debug messages (useful for tuning SLEEP, ensure each block is read at | |
# least 1-2 times) | |
DEBUG=1 | |
while : | |
do | |
# Get process pid | |
bpid=$(pgrep -u$USER $PROC) | |
[[ $bpid =~ ^[0-9]+$ ]] || { echo "Invalid or muiltiple pids found: '$bpid'"; false; } | |
((DEBUG)) && echo -n "$PROC ($bpid): Looking for current block... " | |
# This would be much more efficient using an LD_PRELOAD lib :( | |
# Plus rely on likely inconsistent ls behavior!! | |
while ! block=$(ls -lh /proc/$bpid/fd 2>/dev/null|grep 'blk[0-9]*\.dat') | |
do | |
: | |
done | |
# Got full block path, split base and file names | |
[[ $block =~ [[:space:]](/.*/)(blk[0-9]+\.dat) ]] | |
base=${BASH_REMATCH[1]} | |
block=${BASH_REMATCH[2]} | |
((DEBUG)) && echo "Found! $block" | |
# Get next block... Drop 0's or bash will assumes octal | |
[[ $block =~ ^blk0*([0-9]+)\.dat ]] | |
blkid=${BASH_REMATCH[1]} | |
((++blkid)) | |
nextblk=$(printf "blk%05i.dat" $blkid) | |
# Perform read-ahead | |
((DEBUG)) && echo "Performing Read-Ahead on $nextblk" | |
tcmd="" | |
((DEBUG)) && tcmd="time" | |
$tcmd schedtool -D -e cat "$base/$nextblk" >/dev/null | |
sleep $SLEEP | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment