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
21 #include "uv.h"
22 #include "internal.h"
23
24 #include <assert.h>
25 #include <stdint.h>
26 #include <errno.h>
27
28 #include <mach/mach.h>
29 #include <mach/mach_time.h>
30 #include <mach-o/dyld.h> /* _NSGetExecutablePath */
31 #include <sys/resource.h>
32 #include <sys/sysctl.h>
33 #include <unistd.h> /* sysconf */
34
35
uv__platform_loop_init(uv_loop_t * loop)36 int uv__platform_loop_init(uv_loop_t* loop) {
37 loop->cf_state = NULL;
38
39 if (uv__kqueue_init(loop))
40 return UV__ERR(errno);
41
42 return 0;
43 }
44
45
uv__platform_loop_delete(uv_loop_t * loop)46 void uv__platform_loop_delete(uv_loop_t* loop) {
47 uv__fsevents_loop_delete(loop);
48 }
49
50
uv__hrtime(uv_clocktype_t type)51 uint64_t uv__hrtime(uv_clocktype_t type) {
52 static mach_timebase_info_data_t info;
53
54 if ((ACCESS_ONCE(uint32_t, info.numer) == 0 ||
55 ACCESS_ONCE(uint32_t, info.denom) == 0) &&
56 mach_timebase_info(&info) != KERN_SUCCESS)
57 abort();
58
59 return mach_absolute_time() * info.numer / info.denom;
60 }
61
62
uv_exepath(char * buffer,size_t * size)63 int uv_exepath(char* buffer, size_t* size) {
64 /* realpath(exepath) may be > PATH_MAX so double it to be on the safe side. */
65 char abspath[PATH_MAX * 2 + 1];
66 char exepath[PATH_MAX + 1];
67 uint32_t exepath_size;
68 size_t abspath_size;
69
70 if (buffer == NULL || size == NULL || *size == 0)
71 return UV_EINVAL;
72
73 exepath_size = sizeof(exepath);
74 if (_NSGetExecutablePath(exepath, &exepath_size))
75 return UV_EIO;
76
77 if (realpath(exepath, abspath) != abspath)
78 return UV__ERR(errno);
79
80 abspath_size = strlen(abspath);
81 if (abspath_size == 0)
82 return UV_EIO;
83
84 *size -= 1;
85 if (*size > abspath_size)
86 *size = abspath_size;
87
88 memcpy(buffer, abspath, *size);
89 buffer[*size] = '\0';
90
91 return 0;
92 }
93
94
uv_get_free_memory(void)95 uint64_t uv_get_free_memory(void) {
96 vm_statistics_data_t info;
97 mach_msg_type_number_t count = sizeof(info) / sizeof(integer_t);
98
99 if (host_statistics(mach_host_self(), HOST_VM_INFO,
100 (host_info_t)&info, &count) != KERN_SUCCESS) {
101 return UV_EINVAL; /* FIXME(bnoordhuis) Translate error. */
102 }
103
104 return (uint64_t) info.free_count * sysconf(_SC_PAGESIZE);
105 }
106
107
uv_get_total_memory(void)108 uint64_t uv_get_total_memory(void) {
109 uint64_t info;
110 int which[] = {CTL_HW, HW_MEMSIZE};
111 size_t size = sizeof(info);
112
113 if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
114 return UV__ERR(errno);
115
116 return (uint64_t) info;
117 }
118
119
uv_get_constrained_memory(void)120 uint64_t uv_get_constrained_memory(void) {
121 return 0; /* Memory constraints are unknown. */
122 }
123
124
uv_loadavg(double avg[3])125 void uv_loadavg(double avg[3]) {
126 struct loadavg info;
127 size_t size = sizeof(info);
128 int which[] = {CTL_VM, VM_LOADAVG};
129
130 if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) < 0) return;
131
132 avg[0] = (double) info.ldavg[0] / info.fscale;
133 avg[1] = (double) info.ldavg[1] / info.fscale;
134 avg[2] = (double) info.ldavg[2] / info.fscale;
135 }
136
137
uv_resident_set_memory(size_t * rss)138 int uv_resident_set_memory(size_t* rss) {
139 mach_msg_type_number_t count;
140 task_basic_info_data_t info;
141 kern_return_t err;
142
143 count = TASK_BASIC_INFO_COUNT;
144 err = task_info(mach_task_self(),
145 TASK_BASIC_INFO,
146 (task_info_t) &info,
147 &count);
148 (void) &err;
149 /* task_info(TASK_BASIC_INFO) cannot really fail. Anything other than
150 * KERN_SUCCESS implies a libuv bug.
151 */
152 assert(err == KERN_SUCCESS);
153 *rss = info.resident_size;
154
155 return 0;
156 }
157
158
uv_uptime(double * uptime)159 int uv_uptime(double* uptime) {
160 time_t now;
161 struct timeval info;
162 size_t size = sizeof(info);
163 static int which[] = {CTL_KERN, KERN_BOOTTIME};
164
165 if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
166 return UV__ERR(errno);
167
168 now = time(NULL);
169 *uptime = now - info.tv_sec;
170
171 return 0;
172 }
173
uv_cpu_info(uv_cpu_info_t ** cpu_infos,int * count)174 int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
175 unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
176 multiplier = ((uint64_t)1000L / ticks);
177 char model[512];
178 uint64_t cpuspeed;
179 size_t size;
180 unsigned int i;
181 natural_t numcpus;
182 mach_msg_type_number_t msg_type;
183 processor_cpu_load_info_data_t *info;
184 uv_cpu_info_t* cpu_info;
185
186 size = sizeof(model);
187 if (sysctlbyname("machdep.cpu.brand_string", &model, &size, NULL, 0) &&
188 sysctlbyname("hw.model", &model, &size, NULL, 0)) {
189 return UV__ERR(errno);
190 }
191
192 size = sizeof(cpuspeed);
193 if (sysctlbyname("hw.cpufrequency", &cpuspeed, &size, NULL, 0))
194 return UV__ERR(errno);
195
196 if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numcpus,
197 (processor_info_array_t*)&info,
198 &msg_type) != KERN_SUCCESS) {
199 return UV_EINVAL; /* FIXME(bnoordhuis) Translate error. */
200 }
201
202 *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
203 if (!(*cpu_infos)) {
204 vm_deallocate(mach_task_self(), (vm_address_t)info, msg_type);
205 return UV_ENOMEM;
206 }
207
208 *count = numcpus;
209
210 for (i = 0; i < numcpus; i++) {
211 cpu_info = &(*cpu_infos)[i];
212
213 cpu_info->cpu_times.user = (uint64_t)(info[i].cpu_ticks[0]) * multiplier;
214 cpu_info->cpu_times.nice = (uint64_t)(info[i].cpu_ticks[3]) * multiplier;
215 cpu_info->cpu_times.sys = (uint64_t)(info[i].cpu_ticks[1]) * multiplier;
216 cpu_info->cpu_times.idle = (uint64_t)(info[i].cpu_ticks[2]) * multiplier;
217 cpu_info->cpu_times.irq = 0;
218
219 cpu_info->model = uv__strdup(model);
220 cpu_info->speed = cpuspeed/1000000;
221 }
222 vm_deallocate(mach_task_self(), (vm_address_t)info, msg_type);
223
224 return 0;
225 }
226