linux-core.c 27 KB

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