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" |
Mike Wittman | 56a735e | 2019-11-04 23:54:13 | [diff] [blame] | 16 | #include "base/optional.h" |
Charlie Andrews | 584d90842 | 2019-04-25 21:35:37 | [diff] [blame] | 17 | #include "base/profiler/metadata_recorder.h" |
Mike Wittman | 9606835b | 2019-03-26 17:08:06 | [diff] [blame] | 18 | #include "base/profiler/profile_builder.h" |
Xi Cheng | eb46484d | 2018-08-15 01:00:28 | [diff] [blame] | 19 | #include "base/sampling_heap_profiler/module_cache.h" |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 20 | #include "base/time/time.h" |
Xi Cheng | 4dec7e4 | 2018-08-10 16:54:11 | [diff] [blame] | 21 | #include "components/metrics/call_stack_profile_params.h" |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 22 | #include "components/metrics/child_call_stack_profile_collector.h" |
Julie Jeongeun Kim | e91fd9b | 2019-09-24 02:45:53 | [diff] [blame] | 23 | #include "mojo/public/cpp/bindings/pending_remote.h" |
Xi Cheng | 4dec7e4 | 2018-08-10 16:54:11 | [diff] [blame] | 24 | #include "third_party/metrics_proto/sampled_profile.pb.h" |
Xi Cheng | 2e07c5d | 2018-07-03 21:01:29 | [diff] [blame] | 25 | |
Xi Cheng | cbeecd90 | 2018-07-05 01:34:56 | [diff] [blame] | 26 | namespace metrics { |
| 27 | |
Mike Wittman | cb1067c | 2019-01-24 19:04:00 | [diff] [blame] | 28 | // Interface that allows the CallStackProfileBuilder to provide ids for distinct |
| 29 | // work items. Samples with the same id are tagged as coming from the same work |
| 30 | // item in the recorded samples. |
| 31 | class WorkIdRecorder { |
| 32 | public: |
| 33 | WorkIdRecorder() = default; |
| 34 | virtual ~WorkIdRecorder() = default; |
| 35 | |
| 36 | // This function is invoked on the profiler thread while the target thread is |
| 37 | // suspended so must not take any locks, including indirectly through use of |
| 38 | // heap allocation, LOG, CHECK, or DCHECK. |
| 39 | virtual unsigned int RecordWorkId() const = 0; |
| 40 | |
| 41 | WorkIdRecorder(const WorkIdRecorder&) = delete; |
| 42 | WorkIdRecorder& operator=(const WorkIdRecorder&) = delete; |
| 43 | }; |
| 44 | |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 45 | // An instance of the class is meant to be passed to base::StackSamplingProfiler |
| 46 | // to collect profiles. The profiles collected are uploaded via the metrics log. |
Xi Cheng | b2e858e5 | 2018-09-11 20:58:03 | [diff] [blame] | 47 | // |
| 48 | // This uses the new StackSample encoding rather than the legacy Sample |
| 49 | // encoding. |
Mike Wittman | 9606835b | 2019-03-26 17:08:06 | [diff] [blame] | 50 | class CallStackProfileBuilder : public base::ProfileBuilder { |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 51 | public: |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 52 | // |completed_callback| is made when sampling a profile completes. Other |
| 53 | // threads, including the UI thread, may block on callback completion so this |
| 54 | // should run as quickly as possible. |
| 55 | // |
| 56 | // IMPORTANT NOTE: The callback is invoked on a thread the profiler |
| 57 | // constructs, rather than on the thread used to construct the profiler, and |
| 58 | // thus the callback must be callable on any thread. |
Alexei Filippov | dbbde60 | 2018-09-08 00:17:26 | [diff] [blame] | 59 | explicit CallStackProfileBuilder( |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 60 | const CallStackProfileParams& profile_params, |
Mike Wittman | cb1067c | 2019-01-24 19:04:00 | [diff] [blame] | 61 | const WorkIdRecorder* work_id_recorder = nullptr, |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 62 | base::OnceClosure completed_callback = base::OnceClosure()); |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 63 | |
| 64 | ~CallStackProfileBuilder() override; |
| 65 | |
Alexei Filippov | a8d3aba | 2019-05-24 18:20:57 | [diff] [blame] | 66 | // Both weight and count are used by the heap profiler only. |
| 67 | void OnSampleCompleted(std::vector<base::Frame> frames, |
| 68 | size_t weight, |
| 69 | size_t count); |
Alexei Filippov | a0f7250 | 2019-05-22 22:10:44 | [diff] [blame] | 70 | |
Mike Wittman | 9606835b | 2019-03-26 17:08:06 | [diff] [blame] | 71 | // base::ProfileBuilder: |
Mike Wittman | b5121e9 | 2019-02-19 22:39:41 | [diff] [blame] | 72 | base::ModuleCache* GetModuleCache() override; |
Mike Wittman | eca368e | 2019-11-05 00:32:02 | [diff] [blame] | 73 | void RecordMetadata( |
| 74 | base::ProfileBuilder::MetadataProvider* metadata_provider) override; |
Mike Wittman | c51cafa | 2019-04-04 15:48:01 | [diff] [blame] | 75 | void OnSampleCompleted(std::vector<base::Frame> frames) override; |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 76 | void OnProfileCompleted(base::TimeDelta profile_duration, |
| 77 | base::TimeDelta sampling_period) override; |
| 78 | |
Mike Wittman | 2943c9c7 | 2018-08-31 19:28:57 | [diff] [blame] | 79 | // Sets the callback to use for reporting browser process profiles. This |
| 80 | // indirection is required to avoid a dependency on unnecessary metrics code |
| 81 | // in child processes. |
| 82 | static void SetBrowserProcessReceiverCallback( |
| 83 | const base::RepeatingCallback<void(base::TimeTicks, SampledProfile)>& |
| 84 | callback); |
| 85 | |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 86 | // Sets the CallStackProfileCollector interface from |browser_interface|. |
| 87 | // This function must be called within child processes. |
| 88 | static void SetParentProfileCollectorForChildProcess( |
Julie Jeongeun Kim | e91fd9b | 2019-09-24 02:45:53 | [diff] [blame] | 89 | mojo::PendingRemote<metrics::mojom::CallStackProfileCollector> |
| 90 | browser_interface); |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 91 | |
| 92 | protected: |
| 93 | // Test seam. |
| 94 | virtual void PassProfilesToMetricsProvider(SampledProfile sampled_profile); |
| 95 | |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 96 | private: |
Xi Cheng | b2e858e5 | 2018-09-11 20:58:03 | [diff] [blame] | 97 | // The functor for Stack comparison. |
| 98 | struct StackComparer { |
| 99 | bool operator()(const CallStackProfile::Stack* stack1, |
| 100 | const CallStackProfile::Stack* stack2) const; |
| 101 | }; |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 102 | |
Mike Wittman | 56a735e | 2019-11-04 23:54:13 | [diff] [blame] | 103 | // Comparison function for the metadata map. |
| 104 | struct MetadataKey; |
| 105 | struct MetadataKeyCompare { |
| 106 | bool operator()(const MetadataKey& a, const MetadataKey& b) const; |
| 107 | }; |
| 108 | |
| 109 | // Definitions for a map-based representation of sample metadata. |
| 110 | struct MetadataKey { |
| 111 | MetadataKey(uint64_t name_hash, base::Optional<int64_t> key); |
| 112 | |
| 113 | MetadataKey(const MetadataKey& other); |
| 114 | MetadataKey& operator=(const MetadataKey& other); |
| 115 | |
| 116 | // The name_hash and optional user-specified key uniquely identifies a |
| 117 | // metadata value. See base::MetadataRecorder for details. |
| 118 | uint64_t name_hash; |
| 119 | base::Optional<int64_t> key; |
| 120 | }; |
| 121 | using MetadataMap = std::map<MetadataKey, int64_t, MetadataKeyCompare>; |
| 122 | |
| 123 | // Creates the metdata map from the array of items. |
| 124 | MetadataMap CreateMetadataMap(base::ProfileBuilder::MetadataItemArray items, |
| 125 | size_t item_count); |
| 126 | |
| 127 | // Returns all metadata items with new values in the current sample. |
| 128 | MetadataMap GetNewOrModifiedMetadataItems(const MetadataMap& current_items, |
| 129 | const MetadataMap& previous_items); |
| 130 | |
| 131 | // Returns all metadata items deleted since the previous sample. |
| 132 | MetadataMap GetDeletedMetadataItems(const MetadataMap& current_items, |
| 133 | const MetadataMap& previous_items); |
| 134 | |
Mike Wittman | e3d6b448 | 2019-11-05 00:38:05 | [diff] [blame] | 135 | // Creates MetadataItems for the currently active metadata, adding new name |
| 136 | // hashes to |metadata_name_hashes| if necessary. The same |
| 137 | // |metadata_name_hashes| must be passed to each invocation, and must not be |
| 138 | // modified outside this function. |
| 139 | google::protobuf::RepeatedPtrField<CallStackProfile::MetadataItem> |
| 140 | CreateSampleMetadata( |
| 141 | google::protobuf::RepeatedField<uint64_t>* metadata_name_hashes); |
Charlie Andrews | 0bfceb2 | 2019-04-29 17:23:24 | [diff] [blame] | 142 | |
Mike Wittman | e3d6b448 | 2019-11-05 00:38:05 | [diff] [blame] | 143 | // Appends the |name_hash| to |name_hashes| if it's not already |
| 144 | // present. Returns its index in |name_hashes|. |
| 145 | size_t MaybeAppendNameHash( |
| 146 | uint64_t name_hash, |
| 147 | google::protobuf::RepeatedField<uint64_t>* metadata_name_hashes); |
Charlie Andrews | 0bfceb2 | 2019-04-29 17:23:24 | [diff] [blame] | 148 | |
Mike Wittman | b5121e9 | 2019-02-19 22:39:41 | [diff] [blame] | 149 | // The module cache to use for the duration the sampling associated with this |
| 150 | // ProfileBuilder. |
| 151 | base::ModuleCache module_cache_; |
| 152 | |
Mike Wittman | cb1067c | 2019-01-24 19:04:00 | [diff] [blame] | 153 | unsigned int last_work_id_ = std::numeric_limits<unsigned int>::max(); |
| 154 | bool is_continued_work_ = false; |
| 155 | const WorkIdRecorder* const work_id_recorder_; |
| 156 | |
Xi Cheng | b2e858e5 | 2018-09-11 20:58:03 | [diff] [blame] | 157 | // The SampledProfile protobuf message which contains the collected stack |
| 158 | // samples. |
| 159 | SampledProfile sampled_profile_; |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 160 | |
Xi Cheng | b2e858e5 | 2018-09-11 20:58:03 | [diff] [blame] | 161 | // The indexes of stacks, indexed by stack's address. |
| 162 | std::map<const CallStackProfile::Stack*, int, StackComparer> stack_index_; |
Xi Cheng | e91f6fd | 2018-08-14 16:34:56 | [diff] [blame] | 163 | |
Mike Wittman | 4ad741e | 2019-02-22 23:48:21 | [diff] [blame] | 164 | // The indexes of modules in the modules_ vector below.. |
| 165 | std::unordered_map<const base::ModuleCache::Module*, size_t> module_index_; |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 166 | |
Xi Cheng | e91f6fd | 2018-08-14 16:34:56 | [diff] [blame] | 167 | // The distinct modules in the current profile. |
Mike Wittman | 4ad741e | 2019-02-22 23:48:21 | [diff] [blame] | 168 | std::vector<const base::ModuleCache::Module*> modules_; |
Xi Cheng | e91f6fd | 2018-08-14 16:34:56 | [diff] [blame] | 169 | |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 170 | // Callback made when sampling a profile completes. |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 171 | base::OnceClosure completed_callback_; |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 172 | |
Xi Cheng | 9358aa09 | 2018-08-28 16:11:12 | [diff] [blame] | 173 | // The start time of a profile collection. |
| 174 | const base::TimeTicks profile_start_time_; |
| 175 | |
Charlie Andrews | 0bfceb2 | 2019-04-29 17:23:24 | [diff] [blame] | 176 | // The data fetched from the MetadataRecorder for the next sample. |
Charlie Andrews | 81c58dc | 2019-07-11 02:53:46 | [diff] [blame] | 177 | base::ProfileBuilder::MetadataItemArray metadata_items_; |
Charlie Andrews | 72bc22f6 | 2019-04-16 19:19:01 | [diff] [blame] | 178 | size_t metadata_item_count_ = 0; |
Mike Wittman | 56a735e | 2019-11-04 23:54:13 | [diff] [blame] | 179 | |
Charlie Andrews | 0bfceb2 | 2019-04-29 17:23:24 | [diff] [blame] | 180 | // The data fetched from the MetadataRecorder for the previous sample. |
Mike Wittman | 56a735e | 2019-11-04 23:54:13 | [diff] [blame] | 181 | MetadataMap previous_items_; |
Charlie Andrews | 72bc22f6 | 2019-04-16 19:19:01 | [diff] [blame] | 182 | |
Alexei Filippov | 0f15e7a | 2019-02-14 21:34:28 | [diff] [blame] | 183 | // Maps metadata hash to index in |metadata_name_hash| array. |
| 184 | std::unordered_map<uint64_t, int> metadata_hashes_cache_; |
| 185 | |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 186 | DISALLOW_COPY_AND_ASSIGN(CallStackProfileBuilder); |
| 187 | }; |
| 188 | |
Xi Cheng | cbeecd90 | 2018-07-05 01:34:56 | [diff] [blame] | 189 | } // namespace metrics |
| 190 | |
Xi Cheng | 859dfcc | 2018-07-02 23:06:41 | [diff] [blame] | 191 | #endif // COMPONENTS_METRICS_CALL_STACK_PROFILE_BUILDER_H_ |