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.
5 #include "gin/v8_shared_memory_dump_provider.h"
9 #include "base/memory/singleton.h"
10 #include "base/trace_event/memory_dump_manager.h"
11 #include "base/trace_event/process_memory_dump.h"
12 #include "v8/include/v8-initialization.h"
13 #include "v8/include/v8-isolate.h"
17 V8SharedMemoryDumpProvider::V8SharedMemoryDumpProvider() {
18 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
19 this, "V8SharedMemory", nullptr);
23 void V8SharedMemoryDumpProvider::Register() {
24 // The provider is registered as part of the constructor, so all this does is
25 // access the singleton causing it to be constructed on the first access.
27 V8SharedMemoryDumpProvider,
28 base::LeakySingletonTraits<V8SharedMemoryDumpProvider>>::get();
31 // Called at trace dump point time. Creates a snapshot with the memory counters
32 // for the current isolate.
33 bool V8SharedMemoryDumpProvider::OnMemoryDump(
34 const base::trace_event::MemoryDumpArgs& args,
35 base::trace_event::ProcessMemoryDump* process_memory_dump) {
36 v8::SharedMemoryStatistics shared_memory_statistics;
37 v8::V8::GetSharedMemoryStatistics(&shared_memory_statistics);
39 std::string dump_base_name = "v8/shared";
40 auto* shared_memory_dump = process_memory_dump->CreateAllocatorDump(
41 dump_base_name + "/read_only_space");
42 shared_memory_dump->AddScalar(
43 base::trace_event::MemoryAllocatorDump::kNameSize,
44 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
45 shared_memory_statistics.read_only_space_physical_size());
46 shared_memory_dump->AddScalar(
47 "allocated_objects_size",
48 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
49 shared_memory_statistics.read_only_space_used_size());
50 shared_memory_dump->AddScalar(
51 "virtual_size", base::trace_event::MemoryAllocatorDump::kUnitsBytes,
52 shared_memory_statistics.read_only_space_size());