OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #import "ARDUtilities.h" | 11 #import "ARDUtilities.h" |
12 | 12 |
13 #import <mach/mach.h> | |
14 | |
13 #import "RTCLogging.h" | 15 #import "RTCLogging.h" |
14 | 16 |
15 @implementation NSDictionary (ARDUtilites) | 17 @implementation NSDictionary (ARDUtilites) |
16 | 18 |
17 + (NSDictionary *)dictionaryWithJSONString:(NSString *)jsonString { | 19 + (NSDictionary *)dictionaryWithJSONString:(NSString *)jsonString { |
18 NSParameterAssert(jsonString.length > 0); | 20 NSParameterAssert(jsonString.length > 0); |
19 NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; | 21 NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; |
20 NSError *error = nil; | 22 NSError *error = nil; |
21 NSDictionary *dict = | 23 NSDictionary *dict = |
22 [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; | 24 [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 } | 88 } |
87 return; | 89 return; |
88 } | 90 } |
89 if (completionHandler) { | 91 if (completionHandler) { |
90 completionHandler(YES, data); | 92 completionHandler(YES, data); |
91 } | 93 } |
92 }]; | 94 }]; |
93 } | 95 } |
94 | 96 |
95 @end | 97 @end |
98 | |
99 NSInteger ARDGetCpuUsagePercentage() { | |
100 // Create an array of thread ports for the current task | |
jiayl2
2015/08/13 22:21:50
nit: '.' at the end of a comment. here and other p
tkchin_webrtc
2015/08/13 23:55:31
Done.
| |
101 const task_t task = mach_task_self(); | |
102 thread_act_array_t thread_array; | |
103 mach_msg_type_number_t thread_count; | |
104 if (task_threads(task, &thread_array, &thread_count) != KERN_SUCCESS) { | |
105 return -1; | |
106 } | |
107 | |
108 // Sum cpu usage from all threads | |
109 float cpu_usage_percentage = 0; | |
110 thread_basic_info_data_t thread_info_data = {}; | |
111 mach_msg_type_number_t thread_info_count; | |
112 for (size_t i = 0; i < thread_count; ++i) { | |
113 thread_info_count = THREAD_BASIC_INFO_COUNT; | |
114 kern_return_t ret = thread_info(thread_array[i], | |
115 THREAD_BASIC_INFO, | |
116 (thread_info_t)&thread_info_data, | |
117 &thread_info_count); | |
118 if (ret == KERN_SUCCESS) { | |
119 cpu_usage_percentage += | |
120 100.f * (float)thread_info_data.cpu_usage / TH_USAGE_SCALE; | |
121 } | |
122 } | |
123 | |
124 // Dealloc the created array | |
125 vm_deallocate(task, (vm_address_t)thread_array, | |
126 sizeof(thread_act_t) * thread_count); | |
127 return lroundf(cpu_usage_percentage); | |
128 } | |
OLD | NEW |