blob: eaee9b43e3bb8a7601aa32194d09f4cd504d7fcb [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;
36 {
37 // Lock is acquired here so that db_ is not reset in ResetDatabase when
38 // collecting stats.
39 base::AutoLock lock(lock_);
40 if (!db_) {
41 return false;
42 }
43
44 // The high water mark is not tracked for the following usages.
45 int dummy_int;
46 int status = sqlite3_db_status(db_, SQLITE_DBSTATUS_CACHE_USED, &cache_size,
47 &dummy_int, 0 /* resetFlag */);
48 DCHECK_EQ(SQLITE_OK, status);
49 status = sqlite3_db_status(db_, SQLITE_DBSTATUS_SCHEMA_USED, &schema_size,
50 &dummy_int, 0 /* resetFlag */);
51 DCHECK_EQ(SQLITE_OK, status);
52 status = sqlite3_db_status(db_, SQLITE_DBSTATUS_STMT_USED, &statement_size,
53 &dummy_int, 0 /* resetFlag */);
54 DCHECK_EQ(SQLITE_OK, status);
55 }
56
57 std::string name = base::StringPrintf(
ssid4205d7b2016-06-15 20:36:0358 "sqlite/%s_connection/0x%" PRIXPTR,
59 connection_name_.empty() ? "Unknown" : connection_name_.c_str(),
60 reinterpret_cast<uintptr_t>(this));
ssid3be5b1ec2016-01-13 14:21:5761 base::trace_event::MemoryAllocatorDump* dump = pmd->CreateAllocatorDump(name);
62 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
63 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
64 cache_size + schema_size + statement_size);
65 dump->AddScalar("cache_size",
66 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
67 cache_size);
68 dump->AddScalar("schema_size",
69 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
70 schema_size);
71 dump->AddScalar("statement_size",
72 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
73 statement_size);
74 return true;
75}
76
77} // namespace sql