linux-core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. # if defined(__ANDROID_API__) && __ANDROID_API__ < 24
  43. # undef HAVE_IFADDRS_H
  44. #endif
  45. #ifdef __UCLIBC__
  46. # if __UCLIBC_MAJOR__ < 0 && __UCLIBC_MINOR__ < 9 && __UCLIBC_SUBLEVEL__ < 32
  47. # undef HAVE_IFADDRS_H
  48. # endif
  49. #endif
  50. #ifdef HAVE_IFADDRS_H
  51. # include <ifaddrs.h>
  52. # include <sys/socket.h>
  53. # include <net/ethernet.h>
  54. # include <netpacket/packet.h>
  55. #endif /* HAVE_IFADDRS_H */
  56. /* Available from 2.6.32 onwards. */
  57. #ifndef CLOCK_MONOTONIC_COARSE
  58. # define CLOCK_MONOTONIC_COARSE 6
  59. #endif
  60. /* This is rather annoying: CLOCK_BOOTTIME lives in <linux/time.h> but we can't
  61. * include that file because it conflicts with <time.h>. We'll just have to
  62. * define it ourselves.
  63. */
  64. #ifndef CLOCK_BOOTTIME
  65. # define CLOCK_BOOTTIME 7
  66. #endif
  67. static int read_models(unsigned int numcpus, uv_cpu_info_t* ci);
  68. static int read_times(FILE* statfile_fp,
  69. unsigned int numcpus,
  70. uv_cpu_info_t* ci);
  71. static void read_speeds(unsigned int numcpus, uv_cpu_info_t* ci);
  72. static uint64_t read_cpufreq(unsigned int cpunum);
  73. int uv__platform_loop_init(uv_loop_t* loop) {
  74. loop->inotify_fd = -1;
  75. loop->inotify_watchers = NULL;
  76. return uv__epoll_init(loop);
  77. }
  78. int uv__io_fork(uv_loop_t* loop) {
  79. int err;
  80. void* old_watchers;
  81. old_watchers = loop->inotify_watchers;
  82. uv__close(loop->backend_fd);
  83. loop->backend_fd = -1;
  84. uv__platform_loop_delete(loop);
  85. err = uv__platform_loop_init(loop);
  86. if (err)
  87. return err;
  88. return uv__inotify_fork(loop, old_watchers);
  89. }
  90. void uv__platform_loop_delete(uv_loop_t* loop) {
  91. if (loop->inotify_fd == -1) return;
  92. uv__io_stop(loop, &loop->inotify_read_watcher, POLLIN);
  93. uv__close(loop->inotify_fd);
  94. loop->inotify_fd = -1;
  95. }
  96. uint64_t uv__hrtime(uv_clocktype_t type) {
  97. static clock_t fast_clock_id = -1;
  98. struct timespec t;
  99. clock_t clock_id;
  100. /* Prefer CLOCK_MONOTONIC_COARSE if available but only when it has
  101. * millisecond granularity or better. CLOCK_MONOTONIC_COARSE is
  102. * serviced entirely from the vDSO, whereas CLOCK_MONOTONIC may
  103. * decide to make a costly system call.
  104. */
  105. /* TODO(bnoordhuis) Use CLOCK_MONOTONIC_COARSE for UV_CLOCK_PRECISE
  106. * when it has microsecond granularity or better (unlikely).
  107. */
  108. clock_id = CLOCK_MONOTONIC;
  109. if (type != UV_CLOCK_FAST)
  110. goto done;
  111. clock_id = uv__load_relaxed(&fast_clock_id);
  112. if (clock_id != -1)
  113. goto done;
  114. clock_id = CLOCK_MONOTONIC;
  115. if (0 == clock_getres(CLOCK_MONOTONIC_COARSE, &t))
  116. if (t.tv_nsec <= 1 * 1000 * 1000)
  117. clock_id = CLOCK_MONOTONIC_COARSE;
  118. uv__store_relaxed(&fast_clock_id, clock_id);
  119. done:
  120. if (clock_gettime(clock_id, &t))
  121. return 0; /* Not really possible. */
  122. return t.tv_sec * (uint64_t) 1e9 + t.tv_nsec;
  123. }
  124. int uv_resident_set_memory(size_t* rss) {
  125. char buf[1024];
  126. const char* s;
  127. ssize_t n;
  128. long val;
  129. int fd;
  130. int i;
  131. do
  132. fd = open("/proc/self/stat", O_RDONLY);
  133. while (fd == -1 && errno == EINTR);
  134. if (fd == -1)
  135. return UV__ERR(errno);
  136. do
  137. n = read(fd, buf, sizeof(buf) - 1);
  138. while (n == -1 && errno == EINTR);
  139. uv__close(fd);
  140. if (n == -1)
  141. return UV__ERR(errno);
  142. buf[n] = '\0';
  143. s = strchr(buf, ' ');
  144. if (s == NULL)
  145. goto err;
  146. s += 1;
  147. if (*s != '(')
  148. goto err;
  149. s = strchr(s, ')');
  150. if (s == NULL)
  151. goto err;
  152. for (i = 1; i <= 22; i++) {
  153. s = strchr(s + 1, ' ');
  154. if (s == NULL)
  155. goto err;
  156. }
  157. errno = 0;
  158. val = strtol(s, NULL, 10);
  159. if (errno != 0)
  160. goto err;
  161. if (val < 0)
  162. goto err;
  163. *rss = val * getpagesize();
  164. return 0;
  165. err:
  166. return UV_EINVAL;
  167. }
  168. static int uv__slurp(const char* filename, char* buf, size_t len) {
  169. ssize_t n;
  170. int fd;
  171. assert(len > 0);
  172. fd = uv__open_cloexec(filename, O_RDONLY);
  173. if (fd < 0)
  174. return fd;
  175. do
  176. n = read(fd, buf, len - 1);
  177. while (n == -1 && errno == EINTR);
  178. if (uv__close_nocheckstdio(fd))
  179. abort();
  180. if (n < 0)
  181. return UV__ERR(errno);
  182. buf[n] = '\0';
  183. return 0;
  184. }
  185. int uv_uptime(double* uptime) {
  186. static volatile int no_clock_boottime;
  187. char buf[128];
  188. struct timespec now;
  189. int r;
  190. /* Try /proc/uptime first, then fallback to clock_gettime(). */
  191. if (0 == uv__slurp("/proc/uptime", buf, sizeof(buf)))
  192. if (1 == sscanf(buf, "%lf", uptime))
  193. return 0;
  194. /* Try CLOCK_BOOTTIME first, fall back to CLOCK_MONOTONIC if not available
  195. * (pre-2.6.39 kernels). CLOCK_MONOTONIC doesn't increase when the system
  196. * is suspended.
  197. */
  198. if (no_clock_boottime) {
  199. retry_clock_gettime: r = clock_gettime(CLOCK_MONOTONIC, &now);
  200. }
  201. else if ((r = clock_gettime(CLOCK_BOOTTIME, &now)) && errno == EINVAL) {
  202. no_clock_boottime = 1;
  203. goto retry_clock_gettime;
  204. }
  205. if (r)
  206. return UV__ERR(errno);
  207. *uptime = now.tv_sec;
  208. return 0;
  209. }
  210. static int uv__cpu_num(FILE* statfile_fp, unsigned int* numcpus) {
  211. unsigned int num;
  212. char buf[1024];
  213. if (!fgets(buf, sizeof(buf), statfile_fp))
  214. return UV_EIO;
  215. num = 0;
  216. while (fgets(buf, sizeof(buf), statfile_fp)) {
  217. if (strncmp(buf, "cpu", 3))
  218. break;
  219. num++;
  220. }
  221. if (num == 0)
  222. return UV_EIO;
  223. *numcpus = num;
  224. return 0;
  225. }
  226. int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
  227. unsigned int numcpus;
  228. uv_cpu_info_t* ci;
  229. int err;
  230. FILE* statfile_fp;
  231. *cpu_infos = NULL;
  232. *count = 0;
  233. statfile_fp = uv__open_file("/proc/stat");
  234. if (statfile_fp == NULL)
  235. return UV__ERR(errno);
  236. err = uv__cpu_num(statfile_fp, &numcpus);
  237. if (err < 0)
  238. goto out;
  239. err = UV_ENOMEM;
  240. ci = uv__calloc(numcpus, sizeof(*ci));
  241. if (ci == NULL)
  242. goto out;
  243. err = read_models(numcpus, ci);
  244. if (err == 0)
  245. err = read_times(statfile_fp, numcpus, ci);
  246. if (err) {
  247. uv_free_cpu_info(ci, numcpus);
  248. goto out;
  249. }
  250. /* read_models() on x86 also reads the CPU speed from /proc/cpuinfo.
  251. * We don't check for errors here. Worst case, the field is left zero.
  252. */
  253. if (ci[0].speed == 0)
  254. read_speeds(numcpus, ci);
  255. *cpu_infos = ci;
  256. *count = numcpus;
  257. err = 0;
  258. out:
  259. if (fclose(statfile_fp))
  260. if (errno != EINTR && errno != EINPROGRESS)
  261. abort();
  262. return err;
  263. }
  264. static void read_speeds(unsigned int numcpus, uv_cpu_info_t* ci) {
  265. unsigned int num;
  266. for (num = 0; num < numcpus; num++)
  267. ci[num].speed = read_cpufreq(num) / 1000;
  268. }
  269. /* Also reads the CPU frequency on ppc and x86. The other architectures only
  270. * have a BogoMIPS field, which may not be very accurate.
  271. *
  272. * Note: Simply returns on error, uv_cpu_info() takes care of the cleanup.
  273. */
  274. static int read_models(unsigned int numcpus, uv_cpu_info_t* ci) {
  275. #if defined(__PPC__)
  276. static const char model_marker[] = "cpu\t\t: ";
  277. static const char speed_marker[] = "clock\t\t: ";
  278. #else
  279. static const char model_marker[] = "model name\t: ";
  280. static const char speed_marker[] = "cpu MHz\t\t: ";
  281. #endif
  282. const char* inferred_model;
  283. unsigned int model_idx;
  284. unsigned int speed_idx;
  285. unsigned int part_idx;
  286. char buf[1024];
  287. char* model;
  288. FILE* fp;
  289. int model_id;
  290. /* Most are unused on non-ARM, non-MIPS and non-x86 architectures. */
  291. (void) &model_marker;
  292. (void) &speed_marker;
  293. (void) &speed_idx;
  294. (void) &part_idx;
  295. (void) &model;
  296. (void) &buf;
  297. (void) &fp;
  298. (void) &model_id;
  299. model_idx = 0;
  300. speed_idx = 0;
  301. part_idx = 0;
  302. #if defined(__arm__) || \
  303. defined(__i386__) || \
  304. defined(__mips__) || \
  305. defined(__aarch64__) || \
  306. defined(__PPC__) || \
  307. defined(__x86_64__)
  308. fp = uv__open_file("/proc/cpuinfo");
  309. if (fp == NULL)
  310. return UV__ERR(errno);
  311. while (fgets(buf, sizeof(buf), fp)) {
  312. if (model_idx < numcpus) {
  313. if (strncmp(buf, model_marker, sizeof(model_marker) - 1) == 0) {
  314. model = buf + sizeof(model_marker) - 1;
  315. model = uv__strndup(model, strlen(model) - 1); /* Strip newline. */
  316. if (model == NULL) {
  317. fclose(fp);
  318. return UV_ENOMEM;
  319. }
  320. ci[model_idx++].model = model;
  321. continue;
  322. }
  323. }
  324. #if defined(__arm__) || defined(__mips__) || defined(__aarch64__)
  325. if (model_idx < numcpus) {
  326. #if defined(__arm__)
  327. /* Fallback for pre-3.8 kernels. */
  328. static const char model_marker[] = "Processor\t: ";
  329. #elif defined(__aarch64__)
  330. static const char part_marker[] = "CPU part\t: ";
  331. /* Adapted from: https://github.com/karelzak/util-linux */
  332. struct vendor_part {
  333. const int id;
  334. const char* name;
  335. };
  336. static const struct vendor_part arm_chips[] = {
  337. { 0x811, "ARM810" },
  338. { 0x920, "ARM920" },
  339. { 0x922, "ARM922" },
  340. { 0x926, "ARM926" },
  341. { 0x940, "ARM940" },
  342. { 0x946, "ARM946" },
  343. { 0x966, "ARM966" },
  344. { 0xa20, "ARM1020" },
  345. { 0xa22, "ARM1022" },
  346. { 0xa26, "ARM1026" },
  347. { 0xb02, "ARM11 MPCore" },
  348. { 0xb36, "ARM1136" },
  349. { 0xb56, "ARM1156" },
  350. { 0xb76, "ARM1176" },
  351. { 0xc05, "Cortex-A5" },
  352. { 0xc07, "Cortex-A7" },
  353. { 0xc08, "Cortex-A8" },
  354. { 0xc09, "Cortex-A9" },
  355. { 0xc0d, "Cortex-A17" }, /* Originally A12 */
  356. { 0xc0f, "Cortex-A15" },
  357. { 0xc0e, "Cortex-A17" },
  358. { 0xc14, "Cortex-R4" },
  359. { 0xc15, "Cortex-R5" },
  360. { 0xc17, "Cortex-R7" },
  361. { 0xc18, "Cortex-R8" },
  362. { 0xc20, "Cortex-M0" },
  363. { 0xc21, "Cortex-M1" },
  364. { 0xc23, "Cortex-M3" },
  365. { 0xc24, "Cortex-M4" },
  366. { 0xc27, "Cortex-M7" },
  367. { 0xc60, "Cortex-M0+" },
  368. { 0xd01, "Cortex-A32" },
  369. { 0xd03, "Cortex-A53" },
  370. { 0xd04, "Cortex-A35" },
  371. { 0xd05, "Cortex-A55" },
  372. { 0xd06, "Cortex-A65" },
  373. { 0xd07, "Cortex-A57" },
  374. { 0xd08, "Cortex-A72" },
  375. { 0xd09, "Cortex-A73" },
  376. { 0xd0a, "Cortex-A75" },
  377. { 0xd0b, "Cortex-A76" },
  378. { 0xd0c, "Neoverse-N1" },
  379. { 0xd0d, "Cortex-A77" },
  380. { 0xd0e, "Cortex-A76AE" },
  381. { 0xd13, "Cortex-R52" },
  382. { 0xd20, "Cortex-M23" },
  383. { 0xd21, "Cortex-M33" },
  384. { 0xd41, "Cortex-A78" },
  385. { 0xd42, "Cortex-A78AE" },
  386. { 0xd4a, "Neoverse-E1" },
  387. { 0xd4b, "Cortex-A78C" },
  388. };
  389. if (strncmp(buf, part_marker, sizeof(part_marker) - 1) == 0) {
  390. model = buf + sizeof(part_marker) - 1;
  391. errno = 0;
  392. model_id = strtol(model, NULL, 16);
  393. if ((errno != 0) || model_id < 0) {
  394. fclose(fp);
  395. return UV_EINVAL;
  396. }
  397. for (part_idx = 0; part_idx < ARRAY_SIZE(arm_chips); part_idx++) {
  398. if (model_id == arm_chips[part_idx].id) {
  399. model = uv__strdup(arm_chips[part_idx].name);
  400. if (model == NULL) {
  401. fclose(fp);
  402. return UV_ENOMEM;
  403. }
  404. ci[model_idx++].model = model;
  405. break;
  406. }
  407. }
  408. }
  409. #else /* defined(__mips__) */
  410. static const char model_marker[] = "cpu model\t\t: ";
  411. #endif
  412. if (strncmp(buf, model_marker, sizeof(model_marker) - 1) == 0) {
  413. model = buf + sizeof(model_marker) - 1;
  414. model = uv__strndup(model, strlen(model) - 1); /* Strip newline. */
  415. if (model == NULL) {
  416. fclose(fp);
  417. return UV_ENOMEM;
  418. }
  419. ci[model_idx++].model = model;
  420. continue;
  421. }
  422. }
  423. #else /* !__arm__ && !__mips__ && !__aarch64__ */
  424. if (speed_idx < numcpus) {
  425. if (strncmp(buf, speed_marker, sizeof(speed_marker) - 1) == 0) {
  426. ci[speed_idx++].speed = atoi(buf + sizeof(speed_marker) - 1);
  427. continue;
  428. }
  429. }
  430. #endif /* __arm__ || __mips__ || __aarch64__ */
  431. }
  432. fclose(fp);
  433. #endif /* __arm__ || __i386__ || __mips__ || __PPC__ || __x86_64__ || __aarch__ */
  434. /* Now we want to make sure that all the models contain *something* because
  435. * it's not safe to leave them as null. Copy the last entry unless there
  436. * isn't one, in that case we simply put "unknown" into everything.
  437. */
  438. inferred_model = "unknown";
  439. if (model_idx > 0)
  440. inferred_model = ci[model_idx - 1].model;
  441. while (model_idx < numcpus) {
  442. model = uv__strndup(inferred_model, strlen(inferred_model));
  443. if (model == NULL)
  444. return UV_ENOMEM;
  445. ci[model_idx++].model = model;
  446. }
  447. return 0;
  448. }
  449. static int read_times(FILE* statfile_fp,
  450. unsigned int numcpus,
  451. uv_cpu_info_t* ci) {
  452. struct uv_cpu_times_s ts;
  453. unsigned int ticks;
  454. unsigned int multiplier;
  455. uint64_t user;
  456. uint64_t nice;
  457. uint64_t sys;
  458. uint64_t idle;
  459. uint64_t dummy;
  460. uint64_t irq;
  461. uint64_t num;
  462. uint64_t len;
  463. char buf[1024];
  464. ticks = (unsigned int)sysconf(_SC_CLK_TCK);
  465. assert(ticks != (unsigned int) -1);
  466. assert(ticks != 0);
  467. multiplier = ((uint64_t)1000L / ticks);
  468. rewind(statfile_fp);
  469. if (!fgets(buf, sizeof(buf), statfile_fp))
  470. abort();
  471. num = 0;
  472. while (fgets(buf, sizeof(buf), statfile_fp)) {
  473. if (num >= numcpus)
  474. break;
  475. if (strncmp(buf, "cpu", 3))
  476. break;
  477. /* skip "cpu<num> " marker */
  478. {
  479. unsigned int n;
  480. int r = sscanf(buf, "cpu%u ", &n);
  481. assert(r == 1);
  482. (void) r; /* silence build warning */
  483. for (len = sizeof("cpu0"); n /= 10; len++);
  484. }
  485. /* Line contains user, nice, system, idle, iowait, irq, softirq, steal,
  486. * guest, guest_nice but we're only interested in the first four + irq.
  487. *
  488. * Don't use %*s to skip fields or %ll to read straight into the uint64_t
  489. * fields, they're not allowed in C89 mode.
  490. */
  491. if (6 != sscanf(buf + len,
  492. "%" PRIu64 " %" PRIu64 " %" PRIu64
  493. "%" PRIu64 " %" PRIu64 " %" PRIu64,
  494. &user,
  495. &nice,
  496. &sys,
  497. &idle,
  498. &dummy,
  499. &irq))
  500. abort();
  501. ts.user = user * multiplier;
  502. ts.nice = nice * multiplier;
  503. ts.sys = sys * multiplier;
  504. ts.idle = idle * multiplier;
  505. ts.irq = irq * multiplier;
  506. ci[num++].cpu_times = ts;
  507. }
  508. assert(num == numcpus);
  509. return 0;
  510. }
  511. static uint64_t read_cpufreq(unsigned int cpunum) {
  512. uint64_t val;
  513. char buf[1024];
  514. FILE* fp;
  515. snprintf(buf,
  516. sizeof(buf),
  517. "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq",
  518. cpunum);
  519. fp = uv__open_file(buf);
  520. if (fp == NULL)
  521. return 0;
  522. if (fscanf(fp, "%" PRIu64, &val) != 1)
  523. val = 0;
  524. fclose(fp);
  525. return val;
  526. }
  527. static int uv__ifaddr_exclude(struct ifaddrs *ent, int exclude_type) {
  528. if (!((ent->ifa_flags & IFF_UP) && (ent->ifa_flags & IFF_RUNNING)))
  529. return 1;
  530. if (ent->ifa_addr == NULL)
  531. return 1;
  532. /*
  533. * On Linux getifaddrs returns information related to the raw underlying
  534. * devices. We're not interested in this information yet.
  535. */
  536. if (ent->ifa_addr->sa_family == PF_PACKET)
  537. return exclude_type;
  538. return !exclude_type;
  539. }
  540. int uv_interface_addresses(uv_interface_address_t** addresses, int* count) {
  541. #ifndef HAVE_IFADDRS_H
  542. *count = 0;
  543. *addresses = NULL;
  544. return UV_ENOSYS;
  545. #else
  546. struct ifaddrs *addrs, *ent;
  547. uv_interface_address_t* address;
  548. int i;
  549. struct sockaddr_ll *sll;
  550. *count = 0;
  551. *addresses = NULL;
  552. if (getifaddrs(&addrs))
  553. return UV__ERR(errno);
  554. /* Count the number of interfaces */
  555. for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
  556. if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFADDR))
  557. continue;
  558. (*count)++;
  559. }
  560. if (*count == 0) {
  561. freeifaddrs(addrs);
  562. return 0;
  563. }
  564. /* Make sure the memory is initiallized to zero using calloc() */
  565. *addresses = uv__calloc(*count, sizeof(**addresses));
  566. if (!(*addresses)) {
  567. freeifaddrs(addrs);
  568. return UV_ENOMEM;
  569. }
  570. address = *addresses;
  571. for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
  572. if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFADDR))
  573. continue;
  574. address->name = uv__strdup(ent->ifa_name);
  575. if (ent->ifa_addr->sa_family == AF_INET6) {
  576. address->address.address6 = *((struct sockaddr_in6*) ent->ifa_addr);
  577. } else {
  578. address->address.address4 = *((struct sockaddr_in*) ent->ifa_addr);
  579. }
  580. if (ent->ifa_netmask->sa_family == AF_INET6) {
  581. address->netmask.netmask6 = *((struct sockaddr_in6*) ent->ifa_netmask);
  582. } else {
  583. address->netmask.netmask4 = *((struct sockaddr_in*) ent->ifa_netmask);
  584. }
  585. address->is_internal = !!(ent->ifa_flags & IFF_LOOPBACK);
  586. address++;
  587. }
  588. /* Fill in physical addresses for each interface */
  589. for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
  590. if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFPHYS))
  591. continue;
  592. address = *addresses;
  593. for (i = 0; i < (*count); i++) {
  594. size_t namelen = strlen(ent->ifa_name);
  595. /* Alias interface share the same physical address */
  596. if (strncmp(address->name, ent->ifa_name, namelen) == 0 &&
  597. (address->name[namelen] == 0 || address->name[namelen] == ':')) {
  598. sll = (struct sockaddr_ll*)ent->ifa_addr;
  599. memcpy(address->phys_addr, sll->sll_addr, sizeof(address->phys_addr));
  600. }
  601. address++;
  602. }
  603. }
  604. freeifaddrs(addrs);
  605. return 0;
  606. #endif
  607. }
  608. void uv_free_interface_addresses(uv_interface_address_t* addresses,
  609. int count) {
  610. int i;
  611. for (i = 0; i < count; i++) {
  612. uv__free(addresses[i].name);
  613. }
  614. uv__free(addresses);
  615. }
  616. void uv__set_process_title(const char* title) {
  617. #if defined(PR_SET_NAME)
  618. prctl(PR_SET_NAME, title); /* Only copies first 16 characters. */
  619. #endif
  620. }
  621. static uint64_t uv__read_proc_meminfo(const char* what) {
  622. uint64_t rc;
  623. char* p;
  624. char buf[4096]; /* Large enough to hold all of /proc/meminfo. */
  625. if (uv__slurp("/proc/meminfo", buf, sizeof(buf)))
  626. return 0;
  627. p = strstr(buf, what);
  628. if (p == NULL)
  629. return 0;
  630. p += strlen(what);
  631. rc = 0;
  632. sscanf(p, "%" PRIu64 " kB", &rc);
  633. return rc * 1024;
  634. }
  635. uint64_t uv_get_free_memory(void) {
  636. struct sysinfo info;
  637. uint64_t rc;
  638. rc = uv__read_proc_meminfo("MemAvailable:");
  639. if (rc != 0)
  640. return rc;
  641. if (0 == sysinfo(&info))
  642. return (uint64_t) info.freeram * info.mem_unit;
  643. return 0;
  644. }
  645. uint64_t uv_get_total_memory(void) {
  646. struct sysinfo info;
  647. uint64_t rc;
  648. rc = uv__read_proc_meminfo("MemTotal:");
  649. if (rc != 0)
  650. return rc;
  651. if (0 == sysinfo(&info))
  652. return (uint64_t) info.totalram * info.mem_unit;
  653. return 0;
  654. }
  655. static uint64_t uv__read_cgroups_uint64(const char* cgroup, const char* param) {
  656. char filename[256];
  657. char buf[32]; /* Large enough to hold an encoded uint64_t. */
  658. uint64_t rc;
  659. rc = 0;
  660. snprintf(filename, sizeof(filename), "/sys/fs/cgroup/%s/%s", cgroup, param);
  661. if (0 == uv__slurp(filename, buf, sizeof(buf)))
  662. sscanf(buf, "%" PRIu64, &rc);
  663. return rc;
  664. }
  665. uint64_t uv_get_constrained_memory(void) {
  666. /*
  667. * This might return 0 if there was a problem getting the memory limit from
  668. * cgroups. This is OK because a return value of 0 signifies that the memory
  669. * limit is unknown.
  670. */
  671. return uv__read_cgroups_uint64("memory", "memory.limit_in_bytes");
  672. }
  673. void uv_loadavg(double avg[3]) {
  674. struct sysinfo info;
  675. char buf[128]; /* Large enough to hold all of /proc/loadavg. */
  676. if (0 == uv__slurp("/proc/loadavg", buf, sizeof(buf)))
  677. if (3 == sscanf(buf, "%lf %lf %lf", &avg[0], &avg[1], &avg[2]))
  678. return;
  679. if (sysinfo(&info) < 0)
  680. return;
  681. avg[0] = (double) info.loads[0] / 65536.0;
  682. avg[1] = (double) info.loads[1] / 65536.0;
  683. avg[2] = (double) info.loads[2] / 65536.0;
  684. }