Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 1 | // 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 Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 8 | #include <map> |
Xi Cheng | e91f6fd | 2018-08-14 16:34:56 | [diff] [blame] | 9 | #include <vector> |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 10 | |
Xi Cheng | 2e07c5d | 2018-07-03 21:01:29 | [diff] [blame] | 11 | #include "base/callback.h" |
Xi Cheng | 367942c | 2018-08-03 02:43:19 | [diff] [blame] | 12 | #include "base/profiler/stack_sampling_profiler.h" |
Xi Cheng | eb46484d | 2018-08-15 01:00:28 | [diff] [blame] | 13 | #include "base/sampling_heap_profiler/module_cache.h" |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 14 | #include "base/time/time.h" |
Xi Cheng | 4dec7e4 | 2018-08-10 16:54:11 | [diff] [blame] | 15 | #include "components/metrics/call_stack_profile_params.h" |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 16 | #include "components/metrics/child_call_stack_profile_collector.h" |
Xi Cheng | 4dec7e4 | 2018-08-10 16:54:11 | [diff] [blame] | 17 | #include "third_party/metrics_proto/sampled_profile.pb.h" |
Xi Cheng | 2e07c5d | 2018-07-03 21:01:29 | [diff] [blame] | 18 | |
Xi Cheng | cbeecd90 | 2018-07-05 01:34:56 | [diff] [blame] | 19 | namespace metrics { |
| 20 | |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 21 | // 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 Cheng | b2e858e5 | 2018-09-11 20:58:03 | [diff] [blame] | 23 | // |
| 24 | // This uses the new StackSample encoding rather than the legacy Sample |
| 25 | // encoding. |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 26 | class CallStackProfileBuilder |
| 27 | : public base::StackSamplingProfiler::ProfileBuilder { |
| 28 | public: |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 29 | // |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 Filippov | dbbde60 | 2018-09-08 00:17:26 | [diff] [blame] | 36 | explicit CallStackProfileBuilder( |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 37 | const CallStackProfileParams& profile_params, |
| 38 | base::OnceClosure completed_callback = base::OnceClosure()); |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 39 | |
| 40 | ~CallStackProfileBuilder() override; |
| 41 | |
| 42 | // base::StackSamplingProfiler::ProfileBuilder: |
Xi Cheng | e91f6fd | 2018-08-14 16:34:56 | [diff] [blame] | 43 | void OnSampleCompleted( |
| 44 | std::vector<base::StackSamplingProfiler::Frame> frames) override; |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 45 | void OnProfileCompleted(base::TimeDelta profile_duration, |
| 46 | base::TimeDelta sampling_period) override; |
| 47 | |
Alexei Filippov | 750c6f3 | 2018-09-08 02:17:43 | [diff] [blame] | 48 | // 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 Wittman | 2943c9c7 | 2018-08-31 19:28:57 | [diff] [blame] | 53 | // 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 Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 60 | // 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 Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 69 | private: |
Xi Cheng | b2e858e5 | 2018-09-11 20:58:03 | [diff] [blame] | 70 | // The functor for Stack comparison. |
| 71 | struct StackComparer { |
| 72 | bool operator()(const CallStackProfile::Stack* stack1, |
| 73 | const CallStackProfile::Stack* stack2) const; |
| 74 | }; |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 75 | |
Xi Cheng | b2e858e5 | 2018-09-11 20:58:03 | [diff] [blame] | 76 | // The SampledProfile protobuf message which contains the collected stack |
| 77 | // samples. |
| 78 | SampledProfile sampled_profile_; |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 79 | |
Xi Cheng | b2e858e5 | 2018-09-11 20:58:03 | [diff] [blame] | 80 | // The indexes of stacks, indexed by stack's address. |
| 81 | std::map<const CallStackProfile::Stack*, int, StackComparer> stack_index_; |
Xi Cheng | e91f6fd | 2018-08-14 16:34:56 | [diff] [blame] | 82 | |
| 83 | // The indexes of modules, indexed by module's base_address. |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 84 | std::map<uintptr_t, size_t> module_index_; |
| 85 | |
Xi Cheng | e91f6fd | 2018-08-14 16:34:56 | [diff] [blame] | 86 | // The distinct modules in the current profile. |
Xi Cheng | eb46484d | 2018-08-15 01:00:28 | [diff] [blame] | 87 | std::vector<base::ModuleCache::Module> modules_; |
Xi Cheng | e91f6fd | 2018-08-14 16:34:56 | [diff] [blame] | 88 | |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 89 | // Callback made when sampling a profile completes. |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 90 | base::OnceClosure completed_callback_; |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 91 | |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 92 | // The start time of a profile collection. |
| 93 | const base::TimeTicks profile_start_time_; |
| 94 | |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 95 | DISALLOW_COPY_AND_ASSIGN(CallStackProfileBuilder); |
| 96 | }; |
| 97 | |
Xi Cheng | cbeecd90 | 2018-07-05 01:34:56 | [diff] [blame] | 98 | } // namespace metrics |
| 99 | |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 100 | #endif // COMPONENTS_METRICS_CALL_STACK_PROFILE_BUILDER_H_ |