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