| 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( |
| 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) { |
| 164 DFAKE_SCOPED_LOCK(thread_checker_); |
| 165 if (!db_) |
| 166 return false; |
| 167 |
| 168 std::string value; |
| 169 uint64_t size; |
| 170 bool res = db_->GetProperty("leveldb.approximate-memory-usage", &value); |
| 171 DCHECK(res); |
| 172 res = base::StringToUint64(value, &size); |
| 173 DCHECK(res); |
| 174 |
| 175 base::trace_event::MemoryAllocatorDump* dump = pmd->CreateAllocatorDump( |
| 176 base::StringPrintf("leveldb/leveldb_proto/0x%" PRIXPTR, |
| 177 reinterpret_cast<uintptr_t>(db_.get()))); |
| 178 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 179 base::trace_event::MemoryAllocatorDump::kUnitsBytes, size); |
| 180 if (!client_name_.empty() && |
| 181 dump_args.level_of_detail != |
| 182 base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND) { |
| 183 dump->AddString("client_name", "", client_name_); |
| 184 } |
| 185 |
| 186 // Memory is allocated from system allocator (malloc). |
| 187 const char* system_allocator_pool_name = |
| 188 base::trace_event::MemoryDumpManager::GetInstance() |
| 189 ->system_allocator_pool_name(); |
| 190 if (system_allocator_pool_name) |
| 191 pmd->AddSuballocation(dump->guid(), system_allocator_pool_name); |
| 192 |
| 193 return true; |
| 194 } |
| 195 |
| 147 } // namespace leveldb_proto | 196 } // namespace leveldb_proto |
| OLD | NEW |