connection_manager.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2017 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. #ifndef COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_
  5. #define COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_
  6. #include <map>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <vector>
  10. #include "base/containers/flat_map.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/synchronization/lock.h"
  14. #include "base/threading/thread.h"
  15. #include "base/timer/timer.h"
  16. #include "build/build_config.h"
  17. #include "components/services/heap_profiling/allocation.h"
  18. #include "components/services/heap_profiling/public/mojom/heap_profiling_service.mojom.h"
  19. #include "mojo/public/cpp/bindings/pending_remote.h"
  20. #include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h"
  21. namespace heap_profiling {
  22. struct ExportParams;
  23. using VmRegions =
  24. base::flat_map<base::ProcessId,
  25. std::vector<memory_instrumentation::mojom::VmRegionPtr>>;
  26. // Manages all connections and logging for each process. Pipes are supplied by
  27. // the pipe server and this class will connect them to a parser and logger.
  28. //
  29. // Note |backtrace_storage| must outlive ConnectionManager.
  30. //
  31. // This object is constructed on the UI thread, but the rest of the usage
  32. // (including deletion) is on the IO thread.
  33. class ConnectionManager {
  34. using AddressToStringMap = std::unordered_map<uint64_t, std::string>;
  35. using CompleteCallback = base::OnceClosure;
  36. using ContextMap = std::map<std::string, int>;
  37. using DumpProcessesForTracingCallback = memory_instrumentation::mojom::
  38. HeapProfiler::DumpProcessesForTracingCallback;
  39. public:
  40. ConnectionManager();
  41. ConnectionManager(const ConnectionManager&) = delete;
  42. ConnectionManager& operator=(const ConnectionManager&) = delete;
  43. ~ConnectionManager();
  44. // Dumping is asynchronous so will not be complete when this function
  45. // returns. The dump is complete when the callback provided in the args is
  46. // fired.
  47. void DumpProcessesForTracing(bool strip_path_from_mapped_files,
  48. bool write_proto,
  49. DumpProcessesForTracingCallback callback,
  50. VmRegions vm_regions);
  51. void OnNewConnection(base::ProcessId pid,
  52. mojo::PendingRemote<mojom::ProfilingClient> client,
  53. mojom::ProcessType process_type,
  54. mojom::ProfilingParamsPtr params);
  55. // Returns pids of clients that have started profiling.
  56. std::vector<base::ProcessId> GetConnectionPids();
  57. // Returns pids of all connected clients that need vm regions, regardless of
  58. // whether they've started profiling.
  59. std::vector<base::ProcessId> GetConnectionPidsThatNeedVmRegions();
  60. private:
  61. struct Connection;
  62. struct DumpProcessesForTracingTracking;
  63. void HeapProfileRetrieved(
  64. scoped_refptr<DumpProcessesForTracingTracking> tracking,
  65. base::ProcessId pid,
  66. mojom::ProcessType process_type,
  67. bool strip_path_from_mapped_files,
  68. uint32_t sampling_rate,
  69. mojom::HeapProfilePtr profile);
  70. bool ConvertProfileToExportParams(mojom::HeapProfilePtr profile,
  71. uint32_t sampling_rate,
  72. ExportParams* out_params);
  73. // Notification that the client has disconnected. Unlike OnNewConnection which
  74. // is signaled by the pipe server, this is signaled by the allocation tracker
  75. // to ensure that the pipeline for this process has been flushed of all
  76. // messages.
  77. void OnConnectionComplete(base::ProcessId pid);
  78. // Indicates that the client has enabled profiling. Necessary for tests to
  79. // know when initialization is complete.
  80. void OnProfilingStarted(base::ProcessId pid);
  81. // Reports the ProcessTypes of the processes being profiled.
  82. void ReportMetrics();
  83. // The next ID to use when exporting a heap dump.
  84. size_t next_id_ = 1;
  85. // Maps process ID to the connection information for it.
  86. base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_;
  87. base::Lock connections_lock_;
  88. // Every 24-hours, reports the types of profiled processes.
  89. base::RepeatingTimer metrics_timer_;
  90. // Must be the last.
  91. base::WeakPtrFactory<ConnectionManager> weak_factory_{this};
  92. };
  93. } // namespace heap_profiling
  94. #endif // COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_