From cf45384a93d2d2d5b88579f714e3cf07c17dd498 Mon Sep 17 00:00:00 2001 From: Robert Mustacchi Date: Thu, 16 Jun 2011 20:27:52 -0700 Subject: [PATCH] Add high-resolution timestamp support --- config-unix.mk | 18 ++++++++++++++++-- uv-darwin.c | 34 ++++++++++++++++++++++++++++++++++ uv-linux.c | 35 +++++++++++++++++++++++++++++++++++ uv-sunos.c | 28 ++++++++++++++++++++++++++++ uv-win.c | 5 +++++ uv.h | 28 +++++++++++++++++++++++++++- 6 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 uv-darwin.c create mode 100644 uv-linux.c create mode 100644 uv-sunos.c diff --git a/config-unix.mk b/config-unix.mk index 7d5a5cb4..4c82ee24 100644 --- a/config-unix.mk +++ b/config-unix.mk @@ -26,6 +26,17 @@ LINKFLAGS=-lm ifeq (SunOS,$(uname_S)) LINKFLAGS+=-lsocket -lnsl +UV_OS_FILE=uv-sunos.c +endif + +ifeq (Darwin,$(uname_S)) +LINKFLAGS+=-framework CoreServices +UV_OS_FILE=uv-darwin.c +endif + +ifeq (Linux,$(uname_S)) +LINKFLAGS+=-lrt +UV_OS_FILE=uv-linux.c endif # Need _GNU_SOURCE for strdup? @@ -35,8 +46,11 @@ RUNNER_LINKFLAGS=$(LINKFLAGS) -pthread RUNNER_LIBS= RUNNER_SRC=test/runner-unix.c -uv.a: uv-unix.o uv-common.o ev/ev.o c-ares/ares_query.o - $(AR) rcs uv.a uv-unix.o uv-common.o ev/ev.o c-ares/*.o +uv.a: uv-unix.o uv-common.o uv-platform.o ev/ev.o c-ares/ares_query.o + $(AR) rcs uv.a uv-unix.o uv-platform.o uv-common.o ev/ev.o c-ares/*.o + +uv-platform.o: $(UV_OS_FILE) uv.h uv-unix.h + $(CC) $(CFLAGS) -c $(UV_OS_FILE) -o uv-platform.o uv-unix.o: uv-unix.c uv.h uv-unix.h $(CC) $(CFLAGS) -c uv-unix.c -o uv-unix.o diff --git a/uv-darwin.c b/uv-darwin.c new file mode 100644 index 00000000..36730d29 --- /dev/null +++ b/uv-darwin.c @@ -0,0 +1,34 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" + +#include +#include +#include +#include + +uint64_t uv_get_hrtime() { + uint64_t time; + Nanoseconds enano; + time = mach_absolute_time(); + enano = AbsoluteToNanoseconds(*(AbsoluteTime *)&time); + return (*(uint64_t *)&enano); +} diff --git a/uv-linux.c b/uv-linux.c new file mode 100644 index 00000000..2e6bf45f --- /dev/null +++ b/uv-linux.c @@ -0,0 +1,35 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" + +#include +#include +#include + +/* + * There's probably some way to get time from Linux than gettimeofday(). What + * it is, I don't know. + */ +uint64_t uv_get_hrtime() { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (ts.tv_sec * NANOSEC + ts.tv_nsec); +} diff --git a/uv-sunos.c b/uv-sunos.c new file mode 100644 index 00000000..3b0dbb63 --- /dev/null +++ b/uv-sunos.c @@ -0,0 +1,28 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" + +#include +#include + +uint64_t uv_get_hrtime() { + return (gethrtime()); +} diff --git a/uv-win.c b/uv-win.c index e9e1bbb6..8d25a2a4 100644 --- a/uv-win.c +++ b/uv-win.c @@ -1764,3 +1764,8 @@ done: return retVal; } + + +uint64_t uv_get_hrtime(void) { + assert(0 && "implement me"); +} diff --git a/uv.h b/uv.h index 2806cc29..0b83c5bc 100644 --- a/uv.h +++ b/uv.h @@ -372,7 +372,6 @@ struct sockaddr_in uv_ip4_addr(const char* ip, int port); /* Gets the executable path */ int uv_get_exepath(char* buffer, size_t* size); - /* the presence of this union forces similar struct layout */ union uv_any_handle { uv_tcp_t tcp; @@ -397,6 +396,33 @@ typedef struct { uv_counters_t* uv_counters(); +#ifndef SEC +#define SEC 1 +#endif + +#ifndef MILLISEC +#define MILLISEC 1000 +#endif + +#ifndef MICROSEC +#define MICROSEC 1000000 +#endif + +#ifndef NANOSEC +#define NANOSEC 1000000000 +#endif + +/* + * Returns the current high-resolution real time. This is expressed in + * nanoseconds. It is relative to an arbitrary time in the past. It is not + * related to the time of day and therefore not subject to clock drift. The + * primary use is for measuring performance between intervals. + * + * Note not every platform can support nanosecond resolution; however, this + * value will always be in nanoseconds. + */ +extern uint64_t uv_get_hrtime(void); + #ifdef __cplusplus } #endif