File: thread_profiler.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (295 lines) | stat: -rw-r--r-- 11,541 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sampling_profiler/thread_profiler.h"

#include <string>
#include <utility>
#include <vector>

#include "base/android/library_loader/anchor_functions.h"
#include "base/check.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/message_loop/work_id_provider.h"
#include "base/process/process.h"
#include "base/profiler/profiler_buildflags.h"
#include "base/profiler/sample_metadata.h"
#include "base/profiler/sampling_profiler_thread_token.h"
#include "base/rand_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/platform_thread.h"
#include "base/threading/sequence_local_storage_slot.h"
#include "build/blink_buildflags.h"
#include "build/build_config.h"
#include "components/metrics/call_stacks/call_stack_profile_builder.h"
#include "components/sampling_profiler/process_type.h"
#include "components/sampling_profiler/thread_profiler_client.h"

#if BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK))
#include "base/process/port_provider_mac.h"
#endif  // BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK))

namespace sampling_profiler {
namespace {

// Pointer to the main thread instance, if any. Stored as a global because it's
// created very early in chrome/app - and is thus otherwise inaccessible from
// chrome_dll, by the time we need to register the main thread task runner.
ThreadProfiler* g_main_thread_instance = nullptr;

// Pointer to the embedder-specific client implementation.
// |g_thread_profiler_client| is intentionally leaked on shutdown.
ThreadProfilerClient* g_thread_profiler_client = nullptr;

// The kFractionOfExecutionTimeToSample and SamplingParams settings in
// ThreadProfilerConfiguration::GetSamplingParams() specify fraction = 0.02 and
// sampling period = 1 sample / .1s sampling interval * 300 samples = 30s. The
// period length works out to 30s/0.02 = 1500s = 25m. So every 25 minutes a
// random 30 second continuous interval will be picked to sample.

// Run continuous profiling 2% of the time.
constexpr double kFractionOfExecutionTimeToSample = 0.02;

bool IsCurrentProcessBackgrounded() {
#if BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK))
  base::SelfPortProvider provider;
  return base::Process::Current().GetPriority(&provider) ==
         base::Process::Priority::kBestEffort;
#else   // BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK))
  return base::Process::Current().GetPriority() ==
         base::Process::Priority::kBestEffort;
#endif  // BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK))
}

const base::RepeatingClosure GetApplyPerSampleMetadataCallback(
    ProfilerProcessType process) {
  if (process != ProfilerProcessType::kRenderer) {
    return base::RepeatingClosure();
  }
  static const base::SampleMetadata process_backgrounded(
      "ProcessBackgrounded", base::SampleMetadataScope::kProcess);
  return base::BindRepeating(
      [](base::SampleMetadata process_backgrounded) {
        process_backgrounded.Set(IsCurrentProcessBackgrounded());
      },
      process_backgrounded);
}

}  // namespace

// Records the current unique id for the work item being executed in the target
// thread's message loop.
class ThreadProfiler::WorkIdRecorder : public metrics::WorkIdRecorder {
 public:
  explicit WorkIdRecorder(base::WorkIdProvider* work_id_provider)
      : work_id_provider_(work_id_provider) {}

  // Invoked on the profiler thread while the target thread is suspended.
  unsigned int RecordWorkId() const override {
    return work_id_provider_->GetWorkId();
  }

  WorkIdRecorder(const WorkIdRecorder&) = delete;
  WorkIdRecorder& operator=(const WorkIdRecorder&) = delete;

 private:
  const raw_ptr<base::WorkIdProvider> work_id_provider_;
};

ThreadProfiler::~ThreadProfiler() {
  if (g_main_thread_instance == this) {
    g_main_thread_instance = nullptr;
  }
}

// static
std::unique_ptr<ThreadProfiler> ThreadProfiler::CreateAndStartOnMainThread() {
  // If running in single process mode, there may be multiple "main thread"
  // profilers created. In this case, we assume the first created one is the
  // browser one.
  bool is_single_process =
      GetClient()->IsSingleProcess(*base::CommandLine::ForCurrentProcess());
  DCHECK(!g_main_thread_instance || is_single_process);
  auto instance =
      base::WrapUnique(new ThreadProfiler(ProfilerThreadType::kMain));
  if (!g_main_thread_instance) {
    g_main_thread_instance = instance.get();
  }
  return instance;
}

// static
void ThreadProfiler::SetMainThreadTaskRunner(
    scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
  DCHECK(g_main_thread_instance);
  g_main_thread_instance->SetMainThreadTaskRunnerImpl(task_runner);
}

void ThreadProfiler::SetAuxUnwinderFactory(
    const base::RepeatingCallback<std::unique_ptr<base::Unwinder>()>& factory) {
  if (!GetClient()->IsProfilerEnabledForCurrentProcessAndThread(thread_)) {
    return;
  }

  aux_unwinder_factory_ = factory;
  startup_profiler_->AddAuxUnwinder(aux_unwinder_factory_.Run());
  if (periodic_profiler_) {
    periodic_profiler_->AddAuxUnwinder(aux_unwinder_factory_.Run());
  }
}

// static
void ThreadProfiler::StartOnChildThread(ProfilerThreadType thread) {
  // The profiler object is stored in a SequenceLocalStorageSlot on child
  // threads to give it the same lifetime as the threads.
  static base::SequenceLocalStorageSlot<std::unique_ptr<ThreadProfiler>>
      child_thread_profiler_sequence_local_storage;

  if (!GetClient()->IsProfilerEnabledForCurrentProcessAndThread(thread)) {
    return;
  }

  child_thread_profiler_sequence_local_storage.emplace(new ThreadProfiler(
      thread, base::SingleThreadTaskRunner::GetCurrentDefault()));
}

// static
void ThreadProfiler::SetClient(std::unique_ptr<ThreadProfilerClient> client) {
  // Generally, the client should only be set once, at process startup. However,
  // some test infrastructure causes initialization to happen more than once.
  delete g_thread_profiler_client;
  g_thread_profiler_client = client.release();
}

// static
ThreadProfilerClient* ThreadProfiler::GetClient() {
  CHECK(g_thread_profiler_client);
  return g_thread_profiler_client;
}

// ThreadProfiler implementation synopsis:
//
// On creation, the profiler creates and starts the startup
// StackSamplingProfiler, and configures the PeriodicSamplingScheduler such that
// it starts scheduling from the time the startup profiling will be complete.
// When a message loop is available (either in the constructor, or via
// SetMainThreadTaskRunner) a task is posted to start the first periodic
// collection at the initial scheduled collection time.
//
// When the periodic collection task executes, it creates and starts a new
// periodic profiler and configures it to call OnPeriodicCollectionCompleted as
// its completion callback. OnPeriodicCollectionCompleted is called on the
// profiler thread and schedules a task on the original thread to schedule
// another periodic collection. When the task runs, it posts a new task to start
// another periodic collection at the next scheduled collection time.
//
// The process in previous paragraph continues until the ThreadProfiler is
// destroyed prior to thread exit.
ThreadProfiler::ThreadProfiler(
    ProfilerThreadType thread,
    scoped_refptr<base::SingleThreadTaskRunner> owning_thread_task_runner)
    : process_(
          GetClient()->GetProcessType(*base::CommandLine::ForCurrentProcess())),
      thread_(thread),
      owning_thread_task_runner_(owning_thread_task_runner),
      work_id_recorder_(std::make_unique<WorkIdRecorder>(
          base::WorkIdProvider::GetForCurrentThread())) {
  if (!GetClient()->IsProfilerEnabledForCurrentProcessAndThread(thread_)) {
    return;
  }

  const base::StackSamplingProfiler::SamplingParams sampling_params =
      GetClient()->GetSamplingParams();

  startup_profiler_ = CreateSamplingProfiler(
      sampling_params, CallStackProfileParams::Trigger::kProcessStartup,
      /*builder_completed_callback=*/base::OnceClosure());

  startup_profiler_->Start();

  // Estimated time at which the startup profiling will be completed. It's OK if
  // this doesn't exactly coincide with the end of the startup profiling, since
  // there's no harm in having a brief overlap of startup and periodic
  // profiling.
  base::TimeTicks startup_profiling_completion_time =
      base::TimeTicks::Now() +
      sampling_params.samples_per_profile * sampling_params.sampling_interval;

  periodic_sampling_scheduler_ =
      std::make_unique<base::PeriodicSamplingScheduler>(
          sampling_params.samples_per_profile *
              sampling_params.sampling_interval,
          kFractionOfExecutionTimeToSample, startup_profiling_completion_time);

  if (owning_thread_task_runner_) {
    ScheduleNextPeriodicCollection();
  }
}

std::unique_ptr<base::StackSamplingProfiler>
ThreadProfiler::CreateSamplingProfiler(
    base::StackSamplingProfiler::SamplingParams sampling_params,
    CallStackProfileParams::Trigger trigger,
    base::OnceClosure builder_completed_callback) {
  return std::make_unique<base::StackSamplingProfiler>(
      base::GetSamplingProfilerCurrentThreadToken(), sampling_params,
      GetClient()->CreateProfileBuilder(
          CallStackProfileParams(process_, thread_, trigger),
          work_id_recorder_.get(), std::move(builder_completed_callback)),
      GetClient()->GetUnwindersFactory(),
      GetApplyPerSampleMetadataCallback(process_));
}

// static
void ThreadProfiler::OnPeriodicCollectionCompleted(
    scoped_refptr<base::SingleThreadTaskRunner> owning_thread_task_runner,
    base::WeakPtr<ThreadProfiler> thread_profiler) {
  owning_thread_task_runner->PostTask(
      FROM_HERE, base::BindOnce(&ThreadProfiler::ScheduleNextPeriodicCollection,
                                thread_profiler));
}

void ThreadProfiler::SetMainThreadTaskRunnerImpl(
    scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
  if (!GetClient()->IsProfilerEnabledForCurrentProcessAndThread(thread_)) {
    return;
  }

  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

  // This should only be called if the task runner wasn't provided in the
  // constructor.
  DCHECK(!owning_thread_task_runner_);
  owning_thread_task_runner_ = task_runner;
  ScheduleNextPeriodicCollection();
}

void ThreadProfiler::ScheduleNextPeriodicCollection() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  owning_thread_task_runner_->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(&ThreadProfiler::StartPeriodicSamplingCollection,
                     weak_factory_.GetWeakPtr()),
      periodic_sampling_scheduler_->GetTimeToNextCollection());
}

void ThreadProfiler::StartPeriodicSamplingCollection() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  // NB: Destroys the previous profiler as side effect.
  periodic_profiler_ = CreateSamplingProfiler(
      GetClient()->GetSamplingParams(),
      CallStackProfileParams::Trigger::kPeriodicCollection,
      base::BindOnce(&ThreadProfiler::OnPeriodicCollectionCompleted,
                     owning_thread_task_runner_, weak_factory_.GetWeakPtr()));
  if (aux_unwinder_factory_) {
    periodic_profiler_->AddAuxUnwinder(aux_unwinder_factory_.Run());
  }

  periodic_profiler_->Start();
}

}  // namespace sampling_profiler