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