heap_profiling_service.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "components/services/heap_profiling/heap_profiling_service.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/task/thread_pool.h"
  10. #include "components/services/heap_profiling/connection_manager.h"
  11. #include "components/services/heap_profiling/public/mojom/heap_profiling_client.mojom.h"
  12. #include "mojo/public/cpp/bindings/pending_receiver.h"
  13. #include "mojo/public/cpp/bindings/receiver.h"
  14. #include "mojo/public/cpp/bindings/remote.h"
  15. #include "mojo/public/cpp/bindings/self_owned_receiver.h"
  16. #include "mojo/public/cpp/system/platform_handle.h"
  17. #include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h"
  18. namespace heap_profiling {
  19. namespace {
  20. class ProfilingServiceImpl;
  21. class ProfilingServiceImpl
  22. : public mojom::ProfilingService,
  23. public memory_instrumentation::mojom::HeapProfiler {
  24. public:
  25. ProfilingServiceImpl(
  26. mojo::PendingReceiver<memory_instrumentation::mojom::HeapProfiler>
  27. profiler_receiver,
  28. mojo::PendingRemote<memory_instrumentation::mojom::HeapProfilerHelper>
  29. helper)
  30. : heap_profiler_receiver_(this, std::move(profiler_receiver)),
  31. helper_(std::move(helper)) {}
  32. ProfilingServiceImpl(const ProfilingServiceImpl&) = delete;
  33. ProfilingServiceImpl& operator=(const ProfilingServiceImpl&) = delete;
  34. ~ProfilingServiceImpl() override = default;
  35. // mojom::ProfilingService implementation:
  36. void AddProfilingClient(base::ProcessId pid,
  37. mojo::PendingRemote<mojom::ProfilingClient> client,
  38. mojom::ProcessType process_type,
  39. mojom::ProfilingParamsPtr params) override {
  40. if (params->sampling_rate == 0)
  41. params->sampling_rate = 1;
  42. connection_manager_.OnNewConnection(pid, std::move(client), process_type,
  43. std::move(params));
  44. }
  45. void GetProfiledPids(GetProfiledPidsCallback callback) override {
  46. std::move(callback).Run(connection_manager_.GetConnectionPids());
  47. }
  48. // memory_instrumentation::mojom::HeapProfiler implementation:
  49. void DumpProcessesForTracing(
  50. bool strip_path_from_mapped_files,
  51. bool write_proto,
  52. DumpProcessesForTracingCallback callback) override {
  53. std::vector<base::ProcessId> pids =
  54. connection_manager_.GetConnectionPidsThatNeedVmRegions();
  55. if (pids.empty()) {
  56. connection_manager_.DumpProcessesForTracing(
  57. strip_path_from_mapped_files, write_proto, std::move(callback),
  58. VmRegions());
  59. return;
  60. }
  61. // Need a memory map to make sense of the dump. The dump will be triggered
  62. // in the memory map global dump callback.
  63. helper_->GetVmRegionsForHeapProfiler(
  64. pids,
  65. base::BindOnce(&ProfilingServiceImpl::
  66. OnGetVmRegionsCompleteForDumpProcessesForTracing,
  67. weak_factory_.GetWeakPtr(), strip_path_from_mapped_files,
  68. write_proto, std::move(callback)));
  69. }
  70. private:
  71. void OnGetVmRegionsCompleteForDumpProcessesForTracing(
  72. bool strip_path_from_mapped_files,
  73. bool write_proto,
  74. DumpProcessesForTracingCallback callback,
  75. VmRegions vm_regions) {
  76. connection_manager_.DumpProcessesForTracing(
  77. strip_path_from_mapped_files, write_proto, std::move(callback),
  78. std::move(vm_regions));
  79. }
  80. mojo::Receiver<memory_instrumentation::mojom::HeapProfiler>
  81. heap_profiler_receiver_{this};
  82. mojo::Remote<memory_instrumentation::mojom::HeapProfilerHelper> helper_;
  83. ConnectionManager connection_manager_;
  84. base::WeakPtrFactory<ProfilingServiceImpl> weak_factory_{this};
  85. };
  86. void RunHeapProfilingService(
  87. mojo::PendingReceiver<mojom::ProfilingService> receiver,
  88. mojo::PendingReceiver<memory_instrumentation::mojom::HeapProfiler>
  89. profiler_receiver,
  90. mojo::PendingRemote<memory_instrumentation::mojom::HeapProfilerHelper>
  91. helper) {
  92. mojo::MakeSelfOwnedReceiver(
  93. std::make_unique<ProfilingServiceImpl>(std::move(profiler_receiver),
  94. std::move(helper)),
  95. std::move(receiver));
  96. }
  97. } // namespace
  98. mojo::PendingRemote<mojom::ProfilingService> LaunchService(
  99. mojo::PendingReceiver<memory_instrumentation::mojom::HeapProfiler>
  100. profiler_receiver,
  101. mojo::PendingRemote<memory_instrumentation::mojom::HeapProfilerHelper>
  102. helper) {
  103. mojo::PendingRemote<mojom::ProfilingService> remote;
  104. auto task_runner = base::ThreadPool::CreateSingleThreadTaskRunner(
  105. {base::TaskPriority::BEST_EFFORT, base::WithBaseSyncPrimitives()},
  106. base::SingleThreadTaskRunnerThreadMode::DEDICATED);
  107. task_runner->PostTask(
  108. FROM_HERE,
  109. base::BindOnce(&RunHeapProfilingService,
  110. remote.InitWithNewPipeAndPassReceiver(),
  111. std::move(profiler_receiver), std::move(helper)));
  112. return remote;
  113. }
  114. } // namespace heap_profiling