1
0

platform-cocoa.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2013-2014 Ruwen Hahn <[email protected]>
  3. * Hugh "Jim" Bailey <[email protected]>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "base.h"
  18. #include "platform.h"
  19. #include "dstr.h"
  20. #include <dlfcn.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <CoreServices/CoreServices.h>
  24. #include <mach/mach.h>
  25. #include <mach/mach_time.h>
  26. #import <Cocoa/Cocoa.h>
  27. /* clock function selection taken from libc++ */
  28. static uint64_t ns_time_simple()
  29. {
  30. return mach_absolute_time();
  31. }
  32. static double ns_time_compute_factor()
  33. {
  34. mach_timebase_info_data_t info = {1, 1};
  35. mach_timebase_info(&info);
  36. return ((double)info.numer) / info.denom;
  37. }
  38. static uint64_t ns_time_full()
  39. {
  40. static double factor = 0.;
  41. if (factor == 0.) factor = ns_time_compute_factor();
  42. return (uint64_t)(mach_absolute_time() * factor);
  43. }
  44. typedef uint64_t (*time_func)();
  45. static time_func ns_time_select_func()
  46. {
  47. mach_timebase_info_data_t info = {1, 1};
  48. mach_timebase_info(&info);
  49. if (info.denom == info.numer)
  50. return ns_time_simple;
  51. return ns_time_full;
  52. }
  53. uint64_t os_gettime_ns(void)
  54. {
  55. static time_func f = NULL;
  56. if (!f) f = ns_time_select_func();
  57. return f();
  58. }
  59. /* gets the location ~/Library/Application Support/[name] */
  60. char *os_get_config_path(const char *name)
  61. {
  62. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  63. NSApplicationSupportDirectory, NSUserDomainMask, YES);
  64. if([paths count] == 0)
  65. bcrash("Could not get home directory (platform-cocoa)");
  66. NSString *application_support = paths[0];
  67. NSUInteger len = [application_support
  68. lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  69. char *path_ptr = bmalloc(len+1);
  70. path_ptr[len] = 0;
  71. memcpy(path_ptr, [application_support UTF8String], len);
  72. struct dstr path;
  73. dstr_init_move_array(&path, path_ptr);
  74. dstr_cat(&path, "/");
  75. dstr_cat(&path, name);
  76. return path.array;
  77. }
  78. struct os_cpu_usage_info {
  79. int64_t last_cpu_time;
  80. int64_t last_sys_time;
  81. int core_count;
  82. };
  83. static inline void add_time_value(time_value_t *dst, time_value_t *a,
  84. time_value_t *b)
  85. {
  86. dst->microseconds = a->microseconds + b->microseconds;
  87. dst->seconds = a->seconds + b->seconds;
  88. if (dst->microseconds >= 1000000) {
  89. dst->seconds += dst->microseconds / 1000000;
  90. dst->microseconds %= 1000000;
  91. }
  92. }
  93. static bool get_time_info(int64_t *cpu_time, int64_t *sys_time)
  94. {
  95. mach_port_t task = mach_task_self();
  96. struct task_thread_times_info thread_data;
  97. struct task_basic_info_64 task_data;
  98. mach_msg_type_number_t count;
  99. kern_return_t kern_ret;
  100. time_value_t cur_time;
  101. *cpu_time = 0;
  102. *sys_time = 0;
  103. count = TASK_THREAD_TIMES_INFO_COUNT;
  104. kern_ret = task_info(task, TASK_THREAD_TIMES_INFO,
  105. (task_info_t)&thread_data, &count);
  106. if (kern_ret != KERN_SUCCESS)
  107. return false;
  108. count = TASK_BASIC_INFO_64_COUNT;
  109. kern_ret = task_info(task, TASK_BASIC_INFO_64,
  110. (task_info_t)&task_data, &count);
  111. if (kern_ret != KERN_SUCCESS)
  112. return false;
  113. add_time_value(&cur_time, &thread_data.user_time,
  114. &thread_data.system_time);
  115. add_time_value(&cur_time, &cur_time, &task_data.user_time);
  116. add_time_value(&cur_time, &cur_time, &task_data.system_time);
  117. *cpu_time = os_gettime_ns() / 1000;
  118. *sys_time = cur_time.seconds * 1000000 + cur_time.microseconds;
  119. return true;
  120. }
  121. os_cpu_usage_info_t *os_cpu_usage_info_start(void)
  122. {
  123. struct os_cpu_usage_info *info = bmalloc(sizeof(*info));
  124. if (!get_time_info(&info->last_cpu_time, &info->last_sys_time)) {
  125. bfree(info);
  126. return NULL;
  127. }
  128. info->core_count = sysconf(_SC_NPROCESSORS_ONLN);
  129. return info;
  130. }
  131. double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
  132. {
  133. int64_t sys_time, cpu_time;
  134. int64_t sys_time_delta, cpu_time_delta;
  135. if (!info || !get_time_info(&cpu_time, &sys_time))
  136. return 0.0;
  137. sys_time_delta = sys_time - info->last_sys_time;
  138. cpu_time_delta = cpu_time - info->last_cpu_time;
  139. if (cpu_time_delta == 0)
  140. return 0.0;
  141. info->last_sys_time = sys_time;
  142. info->last_cpu_time = cpu_time;
  143. return (double)sys_time_delta * 100.0 / (double)cpu_time_delta /
  144. (double)info->core_count;
  145. }
  146. void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
  147. {
  148. if (info)
  149. bfree(info);
  150. }
  151. os_performance_token_t *os_request_high_performance(const char *reason)
  152. {
  153. @autoreleasepool {
  154. NSProcessInfo *pi = [NSProcessInfo processInfo];
  155. SEL sel = @selector(beginActivityWithOptions:reason:);
  156. if (![pi respondsToSelector:sel])
  157. return nil;
  158. //taken from http://stackoverflow.com/a/20100906
  159. id activity = [pi beginActivityWithOptions:0x00FFFFFF
  160. reason:@(reason)];
  161. return CFBridgingRetain(activity);
  162. }
  163. }
  164. void os_end_high_performance(os_performance_token_t *token)
  165. {
  166. @autoreleasepool {
  167. NSProcessInfo *pi = [NSProcessInfo processInfo];
  168. SEL sel = @selector(beginActivityWithOptions:reason:);
  169. if (![pi respondsToSelector:sel])
  170. return;
  171. [pi endActivity:CFBridgingRelease(token)];
  172. }
  173. }