Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "content/browser/indexed_db/leveldb/leveldb_database.h" | 5 #include "content/browser/indexed_db/leveldb/leveldb_database.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <cerrno> | 8 #include <cerrno> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
| 17 #include "base/strings/string_number_conversions.h" | |
| 17 #include "base/strings/string_piece.h" | 18 #include "base/strings/string_piece.h" |
| 18 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/sys_info.h" | 21 #include "base/sys_info.h" |
| 22 #include "base/trace_event/memory_dump_manager.h" | |
| 23 #include "base/trace_event/process_memory_dump.h" | |
| 21 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 22 #include "content/browser/indexed_db/indexed_db_class_factory.h" | 25 #include "content/browser/indexed_db/indexed_db_class_factory.h" |
| 23 #include "content/browser/indexed_db/indexed_db_tracing.h" | 26 #include "content/browser/indexed_db/indexed_db_tracing.h" |
| 24 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" | 27 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" |
| 25 #include "content/browser/indexed_db/leveldb/leveldb_env.h" | 28 #include "content/browser/indexed_db/leveldb/leveldb_env.h" |
| 26 #include "content/browser/indexed_db/leveldb/leveldb_iterator_impl.h" | 29 #include "content/browser/indexed_db/leveldb/leveldb_iterator_impl.h" |
| 27 #include "content/browser/indexed_db/leveldb/leveldb_write_batch.h" | 30 #include "content/browser/indexed_db/leveldb/leveldb_write_batch.h" |
| 28 #include "third_party/leveldatabase/env_chromium.h" | 31 #include "third_party/leveldatabase/env_chromium.h" |
| 29 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 32 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
| 30 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 33 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 std::string* key) const {} | 85 std::string* key) const {} |
| 83 | 86 |
| 84 LevelDBSnapshot::LevelDBSnapshot(LevelDBDatabase* db) | 87 LevelDBSnapshot::LevelDBSnapshot(LevelDBDatabase* db) |
| 85 : db_(db->db_.get()), snapshot_(db_->GetSnapshot()) {} | 88 : db_(db->db_.get()), snapshot_(db_->GetSnapshot()) {} |
| 86 | 89 |
| 87 LevelDBSnapshot::~LevelDBSnapshot() { db_->ReleaseSnapshot(snapshot_); } | 90 LevelDBSnapshot::~LevelDBSnapshot() { db_->ReleaseSnapshot(snapshot_); } |
| 88 | 91 |
| 89 LevelDBDatabase::LevelDBDatabase() {} | 92 LevelDBDatabase::LevelDBDatabase() {} |
| 90 | 93 |
| 91 LevelDBDatabase::~LevelDBDatabase() { | 94 LevelDBDatabase::~LevelDBDatabase() { |
| 95 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | |
| 96 this); | |
| 92 // db_'s destructor uses comparator_adapter_; order of deletion is important. | 97 // db_'s destructor uses comparator_adapter_; order of deletion is important. |
| 93 CloseDatabase(); | 98 CloseDatabase(); |
| 94 comparator_adapter_.reset(); | 99 comparator_adapter_.reset(); |
| 95 env_.reset(); | 100 env_.reset(); |
| 96 } | 101 } |
| 97 | 102 |
| 98 void LevelDBDatabase::CloseDatabase() { | 103 void LevelDBDatabase::CloseDatabase() { |
| 99 if (db_) { | 104 if (db_) { |
| 100 base::TimeTicks begin_time = base::TimeTicks::Now(); | 105 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 101 db_.reset(); | 106 db_.reset(); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 304 UMA_HISTOGRAM_MEDIUM_TIMES("WebCore.IndexedDB.LevelDB.OpenTime", | 309 UMA_HISTOGRAM_MEDIUM_TIMES("WebCore.IndexedDB.LevelDB.OpenTime", |
| 305 base::TimeTicks::Now() - begin_time); | 310 base::TimeTicks::Now() - begin_time); |
| 306 | 311 |
| 307 CheckFreeSpace("Success", file_name); | 312 CheckFreeSpace("Success", file_name); |
| 308 | 313 |
| 309 (*result).reset(new LevelDBDatabase); | 314 (*result).reset(new LevelDBDatabase); |
| 310 (*result)->db_ = make_scoped_ptr(db); | 315 (*result)->db_ = make_scoped_ptr(db); |
| 311 (*result)->comparator_adapter_ = std::move(comparator_adapter); | 316 (*result)->comparator_adapter_ = std::move(comparator_adapter); |
| 312 (*result)->comparator_ = comparator; | 317 (*result)->comparator_ = comparator; |
| 313 (*result)->filter_policy_ = std::move(filter_policy); | 318 (*result)->filter_policy_ = std::move(filter_policy); |
| 319 (*result)->file_name_for_tracing = file_name.BaseName().AsUTF8Unsafe(); | |
| 314 | 320 |
| 315 return s; | 321 return s; |
| 316 } | 322 } |
| 317 | 323 |
| 318 scoped_ptr<LevelDBDatabase> LevelDBDatabase::OpenInMemory( | 324 scoped_ptr<LevelDBDatabase> LevelDBDatabase::OpenInMemory( |
| 319 const LevelDBComparator* comparator) { | 325 const LevelDBComparator* comparator) { |
| 320 scoped_ptr<ComparatorAdapter> comparator_adapter( | 326 scoped_ptr<ComparatorAdapter> comparator_adapter( |
| 321 new ComparatorAdapter(comparator)); | 327 new ComparatorAdapter(comparator)); |
| 322 scoped_ptr<leveldb::Env> in_memory_env(leveldb::NewMemEnv(LevelDBEnv::Get())); | 328 scoped_ptr<leveldb::Env> in_memory_env(leveldb::NewMemEnv(LevelDBEnv::Get())); |
| 323 | 329 |
| 324 leveldb::DB* db; | 330 leveldb::DB* db; |
| 325 scoped_ptr<const leveldb::FilterPolicy> filter_policy; | 331 scoped_ptr<const leveldb::FilterPolicy> filter_policy; |
| 326 const leveldb::Status s = OpenDB(comparator_adapter.get(), | 332 const leveldb::Status s = OpenDB(comparator_adapter.get(), |
| 327 in_memory_env.get(), | 333 in_memory_env.get(), |
| 328 base::FilePath(), | 334 base::FilePath(), |
| 329 &db, | 335 &db, |
| 330 &filter_policy); | 336 &filter_policy); |
| 331 | 337 |
| 332 if (!s.ok()) { | 338 if (!s.ok()) { |
| 333 LOG(ERROR) << "Failed to open in-memory LevelDB database: " << s.ToString(); | 339 LOG(ERROR) << "Failed to open in-memory LevelDB database: " << s.ToString(); |
| 334 return scoped_ptr<LevelDBDatabase>(); | 340 return scoped_ptr<LevelDBDatabase>(); |
| 335 } | 341 } |
| 336 | 342 |
| 337 scoped_ptr<LevelDBDatabase> result(new LevelDBDatabase); | 343 scoped_ptr<LevelDBDatabase> result(new LevelDBDatabase); |
| 338 result->env_ = std::move(in_memory_env); | 344 result->env_ = std::move(in_memory_env); |
| 339 result->db_ = make_scoped_ptr(db); | 345 result->db_ = make_scoped_ptr(db); |
| 340 result->comparator_adapter_ = std::move(comparator_adapter); | 346 result->comparator_adapter_ = std::move(comparator_adapter); |
| 341 result->comparator_ = comparator; | 347 result->comparator_ = comparator; |
| 342 result->filter_policy_ = std::move(filter_policy); | 348 result->filter_policy_ = std::move(filter_policy); |
| 349 result->file_name_for_tracing = "in-memory-database"; | |
| 343 | 350 |
| 344 return result; | 351 return result; |
| 345 } | 352 } |
| 346 | 353 |
| 347 leveldb::Status LevelDBDatabase::Put(const StringPiece& key, | 354 leveldb::Status LevelDBDatabase::Put(const StringPiece& key, |
| 348 std::string* value) { | 355 std::string* value) { |
| 349 base::TimeTicks begin_time = base::TimeTicks::Now(); | 356 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 350 | 357 |
| 351 leveldb::WriteOptions write_options; | 358 leveldb::WriteOptions write_options; |
| 352 write_options.sync = kSyncWrites; | 359 write_options.sync = kSyncWrites; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 431 IDB_TRACE("LevelDBDatabase::Compact"); | 438 IDB_TRACE("LevelDBDatabase::Compact"); |
| 432 const leveldb::Slice start_slice = MakeSlice(start); | 439 const leveldb::Slice start_slice = MakeSlice(start); |
| 433 const leveldb::Slice stop_slice = MakeSlice(stop); | 440 const leveldb::Slice stop_slice = MakeSlice(stop); |
| 434 // NULL batch means just wait for earlier writes to be done | 441 // NULL batch means just wait for earlier writes to be done |
| 435 db_->Write(leveldb::WriteOptions(), NULL); | 442 db_->Write(leveldb::WriteOptions(), NULL); |
| 436 db_->CompactRange(&start_slice, &stop_slice); | 443 db_->CompactRange(&start_slice, &stop_slice); |
| 437 } | 444 } |
| 438 | 445 |
| 439 void LevelDBDatabase::CompactAll() { db_->CompactRange(NULL, NULL); } | 446 void LevelDBDatabase::CompactAll() { db_->CompactRange(NULL, NULL); } |
| 440 | 447 |
| 448 bool LevelDBDatabase::OnMemoryDump( | |
| 449 const base::trace_event::MemoryDumpArgs& args, | |
| 450 base::trace_event::ProcessMemoryDump* pmd) { | |
| 451 if (!db_) | |
| 452 return false; | |
| 453 | |
| 454 std::string value; | |
| 455 uint64_t size; | |
| 456 bool res = db_->GetProperty("leveldb.approximate-memory-usage", &value); | |
| 457 DCHECK(res); | |
| 458 base::StringToUint64(value, &size); | |
| 459 | |
| 460 auto dump = pmd->CreateAllocatorDump( | |
| 461 base::StringPrintf("leveldb/index_db/%p", db_.get())); | |
|
Primiano Tucci (use gerrit)
2016/03/02 06:21:55
out of curiosity, why don't we name it just level_
ssid
2016/03/02 23:17:50
If file name is long, which is taken from websites
| |
| 462 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
| 463 base::trace_event::MemoryAllocatorDump::kUnitsBytes, size); | |
| 464 dump->AddString("file_name", "", file_name_for_tracing); | |
| 465 | |
| 466 // Memory is allocated from system allocator (malloc). | |
| 467 pmd->AddSuballocation(dump->guid(), | |
| 468 base::trace_event::MemoryDumpManager::GetInstance() | |
| 469 ->system_allocator_pool_name()); | |
| 470 return true; | |
| 471 } | |
| 472 | |
| 441 } // namespace content | 473 } // namespace content |
| OLD | NEW |