5#include "flutter/shell/profiling/sampling_profiler.h"
12 const char* thread_label,
15 int num_samples_per_sec)
16 : thread_label_(thread_label),
17 profiler_task_runner_(
std::move(profiler_task_runner)),
18 sampler_(
std::move(sampler)),
19 num_samples_per_sec_(num_samples_per_sec) {}
28 if (!profiler_task_runner_) {
32 <<
"number of samples must be a positive integer, got: "
33 << num_samples_per_sec_;
34 double delay_between_samples = 1.0 / num_samples_per_sec_;
36 UpdateDartVMServiceThreadName();
38 SampleRepeatedly(task_delay);
43 auto latch = std::make_unique<fml::AutoResetWaitableEvent>();
44 shutdown_latch_.store(latch.get());
46 shutdown_latch_.store(
nullptr);
50void SamplingProfiler::SampleRepeatedly(
fml::TimeDelta task_delay)
const {
52 [profiler =
this, task_delay = task_delay, sampler = sampler_,
53 &shutdown_latch = shutdown_latch_]() {
57 if (
usage.cpu_usage) {
58 const auto& cpu_usage =
usage.cpu_usage;
59 std::string total_cpu_usage =
63 "total_cpu_usage", total_cpu_usage.c_str(),
64 "num_threads", num_threads.c_str());
66 if (
usage.memory_usage) {
67 std::string dirty_memory_usage =
69 std::string owned_shared_memory_usage =
72 "dirty_memory_usage", dirty_memory_usage.c_str(),
73 "owned_shared_memory_usage",
74 owned_shared_memory_usage.c_str());
76 if (
usage.gpu_usage) {
77 std::string gpu_usage =
82 if (shutdown_latch.load()) {
83 shutdown_latch.load()->Signal();
85 profiler->SampleRepeatedly(task_delay);
91void SamplingProfiler::UpdateDartVMServiceThreadName()
const {
95 [label = thread_label_ + std::string{
".profiler"}]() {
SamplingProfiler(const char *thread_label, fml::RefPtr< fml::TaskRunner > profiler_task_runner, Sampler sampler, int num_samples_per_sec)
Construct a new Sampling Profiler object.
void Start()
Starts the SamplingProfiler by triggering SampleRepeatedly.
virtual void PostTask(const fml::closure &task) override
virtual void PostDelayedTask(const fml::closure &task, fml::TimeDelta delay)
static constexpr TimeDelta FromSecondsF(double seconds)
#define FML_CHECK(condition)
#define FML_DCHECK(condition)
std::function< ProfileSample(void)> Sampler
Sampler is run during SamplingProfiler::SampleRepeatedly. Each platform should implement its version ...
static SkString to_string(int n)
static void usage(char *argv0)
Container for the metrics we collect during each run of Sampler. This currently holds CpuUsageInfo an...
#define TRACE_EVENT_INSTANT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
#define TRACE_EVENT_INSTANT1(category_group, name, arg1_name, arg1_val)