call_stack_profile_metrics_provider.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2015 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 COMPONENTS_METRICS_CALL_STACK_PROFILE_METRICS_PROVIDER_H_
  5. #define COMPONENTS_METRICS_CALL_STACK_PROFILE_METRICS_PROVIDER_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/feature_list.h"
  9. #include "base/time/time.h"
  10. #include "components/metrics/metrics_provider.h"
  11. #include "third_party/metrics_proto/sampled_profile.pb.h"
  12. namespace metrics {
  13. class ChromeUserMetricsExtension;
  14. // Performs metrics logging for the stack sampling profiler.
  15. class CallStackProfileMetricsProvider : public MetricsProvider {
  16. public:
  17. // A callback type that can be registered to intercept profiles, for testing
  18. // purposes.
  19. using InterceptorCallback =
  20. base::RepeatingCallback<void(SampledProfile profile)>;
  21. CallStackProfileMetricsProvider();
  22. CallStackProfileMetricsProvider(const CallStackProfileMetricsProvider&) =
  23. delete;
  24. CallStackProfileMetricsProvider& operator=(
  25. const CallStackProfileMetricsProvider&) = delete;
  26. ~CallStackProfileMetricsProvider() override;
  27. // Receives SampledProfile protobuf instances. May be called on any thread.
  28. static void ReceiveProfile(base::TimeTicks profile_start_time,
  29. SampledProfile profile);
  30. // Receives serialized SampledProfile protobuf instances. May be called on any
  31. // thread. Note that receiving serialized profiles is supported separately so
  32. // that profiles received in serialized form can be kept in that form until
  33. // upload. This significantly reduces memory costs. Serialized profile strings
  34. // may be large, so the caller must use std::move() to provide them to this
  35. // API rather than copying by value.
  36. static void ReceiveSerializedProfile(base::TimeTicks profile_start_time,
  37. bool is_heap_profile,
  38. std::string&& serialized_profile);
  39. // Allows tests to intercept received CPU profiles, to validate that the
  40. // expected profiles are received. This function must be invoked prior to
  41. // starting any profiling since the callback is accessed asynchronously on the
  42. // profiling thread.
  43. static void SetCpuInterceptorCallbackForTesting(InterceptorCallback callback);
  44. // MetricsProvider:
  45. void OnRecordingEnabled() override;
  46. void OnRecordingDisabled() override;
  47. void ProvideCurrentSessionData(
  48. ChromeUserMetricsExtension* uma_proto) override;
  49. protected:
  50. // base::Feature for reporting CPU profiles. Provided here for test use.
  51. static const base::Feature kSamplingProfilerReporting;
  52. // Reset the static state to the defaults after startup.
  53. static void ResetStaticStateForTesting();
  54. };
  55. } // namespace metrics
  56. #endif // COMPONENTS_METRICS_CALL_STACK_PROFILE_METRICS_PROVIDER_H_