rbuv 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (233) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +0 -1
  6. data/README.md +6 -1
  7. data/Rakefile +42 -0
  8. data/deps/libuv/.gitignore +34 -0
  9. data/deps/libuv/.mailmap +16 -0
  10. data/deps/libuv/AUTHORS +81 -0
  11. data/deps/libuv/ChangeLog +45 -0
  12. data/deps/libuv/LICENSE +41 -0
  13. data/deps/libuv/Makefile +53 -0
  14. data/deps/libuv/README.md +118 -0
  15. data/deps/libuv/build.mk +164 -0
  16. data/deps/libuv/checksparse.sh +230 -0
  17. data/deps/libuv/common.gypi +197 -0
  18. data/deps/libuv/config-mingw.mk +48 -0
  19. data/deps/libuv/config-unix.mk +167 -0
  20. data/deps/libuv/gyp_uv +98 -0
  21. data/deps/libuv/include/uv-private/ngx-queue.h +129 -0
  22. data/deps/libuv/include/uv-private/stdint-msvc2008.h +247 -0
  23. data/deps/libuv/include/uv-private/tree.h +768 -0
  24. data/deps/libuv/include/uv-private/uv-bsd.h +34 -0
  25. data/deps/libuv/include/uv-private/uv-darwin.h +61 -0
  26. data/deps/libuv/include/uv-private/uv-linux.h +34 -0
  27. data/deps/libuv/include/uv-private/uv-sunos.h +44 -0
  28. data/deps/libuv/include/uv-private/uv-unix.h +332 -0
  29. data/deps/libuv/include/uv-private/uv-win.h +585 -0
  30. data/deps/libuv/include/uv.h +1987 -0
  31. data/deps/libuv/src/fs-poll.c +248 -0
  32. data/deps/libuv/src/inet.c +298 -0
  33. data/deps/libuv/src/unix/aix.c +393 -0
  34. data/deps/libuv/src/unix/async.c +281 -0
  35. data/deps/libuv/src/unix/core.c +714 -0
  36. data/deps/libuv/src/unix/cygwin.c +93 -0
  37. data/deps/libuv/src/unix/darwin-proctitle.m +78 -0
  38. data/deps/libuv/src/unix/darwin.c +431 -0
  39. data/deps/libuv/src/unix/dl.c +83 -0
  40. data/deps/libuv/src/unix/error.c +109 -0
  41. data/deps/libuv/src/unix/freebsd.c +343 -0
  42. data/deps/libuv/src/unix/fs.c +869 -0
  43. data/deps/libuv/src/unix/fsevents.c +299 -0
  44. data/deps/libuv/src/unix/getaddrinfo.c +159 -0
  45. data/deps/libuv/src/unix/internal.h +259 -0
  46. data/deps/libuv/src/unix/kqueue.c +347 -0
  47. data/deps/libuv/src/unix/linux-core.c +724 -0
  48. data/deps/libuv/src/unix/linux-inotify.c +236 -0
  49. data/deps/libuv/src/unix/linux-syscalls.c +388 -0
  50. data/deps/libuv/src/unix/linux-syscalls.h +150 -0
  51. data/deps/libuv/src/unix/loop-watcher.c +64 -0
  52. data/deps/libuv/src/unix/loop.c +114 -0
  53. data/deps/libuv/src/unix/netbsd.c +353 -0
  54. data/deps/libuv/src/unix/openbsd.c +304 -0
  55. data/deps/libuv/src/unix/pipe.c +261 -0
  56. data/deps/libuv/src/unix/poll.c +108 -0
  57. data/deps/libuv/src/unix/process.c +501 -0
  58. data/deps/libuv/src/unix/proctitle.c +103 -0
  59. data/deps/libuv/src/unix/signal.c +455 -0
  60. data/deps/libuv/src/unix/stream.c +1380 -0
  61. data/deps/libuv/src/unix/sunos.c +647 -0
  62. data/deps/libuv/src/unix/tcp.c +357 -0
  63. data/deps/libuv/src/unix/thread.c +431 -0
  64. data/deps/libuv/src/unix/threadpool.c +286 -0
  65. data/deps/libuv/src/unix/timer.c +153 -0
  66. data/deps/libuv/src/unix/tty.c +179 -0
  67. data/deps/libuv/src/unix/udp.c +715 -0
  68. data/deps/libuv/src/uv-common.c +431 -0
  69. data/deps/libuv/src/uv-common.h +204 -0
  70. data/deps/libuv/src/version.c +60 -0
  71. data/deps/libuv/src/win/async.c +99 -0
  72. data/deps/libuv/src/win/atomicops-inl.h +56 -0
  73. data/deps/libuv/src/win/core.c +310 -0
  74. data/deps/libuv/src/win/dl.c +86 -0
  75. data/deps/libuv/src/win/error.c +164 -0
  76. data/deps/libuv/src/win/fs-event.c +506 -0
  77. data/deps/libuv/src/win/fs.c +1951 -0
  78. data/deps/libuv/src/win/getaddrinfo.c +365 -0
  79. data/deps/libuv/src/win/handle-inl.h +164 -0
  80. data/deps/libuv/src/win/handle.c +153 -0
  81. data/deps/libuv/src/win/internal.h +346 -0
  82. data/deps/libuv/src/win/loop-watcher.c +124 -0
  83. data/deps/libuv/src/win/pipe.c +1656 -0
  84. data/deps/libuv/src/win/poll.c +615 -0
  85. data/deps/libuv/src/win/process-stdio.c +503 -0
  86. data/deps/libuv/src/win/process.c +1048 -0
  87. data/deps/libuv/src/win/req-inl.h +224 -0
  88. data/deps/libuv/src/win/req.c +25 -0
  89. data/deps/libuv/src/win/signal.c +354 -0
  90. data/deps/libuv/src/win/stream-inl.h +67 -0
  91. data/deps/libuv/src/win/stream.c +198 -0
  92. data/deps/libuv/src/win/tcp.c +1422 -0
  93. data/deps/libuv/src/win/thread.c +666 -0
  94. data/deps/libuv/src/win/threadpool.c +82 -0
  95. data/deps/libuv/src/win/timer.c +230 -0
  96. data/deps/libuv/src/win/tty.c +1857 -0
  97. data/deps/libuv/src/win/udp.c +744 -0
  98. data/deps/libuv/src/win/util.c +946 -0
  99. data/deps/libuv/src/win/winapi.c +152 -0
  100. data/deps/libuv/src/win/winapi.h +4476 -0
  101. data/deps/libuv/src/win/winsock.c +560 -0
  102. data/deps/libuv/src/win/winsock.h +171 -0
  103. data/deps/libuv/test/benchmark-async-pummel.c +119 -0
  104. data/deps/libuv/test/benchmark-async.c +139 -0
  105. data/deps/libuv/test/benchmark-fs-stat.c +136 -0
  106. data/deps/libuv/test/benchmark-getaddrinfo.c +91 -0
  107. data/deps/libuv/test/benchmark-list.h +163 -0
  108. data/deps/libuv/test/benchmark-loop-count.c +90 -0
  109. data/deps/libuv/test/benchmark-million-async.c +112 -0
  110. data/deps/libuv/test/benchmark-million-timers.c +77 -0
  111. data/deps/libuv/test/benchmark-multi-accept.c +432 -0
  112. data/deps/libuv/test/benchmark-ping-pongs.c +212 -0
  113. data/deps/libuv/test/benchmark-pound.c +325 -0
  114. data/deps/libuv/test/benchmark-pump.c +459 -0
  115. data/deps/libuv/test/benchmark-sizes.c +45 -0
  116. data/deps/libuv/test/benchmark-spawn.c +163 -0
  117. data/deps/libuv/test/benchmark-tcp-write-batch.c +141 -0
  118. data/deps/libuv/test/benchmark-thread.c +64 -0
  119. data/deps/libuv/test/benchmark-udp-pummel.c +238 -0
  120. data/deps/libuv/test/blackhole-server.c +118 -0
  121. data/deps/libuv/test/dns-server.c +329 -0
  122. data/deps/libuv/test/echo-server.c +384 -0
  123. data/deps/libuv/test/fixtures/empty_file +0 -0
  124. data/deps/libuv/test/fixtures/load_error.node +1 -0
  125. data/deps/libuv/test/run-benchmarks.c +64 -0
  126. data/deps/libuv/test/run-tests.c +159 -0
  127. data/deps/libuv/test/runner-unix.c +328 -0
  128. data/deps/libuv/test/runner-unix.h +36 -0
  129. data/deps/libuv/test/runner-win.c +318 -0
  130. data/deps/libuv/test/runner-win.h +43 -0
  131. data/deps/libuv/test/runner.c +394 -0
  132. data/deps/libuv/test/runner.h +165 -0
  133. data/deps/libuv/test/task.h +122 -0
  134. data/deps/libuv/test/test-active.c +83 -0
  135. data/deps/libuv/test/test-async.c +136 -0
  136. data/deps/libuv/test/test-barrier.c +98 -0
  137. data/deps/libuv/test/test-callback-order.c +77 -0
  138. data/deps/libuv/test/test-callback-stack.c +204 -0
  139. data/deps/libuv/test/test-condvar.c +173 -0
  140. data/deps/libuv/test/test-connection-fail.c +150 -0
  141. data/deps/libuv/test/test-cwd-and-chdir.c +64 -0
  142. data/deps/libuv/test/test-delayed-accept.c +189 -0
  143. data/deps/libuv/test/test-dlerror.c +58 -0
  144. data/deps/libuv/test/test-embed.c +136 -0
  145. data/deps/libuv/test/test-error.c +59 -0
  146. data/deps/libuv/test/test-fail-always.c +29 -0
  147. data/deps/libuv/test/test-fs-event.c +504 -0
  148. data/deps/libuv/test/test-fs-poll.c +148 -0
  149. data/deps/libuv/test/test-fs.c +1899 -0
  150. data/deps/libuv/test/test-get-currentexe.c +63 -0
  151. data/deps/libuv/test/test-get-loadavg.c +36 -0
  152. data/deps/libuv/test/test-get-memory.c +38 -0
  153. data/deps/libuv/test/test-getaddrinfo.c +120 -0
  154. data/deps/libuv/test/test-getsockname.c +344 -0
  155. data/deps/libuv/test/test-hrtime.c +54 -0
  156. data/deps/libuv/test/test-idle.c +82 -0
  157. data/deps/libuv/test/test-ipc-send-recv.c +218 -0
  158. data/deps/libuv/test/test-ipc.c +625 -0
  159. data/deps/libuv/test/test-list.h +492 -0
  160. data/deps/libuv/test/test-loop-handles.c +337 -0
  161. data/deps/libuv/test/test-loop-stop.c +73 -0
  162. data/deps/libuv/test/test-multiple-listen.c +103 -0
  163. data/deps/libuv/test/test-mutexes.c +63 -0
  164. data/deps/libuv/test/test-pass-always.c +28 -0
  165. data/deps/libuv/test/test-ping-pong.c +250 -0
  166. data/deps/libuv/test/test-pipe-bind-error.c +144 -0
  167. data/deps/libuv/test/test-pipe-connect-error.c +98 -0
  168. data/deps/libuv/test/test-platform-output.c +87 -0
  169. data/deps/libuv/test/test-poll-close.c +73 -0
  170. data/deps/libuv/test/test-poll.c +575 -0
  171. data/deps/libuv/test/test-process-title.c +49 -0
  172. data/deps/libuv/test/test-ref.c +415 -0
  173. data/deps/libuv/test/test-run-nowait.c +46 -0
  174. data/deps/libuv/test/test-run-once.c +49 -0
  175. data/deps/libuv/test/test-semaphore.c +111 -0
  176. data/deps/libuv/test/test-shutdown-close.c +105 -0
  177. data/deps/libuv/test/test-shutdown-eof.c +184 -0
  178. data/deps/libuv/test/test-signal-multiple-loops.c +270 -0
  179. data/deps/libuv/test/test-signal.c +152 -0
  180. data/deps/libuv/test/test-spawn.c +938 -0
  181. data/deps/libuv/test/test-stdio-over-pipes.c +250 -0
  182. data/deps/libuv/test/test-tcp-bind-error.c +198 -0
  183. data/deps/libuv/test/test-tcp-bind6-error.c +159 -0
  184. data/deps/libuv/test/test-tcp-close-while-connecting.c +81 -0
  185. data/deps/libuv/test/test-tcp-close.c +130 -0
  186. data/deps/libuv/test/test-tcp-connect-error-after-write.c +96 -0
  187. data/deps/libuv/test/test-tcp-connect-error.c +71 -0
  188. data/deps/libuv/test/test-tcp-connect-timeout.c +86 -0
  189. data/deps/libuv/test/test-tcp-connect6-error.c +69 -0
  190. data/deps/libuv/test/test-tcp-flags.c +52 -0
  191. data/deps/libuv/test/test-tcp-open.c +175 -0
  192. data/deps/libuv/test/test-tcp-read-stop.c +73 -0
  193. data/deps/libuv/test/test-tcp-shutdown-after-write.c +132 -0
  194. data/deps/libuv/test/test-tcp-unexpected-read.c +114 -0
  195. data/deps/libuv/test/test-tcp-write-to-half-open-connection.c +136 -0
  196. data/deps/libuv/test/test-tcp-writealot.c +171 -0
  197. data/deps/libuv/test/test-thread.c +183 -0
  198. data/deps/libuv/test/test-threadpool-cancel.c +311 -0
  199. data/deps/libuv/test/test-threadpool.c +77 -0
  200. data/deps/libuv/test/test-timer-again.c +142 -0
  201. data/deps/libuv/test/test-timer.c +266 -0
  202. data/deps/libuv/test/test-tty.c +111 -0
  203. data/deps/libuv/test/test-udp-dgram-too-big.c +87 -0
  204. data/deps/libuv/test/test-udp-ipv6.c +158 -0
  205. data/deps/libuv/test/test-udp-multicast-join.c +140 -0
  206. data/deps/libuv/test/test-udp-multicast-ttl.c +87 -0
  207. data/deps/libuv/test/test-udp-open.c +154 -0
  208. data/deps/libuv/test/test-udp-options.c +87 -0
  209. data/deps/libuv/test/test-udp-send-and-recv.c +210 -0
  210. data/deps/libuv/test/test-util.c +97 -0
  211. data/deps/libuv/test/test-walk-handles.c +78 -0
  212. data/deps/libuv/uv.gyp +431 -0
  213. data/deps/libuv/vcbuild.bat +128 -0
  214. data/ext/rbuv/debug.h +27 -0
  215. data/ext/rbuv/error.c +7 -0
  216. data/ext/rbuv/error.h +10 -0
  217. data/ext/rbuv/extconf.rb +35 -0
  218. data/ext/rbuv/handle.c +40 -0
  219. data/ext/rbuv/handle.h +14 -0
  220. data/ext/rbuv/libuv.mk +12 -0
  221. data/ext/rbuv/loop.c +50 -0
  222. data/ext/rbuv/loop.h +13 -0
  223. data/ext/rbuv/rbuv.c +15 -0
  224. data/ext/rbuv/rbuv.h +27 -0
  225. data/ext/rbuv/timer.c +133 -0
  226. data/ext/rbuv/timer.h +13 -0
  227. data/lib/rbuv/timer.rb +7 -0
  228. data/lib/rbuv/version.rb +1 -1
  229. data/lib/rbuv.rb +24 -2
  230. data/rbuv.gemspec +5 -1
  231. data/spec/spec_helper.rb +22 -0
  232. data/spec/timer_spec.rb +144 -0
  233. metadata +278 -9
@@ -0,0 +1,724 @@
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 <stdint.h>
25
+ #include <stdio.h>
26
+ #include <stdlib.h>
27
+ #include <string.h>
28
+ #include <assert.h>
29
+ #include <errno.h>
30
+
31
+ #include <net/if.h>
32
+ #include <sys/param.h>
33
+ #include <sys/prctl.h>
34
+ #include <sys/sysinfo.h>
35
+ #include <unistd.h>
36
+ #include <fcntl.h>
37
+ #include <time.h>
38
+
39
+ #define HAVE_IFADDRS_H 1
40
+ #ifdef __UCLIBC__
41
+ # if __UCLIBC_MAJOR__ < 0 || __UCLIBC_MINOR__ < 9 || __UCLIBC_SUBLEVEL__ < 32
42
+ # undef HAVE_IFADDRS_H
43
+ # endif
44
+ #endif
45
+ #ifdef HAVE_IFADDRS_H
46
+ # include <ifaddrs.h>
47
+ #endif
48
+
49
+ #undef NANOSEC
50
+ #define NANOSEC ((uint64_t) 1e9)
51
+
52
+ /* This is rather annoying: CLOCK_BOOTTIME lives in <linux/time.h> but we can't
53
+ * include that file because it conflicts with <time.h>. We'll just have to
54
+ * define it ourselves.
55
+ */
56
+ #ifndef CLOCK_BOOTTIME
57
+ # define CLOCK_BOOTTIME 7
58
+ #endif
59
+
60
+ static int read_models(unsigned int numcpus, uv_cpu_info_t* ci);
61
+ static int read_times(unsigned int numcpus, uv_cpu_info_t* ci);
62
+ static void read_speeds(unsigned int numcpus, uv_cpu_info_t* ci);
63
+ static unsigned long read_cpufreq(unsigned int cpunum);
64
+
65
+
66
+ int uv__platform_loop_init(uv_loop_t* loop, int default_loop) {
67
+ int fd;
68
+
69
+ fd = uv__epoll_create1(UV__EPOLL_CLOEXEC);
70
+
71
+ /* epoll_create1() can fail either because it's not implemented (old kernel)
72
+ * or because it doesn't understand the EPOLL_CLOEXEC flag.
73
+ */
74
+ if (fd == -1 && (errno == ENOSYS || errno == EINVAL)) {
75
+ fd = uv__epoll_create(256);
76
+
77
+ if (fd != -1)
78
+ uv__cloexec(fd, 1);
79
+ }
80
+
81
+ loop->backend_fd = fd;
82
+ loop->inotify_fd = -1;
83
+ loop->inotify_watchers = NULL;
84
+
85
+ if (fd == -1)
86
+ return -1;
87
+
88
+ return 0;
89
+ }
90
+
91
+
92
+ void uv__platform_loop_delete(uv_loop_t* loop) {
93
+ if (loop->inotify_fd == -1) return;
94
+ uv__io_stop(loop, &loop->inotify_read_watcher, UV__POLLIN);
95
+ close(loop->inotify_fd);
96
+ loop->inotify_fd = -1;
97
+ }
98
+
99
+
100
+ void uv__io_poll(uv_loop_t* loop, int timeout) {
101
+ struct uv__epoll_event events[1024];
102
+ struct uv__epoll_event* pe;
103
+ struct uv__epoll_event e;
104
+ ngx_queue_t* q;
105
+ uv__io_t* w;
106
+ uint64_t base;
107
+ uint64_t diff;
108
+ int nevents;
109
+ int count;
110
+ int nfds;
111
+ int fd;
112
+ int op;
113
+ int i;
114
+
115
+ if (loop->nfds == 0) {
116
+ assert(ngx_queue_empty(&loop->watcher_queue));
117
+ return;
118
+ }
119
+
120
+ while (!ngx_queue_empty(&loop->watcher_queue)) {
121
+ q = ngx_queue_head(&loop->watcher_queue);
122
+ ngx_queue_remove(q);
123
+ ngx_queue_init(q);
124
+
125
+ w = ngx_queue_data(q, uv__io_t, watcher_queue);
126
+ assert(w->pevents != 0);
127
+ assert(w->fd >= 0);
128
+ assert(w->fd < (int) loop->nwatchers);
129
+
130
+ e.events = w->pevents;
131
+ e.data = w->fd;
132
+
133
+ if (w->events == 0)
134
+ op = UV__EPOLL_CTL_ADD;
135
+ else
136
+ op = UV__EPOLL_CTL_MOD;
137
+
138
+ /* XXX Future optimization: do EPOLL_CTL_MOD lazily if we stop watching
139
+ * events, skip the syscall and squelch the events after epoll_wait().
140
+ */
141
+ if (uv__epoll_ctl(loop->backend_fd, op, w->fd, &e)) {
142
+ if (errno != EEXIST)
143
+ abort();
144
+
145
+ assert(op == UV__EPOLL_CTL_ADD);
146
+
147
+ /* We've reactivated a file descriptor that's been watched before. */
148
+ if (uv__epoll_ctl(loop->backend_fd, UV__EPOLL_CTL_MOD, w->fd, &e))
149
+ abort();
150
+ }
151
+
152
+ w->events = w->pevents;
153
+ }
154
+
155
+ assert(timeout >= -1);
156
+ base = loop->time;
157
+ count = 48; /* Benchmarks suggest this gives the best throughput. */
158
+
159
+ for (;;) {
160
+ nfds = uv__epoll_wait(loop->backend_fd,
161
+ events,
162
+ ARRAY_SIZE(events),
163
+ timeout);
164
+
165
+ /* Update loop->time unconditionally. It's tempting to skip the update when
166
+ * timeout == 0 (i.e. non-blocking poll) but there is no guarantee that the
167
+ * operating system didn't reschedule our process while in the syscall.
168
+ */
169
+ SAVE_ERRNO(uv__update_time(loop));
170
+
171
+ if (nfds == 0) {
172
+ assert(timeout != -1);
173
+ return;
174
+ }
175
+
176
+ if (nfds == -1) {
177
+ if (errno != EINTR)
178
+ abort();
179
+
180
+ if (timeout == -1)
181
+ continue;
182
+
183
+ if (timeout == 0)
184
+ return;
185
+
186
+ /* Interrupted by a signal. Update timeout and poll again. */
187
+ goto update_timeout;
188
+ }
189
+
190
+ nevents = 0;
191
+
192
+ for (i = 0; i < nfds; i++) {
193
+ pe = events + i;
194
+ fd = pe->data;
195
+
196
+ assert(fd >= 0);
197
+ assert((unsigned) fd < loop->nwatchers);
198
+
199
+ w = loop->watchers[fd];
200
+
201
+ if (w == NULL) {
202
+ /* File descriptor that we've stopped watching, disarm it.
203
+ *
204
+ * Ignore all errors because we may be racing with another thread
205
+ * when the file descriptor is closed.
206
+ */
207
+ uv__epoll_ctl(loop->backend_fd, UV__EPOLL_CTL_DEL, fd, pe);
208
+ continue;
209
+ }
210
+
211
+ w->cb(loop, w, pe->events);
212
+ nevents++;
213
+ }
214
+
215
+ if (nevents != 0) {
216
+ if (nfds == ARRAY_SIZE(events) && --count != 0) {
217
+ /* Poll for more events but don't block this time. */
218
+ timeout = 0;
219
+ continue;
220
+ }
221
+ return;
222
+ }
223
+
224
+ if (timeout == 0)
225
+ return;
226
+
227
+ if (timeout == -1)
228
+ continue;
229
+
230
+ update_timeout:
231
+ assert(timeout > 0);
232
+
233
+ diff = loop->time - base;
234
+ if (diff >= (uint64_t) timeout)
235
+ return;
236
+
237
+ timeout -= diff;
238
+ }
239
+ }
240
+
241
+
242
+ uint64_t uv__hrtime(void) {
243
+ struct timespec ts;
244
+ clock_gettime(CLOCK_MONOTONIC, &ts);
245
+ return (((uint64_t) ts.tv_sec) * NANOSEC + ts.tv_nsec);
246
+ }
247
+
248
+
249
+ void uv_loadavg(double avg[3]) {
250
+ struct sysinfo info;
251
+
252
+ if (sysinfo(&info) < 0) return;
253
+
254
+ avg[0] = (double) info.loads[0] / 65536.0;
255
+ avg[1] = (double) info.loads[1] / 65536.0;
256
+ avg[2] = (double) info.loads[2] / 65536.0;
257
+ }
258
+
259
+
260
+ int uv_exepath(char* buffer, size_t* size) {
261
+ ssize_t n;
262
+
263
+ if (!buffer || !size) {
264
+ return -1;
265
+ }
266
+
267
+ n = readlink("/proc/self/exe", buffer, *size - 1);
268
+ if (n <= 0) return -1;
269
+ buffer[n] = '\0';
270
+ *size = n;
271
+
272
+ return 0;
273
+ }
274
+
275
+
276
+ uint64_t uv_get_free_memory(void) {
277
+ return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
278
+ }
279
+
280
+
281
+ uint64_t uv_get_total_memory(void) {
282
+ return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
283
+ }
284
+
285
+
286
+ uv_err_t uv_resident_set_memory(size_t* rss) {
287
+ FILE* f;
288
+ int itmp;
289
+ char ctmp;
290
+ unsigned int utmp;
291
+ size_t page_size = getpagesize();
292
+ char *cbuf;
293
+ int foundExeEnd;
294
+ char buf[PATH_MAX + 1];
295
+
296
+ f = fopen("/proc/self/stat", "r");
297
+ if (!f) return uv__new_sys_error(errno);
298
+
299
+ /* PID */
300
+ if (fscanf(f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
301
+ /* Exec file */
302
+ cbuf = buf;
303
+ foundExeEnd = 0;
304
+ if (fscanf (f, "%c", cbuf++) == 0) goto error;
305
+ while (1) {
306
+ if (fscanf(f, "%c", cbuf) == 0) goto error;
307
+ if (*cbuf == ')') {
308
+ foundExeEnd = 1;
309
+ } else if (foundExeEnd && *cbuf == ' ') {
310
+ *cbuf = 0;
311
+ break;
312
+ }
313
+
314
+ cbuf++;
315
+ }
316
+ /* State */
317
+ if (fscanf (f, "%c ", &ctmp) == 0) goto error; /* coverity[secure_coding] */
318
+ /* Parent process */
319
+ if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
320
+ /* Process group */
321
+ if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
322
+ /* Session id */
323
+ if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
324
+ /* TTY */
325
+ if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
326
+ /* TTY owner process group */
327
+ if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
328
+ /* Flags */
329
+ if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
330
+ /* Minor faults (no memory page) */
331
+ if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
332
+ /* Minor faults, children */
333
+ if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
334
+ /* Major faults (memory page faults) */
335
+ if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
336
+ /* Major faults, children */
337
+ if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
338
+ /* utime */
339
+ if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
340
+ /* stime */
341
+ if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
342
+ /* utime, children */
343
+ if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
344
+ /* stime, children */
345
+ if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
346
+ /* jiffies remaining in current time slice */
347
+ if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
348
+ /* 'nice' value */
349
+ if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
350
+ /* jiffies until next timeout */
351
+ if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
352
+ /* jiffies until next SIGALRM */
353
+ if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
354
+ /* start time (jiffies since system boot) */
355
+ if (fscanf (f, "%d ", &itmp) == 0) goto error; /* coverity[secure_coding] */
356
+
357
+ /* Virtual memory size */
358
+ if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
359
+
360
+ /* Resident set size */
361
+ if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
362
+ *rss = (size_t) utmp * page_size;
363
+
364
+ /* rlim */
365
+ if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
366
+ /* Start of text */
367
+ if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
368
+ /* End of text */
369
+ if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
370
+ /* Start of stack */
371
+ if (fscanf (f, "%u ", &utmp) == 0) goto error; /* coverity[secure_coding] */
372
+
373
+ fclose (f);
374
+ return uv_ok_;
375
+
376
+ error:
377
+ fclose (f);
378
+ return uv__new_sys_error(errno);
379
+ }
380
+
381
+
382
+ uv_err_t uv_uptime(double* uptime) {
383
+ static volatile int no_clock_boottime;
384
+ struct timespec now;
385
+ int r;
386
+
387
+ /* Try CLOCK_BOOTTIME first, fall back to CLOCK_MONOTONIC if not available
388
+ * (pre-2.6.39 kernels). CLOCK_MONOTONIC doesn't increase when the system
389
+ * is suspended.
390
+ */
391
+ if (no_clock_boottime) {
392
+ retry: r = clock_gettime(CLOCK_MONOTONIC, &now);
393
+ }
394
+ else if ((r = clock_gettime(CLOCK_BOOTTIME, &now)) && errno == EINVAL) {
395
+ no_clock_boottime = 1;
396
+ goto retry;
397
+ }
398
+
399
+ if (r)
400
+ return uv__new_sys_error(errno);
401
+
402
+ *uptime = now.tv_sec;
403
+ *uptime += (double)now.tv_nsec / 1000000000.0;
404
+ return uv_ok_;
405
+ }
406
+
407
+
408
+ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
409
+ unsigned int numcpus;
410
+ uv_cpu_info_t* ci;
411
+
412
+ *cpu_infos = NULL;
413
+ *count = 0;
414
+
415
+ numcpus = sysconf(_SC_NPROCESSORS_ONLN);
416
+ assert(numcpus != (unsigned int) -1);
417
+ assert(numcpus != 0);
418
+
419
+ ci = calloc(numcpus, sizeof(*ci));
420
+ if (ci == NULL)
421
+ return uv__new_sys_error(ENOMEM);
422
+
423
+ if (read_models(numcpus, ci)) {
424
+ SAVE_ERRNO(free(ci));
425
+ return uv__new_sys_error(errno);
426
+ }
427
+
428
+ if (read_times(numcpus, ci)) {
429
+ SAVE_ERRNO(free(ci));
430
+ return uv__new_sys_error(errno);
431
+ }
432
+
433
+ /* read_models() on x86 also reads the CPU speed from /proc/cpuinfo.
434
+ * We don't check for errors here. Worst case, the field is left zero.
435
+ */
436
+ if (ci[0].speed == 0)
437
+ read_speeds(numcpus, ci);
438
+
439
+ *cpu_infos = ci;
440
+ *count = numcpus;
441
+
442
+ return uv_ok_;
443
+ }
444
+
445
+
446
+ static void read_speeds(unsigned int numcpus, uv_cpu_info_t* ci) {
447
+ unsigned int num;
448
+
449
+ for (num = 0; num < numcpus; num++)
450
+ ci[num].speed = read_cpufreq(num) / 1000;
451
+ }
452
+
453
+
454
+ /* Also reads the CPU frequency on x86. The other architectures only have
455
+ * a BogoMIPS field, which may not be very accurate.
456
+ */
457
+ static int read_models(unsigned int numcpus, uv_cpu_info_t* ci) {
458
+ #if defined(__i386__) || defined(__x86_64__)
459
+ static const char model_marker[] = "model name\t: ";
460
+ static const char speed_marker[] = "cpu MHz\t\t: ";
461
+ #elif defined(__arm__)
462
+ static const char model_marker[] = "Processor\t: ";
463
+ static const char speed_marker[] = "";
464
+ #elif defined(__mips__)
465
+ static const char model_marker[] = "cpu model\t\t: ";
466
+ static const char speed_marker[] = "";
467
+ #else
468
+ # warning uv_cpu_info() is not supported on this architecture.
469
+ static const char model_marker[] = "";
470
+ static const char speed_marker[] = "";
471
+ #endif
472
+ static const char bogus_model[] = "unknown";
473
+ unsigned int model_idx;
474
+ unsigned int speed_idx;
475
+ char buf[1024];
476
+ char* model;
477
+ FILE* fp;
478
+ char* inferred_model;
479
+
480
+ fp = fopen("/proc/cpuinfo", "r");
481
+ if (fp == NULL)
482
+ return -1;
483
+
484
+ model_idx = 0;
485
+ speed_idx = 0;
486
+
487
+ while (fgets(buf, sizeof(buf), fp)) {
488
+ if (model_marker[0] != '\0' &&
489
+ model_idx < numcpus &&
490
+ strncmp(buf, model_marker, sizeof(model_marker) - 1) == 0)
491
+ {
492
+ model = buf + sizeof(model_marker) - 1;
493
+ model = strndup(model, strlen(model) - 1); /* strip newline */
494
+ ci[model_idx++].model = model;
495
+ continue;
496
+ }
497
+
498
+ if (speed_marker[0] != '\0' &&
499
+ speed_idx < numcpus &&
500
+ strncmp(buf, speed_marker, sizeof(speed_marker) - 1) == 0)
501
+ {
502
+ ci[speed_idx++].speed = atoi(buf + sizeof(speed_marker) - 1);
503
+ continue;
504
+ }
505
+ }
506
+ fclose(fp);
507
+
508
+ /* Now we want to make sure that all the models contain *something*:
509
+ * it's not safe to leave them as null.
510
+ */
511
+ if (model_idx == 0) {
512
+ /* No models at all: fake up the first one. */
513
+ ci[0].model = strndup(bogus_model, sizeof(bogus_model) - 1);
514
+ model_idx = 1;
515
+ }
516
+
517
+ /* Not enough models, but we do have at least one. So we'll just
518
+ * copy the rest down: it might be better to indicate somehow that
519
+ * the remaining ones have been guessed.
520
+ */
521
+ inferred_model = ci[model_idx - 1].model;
522
+
523
+ while (model_idx < numcpus) {
524
+ ci[model_idx].model = strndup(inferred_model, strlen(inferred_model));
525
+ model_idx++;
526
+ }
527
+
528
+ return 0;
529
+ }
530
+
531
+
532
+ static int read_times(unsigned int numcpus, uv_cpu_info_t* ci) {
533
+ unsigned long clock_ticks;
534
+ struct uv_cpu_times_s ts;
535
+ unsigned long user;
536
+ unsigned long nice;
537
+ unsigned long sys;
538
+ unsigned long idle;
539
+ unsigned long dummy;
540
+ unsigned long irq;
541
+ unsigned int num;
542
+ unsigned int len;
543
+ char buf[1024];
544
+ FILE* fp;
545
+
546
+ clock_ticks = sysconf(_SC_CLK_TCK);
547
+ assert(clock_ticks != (unsigned long) -1);
548
+ assert(clock_ticks != 0);
549
+
550
+ fp = fopen("/proc/stat", "r");
551
+ if (fp == NULL)
552
+ return -1;
553
+
554
+ if (!fgets(buf, sizeof(buf), fp))
555
+ abort();
556
+
557
+ num = 0;
558
+
559
+ while (fgets(buf, sizeof(buf), fp)) {
560
+ if (num >= numcpus)
561
+ break;
562
+
563
+ if (strncmp(buf, "cpu", 3))
564
+ break;
565
+
566
+ /* skip "cpu<num> " marker */
567
+ {
568
+ unsigned int n = num;
569
+ for (len = sizeof("cpu0"); n /= 10; len++);
570
+ assert(sscanf(buf, "cpu%u ", &n) == 1 && n == num);
571
+ }
572
+
573
+ /* Line contains user, nice, system, idle, iowait, irq, softirq, steal,
574
+ * guest, guest_nice but we're only interested in the first four + irq.
575
+ *
576
+ * Don't use %*s to skip fields or %ll to read straight into the uint64_t
577
+ * fields, they're not allowed in C89 mode.
578
+ */
579
+ if (6 != sscanf(buf + len,
580
+ "%lu %lu %lu %lu %lu %lu",
581
+ &user,
582
+ &nice,
583
+ &sys,
584
+ &idle,
585
+ &dummy,
586
+ &irq))
587
+ abort();
588
+
589
+ ts.user = clock_ticks * user;
590
+ ts.nice = clock_ticks * nice;
591
+ ts.sys = clock_ticks * sys;
592
+ ts.idle = clock_ticks * idle;
593
+ ts.irq = clock_ticks * irq;
594
+ ci[num++].cpu_times = ts;
595
+ }
596
+ fclose(fp);
597
+
598
+ return 0;
599
+ }
600
+
601
+
602
+ static unsigned long read_cpufreq(unsigned int cpunum) {
603
+ unsigned long val;
604
+ char buf[1024];
605
+ FILE* fp;
606
+
607
+ snprintf(buf,
608
+ sizeof(buf),
609
+ "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq",
610
+ cpunum);
611
+
612
+ fp = fopen(buf, "r");
613
+ if (fp == NULL)
614
+ return 0;
615
+
616
+ if (fscanf(fp, "%lu", &val) != 1)
617
+ val = 0;
618
+
619
+ fclose(fp);
620
+
621
+ return val;
622
+ }
623
+
624
+
625
+ void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
626
+ int i;
627
+
628
+ for (i = 0; i < count; i++) {
629
+ free(cpu_infos[i].model);
630
+ }
631
+
632
+ free(cpu_infos);
633
+ }
634
+
635
+
636
+ uv_err_t uv_interface_addresses(uv_interface_address_t** addresses,
637
+ int* count) {
638
+ #ifndef HAVE_IFADDRS_H
639
+ return uv__new_artificial_error(UV_ENOSYS);
640
+ #else
641
+ struct ifaddrs *addrs, *ent;
642
+ char ip[INET6_ADDRSTRLEN];
643
+ uv_interface_address_t* address;
644
+
645
+ if (getifaddrs(&addrs) != 0) {
646
+ return uv__new_sys_error(errno);
647
+ }
648
+
649
+ *count = 0;
650
+
651
+ /* Count the number of interfaces */
652
+ for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
653
+ if (!(ent->ifa_flags & IFF_UP && ent->ifa_flags & IFF_RUNNING) ||
654
+ (ent->ifa_addr == NULL) ||
655
+ (ent->ifa_addr->sa_family == PF_PACKET)) {
656
+ continue;
657
+ }
658
+
659
+ (*count)++;
660
+ }
661
+
662
+ *addresses = (uv_interface_address_t*)
663
+ malloc(*count * sizeof(uv_interface_address_t));
664
+ if (!(*addresses)) {
665
+ return uv__new_artificial_error(UV_ENOMEM);
666
+ }
667
+
668
+ address = *addresses;
669
+
670
+ for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
671
+ bzero(&ip, sizeof (ip));
672
+ if (!(ent->ifa_flags & IFF_UP && ent->ifa_flags & IFF_RUNNING)) {
673
+ continue;
674
+ }
675
+
676
+ if (ent->ifa_addr == NULL) {
677
+ continue;
678
+ }
679
+
680
+ /*
681
+ * On Linux getifaddrs returns information related to the raw underlying
682
+ * devices. We're not interested in this information.
683
+ */
684
+ if (ent->ifa_addr->sa_family == PF_PACKET) {
685
+ continue;
686
+ }
687
+
688
+ address->name = strdup(ent->ifa_name);
689
+
690
+ if (ent->ifa_addr->sa_family == AF_INET6) {
691
+ address->address.address6 = *((struct sockaddr_in6 *)ent->ifa_addr);
692
+ } else {
693
+ address->address.address4 = *((struct sockaddr_in *)ent->ifa_addr);
694
+ }
695
+
696
+ address->is_internal = ent->ifa_flags & IFF_LOOPBACK ? 1 : 0;
697
+
698
+ address++;
699
+ }
700
+
701
+ freeifaddrs(addrs);
702
+
703
+ return uv_ok_;
704
+ #endif
705
+ }
706
+
707
+
708
+ void uv_free_interface_addresses(uv_interface_address_t* addresses,
709
+ int count) {
710
+ int i;
711
+
712
+ for (i = 0; i < count; i++) {
713
+ free(addresses[i].name);
714
+ }
715
+
716
+ free(addresses);
717
+ }
718
+
719
+
720
+ void uv__set_process_title(const char* title) {
721
+ #if defined(PR_SET_NAME)
722
+ prctl(PR_SET_NAME, title); /* Only copies first 16 characters. */
723
+ #endif
724
+ }