From 46a15649d1e544dce95f76424a4870d69caa955c Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Thu, 16 Feb 2012 20:15:00 +0100 Subject: [PATCH] Time: Timing functions separated out to Updated all references and wrote documentation. --- images/color.c | 1 + ucw/Makefile | 5 ++-- ucw/asort-test.c | 1 + ucw/doc/Makefile | 2 +- ucw/doc/index.txt | 1 + ucw/doc/time.txt | 6 ++++ ucw/lib.h | 8 ----- ucw/mainloop.c | 1 + ucw/sorter/debug/radix-asio-test.c | 1 + ucw/sorter/debug/retros.c | 1 + ucw/sorter/govern.c | 1 + ucw/sorter/sort-test.c | 1 + ucw/sorter/sorter.h | 1 + ucw/{timer.c => time-stamp.c} | 24 +-------------- ucw/time-timer.c | 48 ++++++++++++++++++++++++++++++ ucw/time.h | 37 +++++++++++++++++++++++ 16 files changed, 105 insertions(+), 34 deletions(-) create mode 100644 ucw/doc/time.txt rename ucw/{timer.c => time-stamp.c} (72%) create mode 100644 ucw/time-timer.c create mode 100644 ucw/time.h diff --git a/images/color.c b/images/color.c index dd7a0238..2d3a02d3 100644 --- a/images/color.c +++ b/images/color.c @@ -10,6 +10,7 @@ #undef LOCAL_DEBUG #include +#include #include #include #include diff --git a/ucw/Makefile b/ucw/Makefile index 97144a0a..1b2760ef 100644 --- a/ucw/Makefile +++ b/ucw/Makefile @@ -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 \ diff --git a/ucw/asort-test.c b/ucw/asort-test.c index a0f0d019..351e6555 100644 --- a/ucw/asort-test.c +++ b/ucw/asort-test.c @@ -8,6 +8,7 @@ */ #include +#include #include #include diff --git a/ucw/doc/Makefile b/ucw/doc/Makefile index a61bb14c..c386f4a0 100644 --- a/ucw/doc/Makefile +++ b/ucw/doc/Makefile @@ -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))) diff --git a/ucw/doc/index.txt b/ucw/doc/index.txt index a4e2f0e4..aaaadc6e 100644 --- a/ucw/doc/index.txt +++ b/ucw/doc/index.txt @@ -37,6 +37,7 @@ Modules - <> - <> - <> +- <> Other features -------------- diff --git a/ucw/doc/time.txt b/ucw/doc/time.txt new file mode 100644 index 00000000..c8ab36c1 --- /dev/null +++ b/ucw/doc/time.txt @@ -0,0 +1,6 @@ +Time and timers +=============== + +ucw/time.h +---------- +!!ucw/time.h diff --git a/ucw/lib.h b/ucw/lib.h index d94931d1..f1ea7e9d 100644 --- 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. **/ diff --git a/ucw/mainloop.c b/ucw/mainloop.c index a8bf0e5e..b2b07c50 100644 --- a/ucw/mainloop.c +++ b/ucw/mainloop.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include diff --git a/ucw/sorter/debug/radix-asio-test.c b/ucw/sorter/debug/radix-asio-test.c index 6996766a..0301f358 100644 --- a/ucw/sorter/debug/radix-asio-test.c +++ b/ucw/sorter/debug/radix-asio-test.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include diff --git a/ucw/sorter/debug/retros.c b/ucw/sorter/debug/retros.c index 3d09de65..25423cc5 100644 --- a/ucw/sorter/debug/retros.c +++ b/ucw/sorter/debug/retros.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include diff --git a/ucw/sorter/govern.c b/ucw/sorter/govern.c index b6b723c2..4fe95337 100644 --- a/ucw/sorter/govern.c +++ b/ucw/sorter/govern.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include diff --git a/ucw/sorter/sort-test.c b/ucw/sorter/sort-test.c index 98eb9f7e..a35a6887 100644 --- a/ucw/sorter/sort-test.c +++ b/ucw/sorter/sort-test.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include diff --git a/ucw/sorter/sorter.h b/ucw/sorter/sorter.h index 47adc24b..3c84c386 100644 --- a/ucw/sorter/sorter.h +++ b/ucw/sorter/sorter.h @@ -116,6 +116,7 @@ #include #include +#include #include diff --git a/ucw/timer.c b/ucw/time-stamp.c similarity index 72% rename from ucw/timer.c rename to ucw/time-stamp.c index 3e680bc1..f2c366f9 100644 --- a/ucw/timer.c +++ b/ucw/time-stamp.c @@ -8,9 +8,8 @@ */ #include +#include -#include -#include #include #include @@ -41,27 +40,6 @@ get_timestamp(void) #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 diff --git a/ucw/time-timer.c b/ucw/time-timer.c new file mode 100644 index 00000000..30376335 --- /dev/null +++ b/ucw/time-timer.c @@ -0,0 +1,48 @@ +/* + * UCW Library -- A Simple Millisecond Timer + * + * (c) 2007--2012 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include +#include + +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 +#include + +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 index 00000000..6d07cdc7 --- /dev/null +++ b/ucw/time.h @@ -0,0 +1,37 @@ +/* + * UCW Library -- A Simple Millisecond Timer + * + * (c) 2007--2012 Martin Mares + * + * 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 <> 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 -- 2.39.2