ssid | 9f8022f | 2015-10-12 17:49:03 | [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/sql_memory_dump_provider.h" |
| 6 | |
| 7 | #include "base/trace_event/memory_dump_manager.h" |
| 8 | #include "base/trace_event/process_memory_dump.h" |
Victor Costan | e60db54 | 2022-03-22 05:33:33 | [diff] [blame] | 9 | #include "sql/sqlite_result_code.h" |
| 10 | #include "sql/sqlite_result_code_values.h" |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 11 | #include "third_party/sqlite/sqlite3.h" |
| 12 | |
| 13 | namespace sql { |
| 14 | |
| 15 | // static |
| 16 | SqlMemoryDumpProvider* SqlMemoryDumpProvider::GetInstance() { |
| 17 | return base::Singleton< |
| 18 | SqlMemoryDumpProvider, |
| 19 | base::LeakySingletonTraits<SqlMemoryDumpProvider>>::get(); |
| 20 | } |
| 21 | |
Chris Watkins | cf617255 | 2017-11-27 03:25:18 | [diff] [blame] | 22 | SqlMemoryDumpProvider::SqlMemoryDumpProvider() = default; |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 23 | |
Chris Watkins | cf617255 | 2017-11-27 03:25:18 | [diff] [blame] | 24 | SqlMemoryDumpProvider::~SqlMemoryDumpProvider() = default; |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 25 | |
| 26 | bool SqlMemoryDumpProvider::OnMemoryDump( |
| 27 | const base::trace_event::MemoryDumpArgs& args, |
| 28 | base::trace_event::ProcessMemoryDump* pmd) { |
Victor Costan | ccc76472 | 2018-07-23 21:15:09 | [diff] [blame] | 29 | sqlite3_int64 memory_used = 0; |
| 30 | sqlite3_int64 memory_high_water = 0; |
Victor Costan | e60db54 | 2022-03-22 05:33:33 | [diff] [blame] | 31 | auto sqlite_result_code = ToSqliteResultCode(sqlite3_status64( |
| 32 | SQLITE_STATUS_MEMORY_USED, &memory_used, &memory_high_water, |
| 33 | /*resetFlag=*/1)); |
| 34 | DCHECK_EQ(sqlite_result_code, SqliteResultCode::kOk) |
| 35 | << "sqlite3_status64(SQLITE_STATUS_MEMORY_USED) failed"; |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 36 | |
| 37 | base::trace_event::MemoryAllocatorDump* dump = |
| 38 | pmd->CreateAllocatorDump("sqlite"); |
| 39 | dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 40 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 41 | memory_used); |
| 42 | dump->AddScalar("malloc_high_wmark_size", |
| 43 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 44 | memory_high_water); |
| 45 | |
Victor Costan | ccc76472 | 2018-07-23 21:15:09 | [diff] [blame] | 46 | sqlite3_int64 dummy_high_water = -1; |
| 47 | sqlite3_int64 malloc_count = -1; |
Victor Costan | e60db54 | 2022-03-22 05:33:33 | [diff] [blame] | 48 | sqlite_result_code = ToSqliteResultCode(sqlite3_status64( |
| 49 | SQLITE_STATUS_MALLOC_COUNT, &malloc_count, &dummy_high_water, |
| 50 | /*resetFlag=*/0)); |
| 51 | DCHECK_EQ(sqlite_result_code, SqliteResultCode::kOk) |
| 52 | << "sqlite3_status64(SQLITE_STATUS_MALLOC_COUNT) failed"; |
| 53 | dump->AddScalar("malloc_count", |
| 54 | base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 55 | malloc_count); |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 56 | |
| 57 | const char* system_allocator_name = |
| 58 | base::trace_event::MemoryDumpManager::GetInstance() |
| 59 | ->system_allocator_pool_name(); |
| 60 | if (system_allocator_name) { |
| 61 | pmd->AddSuballocation(dump->guid(), system_allocator_name); |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | } // namespace sql |