xref: /aosp_15_r20/external/cronet/base/profiler/stack_sampler_win.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2015 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 "base/check.h"
8*6777b538SAndroid Build Coastguard Worker #include "base/functional/bind.h"
9*6777b538SAndroid Build Coastguard Worker #include "base/memory/ptr_util.h"
10*6777b538SAndroid Build Coastguard Worker #include "base/profiler/native_unwinder_win.h"
11*6777b538SAndroid Build Coastguard Worker #include "base/profiler/stack_copier_suspend.h"
12*6777b538SAndroid Build Coastguard Worker #include "base/profiler/suspendable_thread_delegate_win.h"
13*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h"
14*6777b538SAndroid Build Coastguard Worker 
15*6777b538SAndroid Build Coastguard Worker namespace base {
16*6777b538SAndroid Build Coastguard Worker 
17*6777b538SAndroid Build Coastguard Worker // static
Create(SamplingProfilerThreadToken thread_token,ModuleCache * module_cache,UnwindersFactory core_unwinders_factory,RepeatingClosure record_sample_callback,StackSamplerTestDelegate * test_delegate)18*6777b538SAndroid Build Coastguard Worker std::unique_ptr<StackSampler> StackSampler::Create(
19*6777b538SAndroid Build Coastguard Worker     SamplingProfilerThreadToken thread_token,
20*6777b538SAndroid Build Coastguard Worker     ModuleCache* module_cache,
21*6777b538SAndroid Build Coastguard Worker     UnwindersFactory core_unwinders_factory,
22*6777b538SAndroid Build Coastguard Worker     RepeatingClosure record_sample_callback,
23*6777b538SAndroid Build Coastguard Worker     StackSamplerTestDelegate* test_delegate) {
24*6777b538SAndroid Build Coastguard Worker   DCHECK(!core_unwinders_factory);
25*6777b538SAndroid Build Coastguard Worker #if defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM64)
26*6777b538SAndroid Build Coastguard Worker   const auto create_unwinders = []() {
27*6777b538SAndroid Build Coastguard Worker     std::vector<std::unique_ptr<Unwinder>> unwinders;
28*6777b538SAndroid Build Coastguard Worker     unwinders.push_back(std::make_unique<NativeUnwinderWin>());
29*6777b538SAndroid Build Coastguard Worker     return unwinders;
30*6777b538SAndroid Build Coastguard Worker   };
31*6777b538SAndroid Build Coastguard Worker   return base::WrapUnique(new StackSampler(
32*6777b538SAndroid Build Coastguard Worker       std::make_unique<StackCopierSuspend>(
33*6777b538SAndroid Build Coastguard Worker           std::make_unique<SuspendableThreadDelegateWin>(thread_token)),
34*6777b538SAndroid Build Coastguard Worker       BindOnce(create_unwinders), module_cache,
35*6777b538SAndroid Build Coastguard Worker       std::move(record_sample_callback), test_delegate));
36*6777b538SAndroid Build Coastguard Worker #else
37*6777b538SAndroid Build Coastguard Worker   return nullptr;
38*6777b538SAndroid Build Coastguard Worker #endif
39*6777b538SAndroid Build Coastguard Worker }
40*6777b538SAndroid Build Coastguard Worker 
41*6777b538SAndroid Build Coastguard Worker // static
GetStackBufferSize()42*6777b538SAndroid Build Coastguard Worker size_t StackSampler::GetStackBufferSize() {
43*6777b538SAndroid Build Coastguard Worker   // The default Win32 reserved stack size is 1 MB and Chrome Windows threads
44*6777b538SAndroid Build Coastguard Worker   // currently always use the default, but this allows for expansion if it
45*6777b538SAndroid Build Coastguard Worker   // occurs. The size beyond the actual stack size consists of unallocated
46*6777b538SAndroid Build Coastguard Worker   // virtual memory pages so carries little cost (just a bit of wasted address
47*6777b538SAndroid Build Coastguard Worker   // space).
48*6777b538SAndroid Build Coastguard Worker   return 2 << 20;  // 2 MiB
49*6777b538SAndroid Build Coastguard Worker }
50*6777b538SAndroid Build Coastguard Worker 
51*6777b538SAndroid Build Coastguard Worker }  // namespace base
52