summaryrefslogtreecommitdiffstats
path: root/base/profiler/stack_sampling_profiler.cc
blob: a197d844093d13cd08c447a6748d426499d713bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/profiler/stack_sampling_profiler.h"

#include <algorithm>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/profiler/native_stack_sampler.h"
#include "base/synchronization/lock.h"
#include "base/thread_task_runner_handle.h"
#include "base/timer/elapsed_timer.h"

namespace base {

namespace {

// Used to ensure only one profiler is running at a time.
LazyInstance<Lock> concurrent_profiling_lock = LAZY_INSTANCE_INITIALIZER;

// AsyncRunner ----------------------------------------------------------------

// Helper class to allow a profiler to be run completely asynchronously from the
// initiator, without being concerned with the profiler's lifetime.
class AsyncRunner {
 public:
  // Sets up a profiler and arranges for it to be deleted on its completed
  // callback.
  static void Run(PlatformThreadId thread_id,
                  const StackSamplingProfiler::SamplingParams& params,
                  const StackSamplingProfiler::CompletedCallback& callback);

 private:
  AsyncRunner();

  // Runs the callback and deletes the AsyncRunner instance.
  static void RunCallbackAndDeleteInstance(
      scoped_ptr<AsyncRunner> object_to_be_deleted,
      const StackSamplingProfiler::CompletedCallback& callback,
      scoped_refptr<SingleThreadTaskRunner> task_runner,
      const StackSamplingProfiler::CallStackProfiles& profiles);

  scoped_ptr<StackSamplingProfiler> profiler_;

  DISALLOW_COPY_AND_ASSIGN(AsyncRunner);
};

// static
void AsyncRunner::Run(
    PlatformThreadId thread_id,
    const StackSamplingProfiler::SamplingParams& params,
    const StackSamplingProfiler::CompletedCallback &callback) {
  scoped_ptr<AsyncRunner> runner(new AsyncRunner);
  AsyncRunner* temp_ptr = runner.get();
  temp_ptr->profiler_.reset(
      new StackSamplingProfiler(thread_id, params,
                                Bind(&AsyncRunner::RunCallbackAndDeleteInstance,
                                     Passed(&runner), callback,
                                     ThreadTaskRunnerHandle::Get())));
  // The callback won't be called until after Start(), so temp_ptr will still
  // be valid here.
  temp_ptr->profiler_->Start();
}

AsyncRunner::AsyncRunner() {}

void AsyncRunner::RunCallbackAndDeleteInstance(
    scoped_ptr<AsyncRunner> object_to_be_deleted,
    const StackSamplingProfiler::CompletedCallback& callback,
    scoped_refptr<SingleThreadTaskRunner> task_runner,
    const StackSamplingProfiler::CallStackProfiles& profiles) {
  callback.Run(profiles);
  // Delete the instance on the original calling thread.
  task_runner->DeleteSoon(FROM_HERE, object_to_be_deleted.release());
}

}  // namespace

// StackSamplingProfiler::Module ----------------------------------------------

StackSamplingProfiler::Module::Module() : base_address(0u) {}
StackSamplingProfiler::Module::Module(uintptr_t base_address,
                                      const std::string& id,
                                      const FilePath& filename)
    : base_address(base_address), id(id), filename(filename) {}

StackSamplingProfiler::Module::~Module() {}

// StackSamplingProfiler::Frame -----------------------------------------------

StackSamplingProfiler::Frame::Frame(uintptr_t instruction_pointer,
                                    size_t module_index)
    : instruction_pointer(instruction_pointer), module_index(module_index) {}

StackSamplingProfiler::Frame::~Frame() {}

StackSamplingProfiler::Frame::Frame() {}

// StackSamplingProfiler::CallStackProfile ------------------------------------

StackSamplingProfiler::CallStackProfile::CallStackProfile() {}

StackSamplingProfiler::CallStackProfile::~CallStackProfile() {}

// StackSamplingProfiler::SamplingThread --------------------------------------

StackSamplingProfiler::SamplingThread::SamplingThread(
    scoped_ptr<NativeStackSampler> native_sampler,
    const SamplingParams& params,
    const CompletedCallback& completed_callback)
    : native_sampler_(native_sampler.Pass()),
      params_(params),
      stop_event_(false, false),
      completed_callback_(completed_callback) {
}

StackSamplingProfiler::SamplingThread::~SamplingThread() {}

void StackSamplingProfiler::SamplingThread::ThreadMain() {
  PlatformThread::SetName("Chrome_SamplingProfilerThread");

  // For now, just ignore any requests to profile while another profiler is
  // working.
  if (!concurrent_profiling_lock.Get().Try())
    return;

  CallStackProfiles profiles;
  CollectProfiles(&profiles);
  concurrent_profiling_lock.Get().Release();
  completed_callback_.Run(profiles);
}

// Depending on how long the sampling takes and the length of the sampling
// interval, a burst of samples could take arbitrarily longer than
// samples_per_burst * sampling_interval. In this case, we (somewhat
// arbitrarily) honor the number of samples requested rather than strictly
// adhering to the sampling intervals. Once we have established users for the
// StackSamplingProfiler and the collected data to judge, we may go the other
// way or make this behavior configurable.
void StackSamplingProfiler::SamplingThread::CollectProfile(
    CallStackProfile* profile,
    TimeDelta* elapsed_time,
    bool* was_stopped) {
  ElapsedTimer profile_timer;
  native_sampler_->ProfileRecordingStarting(&profile->modules);
  profile->sampling_period = params_.sampling_interval;
  *was_stopped = false;
  TimeDelta previous_elapsed_sample_time;
  for (int i = 0; i < params_.samples_per_burst; ++i) {
    if (i != 0) {
      // Always wait, even if for 0 seconds, so we can observe a signal on
      // stop_event_.
      if (stop_event_.TimedWait(
              std::max(params_.sampling_interval - previous_elapsed_sample_time,
                       TimeDelta()))) {
        *was_stopped = true;
        break;
      }
    }
    ElapsedTimer sample_timer;
    profile->samples.push_back(Sample());
    native_sampler_->RecordStackSample(&profile->samples.back());
    previous_elapsed_sample_time = sample_timer.Elapsed();
  }

  *elapsed_time = profile_timer.Elapsed();
  profile->profile_duration = *elapsed_time;
  native_sampler_->ProfileRecordingStopped();
}

// In an analogous manner to CollectProfile() and samples exceeding the expected
// total sampling time, bursts may also exceed the burst_interval. We adopt the
// same wait-and-see approach here.
void StackSamplingProfiler::SamplingThread::CollectProfiles(
    CallStackProfiles* profiles) {
  if (stop_event_.TimedWait(params_.initial_delay))
    return;

  TimeDelta previous_elapsed_profile_time;
  for (int i = 0; i < params_.bursts; ++i) {
    if (i != 0) {
      // Always wait, even if for 0 seconds, so we can observe a signal on
      // stop_event_.
      if (stop_event_.TimedWait(
              std::max(params_.burst_interval - previous_elapsed_profile_time,
                       TimeDelta())))
        return;
    }

    CallStackProfile profile;
    bool was_stopped = false;
    CollectProfile(&profile, &previous_elapsed_profile_time, &was_stopped);
    if (!profile.samples.empty())
      profiles->push_back(profile);

    if (was_stopped)
      return;
  }
}

void StackSamplingProfiler::SamplingThread::Stop() {
  stop_event_.Signal();
}

// StackSamplingProfiler ------------------------------------------------------

StackSamplingProfiler::SamplingParams::SamplingParams()
    : initial_delay(TimeDelta::FromMilliseconds(0)),
      bursts(1),
      burst_interval(TimeDelta::FromMilliseconds(10000)),
      samples_per_burst(300),
      sampling_interval(TimeDelta::FromMilliseconds(100)) {
}

StackSamplingProfiler::StackSamplingProfiler(PlatformThreadId thread_id,
                                             const SamplingParams& params,
                                             const CompletedCallback& callback)
    : thread_id_(thread_id), params_(params), completed_callback_(callback) {}

StackSamplingProfiler::~StackSamplingProfiler() {
  Stop();
  if (!sampling_thread_handle_.is_null())
    PlatformThread::Join(sampling_thread_handle_);
}

// static
void StackSamplingProfiler::StartAndRunAsync(
    PlatformThreadId thread_id,
    const SamplingParams& params,
    const CompletedCallback& callback) {
  CHECK(ThreadTaskRunnerHandle::Get());
  AsyncRunner::Run(thread_id, params, callback);
}

void StackSamplingProfiler::Start() {
  if (completed_callback_.is_null())
    return;

  scoped_ptr<NativeStackSampler> native_sampler =
      NativeStackSampler::Create(thread_id_);
  if (!native_sampler)
    return;

  sampling_thread_.reset(
      new SamplingThread(native_sampler.Pass(), params_, completed_callback_));
  if (!PlatformThread::Create(0, sampling_thread_.get(),
                              &sampling_thread_handle_))
    sampling_thread_.reset();
}

void StackSamplingProfiler::Stop() {
  if (sampling_thread_)
    sampling_thread_->Stop();
}

// StackSamplingProfiler::Frame global functions ------------------------------

bool operator==(const StackSamplingProfiler::Frame &a,
                const StackSamplingProfiler::Frame &b) {
  return a.instruction_pointer == b.instruction_pointer &&
      a.module_index == b.module_index;
}

bool operator<(const StackSamplingProfiler::Frame &a,
               const StackSamplingProfiler::Frame &b) {
  return (a.module_index < b.module_index) ||
      (a.module_index == b.module_index &&
       a.instruction_pointer < b.instruction_pointer);
}

}  // namespace base