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/* We lean on the fact that POLL{IN,OUT,ERR,HUP} correspond with their
22 * EPOLL* counterparts. We use the POLL* variants in this file because that
23 * is what libuv uses elsewhere.
24 */
25
26#include "uv.h"
27#include "internal.h"
28
29#include <inttypes.h>
30#include <stdint.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <assert.h>
35#include <errno.h>
36
37#include <net/if.h>
38#include <sys/epoll.h>
39#include <sys/param.h>
40#include <sys/prctl.h>
41#include <sys/sysinfo.h>
42#include <unistd.h>
43#include <fcntl.h>
44#include <time.h>
45
46#define HAVE_IFADDRS_H 1
47
48#ifdef __UCLIBC__
49# if __UCLIBC_MAJOR__ < 0 && __UCLIBC_MINOR__ < 9 && __UCLIBC_SUBLEVEL__ < 32
50# undef HAVE_IFADDRS_H
51# endif
52#endif
53
54#ifdef HAVE_IFADDRS_H
55# if defined(__ANDROID__)
56# include "uv/android-ifaddrs.h"
57# else
58# include <ifaddrs.h>
59# endif
60# include <sys/socket.h>
61# include <net/ethernet.h>
62# include <netpacket/packet.h>
63#endif /* HAVE_IFADDRS_H */
64
65/* Available from 2.6.32 onwards. */
66#ifndef CLOCK_MONOTONIC_COARSE
67# define CLOCK_MONOTONIC_COARSE 6
68#endif
69
70/* This is rather annoying: CLOCK_BOOTTIME lives in <linux/time.h> but we can't
71 * include that file because it conflicts with <time.h>. We'll just have to
72 * define it ourselves.
73 */
74#ifndef CLOCK_BOOTTIME
75# define CLOCK_BOOTTIME 7
76#endif
77
78static int read_models(unsigned int numcpus, uv_cpu_info_t* ci);
79static int read_times(FILE* statfile_fp,
80 unsigned int numcpus,
81 uv_cpu_info_t* ci);
82static void read_speeds(unsigned int numcpus, uv_cpu_info_t* ci);
83static uint64_t read_cpufreq(unsigned int cpunum);
84
85
86int uv__platform_loop_init(uv_loop_t* loop) {
87 int fd;
88
89 /* It was reported that EPOLL_CLOEXEC is not defined on Android API < 21,
90 * a.k.a. Lollipop. Since EPOLL_CLOEXEC is an alias for O_CLOEXEC on all
91 * architectures, we just use that instead.
92 */
93 fd = epoll_create1(O_CLOEXEC);
94
95 /* epoll_create1() can fail either because it's not implemented (old kernel)
96 * or because it doesn't understand the O_CLOEXEC flag.
97 */
98 if (fd == -1 && (errno == ENOSYS || errno == EINVAL)) {
99 fd = epoll_create(256);
100
101 if (fd != -1)
102 uv__cloexec(fd, 1);
103 }
104
105 loop->backend_fd = fd;
106 loop->inotify_fd = -1;
107 loop->inotify_watchers = NULL;
108
109 if (fd == -1)
110 return UV__ERR(errno);
111
112 return 0;
113}
114
115
116int uv__io_fork(uv_loop_t* loop) {
117 int err;
118 void* old_watchers;
119
120 old_watchers = loop->inotify_watchers;
121
122 uv__close(loop->backend_fd);
123 loop->backend_fd = -1;
124 uv__platform_loop_delete(loop);
125
126 err = uv__platform_loop_init(loop);
127 if (err)
128 return err;
129
130 return uv__inotify_fork(loop, old_watchers);
131}
132
133
134void uv__platform_loop_delete(uv_loop_t* loop) {
135 if (loop->inotify_fd == -1) return;
136 uv__io_stop(loop, &loop->inotify_read_watcher, POLLIN);
137 uv__close(loop->inotify_fd);
138 loop->inotify_fd = -1;
139}
140
141
142void uv__platform_invalidate_fd(uv_loop_t* loop, int fd) {
143 struct epoll_event* events;
144 struct epoll_event dummy;
145 uintptr_t i;
146 uintptr_t nfds;
147
148 assert(loop->watchers != NULL);
149 assert(fd >= 0);
150
151 events = (struct epoll_event*) loop->watchers[loop->nwatchers];
152 nfds = (uintptr_t) loop->watchers[loop->nwatchers + 1];
153 if (events != NULL)
154 /* Invalidate events with same file descriptor */
155 for (i = 0; i < nfds; i++)
156 if (events[i].data.fd == fd)
157 events[i].data.fd = -1;
158
159 /* Remove the file descriptor from the epoll.
160 * This avoids a problem where the same file description remains open
161 * in another process, causing repeated junk epoll events.
162 *
163 * We pass in a dummy epoll_event, to work around a bug in old kernels.
164 */
165 if (loop->backend_fd >= 0) {
166 /* Work around a bug in kernels 3.10 to 3.19 where passing a struct that
167 * has the EPOLLWAKEUP flag set generates spurious audit syslog warnings.
168 */
169 memset(&dummy, 0, sizeof(dummy));
170 epoll_ctl(loop->backend_fd, EPOLL_CTL_DEL, fd, &dummy);
171 }
172}
173
174
175int uv__io_check_fd(uv_loop_t* loop, int fd) {
176 struct epoll_event e;
177 int rc;
178
179 memset(&e, 0, sizeof(e));
180 e.events = POLLIN;
181 e.data.fd = -1;
182
183 rc = 0;
184 if (epoll_ctl(loop->backend_fd, EPOLL_CTL_ADD, fd, &e))
185 if (errno != EEXIST)
186 rc = UV__ERR(errno);
187
188 if (rc == 0)
189 if (epoll_ctl(loop->backend_fd, EPOLL_CTL_DEL, fd, &e))
190 abort();
191
192 return rc;
193}
194
195
196void uv__io_poll(uv_loop_t* loop, int timeout) {
197 /* A bug in kernels < 2.6.37 makes timeouts larger than ~30 minutes
198 * effectively infinite on 32 bits architectures. To avoid blocking
199 * indefinitely, we cap the timeout and poll again if necessary.
200 *
201 * Note that "30 minutes" is a simplification because it depends on
202 * the value of CONFIG_HZ. The magic constant assumes CONFIG_HZ=1200,
203 * that being the largest value I have seen in the wild (and only once.)
204 */
205 static const int max_safe_timeout = 1789569;
206 struct epoll_event events[1024];
207 struct epoll_event* pe;
208 struct epoll_event e;
209 int real_timeout;
210 QUEUE* q;
211 uv__io_t* w;
212 sigset_t sigset;
213 sigset_t* psigset;
214 uint64_t base;
215 int have_signals;
216 int nevents;
217 int count;
218 int nfds;
219 int fd;
220 int op;
221 int i;
222
223 if (loop->nfds == 0) {
224 assert(QUEUE_EMPTY(&loop->watcher_queue));
225 return;
226 }
227
228 memset(&e, 0, sizeof(e));
229
230 while (!QUEUE_EMPTY(&loop->watcher_queue)) {
231 q = QUEUE_HEAD(&loop->watcher_queue);
232 QUEUE_REMOVE(q);
233 QUEUE_INIT(q);
234
235 w = QUEUE_DATA(q, uv__io_t, watcher_queue);
236 assert(w->pevents != 0);
237 assert(w->fd >= 0);
238 assert(w->fd < (int) loop->nwatchers);
239
240 e.events = w->pevents;
241 e.data.fd = w->fd;
242
243 if (w->events == 0)
244 op = EPOLL_CTL_ADD;
245 else
246 op = EPOLL_CTL_MOD;
247
248 /* XXX Future optimization: do EPOLL_CTL_MOD lazily if we stop watching
249 * events, skip the syscall and squelch the events after epoll_wait().
250 */
251 if (epoll_ctl(loop->backend_fd, op, w->fd, &e)) {
252 if (errno != EEXIST)
253 abort();
254
255 assert(op == EPOLL_CTL_ADD);
256
257 /* We've reactivated a file descriptor that's been watched before. */
258 if (epoll_ctl(loop->backend_fd, EPOLL_CTL_MOD, w->fd, &e))
259 abort();
260 }
261
262 w->events = w->pevents;
263 }
264
265 psigset = NULL;
266 if (loop->flags & UV_LOOP_BLOCK_SIGPROF) {
267 sigemptyset(&sigset);
268 sigaddset(&sigset, SIGPROF);
269 psigset = &sigset;
270 }
271
272 assert(timeout >= -1);
273 base = loop->time;
274 count = 48; /* Benchmarks suggest this gives the best throughput. */
275 real_timeout = timeout;
276
277 for (;;) {
278 /* See the comment for max_safe_timeout for an explanation of why
279 * this is necessary. Executive summary: kernel bug workaround.
280 */
281 if (sizeof(int32_t) == sizeof(long) && timeout >= max_safe_timeout)
282 timeout = max_safe_timeout;
283
284 nfds = epoll_pwait(loop->backend_fd,
285 events,
286 ARRAY_SIZE(events),
287 timeout,
288 psigset);
289
290 /* Update loop->time unconditionally. It's tempting to skip the update when
291 * timeout == 0 (i.e. non-blocking poll) but there is no guarantee that the
292 * operating system didn't reschedule our process while in the syscall.
293 */
294 SAVE_ERRNO(uv__update_time(loop));
295
296 if (nfds == 0) {
297 assert(timeout != -1);
298
299 if (timeout == 0)
300 return;
301
302 /* We may have been inside the system call for longer than |timeout|
303 * milliseconds so we need to update the timestamp to avoid drift.
304 */
305 goto update_timeout;
306 }
307
308 if (nfds == -1) {
309 if (errno != EINTR)
310 abort();
311
312 if (timeout == -1)
313 continue;
314
315 if (timeout == 0)
316 return;
317
318 /* Interrupted by a signal. Update timeout and poll again. */
319 goto update_timeout;
320 }
321
322 have_signals = 0;
323 nevents = 0;
324
325 assert(loop->watchers != NULL);
326 loop->watchers[loop->nwatchers] = (void*) events;
327 loop->watchers[loop->nwatchers + 1] = (void*) (uintptr_t) nfds;
328 for (i = 0; i < nfds; i++) {
329 pe = events + i;
330 fd = pe->data.fd;
331
332 /* Skip invalidated events, see uv__platform_invalidate_fd */
333 if (fd == -1)
334 continue;
335
336 assert(fd >= 0);
337 assert((unsigned) fd < loop->nwatchers);
338
339 w = loop->watchers[fd];
340
341 if (w == NULL) {
342 /* File descriptor that we've stopped watching, disarm it.
343 *
344 * Ignore all errors because we may be racing with another thread
345 * when the file descriptor is closed.
346 */
347 epoll_ctl(loop->backend_fd, EPOLL_CTL_DEL, fd, pe);
348 continue;
349 }
350
351 /* Give users only events they're interested in. Prevents spurious
352 * callbacks when previous callback invocation in this loop has stopped
353 * the current watcher. Also, filters out events that users has not
354 * requested us to watch.
355 */
356 pe->events &= w->pevents | POLLERR | POLLHUP;
357
358 /* Work around an epoll quirk where it sometimes reports just the
359 * EPOLLERR or EPOLLHUP event. In order to force the event loop to
360 * move forward, we merge in the read/write events that the watcher
361 * is interested in; uv__read() and uv__write() will then deal with
362 * the error or hangup in the usual fashion.
363 *
364 * Note to self: happens when epoll reports EPOLLIN|EPOLLHUP, the user
365 * reads the available data, calls uv_read_stop(), then sometime later
366 * calls uv_read_start() again. By then, libuv has forgotten about the
367 * hangup and the kernel won't report EPOLLIN again because there's
368 * nothing left to read. If anything, libuv is to blame here. The
369 * current hack is just a quick bandaid; to properly fix it, libuv
370 * needs to remember the error/hangup event. We should get that for
371 * free when we switch over to edge-triggered I/O.
372 */
373 if (pe->events == POLLERR || pe->events == POLLHUP)
374 pe->events |=
375 w->pevents & (POLLIN | POLLOUT | UV__POLLRDHUP | UV__POLLPRI);
376
377 if (pe->events != 0) {
378 /* Run signal watchers last. This also affects child process watchers
379 * because those are implemented in terms of signal watchers.
380 */
381 if (w == &loop->signal_io_watcher)
382 have_signals = 1;
383 else
384 w->cb(loop, w, pe->events);
385
386 nevents++;
387 }
388 }
389
390 if (have_signals != 0)
391 loop->signal_io_watcher.cb(loop, &loop->signal_io_watcher, POLLIN);
392
393 loop->watchers[loop->nwatchers] = NULL;
394 loop->watchers[loop->nwatchers + 1] = NULL;
395
396 if (have_signals != 0)
397 return; /* Event loop should cycle now so don't poll again. */
398
399 if (nevents != 0) {
400 if (nfds == ARRAY_SIZE(events) && --count != 0) {
401 /* Poll for more events but don't block this time. */
402 timeout = 0;
403 continue;
404 }
405 return;
406 }
407
408 if (timeout == 0)
409 return;
410
411 if (timeout == -1)
412 continue;
413
414update_timeout:
415 assert(timeout > 0);
416
417 real_timeout -= (loop->time - base);
418 if (real_timeout <= 0)
419 return;
420
421 timeout = real_timeout;
422 }
423}
424
425
426uint64_t uv__hrtime(uv_clocktype_t type) {
427 static clock_t fast_clock_id = -1;
428 struct timespec t;
429 clock_t clock_id;
430
431 /* Prefer CLOCK_MONOTONIC_COARSE if available but only when it has
432 * millisecond granularity or better. CLOCK_MONOTONIC_COARSE is
433 * serviced entirely from the vDSO, whereas CLOCK_MONOTONIC may
434 * decide to make a costly system call.
435 */
436 /* TODO(bnoordhuis) Use CLOCK_MONOTONIC_COARSE for UV_CLOCK_PRECISE
437 * when it has microsecond granularity or better (unlikely).
438 */
439 if (type == UV_CLOCK_FAST && fast_clock_id == -1) {
440 if (clock_getres(CLOCK_MONOTONIC_COARSE, &t) == 0 &&
441 t.tv_nsec <= 1 * 1000 * 1000) {
442 fast_clock_id = CLOCK_MONOTONIC_COARSE;
443 } else {
444 fast_clock_id = CLOCK_MONOTONIC;
445 }
446 }
447
448 clock_id = CLOCK_MONOTONIC;
449 if (type == UV_CLOCK_FAST)
450 clock_id = fast_clock_id;
451
452 if (clock_gettime(clock_id, &t))
453 return 0; /* Not really possible. */
454
455 return t.tv_sec * (uint64_t) 1e9 + t.tv_nsec;
456}
457
458
459int uv_resident_set_memory(size_t* rss) {
460 char buf[1024];
461 const char* s;
462 ssize_t n;
463 long val;
464 int fd;
465 int i;
466
467 do
468 fd = open("/proc/self/stat", O_RDONLY);
469 while (fd == -1 && errno == EINTR);
470
471 if (fd == -1)
472 return UV__ERR(errno);
473
474 do
475 n = read(fd, buf, sizeof(buf) - 1);
476 while (n == -1 && errno == EINTR);
477
478 uv__close(fd);
479 if (n == -1)
480 return UV__ERR(errno);
481 buf[n] = '\0';
482
483 s = strchr(buf, ' ');
484 if (s == NULL)
485 goto err;
486
487 s += 1;
488 if (*s != '(')
489 goto err;
490
491 s = strchr(s, ')');
492 if (s == NULL)
493 goto err;
494
495 for (i = 1; i <= 22; i++) {
496 s = strchr(s + 1, ' ');
497 if (s == NULL)
498 goto err;
499 }
500
501 errno = 0;
502 val = strtol(s, NULL, 10);
503 if (errno != 0)
504 goto err;
505 if (val < 0)
506 goto err;
507
508 *rss = val * getpagesize();
509 return 0;
510
511err:
512 return UV_EINVAL;
513}
514
515
516int uv_uptime(double* uptime) {
517 static volatile int no_clock_boottime;
518 struct timespec now;
519 int r;
520
521 /* Try CLOCK_BOOTTIME first, fall back to CLOCK_MONOTONIC if not available
522 * (pre-2.6.39 kernels). CLOCK_MONOTONIC doesn't increase when the system
523 * is suspended.
524 */
525 if (no_clock_boottime) {
526 retry: r = clock_gettime(CLOCK_MONOTONIC, &now);
527 }
528 else if ((r = clock_gettime(CLOCK_BOOTTIME, &now)) && errno == EINVAL) {
529 no_clock_boottime = 1;
530 goto retry;
531 }
532
533 if (r)
534 return UV__ERR(errno);
535
536 *uptime = now.tv_sec;
537 return 0;
538}
539
540
541static int uv__cpu_num(FILE* statfile_fp, unsigned int* numcpus) {
542 unsigned int num;
543 char buf[1024];
544
545 if (!fgets(buf, sizeof(buf), statfile_fp))
546 return UV_EIO;
547
548 num = 0;
549 while (fgets(buf, sizeof(buf), statfile_fp)) {
550 if (strncmp(buf, "cpu", 3))
551 break;
552 num++;
553 }
554
555 if (num == 0)
556 return UV_EIO;
557
558 *numcpus = num;
559 return 0;
560}
561
562
563int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
564 unsigned int numcpus;
565 uv_cpu_info_t* ci;
566 int err;
567 FILE* statfile_fp;
568
569 *cpu_infos = NULL;
570 *count = 0;
571
572 statfile_fp = uv__open_file("/proc/stat");
573 if (statfile_fp == NULL)
574 return UV__ERR(errno);
575
576 err = uv__cpu_num(statfile_fp, &numcpus);
577 if (err < 0)
578 goto out;
579
580 err = UV_ENOMEM;
581 ci = uv__calloc(numcpus, sizeof(*ci));
582 if (ci == NULL)
583 goto out;
584
585 err = read_models(numcpus, ci);
586 if (err == 0)
587 err = read_times(statfile_fp, numcpus, ci);
588
589 if (err) {
590 uv_free_cpu_info(ci, numcpus);
591 goto out;
592 }
593
594 /* read_models() on x86 also reads the CPU speed from /proc/cpuinfo.
595 * We don't check for errors here. Worst case, the field is left zero.
596 */
597 if (ci[0].speed == 0)
598 read_speeds(numcpus, ci);
599
600 *cpu_infos = ci;
601 *count = numcpus;
602 err = 0;
603
604out:
605
606 if (fclose(statfile_fp))
607 if (errno != EINTR && errno != EINPROGRESS)
608 abort();
609
610 return err;
611}
612
613
614static void read_speeds(unsigned int numcpus, uv_cpu_info_t* ci) {
615 unsigned int num;
616
617 for (num = 0; num < numcpus; num++)
618 ci[num].speed = read_cpufreq(num) / 1000;
619}
620
621
622/* Also reads the CPU frequency on x86. The other architectures only have
623 * a BogoMIPS field, which may not be very accurate.
624 *
625 * Note: Simply returns on error, uv_cpu_info() takes care of the cleanup.
626 */
627static int read_models(unsigned int numcpus, uv_cpu_info_t* ci) {
628 static const char model_marker[] = "model name\t: ";
629 static const char speed_marker[] = "cpu MHz\t\t: ";
630 const char* inferred_model;
631 unsigned int model_idx;
632 unsigned int speed_idx;
633 char buf[1024];
634 char* model;
635 FILE* fp;
636
637 /* Most are unused on non-ARM, non-MIPS and non-x86 architectures. */
638 (void) &model_marker;
639 (void) &speed_marker;
640 (void) &speed_idx;
641 (void) &model;
642 (void) &buf;
643 (void) &fp;
644
645 model_idx = 0;
646 speed_idx = 0;
647
648#if defined(__arm__) || \
649 defined(__i386__) || \
650 defined(__mips__) || \
651 defined(__x86_64__)
652 fp = uv__open_file("/proc/cpuinfo");
653 if (fp == NULL)
654 return UV__ERR(errno);
655
656 while (fgets(buf, sizeof(buf), fp)) {
657 if (model_idx < numcpus) {
658 if (strncmp(buf, model_marker, sizeof(model_marker) - 1) == 0) {
659 model = buf + sizeof(model_marker) - 1;
660 model = uv__strndup(model, strlen(model) - 1); /* Strip newline. */
661 if (model == NULL) {
662 fclose(fp);
663 return UV_ENOMEM;
664 }
665 ci[model_idx++].model = model;
666 continue;
667 }
668 }
669#if defined(__arm__) || defined(__mips__)
670 if (model_idx < numcpus) {
671#if defined(__arm__)
672 /* Fallback for pre-3.8 kernels. */
673 static const char model_marker[] = "Processor\t: ";
674#else /* defined(__mips__) */
675 static const char model_marker[] = "cpu model\t\t: ";
676#endif
677 if (strncmp(buf, model_marker, sizeof(model_marker) - 1) == 0) {
678 model = buf + sizeof(model_marker) - 1;
679 model = uv__strndup(model, strlen(model) - 1); /* Strip newline. */
680 if (model == NULL) {
681 fclose(fp);
682 return UV_ENOMEM;
683 }
684 ci[model_idx++].model = model;
685 continue;
686 }
687 }
688#else /* !__arm__ && !__mips__ */
689 if (speed_idx < numcpus) {
690 if (strncmp(buf, speed_marker, sizeof(speed_marker) - 1) == 0) {
691 ci[speed_idx++].speed = atoi(buf + sizeof(speed_marker) - 1);
692 continue;
693 }
694 }
695#endif /* __arm__ || __mips__ */
696 }
697
698 fclose(fp);
699#endif /* __arm__ || __i386__ || __mips__ || __x86_64__ */
700
701 /* Now we want to make sure that all the models contain *something* because
702 * it's not safe to leave them as null. Copy the last entry unless there
703 * isn't one, in that case we simply put "unknown" into everything.
704 */
705 inferred_model = "unknown";
706 if (model_idx > 0)
707 inferred_model = ci[model_idx - 1].model;
708
709 while (model_idx < numcpus) {
710 model = uv__strndup(inferred_model, strlen(inferred_model));
711 if (model == NULL)
712 return UV_ENOMEM;
713 ci[model_idx++].model = model;
714 }
715
716 return 0;
717}
718
719
720static int read_times(FILE* statfile_fp,
721 unsigned int numcpus,
722 uv_cpu_info_t* ci) {
723 struct uv_cpu_times_s ts;
724 uint64_t clock_ticks;
725 uint64_t user;
726 uint64_t nice;
727 uint64_t sys;
728 uint64_t idle;
729 uint64_t dummy;
730 uint64_t irq;
731 uint64_t num;
732 uint64_t len;
733 char buf[1024];
734
735 clock_ticks = sysconf(_SC_CLK_TCK);
736 assert(clock_ticks != (uint64_t) -1);
737 assert(clock_ticks != 0);
738
739 rewind(statfile_fp);
740
741 if (!fgets(buf, sizeof(buf), statfile_fp))
742 abort();
743
744 num = 0;
745
746 while (fgets(buf, sizeof(buf), statfile_fp)) {
747 if (num >= numcpus)
748 break;
749
750 if (strncmp(buf, "cpu", 3))
751 break;
752
753 /* skip "cpu<num> " marker */
754 {
755 unsigned int n;
756 int r = sscanf(buf, "cpu%u ", &n);
757 assert(r == 1);
758 (void) r; /* silence build warning */
759 for (len = sizeof("cpu0"); n /= 10; len++);
760 }
761
762 /* Line contains user, nice, system, idle, iowait, irq, softirq, steal,
763 * guest, guest_nice but we're only interested in the first four + irq.
764 *
765 * Don't use %*s to skip fields or %ll to read straight into the uint64_t
766 * fields, they're not allowed in C89 mode.
767 */
768 if (6 != sscanf(buf + len,
769 "%" PRIu64 " %" PRIu64 " %" PRIu64
770 "%" PRIu64 " %" PRIu64 " %" PRIu64,
771 &user,
772 &nice,
773 &sys,
774 &idle,
775 &dummy,
776 &irq))
777 abort();
778
779 ts.user = clock_ticks * user;
780 ts.nice = clock_ticks * nice;
781 ts.sys = clock_ticks * sys;
782 ts.idle = clock_ticks * idle;
783 ts.irq = clock_ticks * irq;
784 ci[num++].cpu_times = ts;
785 }
786 assert(num == numcpus);
787
788 return 0;
789}
790
791
792static uint64_t read_cpufreq(unsigned int cpunum) {
793 uint64_t val;
794 char buf[1024];
795 FILE* fp;
796
797 snprintf(buf,
798 sizeof(buf),
799 "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq",
800 cpunum);
801
802 fp = uv__open_file(buf);
803 if (fp == NULL)
804 return 0;
805
806 if (fscanf(fp, "%" PRIu64, &val) != 1)
807 val = 0;
808
809 fclose(fp);
810
811 return val;
812}
813
814
815void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
816 int i;
817
818 for (i = 0; i < count; i++) {
819 uv__free(cpu_infos[i].model);
820 }
821
822 uv__free(cpu_infos);
823}
824
825static int uv__ifaddr_exclude(struct ifaddrs *ent, int exclude_type) {
826 if (!((ent->ifa_flags & IFF_UP) && (ent->ifa_flags & IFF_RUNNING)))
827 return 1;
828 if (ent->ifa_addr == NULL)
829 return 1;
830 /*
831 * On Linux getifaddrs returns information related to the raw underlying
832 * devices. We're not interested in this information yet.
833 */
834 if (ent->ifa_addr->sa_family == PF_PACKET)
835 return exclude_type;
836 return !exclude_type;
837}
838
839int uv_interface_addresses(uv_interface_address_t** addresses, int* count) {
840#ifndef HAVE_IFADDRS_H
841 *count = 0;
842 *addresses = NULL;
843 return UV_ENOSYS;
844#else
845 struct ifaddrs *addrs, *ent;
846 uv_interface_address_t* address;
847 int i;
848 struct sockaddr_ll *sll;
849
850 *count = 0;
851 *addresses = NULL;
852
853 if (getifaddrs(&addrs))
854 return UV__ERR(errno);
855
856 /* Count the number of interfaces */
857 for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
858 if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFADDR))
859 continue;
860
861 (*count)++;
862 }
863
864 if (*count == 0) {
865 freeifaddrs(addrs);
866 return 0;
867 }
868
869 /* Make sure the memory is initiallized to zero using calloc() */
870 *addresses = uv__calloc(*count, sizeof(**addresses));
871 if (!(*addresses)) {
872 freeifaddrs(addrs);
873 return UV_ENOMEM;
874 }
875
876 address = *addresses;
877
878 for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
879 if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFADDR))
880 continue;
881
882 address->name = uv__strdup(ent->ifa_name);
883
884 if (ent->ifa_addr->sa_family == AF_INET6) {
885 address->address.address6 = *((struct sockaddr_in6*) ent->ifa_addr);
886 } else {
887 address->address.address4 = *((struct sockaddr_in*) ent->ifa_addr);
888 }
889
890 if (ent->ifa_netmask->sa_family == AF_INET6) {
891 address->netmask.netmask6 = *((struct sockaddr_in6*) ent->ifa_netmask);
892 } else {
893 address->netmask.netmask4 = *((struct sockaddr_in*) ent->ifa_netmask);
894 }
895
896 address->is_internal = !!(ent->ifa_flags & IFF_LOOPBACK);
897
898 address++;
899 }
900
901 /* Fill in physical addresses for each interface */
902 for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
903 if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFPHYS))
904 continue;
905
906 address = *addresses;
907
908 for (i = 0; i < (*count); i++) {
909 size_t namelen = strlen(ent->ifa_name);
910 /* Alias interface share the same physical address */
911 if (strncmp(address->name, ent->ifa_name, namelen) == 0 &&
912 (address->name[namelen] == 0 || address->name[namelen] == ':')) {
913 sll = (struct sockaddr_ll*)ent->ifa_addr;
914 memcpy(address->phys_addr, sll->sll_addr, sizeof(address->phys_addr));
915 }
916 address++;
917 }
918 }
919
920 freeifaddrs(addrs);
921
922 return 0;
923#endif
924}
925
926
927void uv_free_interface_addresses(uv_interface_address_t* addresses,
928 int count) {
929 int i;
930
931 for (i = 0; i < count; i++) {
932 uv__free(addresses[i].name);
933 }
934
935 uv__free(addresses);
936}
937
938
939void uv__set_process_title(const char* title) {
940#if defined(PR_SET_NAME)
941 prctl(PR_SET_NAME, title); /* Only copies first 16 characters. */
942#endif
943}
944
945
946static uint64_t uv__read_proc_meminfo(const char* what) {
947 uint64_t rc;
948 ssize_t n;
949 char* p;
950 int fd;
951 char buf[4096]; /* Large enough to hold all of /proc/meminfo. */
952
953 rc = 0;
954 fd = uv__open_cloexec("/proc/meminfo", O_RDONLY);
955
956 if (fd == -1)
957 return 0;
958
959 n = read(fd, buf, sizeof(buf) - 1);
960
961 if (n <= 0)
962 goto out;
963
964 buf[n] = '\0';
965 p = strstr(buf, what);
966
967 if (p == NULL)
968 goto out;
969
970 p += strlen(what);
971
972 if (1 != sscanf(p, "%" PRIu64 " kB", &rc))
973 goto out;
974
975 rc *= 1024;
976
977out:
978
979 if (uv__close_nocheckstdio(fd))
980 abort();
981
982 return rc;
983}
984
985
986uint64_t uv_get_free_memory(void) {
987 struct sysinfo info;
988 uint64_t rc;
989
990 rc = uv__read_proc_meminfo("MemFree:");
991
992 if (rc != 0)
993 return rc;
994
995 if (0 == sysinfo(&info))
996 return (uint64_t) info.freeram * info.mem_unit;
997
998 return 0;
999}
1000
1001
1002uint64_t uv_get_total_memory(void) {
1003 struct sysinfo info;
1004 uint64_t rc;
1005
1006 rc = uv__read_proc_meminfo("MemTotal:");
1007
1008 if (rc != 0)
1009 return rc;
1010
1011 if (0 == sysinfo(&info))
1012 return (uint64_t) info.totalram * info.mem_unit;
1013
1014 return 0;
1015}
1016
1017
1018static uint64_t uv__read_cgroups_uint64(const char* cgroup, const char* param) {
1019 char filename[256];
1020 uint64_t rc;
1021 int fd;
1022 ssize_t n;
1023 char buf[32]; /* Large enough to hold an encoded uint64_t. */
1024
1025 snprintf(filename, 256, "/sys/fs/cgroup/%s/%s", cgroup, param);
1026
1027 rc = 0;
1028 fd = uv__open_cloexec(filename, O_RDONLY);
1029
1030 if (fd < 0)
1031 return 0;
1032
1033 n = read(fd, buf, sizeof(buf) - 1);
1034
1035 if (n > 0) {
1036 buf[n] = '\0';
1037 sscanf(buf, "%" PRIu64, &rc);
1038 }
1039
1040 if (uv__close_nocheckstdio(fd))
1041 abort();
1042
1043 return rc;
1044}
1045
1046
1047uint64_t uv_get_constrained_memory(void) {
1048 /*
1049 * This might return 0 if there was a problem getting the memory limit from
1050 * cgroups. This is OK because a return value of 0 signifies that the memory
1051 * limit is unknown.
1052 */
1053 return uv__read_cgroups_uint64("memory", "memory.limit_in_bytes");
1054}
1055