profile_builder.h 2.6 KB

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