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

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, 5 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 // DBTracker's leveldb::DB wrapper. Forwards all calls to the underlying
1106 // leveldb::DB instance. Registers / unregisters itself in the registry.
1107 class DBTracker::DBWrapperImpl : public base::LinkNode<DBWrapperImpl>,
1108 public DBWrapper {
1109 public:
1110 DBWrapperImpl(DBTracker* tracker, const std::string name, leveldb::DB* db)
1111 : tracker_(tracker), name_(name), db_(db) {
1112 tracker_->DatabaseOpened(this);
1113 }
1114
1115 ~DBWrapperImpl() override { tracker_->DatabaseClosed(this); }
1116
1117 const std::string& name() const override { return name_; }
1118
1119 leveldb::Status Put(const leveldb::WriteOptions& options,
1120 const leveldb::Slice& key,
1121 const leveldb::Slice& value) override {
1122 return db_->Put(options, key, value);
1123 }
1124
1125 leveldb::Status Delete(const leveldb::WriteOptions& options,
1126 const leveldb::Slice& key) override {
1127 return db_->Delete(options, key);
1128 }
1129
1130 leveldb::Status Write(const leveldb::WriteOptions& options,
1131 leveldb::WriteBatch* updates) override {
1132 return db_->Write(options, updates);
1133 }
1134
1135 leveldb::Status Get(const leveldb::ReadOptions& options,
1136 const leveldb::Slice& key,
1137 std::string* value) override {
1138 return db_->Get(options, key, value);
1139 }
1140
1141 const leveldb::Snapshot* GetSnapshot() override { return db_->GetSnapshot(); }
1142
1143 void ReleaseSnapshot(const leveldb::Snapshot* snapshot) override {
1144 return db_->ReleaseSnapshot(snapshot);
1145 }
1146
1147 bool GetProperty(const leveldb::Slice& property,
1148 std::string* value) override {
1149 return db_->GetProperty(property, value);
1150 }
1151
1152 void GetApproximateSizes(const leveldb::Range* range,
1153 int n,
1154 uint64_t* sizes) override {
1155 return db_->GetApproximateSizes(range, n, sizes);
1156 }
1157
1158 void CompactRange(const leveldb::Slice* begin,
1159 const leveldb::Slice* end) override {
1160 return db_->CompactRange(begin, end);
1161 }
1162
1163 leveldb::Iterator* NewIterator(const leveldb::ReadOptions& options) override {
1164 return db_->NewIterator(options);
1165 }
1166
1167 private:
1168 DBTracker* tracker_;
1169 std::string name_;
1170 std::unique_ptr<leveldb::DB> db_;
1171 };
1172
1173 // Reports live databases to memory-infra. For each live database the following
1174 // information is reported:
1175 // 1. Instance pointer (to disambiguate databases).
1176 // 2. Memory taken by the database.
1177 // 3. The name of the database (when not in BACKGROUND mode to avoid exposing
1178 // PIIs in slow reports).
1179 //
1180 // Example report (as seen after clicking "leveldatabase" in "Overview" pane
1181 // in Chrome tracing UI):
1182 //
1183 // Component size name
1184 // ---------------------------------------------------------------------------
1185 // leveldatabase 204.4 KiB
1186 // 0x7FE70F2040A0 4.0 KiB /Users/.../data_reduction_proxy_leveldb
1187 // 0x7FE70F530D80 188.4 KiB /Users/.../Sync Data/LevelDB
1188 // 0x7FE71442F270 4.0 KiB /Users/.../Sync App Settings/...
1189 // 0x7FE71471EC50 8.0 KiB /Users/.../Extension State
1190 //
1191 class DBTracker::MemoryDumpProvider
1192 : public base::trace_event::MemoryDumpProvider {
1193 public:
1194 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
1195 base::trace_event::ProcessMemoryDump* pmd) override {
1196 auto db_visitor = [&](DBWrapper* db) {
1197 std::string db_dump_name = DBTracker::GetMemoryDumpName(db);
1198 auto* db_dump = pmd->CreateAllocatorDump(db_dump_name.c_str());
1199
1200 uint64_t db_memory_usage = 0;
1201 {
1202 std::string usage_string;
1203 bool success = db->GetProperty("leveldb.approximate-memory-usage",
1204 &usage_string) &&
1205 base::StringToUint64(usage_string, &db_memory_usage);
1206 DCHECK(success);
1207 }
1208 db_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
1209 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
1210 db_memory_usage);
1211
1212 if (args.level_of_detail !=
1213 base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND) {
1214 db_dump->AddString("name", "", db->name());
1215 }
1216
1217 const char* system_allocator_name =
1218 base::trace_event::MemoryDumpManager::GetInstance()
1219 ->system_allocator_pool_name();
1220 if (system_allocator_name) {
1221 pmd->AddSuballocation(db_dump->guid(), system_allocator_name);
1222 }
1223 };
1224
1225 DBTracker::GetInstance()->VisitDatabases(db_visitor);
1226 return true;
1227 }
1228 };
1229
1230 DBTracker::DBTracker() : mdp_(new MemoryDumpProvider()) {
1231 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
1232 mdp_.get(), "LevelDB", nullptr);
1233 }
1234
1235 DBTracker::~DBTracker() {
1236 NOTREACHED(); // DBTracker is a singleton
1237 }
1238
1239 DBTracker* DBTracker::GetInstance() {
1240 static DBTracker* instance = new DBTracker();
1241 return instance;
1242 }
1243
1244 leveldb::Status DBTracker::Open(const leveldb::Options& options,
1245 const std::string& name,
1246 leveldb::DB** dbptr) {
1247 auto status = leveldb::DB::Open(options, name, dbptr);
1248 if (status.ok()) {
1249 // DBWrapperImpl ctor adds the instance to the tracker.
1250 *dbptr = new DBWrapperImpl(GetInstance(), name, *dbptr);
1251 }
1252 return status;
1253 }
1254
1255 std::string DBTracker::GetMemoryDumpName(leveldb::DB* db) {
1256 return base::StringPrintf("leveldatabase/0x%" PRIXPTR,
1257 reinterpret_cast<uintptr_t>(db));
1258 }
1259
1260 void DBTracker::VisitDatabases(const DatabaseVisitor& visitor) {
1261 base::AutoLock lock(databases_lock_);
1262 for (auto* i = databases_.head(); i != databases_.end(); i = i->next()) {
1263 visitor(i->value());
1264 }
1265 }
1266
1267 void DBTracker::DatabaseOpened(DBWrapperImpl* database) {
1268 base::AutoLock lock(databases_lock_);
1269 databases_.Append(database);
1270 }
1271
1272 void DBTracker::DatabaseClosed(DBWrapperImpl* database) {
1273 base::AutoLock lock(databases_lock_);
1274 database->RemoveFromList();
1275 }
1276
1098 } // namespace leveldb_env 1277 } // namespace leveldb_env
1099 1278
1100 namespace leveldb { 1279 namespace leveldb {
1101 1280
1102 Env* Env::Default() { 1281 Env* Env::Default() {
1103 return leveldb_env::default_env.Pointer(); 1282 return leveldb_env::default_env.Pointer();
1104 } 1283 }
1105 1284
1106 } // namespace leveldb 1285 } // namespace leveldb
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698