7#import <Foundation/Foundation.h>
21 thread_array_t threads = NULL;
22 mach_msg_type_number_t thread_count = 0;
27 [[maybe_unused]] kern_return_t kernel_return_code = vm_deallocate(
28 mach_task_self(),
reinterpret_cast<vm_offset_t
>(threads), thread_count *
sizeof(thread_t));
29 FML_DCHECK(kernel_return_code == KERN_SUCCESS) <<
"Failed to deallocate thread infos.";
38#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG || \
39 FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE
45struct CFRefTraits<io_object_t> {
58#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG || \
59 FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE
61std::optional<GpuUsageInfo> FindGpuUsageInfo(
io_iterator_t iterator) {
64 CFMutableDictionaryRef cf_service_dictionary;
70 NSDictionary* service_dictionary = (__bridge_transfer NSDictionary*)cf_service_dictionary;
71 cf_service_dictionary =
nullptr;
72 NSDictionary* performanceStatistics = service_dictionary[
@"PerformanceStatistics"];
73 NSNumber* utilization = performanceStatistics[
@"Device Utilization %"];
75 return (GpuUsageInfo){.percent_usage = [utilization doubleValue]};
81[[maybe_unused]] std::optional<GpuUsageInfo> FindSimulatorGpuUsageInfo() {
86 return FindGpuUsageInfo(iterator.Get());
91[[maybe_unused]] std::optional<GpuUsageInfo> FindDeviceGpuUsageInfo() {
102 std::optional<GpuUsageInfo> result = FindGpuUsageInfo(inner_iterator.Get());
103 if (result.has_value()) {
115std::optional<GpuUsageInfo> PollGpuUsage() {
116#if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_RELEASE || \
117 FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_JIT_RELEASE)
119#elif TARGET_IPHONE_SIMULATOR
120 return FindSimulatorGpuUsageInfo();
122 return FindDeviceGpuUsageInfo();
128 return {.
cpu_usage = CpuUsage(), .memory_usage = MemoryUsage(), .gpu_usage = PollGpuUsage()};
131std::optional<CpuUsageInfo> ProfilerMetricsIOS::CpuUsage() {
132 kern_return_t kernel_return_code;
133 MachThreads mach_threads = MachThreads();
137 task_threads(mach_task_self(), &mach_threads.threads, &mach_threads.thread_count);
138 if (kernel_return_code != KERN_SUCCESS) {
142 double total_cpu_usage = 0.0;
143 uint32_t num_threads = mach_threads.thread_count;
152 for (mach_msg_type_number_t
i = 0;
i < mach_threads.thread_count;
i++) {
153 thread_basic_info_data_t basic_thread_info;
154 mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT;
156 thread_info(mach_threads.threads[
i], THREAD_BASIC_INFO,
157 reinterpret_cast<thread_info_t
>(&basic_thread_info), &thread_info_count);
158 switch (kernel_return_code) {
160 const double current_thread_cpu_usage =
161 basic_thread_info.cpu_usage /
static_cast<float>(TH_USAGE_SCALE);
162 total_cpu_usage += current_thread_cpu_usage;
165 case MACH_SEND_TIMEOUT:
166 case MACH_SEND_TIMED_OUT:
167 case MACH_SEND_INVALID_DEST:
180 .total_cpu_usage = total_cpu_usage * 100.0};
181 return cpu_usage_info;
184std::optional<MemoryUsageInfo> ProfilerMetricsIOS::MemoryUsage() {
185 kern_return_t kernel_return_code;
186 task_vm_info_data_t task_memory_info;
187 mach_msg_type_number_t task_memory_info_count = TASK_VM_INFO_COUNT;
190 task_info(mach_task_self(), TASK_VM_INFO,
reinterpret_cast<task_info_t
>(&task_memory_info),
191 &task_memory_info_count);
192 if (kernel_return_code != KERN_SUCCESS) {
201 const double dirty_memory_usage =
202 static_cast<double>(task_memory_info.phys_footprint) / 1024.0 / 1024.0;
203 const double owned_shared_memory_usage =
204 static_cast<double>(task_memory_info.resident_size) / 1024.0 / 1024.0 - dirty_memory_usage;
207 .owned_shared_memory_usage = owned_shared_memory_usage};
208 return memory_usage_info;
kern_return_t IORegistryEntryGetChildIterator(io_registry_entry_t entry, const io_name_t plane, io_iterator_t *it)
io_object_t io_iterator_t
const mach_port_t kIOMasterPortDefault
constexpr const char * kIOServicePlane
CFMutableDictionaryRef IOServiceNameMatching(const char *name) CF_RETURNS_RETAINED
kern_return_t IORegistryEntryCreateCFProperties(io_registry_entry_t entry, CFMutableDictionaryRef *properties, CFAllocatorRef allocator, uint32_t options)
kern_return_t IOObjectRelease(io_object_t object)
kern_return_t IOServiceGetMatchingServices(mach_port_t master, CFDictionaryRef matching CF_RELEASES_ARGUMENT, io_iterator_t *it)
kern_return_t IOObjectRetain(io_object_t object)
io_object_t IOIteratorNext(io_iterator_t it)
ProfileSample GenerateSample()
#define FML_DCHECK(condition)
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
CPU usage stats. num_threads is the number of threads owned by the process. It is to be noted that th...
Memory usage stats. dirty_memory_usage is the memory usage (in MB) such that the app uses its physica...
double dirty_memory_usage
Container for the metrics we collect during each run of Sampler. This currently holds CpuUsageInfo an...
std::optional< CpuUsageInfo > cpu_usage
static void Retain(T instance)
static constexpr T kNullValue
static void Release(T instance)