Flutter Engine
The Flutter Engine
sampling_profiler.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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#ifndef FLUTTER_SHELL_PROFILING_SAMPLING_PROFILER_H_
6#define FLUTTER_SHELL_PROFILING_SAMPLING_PROFILER_H_
7
8#include <functional>
9#include <memory>
10#include <optional>
11#include <string>
12
13#include "flutter/fml/synchronization/count_down_latch.h"
14#include "flutter/fml/task_runner.h"
15#include "flutter/fml/trace_event.h"
16
17namespace flutter {
18
19/**
20 * @brief CPU usage stats. `num_threads` is the number of threads owned by the
21 * process. It is to be noted that this is not per shell, there can be multiple
22 * shells within the process. `total_cpu_usage` is the percentage (between [0,
23 * 100]) cpu usage of the application. This is across all the cores, for example
24 * an application using 100% of all the core will report `total_cpu_usage` as
25 * `100`, if it has 100% across 2 cores and 0% across the other cores, embedder
26 * must report `total_cpu_usage` as `50`.
27 */
29 uint32_t num_threads;
31};
32
33/**
34 * @brief Memory usage stats. `dirty_memory_usage` is the memory usage (in
35 * MB) such that the app uses its physical memory for dirty memory. Dirty memory
36 * is the memory data that cannot be paged to disk. `owned_shared_memory_usage`
37 * is the memory usage (in MB) such that the app uses its physical memory for
38 * shared memory, including loaded frameworks and executables. On iOS, it's
39 * `physical memory - dirty memory`.
40 */
44};
45
46/**
47 * @brief Polled information related to the usage of the GPU.
48 */
51};
52
53/**
54 * @brief Container for the metrics we collect during each run of `Sampler`.
55 * This currently holds `CpuUsageInfo` and `MemoryUsageInfo` but the intent
56 * is to expand it to other metrics.
57 *
58 * @see flutter::Sampler
59 */
61 std::optional<CpuUsageInfo> cpu_usage;
62 std::optional<MemoryUsageInfo> memory_usage;
63 std::optional<GpuUsageInfo> gpu_usage;
64};
65
66/**
67 * @brief Sampler is run during `SamplingProfiler::SampleRepeatedly`. Each
68 * platform should implement its version of a `Sampler` if they decide to
69 * participate in gathering profiling metrics.
70 *
71 * @see flutter::SamplingProfiler::SampleRepeatedly
72 */
74
75/**
76 * @brief a Sampling Profiler that runs peridically and calls the `Sampler`
77 * which servers as a value function to gather various profiling metrics as
78 * represented by `ProfileSample`. These profiling metrics are then posted to
79 * the Dart VM Service timeline.
80 *
81 */
83 public:
84 /**
85 * @brief Construct a new Sampling Profiler object
86 *
87 * @param thread_label Dart VM Service prefix to be set for the profiling task
88 * runner.
89 * @param profiler_task_runner the task runner to service sampling requests.
90 * @param sampler the value function to collect the profiling metrics.
91 * @param num_samples_per_sec number of times you wish to run the sampler per
92 * second.
93 *
94 * @see fml::TaskRunner
95 */
96 SamplingProfiler(const char* thread_label,
97 fml::RefPtr<fml::TaskRunner> profiler_task_runner,
98 Sampler sampler,
99 int num_samples_per_sec);
100
102
103 /**
104 * @brief Starts the SamplingProfiler by triggering `SampleRepeatedly`.
105 *
106 */
107 void Start();
108
109 void Stop();
110
111 private:
112 const std::string thread_label_;
113 const fml::RefPtr<fml::TaskRunner> profiler_task_runner_;
114 const Sampler sampler_;
115 const uint32_t num_samples_per_sec_;
116 bool is_running_ = false;
117 std::atomic<fml::AutoResetWaitableEvent*> shutdown_latch_ = nullptr;
118
119 void SampleRepeatedly(fml::TimeDelta task_delay) const;
120
121 /**
122 * @brief This doesn't update the underlying OS thread name for the thread
123 * backing `profiler_task_runner_`. Instead, this is just additional metadata
124 * for the VM Service to show the thread name of the isolate.
125 *
126 */
127 void UpdateDartVMServiceThreadName() const;
128
129 FML_DISALLOW_COPY_AND_ASSIGN(SamplingProfiler);
130};
131
132} // namespace flutter
133
134#endif // FLUTTER_SHELL_PROFILING_SAMPLING_PROFILER_H_
a Sampling Profiler that runs peridically and calls the Sampler which servers as a value function to ...
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.
Dart_NativeFunction function
Definition: fuchsia.cc:51
std::function< ProfileSample(void)> Sampler
Sampler is run during SamplingProfiler::SampleRepeatedly. Each platform should implement its version ...
CPU usage stats. num_threads is the number of threads owned by the process. It is to be noted that th...
Polled information related to the usage of the GPU.
Memory usage stats. dirty_memory_usage is the memory usage (in MB) such that the app uses its physica...
Container for the metrics we collect during each run of Sampler. This currently holds CpuUsageInfo an...
std::optional< CpuUsageInfo > cpu_usage
std::optional< GpuUsageInfo > gpu_usage
std::optional< MemoryUsageInfo > memory_usage