Index: webrtc/examples/objc/AppRTCDemo/common/ARDUtilities.m |
diff --git a/webrtc/examples/objc/AppRTCDemo/common/ARDUtilities.m b/webrtc/examples/objc/AppRTCDemo/common/ARDUtilities.m |
index 257b6a6a5c9e6ed6d61b5654990ac50a1a269b92..2d03f708a7c10dc980b132eda9ad2e9233b1baf1 100644 |
--- a/webrtc/examples/objc/AppRTCDemo/common/ARDUtilities.m |
+++ b/webrtc/examples/objc/AppRTCDemo/common/ARDUtilities.m |
@@ -10,6 +10,8 @@ |
#import "ARDUtilities.h" |
+#import <mach/mach.h> |
+ |
#import "RTCLogging.h" |
@implementation NSDictionary (ARDUtilites) |
@@ -93,3 +95,34 @@ |
} |
@end |
+ |
+NSInteger ARDGetCpuUsagePercentage() { |
+ // Create an array of thread ports for the current task. |
+ const task_t task = mach_task_self(); |
+ thread_act_array_t thread_array; |
+ mach_msg_type_number_t thread_count; |
+ if (task_threads(task, &thread_array, &thread_count) != KERN_SUCCESS) { |
+ return -1; |
+ } |
+ |
+ // Sum cpu usage from all threads. |
+ float cpu_usage_percentage = 0; |
+ thread_basic_info_data_t thread_info_data = {}; |
+ mach_msg_type_number_t thread_info_count; |
+ for (size_t i = 0; i < thread_count; ++i) { |
+ thread_info_count = THREAD_BASIC_INFO_COUNT; |
+ kern_return_t ret = thread_info(thread_array[i], |
+ THREAD_BASIC_INFO, |
+ (thread_info_t)&thread_info_data, |
+ &thread_info_count); |
+ if (ret == KERN_SUCCESS) { |
+ cpu_usage_percentage += |
+ 100.f * (float)thread_info_data.cpu_usage / TH_USAGE_SCALE; |
+ } |
+ } |
+ |
+ // Dealloc the created array. |
+ vm_deallocate(task, (vm_address_t)thread_array, |
+ sizeof(thread_act_t) * thread_count); |
+ return lroundf(cpu_usage_percentage); |
+} |