Commit 1c4e7620 authored by Chris Harrelson's avatar Chris Harrelson Committed by Commit Bot

Use a shared LevelDB block cache for LevelDB instances.

There will be one shared block cache for all users of
LevelDB::Init, and a separate one for All IndexedDB
instances. The default is 8MB, but it's 512KB for
low-memory Android devices.

Previously, each instance would get its own block cache, and use
the default LevelDB settings for such a cache, which is 8MB (*)

(*) https://cs.chromium.org/chromium/src/third_party/leveldatabase/src/include/leveldb/options.h?gsn=Options&l=96

Bug: 711518
Change-Id: I6a64467815489ae7695b8db9b9074170d9e56d94
Reviewed-on: https://chromium-review.googlesource.com/567583
Commit-Queue: Chris Harrelson <chrishtr@chromium.org>
Reviewed-by: default avatarDaniel Murphy <dmurph@chromium.org>
Reviewed-by: default avatarMarijn Kruisselbrink <mek@chromium.org>
Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Cr-Commit-Position: refs/heads/master@{#486934}
parent c0f7856f
......@@ -15,6 +15,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/sys_info.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/threading/thread_checker.h"
#include "base/trace_event/memory_dump_manager.h"
......@@ -86,16 +87,33 @@ bool LevelDB::InitWithOptions(const base::FilePath& database_dir,
return false;
}
static size_t DefaultBlockCacheSize() {
if (base::SysInfo::IsLowEndDevice())
return 512 * 1024; // 512KB
else
return 8 * 1024 * 1024; // 8MB
}
bool LevelDB::Init(const leveldb_proto::Options& options) {
leveldb::Options leveldb_options;
leveldb_options.create_if_missing = true;
leveldb_options.max_open_files = 0; // Use minimum.
leveldb_options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue;
static leveldb::Cache* default_block_cache =
leveldb::NewLRUCache(DefaultBlockCacheSize());
if (options.write_buffer_size != 0)
leveldb_options.write_buffer_size = options.write_buffer_size;
if (options.read_cache_size != 0) {
custom_block_cache_.reset(leveldb::NewLRUCache(options.read_cache_size));
leveldb_options.block_cache = custom_block_cache_.get();
} else {
// By default, allocate a single block cache to be shared across
// all LevelDB instances created via this method.
// See also content/browser/indexed_db/leveldb/leveldb_database.cc,
// which has its own shared block cache for IndexedDB databases.
leveldb_options.block_cache = default_block_cache;
}
if (options.database_dir.empty()) {
env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
......
......@@ -10,6 +10,9 @@
namespace leveldb_proto {
struct Options {
// If read_cache_size is specified, a segregated block cache will be
// created for this LevelDB instance. Otherwise a shared block cache
// will be used.
Options(const base::FilePath& database_dir)
: database_dir(database_dir), write_buffer_size(0), read_cache_size(0) {}
Options(const base::FilePath& database_dir,
......
......@@ -33,11 +33,11 @@
#include "content/browser/indexed_db/leveldb/leveldb_write_batch.h"
#include "third_party/leveldatabase/env_chromium.h"
#include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
#include "third_party/leveldatabase/src/include/leveldb/cache.h"
#include "third_party/leveldatabase/src/include/leveldb/db.h"
#include "third_party/leveldatabase/src/include/leveldb/env.h"
#include "third_party/leveldatabase/src/include/leveldb/filter_policy.h"
#include "third_party/leveldatabase/src/include/leveldb/slice.h"
using base::StringPiece;
namespace content {
......@@ -117,6 +117,13 @@ void LevelDBDatabase::CloseDatabase() {
}
}
static size_t DefaultBlockCacheSize() {
if (base::SysInfo::IsLowEndDevice())
return 512 * 1024; // 512KB
else
return 8 * 1024 * 1024; // 8MB
}
static leveldb::Status OpenDB(
leveldb::Comparator* comparator,
leveldb::Env* env,
......@@ -139,6 +146,14 @@ static leveldb::Status OpenDB(
options.max_open_files = 80;
options.env = env;
// A shared block cache for all IndexedDB instances across all renderers.
// See also components/leveldb_proto/leveldb_database.cc, which has
// its own block cache for a different (internal use-cases) set of LevelDB
// instances.
static leveldb::Cache* default_block_cache =
leveldb::NewLRUCache(DefaultBlockCacheSize());
options.block_cache = default_block_cache;
// ChromiumEnv assumes UTF8, converts back to FilePath before using.
leveldb::DB* db_ptr = nullptr;
leveldb::Status s = leveldb::DB::Open(options, path.AsUTF8Unsafe(), &db_ptr);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment