blob: e0beea9f0be418c1d4fb4d20503157c9e2843183 [file] [log] [blame]
Xi Cheng859dfcc2018-07-02 23:06:411// Copyright 2018 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_BUILDER_H_
6#define COMPONENTS_METRICS_CALL_STACK_PROFILE_BUILDER_H_
7
Xi Cheng859dfcc2018-07-02 23:06:418#include <map>
Xi Chenge91f6fd2018-08-14 16:34:569#include <vector>
Xi Cheng859dfcc2018-07-02 23:06:4110
Xi Cheng2e07c5d2018-07-03 21:01:2911#include "base/callback.h"
Xi Cheng367942c2018-08-03 02:43:1912#include "base/profiler/stack_sampling_profiler.h"
Xi Chengeb46484d2018-08-15 01:00:2813#include "base/sampling_heap_profiler/module_cache.h"
Xi Cheng9358aa092018-08-28 16:11:1214#include "base/time/time.h"
Xi Cheng4dec7e42018-08-10 16:54:1115#include "components/metrics/call_stack_profile_params.h"
Xi Cheng9358aa092018-08-28 16:11:1216#include "components/metrics/child_call_stack_profile_collector.h"
Xi Cheng4dec7e42018-08-10 16:54:1117#include "third_party/metrics_proto/sampled_profile.pb.h"
Xi Cheng2e07c5d2018-07-03 21:01:2918
Xi Chengcbeecd902018-07-05 01:34:5619namespace metrics {
20
Xi Cheng9358aa092018-08-28 16:11:1221// An instance of the class is meant to be passed to base::StackSamplingProfiler
22// to collect profiles. The profiles collected are uploaded via the metrics log.
Xi Chengb2e858e52018-09-11 20:58:0323//
24// This uses the new StackSample encoding rather than the legacy Sample
25// encoding.
Xi Cheng859dfcc2018-07-02 23:06:4126class CallStackProfileBuilder
27 : public base::StackSamplingProfiler::ProfileBuilder {
28 public:
Xi Cheng9358aa092018-08-28 16:11:1229 // |completed_callback| is made when sampling a profile completes. Other
30 // threads, including the UI thread, may block on callback completion so this
31 // should run as quickly as possible.
32 //
33 // IMPORTANT NOTE: The callback is invoked on a thread the profiler
34 // constructs, rather than on the thread used to construct the profiler, and
35 // thus the callback must be callable on any thread.
Alexei Filippovdbbde602018-09-08 00:17:2636 explicit CallStackProfileBuilder(
Xi Cheng9358aa092018-08-28 16:11:1237 const CallStackProfileParams& profile_params,
38 base::OnceClosure completed_callback = base::OnceClosure());
Xi Cheng859dfcc2018-07-02 23:06:4139
40 ~CallStackProfileBuilder() override;
41
42 // base::StackSamplingProfiler::ProfileBuilder:
Xi Chenge91f6fd2018-08-14 16:34:5643 void OnSampleCompleted(
44 std::vector<base::StackSamplingProfiler::Frame> frames) override;
Xi Cheng859dfcc2018-07-02 23:06:4145 void OnProfileCompleted(base::TimeDelta profile_duration,
46 base::TimeDelta sampling_period) override;
47
Alexei Filippov750c6f32018-09-08 02:17:4348 // The function is used by sampling heap profiler. Its samples already come
49 // with different counts.
50 void OnSampleCompleted(std::vector<base::StackSamplingProfiler::Frame> frames,
51 size_t count);
52
Mike Wittman2943c9c72018-08-31 19:28:5753 // Sets the callback to use for reporting browser process profiles. This
54 // indirection is required to avoid a dependency on unnecessary metrics code
55 // in child processes.
56 static void SetBrowserProcessReceiverCallback(
57 const base::RepeatingCallback<void(base::TimeTicks, SampledProfile)>&
58 callback);
59
Xi Cheng9358aa092018-08-28 16:11:1260 // Sets the CallStackProfileCollector interface from |browser_interface|.
61 // This function must be called within child processes.
62 static void SetParentProfileCollectorForChildProcess(
63 metrics::mojom::CallStackProfileCollectorPtr browser_interface);
64
65 protected:
66 // Test seam.
67 virtual void PassProfilesToMetricsProvider(SampledProfile sampled_profile);
68
Xi Cheng859dfcc2018-07-02 23:06:4169 private:
Xi Chengb2e858e52018-09-11 20:58:0370 // The functor for Stack comparison.
71 struct StackComparer {
72 bool operator()(const CallStackProfile::Stack* stack1,
73 const CallStackProfile::Stack* stack2) const;
74 };
Xi Cheng859dfcc2018-07-02 23:06:4175
Xi Chengb2e858e52018-09-11 20:58:0376 // The SampledProfile protobuf message which contains the collected stack
77 // samples.
78 SampledProfile sampled_profile_;
Xi Cheng859dfcc2018-07-02 23:06:4179
Xi Chengb2e858e52018-09-11 20:58:0380 // The indexes of stacks, indexed by stack's address.
81 std::map<const CallStackProfile::Stack*, int, StackComparer> stack_index_;
Xi Chenge91f6fd2018-08-14 16:34:5682
83 // The indexes of modules, indexed by module's base_address.
Xi Cheng859dfcc2018-07-02 23:06:4184 std::map<uintptr_t, size_t> module_index_;
85
Xi Chenge91f6fd2018-08-14 16:34:5686 // The distinct modules in the current profile.
Xi Chengeb46484d2018-08-15 01:00:2887 std::vector<base::ModuleCache::Module> modules_;
Xi Chenge91f6fd2018-08-14 16:34:5688
Xi Cheng859dfcc2018-07-02 23:06:4189 // Callback made when sampling a profile completes.
Xi Cheng9358aa092018-08-28 16:11:1290 base::OnceClosure completed_callback_;
Xi Cheng859dfcc2018-07-02 23:06:4191
Xi Cheng9358aa092018-08-28 16:11:1292 // The start time of a profile collection.
93 const base::TimeTicks profile_start_time_;
94
Xi Cheng859dfcc2018-07-02 23:06:4195 DISALLOW_COPY_AND_ASSIGN(CallStackProfileBuilder);
96};
97
Xi Chengcbeecd902018-07-05 01:34:5698} // namespace metrics
99
Xi Cheng859dfcc2018-07-02 23:06:41100#endif // COMPONENTS_METRICS_CALL_STACK_PROFILE_BUILDER_H_