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,647 @@
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 <stdio.h>
25
+ #include <stdint.h>
26
+ #include <stdlib.h>
27
+ #include <string.h>
28
+ #include <assert.h>
29
+ #include <errno.h>
30
+
31
+ #ifndef SUNOS_NO_IFADDRS
32
+ # include <ifaddrs.h>
33
+ #endif
34
+ #include <net/if.h>
35
+
36
+ #include <sys/loadavg.h>
37
+ #include <sys/time.h>
38
+ #include <unistd.h>
39
+ #include <kstat.h>
40
+ #include <fcntl.h>
41
+
42
+ #include <sys/port.h>
43
+ #include <port.h>
44
+
45
+ #define PORT_FIRED 0x69
46
+ #define PORT_UNUSED 0x0
47
+ #define PORT_LOADED 0x99
48
+ #define PORT_DELETED -1
49
+
50
+ #if (!defined(_LP64)) && (_FILE_OFFSET_BITS - 0 == 64)
51
+ #define PROCFS_FILE_OFFSET_BITS_HACK 1
52
+ #undef _FILE_OFFSET_BITS
53
+ #else
54
+ #define PROCFS_FILE_OFFSET_BITS_HACK 0
55
+ #endif
56
+
57
+ #include <procfs.h>
58
+
59
+ #if (PROCFS_FILE_OFFSET_BITS_HACK - 0 == 1)
60
+ #define _FILE_OFFSET_BITS 64
61
+ #endif
62
+
63
+
64
+ int uv__platform_loop_init(uv_loop_t* loop, int default_loop) {
65
+ loop->fs_fd = -1;
66
+ loop->backend_fd = port_create();
67
+
68
+ if (loop->backend_fd == -1)
69
+ return -1;
70
+
71
+ uv__cloexec(loop->backend_fd, 1);
72
+
73
+ return 0;
74
+ }
75
+
76
+
77
+ void uv__platform_loop_delete(uv_loop_t* loop) {
78
+ if (loop->fs_fd != -1) {
79
+ close(loop->fs_fd);
80
+ loop->fs_fd = -1;
81
+ }
82
+
83
+ if (loop->backend_fd != -1) {
84
+ close(loop->backend_fd);
85
+ loop->backend_fd = -1;
86
+ }
87
+ }
88
+
89
+
90
+ void uv__io_poll(uv_loop_t* loop, int timeout) {
91
+ struct port_event events[1024];
92
+ struct port_event* pe;
93
+ struct timespec spec;
94
+ ngx_queue_t* q;
95
+ uv__io_t* w;
96
+ uint64_t base;
97
+ uint64_t diff;
98
+ unsigned int nfds;
99
+ unsigned int i;
100
+ int saved_errno;
101
+ int nevents;
102
+ int count;
103
+ int fd;
104
+
105
+ if (loop->nfds == 0) {
106
+ assert(ngx_queue_empty(&loop->watcher_queue));
107
+ return;
108
+ }
109
+
110
+ while (!ngx_queue_empty(&loop->watcher_queue)) {
111
+ q = ngx_queue_head(&loop->watcher_queue);
112
+ ngx_queue_remove(q);
113
+ ngx_queue_init(q);
114
+
115
+ w = ngx_queue_data(q, uv__io_t, watcher_queue);
116
+ assert(w->pevents != 0);
117
+
118
+ if (port_associate(loop->backend_fd, PORT_SOURCE_FD, w->fd, w->pevents, 0))
119
+ abort();
120
+
121
+ w->events = w->pevents;
122
+ }
123
+
124
+ assert(timeout >= -1);
125
+ base = loop->time;
126
+ count = 48; /* Benchmarks suggest this gives the best throughput. */
127
+
128
+ for (;;) {
129
+ if (timeout != -1) {
130
+ spec.tv_sec = timeout / 1000;
131
+ spec.tv_nsec = (timeout % 1000) * 1000000;
132
+ }
133
+
134
+ /* Work around a kernel bug where nfds is not updated. */
135
+ events[0].portev_source = 0;
136
+
137
+ nfds = 1;
138
+ saved_errno = 0;
139
+ if (port_getn(loop->backend_fd,
140
+ events,
141
+ ARRAY_SIZE(events),
142
+ &nfds,
143
+ timeout == -1 ? NULL : &spec)) {
144
+ /* Work around another kernel bug: port_getn() may return events even
145
+ * on error.
146
+ */
147
+ if (errno == EINTR || errno == ETIME)
148
+ saved_errno = errno;
149
+ else
150
+ abort();
151
+ }
152
+
153
+ /* Update loop->time unconditionally. It's tempting to skip the update when
154
+ * timeout == 0 (i.e. non-blocking poll) but there is no guarantee that the
155
+ * operating system didn't reschedule our process while in the syscall.
156
+ */
157
+ SAVE_ERRNO(uv__update_time(loop));
158
+
159
+ if (events[0].portev_source == 0) {
160
+ if (timeout == 0)
161
+ return;
162
+
163
+ if (timeout == -1)
164
+ continue;
165
+
166
+ goto update_timeout;
167
+ }
168
+
169
+ if (nfds == 0) {
170
+ assert(timeout != -1);
171
+ return;
172
+ }
173
+
174
+ nevents = 0;
175
+
176
+ for (i = 0; i < nfds; i++) {
177
+ pe = events + i;
178
+ fd = pe->portev_object;
179
+
180
+ assert(fd >= 0);
181
+ assert((unsigned) fd < loop->nwatchers);
182
+
183
+ w = loop->watchers[fd];
184
+
185
+ /* File descriptor that we've stopped watching, ignore. */
186
+ if (w == NULL)
187
+ continue;
188
+
189
+ w->cb(loop, w, pe->portev_events);
190
+ nevents++;
191
+
192
+ /* Events Ports operates in oneshot mode, rearm timer on next run. */
193
+ if (w->pevents != 0 && ngx_queue_empty(&w->watcher_queue))
194
+ ngx_queue_insert_tail(&loop->watcher_queue, &w->watcher_queue);
195
+ }
196
+
197
+ if (nevents != 0) {
198
+ if (nfds == ARRAY_SIZE(events) && --count != 0) {
199
+ /* Poll for more events but don't block this time. */
200
+ timeout = 0;
201
+ continue;
202
+ }
203
+ return;
204
+ }
205
+
206
+ if (saved_errno == ETIME) {
207
+ assert(timeout != -1);
208
+ return;
209
+ }
210
+
211
+ if (timeout == 0)
212
+ return;
213
+
214
+ if (timeout == -1)
215
+ continue;
216
+
217
+ update_timeout:
218
+ assert(timeout > 0);
219
+
220
+ diff = loop->time - base;
221
+ if (diff >= (uint64_t) timeout)
222
+ return;
223
+
224
+ timeout -= diff;
225
+ }
226
+ }
227
+
228
+
229
+ uint64_t uv__hrtime(void) {
230
+ return gethrtime();
231
+ }
232
+
233
+
234
+ /*
235
+ * We could use a static buffer for the path manipulations that we need outside
236
+ * of the function, but this function could be called by multiple consumers and
237
+ * we don't want to potentially create a race condition in the use of snprintf.
238
+ */
239
+ int uv_exepath(char* buffer, size_t* size) {
240
+ ssize_t res;
241
+ char buf[128];
242
+
243
+ if (buffer == NULL)
244
+ return (-1);
245
+
246
+ if (size == NULL)
247
+ return (-1);
248
+
249
+ (void) snprintf(buf, sizeof(buf), "/proc/%lu/path/a.out", (unsigned long) getpid());
250
+ res = readlink(buf, buffer, *size - 1);
251
+
252
+ if (res < 0)
253
+ return (res);
254
+
255
+ buffer[res] = '\0';
256
+ *size = res;
257
+ return (0);
258
+ }
259
+
260
+
261
+ uint64_t uv_get_free_memory(void) {
262
+ return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
263
+ }
264
+
265
+
266
+ uint64_t uv_get_total_memory(void) {
267
+ return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
268
+ }
269
+
270
+
271
+ void uv_loadavg(double avg[3]) {
272
+ (void) getloadavg(avg, 3);
273
+ }
274
+
275
+
276
+ #if defined(PORT_SOURCE_FILE)
277
+
278
+ static void uv__fs_event_rearm(uv_fs_event_t *handle) {
279
+ if (handle->fd == -1)
280
+ return;
281
+
282
+ if (port_associate(handle->loop->fs_fd,
283
+ PORT_SOURCE_FILE,
284
+ (uintptr_t) &handle->fo,
285
+ FILE_ATTRIB | FILE_MODIFIED,
286
+ handle) == -1) {
287
+ uv__set_sys_error(handle->loop, errno);
288
+ }
289
+ handle->fd = PORT_LOADED;
290
+ }
291
+
292
+
293
+ static void uv__fs_event_read(uv_loop_t* loop,
294
+ uv__io_t* w,
295
+ unsigned int revents) {
296
+ uv_fs_event_t *handle = NULL;
297
+ timespec_t timeout;
298
+ port_event_t pe;
299
+ int events;
300
+ int r;
301
+
302
+ (void) w;
303
+ (void) revents;
304
+
305
+ do {
306
+ uint_t n = 1;
307
+
308
+ /*
309
+ * Note that our use of port_getn() here (and not port_get()) is deliberate:
310
+ * there is a bug in event ports (Sun bug 6456558) whereby a zeroed timeout
311
+ * causes port_get() to return success instead of ETIME when there aren't
312
+ * actually any events (!); by using port_getn() in lieu of port_get(),
313
+ * we can at least workaround the bug by checking for zero returned events
314
+ * and treating it as we would ETIME.
315
+ */
316
+ do {
317
+ memset(&timeout, 0, sizeof timeout);
318
+ r = port_getn(loop->fs_fd, &pe, 1, &n, &timeout);
319
+ }
320
+ while (r == -1 && errno == EINTR);
321
+
322
+ if ((r == -1 && errno == ETIME) || n == 0)
323
+ break;
324
+
325
+ handle = (uv_fs_event_t *)pe.portev_user;
326
+ assert((r == 0) && "unexpected port_get() error");
327
+
328
+ events = 0;
329
+ if (pe.portev_events & (FILE_ATTRIB | FILE_MODIFIED))
330
+ events |= UV_CHANGE;
331
+ if (pe.portev_events & ~(FILE_ATTRIB | FILE_MODIFIED))
332
+ events |= UV_RENAME;
333
+ assert(events != 0);
334
+ handle->fd = PORT_FIRED;
335
+ handle->cb(handle, NULL, events, 0);
336
+ }
337
+ while (handle->fd != PORT_DELETED);
338
+
339
+ if (handle != NULL && handle->fd != PORT_DELETED)
340
+ uv__fs_event_rearm(handle);
341
+ }
342
+
343
+
344
+ int uv_fs_event_init(uv_loop_t* loop,
345
+ uv_fs_event_t* handle,
346
+ const char* filename,
347
+ uv_fs_event_cb cb,
348
+ int flags) {
349
+ int portfd;
350
+ int first_run = 0;
351
+
352
+ if (loop->fs_fd == -1) {
353
+ if ((portfd = port_create()) == -1) {
354
+ uv__set_sys_error(loop, errno);
355
+ return -1;
356
+ }
357
+ loop->fs_fd = portfd;
358
+ first_run = 1;
359
+ }
360
+
361
+ uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_EVENT);
362
+ uv__handle_start(handle); /* FIXME shouldn't start automatically */
363
+ handle->filename = strdup(filename);
364
+ handle->fd = PORT_UNUSED;
365
+ handle->cb = cb;
366
+
367
+ memset(&handle->fo, 0, sizeof handle->fo);
368
+ handle->fo.fo_name = handle->filename;
369
+ uv__fs_event_rearm(handle);
370
+
371
+ if (first_run) {
372
+ uv__io_init(&loop->fs_event_watcher, uv__fs_event_read, portfd);
373
+ uv__io_start(loop, &loop->fs_event_watcher, UV__POLLIN);
374
+ }
375
+
376
+ return 0;
377
+ }
378
+
379
+
380
+ void uv__fs_event_close(uv_fs_event_t* handle) {
381
+ if (handle->fd == PORT_FIRED || handle->fd == PORT_LOADED) {
382
+ port_dissociate(handle->loop->fs_fd, PORT_SOURCE_FILE, (uintptr_t)&handle->fo);
383
+ }
384
+ handle->fd = PORT_DELETED;
385
+ free(handle->filename);
386
+ handle->filename = NULL;
387
+ handle->fo.fo_name = NULL;
388
+ uv__handle_stop(handle);
389
+ }
390
+
391
+ #else /* !defined(PORT_SOURCE_FILE) */
392
+
393
+ int uv_fs_event_init(uv_loop_t* loop,
394
+ uv_fs_event_t* handle,
395
+ const char* filename,
396
+ uv_fs_event_cb cb,
397
+ int flags) {
398
+ uv__set_sys_error(loop, ENOSYS);
399
+ return -1;
400
+ }
401
+
402
+
403
+ void uv__fs_event_close(uv_fs_event_t* handle) {
404
+ UNREACHABLE();
405
+ }
406
+
407
+ #endif /* defined(PORT_SOURCE_FILE) */
408
+
409
+
410
+ char** uv_setup_args(int argc, char** argv) {
411
+ return argv;
412
+ }
413
+
414
+
415
+ uv_err_t uv_set_process_title(const char* title) {
416
+ return uv_ok_;
417
+ }
418
+
419
+
420
+ uv_err_t uv_get_process_title(char* buffer, size_t size) {
421
+ if (size > 0) {
422
+ buffer[0] = '\0';
423
+ }
424
+ return uv_ok_;
425
+ }
426
+
427
+
428
+ uv_err_t uv_resident_set_memory(size_t* rss) {
429
+ psinfo_t psinfo;
430
+ uv_err_t err;
431
+ int fd;
432
+
433
+ fd = open("/proc/self/psinfo", O_RDONLY);
434
+ if (fd == -1)
435
+ return uv__new_sys_error(errno);
436
+
437
+ err = uv_ok_;
438
+
439
+ if (read(fd, &psinfo, sizeof(psinfo)) == sizeof(psinfo))
440
+ *rss = (size_t)psinfo.pr_rssize * 1024;
441
+ else
442
+ err = uv__new_sys_error(EINVAL);
443
+
444
+ close(fd);
445
+
446
+ return err;
447
+ }
448
+
449
+
450
+ uv_err_t uv_uptime(double* uptime) {
451
+ kstat_ctl_t *kc;
452
+ kstat_t *ksp;
453
+ kstat_named_t *knp;
454
+
455
+ long hz = sysconf(_SC_CLK_TCK);
456
+
457
+ if ((kc = kstat_open()) == NULL)
458
+ return uv__new_sys_error(errno);
459
+
460
+ ksp = kstat_lookup(kc, (char *)"unix", 0, (char *)"system_misc");
461
+
462
+ if (kstat_read(kc, ksp, NULL) == -1) {
463
+ *uptime = -1;
464
+ } else {
465
+ knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"clk_intr");
466
+ *uptime = knp->value.ul / hz;
467
+ }
468
+
469
+ kstat_close(kc);
470
+
471
+ return uv_ok_;
472
+ }
473
+
474
+
475
+ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
476
+ int lookup_instance;
477
+ kstat_ctl_t *kc;
478
+ kstat_t *ksp;
479
+ kstat_named_t *knp;
480
+ uv_cpu_info_t* cpu_info;
481
+
482
+ if ((kc = kstat_open()) == NULL) {
483
+ return uv__new_sys_error(errno);
484
+ }
485
+
486
+ /* Get count of cpus */
487
+ lookup_instance = 0;
488
+ while ((ksp = kstat_lookup(kc, (char *)"cpu_info", lookup_instance, NULL))) {
489
+ lookup_instance++;
490
+ }
491
+
492
+ *cpu_infos = (uv_cpu_info_t*)
493
+ malloc(lookup_instance * sizeof(uv_cpu_info_t));
494
+ if (!(*cpu_infos)) {
495
+ return uv__new_artificial_error(UV_ENOMEM);
496
+ }
497
+
498
+ *count = lookup_instance;
499
+
500
+ cpu_info = *cpu_infos;
501
+ lookup_instance = 0;
502
+ while ((ksp = kstat_lookup(kc, (char *)"cpu_info", lookup_instance, NULL))) {
503
+ if (kstat_read(kc, ksp, NULL) == -1) {
504
+ cpu_info->speed = 0;
505
+ cpu_info->model = NULL;
506
+ } else {
507
+ knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"clock_MHz");
508
+ assert(knp->data_type == KSTAT_DATA_INT32 ||
509
+ knp->data_type == KSTAT_DATA_INT64);
510
+ cpu_info->speed = (knp->data_type == KSTAT_DATA_INT32) ? knp->value.i32
511
+ : knp->value.i64;
512
+
513
+ knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"brand");
514
+ assert(knp->data_type == KSTAT_DATA_STRING);
515
+ cpu_info->model = strdup(KSTAT_NAMED_STR_PTR(knp));
516
+ }
517
+
518
+ lookup_instance++;
519
+ cpu_info++;
520
+ }
521
+
522
+ cpu_info = *cpu_infos;
523
+ lookup_instance = 0;
524
+ while ((ksp = kstat_lookup(kc, (char *)"cpu", lookup_instance, (char *)"sys"))){
525
+
526
+ if (kstat_read(kc, ksp, NULL) == -1) {
527
+ cpu_info->cpu_times.user = 0;
528
+ cpu_info->cpu_times.nice = 0;
529
+ cpu_info->cpu_times.sys = 0;
530
+ cpu_info->cpu_times.idle = 0;
531
+ cpu_info->cpu_times.irq = 0;
532
+ } else {
533
+ knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_user");
534
+ assert(knp->data_type == KSTAT_DATA_UINT64);
535
+ cpu_info->cpu_times.user = knp->value.ui64;
536
+
537
+ knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_kernel");
538
+ assert(knp->data_type == KSTAT_DATA_UINT64);
539
+ cpu_info->cpu_times.sys = knp->value.ui64;
540
+
541
+ knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_idle");
542
+ assert(knp->data_type == KSTAT_DATA_UINT64);
543
+ cpu_info->cpu_times.idle = knp->value.ui64;
544
+
545
+ knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"intr");
546
+ assert(knp->data_type == KSTAT_DATA_UINT64);
547
+ cpu_info->cpu_times.irq = knp->value.ui64;
548
+ cpu_info->cpu_times.nice = 0;
549
+ }
550
+
551
+ lookup_instance++;
552
+ cpu_info++;
553
+ }
554
+
555
+ kstat_close(kc);
556
+
557
+ return uv_ok_;
558
+ }
559
+
560
+
561
+ void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
562
+ int i;
563
+
564
+ for (i = 0; i < count; i++) {
565
+ free(cpu_infos[i].model);
566
+ }
567
+
568
+ free(cpu_infos);
569
+ }
570
+
571
+
572
+ uv_err_t uv_interface_addresses(uv_interface_address_t** addresses,
573
+ int* count) {
574
+ #ifdef SUNOS_NO_IFADDRS
575
+ return uv__new_artificial_error(UV_ENOSYS);
576
+ #else
577
+ struct ifaddrs *addrs, *ent;
578
+ char ip[INET6_ADDRSTRLEN];
579
+ uv_interface_address_t* address;
580
+
581
+ if (getifaddrs(&addrs) != 0) {
582
+ return uv__new_sys_error(errno);
583
+ }
584
+
585
+ *count = 0;
586
+
587
+ /* Count the number of interfaces */
588
+ for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
589
+ if (!(ent->ifa_flags & IFF_UP && ent->ifa_flags & IFF_RUNNING) ||
590
+ (ent->ifa_addr == NULL) ||
591
+ (ent->ifa_addr->sa_family == PF_PACKET)) {
592
+ continue;
593
+ }
594
+
595
+ (*count)++;
596
+ }
597
+
598
+ *addresses = (uv_interface_address_t*)
599
+ malloc(*count * sizeof(uv_interface_address_t));
600
+ if (!(*addresses)) {
601
+ return uv__new_artificial_error(UV_ENOMEM);
602
+ }
603
+
604
+ address = *addresses;
605
+
606
+ for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
607
+ memset(&ip, 0, sizeof(ip));
608
+
609
+ if (!(ent->ifa_flags & IFF_UP && ent->ifa_flags & IFF_RUNNING)) {
610
+ continue;
611
+ }
612
+
613
+ if (ent->ifa_addr == NULL) {
614
+ continue;
615
+ }
616
+
617
+ address->name = strdup(ent->ifa_name);
618
+
619
+ if (ent->ifa_addr->sa_family == AF_INET6) {
620
+ address->address.address6 = *((struct sockaddr_in6 *)ent->ifa_addr);
621
+ } else {
622
+ address->address.address4 = *((struct sockaddr_in *)ent->ifa_addr);
623
+ }
624
+
625
+ address->is_internal = ent->ifa_flags & IFF_PRIVATE || ent->ifa_flags &
626
+ IFF_LOOPBACK ? 1 : 0;
627
+
628
+ address++;
629
+ }
630
+
631
+ freeifaddrs(addrs);
632
+
633
+ return uv_ok_;
634
+ #endif /* SUNOS_NO_IFADDRS */
635
+ }
636
+
637
+
638
+ void uv_free_interface_addresses(uv_interface_address_t* addresses,
639
+ int count) {
640
+ int i;
641
+
642
+ for (i = 0; i < count; i++) {
643
+ free(addresses[i].name);
644
+ }
645
+
646
+ free(addresses);
647
+ }