• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/types.h>
25 #include <sys/param.h>
26 #include <sys/resource.h>
27 #include <sys/sched.h>
28 #include <sys/time.h>
29 #include <sys/sysctl.h>
30 
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <paths.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 
uv__platform_loop_init(uv_loop_t * loop)39 int uv__platform_loop_init(uv_loop_t* loop) {
40   return uv__kqueue_init(loop);
41 }
42 
43 
uv__platform_loop_delete(uv_loop_t * loop)44 void uv__platform_loop_delete(uv_loop_t* loop) {
45 }
46 
47 
uv_loadavg(double avg[3])48 void uv_loadavg(double avg[3]) {
49   struct loadavg info;
50   size_t size = sizeof(info);
51   int which[] = {CTL_VM, VM_LOADAVG};
52 
53   if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) < 0) return;
54 
55   avg[0] = (double) info.ldavg[0] / info.fscale;
56   avg[1] = (double) info.ldavg[1] / info.fscale;
57   avg[2] = (double) info.ldavg[2] / info.fscale;
58 }
59 
60 
uv_exepath(char * buffer,size_t * size)61 int uv_exepath(char* buffer, size_t* size) {
62   int mib[4];
63   char **argsbuf = NULL;
64   char **argsbuf_tmp;
65   size_t argsbuf_size = 100U;
66   size_t exepath_size;
67   pid_t mypid;
68   int err;
69 
70   if (buffer == NULL || size == NULL || *size == 0)
71     return UV_EINVAL;
72 
73   mypid = getpid();
74   for (;;) {
75     err = UV_ENOMEM;
76     argsbuf_tmp = uv__realloc(argsbuf, argsbuf_size);
77     if (argsbuf_tmp == NULL)
78       goto out;
79     argsbuf = argsbuf_tmp;
80     mib[0] = CTL_KERN;
81     mib[1] = KERN_PROC_ARGS;
82     mib[2] = mypid;
83     mib[3] = KERN_PROC_ARGV;
84     if (sysctl(mib, ARRAY_SIZE(mib), argsbuf, &argsbuf_size, NULL, 0) == 0) {
85       break;
86     }
87     if (errno != ENOMEM) {
88       err = UV__ERR(errno);
89       goto out;
90     }
91     argsbuf_size *= 2U;
92   }
93 
94   if (argsbuf[0] == NULL) {
95     err = UV_EINVAL;  /* FIXME(bnoordhuis) More appropriate error. */
96     goto out;
97   }
98 
99   *size -= 1;
100   exepath_size = strlen(argsbuf[0]);
101   if (*size > exepath_size)
102     *size = exepath_size;
103 
104   memcpy(buffer, argsbuf[0], *size);
105   buffer[*size] = '\0';
106   err = 0;
107 
108 out:
109   uv__free(argsbuf);
110 
111   return err;
112 }
113 
114 
uv_get_free_memory(void)115 uint64_t uv_get_free_memory(void) {
116   struct uvmexp info;
117   size_t size = sizeof(info);
118   int which[] = {CTL_VM, VM_UVMEXP};
119 
120   if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
121     return UV__ERR(errno);
122 
123   return (uint64_t) info.free * sysconf(_SC_PAGESIZE);
124 }
125 
126 
uv_get_total_memory(void)127 uint64_t uv_get_total_memory(void) {
128   uint64_t info;
129   int which[] = {CTL_HW, HW_PHYSMEM64};
130   size_t size = sizeof(info);
131 
132   if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
133     return UV__ERR(errno);
134 
135   return (uint64_t) info;
136 }
137 
138 
uv_get_constrained_memory(void)139 uint64_t uv_get_constrained_memory(void) {
140   return 0;  /* Memory constraints are unknown. */
141 }
142 
143 
uv_resident_set_memory(size_t * rss)144 int uv_resident_set_memory(size_t* rss) {
145   struct kinfo_proc kinfo;
146   size_t page_size = getpagesize();
147   size_t size = sizeof(struct kinfo_proc);
148   int mib[6];
149 
150   mib[0] = CTL_KERN;
151   mib[1] = KERN_PROC;
152   mib[2] = KERN_PROC_PID;
153   mib[3] = getpid();
154   mib[4] = sizeof(struct kinfo_proc);
155   mib[5] = 1;
156 
157   if (sysctl(mib, ARRAY_SIZE(mib), &kinfo, &size, NULL, 0) < 0)
158     return UV__ERR(errno);
159 
160   *rss = kinfo.p_vm_rssize * page_size;
161   return 0;
162 }
163 
164 
uv_uptime(double * uptime)165 int uv_uptime(double* uptime) {
166   time_t now;
167   struct timeval info;
168   size_t size = sizeof(info);
169   static int which[] = {CTL_KERN, KERN_BOOTTIME};
170 
171   if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
172     return UV__ERR(errno);
173 
174   now = time(NULL);
175 
176   *uptime = (double)(now - info.tv_sec);
177   return 0;
178 }
179 
180 
uv_cpu_info(uv_cpu_info_t ** cpu_infos,int * count)181 int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
182   unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
183                multiplier = ((uint64_t)1000L / ticks), cpuspeed;
184   uint64_t info[CPUSTATES];
185   char model[512];
186   int numcpus = 1;
187   int which[] = {CTL_HW,HW_MODEL};
188   int percpu[] = {CTL_KERN,KERN_CPTIME2,0};
189   size_t size;
190   int i, j;
191   uv_cpu_info_t* cpu_info;
192 
193   size = sizeof(model);
194   if (sysctl(which, ARRAY_SIZE(which), &model, &size, NULL, 0))
195     return UV__ERR(errno);
196 
197   which[1] = HW_NCPUONLINE;
198   size = sizeof(numcpus);
199   if (sysctl(which, ARRAY_SIZE(which), &numcpus, &size, NULL, 0))
200     return UV__ERR(errno);
201 
202   *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
203   if (!(*cpu_infos))
204     return UV_ENOMEM;
205 
206   i = 0;
207   *count = numcpus;
208 
209   which[1] = HW_CPUSPEED;
210   size = sizeof(cpuspeed);
211   if (sysctl(which, ARRAY_SIZE(which), &cpuspeed, &size, NULL, 0))
212     goto error;
213 
214   size = sizeof(info);
215   for (i = 0; i < numcpus; i++) {
216     percpu[2] = i;
217     if (sysctl(percpu, ARRAY_SIZE(percpu), &info, &size, NULL, 0))
218       goto error;
219 
220     cpu_info = &(*cpu_infos)[i];
221 
222     cpu_info->cpu_times.user = (uint64_t)(info[CP_USER]) * multiplier;
223     cpu_info->cpu_times.nice = (uint64_t)(info[CP_NICE]) * multiplier;
224     cpu_info->cpu_times.sys = (uint64_t)(info[CP_SYS]) * multiplier;
225     cpu_info->cpu_times.idle = (uint64_t)(info[CP_IDLE]) * multiplier;
226     cpu_info->cpu_times.irq = (uint64_t)(info[CP_INTR]) * multiplier;
227 
228     cpu_info->model = uv__strdup(model);
229     cpu_info->speed = cpuspeed;
230   }
231 
232   return 0;
233 
234 error:
235   *count = 0;
236   for (j = 0; j < i; j++)
237     uv__free((*cpu_infos)[j].model);
238 
239   uv__free(*cpu_infos);
240   *cpu_infos = NULL;
241   return UV__ERR(errno);
242 }
243