xref: /libuv/src/unix/freebsd.c (revision 8ddffeee)
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 <string.h>
26 #include <errno.h>
27 
28 #include <paths.h>
29 #include <sys/user.h>
30 #include <sys/types.h>
31 #include <sys/resource.h>
32 #include <sys/sysctl.h>
33 #include <vm/vm_param.h> /* VM_LOADAVG */
34 #include <time.h>
35 #include <stdlib.h>
36 #include <unistd.h> /* sysconf */
37 #include <fcntl.h>
38 
39 #ifndef CPUSTATES
40 # define CPUSTATES 5U
41 #endif
42 #ifndef CP_USER
43 # define CP_USER 0
44 # define CP_NICE 1
45 # define CP_SYS 2
46 # define CP_IDLE 3
47 # define CP_INTR 4
48 #endif
49 
50 
uv__platform_loop_init(uv_loop_t * loop)51 int uv__platform_loop_init(uv_loop_t* loop) {
52   return uv__kqueue_init(loop);
53 }
54 
55 
uv__platform_loop_delete(uv_loop_t * loop)56 void uv__platform_loop_delete(uv_loop_t* loop) {
57 }
58 
uv_exepath(char * buffer,size_t * size)59 int uv_exepath(char* buffer, size_t* size) {
60   char abspath[PATH_MAX * 2 + 1];
61   int mib[4];
62   size_t abspath_size;
63 
64   if (buffer == NULL || size == NULL || *size == 0)
65     return UV_EINVAL;
66 
67   mib[0] = CTL_KERN;
68   mib[1] = KERN_PROC;
69   mib[2] = KERN_PROC_PATHNAME;
70   mib[3] = -1;
71 
72   abspath_size = sizeof abspath;
73   if (sysctl(mib, ARRAY_SIZE(mib), abspath, &abspath_size, NULL, 0))
74     return UV__ERR(errno);
75 
76   assert(abspath_size > 0);
77   abspath_size -= 1;
78   *size -= 1;
79 
80   if (*size > abspath_size)
81     *size = abspath_size;
82 
83   memcpy(buffer, abspath, *size);
84   buffer[*size] = '\0';
85 
86   return 0;
87 }
88 
uv_get_free_memory(void)89 uint64_t uv_get_free_memory(void) {
90   int freecount;
91   size_t size = sizeof(freecount);
92 
93   if (sysctlbyname("vm.stats.vm.v_free_count", &freecount, &size, NULL, 0))
94     return 0;
95 
96   return (uint64_t) freecount * sysconf(_SC_PAGESIZE);
97 
98 }
99 
100 
uv_get_total_memory(void)101 uint64_t uv_get_total_memory(void) {
102   unsigned long info;
103   int which[] = {CTL_HW, HW_PHYSMEM};
104 
105   size_t size = sizeof(info);
106 
107   if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
108     return 0;
109 
110   return (uint64_t) info;
111 }
112 
113 
uv_get_constrained_memory(void)114 uint64_t uv_get_constrained_memory(void) {
115   return 0;  /* Memory constraints are unknown. */
116 }
117 
118 
uv_get_available_memory(void)119 uint64_t uv_get_available_memory(void) {
120   return uv_get_free_memory();
121 }
122 
123 
uv_loadavg(double avg[3])124 void uv_loadavg(double avg[3]) {
125   struct loadavg info;
126   size_t size = sizeof(info);
127   int which[] = {CTL_VM, VM_LOADAVG};
128 
129   if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) < 0) return;
130 
131   avg[0] = (double) info.ldavg[0] / info.fscale;
132   avg[1] = (double) info.ldavg[1] / info.fscale;
133   avg[2] = (double) info.ldavg[2] / info.fscale;
134 }
135 
136 
uv_resident_set_memory(size_t * rss)137 int uv_resident_set_memory(size_t* rss) {
138   struct kinfo_proc kinfo;
139   size_t page_size;
140   size_t kinfo_size;
141   int mib[4];
142 
143   mib[0] = CTL_KERN;
144   mib[1] = KERN_PROC;
145   mib[2] = KERN_PROC_PID;
146   mib[3] = getpid();
147 
148   kinfo_size = sizeof(kinfo);
149 
150   if (sysctl(mib, ARRAY_SIZE(mib), &kinfo, &kinfo_size, NULL, 0))
151     return UV__ERR(errno);
152 
153   page_size = getpagesize();
154 
155 #ifdef __DragonFly__
156   *rss = kinfo.kp_vm_rssize * page_size;
157 #else
158   *rss = kinfo.ki_rssize * page_size;
159 #endif
160 
161   return 0;
162 }
163 
164 
uv_uptime(double * uptime)165 int uv_uptime(double* uptime) {
166   int r;
167   struct timespec sp;
168   r = clock_gettime(CLOCK_MONOTONIC, &sp);
169   if (r)
170     return UV__ERR(errno);
171 
172   *uptime = sp.tv_sec;
173   return 0;
174 }
175 
176 
uv_cpu_info(uv_cpu_info_t ** cpu_infos,int * count)177 int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
178   unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
179                multiplier = ((uint64_t)1000L / ticks), cpuspeed, maxcpus,
180                cur = 0;
181   uv_cpu_info_t* cpu_info;
182   const char* maxcpus_key;
183   const char* cptimes_key;
184   const char* model_key;
185   char model[512];
186   long* cp_times;
187   int numcpus;
188   size_t size;
189   int i;
190 
191 #if defined(__DragonFly__)
192   /* This is not quite correct but DragonFlyBSD doesn't seem to have anything
193    * comparable to kern.smp.maxcpus or kern.cp_times (kern.cp_time is a total,
194    * not per CPU). At least this stops uv_cpu_info() from failing completely.
195    */
196   maxcpus_key = "hw.ncpu";
197   cptimes_key = "kern.cp_time";
198 #else
199   maxcpus_key = "kern.smp.maxcpus";
200   cptimes_key = "kern.cp_times";
201 #endif
202 
203 #if defined(__arm__) || defined(__aarch64__)
204   /* The key hw.model and hw.clockrate are not available on FreeBSD ARM. */
205   model_key = "hw.machine";
206   cpuspeed = 0;
207 #else
208   model_key = "hw.model";
209 
210   size = sizeof(cpuspeed);
211   if (sysctlbyname("hw.clockrate", &cpuspeed, &size, NULL, 0))
212     return -errno;
213 #endif
214 
215   size = sizeof(model);
216   if (sysctlbyname(model_key, &model, &size, NULL, 0))
217     return UV__ERR(errno);
218 
219   size = sizeof(numcpus);
220   if (sysctlbyname("hw.ncpu", &numcpus, &size, NULL, 0))
221     return UV__ERR(errno);
222 
223   *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
224   if (!(*cpu_infos))
225     return UV_ENOMEM;
226 
227   *count = numcpus;
228 
229   /* kern.cp_times on FreeBSD i386 gives an array up to maxcpus instead of
230    * ncpu.
231    */
232   size = sizeof(maxcpus);
233   if (sysctlbyname(maxcpus_key, &maxcpus, &size, NULL, 0)) {
234     uv__free(*cpu_infos);
235     return UV__ERR(errno);
236   }
237 
238   size = maxcpus * CPUSTATES * sizeof(long);
239 
240   cp_times = uv__malloc(size);
241   if (cp_times == NULL) {
242     uv__free(*cpu_infos);
243     return UV_ENOMEM;
244   }
245 
246   if (sysctlbyname(cptimes_key, cp_times, &size, NULL, 0)) {
247     uv__free(cp_times);
248     uv__free(*cpu_infos);
249     return UV__ERR(errno);
250   }
251 
252   for (i = 0; i < numcpus; i++) {
253     cpu_info = &(*cpu_infos)[i];
254 
255     cpu_info->cpu_times.user = (uint64_t)(cp_times[CP_USER+cur]) * multiplier;
256     cpu_info->cpu_times.nice = (uint64_t)(cp_times[CP_NICE+cur]) * multiplier;
257     cpu_info->cpu_times.sys = (uint64_t)(cp_times[CP_SYS+cur]) * multiplier;
258     cpu_info->cpu_times.idle = (uint64_t)(cp_times[CP_IDLE+cur]) * multiplier;
259     cpu_info->cpu_times.irq = (uint64_t)(cp_times[CP_INTR+cur]) * multiplier;
260 
261     cpu_info->model = uv__strdup(model);
262     cpu_info->speed = cpuspeed;
263 
264     cur+=CPUSTATES;
265   }
266 
267   uv__free(cp_times);
268   return 0;
269 }
270 
271 
272 ssize_t
uv__fs_copy_file_range(int fd_in,off_t * off_in,int fd_out,off_t * off_out,size_t len,unsigned int flags)273 uv__fs_copy_file_range(int fd_in,
274                        off_t* off_in,
275                        int fd_out,
276                        off_t* off_out,
277                        size_t len,
278                        unsigned int flags)
279 {
280 #if __FreeBSD__ >= 13 && !defined(__DragonFly__)
281 	return copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
282 #else
283 	return errno = ENOSYS, -1;
284 #endif
285 }
286