1 // Copyright 2015 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.
5 #include "base/profiler/stack_sampling_profiler.h"
10 #include "base/callback.h"
11 #include "base/memory/singleton.h"
12 #include "base/synchronization/lock.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/timer/elapsed_timer.h"
16 template <typename T
> struct DefaultSingletonTraits
;
22 // Thread-safe singleton class that stores collected profiles waiting to be
24 class PendingProfiles
{
29 static PendingProfiles
* GetInstance();
31 // Appends |profiles|. This function is thread safe.
32 void PutProfiles(const std::vector
<StackSamplingProfiler::Profile
>& profiles
);
33 // Gets the pending profiles into *|profiles|. This function is thread safe.
34 void GetProfiles(std::vector
<StackSamplingProfiler::Profile
>* profiles
);
38 std::vector
<StackSamplingProfiler::Profile
> profiles_
;
40 DISALLOW_COPY_AND_ASSIGN(PendingProfiles
);
43 PendingProfiles::PendingProfiles() {}
45 PendingProfiles::~PendingProfiles() {}
48 PendingProfiles
* PendingProfiles::GetInstance() {
49 return Singleton
<PendingProfiles
>::get();
52 void PendingProfiles::PutProfiles(
53 const std::vector
<StackSamplingProfiler::Profile
>& profiles
) {
54 AutoLock
scoped_lock(profiles_lock_
);
55 profiles_
.insert(profiles_
.end(), profiles
.begin(), profiles
.end());
58 void PendingProfiles::GetProfiles(
59 std::vector
<StackSamplingProfiler::Profile
>* profiles
) {
62 AutoLock
scoped_lock(profiles_lock_
);
63 profiles_
.swap(*profiles
);
67 StackSamplingProfiler::Module::Module() : base_address(nullptr) {}
68 StackSamplingProfiler::Module::Module(const void* base_address
,
69 const std::string
& id
,
70 const FilePath
& filename
)
71 : base_address(base_address
), id(id
), filename(filename
) {}
73 StackSamplingProfiler::Module::~Module() {}
75 StackSamplingProfiler::Frame::Frame()
76 : instruction_pointer(nullptr),
79 StackSamplingProfiler::Frame::Frame(const void* instruction_pointer
,
81 : instruction_pointer(instruction_pointer
),
82 module_index(module_index
) {}
84 StackSamplingProfiler::Frame::~Frame() {}
86 StackSamplingProfiler::Profile::Profile() : preserve_sample_ordering(false) {}
88 StackSamplingProfiler::Profile::~Profile() {}
90 class StackSamplingProfiler::SamplingThread
: public PlatformThread::Delegate
{
92 // Samples stacks using |native_sampler|. When complete, invokes
93 // |profiles_callback| with the collected profiles. |profiles_callback| must
94 // be thread-safe and may consume the contents of the vector.
96 scoped_ptr
<NativeStackSampler
> native_sampler
,
97 const SamplingParams
& params
,
98 Callback
<void(const std::vector
<Profile
>&)> completed_callback
);
99 ~SamplingThread() override
;
101 // Implementation of PlatformThread::Delegate:
102 void ThreadMain() override
;
107 // Collects a profile from a single burst. Returns true if the profile was
108 // collected, or false if collection was stopped before it completed.
109 bool CollectProfile(Profile
* profile
, TimeDelta
* elapsed_time
);
110 // Collects profiles from all bursts, or until the sampling is stopped. If
111 // stopped before complete, |profiles| will contains only full bursts.
112 void CollectProfiles(std::vector
<Profile
>* profiles
);
114 scoped_ptr
<NativeStackSampler
> native_sampler_
;
116 const SamplingParams params_
;
118 WaitableEvent stop_event_
;
120 Callback
<void(const std::vector
<Profile
>&)> completed_callback_
;
122 DISALLOW_COPY_AND_ASSIGN(SamplingThread
);
125 StackSamplingProfiler::SamplingThread::SamplingThread(
126 scoped_ptr
<NativeStackSampler
> native_sampler
,
127 const SamplingParams
& params
,
128 Callback
<void(const std::vector
<Profile
>&)> completed_callback
)
129 : native_sampler_(native_sampler
.Pass()),
131 stop_event_(false, false),
132 completed_callback_(completed_callback
) {
135 StackSamplingProfiler::SamplingThread::~SamplingThread() {}
137 void StackSamplingProfiler::SamplingThread::ThreadMain() {
138 PlatformThread::SetName("Chrome_SamplingProfilerThread");
140 std::vector
<Profile
> profiles
;
141 CollectProfiles(&profiles
);
142 completed_callback_
.Run(profiles
);
145 bool StackSamplingProfiler::SamplingThread::CollectProfile(
147 TimeDelta
* elapsed_time
) {
148 ElapsedTimer profile_timer
;
149 Profile current_profile
;
150 native_sampler_
->ProfileRecordingStarting(¤t_profile
);
151 current_profile
.sampling_period
= params_
.sampling_interval
;
152 bool stopped_early
= false;
153 for (int i
= 0; i
< params_
.samples_per_burst
; ++i
) {
154 ElapsedTimer sample_timer
;
155 current_profile
.samples
.push_back(Sample());
156 native_sampler_
->RecordStackSample(¤t_profile
.samples
.back());
157 TimeDelta elapsed_sample_time
= sample_timer
.Elapsed();
158 if (i
!= params_
.samples_per_burst
- 1) {
159 if (stop_event_
.TimedWait(
160 std::max(params_
.sampling_interval
- elapsed_sample_time
,
162 stopped_early
= true;
168 *elapsed_time
= profile_timer
.Elapsed();
169 current_profile
.profile_duration
= *elapsed_time
;
170 native_sampler_
->ProfileRecordingStopped();
173 *profile
= current_profile
;
175 return !stopped_early
;
178 void StackSamplingProfiler::SamplingThread::CollectProfiles(
179 std::vector
<Profile
>* profiles
) {
180 if (stop_event_
.TimedWait(params_
.initial_delay
))
183 for (int i
= 0; i
< params_
.bursts
; ++i
) {
185 TimeDelta elapsed_profile_time
;
186 if (CollectProfile(&profile
, &elapsed_profile_time
))
187 profiles
->push_back(profile
);
191 if (stop_event_
.TimedWait(
192 std::max(params_
.burst_interval
- elapsed_profile_time
,
198 void StackSamplingProfiler::SamplingThread::Stop() {
199 stop_event_
.Signal();
202 void StackSamplingProfiler::SamplingThreadDeleter::operator()(
203 SamplingThread
* thread
) const {
207 StackSamplingProfiler::NativeStackSampler::NativeStackSampler() {}
209 StackSamplingProfiler::NativeStackSampler::~NativeStackSampler() {}
211 StackSamplingProfiler::SamplingParams::SamplingParams()
212 : initial_delay(TimeDelta::FromMilliseconds(0)),
214 burst_interval(TimeDelta::FromMilliseconds(10000)),
215 samples_per_burst(300),
216 sampling_interval(TimeDelta::FromMilliseconds(100)),
217 preserve_sample_ordering(false) {
220 StackSamplingProfiler::StackSamplingProfiler(PlatformThreadId thread_id
,
221 const SamplingParams
& params
)
222 : thread_id_(thread_id
), params_(params
) {}
224 StackSamplingProfiler::~StackSamplingProfiler() {}
226 void StackSamplingProfiler::Start() {
227 native_sampler_
= NativeStackSampler::Create(thread_id_
);
228 if (!native_sampler_
)
231 sampling_thread_
.reset(
233 native_sampler_
.Pass(), params_
,
234 (custom_completed_callback_
.is_null() ?
235 Bind(&PendingProfiles::PutProfiles
,
236 Unretained(PendingProfiles::GetInstance())) :
237 custom_completed_callback_
)));
238 if (!PlatformThread::CreateNonJoinable(0, sampling_thread_
.get()))
239 LOG(ERROR
) << "failed to create thread";
242 void StackSamplingProfiler::Stop() {
243 if (sampling_thread_
)
244 sampling_thread_
->Stop();
248 void StackSamplingProfiler::GetPendingProfiles(std::vector
<Profile
>* profiles
) {
249 PendingProfiles::GetInstance()->GetProfiles(profiles
);
252 void StackSamplingProfiler::SetCustomCompletedCallback(
253 Callback
<void(const std::vector
<Profile
>&)> callback
) {
254 custom_completed_callback_
= callback
;
257 bool operator==(const StackSamplingProfiler::Frame
&a
,
258 const StackSamplingProfiler::Frame
&b
) {
259 return a
.instruction_pointer
== b
.instruction_pointer
&&
260 a
.module_index
== b
.module_index
;
263 bool operator<(const StackSamplingProfiler::Frame
&a
,
264 const StackSamplingProfiler::Frame
&b
) {
265 return (a
.module_index
< b
.module_index
) ||
266 (a
.module_index
== b
.module_index
&&
267 a
.instruction_pointer
< b
.instruction_pointer
);