Charlie Andrews | 63dbdf6 | 2019-07-30 20:55:49 | [diff] [blame] | 1 | // Copyright 2019 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. |
| 4 | |
| 5 | #include "base/profiler/stack_sampler.h" |
| 6 | |
| 7 | #include <pthread.h> |
| 8 | |
| 9 | #include "base/profiler/native_unwinder_android.h" |
Mike Wittman | fe998b3 | 2019-09-06 21:35:22 | [diff] [blame] | 10 | #include "base/profiler/stack_copier_signal.h" |
Charlie Andrews | 63dbdf6 | 2019-07-30 20:55:49 | [diff] [blame] | 11 | #include "base/profiler/stack_sampler_impl.h" |
Mike Wittman | 1834978 | 2019-10-22 17:11:22 | [diff] [blame^] | 12 | #include "base/profiler/thread_delegate_posix.h" |
Charlie Andrews | 63dbdf6 | 2019-07-30 20:55:49 | [diff] [blame] | 13 | #include "base/threading/platform_thread.h" |
| 14 | |
| 15 | namespace base { |
| 16 | |
| 17 | std::unique_ptr<StackSampler> StackSampler::Create( |
| 18 | PlatformThreadId thread_id, |
| 19 | ModuleCache* module_cache, |
| 20 | StackSamplerTestDelegate* test_delegate) { |
| 21 | return std::make_unique<StackSamplerImpl>( |
Mike Wittman | a31d7d44 | 2019-09-09 20:54:48 | [diff] [blame] | 22 | std::make_unique<StackCopierSignal>( |
Mike Wittman | 1834978 | 2019-10-22 17:11:22 | [diff] [blame^] | 23 | std::make_unique<ThreadDelegatePosix>(thread_id)), |
Charlie Andrews | 63dbdf6 | 2019-07-30 20:55:49 | [diff] [blame] | 24 | std::make_unique<NativeUnwinderAndroid>(), module_cache, test_delegate); |
| 25 | } |
| 26 | |
| 27 | size_t StackSampler::GetStackBufferSize() { |
| 28 | size_t stack_size = PlatformThread::GetDefaultThreadStackSize(); |
| 29 | |
| 30 | pthread_attr_t attr; |
| 31 | if (stack_size == 0 && pthread_attr_init(&attr) == 0) { |
| 32 | if (pthread_attr_getstacksize(&attr, &stack_size) != 0) |
| 33 | stack_size = 0; |
| 34 | pthread_attr_destroy(&attr); |
| 35 | } |
| 36 | |
| 37 | // 1MB is default thread limit set by Android at art/runtime/thread_pool.h. |
| 38 | constexpr size_t kDefaultStackLimit = 1 << 20; |
| 39 | return stack_size > 0 ? stack_size : kDefaultStackLimit; |
| 40 | } |
| 41 | |
| 42 | } // namespace base |