sql_memory_dump_provider.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include "sql/sql_memory_dump_provider.h"
  5. #include "base/trace_event/memory_dump_manager.h"
  6. #include "base/trace_event/process_memory_dump.h"
  7. #include "sql/sqlite_result_code.h"
  8. #include "sql/sqlite_result_code_values.h"
  9. #include "third_party/sqlite/sqlite3.h"
  10. namespace sql {
  11. // static
  12. SqlMemoryDumpProvider* SqlMemoryDumpProvider::GetInstance() {
  13. return base::Singleton<
  14. SqlMemoryDumpProvider,
  15. base::LeakySingletonTraits<SqlMemoryDumpProvider>>::get();
  16. }
  17. SqlMemoryDumpProvider::SqlMemoryDumpProvider() = default;
  18. SqlMemoryDumpProvider::~SqlMemoryDumpProvider() = default;
  19. bool SqlMemoryDumpProvider::OnMemoryDump(
  20. const base::trace_event::MemoryDumpArgs& args,
  21. base::trace_event::ProcessMemoryDump* pmd) {
  22. sqlite3_int64 memory_used = 0;
  23. sqlite3_int64 memory_high_water = 0;
  24. auto sqlite_result_code = ToSqliteResultCode(sqlite3_status64(
  25. SQLITE_STATUS_MEMORY_USED, &memory_used, &memory_high_water,
  26. /*resetFlag=*/1));
  27. DCHECK_EQ(sqlite_result_code, SqliteResultCode::kOk)
  28. << "sqlite3_status64(SQLITE_STATUS_MEMORY_USED) failed";
  29. base::trace_event::MemoryAllocatorDump* dump =
  30. pmd->CreateAllocatorDump("sqlite");
  31. dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
  32. base::trace_event::MemoryAllocatorDump::kUnitsBytes,
  33. memory_used);
  34. dump->AddScalar("malloc_high_wmark_size",
  35. base::trace_event::MemoryAllocatorDump::kUnitsBytes,
  36. memory_high_water);
  37. sqlite3_int64 dummy_high_water = -1;
  38. sqlite3_int64 malloc_count = -1;
  39. sqlite_result_code = ToSqliteResultCode(sqlite3_status64(
  40. SQLITE_STATUS_MALLOC_COUNT, &malloc_count, &dummy_high_water,
  41. /*resetFlag=*/0));
  42. DCHECK_EQ(sqlite_result_code, SqliteResultCode::kOk)
  43. << "sqlite3_status64(SQLITE_STATUS_MALLOC_COUNT) failed";
  44. dump->AddScalar("malloc_count",
  45. base::trace_event::MemoryAllocatorDump::kUnitsObjects,
  46. malloc_count);
  47. const char* system_allocator_name =
  48. base::trace_event::MemoryDumpManager::GetInstance()
  49. ->system_allocator_pool_name();
  50. if (system_allocator_name) {
  51. pmd->AddSuballocation(dump->guid(), system_allocator_name);
  52. }
  53. return true;
  54. }
  55. } // namespace sql