xref: /aosp_15_r20/external/cronet/base/profiler/profile_builder.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2019 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_PROFILER_PROFILE_BUILDER_H_
6*6777b538SAndroid Build Coastguard Worker #define BASE_PROFILER_PROFILE_BUILDER_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <memory>
9*6777b538SAndroid Build Coastguard Worker #include <vector>
10*6777b538SAndroid Build Coastguard Worker 
11*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h"
12*6777b538SAndroid Build Coastguard Worker #include "base/profiler/frame.h"
13*6777b538SAndroid Build Coastguard Worker #include "base/profiler/metadata_recorder.h"
14*6777b538SAndroid Build Coastguard Worker #include "base/profiler/module_cache.h"
15*6777b538SAndroid Build Coastguard Worker #include "base/time/time.h"
16*6777b538SAndroid Build Coastguard Worker 
17*6777b538SAndroid Build Coastguard Worker namespace base {
18*6777b538SAndroid Build Coastguard Worker 
19*6777b538SAndroid Build Coastguard Worker // The ProfileBuilder interface allows the user to record profile information on
20*6777b538SAndroid Build Coastguard Worker // the fly in whatever format is desired. Functions are invoked by the profiler
21*6777b538SAndroid Build Coastguard Worker // on its own thread so must not block or perform expensive operations.
22*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT ProfileBuilder {
23*6777b538SAndroid Build Coastguard Worker  public:
24*6777b538SAndroid Build Coastguard Worker   ProfileBuilder() = default;
25*6777b538SAndroid Build Coastguard Worker 
26*6777b538SAndroid Build Coastguard Worker   ProfileBuilder(const ProfileBuilder&) = delete;
27*6777b538SAndroid Build Coastguard Worker   ProfileBuilder& operator=(const ProfileBuilder&) = delete;
28*6777b538SAndroid Build Coastguard Worker 
29*6777b538SAndroid Build Coastguard Worker   virtual ~ProfileBuilder() = default;
30*6777b538SAndroid Build Coastguard Worker 
31*6777b538SAndroid Build Coastguard Worker   // Gets the ModuleCache to be used by the StackSamplingProfiler when looking
32*6777b538SAndroid Build Coastguard Worker   // up modules from addresses.
33*6777b538SAndroid Build Coastguard Worker   virtual ModuleCache* GetModuleCache() = 0;
34*6777b538SAndroid Build Coastguard Worker 
35*6777b538SAndroid Build Coastguard Worker   // Records metadata to be associated with the current sample. To avoid
36*6777b538SAndroid Build Coastguard Worker   // deadlock on locks taken by the suspended profiled thread, implementations
37*6777b538SAndroid Build Coastguard Worker   // of this method must not execute any code that could take a lock, including
38*6777b538SAndroid Build Coastguard Worker   // heap allocation or use of CHECK/DCHECK/LOG statements. Generally
39*6777b538SAndroid Build Coastguard Worker   // implementations should simply atomically copy metadata state to be
40*6777b538SAndroid Build Coastguard Worker   // associated with the sample.
RecordMetadata(const MetadataRecorder::MetadataProvider & metadata_provider)41*6777b538SAndroid Build Coastguard Worker   virtual void RecordMetadata(
42*6777b538SAndroid Build Coastguard Worker       const MetadataRecorder::MetadataProvider& metadata_provider) {}
43*6777b538SAndroid Build Coastguard Worker 
44*6777b538SAndroid Build Coastguard Worker   // Applies the specified metadata |item| to samples collected in the range
45*6777b538SAndroid Build Coastguard Worker   // [period_start, period_end), iff the profile already captured execution that
46*6777b538SAndroid Build Coastguard Worker   // covers that range entirely. This restriction avoids bias in the results
47*6777b538SAndroid Build Coastguard Worker   // towards samples in the middle of the period, at the expense of excluding
48*6777b538SAndroid Build Coastguard Worker   // periods overlapping the start or end of the profile. |period_end| must be
49*6777b538SAndroid Build Coastguard Worker   // <= TimeTicks::Now().
ApplyMetadataRetrospectively(TimeTicks period_start,TimeTicks period_end,const MetadataRecorder::Item & item)50*6777b538SAndroid Build Coastguard Worker   virtual void ApplyMetadataRetrospectively(
51*6777b538SAndroid Build Coastguard Worker       TimeTicks period_start,
52*6777b538SAndroid Build Coastguard Worker       TimeTicks period_end,
53*6777b538SAndroid Build Coastguard Worker       const MetadataRecorder::Item& item) {}
54*6777b538SAndroid Build Coastguard Worker 
55*6777b538SAndroid Build Coastguard Worker   // Adds the specified metadata |item| to |CallstackProfile::profile_metadata|.
56*6777b538SAndroid Build Coastguard Worker   // |CallstackProfile::profile_metadata| stores metadata global to the profile.
AddProfileMetadata(const MetadataRecorder::Item & item)57*6777b538SAndroid Build Coastguard Worker   virtual void AddProfileMetadata(const MetadataRecorder::Item& item) {}
58*6777b538SAndroid Build Coastguard Worker 
59*6777b538SAndroid Build Coastguard Worker   // Records a new set of frames. Invoked when sampling a sample completes.
60*6777b538SAndroid Build Coastguard Worker   virtual void OnSampleCompleted(std::vector<Frame> frames,
61*6777b538SAndroid Build Coastguard Worker                                  TimeTicks sample_timestamp) = 0;
62*6777b538SAndroid Build Coastguard Worker 
63*6777b538SAndroid Build Coastguard Worker   // Finishes the profile construction with |profile_duration| and
64*6777b538SAndroid Build Coastguard Worker   // |sampling_period|. Invoked when sampling a profile completes.
65*6777b538SAndroid Build Coastguard Worker   virtual void OnProfileCompleted(TimeDelta profile_duration,
66*6777b538SAndroid Build Coastguard Worker                                   TimeDelta sampling_period) = 0;
67*6777b538SAndroid Build Coastguard Worker };
68*6777b538SAndroid Build Coastguard Worker 
69*6777b538SAndroid Build Coastguard Worker }  // namespace base
70*6777b538SAndroid Build Coastguard Worker 
71*6777b538SAndroid Build Coastguard Worker #endif  // BASE_PROFILER_PROFILE_BUILDER_H_
72