child_call_stack_profile_collector.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2016 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_METRICS_CHILD_CALL_STACK_PROFILE_COLLECTOR_H_
  5. #define COMPONENTS_METRICS_CHILD_CALL_STACK_PROFILE_COLLECTOR_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/memory/ref_counted.h"
  9. #include "base/synchronization/lock.h"
  10. #include "base/task/single_thread_task_runner.h"
  11. #include "base/time/time.h"
  12. #include "components/metrics/public/mojom/call_stack_profile_collector.mojom.h"
  13. #include "mojo/public/cpp/bindings/pending_remote.h"
  14. #include "mojo/public/cpp/bindings/remote.h"
  15. namespace service_manager {
  16. class InterfaceProvider;
  17. }
  18. namespace metrics {
  19. class SampledProfile;
  20. // ChildCallStackProfileCollector collects stacks at startup, caching them
  21. // internally until a CallStackProfileCollector interface is available. If a
  22. // CallStackProfileCollector is provided via the InterfaceProvider supplied to
  23. // SetParentProfileCollector, the cached stacks are sent via that interface. All
  24. // future stacks received via callbacks supplied by GetProfilerCallback are sent
  25. // via that interface as well.
  26. //
  27. // If no CallStackProfileCollector is provided via InterfaceProvider, any cached
  28. // stacks and all future stacks received via callbacks supplied by
  29. // GetProfilerCallback are flushed. In typical usage this should not happen
  30. // because the browser is expected to always supply a CallStackProfileCollector.
  31. //
  32. // This class is only necessary if a CallStackProfileCollector is not available
  33. // at the time the profiler is created. Otherwise the CallStackProfileCollector
  34. // can be used directly.
  35. //
  36. // CallStackProfileBuilder owns and manages a ChildCallStackProfileCollector. It
  37. // invokes Collect() in CallStackProfileBuilder::OnProfileCompleted() to collect
  38. // a profile.
  39. //
  40. // When the mojo InterfaceProvider becomes available, provide it via
  41. // SetParentProfileCollector().
  42. class ChildCallStackProfileCollector {
  43. public:
  44. ChildCallStackProfileCollector();
  45. ChildCallStackProfileCollector(const ChildCallStackProfileCollector&) =
  46. delete;
  47. ChildCallStackProfileCollector& operator=(
  48. const ChildCallStackProfileCollector&) = delete;
  49. ~ChildCallStackProfileCollector();
  50. // Sets the CallStackProfileCollector interface from |parent_collector|. This
  51. // function MUST be invoked exactly once, regardless of whether
  52. // |parent_collector| is mojo::NullRemote(), as it flushes pending data in
  53. // either case.
  54. void SetParentProfileCollector(
  55. mojo::PendingRemote<metrics::mojom::CallStackProfileCollector>
  56. parent_collector);
  57. // Collects |profile| whose collection start time is |start_timestamp|.
  58. void Collect(base::TimeTicks start_timestamp, SampledProfile profile);
  59. private:
  60. friend class ChildCallStackProfileCollectorTest;
  61. // Bundles together a collected serialized profile and the collection state
  62. // for storage, pending availability of the parent mojo interface.
  63. struct ProfileState {
  64. ProfileState();
  65. // |profile| can be very large and must be passed with std::move.
  66. ProfileState(base::TimeTicks start_timestamp,
  67. mojom::ProfileType profile_type,
  68. std::string&& profile);
  69. ProfileState(const ProfileState&) = delete;
  70. ProfileState& operator=(const ProfileState&) = delete;
  71. ProfileState(ProfileState&&);
  72. ~ProfileState();
  73. ProfileState& operator=(ProfileState&&);
  74. base::TimeTicks start_timestamp;
  75. mojom::ProfileType profile_type;
  76. // The serialized sampled profile.
  77. std::string profile;
  78. };
  79. // This object may be accessed on any thread, including the profiler
  80. // thread. The expected use case for the object is to be created and have
  81. // GetProfilerCallback before the message loop starts, which prevents the use
  82. // of PostTask and the like for inter-thread communication.
  83. base::Lock lock_;
  84. // Whether to retain the profile when the interface is not set. Remains true
  85. // until the invocation of SetParentProfileCollector(), at which point it is
  86. // false for the rest of the object lifetime.
  87. bool retain_profiles_ = true;
  88. // The task runner associated with the parent interface.
  89. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  90. // The interface to use to collect the stack profiles provided to this
  91. // object. Initially mojo::NullRemote() until SetParentProfileCollector() is
  92. // invoked, at which point it may either become set or remain
  93. // mojo::NullRemote(). If set, stacks are collected via the interface,
  94. // otherwise they are ignored.
  95. mojo::Remote<mojom::CallStackProfileCollector> parent_collector_;
  96. // Profiles being cached by this object, pending a parent interface to be
  97. // supplied.
  98. std::vector<ProfileState> profiles_;
  99. };
  100. } // namespace metrics
  101. #endif // COMPONENTS_METRICS_CHILD_CALL_STACK_PROFILE_COLLECTOR_H_