blob: 23a90450eba44d53c7bbaa8a6032d51677134385 [file] [log] [blame]
ssid3be5b1ec2016-01-13 14:21:571// 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
ssid4205d7b2016-06-15 20:36:037#include <inttypes.h>
8
ssid3be5b1ec2016-01-13 14:21:579#include "base/strings/stringprintf.h"
10#include "base/trace_event/process_memory_dump.h"
11#include "third_party/sqlite/sqlite3.h"
12
13namespace sql {
14
15ConnectionMemoryDumpProvider::ConnectionMemoryDumpProvider(
16 sqlite3* db,
17 const std::string& name)
18 : db_(db), connection_name_(name) {}
19
20ConnectionMemoryDumpProvider::~ConnectionMemoryDumpProvider() {}
21
22void ConnectionMemoryDumpProvider::ResetDatabase() {
23 base::AutoLock lock(lock_);
24 db_ = nullptr;
25}
26
27bool 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;
dskibab4199f82016-11-21 20:16:1336 if (!GetDbMemoryUsage(&cache_size, &schema_size, &statement_size)) {
37 return false;
ssid3be5b1ec2016-01-13 14:21:5738 }
39
dskibab4199f82016-11-21 20:16:1340 base::trace_event::MemoryAllocatorDump* dump =
41 pmd->CreateAllocatorDump(FormatDumpName());
ssid3be5b1ec2016-01-13 14:21:5742 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
dskibab4199f82016-11-21 20:16:1357bool ConnectionMemoryDumpProvider::ReportMemoryUsage(
58 base::trace_event::MemoryAllocatorDump* mad) {
59 int cache_size = 0;
60 int schema_size = 0;
61 int statement_size = 0;
62 if (!GetDbMemoryUsage(&cache_size, &schema_size, &statement_size)) {
63 return false;
64 }
65
66 mad->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
67 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
68 cache_size + schema_size + statement_size);
69 mad->process_memory_dump()->AddSuballocation(mad->guid(), FormatDumpName());
70
71 return true;
72}
73
74bool ConnectionMemoryDumpProvider::GetDbMemoryUsage(int* cache_size,
75 int* schema_size,
76 int* statement_size) {
77 // Lock is acquired here so that db_ is not reset in ResetDatabase when
78 // collecting stats.
79 base::AutoLock lock(lock_);
80 if (!db_) {
81 return false;
82 }
83
84 // The high water mark is not tracked for the following usages.
85 int dummy_int;
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);
95
96 return true;
97}
98
99std::string ConnectionMemoryDumpProvider::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));
104}
105
ssid3be5b1ec2016-01-13 14:21:57106} // namespace sql