1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/profiler/stack_sampler.h"
6
7 #include <pthread.h>
8
9 #include "base/check.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/profiler/stack_copier_signal.h"
12 #include "base/profiler/thread_delegate_posix.h"
13 #include "base/profiler/unwinder.h"
14 #include "base/threading/platform_thread.h"
15
16 namespace base {
17
Create(SamplingProfilerThreadToken thread_token,ModuleCache * module_cache,UnwindersFactory core_unwinders_factory,RepeatingClosure record_sample_callback,StackSamplerTestDelegate * test_delegate)18 std::unique_ptr<StackSampler> StackSampler::Create(
19 SamplingProfilerThreadToken thread_token,
20 ModuleCache* module_cache,
21 UnwindersFactory core_unwinders_factory,
22 RepeatingClosure record_sample_callback,
23 StackSamplerTestDelegate* test_delegate) {
24 auto thread_delegate = ThreadDelegatePosix::Create(thread_token);
25 if (!thread_delegate)
26 return nullptr;
27 return base::WrapUnique(new StackSampler(
28 std::make_unique<StackCopierSignal>(std::move(thread_delegate)),
29 std::move(core_unwinders_factory), module_cache,
30 std::move(record_sample_callback), test_delegate));
31 }
32
GetStackBufferSize()33 size_t StackSampler::GetStackBufferSize() {
34 size_t stack_size = PlatformThread::GetDefaultThreadStackSize();
35
36 pthread_attr_t attr;
37 if (stack_size == 0 && pthread_attr_init(&attr) == 0) {
38 if (pthread_attr_getstacksize(&attr, &stack_size) != 0)
39 stack_size = 0;
40 pthread_attr_destroy(&attr);
41 }
42
43 // 1MB is default thread limit set by Android at art/runtime/thread_pool.h.
44 constexpr size_t kDefaultStackLimit = 1 << 20;
45 return stack_size > 0 ? stack_size : kDefaultStackLimit;
46 }
47
48 } // namespace base
49