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 | |
Mike Wittman | cb1067c | 2019-01-24 19:04:00 | [diff] [blame^] | 8 | #include <limits> |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 9 | #include <map> |
Xi Cheng | e91f6fd | 2018-08-14 16:34:56 | [diff] [blame] | 10 | #include <vector> |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 11 | |
Xi Cheng | 2e07c5d | 2018-07-03 21:01:29 | [diff] [blame] | 12 | #include "base/callback.h" |
Xi Cheng | 367942c | 2018-08-03 02:43:19 | [diff] [blame] | 13 | #include "base/profiler/stack_sampling_profiler.h" |
Xi Cheng | eb46484d | 2018-08-15 01:00:28 | [diff] [blame] | 14 | #include "base/sampling_heap_profiler/module_cache.h" |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 15 | #include "base/time/time.h" |
Xi Cheng | 4dec7e4 | 2018-08-10 16:54:11 | [diff] [blame] | 16 | #include "components/metrics/call_stack_profile_params.h" |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 17 | #include "components/metrics/child_call_stack_profile_collector.h" |
Xi Cheng | 4dec7e4 | 2018-08-10 16:54:11 | [diff] [blame] | 18 | #include "third_party/metrics_proto/sampled_profile.pb.h" |
Xi Cheng | 2e07c5d | 2018-07-03 21:01:29 | [diff] [blame] | 19 | |
Xi Cheng | cbeecd90 | 2018-07-05 01:34:56 | [diff] [blame] | 20 | namespace metrics { |
| 21 | |
Mike Wittman | cb1067c | 2019-01-24 19:04:00 | [diff] [blame^] | 22 | // Interface that allows the CallStackProfileBuilder to provide ids for distinct |
| 23 | // work items. Samples with the same id are tagged as coming from the same work |
| 24 | // item in the recorded samples. |
| 25 | class WorkIdRecorder { |
| 26 | public: |
| 27 | WorkIdRecorder() = default; |
| 28 | virtual ~WorkIdRecorder() = default; |
| 29 | |
| 30 | // This function is invoked on the profiler thread while the target thread is |
| 31 | // suspended so must not take any locks, including indirectly through use of |
| 32 | // heap allocation, LOG, CHECK, or DCHECK. |
| 33 | virtual unsigned int RecordWorkId() const = 0; |
| 34 | |
| 35 | WorkIdRecorder(const WorkIdRecorder&) = delete; |
| 36 | WorkIdRecorder& operator=(const WorkIdRecorder&) = delete; |
| 37 | }; |
| 38 | |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 39 | // An instance of the class is meant to be passed to base::StackSamplingProfiler |
| 40 | // to collect profiles. The profiles collected are uploaded via the metrics log. |
Xi Cheng | b2e858e5 | 2018-09-11 20:58:03 | [diff] [blame] | 41 | // |
| 42 | // This uses the new StackSample encoding rather than the legacy Sample |
| 43 | // encoding. |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 44 | class CallStackProfileBuilder |
| 45 | : public base::StackSamplingProfiler::ProfileBuilder { |
| 46 | public: |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 47 | // |completed_callback| is made when sampling a profile completes. Other |
| 48 | // threads, including the UI thread, may block on callback completion so this |
| 49 | // should run as quickly as possible. |
| 50 | // |
| 51 | // IMPORTANT NOTE: The callback is invoked on a thread the profiler |
| 52 | // constructs, rather than on the thread used to construct the profiler, and |
| 53 | // thus the callback must be callable on any thread. |
Alexei Filippov | dbbde60 | 2018-09-08 00:17:26 | [diff] [blame] | 54 | explicit CallStackProfileBuilder( |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 55 | const CallStackProfileParams& profile_params, |
Mike Wittman | cb1067c | 2019-01-24 19:04:00 | [diff] [blame^] | 56 | const WorkIdRecorder* work_id_recorder = nullptr, |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 57 | base::OnceClosure completed_callback = base::OnceClosure()); |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 58 | |
| 59 | ~CallStackProfileBuilder() override; |
| 60 | |
| 61 | // base::StackSamplingProfiler::ProfileBuilder: |
Mike Wittman | cb1067c | 2019-01-24 19:04:00 | [diff] [blame^] | 62 | void RecordMetadata() override; |
Xi Cheng | e91f6fd | 2018-08-14 16:34:56 | [diff] [blame] | 63 | void OnSampleCompleted( |
| 64 | std::vector<base::StackSamplingProfiler::Frame> frames) override; |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 65 | void OnProfileCompleted(base::TimeDelta profile_duration, |
| 66 | base::TimeDelta sampling_period) override; |
| 67 | |
Mike Wittman | 2943c9c7 | 2018-08-31 19:28:57 | [diff] [blame] | 68 | // Sets the callback to use for reporting browser process profiles. This |
| 69 | // indirection is required to avoid a dependency on unnecessary metrics code |
| 70 | // in child processes. |
| 71 | static void SetBrowserProcessReceiverCallback( |
| 72 | const base::RepeatingCallback<void(base::TimeTicks, SampledProfile)>& |
| 73 | callback); |
| 74 | |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 75 | // Sets the CallStackProfileCollector interface from |browser_interface|. |
| 76 | // This function must be called within child processes. |
| 77 | static void SetParentProfileCollectorForChildProcess( |
| 78 | metrics::mojom::CallStackProfileCollectorPtr browser_interface); |
| 79 | |
| 80 | protected: |
| 81 | // Test seam. |
| 82 | virtual void PassProfilesToMetricsProvider(SampledProfile sampled_profile); |
| 83 | |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 84 | private: |
Xi Cheng | b2e858e5 | 2018-09-11 20:58:03 | [diff] [blame] | 85 | // The functor for Stack comparison. |
| 86 | struct StackComparer { |
| 87 | bool operator()(const CallStackProfile::Stack* stack1, |
| 88 | const CallStackProfile::Stack* stack2) const; |
| 89 | }; |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 90 | |
Mike Wittman | cb1067c | 2019-01-24 19:04:00 | [diff] [blame^] | 91 | unsigned int last_work_id_ = std::numeric_limits<unsigned int>::max(); |
| 92 | bool is_continued_work_ = false; |
| 93 | const WorkIdRecorder* const work_id_recorder_; |
| 94 | |
Xi Cheng | b2e858e5 | 2018-09-11 20:58:03 | [diff] [blame] | 95 | // The SampledProfile protobuf message which contains the collected stack |
| 96 | // samples. |
| 97 | SampledProfile sampled_profile_; |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 98 | |
Xi Cheng | b2e858e5 | 2018-09-11 20:58:03 | [diff] [blame] | 99 | // The indexes of stacks, indexed by stack's address. |
| 100 | std::map<const CallStackProfile::Stack*, int, StackComparer> stack_index_; |
Xi Cheng | e91f6fd | 2018-08-14 16:34:56 | [diff] [blame] | 101 | |
| 102 | // The indexes of modules, indexed by module's base_address. |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 103 | std::map<uintptr_t, size_t> module_index_; |
| 104 | |
Xi Cheng | e91f6fd | 2018-08-14 16:34:56 | [diff] [blame] | 105 | // The distinct modules in the current profile. |
Xi Cheng | eb46484d | 2018-08-15 01:00:28 | [diff] [blame] | 106 | std::vector<base::ModuleCache::Module> modules_; |
Xi Cheng | e91f6fd | 2018-08-14 16:34:56 | [diff] [blame] | 107 | |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 108 | // Callback made when sampling a profile completes. |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 109 | base::OnceClosure completed_callback_; |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 110 | |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 111 | // The start time of a profile collection. |
| 112 | const base::TimeTicks profile_start_time_; |
| 113 | |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 114 | DISALLOW_COPY_AND_ASSIGN(CallStackProfileBuilder); |
| 115 | }; |
| 116 | |
Xi Cheng | cbeecd90 | 2018-07-05 01:34:56 | [diff] [blame] | 117 | } // namespace metrics |
| 118 | |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 119 | #endif // COMPONENTS_METRICS_CALL_STACK_PROFILE_BUILDER_H_ |