Browse Source

Cache size optimizations

0.8
Pieter Wuille 12 years ago
parent
commit
1c83b0a377
  1. 17
      src/init.cpp
  2. 10
      src/leveldb.cpp
  3. 2
      src/leveldb.h
  4. 3
      src/main.cpp
  5. 1
      src/main.h
  6. 4
      src/txdb.cpp
  7. 4
      src/txdb.h

17
src/init.cpp

@ -236,7 +236,6 @@ std::string HelpMessage() @@ -236,7 +236,6 @@ std::string HelpMessage()
" -gen=0 " + _("Don't generate coins") + "\n" +
" -datadir=<dir> " + _("Specify data directory") + "\n" +
" -dbcache=<n> " + _("Set database cache size in megabytes (default: 25)") + "\n" +
" -dblogsize=<n> " + _("Set database disk log size in megabytes (default: 100)") + "\n" +
" -timeout=<n> " + _("Specify connection timeout in milliseconds (default: 5000)") + "\n" +
" -proxy=<ip:port> " + _("Connect through socks proxy") + "\n" +
" -socks=<n> " + _("Select the version of socks proxy to use (4-5, default: 5)") + "\n" +
@ -651,11 +650,23 @@ bool AppInit2() @@ -651,11 +650,23 @@ bool AppInit2()
return InitError(msg);
}
// cache size calculations
size_t nTotalCache = GetArg("-dbcache", 25) << 20;
if (nTotalCache < (1 << 22))
nTotalCache = (1 << 22); // total cache cannot be less than 4 MiB
size_t nBlockTreeDBCache = nTotalCache / 8;
if (nBlockTreeDBCache > (1 << 21))
nBlockTreeDBCache = (1 << 21); // block tree db cache shouldn't be larger than 2 MiB
nTotalCache -= nBlockTreeDBCache;
size_t nCoinDBCache = nTotalCache / 2; // use half of the remaining cache for coindb cache
nTotalCache -= nCoinDBCache;
nCoinCacheSize = nTotalCache / 300; // coins in memory require around 300 bytes
uiInterface.InitMessage(_("Loading block index..."));
printf("Loading block index...\n");
nStart = GetTimeMillis();
pblocktree = new CBlockTreeDB();
pcoinsdbview = new CCoinsViewDB();
pblocktree = new CBlockTreeDB(nBlockTreeDBCache);
pcoinsdbview = new CCoinsViewDB(nCoinDBCache);
pcoinsTip = new CCoinsViewCache(*pcoinsdbview);
if (!LoadBlockIndex())

10
src/leveldb.cpp

@ -12,22 +12,22 @@ @@ -12,22 +12,22 @@
#include <boost/filesystem.hpp>
static leveldb::Options GetOptions() {
static leveldb::Options GetOptions(size_t nCacheSize) {
leveldb::Options options;
int nCacheSizeMB = GetArg("-dbcache", 25);
options.block_cache = leveldb::NewLRUCache(nCacheSizeMB * 1048576);
options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
options.compression = leveldb::kNoCompression;
return options;
}
CLevelDB::CLevelDB(const boost::filesystem::path &path, bool fMemory) {
CLevelDB::CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory) {
penv = NULL;
readoptions.verify_checksums = true;
iteroptions.verify_checksums = true;
iteroptions.fill_cache = false;
syncoptions.sync = true;
options = GetOptions();
options = GetOptions(nCacheSize);
options.create_if_missing = true;
if (fMemory) {
penv = leveldb::NewMemEnv(leveldb::Env::Default());

2
src/leveldb.h

@ -69,7 +69,7 @@ private: @@ -69,7 +69,7 @@ private:
leveldb::DB *pdb;
public:
CLevelDB(const boost::filesystem::path &path, bool fMemory = false);
CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory = false);
~CLevelDB();
template<typename K, typename V> bool Read(const K& key, V& value) {

3
src/main.cpp

@ -41,6 +41,7 @@ CBlockIndex* pindexBest = NULL; @@ -41,6 +41,7 @@ CBlockIndex* pindexBest = NULL;
set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid; // may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
int64 nTimeBestReceived = 0;
bool fImporting = false;
unsigned int nCoinCacheSize = 5000;
CMedianFilter<int> cPeerBlockCounts(5, 0); // Amount of blocks that other nodes claim to have
@ -1735,7 +1736,7 @@ bool SetBestChain(CBlockIndex* pindexNew) @@ -1735,7 +1736,7 @@ bool SetBestChain(CBlockIndex* pindexNew)
// Make sure it's successfully written to disk before changing memory structure
bool fIsInitialDownload = IsInitialBlockDownload();
if (!fIsInitialDownload || view.GetCacheSize()>5000) {
if (!fIsInitialDownload || view.GetCacheSize() > nCoinCacheSize) {
FlushBlockFile();
pblocktree->Sync();
if (!view.Flush())

1
src/main.h

@ -88,6 +88,7 @@ extern CCriticalSection cs_setpwalletRegistered; @@ -88,6 +88,7 @@ extern CCriticalSection cs_setpwalletRegistered;
extern std::set<CWallet*> setpwalletRegistered;
extern unsigned char pchMessageStart[4];
extern bool fImporting;
extern unsigned int nCoinCacheSize;
// Settings
extern int64 nTransactionFee;

4
src/txdb.cpp

@ -19,7 +19,7 @@ void static BatchWriteHashBestChain(CLevelDBBatch &batch, const uint256 &hash) { @@ -19,7 +19,7 @@ void static BatchWriteHashBestChain(CLevelDBBatch &batch, const uint256 &hash) {
batch.Write('B', hash);
}
CCoinsViewDB::CCoinsViewDB(bool fMemory) : db(GetDataDir() / "coins", fMemory) {
CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory) : db(GetDataDir() / "coins", nCacheSize, fMemory) {
}
bool CCoinsViewDB::GetCoins(uint256 txid, CCoins &coins) {
@ -64,7 +64,7 @@ bool CCoinsViewDB::BatchWrite(const std::map<uint256, CCoins> &mapCoins, CBlockI @@ -64,7 +64,7 @@ bool CCoinsViewDB::BatchWrite(const std::map<uint256, CCoins> &mapCoins, CBlockI
return db.WriteBatch(batch);
}
CBlockTreeDB::CBlockTreeDB(bool fMemory) : CLevelDB(GetDataDir() / "blktree", fMemory) {
CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory) : CLevelDB(GetDataDir() / "blktree", nCacheSize, fMemory) {
}
bool CBlockTreeDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)

4
src/txdb.h

@ -14,7 +14,7 @@ class CCoinsViewDB : public CCoinsView @@ -14,7 +14,7 @@ class CCoinsViewDB : public CCoinsView
protected:
CLevelDB db;
public:
CCoinsViewDB(bool fMemory = false);
CCoinsViewDB(size_t nCacheSize, bool fMemory = false);
bool GetCoins(uint256 txid, CCoins &coins);
bool SetCoins(uint256 txid, const CCoins &coins);
@ -29,7 +29,7 @@ public: @@ -29,7 +29,7 @@ public:
class CBlockTreeDB : public CLevelDB
{
public:
CBlockTreeDB(bool fMemory = false);
CBlockTreeDB(size_t nCacheSize, bool fMemory = false);
private:
CBlockTreeDB(const CBlockTreeDB&);
void operator=(const CBlockTreeDB&);

Loading…
Cancel
Save