I recently noticed that the Bitcoin Core GUI (v0.18.1) was constantly using significant amounts of CPU when idle. I disconnected all peers and it continued to use the CPU.
It turns out that it is calling WalletModel::pollBalanceChanged
4 times per second for all of my wallets, which causes it to loop through all the transactions in all my wallets 4 times per second, and uses a lot of CPU.
Is this intentional? It seems like it may be unnecessary.
I tried changing the pollTimer, using 2.5 seconds instead of 250 ms and it dropped the CPU usage to much lower, so I’m pretty sure this is what’s causing the CPU load:
connect(pollTimer, &QTimer::timeout, this, &WalletModel::pollBalanceChanged);
pollTimer->start(MODEL_UPDATE_DELAY*10);
Most of the CPU is being used by LookupBlockIndex
looking up the same few thousand block hashes over and over, here:
[#3](/bitcoin-bitcoin/3/) std::unordered_map<...>::find (__x=..., this=<optimized out>) at /usr/include/c++/8/bits/unordered_map.h:920
[#4](/bitcoin-bitcoin/4/) LookupBlockIndex (hash=...) at ./validation.h:436
[#5](/bitcoin-bitcoin/5/) interfaces::(anonymous namespace)::LockImpl::getBlockHeight (this=<optimized out>, hash=...) at interfaces/chain.cpp:34
[#6](/bitcoin-bitcoin/6/) interfaces::(anonymous namespace)::LockImpl::getBlockDepth (this=0x555556ebb4c0, hash=...) at interfaces/chain.cpp:43
[#7](/bitcoin-bitcoin/7/) CMerkleTx::GetDepthInMainChain (this=this@entry=0x7fff7ce26940, locked_chain=...) at wallet/wallet.cpp:4479
[#8](/bitcoin-bitcoin/8/) CWalletTx::IsTrusted (this=this@entry=0x7fff7ce26940, locked_chain=...) at wallet/wallet.cpp:2081
[#9](/bitcoin-bitcoin/9/) CWallet::GetUnconfirmedBalance (this=0x7fff7dc19150) at /usr/include/c++/8/bits/unique_ptr.h:342
[#10](/bitcoin-bitcoin/10/) interfaces::(anonymous namespace)::WalletImpl::getBalances (this=0x7fff7d4e66a0) at /usr/include/c++/8/bits/shared_ptr_base.h:1307
[#11](/bitcoin-bitcoin/11/) interfaces::(anonymous namespace)::WalletImpl::tryGetBalances (this=0x7fff7d4e66a0, balances=..., num_blocks=@0x7fffffffd9cc: -1) at interfaces/wallet.cpp:388
[#12](/bitcoin-bitcoin/12/) WalletModel::pollBalanceChanged (this=0x5555569caef0) at /usr/include/c++/8/bits/unique_ptr.h:342