| 6 | #define BASE_PROFILER_PROFILE_BUILDER_H_ |
| 7 |
| 8 | #include <memory> |
| 9 |
| 10 | #include "base/base_export.h" |
| 11 | #include "base/optional.h" |
| 12 | #include "base/profiler/frame.h" |
| 13 | #include "base/profiler/module_cache.h" |
| 14 | #include "base/time/time.h" |
| 15 |
| 16 | namespace base { |
| 17 |
| 18 | // The ProfileBuilder interface allows the user to record profile information on |
| 19 | // the fly in whatever format is desired. Functions are invoked by the profiler |
| 20 | // on its own thread so must not block or perform expensive operations. |
| 21 | class BASE_EXPORT ProfileBuilder { |
| 22 | public: |
| 23 | ProfileBuilder() = default; |
| 24 | virtual ~ProfileBuilder() = default; |
| 25 |
| 26 | // Gets the ModuleCache to be used by the StackSamplingProfiler when looking |
| 27 | // up modules from addresses. |
| 28 | virtual ModuleCache* GetModuleCache() = 0; |
| 29 |
| 30 | struct BASE_EXPORT MetadataItem { |
| 31 | MetadataItem(uint64_t name_hash, Optional<int64_t> key, int64_t value); |
| 32 | MetadataItem(); |
| 33 |
| 34 | MetadataItem(const MetadataItem& other); |
| 35 | MetadataItem& operator=(const MetadataItem& other); |
| 36 |
| 37 | // The hash of the metadata name, as produced by HashMetricName(). |
| 38 | uint64_t name_hash; |
| 39 | // The key if specified when setting the item. |
| 40 | Optional<int64_t> key; |
| 41 | // The value of the metadata item. |
| 42 | int64_t value; |
| 43 | }; |
| 44 |
| 45 | static constexpr size_t MAX_METADATA_COUNT = 50; |
| 46 | typedef std::array<MetadataItem, MAX_METADATA_COUNT> MetadataItemArray; |
| 47 |
| 48 | class MetadataProvider { |
| 49 | public: |
| 50 | MetadataProvider() = default; |
| 51 | virtual ~MetadataProvider() = default; |
| 52 |
| 53 | virtual size_t GetItems(ProfileBuilder::MetadataItemArray* const items) = 0; |
| 54 | }; |
| 55 |
| 56 | // Records metadata to be associated with the current sample. To avoid |
| 57 | // deadlock on locks taken by the suspended profiled thread, implementations |
| 58 | // of this method must not execute any code that could take a lock, including |
| 59 | // heap allocation or use of CHECK/DCHECK/LOG statements. Generally |
| 60 | // implementations should simply atomically copy metadata state to be |
| 61 | // associated with the sample. |
| 62 | virtual void RecordMetadata(MetadataProvider* metadata_provider) {} |
| 63 |
| 64 | // Applies the specified metadata |item| to samples collected in the range |
| 65 | // [period_start, period_end), iff the profile already captured execution that |
| 66 | // covers that range entirely. This restriction avoids bias in the results |
| 67 | // towards samples in the middle of the period, at the expense of excluding |
| 68 | // periods overlapping the start or end of the profile. |period_end| must be |
| 69 | // <= TimeTicks::Now(). |
| 70 | virtual void ApplyMetadataRetrospectively(TimeTicks period_start, |
| 71 | TimeTicks period_end, |
| 72 | const MetadataItem& item) {} |
| 73 |
| 74 | // Records a new set of frames. Invoked when sampling a sample completes. |
| 75 | virtual void OnSampleCompleted(std::vector<Frame> frames, |
| 76 | TimeTicks sample_timestamp) = 0; |
| 77 |
| 78 | // Finishes the profile construction with |profile_duration| and |
| 79 | // |sampling_period|. Invoked when sampling a profile completes. |
| 80 | virtual void OnProfileCompleted(TimeDelta profile_duration, |
| 81 | TimeDelta sampling_period) = 0; |
| 82 |
| 83 | private: |
| 84 | DISALLOW_COPY_AND_ASSIGN(ProfileBuilder); |
| 85 | }; |
| 86 |
| 87 | } // namespace base |
| 88 |
| 89 | #endif // BASE_PROFILER_PROFILE_BUILDER_H_ |
| 90 |