xref: /aosp_15_r20/external/cronet/base/memory/madv_free_discardable_memory_allocator_posix.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 The Chromium Authors
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 <inttypes.h>
6 #include <sys/mman.h>
7 
8 #include "base/memory/madv_free_discardable_memory_allocator_posix.h"
9 #include "base/process/process_metrics.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/task/single_thread_task_runner.h"
12 #include "base/tracing_buildflags.h"
13 
14 #if BUILDFLAG(ENABLE_BASE_TRACING)
15 #include "base/trace_event/memory_dump_manager.h"  // no-presubmit-check
16 #endif  // BUILDFLAG(ENABLE_BASE_TRACING)
17 
18 namespace base {
19 
20 MadvFreeDiscardableMemoryAllocatorPosix::
MadvFreeDiscardableMemoryAllocatorPosix()21     MadvFreeDiscardableMemoryAllocatorPosix() {
22 #if BUILDFLAG(ENABLE_BASE_TRACING)
23   // Don't register dump provider if
24   // SingleThreadTaskRunner::CurrentDefaultHAndle is not set, such as in tests
25   // and Android Webview.
26   if (base::SingleThreadTaskRunner::HasCurrentDefault()) {
27     trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
28         this, "MadvFreeDiscardableMemoryAllocator",
29         SingleThreadTaskRunner::GetCurrentDefault());
30   }
31 #endif  // BUILDFLAG(ENABLE_BASE_TRACING)
32 }
33 
34 MadvFreeDiscardableMemoryAllocatorPosix::
~MadvFreeDiscardableMemoryAllocatorPosix()35     ~MadvFreeDiscardableMemoryAllocatorPosix() {
36 #if BUILDFLAG(ENABLE_BASE_TRACING)
37   trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(this);
38 #endif  // BUILDFLAG(ENABLE_BASE_TRACING)
39 }
40 
41 std::unique_ptr<DiscardableMemory>
AllocateLockedDiscardableMemory(size_t size)42 MadvFreeDiscardableMemoryAllocatorPosix::AllocateLockedDiscardableMemory(
43     size_t size) {
44   return std::make_unique<MadvFreeDiscardableMemoryPosix>(size,
45                                                           &bytes_allocated_);
46 }
47 
GetBytesAllocated() const48 size_t MadvFreeDiscardableMemoryAllocatorPosix::GetBytesAllocated() const {
49   return bytes_allocated_;
50 }
51 
OnMemoryDump(const trace_event::MemoryDumpArgs & args,trace_event::ProcessMemoryDump * pmd)52 bool MadvFreeDiscardableMemoryAllocatorPosix::OnMemoryDump(
53     const trace_event::MemoryDumpArgs& args,
54     trace_event::ProcessMemoryDump* pmd) {
55 #if BUILDFLAG(ENABLE_BASE_TRACING)
56   if (args.level_of_detail !=
57       base::trace_event::MemoryDumpLevelOfDetail::kBackground) {
58     return true;
59   }
60 
61   base::trace_event::MemoryAllocatorDump* total_dump =
62       pmd->CreateAllocatorDump("discardable/madv_free_allocated");
63   total_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
64                         base::trace_event::MemoryAllocatorDump::kUnitsBytes,
65                         GetBytesAllocated());
66   return true;
67 #else   // BUILDFLAG(ENABLE_BASE_TRACING)
68   return false;
69 #endif  // BUILDFLAG(ENABLE_BASE_TRACING)
70 }
71 
72 }  // namespace base
73