ARDUtilities.m (4643B)
1 /* 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. 3 * 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 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #import "ARDUtilities.h" 12 13 #import <mach/mach.h> 14 15 #import "sdk/objc/base/RTCLogging.h" 16 17 @implementation NSDictionary (ARDUtilites) 18 19 + (NSDictionary *)dictionaryWithJSONString:(NSString *)jsonString { 20 NSParameterAssert(jsonString.length > 0); 21 NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; 22 NSError *error = nil; 23 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data 24 options:0 25 error:&error]; 26 if (error) { 27 RTCLogError(@"Error parsing JSON: %@", error.localizedDescription); 28 } 29 return dict; 30 } 31 32 + (NSDictionary *)dictionaryWithJSONData:(NSData *)jsonData { 33 NSError *error = nil; 34 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData 35 options:0 36 error:&error]; 37 if (error) { 38 RTCLogError(@"Error parsing JSON: %@", error.localizedDescription); 39 } 40 return dict; 41 } 42 43 @end 44 45 @implementation NSURLConnection (ARDUtilities) 46 47 + (void)sendAsyncRequest:(NSURLRequest *)request 48 completionHandler:(void (^)(NSURLResponse *response, 49 NSData *data, 50 NSError *error))completionHandler { 51 // Kick off an async request which will call back on main thread. 52 NSURLSession *session = [NSURLSession sharedSession]; 53 [[session dataTaskWithRequest:request 54 completionHandler:^( 55 NSData *data, NSURLResponse *response, NSError *error) { 56 if (completionHandler) { 57 completionHandler(response, data, error); 58 } 59 }] resume]; 60 } 61 62 // Posts data to the specified URL. 63 + (void)sendAsyncPostToURL:(NSURL *)url 64 withData:(NSData *)data 65 completionHandler: 66 (void (^)(BOOL succeeded, NSData *data))completionHandler { 67 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 68 request.HTTPMethod = @"POST"; 69 request.HTTPBody = data; 70 [[self class] 71 sendAsyncRequest:request 72 completionHandler:^( 73 NSURLResponse *response, NSData *responseData, NSError *error) { 74 if (error) { 75 RTCLogError(@"Error posting data: %@", error.localizedDescription); 76 if (completionHandler) { 77 completionHandler(NO, responseData); 78 } 79 return; 80 } 81 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 82 if (httpResponse.statusCode != 200) { 83 NSString *serverResponse = responseData.length > 0 ? 84 [[NSString alloc] initWithData:responseData 85 encoding:NSUTF8StringEncoding] : 86 nil; 87 RTCLogError(@"Received bad response: %@", serverResponse); 88 if (completionHandler) { 89 completionHandler(NO, responseData); 90 } 91 return; 92 } 93 if (completionHandler) { 94 completionHandler(YES, responseData); 95 } 96 }]; 97 } 98 99 @end 100 101 NSInteger ARDGetCpuUsagePercentage(void) { 102 // Create an array of thread ports for the current task. 103 const task_t task = mach_task_self(); 104 thread_act_array_t thread_array; 105 mach_msg_type_number_t thread_count; 106 if (task_threads(task, &thread_array, &thread_count) != KERN_SUCCESS) { 107 return -1; 108 } 109 110 // Sum cpu usage from all threads. 111 float cpu_usage_percentage = 0; 112 thread_basic_info_data_t thread_info_data = {}; 113 mach_msg_type_number_t thread_info_count; 114 for (size_t i = 0; i < thread_count; ++i) { 115 thread_info_count = THREAD_BASIC_INFO_COUNT; 116 kern_return_t ret = thread_info(thread_array[i], 117 THREAD_BASIC_INFO, 118 (thread_info_t)&thread_info_data, 119 &thread_info_count); 120 if (ret == KERN_SUCCESS) { 121 cpu_usage_percentage += 122 100.f * (float)thread_info_data.cpu_usage / TH_USAGE_SCALE; 123 } 124 } 125 126 // Dealloc the created array. 127 vm_deallocate( 128 task, (vm_address_t)thread_array, sizeof(thread_act_t) * thread_count); 129 return lroundf(cpu_usage_percentage); 130 }