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