diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-05-04 17:26:26 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-05-04 17:26:26 +0100 |
commit | f4232b3a9fb8dcaad06807ff49749e41bdb9d6a2 (patch) | |
tree | ff503c6ea95c82ec2415d9ad09fa1ec1d562899c /src | |
parent | d14083964c313c566d1cd6017a4f622f2ef97ef5 (diff) | |
download | rspamd-f4232b3a9fb8dcaad06807ff49749e41bdb9d6a2.tar.gz rspamd-f4232b3a9fb8dcaad06807ff49749e41bdb9d6a2.zip |
Rework timers invocation.
Diffstat (limited to 'src')
-rw-r--r-- | src/libutil/util.c | 75 | ||||
-rw-r--r-- | src/libutil/util.h | 23 |
2 files changed, 48 insertions, 50 deletions
diff --git a/src/libutil/util.c b/src/libutil/util.c index 6ecab9bcd..a86f67b48 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -1191,47 +1191,18 @@ resolve_stat_filename (rspamd_mempool_t * pool, return new; } -#ifdef HAVE_CLOCK_GETTIME const gchar * -calculate_check_time (struct timeval *tv, - struct timespec *begin, - gint resolution, +calculate_check_time (gdouble start_real, gdouble start_virtual, gint resolution, guint32 *scan_time) -#else -const gchar * -calculate_check_time (struct timeval *begin, gint resolution, - guint32 *scan_time) -#endif { - double vdiff, diff; + double vdiff, diff, end_real, end_virtual; static gchar res[64]; static gchar fmt[sizeof ("%.10f ms real, %.10f ms virtual")]; - struct timeval tv_now; - - if (gettimeofday (&tv_now, NULL) == -1) { - msg_warn ("gettimeofday failed: %s", strerror (errno)); - } -#ifdef HAVE_CLOCK_GETTIME - struct timespec ts; - diff = (tv_now.tv_sec - tv->tv_sec) * 1000. + /* Seconds */ - (tv_now.tv_usec - tv->tv_usec) / 1000.; /* Microseconds */ -#ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID - clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts); -#elif defined(HAVE_CLOCK_VIRTUAL) - clock_gettime (CLOCK_VIRTUAL, &ts); -#else - clock_gettime (CLOCK_REALTIME, &ts); -#endif - - vdiff = (ts.tv_sec - begin->tv_sec) * 1000. + /* Seconds */ - (ts.tv_nsec - begin->tv_nsec) / 1000000.; /* Nanoseconds */ -#else - diff = (tv_now.tv_sec - begin->tv_sec) * 1000. + /* Seconds */ - (tv_now.tv_usec - begin->tv_usec) / 1000.; /* Microseconds */ - - vdiff = diff; -#endif + end_real = rspamd_get_ticks (); + end_virtual = rspamd_get_virtual_ticks (); + vdiff = (end_virtual - start_virtual) * 1000; + diff = (end_real - start_real) * 1000; *scan_time = diff; @@ -2335,13 +2306,43 @@ rspamd_get_ticks (void) { gdouble res; -#ifdef __APPLE__ +#ifdef HAVE_CLOCK_GETTIME + struct timespec ts; + clock_gettime (CLOCK_MONOTONIC, &ts); + + res = (double)ts.tv_sec + ts.tv_nsec / 1000000000.; +#elif defined(__APPLE__) res = mach_absolute_time () / 1000000000.; #else + struct timeval tv; + + (void)gettimeofday (&tv, NULL); + res = (double)tv.tv_sec + tv.tv_nsec / 1000000.; +#endif + + return res; +} + +gdouble +rspamd_get_virtual_ticks (void) +{ + gdouble res; + +#ifdef HAVE_CLOCK_GETTIME struct timespec ts; - clock_gettime (CLOCK_MONOTONIC, &ts); +# ifdef CLOCK_PROCESS_CPUTIME_ID + clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts); +# elif defined(CLOCK_PROF) + clock_gettime (CLOCK_PROF, &ts); +# elif defined(CLOCK_VIRTUAL) + clock_gettime (CLOCK_VIRTUAL, &ts); +# else + clock_gettime (CLOCK_REALTIME, &ts); +# endif res = (double)ts.tv_sec + ts.tv_nsec / 1000000000.; +#else + res = clock () / (double)CLOCKS_PER_SEC; #endif return res; diff --git a/src/libutil/util.h b/src/libutil/util.h index 6b8b7f3ad..f3a8dcbc5 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -156,19 +156,10 @@ gchar * resolve_stat_filename (rspamd_mempool_t *pool, gchar *pattern, gchar *rcpt, gchar *from); -#ifdef HAVE_CLOCK_GETTIME -/* - * Calculate check time with specified resolution of timer - */ -const gchar * calculate_check_time (struct timeval *tv, - struct timespec *begin, - gint resolution, - guint32 *scan_ms); -#else -const gchar * calculate_check_time (struct timeval *begin, - gint resolution, - guint32 *scan_ms); -#endif + +const gchar * +calculate_check_time (gdouble start_real, gdouble start_virtual, gint resolution, + guint32 *scan_time); /* * File locking functions @@ -444,6 +435,12 @@ guchar* rspamd_decode_base32 (const gchar *in, gsize inlen, gsize *outlen); gdouble rspamd_get_ticks (void); /** + * Portably return the current virtual clock ticks as seconds + * @return + */ +gdouble rspamd_get_virtual_ticks (void); + +/** * Special utility to help array freeing in rspamd_mempool * @param p */ |