From: Vsevolod Stakhov Date: Wed, 25 Mar 2009 10:10:48 +0000 (+0300) Subject: * More accurate work with clock X-Git-Tag: 0.2.7~229 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b539b432494cafe78c47a0fc13a289006c4a1b24;p=rspamd.git * More accurate work with clock --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 4fd124d0a..bc9f50f8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -177,6 +177,8 @@ CHECK_SYMBOL_EXISTS(MAP_SHARED sys/mman.h HAVE_MMAP_SHARED) CHECK_SYMBOL_EXISTS(MAP_ANON sys/mman.h HAVE_MMAP_ANON) CHECK_SYMBOL_EXISTS(_SC_NPROCESSORS_ONLN unistd.h HAVE_SC_NPROCESSORS_ONLN) CHECK_SYMBOL_EXISTS(SLIST_FOREACH_SAFE sys/queue.h HAVE_COMPATIBLE_QUEUE_H) +CHECK_SYMBOL_EXISTS(CLOCK_PROCESS_CPUTIME_ID time.h HAVE_CLOCK_PROCESS_CPUTIME_ID) +CHECK_SYMBOL_EXISTS(CLOCK_VIRTUAL time.h HAVE_CLOCK_VIRTUAL) IF(NOT HAVE_COMPATIBLE_QUEUE_H) INCLUDE_DIRECTORIES(compat) diff --git a/config.h.in b/config.h.in index f23eb752d..a2162144b 100644 --- a/config.h.in +++ b/config.h.in @@ -91,6 +91,9 @@ #cmakedefine GMIME24 1 +#cmakedefine HAVE_CLOCK_VIRTUAL 1 +#cmakedefine HAVE_CLOCK_PROCESS_CPUTIME_ID 1 + #define RVERSION "${RSPAMD_VERSION}" #define RSPAMD_MASTER_SITE_URL "${RSPAMD_MASTER_SITE_URL}" diff --git a/src/cfg_file.h b/src/cfg_file.h index 7a4a7c7a3..fbf7309d3 100644 --- a/src/cfg_file.h +++ b/src/cfg_file.h @@ -220,6 +220,7 @@ struct config_file { GHashTable* composite_symbols; /**< hash of composite symbols indexed by its name */ GHashTable* statfiles; /**< hash of defined statfiles indexed by alias */ GHashTable* cfg_params; /**< all cfg params indexed by its name in this structure */ + int clock_res; /**< resolution of clock used */ }; /** diff --git a/src/cfg_utils.c b/src/cfg_utils.c index 1dce0ee07..a9be83545 100644 --- a/src/cfg_utils.c +++ b/src/cfg_utils.c @@ -24,6 +24,7 @@ #include "config.h" +#include #include "cfg_file.h" #include "main.h" #include "filter.h" @@ -542,6 +543,8 @@ fill_cfg_params (struct config_file *cfg) void post_load_config (struct config_file *cfg) { + struct timespec ts; + if (cfg->lmtp_enable && !cfg->delivery_enable) { yywarn ("post_load_config: lmtp is enabled, but delivery is not enabled, disabling lmtp"); cfg->lmtp_enable = FALSE; @@ -554,6 +557,21 @@ post_load_config (struct config_file *cfg) parse_filters_str (cfg, cfg->message_filters_str, SCRIPT_MESSAGE); parse_filters_str (cfg, cfg->url_filters_str, SCRIPT_URL); fill_cfg_params (cfg); + +#ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID + clock_getres (CLOCK_PROCESS_CPUTIME_ID, &ts); +#elif defined(HAVE_CLOCK_VIRTUAL) + clock_getres (CLOCK_VIRTUAL, &ts); +#else + clock_getres (CLOCK_REALTIME, &ts); +#endif + cfg->clock_res = (int)log10 (1000000 / ts.tv_nsec); + if (cfg->clock_res < 0) { + cfg->clock_res = 0; + } + if (cfg->clock_res > 3) { + cfg->clock_res = 3; + } } diff --git a/src/protocol.c b/src/protocol.c index c9e4925ff..3aeb46deb 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -486,8 +486,8 @@ show_metric_result (gpointer metric_name, gpointer metric_value, void *user_data if (task->cmd == CMD_SYMBOLS && metric_value != NULL) { show_metric_symbols (metric_res, cd); } - cd->log_offset += snprintf (cd->log_buf + cd->log_offset, cd->log_size - cd->log_offset, "] ), len: %ld, time: %ldms", - (long int)task->msg->len, calculate_check_time (&task->ts)); + cd->log_offset += snprintf (cd->log_buf + cd->log_offset, cd->log_size - cd->log_offset, "] ), len: %ld, time: %s", + (long int)task->msg->len, calculate_check_time (&task->ts, task->cfg->clock_res)); } static int diff --git a/src/util.c b/src/util.c index ef9531bd3..64f8910e6 100644 --- a/src/util.c +++ b/src/util.c @@ -787,18 +787,28 @@ resolve_stat_filename (memory_pool_t *pool, char *pattern, char *rcpt, char *fro return new; } -long int -calculate_check_time (struct timespec *begin) +const char * +calculate_check_time (struct timespec *begin, int resolution) { struct timespec ts; - long int res; + double diff; + static char res[sizeof("100000.000")]; + static char fmt[sizeof("%.10f")]; +#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 - res = (ts.tv_sec - begin->tv_sec) * 1000 + /* Seconds */ - (ts.tv_nsec - begin->tv_nsec) / 1000000; /* Nanoseconds */ + diff = (ts.tv_sec - begin->tv_sec) * 1000. + /* Seconds */ + (ts.tv_nsec - begin->tv_nsec) / 1000000.; /* Nanoseconds */ + sprintf (fmt, "%%.%df", resolution); + snprintf (res, sizeof (res), fmt, diff); - return res; + return (const char *)res; } /* diff --git a/src/util.h b/src/util.h index 67e3d3d01..501a78cc3 100644 --- a/src/util.h +++ b/src/util.h @@ -55,6 +55,6 @@ void file_log_function (const gchar *log_domain, GLogLevelFlags log_level, const /* Replace %r with rcpt value and %f with from value, new string is allocated in pool */ char* resolve_stat_filename (memory_pool_t *pool, char *pattern, char *rcpt, char *from); -long int calculate_check_time (struct timespec *begin); +const char* calculate_check_time (struct timespec *begin, int resolution); #endif diff --git a/src/worker.c b/src/worker.c index 295f331dc..1ec5a64dd 100644 --- a/src/worker.c +++ b/src/worker.c @@ -254,7 +254,13 @@ accept_socket (int fd, short what, void *arg) new_task->state = READ_COMMAND; new_task->sock = nfd; new_task->cfg = worker->srv->cfg; +#ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID + clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &new_task->ts); +#elif defined(HAVE_CLOCK_VIRTUAL) + clock_gettime (CLOCK_VIRTUAL, &new_task->ts); +#else clock_gettime (CLOCK_REALTIME, &new_task->ts); +#endif io_tv.tv_sec = WORKER_IO_TIMEOUT; io_tv.tv_usec = 0; TAILQ_INIT (&new_task->urls);