ssid | 3be5b1ec | 2016-01-13 14:21:57 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "sql/connection_memory_dump_provider.h" |
| 6 | |
ssid | 4205d7b | 2016-06-15 20:36:03 | [diff] [blame] | 7 | #include <inttypes.h> |
| 8 | |
ssid | 3be5b1ec | 2016-01-13 14:21:57 | [diff] [blame] | 9 | #include "base/strings/stringprintf.h" |
| 10 | #include "base/trace_event/process_memory_dump.h" |
| 11 | #include "third_party/sqlite/sqlite3.h" |
| 12 | |
| 13 | namespace sql { |
| 14 | |
| 15 | ConnectionMemoryDumpProvider::ConnectionMemoryDumpProvider( |
| 16 | sqlite3* db, |
| 17 | const std::string& name) |
| 18 | : db_(db), connection_name_(name) {} |
| 19 | |
Chris Watkins | cf617255 | 2017-11-27 03:25:18 | [diff] [blame] | 20 | ConnectionMemoryDumpProvider::~ConnectionMemoryDumpProvider() = default; |
ssid | 3be5b1ec | 2016-01-13 14:21:57 | [diff] [blame] | 21 | |
| 22 | void ConnectionMemoryDumpProvider::ResetDatabase() { |
| 23 | base::AutoLock lock(lock_); |
| 24 | db_ = nullptr; |
| 25 | } |
| 26 | |
| 27 | bool ConnectionMemoryDumpProvider::OnMemoryDump( |
| 28 | const base::trace_event::MemoryDumpArgs& args, |
| 29 | base::trace_event::ProcessMemoryDump* pmd) { |
| 30 | if (args.level_of_detail == base::trace_event::MemoryDumpLevelOfDetail::LIGHT) |
| 31 | return true; |
| 32 | |
| 33 | int cache_size = 0; |
| 34 | int schema_size = 0; |
| 35 | int statement_size = 0; |
dskiba | b4199f8 | 2016-11-21 20:16:13 | [diff] [blame] | 36 | if (!GetDbMemoryUsage(&cache_size, &schema_size, &statement_size)) { |
| 37 | return false; |
ssid | 3be5b1ec | 2016-01-13 14:21:57 | [diff] [blame] | 38 | } |
| 39 | |
dskiba | b4199f8 | 2016-11-21 20:16:13 | [diff] [blame] | 40 | base::trace_event::MemoryAllocatorDump* dump = |
| 41 | pmd->CreateAllocatorDump(FormatDumpName()); |
ssid | 3be5b1ec | 2016-01-13 14:21:57 | [diff] [blame] | 42 | dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 43 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 44 | cache_size + schema_size + statement_size); |
| 45 | dump->AddScalar("cache_size", |
| 46 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 47 | cache_size); |
| 48 | dump->AddScalar("schema_size", |
| 49 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 50 | schema_size); |
| 51 | dump->AddScalar("statement_size", |
| 52 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 53 | statement_size); |
| 54 | return true; |
| 55 | } |
| 56 | |
dskiba | b4199f8 | 2016-11-21 20:16:13 | [diff] [blame] | 57 | bool ConnectionMemoryDumpProvider::ReportMemoryUsage( |
ssid | 1f4e536 | 2016-12-08 20:41:38 | [diff] [blame] | 58 | base::trace_event::ProcessMemoryDump* pmd, |
| 59 | const std::string& dump_name) { |
dskiba | b4199f8 | 2016-11-21 20:16:13 | [diff] [blame] | 60 | int cache_size = 0; |
| 61 | int schema_size = 0; |
| 62 | int statement_size = 0; |
ssid | 1f4e536 | 2016-12-08 20:41:38 | [diff] [blame] | 63 | if (!GetDbMemoryUsage(&cache_size, &schema_size, &statement_size)) |
dskiba | b4199f8 | 2016-11-21 20:16:13 | [diff] [blame] | 64 | return false; |
dskiba | b4199f8 | 2016-11-21 20:16:13 | [diff] [blame] | 65 | |
ssid | 1f4e536 | 2016-12-08 20:41:38 | [diff] [blame] | 66 | auto* mad = pmd->CreateAllocatorDump(dump_name); |
dskiba | b4199f8 | 2016-11-21 20:16:13 | [diff] [blame] | 67 | mad->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 68 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 69 | cache_size + schema_size + statement_size); |
ssid | 1f4e536 | 2016-12-08 20:41:38 | [diff] [blame] | 70 | pmd->AddSuballocation(mad->guid(), FormatDumpName()); |
dskiba | b4199f8 | 2016-11-21 20:16:13 | [diff] [blame] | 71 | |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | bool ConnectionMemoryDumpProvider::GetDbMemoryUsage(int* cache_size, |
| 76 | int* schema_size, |
| 77 | int* statement_size) { |
| 78 | // Lock is acquired here so that db_ is not reset in ResetDatabase when |
| 79 | // collecting stats. |
| 80 | base::AutoLock lock(lock_); |
| 81 | if (!db_) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | // The high water mark is not tracked for the following usages. |
| 86 | int dummy_int; |
| 87 | int status = sqlite3_db_status(db_, SQLITE_DBSTATUS_CACHE_USED, cache_size, |
| 88 | &dummy_int, 0 /* resetFlag */); |
| 89 | DCHECK_EQ(SQLITE_OK, status); |
| 90 | status = sqlite3_db_status(db_, SQLITE_DBSTATUS_SCHEMA_USED, schema_size, |
| 91 | &dummy_int, 0 /* resetFlag */); |
| 92 | DCHECK_EQ(SQLITE_OK, status); |
| 93 | status = sqlite3_db_status(db_, SQLITE_DBSTATUS_STMT_USED, statement_size, |
| 94 | &dummy_int, 0 /* resetFlag */); |
| 95 | DCHECK_EQ(SQLITE_OK, status); |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | std::string ConnectionMemoryDumpProvider::FormatDumpName() const { |
| 101 | return base::StringPrintf( |
| 102 | "sqlite/%s_connection/0x%" PRIXPTR, |
| 103 | connection_name_.empty() ? "Unknown" : connection_name_.c_str(), |
| 104 | reinterpret_cast<uintptr_t>(this)); |
| 105 | } |
| 106 | |
ssid | 3be5b1ec | 2016-01-13 14:21:57 | [diff] [blame] | 107 | } // namespace sql |