1*6777b538SAndroid Build Coastguard Worker // Copyright 2017 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker
5*6777b538SAndroid Build Coastguard Worker #include "base/profiler/stack_sampler.h"
6*6777b538SAndroid Build Coastguard Worker
7*6777b538SAndroid Build Coastguard Worker #include <memory>
8*6777b538SAndroid Build Coastguard Worker
9*6777b538SAndroid Build Coastguard Worker #include "base/check.h"
10*6777b538SAndroid Build Coastguard Worker #include "base/functional/bind.h"
11*6777b538SAndroid Build Coastguard Worker #include "base/memory/ptr_util.h"
12*6777b538SAndroid Build Coastguard Worker #include "base/profiler/frame_pointer_unwinder.h"
13*6777b538SAndroid Build Coastguard Worker #include "base/profiler/stack_copier_suspend.h"
14*6777b538SAndroid Build Coastguard Worker #include "base/profiler/suspendable_thread_delegate_mac.h"
15*6777b538SAndroid Build Coastguard Worker #include "base/profiler/unwinder.h"
16*6777b538SAndroid Build Coastguard Worker
17*6777b538SAndroid Build Coastguard Worker namespace base {
18*6777b538SAndroid Build Coastguard Worker
19*6777b538SAndroid Build Coastguard Worker namespace {
20*6777b538SAndroid Build Coastguard Worker
CreateUnwinders()21*6777b538SAndroid Build Coastguard Worker std::vector<std::unique_ptr<Unwinder>> CreateUnwinders() {
22*6777b538SAndroid Build Coastguard Worker std::vector<std::unique_ptr<Unwinder>> unwinders;
23*6777b538SAndroid Build Coastguard Worker unwinders.push_back(std::make_unique<FramePointerUnwinder>());
24*6777b538SAndroid Build Coastguard Worker return unwinders;
25*6777b538SAndroid Build Coastguard Worker }
26*6777b538SAndroid Build Coastguard Worker
27*6777b538SAndroid Build Coastguard Worker } // namespace
28*6777b538SAndroid Build Coastguard Worker
29*6777b538SAndroid Build Coastguard Worker // static
Create(SamplingProfilerThreadToken thread_token,ModuleCache * module_cache,UnwindersFactory core_unwinders_factory,RepeatingClosure record_sample_callback,StackSamplerTestDelegate * test_delegate)30*6777b538SAndroid Build Coastguard Worker std::unique_ptr<StackSampler> StackSampler::Create(
31*6777b538SAndroid Build Coastguard Worker SamplingProfilerThreadToken thread_token,
32*6777b538SAndroid Build Coastguard Worker ModuleCache* module_cache,
33*6777b538SAndroid Build Coastguard Worker UnwindersFactory core_unwinders_factory,
34*6777b538SAndroid Build Coastguard Worker RepeatingClosure record_sample_callback,
35*6777b538SAndroid Build Coastguard Worker StackSamplerTestDelegate* test_delegate) {
36*6777b538SAndroid Build Coastguard Worker DCHECK(!core_unwinders_factory);
37*6777b538SAndroid Build Coastguard Worker return base::WrapUnique(new StackSampler(
38*6777b538SAndroid Build Coastguard Worker std::make_unique<StackCopierSuspend>(
39*6777b538SAndroid Build Coastguard Worker std::make_unique<SuspendableThreadDelegateMac>(thread_token)),
40*6777b538SAndroid Build Coastguard Worker BindOnce(&CreateUnwinders), module_cache,
41*6777b538SAndroid Build Coastguard Worker std::move(record_sample_callback), test_delegate));
42*6777b538SAndroid Build Coastguard Worker }
43*6777b538SAndroid Build Coastguard Worker
44*6777b538SAndroid Build Coastguard Worker // static
GetStackBufferSize()45*6777b538SAndroid Build Coastguard Worker size_t StackSampler::GetStackBufferSize() {
46*6777b538SAndroid Build Coastguard Worker size_t stack_size = PlatformThread::GetDefaultThreadStackSize();
47*6777b538SAndroid Build Coastguard Worker
48*6777b538SAndroid Build Coastguard Worker // If getrlimit somehow fails, return the default macOS main thread stack size
49*6777b538SAndroid Build Coastguard Worker // of 8 MB (DFLSSIZ in <i386/vmparam.h>) with extra wiggle room.
50*6777b538SAndroid Build Coastguard Worker return stack_size > 0 ? stack_size : 12 * 1024 * 1024;
51*6777b538SAndroid Build Coastguard Worker }
52*6777b538SAndroid Build Coastguard Worker
53*6777b538SAndroid Build Coastguard Worker } // namespace base
54