darwin.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. * Permission is hereby granted, free of charge, to any person obtaining a copy
  3. * of this software and associated documentation files (the "Software"), to
  4. * deal in the Software without restriction, including without limitation the
  5. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  6. * sell copies of the Software, and to permit persons to whom the Software is
  7. * furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  18. * IN THE SOFTWARE.
  19. */
  20. #include "uv.h"
  21. #include "internal.h"
  22. #include <assert.h>
  23. #include <stdint.h>
  24. #include <errno.h>
  25. #include <mach/mach.h>
  26. #include <mach/mach_time.h>
  27. #include <mach-o/dyld.h> /* _NSGetExecutablePath */
  28. #include <sys/resource.h>
  29. #include <sys/sysctl.h>
  30. #include <unistd.h> /* sysconf */
  31. int uv__platform_loop_init(uv_loop_t* loop) {
  32. loop->cf_state = NULL;
  33. if (uv__kqueue_init(loop))
  34. return UV__ERR(errno);
  35. return 0;
  36. }
  37. void uv__platform_loop_delete(uv_loop_t* loop) {
  38. uv__fsevents_loop_delete(loop);
  39. }
  40. uint64_t uv__hrtime(uv_clocktype_t type) {
  41. static mach_timebase_info_data_t info;
  42. if ((ACCESS_ONCE(uint32_t, info.numer) == 0 ||
  43. ACCESS_ONCE(uint32_t, info.denom) == 0) &&
  44. mach_timebase_info(&info) != KERN_SUCCESS)
  45. abort();
  46. return mach_absolute_time() * info.numer / info.denom;
  47. }
  48. int uv_exepath(char* buffer, size_t* size) {
  49. /* realpath(exepath) may be > PATH_MAX so double it to be on the safe side. */
  50. char abspath[PATH_MAX * 2 + 1];
  51. char exepath[PATH_MAX + 1];
  52. uint32_t exepath_size;
  53. size_t abspath_size;
  54. if (buffer == NULL || size == NULL || *size == 0)
  55. return UV_EINVAL;
  56. exepath_size = sizeof(exepath);
  57. if (_NSGetExecutablePath(exepath, &exepath_size))
  58. return UV_EIO;
  59. if (realpath(exepath, abspath) != abspath)
  60. return UV__ERR(errno);
  61. abspath_size = strlen(abspath);
  62. if (abspath_size == 0)
  63. return UV_EIO;
  64. *size -= 1;
  65. if (*size > abspath_size)
  66. *size = abspath_size;
  67. memcpy(buffer, abspath, *size);
  68. buffer[*size] = '\0';
  69. return 0;
  70. }
  71. uint64_t uv_get_free_memory(void) {
  72. vm_statistics_data_t info;
  73. mach_msg_type_number_t count = sizeof(info) / sizeof(integer_t);
  74. if (host_statistics(mach_host_self(), HOST_VM_INFO,
  75. (host_info_t)&info, &count) != KERN_SUCCESS) {
  76. return UV_EINVAL; /* FIXME(bnoordhuis) Translate error. */
  77. }
  78. return (uint64_t) info.free_count * sysconf(_SC_PAGESIZE);
  79. }
  80. uint64_t uv_get_total_memory(void) {
  81. uint64_t info;
  82. int which[] = {CTL_HW, HW_MEMSIZE};
  83. size_t size = sizeof(info);
  84. if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
  85. return UV__ERR(errno);
  86. return (uint64_t) info;
  87. }
  88. uint64_t uv_get_constrained_memory(void) {
  89. return 0; /* Memory constraints are unknown. */
  90. }
  91. void uv_loadavg(double avg[3]) {
  92. struct loadavg info;
  93. size_t size = sizeof(info);
  94. int which[] = {CTL_VM, VM_LOADAVG};
  95. if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) < 0) return;
  96. avg[0] = (double) info.ldavg[0] / info.fscale;
  97. avg[1] = (double) info.ldavg[1] / info.fscale;
  98. avg[2] = (double) info.ldavg[2] / info.fscale;
  99. }
  100. int uv_resident_set_memory(size_t* rss) {
  101. mach_msg_type_number_t count;
  102. task_basic_info_data_t info;
  103. kern_return_t err;
  104. count = TASK_BASIC_INFO_COUNT;
  105. err = task_info(mach_task_self(),
  106. TASK_BASIC_INFO,
  107. (task_info_t) &info,
  108. &count);
  109. (void) &err;
  110. /* task_info(TASK_BASIC_INFO) cannot really fail. Anything other than
  111. * KERN_SUCCESS implies a libuv bug.
  112. */
  113. assert(err == KERN_SUCCESS);
  114. *rss = info.resident_size;
  115. return 0;
  116. }
  117. int uv_uptime(double* uptime) {
  118. time_t now;
  119. struct timeval info;
  120. size_t size = sizeof(info);
  121. static int which[] = {CTL_KERN, KERN_BOOTTIME};
  122. if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
  123. return UV__ERR(errno);
  124. now = time(NULL);
  125. *uptime = now - info.tv_sec;
  126. return 0;
  127. }
  128. int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
  129. unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
  130. multiplier = ((uint64_t)1000L / ticks);
  131. char model[512];
  132. uint64_t cpuspeed;
  133. size_t size;
  134. unsigned int i;
  135. natural_t numcpus;
  136. mach_msg_type_number_t msg_type;
  137. processor_cpu_load_info_data_t *info;
  138. uv_cpu_info_t* cpu_info;
  139. size = sizeof(model);
  140. if (sysctlbyname("machdep.cpu.brand_string", &model, &size, NULL, 0) &&
  141. sysctlbyname("hw.model", &model, &size, NULL, 0)) {
  142. return UV__ERR(errno);
  143. }
  144. size = sizeof(cpuspeed);
  145. if (sysctlbyname("hw.cpufrequency", &cpuspeed, &size, NULL, 0))
  146. return UV__ERR(errno);
  147. if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numcpus,
  148. (processor_info_array_t*)&info,
  149. &msg_type) != KERN_SUCCESS) {
  150. return UV_EINVAL; /* FIXME(bnoordhuis) Translate error. */
  151. }
  152. *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
  153. if (!(*cpu_infos)) {
  154. vm_deallocate(mach_task_self(), (vm_address_t)info, msg_type);
  155. return UV_ENOMEM;
  156. }
  157. *count = numcpus;
  158. for (i = 0; i < numcpus; i++) {
  159. cpu_info = &(*cpu_infos)[i];
  160. cpu_info->cpu_times.user = (uint64_t)(info[i].cpu_ticks[0]) * multiplier;
  161. cpu_info->cpu_times.nice = (uint64_t)(info[i].cpu_ticks[3]) * multiplier;
  162. cpu_info->cpu_times.sys = (uint64_t)(info[i].cpu_ticks[1]) * multiplier;
  163. cpu_info->cpu_times.idle = (uint64_t)(info[i].cpu_ticks[2]) * multiplier;
  164. cpu_info->cpu_times.irq = 0;
  165. cpu_info->model = uv__strdup(model);
  166. cpu_info->speed = cpuspeed/1000000;
  167. }
  168. vm_deallocate(mach_task_self(), (vm_address_t)info, msg_type);
  169. return 0;
  170. }