rbuv 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +0 -1
- data/README.md +6 -1
- data/Rakefile +42 -0
- data/deps/libuv/.gitignore +34 -0
- data/deps/libuv/.mailmap +16 -0
- data/deps/libuv/AUTHORS +81 -0
- data/deps/libuv/ChangeLog +45 -0
- data/deps/libuv/LICENSE +41 -0
- data/deps/libuv/Makefile +53 -0
- data/deps/libuv/README.md +118 -0
- data/deps/libuv/build.mk +164 -0
- data/deps/libuv/checksparse.sh +230 -0
- data/deps/libuv/common.gypi +197 -0
- data/deps/libuv/config-mingw.mk +48 -0
- data/deps/libuv/config-unix.mk +167 -0
- data/deps/libuv/gyp_uv +98 -0
- data/deps/libuv/include/uv-private/ngx-queue.h +129 -0
- data/deps/libuv/include/uv-private/stdint-msvc2008.h +247 -0
- data/deps/libuv/include/uv-private/tree.h +768 -0
- data/deps/libuv/include/uv-private/uv-bsd.h +34 -0
- data/deps/libuv/include/uv-private/uv-darwin.h +61 -0
- data/deps/libuv/include/uv-private/uv-linux.h +34 -0
- data/deps/libuv/include/uv-private/uv-sunos.h +44 -0
- data/deps/libuv/include/uv-private/uv-unix.h +332 -0
- data/deps/libuv/include/uv-private/uv-win.h +585 -0
- data/deps/libuv/include/uv.h +1987 -0
- data/deps/libuv/src/fs-poll.c +248 -0
- data/deps/libuv/src/inet.c +298 -0
- data/deps/libuv/src/unix/aix.c +393 -0
- data/deps/libuv/src/unix/async.c +281 -0
- data/deps/libuv/src/unix/core.c +714 -0
- data/deps/libuv/src/unix/cygwin.c +93 -0
- data/deps/libuv/src/unix/darwin-proctitle.m +78 -0
- data/deps/libuv/src/unix/darwin.c +431 -0
- data/deps/libuv/src/unix/dl.c +83 -0
- data/deps/libuv/src/unix/error.c +109 -0
- data/deps/libuv/src/unix/freebsd.c +343 -0
- data/deps/libuv/src/unix/fs.c +869 -0
- data/deps/libuv/src/unix/fsevents.c +299 -0
- data/deps/libuv/src/unix/getaddrinfo.c +159 -0
- data/deps/libuv/src/unix/internal.h +259 -0
- data/deps/libuv/src/unix/kqueue.c +347 -0
- data/deps/libuv/src/unix/linux-core.c +724 -0
- data/deps/libuv/src/unix/linux-inotify.c +236 -0
- data/deps/libuv/src/unix/linux-syscalls.c +388 -0
- data/deps/libuv/src/unix/linux-syscalls.h +150 -0
- data/deps/libuv/src/unix/loop-watcher.c +64 -0
- data/deps/libuv/src/unix/loop.c +114 -0
- data/deps/libuv/src/unix/netbsd.c +353 -0
- data/deps/libuv/src/unix/openbsd.c +304 -0
- data/deps/libuv/src/unix/pipe.c +261 -0
- data/deps/libuv/src/unix/poll.c +108 -0
- data/deps/libuv/src/unix/process.c +501 -0
- data/deps/libuv/src/unix/proctitle.c +103 -0
- data/deps/libuv/src/unix/signal.c +455 -0
- data/deps/libuv/src/unix/stream.c +1380 -0
- data/deps/libuv/src/unix/sunos.c +647 -0
- data/deps/libuv/src/unix/tcp.c +357 -0
- data/deps/libuv/src/unix/thread.c +431 -0
- data/deps/libuv/src/unix/threadpool.c +286 -0
- data/deps/libuv/src/unix/timer.c +153 -0
- data/deps/libuv/src/unix/tty.c +179 -0
- data/deps/libuv/src/unix/udp.c +715 -0
- data/deps/libuv/src/uv-common.c +431 -0
- data/deps/libuv/src/uv-common.h +204 -0
- data/deps/libuv/src/version.c +60 -0
- data/deps/libuv/src/win/async.c +99 -0
- data/deps/libuv/src/win/atomicops-inl.h +56 -0
- data/deps/libuv/src/win/core.c +310 -0
- data/deps/libuv/src/win/dl.c +86 -0
- data/deps/libuv/src/win/error.c +164 -0
- data/deps/libuv/src/win/fs-event.c +506 -0
- data/deps/libuv/src/win/fs.c +1951 -0
- data/deps/libuv/src/win/getaddrinfo.c +365 -0
- data/deps/libuv/src/win/handle-inl.h +164 -0
- data/deps/libuv/src/win/handle.c +153 -0
- data/deps/libuv/src/win/internal.h +346 -0
- data/deps/libuv/src/win/loop-watcher.c +124 -0
- data/deps/libuv/src/win/pipe.c +1656 -0
- data/deps/libuv/src/win/poll.c +615 -0
- data/deps/libuv/src/win/process-stdio.c +503 -0
- data/deps/libuv/src/win/process.c +1048 -0
- data/deps/libuv/src/win/req-inl.h +224 -0
- data/deps/libuv/src/win/req.c +25 -0
- data/deps/libuv/src/win/signal.c +354 -0
- data/deps/libuv/src/win/stream-inl.h +67 -0
- data/deps/libuv/src/win/stream.c +198 -0
- data/deps/libuv/src/win/tcp.c +1422 -0
- data/deps/libuv/src/win/thread.c +666 -0
- data/deps/libuv/src/win/threadpool.c +82 -0
- data/deps/libuv/src/win/timer.c +230 -0
- data/deps/libuv/src/win/tty.c +1857 -0
- data/deps/libuv/src/win/udp.c +744 -0
- data/deps/libuv/src/win/util.c +946 -0
- data/deps/libuv/src/win/winapi.c +152 -0
- data/deps/libuv/src/win/winapi.h +4476 -0
- data/deps/libuv/src/win/winsock.c +560 -0
- data/deps/libuv/src/win/winsock.h +171 -0
- data/deps/libuv/test/benchmark-async-pummel.c +119 -0
- data/deps/libuv/test/benchmark-async.c +139 -0
- data/deps/libuv/test/benchmark-fs-stat.c +136 -0
- data/deps/libuv/test/benchmark-getaddrinfo.c +91 -0
- data/deps/libuv/test/benchmark-list.h +163 -0
- data/deps/libuv/test/benchmark-loop-count.c +90 -0
- data/deps/libuv/test/benchmark-million-async.c +112 -0
- data/deps/libuv/test/benchmark-million-timers.c +77 -0
- data/deps/libuv/test/benchmark-multi-accept.c +432 -0
- data/deps/libuv/test/benchmark-ping-pongs.c +212 -0
- data/deps/libuv/test/benchmark-pound.c +325 -0
- data/deps/libuv/test/benchmark-pump.c +459 -0
- data/deps/libuv/test/benchmark-sizes.c +45 -0
- data/deps/libuv/test/benchmark-spawn.c +163 -0
- data/deps/libuv/test/benchmark-tcp-write-batch.c +141 -0
- data/deps/libuv/test/benchmark-thread.c +64 -0
- data/deps/libuv/test/benchmark-udp-pummel.c +238 -0
- data/deps/libuv/test/blackhole-server.c +118 -0
- data/deps/libuv/test/dns-server.c +329 -0
- data/deps/libuv/test/echo-server.c +384 -0
- data/deps/libuv/test/fixtures/empty_file +0 -0
- data/deps/libuv/test/fixtures/load_error.node +1 -0
- data/deps/libuv/test/run-benchmarks.c +64 -0
- data/deps/libuv/test/run-tests.c +159 -0
- data/deps/libuv/test/runner-unix.c +328 -0
- data/deps/libuv/test/runner-unix.h +36 -0
- data/deps/libuv/test/runner-win.c +318 -0
- data/deps/libuv/test/runner-win.h +43 -0
- data/deps/libuv/test/runner.c +394 -0
- data/deps/libuv/test/runner.h +165 -0
- data/deps/libuv/test/task.h +122 -0
- data/deps/libuv/test/test-active.c +83 -0
- data/deps/libuv/test/test-async.c +136 -0
- data/deps/libuv/test/test-barrier.c +98 -0
- data/deps/libuv/test/test-callback-order.c +77 -0
- data/deps/libuv/test/test-callback-stack.c +204 -0
- data/deps/libuv/test/test-condvar.c +173 -0
- data/deps/libuv/test/test-connection-fail.c +150 -0
- data/deps/libuv/test/test-cwd-and-chdir.c +64 -0
- data/deps/libuv/test/test-delayed-accept.c +189 -0
- data/deps/libuv/test/test-dlerror.c +58 -0
- data/deps/libuv/test/test-embed.c +136 -0
- data/deps/libuv/test/test-error.c +59 -0
- data/deps/libuv/test/test-fail-always.c +29 -0
- data/deps/libuv/test/test-fs-event.c +504 -0
- data/deps/libuv/test/test-fs-poll.c +148 -0
- data/deps/libuv/test/test-fs.c +1899 -0
- data/deps/libuv/test/test-get-currentexe.c +63 -0
- data/deps/libuv/test/test-get-loadavg.c +36 -0
- data/deps/libuv/test/test-get-memory.c +38 -0
- data/deps/libuv/test/test-getaddrinfo.c +120 -0
- data/deps/libuv/test/test-getsockname.c +344 -0
- data/deps/libuv/test/test-hrtime.c +54 -0
- data/deps/libuv/test/test-idle.c +82 -0
- data/deps/libuv/test/test-ipc-send-recv.c +218 -0
- data/deps/libuv/test/test-ipc.c +625 -0
- data/deps/libuv/test/test-list.h +492 -0
- data/deps/libuv/test/test-loop-handles.c +337 -0
- data/deps/libuv/test/test-loop-stop.c +73 -0
- data/deps/libuv/test/test-multiple-listen.c +103 -0
- data/deps/libuv/test/test-mutexes.c +63 -0
- data/deps/libuv/test/test-pass-always.c +28 -0
- data/deps/libuv/test/test-ping-pong.c +250 -0
- data/deps/libuv/test/test-pipe-bind-error.c +144 -0
- data/deps/libuv/test/test-pipe-connect-error.c +98 -0
- data/deps/libuv/test/test-platform-output.c +87 -0
- data/deps/libuv/test/test-poll-close.c +73 -0
- data/deps/libuv/test/test-poll.c +575 -0
- data/deps/libuv/test/test-process-title.c +49 -0
- data/deps/libuv/test/test-ref.c +415 -0
- data/deps/libuv/test/test-run-nowait.c +46 -0
- data/deps/libuv/test/test-run-once.c +49 -0
- data/deps/libuv/test/test-semaphore.c +111 -0
- data/deps/libuv/test/test-shutdown-close.c +105 -0
- data/deps/libuv/test/test-shutdown-eof.c +184 -0
- data/deps/libuv/test/test-signal-multiple-loops.c +270 -0
- data/deps/libuv/test/test-signal.c +152 -0
- data/deps/libuv/test/test-spawn.c +938 -0
- data/deps/libuv/test/test-stdio-over-pipes.c +250 -0
- data/deps/libuv/test/test-tcp-bind-error.c +198 -0
- data/deps/libuv/test/test-tcp-bind6-error.c +159 -0
- data/deps/libuv/test/test-tcp-close-while-connecting.c +81 -0
- data/deps/libuv/test/test-tcp-close.c +130 -0
- data/deps/libuv/test/test-tcp-connect-error-after-write.c +96 -0
- data/deps/libuv/test/test-tcp-connect-error.c +71 -0
- data/deps/libuv/test/test-tcp-connect-timeout.c +86 -0
- data/deps/libuv/test/test-tcp-connect6-error.c +69 -0
- data/deps/libuv/test/test-tcp-flags.c +52 -0
- data/deps/libuv/test/test-tcp-open.c +175 -0
- data/deps/libuv/test/test-tcp-read-stop.c +73 -0
- data/deps/libuv/test/test-tcp-shutdown-after-write.c +132 -0
- data/deps/libuv/test/test-tcp-unexpected-read.c +114 -0
- data/deps/libuv/test/test-tcp-write-to-half-open-connection.c +136 -0
- data/deps/libuv/test/test-tcp-writealot.c +171 -0
- data/deps/libuv/test/test-thread.c +183 -0
- data/deps/libuv/test/test-threadpool-cancel.c +311 -0
- data/deps/libuv/test/test-threadpool.c +77 -0
- data/deps/libuv/test/test-timer-again.c +142 -0
- data/deps/libuv/test/test-timer.c +266 -0
- data/deps/libuv/test/test-tty.c +111 -0
- data/deps/libuv/test/test-udp-dgram-too-big.c +87 -0
- data/deps/libuv/test/test-udp-ipv6.c +158 -0
- data/deps/libuv/test/test-udp-multicast-join.c +140 -0
- data/deps/libuv/test/test-udp-multicast-ttl.c +87 -0
- data/deps/libuv/test/test-udp-open.c +154 -0
- data/deps/libuv/test/test-udp-options.c +87 -0
- data/deps/libuv/test/test-udp-send-and-recv.c +210 -0
- data/deps/libuv/test/test-util.c +97 -0
- data/deps/libuv/test/test-walk-handles.c +78 -0
- data/deps/libuv/uv.gyp +431 -0
- data/deps/libuv/vcbuild.bat +128 -0
- data/ext/rbuv/debug.h +27 -0
- data/ext/rbuv/error.c +7 -0
- data/ext/rbuv/error.h +10 -0
- data/ext/rbuv/extconf.rb +35 -0
- data/ext/rbuv/handle.c +40 -0
- data/ext/rbuv/handle.h +14 -0
- data/ext/rbuv/libuv.mk +12 -0
- data/ext/rbuv/loop.c +50 -0
- data/ext/rbuv/loop.h +13 -0
- data/ext/rbuv/rbuv.c +15 -0
- data/ext/rbuv/rbuv.h +27 -0
- data/ext/rbuv/timer.c +133 -0
- data/ext/rbuv/timer.h +13 -0
- data/lib/rbuv/timer.rb +7 -0
- data/lib/rbuv/version.rb +1 -1
- data/lib/rbuv.rb +24 -2
- data/rbuv.gemspec +5 -1
- data/spec/spec_helper.rb +22 -0
- data/spec/timer_spec.rb +144 -0
- 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
|
+
}
|