Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: third_party/leveldatabase/env_chromium.cc

Issue 2855953002: leveldb: Add DBTracker for exposing databases to Chrome's memory-infra. (Closed)
Patch Set: Address comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors. 3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 4
5 #include "third_party/leveldatabase/env_chromium.h" 5 #include "third_party/leveldatabase/env_chromium.h"
6 6
7 #include <inttypes.h>
8
7 #include <utility> 9 #include <utility>
8 10
9 #if defined(OS_POSIX) 11 #if defined(OS_POSIX)
10 #include <dirent.h> 12 #include <dirent.h>
11 #include <sys/types.h> 13 #include <sys/types.h>
12 #endif 14 #endif
13 15
14 #include "base/files/file_enumerator.h" 16 #include "base/files/file_enumerator.h"
15 #include "base/files/file_util.h" 17 #include "base/files/file_util.h"
16 #include "base/lazy_instance.h" 18 #include "base/lazy_instance.h"
19 #include "base/logging.h"
17 #include "base/macros.h" 20 #include "base/macros.h"
18 #include "base/metrics/histogram_functions.h" 21 #include "base/metrics/histogram_functions.h"
19 #include "base/process/process_metrics.h" 22 #include "base/process/process_metrics.h"
20 #include "base/stl_util.h" 23 #include "base/stl_util.h"
24 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_util.h" 25 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h" 26 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h" 27 #include "base/strings/utf_string_conversions.h"
24 #include "base/threading/thread_restrictions.h" 28 #include "base/threading/thread_restrictions.h"
29 #include "base/trace_event/memory_dump_manager.h"
30 #include "base/trace_event/memory_dump_provider.h"
31 #include "base/trace_event/process_memory_dump.h"
25 #include "base/trace_event/trace_event.h" 32 #include "base/trace_event/trace_event.h"
26 #include "third_party/leveldatabase/chromium_logger.h" 33 #include "third_party/leveldatabase/chromium_logger.h"
27 #include "third_party/leveldatabase/src/include/leveldb/options.h" 34 #include "third_party/leveldatabase/src/include/leveldb/options.h"
28 #include "third_party/re2/src/re2/re2.h" 35 #include "third_party/re2/src/re2/re2.h"
29 36
30 using base::FilePath; 37 using base::FilePath;
31 using leveldb::FileLock; 38 using leveldb::FileLock;
32 using leveldb::Slice; 39 using leveldb::Slice;
33 using leveldb::Status; 40 using leveldb::Status;
34 41
(...skipping 1053 matching lines...) Expand 10 before | Expand all | Expand 10 after
1088 return LEVELDB_STATUS_CORRUPTION; 1095 return LEVELDB_STATUS_CORRUPTION;
1089 if (s.IsNotSupportedError()) 1096 if (s.IsNotSupportedError())
1090 return LEVELDB_STATUS_NOT_SUPPORTED; 1097 return LEVELDB_STATUS_NOT_SUPPORTED;
1091 if (s.IsIOError()) 1098 if (s.IsIOError())
1092 return LEVELDB_STATUS_IO_ERROR; 1099 return LEVELDB_STATUS_IO_ERROR;
1093 // TODO(cmumford): IsInvalidArgument() was just added to leveldb. Use this 1100 // TODO(cmumford): IsInvalidArgument() was just added to leveldb. Use this
1094 // function once that change goes to the public repository. 1101 // function once that change goes to the public repository.
1095 return LEVELDB_STATUS_INVALID_ARGUMENT; 1102 return LEVELDB_STATUS_INVALID_ARGUMENT;
1096 } 1103 }
1097 1104
1105 // DBRegistry's leveldb::DB wrapper. Forwards all calls to the underlying
1106 // leveldb::DB instance. Registers / unregisters itself in the registry.
1107 class DBRegistry::DBWrapperImpl : public base::LinkNode<DBWrapperImpl>,
1108 public DBWrapper {
1109 public:
1110 DBWrapperImpl(const std::string name, leveldb::DB* db)
pwnall 2017/06/22 02:50:22 I think it's cleaner to pass a DBRegistry pointer
DmitrySkiba 2017/06/26 20:45:18 Done.
1111 : name_(name), db_(db) {
1112 DBRegistry::GetInstance()->RegisterDatabase(this);
1113 }
1114
1115 ~DBWrapperImpl() override {
1116 DBRegistry::GetInstance()->UnregisterDatabase(this);
1117 }
1118
1119 const std::string& name() const override { return name_; }
1120
1121 leveldb::Status Put(const leveldb::WriteOptions& options,
1122 const leveldb::Slice& key,
1123 const leveldb::Slice& value) override {
1124 return db_->Put(options, key, value);
1125 }
1126
1127 leveldb::Status Delete(const leveldb::WriteOptions& options,
1128 const leveldb::Slice& key) override {
1129 return db_->Delete(options, key);
1130 }
1131
1132 leveldb::Status Write(const leveldb::WriteOptions& options,
1133 leveldb::WriteBatch* updates) override {
1134 return db_->Write(options, updates);
1135 }
1136
1137 leveldb::Status Get(const leveldb::ReadOptions& options,
1138 const leveldb::Slice& key,
1139 std::string* value) override {
1140 return db_->Get(options, key, value);
1141 }
1142
1143 const leveldb::Snapshot* GetSnapshot() override { return db_->GetSnapshot(); }
1144
1145 void ReleaseSnapshot(const leveldb::Snapshot* snapshot) override {
1146 return db_->ReleaseSnapshot(snapshot);
1147 }
1148
1149 bool GetProperty(const leveldb::Slice& property,
1150 std::string* value) override {
1151 return db_->GetProperty(property, value);
1152 }
1153
1154 void GetApproximateSizes(const leveldb::Range* range,
1155 int n,
1156 uint64_t* sizes) override {
1157 return db_->GetApproximateSizes(range, n, sizes);
1158 }
1159
1160 void CompactRange(const leveldb::Slice* begin,
1161 const leveldb::Slice* end) override {
1162 return db_->CompactRange(begin, end);
1163 }
1164
1165 leveldb::Iterator* NewIterator(const leveldb::ReadOptions& options) override {
1166 return db_->NewIterator(options);
1167 }
1168
1169 private:
1170 std::string name_;
1171 std::unique_ptr<leveldb::DB> db_;
1172 };
1173
1174 // DBRegistry MemoryDumpProvider implementation. Declared here to minimize
pwnall 2017/06/22 02:50:22 Both sentences in the comment seem redundant with
DmitrySkiba 2017/06/26 20:45:18 Done.
1175 // header file dependencies.
1176 class DBRegistry::MDP : public base::trace_event::MemoryDumpProvider {
1177 public:
1178 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
1179 base::trace_event::ProcessMemoryDump* pmd) override {
1180 auto db_visitor = [&](DBWrapper* db) {
1181 std::string db_dump_name = base::StringPrintf(
1182 "leveldatabase/0x%" PRIXPTR, reinterpret_cast<uintptr_t>(db));
1183 auto* db_dump = pmd->CreateAllocatorDump(db_dump_name.c_str());
1184
1185 uint64_t db_memory_usage = 0;
1186 {
1187 std::string usage_string;
1188 bool success = db->GetProperty("leveldb.approximate-memory-usage",
1189 &usage_string) &&
1190 base::StringToUint64(usage_string, &db_memory_usage);
1191 DCHECK(success);
1192 }
1193 db_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
1194 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
1195 db_memory_usage);
1196
1197 if (args.level_of_detail !=
1198 base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND) {
1199 db_dump->AddString("name", "", db->name());
1200 }
1201
1202 const char* system_allocator_name =
1203 base::trace_event::MemoryDumpManager::GetInstance()
1204 ->system_allocator_pool_name();
1205 if (system_allocator_name) {
1206 pmd->AddSuballocation(db_dump->guid(), system_allocator_name);
1207 }
1208 };
1209
1210 DBRegistry::GetInstance()->VisitDatabases(db_visitor);
1211 return true;
1212 }
1213 };
1214
1215 DBRegistry::DBRegistry() : mdp_(new MDP()) {
1216 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
1217 mdp_.get(), "LevelDB", nullptr);
1218 }
1219
1220 DBRegistry::~DBRegistry() {
1221 NOTREACHED(); // DBRegistry is a singleton
1222 }
1223
1224 DBRegistry* DBRegistry::GetInstance() {
1225 static DBRegistry* instance = new DBRegistry();
pwnall 2017/06/22 02:50:22 Is this threadsafe? (Asking because DBRegistry's c
DmitrySkiba 2017/06/26 20:45:18 Yes, Chrome switched back to using treadsafe stati
1226 return instance;
1227 }
1228
1229 leveldb::Status DBRegistry::Open(const leveldb::Options& options,
1230 const std::string& name,
1231 leveldb::DB** dbptr) {
1232 auto status = leveldb::DB::Open(options, name, dbptr);
1233 if (status.ok()) {
1234 // DBWrapperImpl ctor registers the instance in DBRegistry
1235 *dbptr = new DBWrapperImpl(name, *dbptr);
1236 }
1237 return status;
1238 }
1239
1240 void DBRegistry::VisitDatabases(const DatabaseVisitor& visitor) {
1241 base::AutoLock lock(databases_lock_);
1242 for (auto* i = databases_.head(); i != databases_.end(); i = i->next()) {
1243 visitor(i->value());
1244 }
1245 }
1246
1247 void DBRegistry::RegisterDatabase(DBWrapperImpl* database) {
1248 base::AutoLock lock(databases_lock_);
1249 databases_.Append(database);
1250 }
1251
1252 void DBRegistry::UnregisterDatabase(DBWrapperImpl* database) {
1253 base::AutoLock lock(databases_lock_);
1254 database->RemoveFromList();
1255 }
1256
1098 } // namespace leveldb_env 1257 } // namespace leveldb_env
1099 1258
1100 namespace leveldb { 1259 namespace leveldb {
1101 1260
1102 Env* Env::Default() { 1261 Env* Env::Default() {
1103 return leveldb_env::default_env.Pointer(); 1262 return leveldb_env::default_env.Pointer();
1104 } 1263 }
1105 1264
1106 } // namespace leveldb 1265 } // namespace leveldb
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698