stack_sampler_android.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. #include "base/profiler/stack_sampler.h"
  5. #include <pthread.h>
  6. #include "base/check.h"
  7. #include "base/profiler/stack_copier_signal.h"
  8. #include "base/profiler/stack_sampler_impl.h"
  9. #include "base/profiler/thread_delegate_posix.h"
  10. #include "base/profiler/unwinder.h"
  11. #include "base/threading/platform_thread.h"
  12. namespace base {
  13. std::unique_ptr<StackSampler> StackSampler::Create(
  14. SamplingProfilerThreadToken thread_token,
  15. ModuleCache* module_cache,
  16. UnwindersFactory core_unwinders_factory,
  17. RepeatingClosure record_sample_callback,
  18. StackSamplerTestDelegate* test_delegate) {
  19. auto thread_delegate = ThreadDelegatePosix::Create(thread_token);
  20. if (!thread_delegate)
  21. return nullptr;
  22. return std::make_unique<StackSamplerImpl>(
  23. std::make_unique<StackCopierSignal>(std::move(thread_delegate)),
  24. std::move(core_unwinders_factory), module_cache,
  25. std::move(record_sample_callback), test_delegate);
  26. }
  27. size_t StackSampler::GetStackBufferSize() {
  28. size_t stack_size = PlatformThread::GetDefaultThreadStackSize();
  29. pthread_attr_t attr;
  30. if (stack_size == 0 && pthread_attr_init(&attr) == 0) {
  31. if (pthread_attr_getstacksize(&attr, &stack_size) != 0)
  32. stack_size = 0;
  33. pthread_attr_destroy(&attr);
  34. }
  35. // 1MB is default thread limit set by Android at art/runtime/thread_pool.h.
  36. constexpr size_t kDefaultStackLimit = 1 << 20;
  37. return stack_size > 0 ? stack_size : kDefaultStackLimit;
  38. }
  39. } // namespace base