blob: d5e4f7e46f040fa8773e5a66a15261097a3166d9 [file] [log] [blame]
wittmand19f5202015-03-24 03:25:471// 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
8#include <vector>
9
asvitkine2d8b08c2017-07-14 22:16:0010#include "base/feature_list.h"
avi26062922015-12-26 00:14:1811#include "base/macros.h"
wittmanae2a08a2015-04-08 02:20:3512#include "base/memory/ref_counted.h"
wittmand19f5202015-03-24 03:25:4713#include "base/profiler/stack_sampling_profiler.h"
wittmane16da2782016-09-21 16:43:0414#include "components/metrics/call_stack_profile_params.h"
wittmand19f5202015-03-24 03:25:4715#include "components/metrics/metrics_provider.h"
16
17namespace metrics {
asvitkine2d8b08c2017-07-14 22:16:0018
wittmand19f5202015-03-24 03:25:4719class ChromeUserMetricsExtension;
20
asvitkine2d8b08c2017-07-14 22:16:0021// Internal to expose functions for testing.
22namespace internal {
23
24// Returns the process uptime as a TimeDelta.
25base::TimeDelta GetUptime();
26
27// Get a callback for use with StackSamplingProfiler that provides completed
28// profiles to this object. The callback should be immediately passed to the
29// StackSamplingProfiler, and should not be reused between
30// StackSamplingProfilers. This function may be called on any thread.
31base::StackSamplingProfiler::CompletedCallback GetProfilerCallback(
32 CallStackProfileParams* params);
33
34} // namespace internal
35
wittmand19f5202015-03-24 03:25:4736// Performs metrics logging for the stack sampling profiler.
37class CallStackProfileMetricsProvider : public MetricsProvider {
38 public:
bcwhiteda097d32017-01-12 23:55:0339 // These milestones of a process lifetime can be passed as process "mile-
40 // stones" to StackSmaplingProfile::SetProcessMilestone(). Be sure to update
41 // the translation constants at the top of the .cc file when this is changed.
42 enum Milestones : int {
bcwhite30c14f22016-11-23 22:56:1443 MAIN_LOOP_START,
44 MAIN_NAVIGATION_START,
45 MAIN_NAVIGATION_FINISHED,
46 FIRST_NONEMPTY_PAINT,
47
48 SHUTDOWN_START,
49
bcwhiteda097d32017-01-12 23:55:0350 MILESTONES_MAX_VALUE
bcwhite30c14f22016-11-23 22:56:1451 };
52
wittmand19f5202015-03-24 03:25:4753 CallStackProfileMetricsProvider();
54 ~CallStackProfileMetricsProvider() override;
55
asvitkine2d8b08c2017-07-14 22:16:0056 // Returns a callback for use with StackSamplingProfiler that sets up
57 // parameters for browser process startup sampling. The callback should be
58 // immediately passed to the StackSamplingProfiler, and should not be reused.
59 static base::StackSamplingProfiler::CompletedCallback
60 GetProfilerCallbackForBrowserProcessStartup();
wittmane16da2782016-09-21 16:43:0461
62 // Provides completed stack profiles to the metrics provider. Intended for use
63 // when receiving profiles over IPC. In-process StackSamplingProfiler users
asvitkine2d8b08c2017-07-14 22:16:0064 // should instead use a variant of GetProfilerCallback*(). |profiles| is not
65 // const& because it must be passed with std::move.
wittmane16da2782016-09-21 16:43:0466 static void ReceiveCompletedProfiles(
asvitkine2d8b08c2017-07-14 22:16:0067 CallStackProfileParams* params,
bcwhitec3c851d2016-10-26 21:20:4268 base::StackSamplingProfiler::CallStackProfiles profiles);
wittman622851e2015-07-31 18:13:4069
asvitkine2d8b08c2017-07-14 22:16:0070 // Whether periodic sampling is enabled via a trial.
71 static bool IsPeriodicSamplingEnabled();
72
wittmand19f5202015-03-24 03:25:4773 // MetricsProvider:
wittmanae2a08a2015-04-08 02:20:3574 void OnRecordingEnabled() override;
75 void OnRecordingDisabled() override;
wittmand19f5202015-03-24 03:25:4776 void ProvideGeneralMetrics(ChromeUserMetricsExtension* uma_proto) override;
77
wittmanae2a08a2015-04-08 02:20:3578 protected:
asvitkine2d8b08c2017-07-14 22:16:0079 // base::Feature for reporting profiles. Provided here for test use.
80 static const base::Feature kEnableReporting;
wittmanae2a08a2015-04-08 02:20:3581
wittman622851e2015-07-31 18:13:4082 // Reset the static state to the defaults after startup.
83 static void ResetStaticStateForTesting();
84
wittmand19f5202015-03-24 03:25:4785 private:
wittmanae2a08a2015-04-08 02:20:3586 // Returns true if reporting of profiles is enabled according to the
87 // controlling Finch field trial.
wittman622851e2015-07-31 18:13:4088 static bool IsReportingEnabledByFieldTrial();
wittmand19f5202015-03-24 03:25:4789
90 DISALLOW_COPY_AND_ASSIGN(CallStackProfileMetricsProvider);
91};
92
93} // namespace metrics
94
95#endif // COMPONENTS_METRICS_CALL_STACK_PROFILE_METRICS_PROVIDER_H_