[njs] Added nxt_time().

Dmitry Volyntsev xeioex at nginx.com
Thu Nov 29 18:16:45 UTC 2018


details:   https://hg.nginx.org/njs/rev/2ad7533bcbae
branches:  
changeset: 677:2ad7533bcbae
user:      Artem S. Povalyukhin <artem.povaluhin at gmail.com>
date:      Wed Nov 28 21:50:28 2018 +0300
description:
Added nxt_time().

Returns the current high-resolution real time.

diffstat:

 Makefile       |  10 ++++++----
 nxt/Makefile   |  13 +++++++++++++
 nxt/auto/time  |  40 ++++++++++++++++++++++++++++++++++++++++
 nxt/nxt_time.c |  30 ++++++++++++++++++++++++++++++
 nxt/nxt_time.h |   3 +++
 5 files changed, 92 insertions(+), 4 deletions(-)

diffs (186 lines):

diff -r cf424d6313ae -r 2ad7533bcbae Makefile
--- a/Makefile	Thu Nov 29 18:14:12 2018 +0300
+++ b/Makefile	Wed Nov 28 21:50:28 2018 +0300
@@ -48,6 +48,7 @@ NXT_BUILDDIR =	build
 	$(NXT_BUILDDIR)/nxt_sha1.o \
 	$(NXT_BUILDDIR)/nxt_sha2.o \
 	$(NXT_BUILDDIR)/nxt_pcre.o \
+	$(NXT_BUILDDIR)/nxt_time.o \
 	$(NXT_BUILDDIR)/nxt_malloc.o \
 	$(NXT_BUILDDIR)/nxt_mem_cache_pool.o \
 
@@ -93,6 +94,7 @@ NXT_BUILDDIR =	build
 		$(NXT_BUILDDIR)/nxt_sha1.o \
 		$(NXT_BUILDDIR)/nxt_sha2.o \
 		$(NXT_BUILDDIR)/nxt_pcre.o \
+		$(NXT_BUILDDIR)/nxt_time.o \
 		$(NXT_BUILDDIR)/nxt_malloc.o \
 		$(NXT_BUILDDIR)/nxt_mem_cache_pool.o \
 
@@ -535,7 +537,7 @@ dist:
 		-I$(NXT_LIB) $(NXT_EDITLINE_CFLAGS) -Injs \
 		njs/njs_shell.c \
 		$(NXT_BUILDDIR)/libnjs.a \
-		-lm $(NXT_PCRE_LIB) $(NXT_EDITLINE_LIB)
+		-lm $(NXT_PCRE_LIB) $(NXT_LIBRT) $(NXT_EDITLINE_LIB)
 
 $(NXT_BUILDDIR)/njs_unit_test: \
 	$(NXT_BUILDDIR)/libnxt.a \
@@ -546,7 +548,7 @@ dist:
 		-I$(NXT_LIB) -Injs \
 		njs/test/njs_unit_test.c \
 		$(NXT_BUILDDIR)/libnjs.a \
-		-lm $(NXT_PCRE_LIB)
+		-lm $(NXT_PCRE_LIB) $(NXT_LIBRT)
 
 $(NXT_BUILDDIR)/njs_interactive_test: \
 	$(NXT_BUILDDIR)/libnxt.a \
@@ -557,7 +559,7 @@ dist:
 		-I$(NXT_LIB) -Injs \
 		njs/test/njs_interactive_test.c \
 		$(NXT_BUILDDIR)/libnjs.a \
-		-lm $(NXT_PCRE_LIB)
+		-lm $(NXT_PCRE_LIB) $(NXT_LIBRT)
 
 $(NXT_BUILDDIR)/njs_benchmark: \
 	$(NXT_BUILDDIR)/libnxt.a \
@@ -568,6 +570,6 @@ dist:
 		-I$(NXT_LIB) -Injs \
 		njs/test/njs_benchmark.c \
 		$(NXT_BUILDDIR)/libnjs.a \
-		-lm $(NXT_PCRE_LIB)
+		-lm $(NXT_PCRE_LIB) $(NXT_LIBRT)
 
 include $(NXT_LIB)/Makefile
diff -r cf424d6313ae -r 2ad7533bcbae nxt/Makefile
--- a/nxt/Makefile	Thu Nov 29 18:14:12 2018 +0300
+++ b/nxt/Makefile	Wed Nov 28 21:50:28 2018 +0300
@@ -20,6 +20,7 @@ NXT_LIB =	nxt
 	$(NXT_BUILDDIR)/nxt_pcre.o \
 	$(NXT_BUILDDIR)/nxt_malloc.o \
 	$(NXT_BUILDDIR)/nxt_trace.o \
+	$(NXT_BUILDDIR)/nxt_time.o \
 	$(NXT_BUILDDIR)/nxt_mem_cache_pool.o \
 
 	ar -r -c $(NXT_BUILDDIR)/libnxt.a \
@@ -37,6 +38,7 @@ NXT_LIB =	nxt
 		$(NXT_BUILDDIR)/nxt_sha2.o \
 		$(NXT_BUILDDIR)/nxt_pcre.o \
 		$(NXT_BUILDDIR)/nxt_malloc.o \
+		$(NXT_BUILDDIR)/nxt_time.o \
 		$(NXT_BUILDDIR)/nxt_trace.o \
 		$(NXT_BUILDDIR)/nxt_mem_cache_pool.o \
 
@@ -206,6 +208,17 @@ NXT_LIB =	nxt
 		-I$(NXT_LIB) \
 		$(NXT_LIB)/nxt_malloc.c
 
+$(NXT_BUILDDIR)/nxt_time.o: \
+	$(NXT_LIB)/nxt_auto_config.h \
+	$(NXT_LIB)/nxt_types.h \
+	$(NXT_LIB)/nxt_clang.h \
+	$(NXT_LIB)/nxt_time.h \
+	$(NXT_LIB)/nxt_time.c \
+
+	$(NXT_CC) -c -o $(NXT_BUILDDIR)/nxt_time.o $(NXT_CFLAGS) \
+		-I$(NXT_LIB) \
+		$(NXT_LIB)/nxt_time.c
+
 $(NXT_BUILDDIR)/nxt_trace.o: \
 	$(NXT_LIB)/nxt_auto_config.h \
 	$(NXT_LIB)/nxt_types.h \
diff -r cf424d6313ae -r 2ad7533bcbae nxt/auto/time
--- a/nxt/auto/time	Thu Nov 29 18:14:12 2018 +0300
+++ b/nxt/auto/time	Wed Nov 28 21:50:28 2018 +0300
@@ -3,6 +3,46 @@
 # Copyright (C) NGINX, Inc.
 
 
+nxt_feature="clock_gettime(CLOCK_MONOTONIC)"
+nxt_feature_name=NXT_HAVE_CLOCK_MONOTONIC
+nxt_feature_run=yes
+nxt_feature_incs=
+nxt_feature_libs=
+nxt_feature_test="#include <time.h>
+
+                  int main() {
+                      struct timespec  ts;
+
+                      if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
+                          return 1;
+                      return 0;
+                  }"
+. ${NXT_AUTO}feature
+
+
+if [ $nxt_found = no ]; then
+
+    # Linux and Solaris 10 clock_gettime() are in librt.
+
+    nxt_feature="clock_gettime(CLOCK_MONOTONIC) in librt"
+    nxt_feature_libs="-lrt"
+    . ${NXT_AUTO}feature
+fi
+
+if [ $nxt_found = yes ]; then
+    cat << END >> $NXT_MAKEFILE_CONF
+
+NXT_LIBRT = -lrt
+END
+
+else
+    cat << END >> $NXT_MAKEFILE_CONF
+
+NXT_LIBRT =
+END
+
+fi
+
 # Linux, FreeBSD, MacOSX.
 
 nxt_feature="struct tm.tm_gmtoff"
diff -r cf424d6313ae -r 2ad7533bcbae nxt/nxt_time.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nxt/nxt_time.c	Wed Nov 28 21:50:28 2018 +0300
@@ -0,0 +1,30 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) NGINX, Inc.
+ */
+
+#include <nxt_auto_config.h>
+#include <nxt_types.h>
+#include <nxt_clang.h>
+#include <nxt_time.h>
+
+#include <time.h>
+
+uint64_t
+nxt_time(void)
+{
+#if (NXT_HAVE_CLOCK_MONOTONIC)
+    struct timespec ts;
+
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+
+    return (uint64_t) ts.tv_sec * 1000000000 + ts.tv_nsec;
+#else
+    struct timeval tv;
+
+    gettimeofday(&tv, NULL);
+
+    return (uint64_t) tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
+#endif
+}
diff -r cf424d6313ae -r 2ad7533bcbae nxt/nxt_time.h
--- a/nxt/nxt_time.h	Thu Nov 29 18:14:12 2018 +0300
+++ b/nxt/nxt_time.h	Wed Nov 28 21:50:28 2018 +0300
@@ -21,4 +21,7 @@
 #endif
 
 
+uint64_t nxt_time(void);
+
+
 #endif /* _NXT_TIME_H_INCLUDED_ */


More information about the nginx-devel mailing list