1
0

netbsd.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 <string.h>
  24. #include <errno.h>
  25. #include <kvm.h>
  26. #include <paths.h>
  27. #include <unistd.h>
  28. #include <time.h>
  29. #include <stdlib.h>
  30. #include <fcntl.h>
  31. #include <sys/resource.h>
  32. #include <sys/types.h>
  33. #include <sys/sysctl.h>
  34. #include <uvm/uvm_extern.h>
  35. #include <unistd.h>
  36. #include <time.h>
  37. int uv__platform_loop_init(uv_loop_t* loop) {
  38. return uv__kqueue_init(loop);
  39. }
  40. void uv__platform_loop_delete(uv_loop_t* loop) {
  41. }
  42. void uv_loadavg(double avg[3]) {
  43. struct loadavg info;
  44. size_t size = sizeof(info);
  45. int which[] = {CTL_VM, VM_LOADAVG};
  46. if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) == -1) return;
  47. avg[0] = (double) info.ldavg[0] / info.fscale;
  48. avg[1] = (double) info.ldavg[1] / info.fscale;
  49. avg[2] = (double) info.ldavg[2] / info.fscale;
  50. }
  51. int uv_exepath(char* buffer, size_t* size) {
  52. /* Intermediate buffer, retrieving partial path name does not work
  53. * As of NetBSD-8(beta), vnode->path translator does not handle files
  54. * with longer names than 31 characters.
  55. */
  56. char int_buf[PATH_MAX];
  57. size_t int_size;
  58. int mib[4];
  59. if (buffer == NULL || size == NULL || *size == 0)
  60. return UV_EINVAL;
  61. mib[0] = CTL_KERN;
  62. mib[1] = KERN_PROC_ARGS;
  63. mib[2] = -1;
  64. mib[3] = KERN_PROC_PATHNAME;
  65. int_size = ARRAY_SIZE(int_buf);
  66. if (sysctl(mib, 4, int_buf, &int_size, NULL, 0))
  67. return UV__ERR(errno);
  68. /* Copy string from the intermediate buffer to outer one with appropriate
  69. * length.
  70. */
  71. /* TODO(bnoordhuis) Check uv__strscpy() return value. */
  72. uv__strscpy(buffer, int_buf, *size);
  73. /* Set new size. */
  74. *size = strlen(buffer);
  75. return 0;
  76. }
  77. uint64_t uv_get_free_memory(void) {
  78. struct uvmexp info;
  79. size_t size = sizeof(info);
  80. int which[] = {CTL_VM, VM_UVMEXP};
  81. if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
  82. return UV__ERR(errno);
  83. return (uint64_t) info.free * sysconf(_SC_PAGESIZE);
  84. }
  85. uint64_t uv_get_total_memory(void) {
  86. #if defined(HW_PHYSMEM64)
  87. uint64_t info;
  88. int which[] = {CTL_HW, HW_PHYSMEM64};
  89. #else
  90. unsigned int info;
  91. int which[] = {CTL_HW, HW_PHYSMEM};
  92. #endif
  93. size_t size = sizeof(info);
  94. if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
  95. return UV__ERR(errno);
  96. return (uint64_t) info;
  97. }
  98. uint64_t uv_get_constrained_memory(void) {
  99. return 0; /* Memory constraints are unknown. */
  100. }
  101. int uv_resident_set_memory(size_t* rss) {
  102. kvm_t *kd = NULL;
  103. struct kinfo_proc2 *kinfo = NULL;
  104. pid_t pid;
  105. int nprocs;
  106. int max_size = sizeof(struct kinfo_proc2);
  107. int page_size;
  108. page_size = getpagesize();
  109. pid = getpid();
  110. kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
  111. if (kd == NULL) goto error;
  112. kinfo = kvm_getproc2(kd, KERN_PROC_PID, pid, max_size, &nprocs);
  113. if (kinfo == NULL) goto error;
  114. *rss = kinfo->p_vm_rssize * page_size;
  115. kvm_close(kd);
  116. return 0;
  117. error:
  118. if (kd) kvm_close(kd);
  119. return UV_EPERM;
  120. }
  121. int uv_uptime(double* uptime) {
  122. time_t now;
  123. struct timeval info;
  124. size_t size = sizeof(info);
  125. static int which[] = {CTL_KERN, KERN_BOOTTIME};
  126. if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
  127. return UV__ERR(errno);
  128. now = time(NULL);
  129. *uptime = (double)(now - info.tv_sec);
  130. return 0;
  131. }
  132. int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
  133. unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK);
  134. unsigned int multiplier = ((uint64_t)1000L / ticks);
  135. unsigned int cur = 0;
  136. uv_cpu_info_t* cpu_info;
  137. u_int64_t* cp_times;
  138. char model[512];
  139. u_int64_t cpuspeed;
  140. int numcpus;
  141. size_t size;
  142. int i;
  143. size = sizeof(model);
  144. if (sysctlbyname("machdep.cpu_brand", &model, &size, NULL, 0) &&
  145. sysctlbyname("hw.model", &model, &size, NULL, 0)) {
  146. return UV__ERR(errno);
  147. }
  148. size = sizeof(numcpus);
  149. if (sysctlbyname("hw.ncpu", &numcpus, &size, NULL, 0))
  150. return UV__ERR(errno);
  151. *count = numcpus;
  152. /* Only i386 and amd64 have machdep.tsc_freq */
  153. size = sizeof(cpuspeed);
  154. if (sysctlbyname("machdep.tsc_freq", &cpuspeed, &size, NULL, 0))
  155. cpuspeed = 0;
  156. size = numcpus * CPUSTATES * sizeof(*cp_times);
  157. cp_times = uv__malloc(size);
  158. if (cp_times == NULL)
  159. return UV_ENOMEM;
  160. if (sysctlbyname("kern.cp_time", cp_times, &size, NULL, 0))
  161. return UV__ERR(errno);
  162. *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
  163. if (!(*cpu_infos)) {
  164. uv__free(cp_times);
  165. uv__free(*cpu_infos);
  166. return UV_ENOMEM;
  167. }
  168. for (i = 0; i < numcpus; i++) {
  169. cpu_info = &(*cpu_infos)[i];
  170. cpu_info->cpu_times.user = (uint64_t)(cp_times[CP_USER+cur]) * multiplier;
  171. cpu_info->cpu_times.nice = (uint64_t)(cp_times[CP_NICE+cur]) * multiplier;
  172. cpu_info->cpu_times.sys = (uint64_t)(cp_times[CP_SYS+cur]) * multiplier;
  173. cpu_info->cpu_times.idle = (uint64_t)(cp_times[CP_IDLE+cur]) * multiplier;
  174. cpu_info->cpu_times.irq = (uint64_t)(cp_times[CP_INTR+cur]) * multiplier;
  175. cpu_info->model = uv__strdup(model);
  176. cpu_info->speed = (int)(cpuspeed/(uint64_t) 1e6);
  177. cur += CPUSTATES;
  178. }
  179. uv__free(cp_times);
  180. return 0;
  181. }
  182. int uv__random_sysctl(void* buf, size_t len) {
  183. static int name[] = {CTL_KERN, KERN_ARND};
  184. size_t count, req;
  185. unsigned char* p;
  186. p = buf;
  187. while (len) {
  188. req = len < 32 ? len : 32;
  189. count = req;
  190. if (sysctl(name, ARRAY_SIZE(name), p, &count, NULL, 0) == -1)
  191. return UV__ERR(errno);
  192. if (count != req)
  193. return UV_EIO; /* Can't happen. */
  194. p += count;
  195. len -= count;
  196. }
  197. return 0;
  198. }