From 1c83b0a3771bc601fdc75588f2cd45318b19c526 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sun, 4 Nov 2012 17:11:48 +0100 Subject: Cache size optimizations --- src/init.cpp | 17 ++++++++++++++--- src/leveldb.cpp | 10 +++++----- src/leveldb.h | 2 +- src/main.cpp | 3 ++- src/main.h | 1 + src/txdb.cpp | 4 ++-- src/txdb.h | 4 ++-- 7 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 1724382bf5..b3ae69d6e3 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -236,7 +236,6 @@ std::string HelpMessage() " -gen=0 " + _("Don't generate coins") + "\n" + " -datadir= " + _("Specify data directory") + "\n" + " -dbcache= " + _("Set database cache size in megabytes (default: 25)") + "\n" + - " -dblogsize= " + _("Set database disk log size in megabytes (default: 100)") + "\n" + " -timeout= " + _("Specify connection timeout in milliseconds (default: 5000)") + "\n" + " -proxy= " + _("Connect through socks proxy") + "\n" + " -socks= " + _("Select the version of socks proxy to use (4-5, default: 5)") + "\n" + @@ -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()) diff --git a/src/leveldb.cpp b/src/leveldb.cpp index e8a0fbe874..58b75e5295 100644 --- a/src/leveldb.cpp +++ b/src/leveldb.cpp @@ -12,22 +12,22 @@ #include -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()); diff --git a/src/leveldb.h b/src/leveldb.h index ee9079c3c3..e5b2e1ef0b 100644 --- a/src/leveldb.h +++ b/src/leveldb.h @@ -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 bool Read(const K& key, V& value) { diff --git a/src/main.cpp b/src/main.cpp index 43bd5dd472..a08e393caa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,6 +41,7 @@ CBlockIndex* pindexBest = NULL; set 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 cPeerBlockCounts(5, 0); // Amount of blocks that other nodes claim to have @@ -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()) diff --git a/src/main.h b/src/main.h index 744c0e4b51..899cabd85a 100644 --- a/src/main.h +++ b/src/main.h @@ -88,6 +88,7 @@ extern CCriticalSection cs_setpwalletRegistered; extern std::set setpwalletRegistered; extern unsigned char pchMessageStart[4]; extern bool fImporting; +extern unsigned int nCoinCacheSize; // Settings extern int64 nTransactionFee; diff --git a/src/txdb.cpp b/src/txdb.cpp index 6550f57876..d9972d5b06 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -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 &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) diff --git a/src/txdb.h b/src/txdb.h index 123ec00d23..e13925c964 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -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: class CBlockTreeDB : public CLevelDB { public: - CBlockTreeDB(bool fMemory = false); + CBlockTreeDB(size_t nCacheSize, bool fMemory = false); private: CBlockTreeDB(const CBlockTreeDB&); void operator=(const CBlockTreeDB&); -- cgit v1.2.3