wittman | d19f520 | 2015-03-24 03:25:47 | [diff] [blame] | 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 | |
| 5 | #ifndef COMPONENTS_METRICS_CALL_STACK_PROFILE_METRICS_PROVIDER_H_ |
| 6 | #define COMPONENTS_METRICS_CALL_STACK_PROFILE_METRICS_PROVIDER_H_ |
| 7 | |
Mike Wittman | 16fa536 | 2018-09-04 17:27:30 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Mike Wittman | 7587214 | 2019-12-04 23:51:29 | [diff] [blame] | 10 | #include "base/callback.h" |
asvitkine | 2d8b08c | 2017-07-14 22:16:00 | [diff] [blame] | 11 | #include "base/feature_list.h" |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 12 | #include "base/macros.h" |
Xi Cheng | 4dec7e4 | 2018-08-10 16:54:11 | [diff] [blame] | 13 | #include "base/time/time.h" |
wittman | d19f520 | 2015-03-24 03:25:47 | [diff] [blame] | 14 | #include "components/metrics/metrics_provider.h" |
Xi Cheng | 4dec7e4 | 2018-08-10 16:54:11 | [diff] [blame] | 15 | #include "third_party/metrics_proto/sampled_profile.pb.h" |
wittman | d19f520 | 2015-03-24 03:25:47 | [diff] [blame] | 16 | |
| 17 | namespace metrics { |
asvitkine | 2d8b08c | 2017-07-14 22:16:00 | [diff] [blame] | 18 | |
wittman | d19f520 | 2015-03-24 03:25:47 | [diff] [blame] | 19 | class ChromeUserMetricsExtension; |
| 20 | |
| 21 | // Performs metrics logging for the stack sampling profiler. |
| 22 | class CallStackProfileMetricsProvider : public MetricsProvider { |
| 23 | public: |
Mike Wittman | 7587214 | 2019-12-04 23:51:29 | [diff] [blame] | 24 | // A callback type that can be registered to intercept profiles, for testing |
| 25 | // purposes. |
| 26 | using InterceptorCallback = |
| 27 | base::RepeatingCallback<void(SampledProfile profile)>; |
| 28 | |
wittman | d19f520 | 2015-03-24 03:25:47 | [diff] [blame] | 29 | CallStackProfileMetricsProvider(); |
| 30 | ~CallStackProfileMetricsProvider() override; |
| 31 | |
Mike Wittman | 16fa536 | 2018-09-04 17:27:30 | [diff] [blame] | 32 | // Receives SampledProfile protobuf instances. May be called on any thread. |
| 33 | static void ReceiveProfile(base::TimeTicks profile_start_time, |
| 34 | SampledProfile profile); |
| 35 | |
| 36 | // Receives serialized SampledProfile protobuf instances. May be called on any |
| 37 | // thread. Note that receiving serialized profiles is supported separately so |
| 38 | // that profiles received in serialized form can be kept in that form until |
| 39 | // upload. This significantly reduces memory costs. Serialized profile strings |
| 40 | // may be large, so the caller should use std::move() to provide them to this |
| 41 | // API rather than copying by value. |
| 42 | static void ReceiveSerializedProfile(base::TimeTicks profile_start_time, |
| 43 | std::string serialized_sampled_profile); |
wittman | 622851e | 2015-07-31 18:13:40 | [diff] [blame] | 44 | |
Mike Wittman | 7587214 | 2019-12-04 23:51:29 | [diff] [blame] | 45 | // Allows tests to intercept received CPU profiles, to validate that the |
| 46 | // expected profiles are received. This function must be invoked prior to |
| 47 | // starting any profiling since the callback is accessed asynchronously on the |
| 48 | // profiling thread. |
| 49 | static void SetCpuInterceptorCallbackForTesting(InterceptorCallback callback); |
| 50 | |
wittman | d19f520 | 2015-03-24 03:25:47 | [diff] [blame] | 51 | // MetricsProvider: |
wittman | ae2a08a | 2015-04-08 02:20:35 | [diff] [blame] | 52 | void OnRecordingEnabled() override; |
| 53 | void OnRecordingDisabled() override; |
Steven Holte | 141462ac | 2017-07-26 01:35:07 | [diff] [blame] | 54 | void ProvideCurrentSessionData( |
| 55 | ChromeUserMetricsExtension* uma_proto) override; |
wittman | d19f520 | 2015-03-24 03:25:47 | [diff] [blame] | 56 | |
Alexei Filippov | 59107801 | 2019-06-19 07:30:28 | [diff] [blame] | 57 | // Enables reporting of sampling heap profiles. |
| 58 | static const base::Feature kHeapProfilerReporting; |
| 59 | |
wittman | ae2a08a | 2015-04-08 02:20:35 | [diff] [blame] | 60 | protected: |
Alexei Filippov | 59107801 | 2019-06-19 07:30:28 | [diff] [blame] | 61 | // base::Feature for reporting CPU profiles. Provided here for test use. |
| 62 | static const base::Feature kSamplingProfilerReporting; |
wittman | ae2a08a | 2015-04-08 02:20:35 | [diff] [blame] | 63 | |
wittman | 622851e | 2015-07-31 18:13:40 | [diff] [blame] | 64 | // Reset the static state to the defaults after startup. |
| 65 | static void ResetStaticStateForTesting(); |
| 66 | |
wittman | d19f520 | 2015-03-24 03:25:47 | [diff] [blame] | 67 | private: |
wittman | d19f520 | 2015-03-24 03:25:47 | [diff] [blame] | 68 | DISALLOW_COPY_AND_ASSIGN(CallStackProfileMetricsProvider); |
| 69 | }; |
| 70 | |
| 71 | } // namespace metrics |
| 72 | |
| 73 | #endif // COMPONENTS_METRICS_CALL_STACK_PROFILE_METRICS_PROVIDER_H_ |