Skip to content

Commit e331216

Browse files
committed
dbwrapper: Set global leveldb mmap limit
Set the default leveldb mmap limit to 4096 from dbwrapper, before creating the first leveldb context. The motivation here is to remove the need for a leveldb patch, see google/leveldb#1265.
1 parent 1b1b9f3 commit e331216

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/dbwrapper.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#include <leveldb/slice.h>
2929
#include <leveldb/status.h>
3030
#include <leveldb/write_batch.h>
31+
#ifndef WIN32
32+
#include <leveldb/util/env_posix_test_helper.h>
33+
#endif
3134
#include <memory>
3235
#include <optional>
3336
#include <utility>
@@ -212,9 +215,40 @@ struct LevelDBContext {
212215
leveldb::DB* pdb;
213216
};
214217

218+
#ifndef WIN32
219+
namespace leveldb {
220+
221+
/** Global levelDB initialization. This needs to be constructed once before the first levelDB context is created.
222+
*
223+
* Sadly, due to EnvPosixTestHelper's methods being private, this can only be done from the leveldb namespace.
224+
*/
225+
class EnvPosixTest {
226+
public:
227+
EnvPosixTest()
228+
{
229+
/*
230+
* By default LevelDB will only mmap() up to 1000 ldb files for reading and then fall back
231+
* to using file descriptors.
232+
*
233+
* The typical linux system has a 'vm.max_map_count = 65530', so mapping only 1000 files
234+
* seems arbitrarily small. Increase this value to another arbitrarily small value, 4096.
235+
*/
236+
if (sizeof(void*) >= 8) {
237+
leveldb::EnvPosixTestHelper::SetReadOnlyMMapLimit(4096);
238+
}
239+
}
240+
};
241+
242+
}
243+
#endif
244+
215245
CDBWrapper::CDBWrapper(const DBParams& params)
216246
: m_db_context{std::make_unique<LevelDBContext>()}, m_name{fs::PathToString(params.path.stem())}, m_path{params.path}, m_is_memory{params.memory_only}
217247
{
248+
#ifndef WIN32
249+
static leveldb::EnvPosixTest one_time_setup;
250+
#endif
251+
218252
DBContext().penv = nullptr;
219253
DBContext().readoptions.verify_checksums = true;
220254
DBContext().iteroptions.verify_checksums = true;

0 commit comments

Comments
 (0)