Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_functions.h" | 20 #include "base/metrics/histogram_functions.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" |
| 23 #include "base/strings/string_number_conversions.h" | |
| 21 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 22 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
| 24 #include "base/threading/thread_restrictions.h" | 27 #include "base/threading/thread_restrictions.h" |
| 28 #include "base/trace_event/memory_dump_manager.h" | |
| 29 #include "base/trace_event/memory_dump_provider.h" | |
| 30 #include "base/trace_event/process_memory_dump.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" |
| 27 #include "third_party/leveldatabase/src/include/leveldb/options.h" | 33 #include "third_party/leveldatabase/src/include/leveldb/options.h" |
| 28 #include "third_party/re2/src/re2/re2.h" | 34 #include "third_party/re2/src/re2/re2.h" |
| 29 | 35 |
| 30 using base::FilePath; | 36 using base::FilePath; |
| 31 using leveldb::FileLock; | 37 using leveldb::FileLock; |
| 32 using leveldb::Slice; | 38 using leveldb::Slice; |
| 33 using leveldb::Status; | 39 using leveldb::Status; |
| 34 | 40 |
| (...skipping 1053 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1088 return LEVELDB_STATUS_CORRUPTION; | 1094 return LEVELDB_STATUS_CORRUPTION; |
| 1089 if (s.IsNotSupportedError()) | 1095 if (s.IsNotSupportedError()) |
| 1090 return LEVELDB_STATUS_NOT_SUPPORTED; | 1096 return LEVELDB_STATUS_NOT_SUPPORTED; |
| 1091 if (s.IsIOError()) | 1097 if (s.IsIOError()) |
| 1092 return LEVELDB_STATUS_IO_ERROR; | 1098 return LEVELDB_STATUS_IO_ERROR; |
| 1093 // TODO(cmumford): IsInvalidArgument() was just added to leveldb. Use this | 1099 // TODO(cmumford): IsInvalidArgument() was just added to leveldb. Use this |
| 1094 // function once that change goes to the public repository. | 1100 // function once that change goes to the public repository. |
| 1095 return LEVELDB_STATUS_INVALID_ARGUMENT; | 1101 return LEVELDB_STATUS_INVALID_ARGUMENT; |
| 1096 } | 1102 } |
| 1097 | 1103 |
| 1104 // DBRegistry's leveldb::DB wrapper. Forwards all calls to the underlying | |
| 1105 // leveldb::DB instance. Registers / unregisters itself in the registry. | |
| 1106 class DBRegistry::DBWrapperImpl : public base::LinkNode<DBWrapperImpl>, | |
| 1107 public DBWrapper { | |
| 1108 public: | |
| 1109 DBWrapperImpl(const std::string name, leveldb::DB* db) | |
| 1110 : name_(name), db_(db) { | |
| 1111 DBRegistry::GetInstance()->RegisterDatabase(this); | |
| 1112 } | |
| 1113 | |
| 1114 ~DBWrapperImpl() override { | |
| 1115 DBRegistry::GetInstance()->UnregisterDatabase(this); | |
| 1116 } | |
| 1117 | |
| 1118 const std::string& name() const override { return name_; } | |
| 1119 | |
| 1120 leveldb::Status Put(const leveldb::WriteOptions& options, | |
| 1121 const leveldb::Slice& key, | |
| 1122 const leveldb::Slice& value) override { | |
| 1123 return db_->Put(options, key, value); | |
| 1124 } | |
| 1125 | |
| 1126 leveldb::Status Delete(const leveldb::WriteOptions& options, | |
| 1127 const leveldb::Slice& key) override { | |
| 1128 return db_->Delete(options, key); | |
| 1129 } | |
| 1130 | |
| 1131 leveldb::Status Write(const leveldb::WriteOptions& options, | |
| 1132 leveldb::WriteBatch* updates) override { | |
| 1133 return db_->Write(options, updates); | |
| 1134 } | |
| 1135 | |
| 1136 leveldb::Status Get(const leveldb::ReadOptions& options, | |
| 1137 const leveldb::Slice& key, | |
| 1138 std::string* value) override { | |
| 1139 return db_->Get(options, key, value); | |
| 1140 } | |
| 1141 | |
| 1142 const leveldb::Snapshot* GetSnapshot() override { return db_->GetSnapshot(); } | |
| 1143 | |
| 1144 void ReleaseSnapshot(const leveldb::Snapshot* snapshot) override { | |
| 1145 return db_->ReleaseSnapshot(snapshot); | |
| 1146 } | |
| 1147 | |
| 1148 bool GetProperty(const leveldb::Slice& property, | |
| 1149 std::string* value) override { | |
| 1150 return db_->GetProperty(property, value); | |
| 1151 } | |
| 1152 | |
| 1153 void GetApproximateSizes(const leveldb::Range* range, | |
| 1154 int n, | |
| 1155 uint64_t* sizes) override { | |
| 1156 return db_->GetApproximateSizes(range, n, sizes); | |
| 1157 } | |
| 1158 | |
| 1159 void CompactRange(const leveldb::Slice* begin, | |
| 1160 const leveldb::Slice* end) override { | |
| 1161 return db_->CompactRange(begin, end); | |
| 1162 } | |
| 1163 | |
| 1164 leveldb::Iterator* NewIterator(const leveldb::ReadOptions& options) override { | |
| 1165 return db_->NewIterator(options); | |
| 1166 } | |
| 1167 | |
| 1168 private: | |
| 1169 std::string name_; | |
| 1170 std::unique_ptr<leveldb::DB> db_; | |
| 1171 }; | |
| 1172 | |
| 1173 // DBRegistry MemoryDumpProvider implementation. Declared here to minimize | |
| 1174 // dependencies in the header file. | |
| 1175 class DBRegistry::MDP : public base::trace_event::MemoryDumpProvider { | |
| 1176 public: | |
| 1177 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, | |
| 1178 base::trace_event::ProcessMemoryDump* pmd) override { | |
| 1179 auto db_visitor = [&](DBWrapper* db) { | |
| 1180 std::string db_dump_name = base::StringPrintf( | |
| 1181 "leveldatabase/0x%" PRIXPTR, reinterpret_cast<uintptr_t>(db)); | |
|
Marijn Kruisselbrink
2017/06/20 23:38:52
nit: should this be leveldb/ something to be consi
ssid
2017/06/20 23:52:13
I think it is intentional.
How about leveldb_regis
DmitrySkiba
2017/06/20 23:59:21
Yes, the name is intentionally different. Some of
| |
| 1182 auto* db_dump = pmd->CreateAllocatorDump(db_dump_name.c_str()); | |
| 1183 | |
| 1184 uint64_t db_memory_usage = 0; | |
| 1185 { | |
| 1186 std::string usage_string; | |
| 1187 bool success = db->GetProperty("leveldb.approximate-memory-usage", | |
| 1188 &usage_string) && | |
| 1189 base::StringToUint64(usage_string, &db_memory_usage); | |
| 1190 DCHECK(success); | |
| 1191 } | |
| 1192 db_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
| 1193 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | |
| 1194 db_memory_usage); | |
| 1195 | |
| 1196 if (args.level_of_detail != | |
| 1197 base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND) { | |
| 1198 db_dump->AddString("name", "", db->name()); | |
| 1199 } | |
| 1200 | |
| 1201 const char* system_allocator_name = | |
| 1202 base::trace_event::MemoryDumpManager::GetInstance() | |
| 1203 ->system_allocator_pool_name(); | |
| 1204 if (system_allocator_name) { | |
| 1205 pmd->AddSuballocation(db_dump->guid(), system_allocator_name); | |
| 1206 } | |
| 1207 }; | |
| 1208 | |
| 1209 DBRegistry::GetInstance()->VisitDatabases(db_visitor); | |
| 1210 return true; | |
| 1211 } | |
| 1212 }; | |
| 1213 | |
| 1214 DBRegistry::DBRegistry() : mdp_(new MDP()) { | |
| 1215 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | |
| 1216 mdp_.get(), "LevelDB", nullptr); | |
| 1217 } | |
| 1218 | |
| 1219 DBRegistry::~DBRegistry() { | |
| 1220 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | |
|
ssid
2017/06/20 23:52:13
You cannot unregister dump providers without task
DmitrySkiba
2017/06/21 01:08:06
Ouch, I didn't know about that. I wonder if Unregi
| |
| 1221 mdp_.get()); | |
| 1222 } | |
| 1223 | |
| 1224 DBRegistry* DBRegistry::GetInstance() { | |
| 1225 static DBRegistry* instance = new DBRegistry(); | |
| 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 |
| OLD | NEW |