Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[profiling] Handle thread_info to account for killed threads #22170

Merged
merged 2 commits into from
Oct 29, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions shell/platform/darwin/ios/framework/Source/profiler_metrics_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ void DeleteIO(io_object_t value) {
}

double total_cpu_usage = 0.0;
uint32_t num_threads = mach_threads.thread_count;

// Add the CPU usage for each thread. It should be noted that there may be some CPU usage missing
// from this calculation. If a thread ends between calls to this routine, then its info will be
Expand All @@ -182,17 +183,30 @@ void DeleteIO(io_object_t value) {
kernel_return_code =
thread_info(mach_threads.threads[i], THREAD_BASIC_INFO,
reinterpret_cast<thread_info_t>(&basic_thread_info), &thread_info_count);
if (kernel_return_code != KERN_SUCCESS) {
FML_LOG(ERROR) << "Error retrieving thread information: "
<< mach_error_string(kernel_return_code);
return std::nullopt;
switch (kernel_return_code) {
case KERN_SUCCESS: {
const double current_thread_cpu_usage =
basic_thread_info.cpu_usage / static_cast<float>(TH_USAGE_SCALE);
total_cpu_usage += current_thread_cpu_usage;
break;
}
case MACH_SEND_TIMEOUT:
case MACH_SEND_TIMED_OUT:
case MACH_SEND_INVALID_DEST:
// Ignore as this thread been destroyed. The possible return codes are not really well
// documented. This handling is inspired from the following sources:
// - https://opensource.apple.com/source/xnu/xnu-4903.221.2/tests/task_inspect.c.auto.html
// - https://github.com/apple/swift-corelibs-libdispatch/blob/main/src/queue.c#L6617
num_threads--;
break;
default:
FML_LOG(ERROR) << "Error retrieving thread information: "
<< mach_error_string(kernel_return_code);
return std::nullopt;
}
const double current_thread_cpu_usage =
basic_thread_info.cpu_usage / static_cast<float>(TH_USAGE_SCALE);
total_cpu_usage += current_thread_cpu_usage;
}

flutter::CpuUsageInfo cpu_usage_info = {.num_threads = mach_threads.thread_count,
flutter::CpuUsageInfo cpu_usage_info = {.num_threads = num_threads,
.total_cpu_usage = total_cpu_usage * 100.0};
return cpu_usage_info;
}
Expand Down