diff options
author | 2019-10-24 14:20:49 +0300 | |
---|---|---|
committer | 2019-10-24 14:20:49 +0300 | |
commit | 69933c5b521eacd968c4d78ad769ea7a3d6b872f (patch) | |
tree | 851b8e5c618f1f1b9666395942ec77e386a274a6 | |
parent | ab8851b48622dba2d2954306cd48c5c14c91db72 (diff) | |
download | librin-69933c5b521eacd968c4d78ad769ea7a3d6b872f.tar.gz librin-69933c5b521eacd968c4d78ad769ea7a3d6b872f.tar.bz2 librin-69933c5b521eacd968c4d78ad769ea7a3d6b872f.zip |
diagnostic: print time since program start instead.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
-rw-r--r-- | include/rin/definitions.h | 10 | ||||
-rw-r--r-- | include/rin/diagnostic.h | 1 | ||||
-rw-r--r-- | src/diagnostic/diagnostic.c | 28 | ||||
-rw-r--r-- | src/diagnostic/diagnostic_private.h | 1 | ||||
-rw-r--r-- | src/time/benchmark.c | 7 |
5 files changed, 28 insertions, 19 deletions
diff --git a/include/rin/definitions.h b/include/rin/definitions.h index 7ba65a6..9995e0f 100644 --- a/include/rin/definitions.h +++ b/include/rin/definitions.h @@ -63,4 +63,14 @@ do { \ } \ } while (0) +/* CLOCK_MONOTONIC is prefered when available, as + * it does not suffer from discontinous jumps. + * Otherwise use CLOCK_REALTIME as fallback. + */ +#if defined CLOCK_MONOTONIC +# define RIN_CLOCK_WALL_COUNTER CLOCK_MONOTONIC +#else +# define RIN_CLOCK_WALL_COUNTER CLOCK_REALTIME +#endif /* defined CLOCK_MONOTONIC */ + #endif /* LIBRIN_DEFINITIONS_INCLUDED */ diff --git a/include/rin/diagnostic.h b/include/rin/diagnostic.h index 128e059..09d2fb9 100644 --- a/include/rin/diagnostic.h +++ b/include/rin/diagnostic.h @@ -40,6 +40,7 @@ enum rin_diag_outstream { rin_diag_info }; +void rin_diag_init(void); int rin_diag_flags(int flag, int action); int rin_diag_set_outstream(enum rin_diag_outstream channel, FILE *stream); diff --git a/src/diagnostic/diagnostic.c b/src/diagnostic/diagnostic.c index 4d67573..4c4a993 100644 --- a/src/diagnostic/diagnostic.c +++ b/src/diagnostic/diagnostic.c @@ -23,16 +23,29 @@ #include <string.h> #include <time.h> #include "rin/diagnostic.h" +#include "rin/time.h" #include "rin/definitions.h" static struct iostate { + struct timespec start; FILE *err; FILE *warn; FILE *fixme; FILE *info; unsigned int flags; char pidconv[8]; -} state = {NULL, NULL, NULL, NULL, RIN_DIAG_PREFIX, "%08x:"}; +} state = {{0, 0}, NULL, NULL, NULL, NULL, RIN_DIAG_PREFIX, "%08x:"}; + +void rin_diag_init(void) +{ + static const char convstr[8] = "%04hx:"; + + if (sizeof(pid_t) == 4) { + memcpy(state.pidconv, convstr, sizeof(convstr)); + } + + clock_gettime(RIN_CLOCK_WALL_COUNTER, &state.start); +} int rin_diag_flags(int flag, int action) { @@ -102,8 +115,6 @@ void rin_info(const char *format, ...) static void __rin_msg(FILE *stream, const char *prefix, const char *format, va_list args) { - rin_once(__rin_pidconvadjust()); - if (state.flags & RIN_DIAG_PREFIX) { fprintf(stream, "%s:", prefix); } @@ -114,18 +125,11 @@ static void __rin_msg(FILE *stream, const char *prefix, const char *format, va_l if (state.flags & RIN_DIAG_TIME) { struct timespec t; - clock_gettime(CLOCK_REALTIME, &t); + clock_gettime(RIN_CLOCK_WALL_COUNTER, &t); + t = rin_time_sub(&t, &state.start); fprintf(stream, "%lu.%lu:", t.tv_sec, t.tv_nsec); } vfprintf(stream, format, args); fputc('\n', stream); } - -static void __rin_pidconvadjust(void) -{ - if (sizeof(pid_t) == 4) { - const char convstr[8] = "%04hx:"; - memcpy(state.pidconv, convstr, sizeof(convstr)); - } -} diff --git a/src/diagnostic/diagnostic_private.h b/src/diagnostic/diagnostic_private.h index 96b1759..981bbe9 100644 --- a/src/diagnostic/diagnostic_private.h +++ b/src/diagnostic/diagnostic_private.h @@ -29,6 +29,5 @@ #include "rin/compat.h" static void __rin_msg(FILE *stream, const char *prefix, const char *format, va_list args); -static void __rin_pidconvadjust(void); #endif /* LIBRIN_DIAGNOSTIC_PRIVATE_INCLUDED */ diff --git a/src/time/benchmark.c b/src/time/benchmark.c index 543275a..4738676 100644 --- a/src/time/benchmark.c +++ b/src/time/benchmark.c @@ -22,14 +22,9 @@ #include <errno.h> #include <sys/resource.h> #include "rin/time.h" +#include "rin/definitions.h" #include "time_private.h" -#if defined CLOCK_MONOTONIC -# define RIN_CLOCK_WALL_COUNTER CLOCK_MONOTONIC -#else -# define RIN_CLOCK_WALL_COUNTER CLOCK_REALTIME -#endif /* defined CLOCK_MONOTONIC */ - static struct bench { int status; struct rusage runtime; |