]> mj.ucw.cz Git - libucw.git/commitdiff
Time: Timing functions separated out to <ucw/time.h>
authorMartin Mares <mj@ucw.cz>
Thu, 16 Feb 2012 19:15:00 +0000 (20:15 +0100)
committerMartin Mares <mj@ucw.cz>
Thu, 16 Feb 2012 19:15:00 +0000 (20:15 +0100)
Updated all references and wrote documentation.

17 files changed:
images/color.c
ucw/Makefile
ucw/asort-test.c
ucw/doc/Makefile
ucw/doc/index.txt
ucw/doc/time.txt [new file with mode: 0644]
ucw/lib.h
ucw/mainloop.c
ucw/sorter/debug/radix-asio-test.c
ucw/sorter/debug/retros.c
ucw/sorter/govern.c
ucw/sorter/sort-test.c
ucw/sorter/sorter.h
ucw/time-stamp.c [new file with mode: 0644]
ucw/time-timer.c [new file with mode: 0644]
ucw/time.h [new file with mode: 0644]
ucw/timer.c [deleted file]

index dd7a0238bf4c6df8f6ef33ee2ebd650995b6e7f6..2d3a02d3e2eb526c11c49f600c77887df51d124c 100644 (file)
@@ -10,6 +10,7 @@
 #undef LOCAL_DEBUG
 
 #include <ucw/lib.h>
+#include <ucw/time.h>
 #include <images/images.h>
 #include <images/color.h>
 #include <images/error.h>
index 97144a0a215527e0fb2c332e5ef49dc1a8c09d03..1b2760ef1a581a3289de86b7b592e16a5bd9ffd3 100644 (file)
@@ -19,7 +19,8 @@ LIBUCW_MODS= \
        fb-file fb-mem fb-temp tempfile fb-mmap fb-limfd fb-buffer fb-grow fb-pool fb-atomic fb-param fb-socket \
        char-cat char-upper char-lower unicode stkstring \
        wildmatch regex \
-       prime primetable random timer \
+       prime primetable random \
+       time-stamp time-timer \
        bit-ffs bit-fls \
        url \
        mainloop main-block main-rec \
@@ -35,7 +36,7 @@ LIBUCW_MODS= \
        resource trans res-fd res-mem res-subpool res-mempool res-eltpool
 
 LIBUCW_MAIN_INCLUDES= \
-       lib.h log.h threads.h \
+       lib.h log.h threads.h time.h \
        mempool.h \
        clists.h slists.h simple-lists.h \
        string.h stkstring.h unicode.h chartype.h regex.h \
index a0f0d01924177bc5c646a0275a577daaa97ff208..351e65550d479aa841226982443b821650e82eae 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include <ucw/lib.h>
+#include <ucw/time.h>
 
 #include <stdlib.h>
 #include <stdio.h>
index a61bb14cda3487e92a26411e0afc1adb881b5b13..c386f4a0a370024910ae15b252bf65ae297e8d6c 100644 (file)
@@ -2,7 +2,7 @@
 
 DIRS+=ucw/doc
 
-UCW_DOCS=basics log fastbuf index config configure install basecode hash docsys conf mempool eltpool mainloop generic growbuf unaligned lists chartype unicode prime binsearch heap binheap compress sort hashtable relnotes trans string
+UCW_DOCS=basics log fastbuf index config configure install basecode hash docsys conf mempool eltpool mainloop generic growbuf unaligned lists chartype unicode prime binsearch heap binheap compress sort hashtable relnotes trans string time
 UCW_INDEX=$(o)/ucw/doc/def_index.html
 UCW_DOCS_HTML=$(addprefix $(o)/ucw/doc/,$(addsuffix .html,$(UCW_DOCS)))
 
index a4e2f0e426fc53e5a16758bd6c277ccc96fe5a17..aaaadc6e965436315a5a06843827836b5bcd5ad1 100644 (file)
@@ -37,6 +37,7 @@ Modules
 - <<compress:,Compression>>
 - <<trans:,Transactions and resource tracking>>
 - <<string:,String operations>>
+- <<time:,Time and timers>>
 
 Other features
 --------------
diff --git a/ucw/doc/time.txt b/ucw/doc/time.txt
new file mode 100644 (file)
index 0000000..c8ab36c
--- /dev/null
@@ -0,0 +1,6 @@
+Time and timers
+===============
+
+ucw/time.h
+----------
+!!ucw/time.h
index d94931d1f3fc37528fa42a141602e3ad929de8d9..f1ea7e9dc4fdb44f11d10a0297e1fb04013c9050 100644 (file)
--- a/ucw/lib.h
+++ b/ucw/lib.h
@@ -178,14 +178,6 @@ void *big_alloc(u64 len) LIKE_MALLOC;              /** Allocate a large memory block in the
 void *big_alloc_zero(u64 len) LIKE_MALLOC;     /** Allocate and clear a large memory block. **/
 void big_free(void *start, u64 len);           /** Free block allocated by @big_alloc() or @big_alloc_zero(). **/
 
-/*** === Trivial timers (timer.c) ***/
-
-timestamp_t get_timestamp(void);               /** Get current time as a millisecond timestamp. **/
-
-void init_timer(timestamp_t *timer);           /** Initialize a timer. **/
-uns get_timer(timestamp_t *timer);             /** Get the number of milliseconds since last init/get of a timer. **/
-uns switch_timer(timestamp_t *oldt, timestamp_t *newt);        /** Stop ticking of one timer and resume another. **/
-
 /*** === Random numbers (random.c) ***/
 
 uns random_u32(void);                          /** Return a pseudorandom 32-bit number. **/
index a8bf0e5e99eb40efe8e298d85f4523c1abde0405..b2b07c5082476bedbdc023e094f49f6feb02a28b 100644 (file)
@@ -15,6 +15,7 @@
 #include <ucw/threads.h>
 #include <ucw/gary.h>
 #include <ucw/process.h>
+#include <ucw/time.h>
 
 #include <stdio.h>
 #include <string.h>
index 6996766a30fb33389813161deb4060ed82e66ae7..0301f35896e4a703e9545e1382e4914296ed9c1e 100644 (file)
@@ -8,6 +8,7 @@
 #include <ucw/conf.h>
 #include <ucw/io.h>
 #include <ucw/asio.h>
+#include <ucw/time.h>
 
 #include <stdio.h>
 #include <stdlib.h>
index 3d09de65de13608d57299ba7cd23befafc9f71cb..25423cc59e11b2ae36300bb1bb472e0805ccd0da 100644 (file)
@@ -8,6 +8,7 @@
 #include <ucw/getopt.h>
 #include <ucw/md5.h>
 #include <ucw/heap.h>
+#include <ucw/time.h>
 
 #include <stdio.h>
 #include <stdlib.h>
index b6b723c25bcac38322ae372985deb28c4cfbbf65..4fe95337208b463e60921f554ee87279497a599b 100644 (file)
@@ -11,6 +11,7 @@
 #include <ucw/fastbuf.h>
 #include <ucw/mempool.h>
 #include <ucw/stkstring.h>
+#include <ucw/time.h>
 #include <ucw/sorter/common.h>
 
 #include <string.h>
index 98eb9f7eebef53d9776f7503fdc481c251f32087..a35a68878b2ca5554581f1f861429457c8148300 100644 (file)
@@ -16,6 +16,7 @@
 #include <ucw/md5.h>
 #include <ucw/string.h>
 #include <ucw/prime.h>
+#include <ucw/timer.h>
 
 #include <stdlib.h>
 #include <stdio.h>
index 47adc24b3b06fc30ef23b184e76f2b4cb9d672df..3c84c386144b33b154cb7ae8b717d67fef9330c7 100644 (file)
 
 #include <ucw/sorter/common.h>
 #include <ucw/fastbuf.h>
+#include <ucw/timer.h>
 
 #include <fcntl.h>
 
diff --git a/ucw/time-stamp.c b/ucw/time-stamp.c
new file mode 100644 (file)
index 0000000..f2c366f
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ *     UCW Library -- A Simple Millisecond Timer
+ *
+ *     (c) 2007--2012 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#include <ucw/lib.h>
+#include <ucw/time.h>
+
+#include <sys/time.h>
+#include <time.h>
+
+#ifdef CONFIG_UCW_MONOTONIC_CLOCK
+
+timestamp_t
+get_timestamp(void)
+{
+  struct timespec ts;
+  if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
+    die("clock_gettime failed: %m");
+  return (timestamp_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
+}
+
+#else
+
+timestamp_t
+get_timestamp(void)
+{
+  struct timeval tv;
+  gettimeofday(&tv, NULL);
+  return (timestamp_t)tv.tv_sec * 1000 + tv.tv_usec / 1000
+#ifdef CONFIG_UCW_DEBUG
+       + 3141592653    // So that we catch all attempts to corelate timestamp_t with wall clock
+#endif
+       ;
+}
+
+#endif
+
+#ifdef TEST
+
+#include <stdio.h>
+
+int main(void)
+{
+  printf("%ju\n", (intmax_t) get_timestamp());
+  return 0;
+}
+
+#endif
diff --git a/ucw/time-timer.c b/ucw/time-timer.c
new file mode 100644 (file)
index 0000000..3037633
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ *     UCW Library -- A Simple Millisecond Timer
+ *
+ *     (c) 2007--2012 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#include <ucw/lib.h>
+#include <ucw/time.h>
+
+void
+init_timer(timestamp_t *timer)
+{
+  *timer = get_timestamp();
+}
+
+uns
+get_timer(timestamp_t *timer)
+{
+  timestamp_t t = *timer;
+  *timer = get_timestamp();
+  return MIN(*timer-t, ~0U);
+}
+
+uns
+switch_timer(timestamp_t *oldt, timestamp_t *newt)
+{
+  *newt = get_timestamp();
+  return MIN(*newt-*oldt, ~0U);
+}
+
+#ifdef TEST
+
+#include <stdio.h>
+#include <unistd.h>
+
+int main(void)
+{
+  timestamp_t t;
+  init_timer(&t);
+  usleep(50000);
+  printf("%u\n", get_timer(&t));
+  return 0;
+}
+
+#endif
diff --git a/ucw/time.h b/ucw/time.h
new file mode 100644 (file)
index 0000000..6d07cdc
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ *     UCW Library -- A Simple Millisecond Timer
+ *
+ *     (c) 2007--2012 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#ifndef _UCW_TIMER_H
+#define _UCW_TIMER_H
+
+/***
+ * === Timestamps
+ *
+ * All timing functions in LibUCW are based on signed 64-bit timestamps
+ * with millisecond precision (the <<basics:type_timestamp_t,`timestamp_t`>> type), which measure
+ * time from an unspecified moment in the past. Depending on the compile-time
+ * settings, that moment can be the traditional UNIX epoch, or for example
+ * system boot if POSIX monotonic clock is used.
+ ***/
+
+timestamp_t get_timestamp(void);               /** Get current time as a millisecond timestamp. **/
+
+/***
+ * === Timers
+ *
+ * A timer is a very simple construct for measuring execution time.
+ * It can be initialized and then read multiple times. Each read returns
+ * the number of milliseconds elapsed since the previous read or initialization.
+ ***/
+
+void init_timer(timestamp_t *timer);           /** Initialize a timer. **/
+uns get_timer(timestamp_t *timer);             /** Get the number of milliseconds since last init/get of a timer. **/
+uns switch_timer(timestamp_t *oldt, timestamp_t *newt);        /** Stop ticking of one timer and resume another. **/
+
+#endif
diff --git a/ucw/timer.c b/ucw/timer.c
deleted file mode 100644 (file)
index 3e680bc..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- *     UCW Library -- A Simple Millisecond Timer
- *
- *     (c) 2007--2012 Martin Mares <mj@ucw.cz>
- *
- *     This software may be freely distributed and used according to the terms
- *     of the GNU Lesser General Public License.
- */
-
-#include <ucw/lib.h>
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/time.h>
-#include <time.h>
-
-#ifdef CONFIG_UCW_MONOTONIC_CLOCK
-
-timestamp_t
-get_timestamp(void)
-{
-  struct timespec ts;
-  if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
-    die("clock_gettime failed: %m");
-  return (timestamp_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
-}
-
-#else
-
-timestamp_t
-get_timestamp(void)
-{
-  struct timeval tv;
-  gettimeofday(&tv, NULL);
-  return (timestamp_t)tv.tv_sec * 1000 + tv.tv_usec / 1000
-#ifdef CONFIG_UCW_DEBUG
-       + 3141592653    // So that we catch all attempts to corelate timestamp_t with wall clock
-#endif
-       ;
-}
-
-#endif
-
-void
-init_timer(timestamp_t *timer)
-{
-  *timer = get_timestamp();
-}
-
-uns
-get_timer(timestamp_t *timer)
-{
-  timestamp_t t = *timer;
-  *timer = get_timestamp();
-  return MIN(*timer-t, ~0U);
-}
-
-uns
-switch_timer(timestamp_t *oldt, timestamp_t *newt)
-{
-  *newt = get_timestamp();
-  return MIN(*newt-*oldt, ~0U);
-}
-
-#ifdef TEST
-
-#include <stdio.h>
-
-int main(void)
-{
-  printf("%ju\n", (intmax_t) get_timestamp());
-  return 0;
-}
-
-#endif