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.
5 #include "sql/database_memory_dump_provider.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/trace_event/process_memory_dump.h"
11 #include "third_party/sqlite/sqlite3.h"
15 DatabaseMemoryDumpProvider::DatabaseMemoryDumpProvider(sqlite3* db,
16 const std::string& name)
17 : db_(db), connection_name_(name) {}
19 DatabaseMemoryDumpProvider::~DatabaseMemoryDumpProvider() = default;
21 void DatabaseMemoryDumpProvider::ResetDatabase() {
22 base::AutoLock lock(lock_);
26 bool DatabaseMemoryDumpProvider::OnMemoryDump(
27 const base::trace_event::MemoryDumpArgs& args,
28 base::trace_event::ProcessMemoryDump* pmd) {
29 if (args.level_of_detail == base::trace_event::MemoryDumpLevelOfDetail::LIGHT)
34 int statement_size = 0;
35 if (!GetDbMemoryUsage(&cache_size, &schema_size, &statement_size)) {
39 base::trace_event::MemoryAllocatorDump* dump =
40 pmd->CreateAllocatorDump(FormatDumpName());
41 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
42 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
43 cache_size + schema_size + statement_size);
44 dump->AddScalar("cache_size",
45 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
47 dump->AddScalar("schema_size",
48 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
50 dump->AddScalar("statement_size",
51 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
56 bool DatabaseMemoryDumpProvider::ReportMemoryUsage(
57 base::trace_event::ProcessMemoryDump* pmd,
58 const std::string& dump_name) {
61 int statement_size = 0;
62 if (!GetDbMemoryUsage(&cache_size, &schema_size, &statement_size))
65 auto* mad = pmd->CreateAllocatorDump(dump_name);
66 mad->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
67 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
68 cache_size + schema_size + statement_size);
69 pmd->AddSuballocation(mad->guid(), FormatDumpName());
74 bool DatabaseMemoryDumpProvider::GetDbMemoryUsage(int* cache_size,
76 int* statement_size) {
77 // Lock is acquired here so that db_ is not reset in ResetDatabase when
79 base::AutoLock lock(lock_);
84 // The high water mark is not tracked for the following usages.
86 int status = sqlite3_db_status(db_, SQLITE_DBSTATUS_CACHE_USED, cache_size,
87 &dummy_int, 0 /* resetFlag */);
88 DCHECK_EQ(SQLITE_OK, status);
89 status = sqlite3_db_status(db_, SQLITE_DBSTATUS_SCHEMA_USED, schema_size,
90 &dummy_int, 0 /* resetFlag */);
91 DCHECK_EQ(SQLITE_OK, status);
92 status = sqlite3_db_status(db_, SQLITE_DBSTATUS_STMT_USED, statement_size,
93 &dummy_int, 0 /* resetFlag */);
94 DCHECK_EQ(SQLITE_OK, status);
99 std::string DatabaseMemoryDumpProvider::FormatDumpName() const {
100 return base::StringPrintf(
101 "sqlite/%s_connection/0x%" PRIXPTR,
102 connection_name_.empty() ? "Unknown" : connection_name_.c_str(),
103 reinterpret_cast<uintptr_t>(this));