Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium 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. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/leveldb_proto/leveldb_database.h" | 5 #include "components/leveldb_proto/leveldb_database.h" |
| 6 | 6 |
| 7 #include <inttypes.h> | |
| 8 | |
| 7 #include <string> | 9 #include <string> |
| 8 #include <vector> | 10 #include <vector> |
| 9 | 11 |
| 10 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 12 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/strings/string_number_conversions.h" | |
| 13 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
| 17 #include "base/strings/stringprintf.h" | |
| 18 #include "base/threading/sequenced_task_runner_handle.h" | |
| 14 #include "base/threading/thread_checker.h" | 19 #include "base/threading/thread_checker.h" |
| 20 #include "base/trace_event/memory_dump_manager.h" | |
| 21 #include "base/trace_event/process_memory_dump.h" | |
| 15 #include "third_party/leveldatabase/env_chromium.h" | 22 #include "third_party/leveldatabase/env_chromium.h" |
| 16 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 23 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
| 17 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 24 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 18 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 25 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
| 19 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" | 26 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" |
| 20 #include "third_party/leveldatabase/src/include/leveldb/options.h" | 27 #include "third_party/leveldatabase/src/include/leveldb/options.h" |
| 21 #include "third_party/leveldatabase/src/include/leveldb/slice.h" | 28 #include "third_party/leveldatabase/src/include/leveldb/slice.h" |
| 22 #include "third_party/leveldatabase/src/include/leveldb/status.h" | 29 #include "third_party/leveldatabase/src/include/leveldb/status.h" |
| 23 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 30 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 24 | 31 |
| 25 namespace leveldb_proto { | 32 namespace leveldb_proto { |
| 26 | 33 |
| 27 // static | 34 // static |
| 28 bool LevelDB::Destroy(const base::FilePath& database_dir) { | 35 bool LevelDB::Destroy(const base::FilePath& database_dir) { |
| 29 const leveldb::Status s = | 36 const leveldb::Status s = |
| 30 leveldb::DestroyDB(database_dir.AsUTF8Unsafe(), leveldb::Options()); | 37 leveldb::DestroyDB(database_dir.AsUTF8Unsafe(), leveldb::Options()); |
| 31 return s.ok(); | 38 return s.ok(); |
| 32 } | 39 } |
| 33 | 40 |
| 34 LevelDB::LevelDB(const char* client_name) : open_histogram_(nullptr) { | 41 LevelDB::LevelDB(const char* client_name) |
| 42 : open_histogram_(nullptr), client_name_(client_name) { | |
| 35 // Used in lieu of UMA_HISTOGRAM_ENUMERATION because the histogram name is | 43 // Used in lieu of UMA_HISTOGRAM_ENUMERATION because the histogram name is |
| 36 // not a constant. | 44 // not a constant. |
| 37 open_histogram_ = base::LinearHistogram::FactoryGet( | 45 open_histogram_ = base::LinearHistogram::FactoryGet( |
| 38 std::string("LevelDB.Open.") + client_name, 1, | 46 std::string("LevelDB.Open.") + client_name_, 1, |
| 39 leveldb_env::LEVELDB_STATUS_MAX, leveldb_env::LEVELDB_STATUS_MAX + 1, | 47 leveldb_env::LEVELDB_STATUS_MAX, leveldb_env::LEVELDB_STATUS_MAX + 1, |
| 40 base::Histogram::kUmaTargetedHistogramFlag); | 48 base::Histogram::kUmaTargetedHistogramFlag); |
| 41 } | 49 } |
| 42 | 50 |
| 43 LevelDB::~LevelDB() { | 51 LevelDB::~LevelDB() { |
| 44 DFAKE_SCOPED_LOCK(thread_checker_); | 52 DFAKE_SCOPED_LOCK(thread_checker_); |
| 53 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | |
|
nyquist
2016/09/21 06:04:16
is thiss safe to do even if Register was never cal
ssid
2016/09/21 19:08:50
yes.
| |
| 54 this); | |
| 45 } | 55 } |
| 46 | 56 |
| 47 bool LevelDB::InitWithOptions(const base::FilePath& database_dir, | 57 bool LevelDB::InitWithOptions(const base::FilePath& database_dir, |
| 48 const leveldb::Options& options) { | 58 const leveldb::Options& options) { |
| 49 DFAKE_SCOPED_LOCK(thread_checker_); | 59 DFAKE_SCOPED_LOCK(thread_checker_); |
| 50 | 60 |
| 51 std::string path = database_dir.AsUTF8Unsafe(); | 61 std::string path = database_dir.AsUTF8Unsafe(); |
| 52 | 62 |
| 53 leveldb::DB* db = NULL; | 63 leveldb::DB* db = NULL; |
| 54 leveldb::Status status = leveldb::DB::Open(options, path, &db); | 64 leveldb::Status status = leveldb::DB::Open(options, path, &db); |
| 55 if (open_histogram_) | 65 if (open_histogram_) |
| 56 open_histogram_->Add(leveldb_env::GetLevelDBStatusUMAValue(status)); | 66 open_histogram_->Add(leveldb_env::GetLevelDBStatusUMAValue(status)); |
| 57 if (status.IsCorruption()) { | 67 if (status.IsCorruption()) { |
| 58 base::DeleteFile(database_dir, true); | 68 base::DeleteFile(database_dir, true); |
| 59 status = leveldb::DB::Open(options, path, &db); | 69 status = leveldb::DB::Open(options, path, &db); |
| 60 } | 70 } |
| 61 | 71 |
| 62 if (status.ok()) { | 72 if (status.ok()) { |
| 63 CHECK(db); | 73 CHECK(db); |
| 64 db_.reset(db); | 74 db_.reset(db); |
| 75 | |
| 76 base::trace_event::MemoryDumpManager::GetInstance() | |
| 77 ->RegisterDumpProviderWithSequencedTaskRunner( | |
| 78 this, "LevelDB", base::SequencedTaskRunnerHandle::Get(), | |
| 79 base::trace_event::MemoryDumpProvider::Options()); | |
| 65 return true; | 80 return true; |
| 66 } | 81 } |
| 67 | 82 |
| 68 LOG(WARNING) << "Unable to open " << database_dir.value() << ": " | 83 LOG(WARNING) << "Unable to open " << database_dir.value() << ": " |
| 69 << status.ToString(); | 84 << status.ToString(); |
| 70 return false; | 85 return false; |
| 71 } | 86 } |
| 72 | 87 |
| 73 bool LevelDB::Init(const base::FilePath& database_dir) { | 88 bool LevelDB::Init(const base::FilePath& database_dir) { |
| 74 leveldb::Options options; | 89 leveldb::Options options; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 if (status.IsNotFound()) { | 152 if (status.IsNotFound()) { |
| 138 *found = false; | 153 *found = false; |
| 139 return true; | 154 return true; |
| 140 } | 155 } |
| 141 | 156 |
| 142 DLOG(WARNING) << "Failed loading leveldb_proto entry with key \"" << key | 157 DLOG(WARNING) << "Failed loading leveldb_proto entry with key \"" << key |
| 143 << "\": " << status.ToString(); | 158 << "\": " << status.ToString(); |
| 144 return false; | 159 return false; |
| 145 } | 160 } |
| 146 | 161 |
| 162 bool LevelDB::OnMemoryDump(const base::trace_event::MemoryDumpArgs& dump_args, | |
| 163 base::trace_event::ProcessMemoryDump* pmd) { | |
|
nyquist
2016/09/21 06:04:16
Is there any way of testing this?
ssid
2016/09/21 19:08:50
added unittest
| |
| 164 DFAKE_SCOPED_LOCK(thread_checker_); | |
| 165 if (!db_) | |
| 166 return false; | |
| 167 std::string value; | |
|
nyquist
2016/09/21 06:04:16
Nit: Could you add an empty line before this?
ssid
2016/09/21 19:08:50
Done.
| |
| 168 uint64_t size; | |
| 169 bool res = db_->GetProperty("leveldb.approximate-memory-usage", &value); | |
| 170 DCHECK(res); | |
| 171 res = base::StringToUint64(value, &size); | |
| 172 DCHECK(res); | |
| 173 | |
| 174 auto* dump = pmd->CreateAllocatorDump( | |
|
nyquist
2016/09/21 06:04:16
Could this just be base::MemoryAllocatorDump*? I d
ssid
2016/09/21 19:08:50
auto bcos base::trace_event::MemoryAllocatorDump w
| |
| 175 base::StringPrintf("leveldb/leveldb_proto/0x%" PRIXPTR, | |
| 176 reinterpret_cast<uintptr_t>(db_.get()))); | |
| 177 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
| 178 base::trace_event::MemoryAllocatorDump::kUnitsBytes, size); | |
| 179 if (!client_name_.empty() && | |
| 180 dump_args.level_of_detail != | |
| 181 base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND) { | |
| 182 dump->AddString("client_name", "", client_name_); | |
| 183 } | |
| 184 | |
| 185 // Memory is allocated from system allocator (malloc). | |
| 186 pmd->AddSuballocation(dump->guid(), | |
| 187 base::trace_event::MemoryDumpManager::GetInstance() | |
| 188 ->system_allocator_pool_name()); | |
| 189 return true; | |
| 190 } | |
| 191 | |
| 147 } // namespace leveldb_proto | 192 } // namespace leveldb_proto |
| OLD | NEW |