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