| 6 | #define BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
| 7 |
| 8 | #include <memory> |
| 9 | #include <vector> |
| 10 |
| 11 | #include "base/base_export.h" |
| 12 | #include "base/macros.h" |
| 13 | #include "base/optional.h" |
| 14 | #include "base/profiler/profile_builder.h" |
| 15 | #include "base/profiler/sampling_profiler_thread_token.h" |
| 16 | #include "base/synchronization/waitable_event.h" |
| 17 | #include "base/threading/platform_thread.h" |
| 18 | #include "base/time/time.h" |
| 19 |
| 20 | namespace base { |
| 21 |
| 22 | class Unwinder; |
| 23 | class StackSampler; |
| 24 | class StackSamplerTestDelegate; |
| 25 |
| 26 | // StackSamplingProfiler periodically stops a thread to sample its stack, for |
| 27 | // the purpose of collecting information about which code paths are |
| 28 | // executing. This information is used in aggregate by UMA to identify hot |
| 29 | // and/or janky code paths. |
| 30 | // |
| 31 | // Sample StackSamplingProfiler usage: |
| 32 | // |
| 33 | // // Create and customize params as desired. |
| 34 | // base::StackStackSamplingProfiler::SamplingParams params; |
| 35 | // |
| 36 | // // To process the profiles, use a custom ProfileBuilder subclass: |
| 37 | // class SubProfileBuilder : public base::ProfileBuilder {...} |
| 38 | // |
| 39 | // // Then create the profiler: |
| 40 | // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId(), |
| 41 | // params, std::make_unique<SubProfileBuilder>(...)); |
| 42 | // |
| 43 | // // On Android the |sampler| is not implemented in base. So, client can pass |
| 44 | // // in |sampler| to use while profiling. |
| 45 | // base::StackSamplingProfiler profiler(base::PlatformThread::CurrentId(), |
| 46 | // params, std::make_unique<SubProfileBuilder>(...), <optional> sampler); |
| 47 | // |
| 48 | // // Then start the profiling. |
| 49 | // profiler.Start(); |
| 50 | // // ... work being done on the target thread here ... |
| 51 | // // Optionally stop collection before complete per params. |
| 52 | // profiler.Stop(); |
| 53 | // |
| 54 | // The default SamplingParams causes stacks to be recorded in a single profile |
| 55 | // at a 10Hz interval for a total of 30 seconds. All of these parameters may be |
| 56 | // altered as desired. |
| 57 | // |
| 58 | // When a call stack profile is complete, or the profiler is stopped, |
| 59 | // ProfileBuilder's OnProfileCompleted function is called from a thread created |
| 60 | // by the profiler. |
| 61 | class BASE_EXPORT StackSamplingProfiler { |
| 62 | public: |
| 63 | // Represents parameters that configure the sampling. |
| 64 | struct BASE_EXPORT SamplingParams { |
| 65 | // Time to delay before first samples are taken. |
| 66 | TimeDelta initial_delay = TimeDelta::FromMilliseconds(0); |
| 67 |
| 68 | // Number of samples to record per profile. |
| 69 | int samples_per_profile = 300; |
| 70 |
| 71 | // Interval between samples during a sampling profile. This is the desired |
| 72 | // duration from the start of one sample to the start of the next sample. |
| 73 | TimeDelta sampling_interval = TimeDelta::FromMilliseconds(100); |
| 74 |
| 75 | // When true, keeps the average sampling interval = |sampling_interval|, |
| 76 | // irrespective of how long each sample takes. If a sample takes too long, |
| 77 | // keeping the interval constant will lock out the sampled thread. When |
| 78 | // false, sample is created with an interval of |sampling_interval|, |
| 79 | // excluding the time taken by a sample. The metrics collected will not be |
| 80 | // accurate, since sampling could take arbitrary amount of time, but makes |
| 81 | // sure that the sampled thread gets at least the interval amount of time to |
| 82 | // run between samples. |
| 83 | bool keep_consistent_sampling_interval = true; |
| 84 | }; |
| 85 |
| 86 | // Creates a profiler for the specified thread. An optional |test_delegate| |
| 87 | // can be supplied by tests. |
| 88 | // |
| 89 | // The caller must ensure that this object gets destroyed before the thread |
| 90 | // exits. |
| 91 | StackSamplingProfiler(SamplingProfilerThreadToken thread_token, |
| 92 | const SamplingParams& params, |
| 93 | std::unique_ptr<ProfileBuilder> profile_builder, |
| 94 | StackSamplerTestDelegate* test_delegate = nullptr); |
| 95 |
| 96 | // Same as above function, with custom |sampler| implementation. The sampler |
| 97 | // on Android is not implemented in base. |
| 98 | StackSamplingProfiler(const SamplingParams& params, |
| 99 | std::unique_ptr<ProfileBuilder> profile_builder, |
| 100 | std::unique_ptr<StackSampler> sampler); |
| 101 |
| 102 | // Stops any profiling currently taking place before destroying the profiler. |
| 103 | // This will block until profile_builder_'s OnProfileCompleted function has |
| 104 | // executed if profiling has started but not already finished. |
| 105 | ~StackSamplingProfiler(); |
| 106 |
| 107 | // Initializes the profiler and starts sampling. Might block on a |
| 108 | // WaitableEvent if this StackSamplingProfiler was previously started and |
| 109 | // recently stopped, while the previous profiling phase winds down. |
| 110 | void Start(); |
| 111 |
| 112 | // Stops the profiler and any ongoing sampling. This method will return |
| 113 | // immediately with the profile_builder_'s OnProfileCompleted function being |
| 114 | // run asynchronously. At most one more stack sample will be taken after this |
| 115 | // method returns. Calling this function is optional; if not invoked profiling |
| 116 | // terminates when all the profiling samples specified in the SamplingParams |
| 117 | // are completed or the profiler object is destroyed, whichever occurs first. |
| 118 | void Stop(); |
| 119 |
| 120 | // Adds an auxiliary unwinder to handle additional, non-native-code unwind |
| 121 | // scenarios. |
| 122 | void AddAuxUnwinder(std::unique_ptr<Unwinder> unwinder); |
| 123 |
| 124 | // Test peer class. These functions are purely for internal testing of |
| 125 | // StackSamplingProfiler; DO NOT USE within tests outside of this directory. |
| 126 | // The functions are static because they interact with the sampling thread, a |
| 127 | // singleton used by all StackSamplingProfiler objects. The functions can |
| 128 | // only be called by the same thread that started the sampling. |
| 129 | class BASE_EXPORT TestPeer { |
| 130 | public: |
| 131 | // Resets the internal state to that of a fresh start. This is necessary |
| 132 | // so that tests don't inherit state from previous tests. |
| 133 | static void Reset(); |
| 134 |
| 135 | // Returns whether the sampling thread is currently running or not. |
| 136 | static bool IsSamplingThreadRunning(); |
| 137 |
| 138 | // Disables inherent idle-shutdown behavior. |
| 139 | static void DisableIdleShutdown(); |
| 140 |
| 141 | // Initiates an idle shutdown task, as though the idle timer had expired, |
| 142 | // causing the thread to exit. There is no "idle" check so this must be |
| 143 | // called only when all sampling tasks have completed. This blocks until |
| 144 | // the task has been executed, though the actual stopping of the thread |
| 145 | // still happens asynchronously. Watch IsSamplingThreadRunning() to know |
| 146 | // when the thread has exited. If |simulate_intervening_start| is true then |
| 147 | // this method will make it appear to the shutdown task that a new profiler |
| 148 | // was started between when the idle-shutdown was initiated and when it |
| 149 | // runs. |
| 150 | static void PerformSamplingThreadIdleShutdown( |
| 151 | bool simulate_intervening_start); |
| 152 | }; |
| 153 |
| 154 | private: |
| 155 | // SamplingThread is a separate thread used to suspend and sample stacks from |
| 156 | // the target thread. |
| 157 | class SamplingThread; |
| 158 |
| 159 | // Friend the global function from sample_metadata.cc so that it can call into |
| 160 | // the function below. |
| 161 | friend void ApplyMetadataToPastSamplesImpl(TimeTicks period_start, |
| 162 | TimeTicks period_end, |
| 163 | int64_t name_hash, |
| 164 | Optional<int64_t> key, |
| 165 | int64_t value); |
| 166 |
| 167 | // Apply metadata to already recorded samples. See the |
| 168 | // ApplyMetadataToPastSamples() docs in sample_metadata.h. |
| 169 | static void ApplyMetadataToPastSamples(TimeTicks period_start, |
| 170 | TimeTicks period_end, |
| 171 | int64_t name_hash, |
| 172 | Optional<int64_t> key, |
| 173 | int64_t value); |
| 174 |
| 175 | // The thread whose stack will be sampled. |
| 176 | SamplingProfilerThreadToken thread_token_; |
| 177 |
| 178 | const SamplingParams params_; |
| 179 |
| 180 | // Receives the sampling data and builds a profile. The ownership of this |
| 181 | // object will be transferred to the sampling thread when thread sampling |
| 182 | // starts. |
| 183 | std::unique_ptr<ProfileBuilder> profile_builder_; |
| 184 |
| 185 | // Stack sampler which stops the thread and collects stack frames. The |
| 186 | // ownership of this object will be transferred to the sampling thread when |
| 187 | // thread sampling starts. |
| 188 | std::unique_ptr<StackSampler> sampler_; |
| 189 |
| 190 | // If an AuxUnwinder is added before Start() it will be saved here until it |
| 191 | // can be passed to the sampling thread when thread sampling starts. |
| 192 | std::unique_ptr<Unwinder> pending_aux_unwinder_; |
| 193 |
| 194 | // This starts "signaled", is reset when sampling begins, and is signaled |
| 195 | // when that sampling is complete and the profile_builder_'s |
| 196 | // OnProfileCompleted function has executed. |
| 197 | WaitableEvent profiling_inactive_; |
| 198 |
| 199 | // An ID uniquely identifying this profiler to the sampling thread. This |
| 200 | // will be an internal "null" value when no collection has been started. |
| 201 | int profiler_id_; |
| 202 |
| 203 | DISALLOW_COPY_AND_ASSIGN(StackSamplingProfiler); |
| 204 | }; |
| 205 |
| 206 | } // namespace base |
| 207 |
| 208 | #endif // BASE_PROFILER_STACK_SAMPLING_PROFILER_H_ |
| 209 |