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