ARDUtilities.m 4.2 KB

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