Updated all references and wrote documentation.
#undef LOCAL_DEBUG
#include <ucw/lib.h>
+#include <ucw/time.h>
#include <images/images.h>
#include <images/color.h>
#include <images/error.h>
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 \
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 \
*/
#include <ucw/lib.h>
+#include <ucw/time.h>
#include <stdlib.h>
#include <stdio.h>
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)))
- <<compress:,Compression>>
- <<trans:,Transactions and resource tracking>>
- <<string:,String operations>>
+- <<time:,Time and timers>>
Other features
--------------
--- /dev/null
+Time and timers
+===============
+
+ucw/time.h
+----------
+!!ucw/time.h
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. **/
#include <ucw/threads.h>
#include <ucw/gary.h>
#include <ucw/process.h>
+#include <ucw/time.h>
#include <stdio.h>
#include <string.h>
#include <ucw/conf.h>
#include <ucw/io.h>
#include <ucw/asio.h>
+#include <ucw/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <ucw/getopt.h>
#include <ucw/md5.h>
#include <ucw/heap.h>
+#include <ucw/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <ucw/fastbuf.h>
#include <ucw/mempool.h>
#include <ucw/stkstring.h>
+#include <ucw/time.h>
#include <ucw/sorter/common.h>
#include <string.h>
#include <ucw/md5.h>
#include <ucw/string.h>
#include <ucw/prime.h>
+#include <ucw/timer.h>
#include <stdlib.h>
#include <stdio.h>
#include <ucw/sorter/common.h>
#include <ucw/fastbuf.h>
+#include <ucw/timer.h>
#include <fcntl.h>
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
+++ /dev/null
-/*
- * 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