1*0e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2*0e552da7Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
3*0e552da7Schristos * of this software and associated documentation files (the "Software"), to
4*0e552da7Schristos * deal in the Software without restriction, including without limitation the
5*0e552da7Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6*0e552da7Schristos * sell copies of the Software, and to permit persons to whom the Software is
7*0e552da7Schristos * furnished to do so, subject to the following conditions:
8*0e552da7Schristos *
9*0e552da7Schristos * The above copyright notice and this permission notice shall be included in
10*0e552da7Schristos * all copies or substantial portions of the Software.
11*0e552da7Schristos *
12*0e552da7Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13*0e552da7Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14*0e552da7Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15*0e552da7Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16*0e552da7Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17*0e552da7Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18*0e552da7Schristos * IN THE SOFTWARE.
19*0e552da7Schristos */
20*0e552da7Schristos
21*0e552da7Schristos #include "uv.h"
22*0e552da7Schristos #include "internal.h"
23*0e552da7Schristos
24*0e552da7Schristos #include <assert.h>
25*0e552da7Schristos #include <string.h>
26*0e552da7Schristos #include <errno.h>
27*0e552da7Schristos
28*0e552da7Schristos #include <kvm.h>
29*0e552da7Schristos #include <paths.h>
30*0e552da7Schristos #include <unistd.h>
31*0e552da7Schristos #include <time.h>
32*0e552da7Schristos #include <stdlib.h>
33*0e552da7Schristos #include <fcntl.h>
34*0e552da7Schristos
35*0e552da7Schristos #include <sys/resource.h>
36*0e552da7Schristos #include <sys/types.h>
37*0e552da7Schristos #include <sys/sysctl.h>
38*0e552da7Schristos #include <uvm/uvm_extern.h>
39*0e552da7Schristos
40*0e552da7Schristos #include <unistd.h>
41*0e552da7Schristos #include <time.h>
42*0e552da7Schristos
43*0e552da7Schristos
uv__platform_loop_init(uv_loop_t * loop)44*0e552da7Schristos int uv__platform_loop_init(uv_loop_t* loop) {
45*0e552da7Schristos return uv__kqueue_init(loop);
46*0e552da7Schristos }
47*0e552da7Schristos
48*0e552da7Schristos
uv__platform_loop_delete(uv_loop_t * loop)49*0e552da7Schristos void uv__platform_loop_delete(uv_loop_t* loop) {
50*0e552da7Schristos }
51*0e552da7Schristos
52*0e552da7Schristos
uv_loadavg(double avg[3])53*0e552da7Schristos void uv_loadavg(double avg[3]) {
54*0e552da7Schristos struct loadavg info;
55*0e552da7Schristos size_t size = sizeof(info);
56*0e552da7Schristos int which[] = {CTL_VM, VM_LOADAVG};
57*0e552da7Schristos
58*0e552da7Schristos if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) == -1) return;
59*0e552da7Schristos
60*0e552da7Schristos avg[0] = (double) info.ldavg[0] / info.fscale;
61*0e552da7Schristos avg[1] = (double) info.ldavg[1] / info.fscale;
62*0e552da7Schristos avg[2] = (double) info.ldavg[2] / info.fscale;
63*0e552da7Schristos }
64*0e552da7Schristos
65*0e552da7Schristos
uv_exepath(char * buffer,size_t * size)66*0e552da7Schristos int uv_exepath(char* buffer, size_t* size) {
67*0e552da7Schristos /* Intermediate buffer, retrieving partial path name does not work
68*0e552da7Schristos * As of NetBSD-8(beta), vnode->path translator does not handle files
69*0e552da7Schristos * with longer names than 31 characters.
70*0e552da7Schristos */
71*0e552da7Schristos char int_buf[PATH_MAX];
72*0e552da7Schristos size_t int_size;
73*0e552da7Schristos int mib[4];
74*0e552da7Schristos
75*0e552da7Schristos if (buffer == NULL || size == NULL || *size == 0)
76*0e552da7Schristos return UV_EINVAL;
77*0e552da7Schristos
78*0e552da7Schristos mib[0] = CTL_KERN;
79*0e552da7Schristos mib[1] = KERN_PROC_ARGS;
80*0e552da7Schristos mib[2] = -1;
81*0e552da7Schristos mib[3] = KERN_PROC_PATHNAME;
82*0e552da7Schristos int_size = ARRAY_SIZE(int_buf);
83*0e552da7Schristos
84*0e552da7Schristos if (sysctl(mib, 4, int_buf, &int_size, NULL, 0))
85*0e552da7Schristos return UV__ERR(errno);
86*0e552da7Schristos
87*0e552da7Schristos /* Copy string from the intermediate buffer to outer one with appropriate
88*0e552da7Schristos * length.
89*0e552da7Schristos */
90*0e552da7Schristos /* TODO(bnoordhuis) Check uv__strscpy() return value. */
91*0e552da7Schristos uv__strscpy(buffer, int_buf, *size);
92*0e552da7Schristos
93*0e552da7Schristos /* Set new size. */
94*0e552da7Schristos *size = strlen(buffer);
95*0e552da7Schristos
96*0e552da7Schristos return 0;
97*0e552da7Schristos }
98*0e552da7Schristos
99*0e552da7Schristos
uv_get_free_memory(void)100*0e552da7Schristos uint64_t uv_get_free_memory(void) {
101*0e552da7Schristos struct uvmexp info;
102*0e552da7Schristos size_t size = sizeof(info);
103*0e552da7Schristos int which[] = {CTL_VM, VM_UVMEXP};
104*0e552da7Schristos
105*0e552da7Schristos if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
106*0e552da7Schristos return UV__ERR(errno);
107*0e552da7Schristos
108*0e552da7Schristos return (uint64_t) info.free * sysconf(_SC_PAGESIZE);
109*0e552da7Schristos }
110*0e552da7Schristos
111*0e552da7Schristos
uv_get_total_memory(void)112*0e552da7Schristos uint64_t uv_get_total_memory(void) {
113*0e552da7Schristos #if defined(HW_PHYSMEM64)
114*0e552da7Schristos uint64_t info;
115*0e552da7Schristos int which[] = {CTL_HW, HW_PHYSMEM64};
116*0e552da7Schristos #else
117*0e552da7Schristos unsigned int info;
118*0e552da7Schristos int which[] = {CTL_HW, HW_PHYSMEM};
119*0e552da7Schristos #endif
120*0e552da7Schristos size_t size = sizeof(info);
121*0e552da7Schristos
122*0e552da7Schristos if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
123*0e552da7Schristos return UV__ERR(errno);
124*0e552da7Schristos
125*0e552da7Schristos return (uint64_t) info;
126*0e552da7Schristos }
127*0e552da7Schristos
128*0e552da7Schristos
uv_get_constrained_memory(void)129*0e552da7Schristos uint64_t uv_get_constrained_memory(void) {
130*0e552da7Schristos return 0; /* Memory constraints are unknown. */
131*0e552da7Schristos }
132*0e552da7Schristos
133*0e552da7Schristos
uv_resident_set_memory(size_t * rss)134*0e552da7Schristos int uv_resident_set_memory(size_t* rss) {
135*0e552da7Schristos kvm_t *kd = NULL;
136*0e552da7Schristos struct kinfo_proc2 *kinfo = NULL;
137*0e552da7Schristos pid_t pid;
138*0e552da7Schristos int nprocs;
139*0e552da7Schristos int max_size = sizeof(struct kinfo_proc2);
140*0e552da7Schristos int page_size;
141*0e552da7Schristos
142*0e552da7Schristos page_size = getpagesize();
143*0e552da7Schristos pid = getpid();
144*0e552da7Schristos
145*0e552da7Schristos kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
146*0e552da7Schristos
147*0e552da7Schristos if (kd == NULL) goto error;
148*0e552da7Schristos
149*0e552da7Schristos kinfo = kvm_getproc2(kd, KERN_PROC_PID, pid, max_size, &nprocs);
150*0e552da7Schristos if (kinfo == NULL) goto error;
151*0e552da7Schristos
152*0e552da7Schristos *rss = kinfo->p_vm_rssize * page_size;
153*0e552da7Schristos
154*0e552da7Schristos kvm_close(kd);
155*0e552da7Schristos
156*0e552da7Schristos return 0;
157*0e552da7Schristos
158*0e552da7Schristos error:
159*0e552da7Schristos if (kd) kvm_close(kd);
160*0e552da7Schristos return UV_EPERM;
161*0e552da7Schristos }
162*0e552da7Schristos
163*0e552da7Schristos
uv_uptime(double * uptime)164*0e552da7Schristos int uv_uptime(double* uptime) {
165*0e552da7Schristos time_t now;
166*0e552da7Schristos struct timeval info;
167*0e552da7Schristos size_t size = sizeof(info);
168*0e552da7Schristos static int which[] = {CTL_KERN, KERN_BOOTTIME};
169*0e552da7Schristos
170*0e552da7Schristos if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
171*0e552da7Schristos return UV__ERR(errno);
172*0e552da7Schristos
173*0e552da7Schristos now = time(NULL);
174*0e552da7Schristos
175*0e552da7Schristos *uptime = (double)(now - info.tv_sec);
176*0e552da7Schristos return 0;
177*0e552da7Schristos }
178*0e552da7Schristos
179*0e552da7Schristos
uv_cpu_info(uv_cpu_info_t ** cpu_infos,int * count)180*0e552da7Schristos int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
181*0e552da7Schristos unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK);
182*0e552da7Schristos unsigned int multiplier = ((uint64_t)1000L / ticks);
183*0e552da7Schristos unsigned int cur = 0;
184*0e552da7Schristos uv_cpu_info_t* cpu_info;
185*0e552da7Schristos u_int64_t* cp_times;
186*0e552da7Schristos char model[512];
187*0e552da7Schristos u_int64_t cpuspeed;
188*0e552da7Schristos int numcpus;
189*0e552da7Schristos size_t size;
190*0e552da7Schristos int i;
191*0e552da7Schristos
192*0e552da7Schristos size = sizeof(model);
193*0e552da7Schristos if (sysctlbyname("machdep.cpu_brand", &model, &size, NULL, 0) &&
194*0e552da7Schristos sysctlbyname("hw.model", &model, &size, NULL, 0)) {
195*0e552da7Schristos return UV__ERR(errno);
196*0e552da7Schristos }
197*0e552da7Schristos
198*0e552da7Schristos size = sizeof(numcpus);
199*0e552da7Schristos if (sysctlbyname("hw.ncpu", &numcpus, &size, NULL, 0))
200*0e552da7Schristos return UV__ERR(errno);
201*0e552da7Schristos *count = numcpus;
202*0e552da7Schristos
203*0e552da7Schristos /* Only i386 and amd64 have machdep.tsc_freq */
204*0e552da7Schristos size = sizeof(cpuspeed);
205*0e552da7Schristos if (sysctlbyname("machdep.tsc_freq", &cpuspeed, &size, NULL, 0))
206*0e552da7Schristos cpuspeed = 0;
207*0e552da7Schristos
208*0e552da7Schristos size = numcpus * CPUSTATES * sizeof(*cp_times);
209*0e552da7Schristos cp_times = uv__malloc(size);
210*0e552da7Schristos if (cp_times == NULL)
211*0e552da7Schristos return UV_ENOMEM;
212*0e552da7Schristos
213*0e552da7Schristos if (sysctlbyname("kern.cp_time", cp_times, &size, NULL, 0))
214*0e552da7Schristos return UV__ERR(errno);
215*0e552da7Schristos
216*0e552da7Schristos *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
217*0e552da7Schristos if (!(*cpu_infos)) {
218*0e552da7Schristos uv__free(cp_times);
219*0e552da7Schristos uv__free(*cpu_infos);
220*0e552da7Schristos return UV_ENOMEM;
221*0e552da7Schristos }
222*0e552da7Schristos
223*0e552da7Schristos for (i = 0; i < numcpus; i++) {
224*0e552da7Schristos cpu_info = &(*cpu_infos)[i];
225*0e552da7Schristos cpu_info->cpu_times.user = (uint64_t)(cp_times[CP_USER+cur]) * multiplier;
226*0e552da7Schristos cpu_info->cpu_times.nice = (uint64_t)(cp_times[CP_NICE+cur]) * multiplier;
227*0e552da7Schristos cpu_info->cpu_times.sys = (uint64_t)(cp_times[CP_SYS+cur]) * multiplier;
228*0e552da7Schristos cpu_info->cpu_times.idle = (uint64_t)(cp_times[CP_IDLE+cur]) * multiplier;
229*0e552da7Schristos cpu_info->cpu_times.irq = (uint64_t)(cp_times[CP_INTR+cur]) * multiplier;
230*0e552da7Schristos cpu_info->model = uv__strdup(model);
231*0e552da7Schristos cpu_info->speed = (int)(cpuspeed/(uint64_t) 1e6);
232*0e552da7Schristos cur += CPUSTATES;
233*0e552da7Schristos }
234*0e552da7Schristos uv__free(cp_times);
235*0e552da7Schristos return 0;
236*0e552da7Schristos }
237*0e552da7Schristos
uv__random_sysctl(void * buf,size_t len)238*0e552da7Schristos int uv__random_sysctl(void* buf, size_t len) {
239*0e552da7Schristos static int name[] = {CTL_KERN, KERN_ARND};
240*0e552da7Schristos size_t count, req;
241*0e552da7Schristos unsigned char* p;
242*0e552da7Schristos
243*0e552da7Schristos p = buf;
244*0e552da7Schristos while (len) {
245*0e552da7Schristos req = len < 32 ? len : 32;
246*0e552da7Schristos count = req;
247*0e552da7Schristos
248*0e552da7Schristos if (sysctl(name, ARRAY_SIZE(name), p, &count, NULL, 0) == -1)
249*0e552da7Schristos return UV__ERR(errno);
250*0e552da7Schristos
251*0e552da7Schristos if (count != req)
252*0e552da7Schristos return UV_EIO; /* Can't happen. */
253*0e552da7Schristos
254*0e552da7Schristos p += count;
255*0e552da7Schristos len -= count;
256*0e552da7Schristos }
257*0e552da7Schristos
258*0e552da7Schristos return 0;
259*0e552da7Schristos }
260