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

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: Created 3 years, 7 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
« no previous file with comments | « third_party/leveldatabase/env_chromium.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
17 #include "base/macros.h" 19 #include "base/macros.h"
18 #include "base/metrics/histogram.h" 20 #include "base/metrics/histogram.h"
19 #include "base/process/process_metrics.h" 21 #include "base/process/process_metrics.h"
20 #include "base/stl_util.h" 22 #include "base/stl_util.h"
21 #include "base/strings/string_util.h" 23 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h" 24 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h" 25 #include "base/strings/utf_string_conversions.h"
26 #include "base/strings/string_number_conversions.h"
27 #include "base/trace_event/memory_dump_manager.h"
28 #include "base/trace_event/memory_dump_provider.h"
29 #include "base/trace_event/process_memory_dump.h"
24 #include "base/threading/thread_restrictions.h" 30 #include "base/threading/thread_restrictions.h"
25 #include "base/trace_event/trace_event.h" 31 #include "base/trace_event/trace_event.h"
26 #include "third_party/leveldatabase/chromium_logger.h" 32 #include "third_party/leveldatabase/chromium_logger.h"
33 #include "third_party/leveldatabase/src/include/leveldb/db.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
35 namespace leveldb_env { 42 namespace leveldb_env {
36 43
(...skipping 1027 matching lines...) Expand 10 before | Expand all | Expand 10 after
1064 return LEVELDB_STATUS_CORRUPTION; 1071 return LEVELDB_STATUS_CORRUPTION;
1065 if (s.IsNotSupportedError()) 1072 if (s.IsNotSupportedError())
1066 return LEVELDB_STATUS_NOT_SUPPORTED; 1073 return LEVELDB_STATUS_NOT_SUPPORTED;
1067 if (s.IsIOError()) 1074 if (s.IsIOError())
1068 return LEVELDB_STATUS_IO_ERROR; 1075 return LEVELDB_STATUS_IO_ERROR;
1069 // TODO(cmumford): IsInvalidArgument() was just added to leveldb. Use this 1076 // TODO(cmumford): IsInvalidArgument() was just added to leveldb. Use this
1070 // function once that change goes to the public repository. 1077 // function once that change goes to the public repository.
1071 return LEVELDB_STATUS_INVALID_ARGUMENT; 1078 return LEVELDB_STATUS_INVALID_ARGUMENT;
1072 } 1079 }
1073 1080
1081 class DBRegistry::DBProxy: public base::LinkNode<DBProxy>, public leveldb::DB {
1082 public:
1083 DBProxy(const leveldb::Options& options, const std::string name, leveldb::DB* db)
1084 : options_(options), name_(name), db_(db) {
1085 DBRegistry::GetInstance()->Register(this);
1086 }
1087
1088 ~DBProxy() override {
1089 DBRegistry::GetInstance()->Unregister(this);
1090 }
1091
1092 const leveldb::Options& options() const { return options_; };
1093
1094 const std::string& name() const { return name_; }
1095
1096 Status Put(const leveldb::WriteOptions& options,
1097 const leveldb::Slice& key,
1098 const leveldb::Slice& value) override {
1099 return db_->Put(options, key, value);
1100 }
1101
1102 Status Delete(const leveldb::WriteOptions& options,
1103 const leveldb::Slice& key) override {
1104 return db_->Delete(options, key);
1105 }
1106
1107 Status Write(const leveldb::WriteOptions& options, leveldb::WriteBatch* update s) override {
1108 return db_->Write(options, updates);
1109 }
1110
1111 Status Get(const leveldb::ReadOptions& options, const leveldb::Slice& key, std ::string* value) override {
1112 return db_->Get(options, key, value);
1113 }
1114
1115 leveldb::Iterator* NewIterator(const leveldb::ReadOptions& options) override {
1116 return db_->NewIterator(options);
1117 }
1118
1119 const leveldb::Snapshot* GetSnapshot() override {
1120 return db_->GetSnapshot();
1121 }
1122
1123 void ReleaseSnapshot(const leveldb::Snapshot* snapshot) override {
1124 return db_->ReleaseSnapshot(snapshot);
1125 }
1126
1127 bool GetProperty(const leveldb::Slice& property, std::string* value) override {
1128 return db_->GetProperty(property, value);
1129 }
1130
1131 void GetApproximateSizes(const leveldb::Range* range, int n, uint64_t* sizes) override {
1132 return db_->GetApproximateSizes(range, n, sizes);
1133 }
1134
1135 void CompactRange(const leveldb::Slice* begin, const leveldb::Slice* end) over ride {
1136 return db_->CompactRange(begin, end);
1137 }
1138
1139 private:
1140 leveldb::Options options_;
1141 std::string name_;
1142 std::unique_ptr<leveldb::DB> db_;
1143 };
1144
1145 class DBRegistry::MDP: public base::trace_event::MemoryDumpProvider {
1146 public:
1147 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
ssid 2017/05/10 23:52:52 If we are adding all the databases here then we sh
1148 base::trace_event::ProcessMemoryDump* pmd) override {
1149 auto db_dumper = [&](const leveldb::Options& options,
1150 const std::string& name,
1151 leveldb::DB* db) {
1152 std::string db_name;
1153 if (name.empty()) {
1154 db_name = "<in memory db>";
1155 } else {
1156 // XXX: 'name' is actually a filesystem path - figure a way to extract
1157 // db name only (last component doesn't cut it).
1158 //
1159 // For now we show the path, but replace slashes with something
1160 // that looks like a slash, to make memory-infra show it as a
1161 // string (and not as a tree).
1162 base::ReplaceChars(name, "/", "\u2215", &db_name);
1163 }
1164 std::string dump_name = base::StringPrintf(
1165 "leveldatabase/%s (0x%" PRIXPTR ")",
1166 db_name.c_str(),
1167 reinterpret_cast<uintptr_t>(db));
1168 auto* dump = pmd->CreateAllocatorDump(dump_name.c_str());
1169
1170 uint64_t memory_usage = 0;
1171 std::string memory_usage_string;
1172 bool got_memory_usage =
1173 db->GetProperty("leveldb.approximate-memory-usage", &memory_usage_stri ng) &&
1174 base::StringToUint64(memory_usage_string, &memory_usage);
1175 DCHECK(got_memory_usage);
1176
1177 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
1178 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
1179 memory_usage);
1180 };
1181
1182 DBRegistry::GetInstance()->Visit(db_dumper);
1183
1184 return true;
1185 }
1186 };
1187
1188 DBRegistry::DBRegistry(): mdp_(new MDP()) {
1189 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
1190 mdp_.get(), "LevelDB", nullptr);
1191 }
1192
1193 DBRegistry::~DBRegistry() = default;
1194
1195 DBRegistry* DBRegistry::GetInstance() {
1196 static DBRegistry* instance = new DBRegistry();
1197 return instance;
1198 }
1199
1200 leveldb::Status DBRegistry::Open(const leveldb::Options& options,
1201 const std::string& name,
1202 leveldb::DB** dbptr) {
1203 auto status = leveldb::DB::Open(options, name, dbptr);
1204 if (status.ok()) {
1205 *dbptr = new DBProxy(options, name, *dbptr);
1206 }
1207 return status;
1208 }
1209
1210 void DBRegistry::Visit(const Visitor& visitor) {
1211 base::AutoLock lock(lock_);
1212 for (auto* i = proxies_.head(); i != proxies_.end(); i = i->next()) {
1213 auto* proxy = i->value();
1214 visitor(proxy->options(), proxy->name(), proxy);
1215 }
1216 }
1217
1218 void DBRegistry::Register(DBProxy* proxy) {
1219 base::AutoLock lock(lock_);
1220 proxies_.Append(proxy);
1221 }
1222
1223 void DBRegistry::Unregister(DBProxy* proxy) {
1224 base::AutoLock lock(lock_);
1225 proxy->RemoveFromList();
1226 }
1227
1074 } // namespace leveldb_env 1228 } // namespace leveldb_env
1075 1229
1076 namespace leveldb { 1230 namespace leveldb {
1077 1231
1078 Env* Env::Default() { 1232 Env* Env::Default() {
1079 return leveldb_env::default_env.Pointer(); 1233 return leveldb_env::default_env.Pointer();
1080 } 1234 }
1081 1235
1082 } // namespace leveldb 1236 } // namespace leveldb
OLDNEW
« no previous file with comments | « third_party/leveldatabase/env_chromium.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698