platform-cocoa.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #include <IOKit/pwr_mgt/IOPMLib.h>
  27. #import <Cocoa/Cocoa.h>
  28. /* clock function selection taken from libc++ */
  29. static uint64_t ns_time_simple()
  30. {
  31. return mach_absolute_time();
  32. }
  33. static double ns_time_compute_factor()
  34. {
  35. mach_timebase_info_data_t info = {1, 1};
  36. mach_timebase_info(&info);
  37. return ((double)info.numer) / info.denom;
  38. }
  39. static uint64_t ns_time_full()
  40. {
  41. static double factor = 0.;
  42. if (factor == 0.) factor = ns_time_compute_factor();
  43. return (uint64_t)(mach_absolute_time() * factor);
  44. }
  45. typedef uint64_t (*time_func)();
  46. static time_func ns_time_select_func()
  47. {
  48. mach_timebase_info_data_t info = {1, 1};
  49. mach_timebase_info(&info);
  50. if (info.denom == info.numer)
  51. return ns_time_simple;
  52. return ns_time_full;
  53. }
  54. uint64_t os_gettime_ns(void)
  55. {
  56. static time_func f = NULL;
  57. if (!f) f = ns_time_select_func();
  58. return f();
  59. }
  60. /* gets the location ~/Library/Application Support/[name] */
  61. int os_get_config_path(char *dst, size_t size, const char *name)
  62. {
  63. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  64. NSApplicationSupportDirectory, NSUserDomainMask, YES);
  65. if([paths count] == 0)
  66. bcrash("Could not get home directory (platform-cocoa)");
  67. NSString *application_support = paths[0];
  68. const char *base_path = [application_support UTF8String];
  69. if (!name || !*name)
  70. return snprintf(dst, size, "%s", base_path);
  71. else
  72. return snprintf(dst, size, "%s/%s", base_path, name);
  73. }
  74. char *os_get_config_path_ptr(const char *name)
  75. {
  76. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  77. NSApplicationSupportDirectory, NSUserDomainMask, YES);
  78. if([paths count] == 0)
  79. bcrash("Could not get home directory (platform-cocoa)");
  80. NSString *application_support = paths[0];
  81. NSUInteger len = [application_support
  82. lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  83. char *path_ptr = bmalloc(len+1);
  84. path_ptr[len] = 0;
  85. memcpy(path_ptr, [application_support UTF8String], len);
  86. struct dstr path;
  87. dstr_init_move_array(&path, path_ptr);
  88. dstr_cat(&path, "/");
  89. dstr_cat(&path, name);
  90. return path.array;
  91. }
  92. struct os_cpu_usage_info {
  93. int64_t last_cpu_time;
  94. int64_t last_sys_time;
  95. int core_count;
  96. };
  97. static inline void add_time_value(time_value_t *dst, time_value_t *a,
  98. time_value_t *b)
  99. {
  100. dst->microseconds = a->microseconds + b->microseconds;
  101. dst->seconds = a->seconds + b->seconds;
  102. if (dst->microseconds >= 1000000) {
  103. dst->seconds += dst->microseconds / 1000000;
  104. dst->microseconds %= 1000000;
  105. }
  106. }
  107. static bool get_time_info(int64_t *cpu_time, int64_t *sys_time)
  108. {
  109. mach_port_t task = mach_task_self();
  110. struct task_thread_times_info thread_data;
  111. struct task_basic_info_64 task_data;
  112. mach_msg_type_number_t count;
  113. kern_return_t kern_ret;
  114. time_value_t cur_time;
  115. *cpu_time = 0;
  116. *sys_time = 0;
  117. count = TASK_THREAD_TIMES_INFO_COUNT;
  118. kern_ret = task_info(task, TASK_THREAD_TIMES_INFO,
  119. (task_info_t)&thread_data, &count);
  120. if (kern_ret != KERN_SUCCESS)
  121. return false;
  122. count = TASK_BASIC_INFO_64_COUNT;
  123. kern_ret = task_info(task, TASK_BASIC_INFO_64,
  124. (task_info_t)&task_data, &count);
  125. if (kern_ret != KERN_SUCCESS)
  126. return false;
  127. add_time_value(&cur_time, &thread_data.user_time,
  128. &thread_data.system_time);
  129. add_time_value(&cur_time, &cur_time, &task_data.user_time);
  130. add_time_value(&cur_time, &cur_time, &task_data.system_time);
  131. *cpu_time = os_gettime_ns() / 1000;
  132. *sys_time = cur_time.seconds * 1000000 + cur_time.microseconds;
  133. return true;
  134. }
  135. os_cpu_usage_info_t *os_cpu_usage_info_start(void)
  136. {
  137. struct os_cpu_usage_info *info = bmalloc(sizeof(*info));
  138. if (!get_time_info(&info->last_cpu_time, &info->last_sys_time)) {
  139. bfree(info);
  140. return NULL;
  141. }
  142. info->core_count = sysconf(_SC_NPROCESSORS_ONLN);
  143. return info;
  144. }
  145. double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
  146. {
  147. int64_t sys_time, cpu_time;
  148. int64_t sys_time_delta, cpu_time_delta;
  149. if (!info || !get_time_info(&cpu_time, &sys_time))
  150. return 0.0;
  151. sys_time_delta = sys_time - info->last_sys_time;
  152. cpu_time_delta = cpu_time - info->last_cpu_time;
  153. if (cpu_time_delta == 0)
  154. return 0.0;
  155. info->last_sys_time = sys_time;
  156. info->last_cpu_time = cpu_time;
  157. return (double)sys_time_delta * 100.0 / (double)cpu_time_delta /
  158. (double)info->core_count;
  159. }
  160. void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
  161. {
  162. if (info)
  163. bfree(info);
  164. }
  165. os_performance_token_t *os_request_high_performance(const char *reason)
  166. {
  167. @autoreleasepool {
  168. NSProcessInfo *pi = [NSProcessInfo processInfo];
  169. SEL sel = @selector(beginActivityWithOptions:reason:);
  170. if (![pi respondsToSelector:sel])
  171. return nil;
  172. //taken from http://stackoverflow.com/a/20100906
  173. id activity = [pi beginActivityWithOptions:0x00FFFFFF
  174. reason:@(reason)];
  175. return CFBridgingRetain(activity);
  176. }
  177. }
  178. void os_end_high_performance(os_performance_token_t *token)
  179. {
  180. @autoreleasepool {
  181. NSProcessInfo *pi = [NSProcessInfo processInfo];
  182. SEL sel = @selector(beginActivityWithOptions:reason:);
  183. if (![pi respondsToSelector:sel])
  184. return;
  185. [pi endActivity:CFBridgingRelease(token)];
  186. }
  187. }
  188. struct os_inhibit_info {
  189. CFStringRef reason;
  190. IOPMAssertionID sleep_id;
  191. IOPMAssertionID user_id;
  192. bool active;
  193. };
  194. os_inhibit_t *os_inhibit_sleep_create(const char *reason)
  195. {
  196. struct os_inhibit_info *info = bzalloc(sizeof(*info));
  197. if (!reason)
  198. info->reason = CFStringCreateWithCString(kCFAllocatorDefault,
  199. reason, kCFStringEncodingUTF8);
  200. else
  201. info->reason = CFStringCreateCopy(kCFAllocatorDefault,
  202. CFSTR(""));
  203. return info;
  204. }
  205. bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
  206. {
  207. IOReturn success;
  208. if (!info)
  209. return false;
  210. if (info->active == active)
  211. return false;
  212. if (active) {
  213. IOPMAssertionDeclareUserActivity(info->reason,
  214. kIOPMUserActiveLocal, &info->user_id);
  215. success = IOPMAssertionCreateWithName(
  216. kIOPMAssertionTypeNoDisplaySleep,
  217. kIOPMAssertionLevelOn, info->reason,
  218. &info->sleep_id);
  219. if (success != kIOReturnSuccess) {
  220. blog(LOG_WARNING, "Failed to disable sleep");
  221. return false;
  222. }
  223. } else {
  224. IOPMAssertionRelease(info->sleep_id);
  225. }
  226. info->active = active;
  227. return true;
  228. }
  229. void os_inhibit_sleep_destroy(os_inhibit_t *info)
  230. {
  231. if (info) {
  232. os_inhibit_sleep_set_active(info, false);
  233. CFRelease(info->reason);
  234. bfree(info);
  235. }
  236. }