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,109 @@
|
|
1
|
+
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
2
|
+
*
|
3
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
* of this software and associated documentation files (the "Software"), to
|
5
|
+
* deal in the Software without restriction, including without limitation the
|
6
|
+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
* sell copies of the Software, and to permit persons to whom the Software is
|
8
|
+
* furnished to do so, subject to the following conditions:
|
9
|
+
*
|
10
|
+
* The above copyright notice and this permission notice shall be included in
|
11
|
+
* all copies or substantial portions of the Software.
|
12
|
+
*
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
18
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
19
|
+
* IN THE SOFTWARE.
|
20
|
+
*/
|
21
|
+
|
22
|
+
/*
|
23
|
+
* TODO Share this code with Windows.
|
24
|
+
* See https://github.com/joyent/libuv/issues/76
|
25
|
+
*/
|
26
|
+
|
27
|
+
#include "uv.h"
|
28
|
+
#include "internal.h"
|
29
|
+
|
30
|
+
#include <errno.h>
|
31
|
+
#include <stdio.h>
|
32
|
+
#include <stdlib.h>
|
33
|
+
#include <string.h>
|
34
|
+
#include <assert.h>
|
35
|
+
|
36
|
+
|
37
|
+
/* TODO Expose callback to user to handle fatal error like V8 does. */
|
38
|
+
void uv_fatal_error(const int errorno, const char* syscall) {
|
39
|
+
char* buf = NULL;
|
40
|
+
const char* errmsg;
|
41
|
+
|
42
|
+
if (buf) {
|
43
|
+
errmsg = buf;
|
44
|
+
} else {
|
45
|
+
errmsg = "Unknown error";
|
46
|
+
}
|
47
|
+
|
48
|
+
if (syscall) {
|
49
|
+
fprintf(stderr, "\nlibuv fatal error. %s: (%d) %s\n", syscall, errorno,
|
50
|
+
errmsg);
|
51
|
+
} else {
|
52
|
+
fprintf(stderr, "\nlibuv fatal error. (%d) %s\n", errorno, errmsg);
|
53
|
+
}
|
54
|
+
|
55
|
+
abort();
|
56
|
+
}
|
57
|
+
|
58
|
+
|
59
|
+
uv_err_code uv_translate_sys_error(int sys_errno) {
|
60
|
+
switch (sys_errno) {
|
61
|
+
case 0: return UV_OK;
|
62
|
+
case EIO: return UV_EIO;
|
63
|
+
case EPERM: return UV_EPERM;
|
64
|
+
case ENOSYS: return UV_ENOSYS;
|
65
|
+
case ENOTSOCK: return UV_ENOTSOCK;
|
66
|
+
case ENOENT: return UV_ENOENT;
|
67
|
+
case EACCES: return UV_EACCES;
|
68
|
+
case EAFNOSUPPORT: return UV_EAFNOSUPPORT;
|
69
|
+
case EBADF: return UV_EBADF;
|
70
|
+
case EPIPE: return UV_EPIPE;
|
71
|
+
case ESPIPE: return UV_ESPIPE;
|
72
|
+
case EAGAIN: return UV_EAGAIN;
|
73
|
+
#if EWOULDBLOCK != EAGAIN
|
74
|
+
case EWOULDBLOCK: return UV_EAGAIN;
|
75
|
+
#endif
|
76
|
+
case ECONNRESET: return UV_ECONNRESET;
|
77
|
+
case EFAULT: return UV_EFAULT;
|
78
|
+
case EMFILE: return UV_EMFILE;
|
79
|
+
case EMSGSIZE: return UV_EMSGSIZE;
|
80
|
+
case ENAMETOOLONG: return UV_ENAMETOOLONG;
|
81
|
+
case EINVAL: return UV_EINVAL;
|
82
|
+
case ENETUNREACH: return UV_ENETUNREACH;
|
83
|
+
case ECONNABORTED: return UV_ECONNABORTED;
|
84
|
+
case ELOOP: return UV_ELOOP;
|
85
|
+
case ECONNREFUSED: return UV_ECONNREFUSED;
|
86
|
+
case EADDRINUSE: return UV_EADDRINUSE;
|
87
|
+
case EADDRNOTAVAIL: return UV_EADDRNOTAVAIL;
|
88
|
+
case ENOTDIR: return UV_ENOTDIR;
|
89
|
+
case EISDIR: return UV_EISDIR;
|
90
|
+
case ENODEV: return UV_ENODEV;
|
91
|
+
case ENOTCONN: return UV_ENOTCONN;
|
92
|
+
case EEXIST: return UV_EEXIST;
|
93
|
+
case EHOSTUNREACH: return UV_EHOSTUNREACH;
|
94
|
+
case EAI_NONAME: return UV_ENOENT;
|
95
|
+
case ESRCH: return UV_ESRCH;
|
96
|
+
case ETIMEDOUT: return UV_ETIMEDOUT;
|
97
|
+
case EXDEV: return UV_EXDEV;
|
98
|
+
case EBUSY: return UV_EBUSY;
|
99
|
+
#if ENOTEMPTY != EEXIST
|
100
|
+
case ENOTEMPTY: return UV_ENOTEMPTY;
|
101
|
+
#endif
|
102
|
+
case ENOSPC: return UV_ENOSPC;
|
103
|
+
case EROFS: return UV_EROFS;
|
104
|
+
case ENOMEM: return UV_ENOMEM;
|
105
|
+
case EDQUOT: return UV_ENOSPC;
|
106
|
+
default: return UV_UNKNOWN;
|
107
|
+
}
|
108
|
+
UNREACHABLE();
|
109
|
+
}
|
@@ -0,0 +1,343 @@
|
|
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 <assert.h>
|
25
|
+
#include <string.h>
|
26
|
+
#include <errno.h>
|
27
|
+
|
28
|
+
#include <kvm.h>
|
29
|
+
#include <paths.h>
|
30
|
+
#include <sys/user.h>
|
31
|
+
#include <sys/types.h>
|
32
|
+
#include <sys/resource.h>
|
33
|
+
#include <sys/sysctl.h>
|
34
|
+
#include <vm/vm_param.h> /* VM_LOADAVG */
|
35
|
+
#include <time.h>
|
36
|
+
#include <stdlib.h>
|
37
|
+
#include <unistd.h> /* sysconf */
|
38
|
+
#include <fcntl.h>
|
39
|
+
|
40
|
+
#undef NANOSEC
|
41
|
+
#define NANOSEC ((uint64_t) 1e9)
|
42
|
+
|
43
|
+
#ifndef CPUSTATES
|
44
|
+
# define CPUSTATES 5U
|
45
|
+
#endif
|
46
|
+
#ifndef CP_USER
|
47
|
+
# define CP_USER 0
|
48
|
+
# define CP_NICE 1
|
49
|
+
# define CP_SYS 2
|
50
|
+
# define CP_IDLE 3
|
51
|
+
# define CP_INTR 4
|
52
|
+
#endif
|
53
|
+
|
54
|
+
static char *process_title;
|
55
|
+
|
56
|
+
|
57
|
+
int uv__platform_loop_init(uv_loop_t* loop, int default_loop) {
|
58
|
+
return uv__kqueue_init(loop);
|
59
|
+
}
|
60
|
+
|
61
|
+
|
62
|
+
void uv__platform_loop_delete(uv_loop_t* loop) {
|
63
|
+
}
|
64
|
+
|
65
|
+
|
66
|
+
uint64_t uv__hrtime(void) {
|
67
|
+
struct timespec ts;
|
68
|
+
clock_gettime(CLOCK_MONOTONIC, &ts);
|
69
|
+
return (((uint64_t) ts.tv_sec) * NANOSEC + ts.tv_nsec);
|
70
|
+
}
|
71
|
+
|
72
|
+
|
73
|
+
int uv_exepath(char* buffer, size_t* size) {
|
74
|
+
int mib[4];
|
75
|
+
size_t cb;
|
76
|
+
|
77
|
+
if (!buffer || !size) {
|
78
|
+
return -1;
|
79
|
+
}
|
80
|
+
|
81
|
+
#ifdef __DragonFly__
|
82
|
+
mib[0] = CTL_KERN;
|
83
|
+
mib[1] = KERN_PROC;
|
84
|
+
mib[2] = KERN_PROC_ARGS;
|
85
|
+
mib[3] = getpid();
|
86
|
+
#else
|
87
|
+
mib[0] = CTL_KERN;
|
88
|
+
mib[1] = KERN_PROC;
|
89
|
+
mib[2] = KERN_PROC_PATHNAME;
|
90
|
+
mib[3] = -1;
|
91
|
+
#endif
|
92
|
+
|
93
|
+
cb = *size;
|
94
|
+
if (sysctl(mib, 4, buffer, &cb, NULL, 0) < 0) {
|
95
|
+
*size = 0;
|
96
|
+
return -1;
|
97
|
+
}
|
98
|
+
*size = strlen(buffer);
|
99
|
+
|
100
|
+
return 0;
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
uint64_t uv_get_free_memory(void) {
|
105
|
+
int freecount;
|
106
|
+
size_t size = sizeof(freecount);
|
107
|
+
|
108
|
+
if(sysctlbyname("vm.stats.vm.v_free_count",
|
109
|
+
&freecount, &size, NULL, 0) == -1){
|
110
|
+
return -1;
|
111
|
+
}
|
112
|
+
return (uint64_t) freecount * sysconf(_SC_PAGESIZE);
|
113
|
+
|
114
|
+
}
|
115
|
+
|
116
|
+
|
117
|
+
uint64_t uv_get_total_memory(void) {
|
118
|
+
unsigned long info;
|
119
|
+
int which[] = {CTL_HW, HW_PHYSMEM};
|
120
|
+
|
121
|
+
size_t size = sizeof(info);
|
122
|
+
|
123
|
+
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
|
124
|
+
return -1;
|
125
|
+
}
|
126
|
+
|
127
|
+
return (uint64_t) info;
|
128
|
+
}
|
129
|
+
|
130
|
+
|
131
|
+
void uv_loadavg(double avg[3]) {
|
132
|
+
struct loadavg info;
|
133
|
+
size_t size = sizeof(info);
|
134
|
+
int which[] = {CTL_VM, VM_LOADAVG};
|
135
|
+
|
136
|
+
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) return;
|
137
|
+
|
138
|
+
avg[0] = (double) info.ldavg[0] / info.fscale;
|
139
|
+
avg[1] = (double) info.ldavg[1] / info.fscale;
|
140
|
+
avg[2] = (double) info.ldavg[2] / info.fscale;
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
char** uv_setup_args(int argc, char** argv) {
|
145
|
+
process_title = argc ? strdup(argv[0]) : NULL;
|
146
|
+
return argv;
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
uv_err_t uv_set_process_title(const char* title) {
|
151
|
+
int oid[4];
|
152
|
+
|
153
|
+
if (process_title) free(process_title);
|
154
|
+
process_title = strdup(title);
|
155
|
+
|
156
|
+
oid[0] = CTL_KERN;
|
157
|
+
oid[1] = KERN_PROC;
|
158
|
+
oid[2] = KERN_PROC_ARGS;
|
159
|
+
oid[3] = getpid();
|
160
|
+
|
161
|
+
sysctl(oid,
|
162
|
+
ARRAY_SIZE(oid),
|
163
|
+
NULL,
|
164
|
+
NULL,
|
165
|
+
process_title,
|
166
|
+
strlen(process_title) + 1);
|
167
|
+
|
168
|
+
return uv_ok_;
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
uv_err_t uv_get_process_title(char* buffer, size_t size) {
|
173
|
+
if (process_title) {
|
174
|
+
strncpy(buffer, process_title, size);
|
175
|
+
} else {
|
176
|
+
if (size > 0) {
|
177
|
+
buffer[0] = '\0';
|
178
|
+
}
|
179
|
+
}
|
180
|
+
|
181
|
+
return uv_ok_;
|
182
|
+
}
|
183
|
+
|
184
|
+
|
185
|
+
uv_err_t uv_resident_set_memory(size_t* rss) {
|
186
|
+
kvm_t *kd = NULL;
|
187
|
+
struct kinfo_proc *kinfo = NULL;
|
188
|
+
pid_t pid;
|
189
|
+
int nprocs;
|
190
|
+
size_t page_size = getpagesize();
|
191
|
+
|
192
|
+
pid = getpid();
|
193
|
+
|
194
|
+
kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
|
195
|
+
if (kd == NULL) goto error;
|
196
|
+
|
197
|
+
kinfo = kvm_getprocs(kd, KERN_PROC_PID, pid, &nprocs);
|
198
|
+
if (kinfo == NULL) goto error;
|
199
|
+
|
200
|
+
#ifdef __DragonFly__
|
201
|
+
*rss = kinfo->kp_vm_rssize * page_size;
|
202
|
+
#else
|
203
|
+
*rss = kinfo->ki_rssize * page_size;
|
204
|
+
#endif
|
205
|
+
|
206
|
+
kvm_close(kd);
|
207
|
+
|
208
|
+
return uv_ok_;
|
209
|
+
|
210
|
+
error:
|
211
|
+
if (kd) kvm_close(kd);
|
212
|
+
return uv__new_sys_error(errno);
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
uv_err_t uv_uptime(double* uptime) {
|
217
|
+
time_t now;
|
218
|
+
struct timeval info;
|
219
|
+
size_t size = sizeof(info);
|
220
|
+
static int which[] = {CTL_KERN, KERN_BOOTTIME};
|
221
|
+
|
222
|
+
if (sysctl(which, 2, &info, &size, NULL, 0) < 0) {
|
223
|
+
return uv__new_sys_error(errno);
|
224
|
+
}
|
225
|
+
|
226
|
+
now = time(NULL);
|
227
|
+
|
228
|
+
*uptime = (double)(now - info.tv_sec);
|
229
|
+
return uv_ok_;
|
230
|
+
}
|
231
|
+
|
232
|
+
|
233
|
+
uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
|
234
|
+
unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
|
235
|
+
multiplier = ((uint64_t)1000L / ticks), cpuspeed, maxcpus,
|
236
|
+
cur = 0;
|
237
|
+
uv_cpu_info_t* cpu_info;
|
238
|
+
const char* maxcpus_key;
|
239
|
+
const char* cptimes_key;
|
240
|
+
char model[512];
|
241
|
+
long* cp_times;
|
242
|
+
int numcpus;
|
243
|
+
size_t size;
|
244
|
+
int i;
|
245
|
+
|
246
|
+
#if defined(__DragonFly__)
|
247
|
+
/* This is not quite correct but DragonFlyBSD doesn't seem to have anything
|
248
|
+
* comparable to kern.smp.maxcpus or kern.cp_times (kern.cp_time is a total,
|
249
|
+
* not per CPU). At least this stops uv_cpu_info() from failing completely.
|
250
|
+
*/
|
251
|
+
maxcpus_key = "hw.ncpu";
|
252
|
+
cptimes_key = "kern.cp_time";
|
253
|
+
#else
|
254
|
+
maxcpus_key = "kern.smp.maxcpus";
|
255
|
+
cptimes_key = "kern.cp_times";
|
256
|
+
#endif
|
257
|
+
|
258
|
+
size = sizeof(model);
|
259
|
+
if (sysctlbyname("hw.model", &model, &size, NULL, 0) < 0) {
|
260
|
+
return uv__new_sys_error(errno);
|
261
|
+
}
|
262
|
+
size = sizeof(numcpus);
|
263
|
+
if (sysctlbyname("hw.ncpu", &numcpus, &size, NULL, 0) < 0) {
|
264
|
+
return uv__new_sys_error(errno);
|
265
|
+
}
|
266
|
+
|
267
|
+
*cpu_infos = (uv_cpu_info_t*)malloc(numcpus * sizeof(uv_cpu_info_t));
|
268
|
+
if (!(*cpu_infos)) {
|
269
|
+
return uv__new_artificial_error(UV_ENOMEM);
|
270
|
+
}
|
271
|
+
|
272
|
+
*count = numcpus;
|
273
|
+
|
274
|
+
size = sizeof(cpuspeed);
|
275
|
+
if (sysctlbyname("hw.clockrate", &cpuspeed, &size, NULL, 0) < 0) {
|
276
|
+
free(*cpu_infos);
|
277
|
+
return uv__new_sys_error(errno);
|
278
|
+
}
|
279
|
+
|
280
|
+
/* kern.cp_times on FreeBSD i386 gives an array up to maxcpus instead of ncpu */
|
281
|
+
size = sizeof(maxcpus);
|
282
|
+
if (sysctlbyname(maxcpus_key, &maxcpus, &size, NULL, 0) < 0) {
|
283
|
+
free(*cpu_infos);
|
284
|
+
return uv__new_sys_error(errno);
|
285
|
+
}
|
286
|
+
|
287
|
+
size = maxcpus * CPUSTATES * sizeof(long);
|
288
|
+
|
289
|
+
cp_times = malloc(size);
|
290
|
+
if (cp_times == NULL) {
|
291
|
+
free(*cpu_infos);
|
292
|
+
return uv__new_sys_error(ENOMEM);
|
293
|
+
}
|
294
|
+
|
295
|
+
if (sysctlbyname(cptimes_key, cp_times, &size, NULL, 0) < 0) {
|
296
|
+
free(cp_times);
|
297
|
+
free(*cpu_infos);
|
298
|
+
return uv__new_sys_error(errno);
|
299
|
+
}
|
300
|
+
|
301
|
+
for (i = 0; i < numcpus; i++) {
|
302
|
+
cpu_info = &(*cpu_infos)[i];
|
303
|
+
|
304
|
+
cpu_info->cpu_times.user = (uint64_t)(cp_times[CP_USER+cur]) * multiplier;
|
305
|
+
cpu_info->cpu_times.nice = (uint64_t)(cp_times[CP_NICE+cur]) * multiplier;
|
306
|
+
cpu_info->cpu_times.sys = (uint64_t)(cp_times[CP_SYS+cur]) * multiplier;
|
307
|
+
cpu_info->cpu_times.idle = (uint64_t)(cp_times[CP_IDLE+cur]) * multiplier;
|
308
|
+
cpu_info->cpu_times.irq = (uint64_t)(cp_times[CP_INTR+cur]) * multiplier;
|
309
|
+
|
310
|
+
cpu_info->model = strdup(model);
|
311
|
+
cpu_info->speed = cpuspeed;
|
312
|
+
|
313
|
+
cur+=CPUSTATES;
|
314
|
+
}
|
315
|
+
|
316
|
+
free(cp_times);
|
317
|
+
return uv_ok_;
|
318
|
+
}
|
319
|
+
|
320
|
+
|
321
|
+
void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
|
322
|
+
int i;
|
323
|
+
|
324
|
+
for (i = 0; i < count; i++) {
|
325
|
+
free(cpu_infos[i].model);
|
326
|
+
}
|
327
|
+
|
328
|
+
free(cpu_infos);
|
329
|
+
}
|
330
|
+
|
331
|
+
|
332
|
+
uv_err_t uv_interface_addresses(uv_interface_address_t** addresses,
|
333
|
+
int* count) {
|
334
|
+
/* TODO: implement */
|
335
|
+
*addresses = NULL;
|
336
|
+
*count = 0;
|
337
|
+
return uv_ok_;
|
338
|
+
}
|
339
|
+
|
340
|
+
|
341
|
+
void uv_free_interface_addresses(uv_interface_address_t* addresses,
|
342
|
+
int count) {
|
343
|
+
}
|