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:
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
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
30 #include <sys/types.h>
31 #include <sys/resource.h>
32 #include <sys/sysctl.h>
33 #include <vm/vm_param.h> /* VM_LOADAVG */
36 #include <unistd.h> /* sysconf */
51 int uv__platform_loop_init(uv_loop_t* loop) {
52 return uv__kqueue_init(loop);
56 void uv__platform_loop_delete(uv_loop_t* loop) {
59 int uv_exepath(char* buffer, size_t* size) {
60 char abspath[PATH_MAX * 2 + 1];
64 if (buffer == NULL || size == NULL || *size == 0)
69 mib[2] = KERN_PROC_PATHNAME;
72 abspath_size = sizeof abspath;
73 if (sysctl(mib, ARRAY_SIZE(mib), abspath, &abspath_size, NULL, 0))
74 return UV__ERR(errno);
76 assert(abspath_size > 0);
80 if (*size > abspath_size)
83 memcpy(buffer, abspath, *size);
89 uint64_t uv_get_free_memory(void) {
91 size_t size = sizeof(freecount);
93 if (sysctlbyname("vm.stats.vm.v_free_count", &freecount, &size, NULL, 0))
94 return UV__ERR(errno);
96 return (uint64_t) freecount * sysconf(_SC_PAGESIZE);
101 uint64_t uv_get_total_memory(void) {
103 int which[] = {CTL_HW, HW_PHYSMEM};
105 size_t size = sizeof(info);
107 if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
108 return UV__ERR(errno);
110 return (uint64_t) info;
114 uint64_t uv_get_constrained_memory(void) {
115 return 0; /* Memory constraints are unknown. */
119 void uv_loadavg(double avg[3]) {
121 size_t size = sizeof(info);
122 int which[] = {CTL_VM, VM_LOADAVG};
124 if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) < 0) return;
126 avg[0] = (double) info.ldavg[0] / info.fscale;
127 avg[1] = (double) info.ldavg[1] / info.fscale;
128 avg[2] = (double) info.ldavg[2] / info.fscale;
132 int uv_resident_set_memory(size_t* rss) {
133 struct kinfo_proc kinfo;
140 mib[2] = KERN_PROC_PID;
143 kinfo_size = sizeof(kinfo);
145 if (sysctl(mib, ARRAY_SIZE(mib), &kinfo, &kinfo_size, NULL, 0))
146 return UV__ERR(errno);
148 page_size = getpagesize();
151 *rss = kinfo.kp_vm_rssize * page_size;
153 *rss = kinfo.ki_rssize * page_size;
160 int uv_uptime(double* uptime) {
163 r = clock_gettime(CLOCK_MONOTONIC, &sp);
165 return UV__ERR(errno);
172 int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
173 unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
174 multiplier = ((uint64_t)1000L / ticks), cpuspeed, maxcpus,
176 uv_cpu_info_t* cpu_info;
177 const char* maxcpus_key;
178 const char* cptimes_key;
179 const char* model_key;
186 #if defined(__DragonFly__)
187 /* This is not quite correct but DragonFlyBSD doesn't seem to have anything
188 * comparable to kern.smp.maxcpus or kern.cp_times (kern.cp_time is a total,
189 * not per CPU). At least this stops uv_cpu_info() from failing completely.
191 maxcpus_key = "hw.ncpu";
192 cptimes_key = "kern.cp_time";
194 maxcpus_key = "kern.smp.maxcpus";
195 cptimes_key = "kern.cp_times";
198 #if defined(__arm__) || defined(__aarch64__)
199 /* The key hw.model and hw.clockrate are not available on FreeBSD ARM. */
200 model_key = "hw.machine";
203 model_key = "hw.model";
205 size = sizeof(cpuspeed);
206 if (sysctlbyname("hw.clockrate", &cpuspeed, &size, NULL, 0))
210 size = sizeof(model);
211 if (sysctlbyname(model_key, &model, &size, NULL, 0))
212 return UV__ERR(errno);
214 size = sizeof(numcpus);
215 if (sysctlbyname("hw.ncpu", &numcpus, &size, NULL, 0))
216 return UV__ERR(errno);
218 *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
224 /* kern.cp_times on FreeBSD i386 gives an array up to maxcpus instead of
227 size = sizeof(maxcpus);
228 if (sysctlbyname(maxcpus_key, &maxcpus, &size, NULL, 0)) {
229 uv__free(*cpu_infos);
230 return UV__ERR(errno);
233 size = maxcpus * CPUSTATES * sizeof(long);
235 cp_times = uv__malloc(size);
236 if (cp_times == NULL) {
237 uv__free(*cpu_infos);
241 if (sysctlbyname(cptimes_key, cp_times, &size, NULL, 0)) {
243 uv__free(*cpu_infos);
244 return UV__ERR(errno);
247 for (i = 0; i < numcpus; i++) {
248 cpu_info = &(*cpu_infos)[i];
250 cpu_info->cpu_times.user = (uint64_t)(cp_times[CP_USER+cur]) * multiplier;
251 cpu_info->cpu_times.nice = (uint64_t)(cp_times[CP_NICE+cur]) * multiplier;
252 cpu_info->cpu_times.sys = (uint64_t)(cp_times[CP_SYS+cur]) * multiplier;
253 cpu_info->cpu_times.idle = (uint64_t)(cp_times[CP_IDLE+cur]) * multiplier;
254 cpu_info->cpu_times.irq = (uint64_t)(cp_times[CP_INTR+cur]) * multiplier;
256 cpu_info->model = uv__strdup(model);
257 cpu_info->speed = cpuspeed;
267 int uv__sendmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen) {
268 #if __FreeBSD__ >= 11 && !defined(__DragonFly__)
270 (struct mmsghdr*) mmsg,
274 return errno = ENOSYS, -1;
279 int uv__recvmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen) {
280 #if __FreeBSD__ >= 11 && !defined(__DragonFly__)
282 (struct mmsghdr*) mmsg,
287 return errno = ENOSYS, -1;
292 uv__fs_copy_file_range(int fd_in,
299 #if __FreeBSD__ >= 13 && !defined(__DragonFly__)
300 return copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
302 return errno = ENOSYS, -1;