1 // Copyright 2018 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 #ifndef COMPONENTS_METRICS_CALL_STACK_PROFILE_BUILDER_H_
6 #define COMPONENTS_METRICS_CALL_STACK_PROFILE_BUILDER_H_
10 #include <unordered_map>
14 #include "base/functional/callback.h"
15 #include "base/memory/raw_ptr.h"
16 #include "base/profiler/metadata_recorder.h"
17 #include "base/profiler/module_cache.h"
18 #include "base/profiler/profile_builder.h"
19 #include "base/time/time.h"
20 #include "components/metrics/call_stack_profile_metadata.h"
21 #include "components/metrics/call_stack_profile_params.h"
22 #include "components/metrics/child_call_stack_profile_collector.h"
23 #include "mojo/public/cpp/bindings/pending_remote.h"
24 #include "third_party/metrics_proto/sampled_profile.pb.h"
28 // Interface that allows the CallStackProfileBuilder to provide ids for distinct
29 // work items. Samples with the same id are tagged as coming from the same work
30 // item in the recorded samples.
31 class WorkIdRecorder {
33 WorkIdRecorder() = default;
34 virtual ~WorkIdRecorder() = default;
36 // This function is invoked on the profiler thread while the target thread is
37 // suspended so must not take any locks, including indirectly through use of
38 // heap allocation, LOG, CHECK, or DCHECK.
39 virtual unsigned int RecordWorkId() const = 0;
41 WorkIdRecorder(const WorkIdRecorder&) = delete;
42 WorkIdRecorder& operator=(const WorkIdRecorder&) = delete;
45 // An instance of the class is meant to be passed to base::StackSamplingProfiler
46 // to collect profiles. The profiles collected are uploaded via the metrics log.
48 // This uses the new StackSample encoding rather than the legacy Sample
50 class CallStackProfileBuilder : public base::ProfileBuilder {
52 // |completed_callback| is made when sampling a profile completes. Other
53 // threads, including the UI thread, may block on callback completion so this
54 // should run as quickly as possible.
56 // IMPORTANT NOTE: The callback is invoked on a thread the profiler
57 // constructs, rather than on the thread used to construct the profiler, and
58 // thus the callback must be callable on any thread.
59 explicit CallStackProfileBuilder(
60 const CallStackProfileParams& profile_params,
61 const WorkIdRecorder* work_id_recorder = nullptr,
62 base::OnceClosure completed_callback = base::OnceClosure());
64 CallStackProfileBuilder(const CallStackProfileBuilder&) = delete;
65 CallStackProfileBuilder& operator=(const CallStackProfileBuilder&) = delete;
67 ~CallStackProfileBuilder() override;
69 // Both weight and count are used by the heap profiler only.
70 void OnSampleCompleted(std::vector<base::Frame> frames,
71 base::TimeTicks sample_timestamp,
75 // base::ProfileBuilder:
76 base::ModuleCache* GetModuleCache() override;
77 void RecordMetadata(const base::MetadataRecorder::MetadataProvider&
78 metadata_provider) override;
79 void ApplyMetadataRetrospectively(
80 base::TimeTicks period_start,
81 base::TimeTicks period_end,
82 const base::MetadataRecorder::Item& item) override;
83 void AddProfileMetadata(const base::MetadataRecorder::Item& item) override;
84 void OnSampleCompleted(std::vector<base::Frame> frames,
85 base::TimeTicks sample_timestamp) override;
86 void OnProfileCompleted(base::TimeDelta profile_duration,
87 base::TimeDelta sampling_period) override;
89 // Sets the callback to use for reporting browser process profiles. This
90 // indirection is required to avoid a dependency on unnecessary metrics code
91 // in child processes.
92 static void SetBrowserProcessReceiverCallback(
93 const base::RepeatingCallback<void(base::TimeTicks, SampledProfile)>&
96 // Sets the CallStackProfileCollector interface from |browser_interface|.
97 // This function must be called within child processes, and must only be
99 static void SetParentProfileCollectorForChildProcess(
100 mojo::PendingRemote<metrics::mojom::CallStackProfileCollector>
103 // Resets the ChildCallStackProfileCollector to its default state. This will
104 // discard all collected profiles, remove any CallStackProfileCollector
105 // interface set through SetParentProfileCollectorForChildProcess, and allow
106 // SetParentProfileCollectorForChildProcess to be called multiple times during
108 static void ResetChildCallStackProfileCollectorForTesting();
112 virtual void PassProfilesToMetricsProvider(base::TimeTicks profile_start_time,
113 SampledProfile sampled_profile);
116 // The functor for Stack comparison.
117 struct StackComparer {
118 bool operator()(const CallStackProfile::Stack* stack1,
119 const CallStackProfile::Stack* stack2) const;
122 // The module cache to use for the duration the sampling associated with this
124 base::ModuleCache module_cache_;
126 unsigned int last_work_id_ = std::numeric_limits<unsigned int>::max();
127 bool is_continued_work_ = false;
128 const raw_ptr<const WorkIdRecorder> work_id_recorder_;
130 // The SampledProfile protobuf message which contains the collected stack
132 SampledProfile sampled_profile_;
134 // The indexes of stacks, indexed by stack's address.
135 std::map<const CallStackProfile::Stack*, int, StackComparer> stack_index_;
137 // The indexes of modules in the modules_ vector below..
138 std::unordered_map<const base::ModuleCache::Module*, size_t> module_index_;
140 // The distinct modules in the current profile.
141 std::vector<const base::ModuleCache::Module*> modules_;
143 // Timestamps recording when each sample was taken.
144 std::vector<base::TimeTicks> sample_timestamps_;
146 // Callback made when sampling a profile completes.
147 base::OnceClosure completed_callback_;
149 // The start time of a profile collection.
150 base::TimeTicks profile_start_time_;
152 // Maintains the current metadata to apply to samples.
153 CallStackProfileMetadata metadata_;
156 } // namespace metrics
158 #endif // COMPONENTS_METRICS_CALL_STACK_PROFILE_BUILDER_H_