v8_shared_memory_dump_provider.cc 2.1 KB

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