]> source.dussan.org Git - rspamd.git/commitdiff
* More accurate work with clock
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Wed, 25 Mar 2009 10:10:48 +0000 (13:10 +0300)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Wed, 25 Mar 2009 10:10:48 +0000 (13:10 +0300)
CMakeLists.txt
config.h.in
src/cfg_file.h
src/cfg_utils.c
src/protocol.c
src/util.c
src/util.h
src/worker.c

index 4fd124d0ad9f3b5ae8a4657600eb12e2dcbb1f2b..bc9f50f8b9dae14786c98428cb91e2085b5f8d12 100644 (file)
@@ -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)
index f23eb752dfa1b947b9b7b7c25b87d922a8014a6d..a2162144bad9af9cbdea825add4122a1e2774136 100644 (file)
@@ -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}"
 
index 7a4a7c7a35c3f0afc21ebb3d4839f45742c55af2..fbf7309d3fb4a1f00ff074bd5fd49134e5ab5051 100644 (file)
@@ -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                                                   */
 };
 
 /**
index 1dce0ee07de1e033c9e4e42620471dd02090865f..a9be83545f0923b86f902a6fb36d41941fd41dda 100644 (file)
@@ -24,6 +24,7 @@
 
 
 #include "config.h"
+#include <math.h>
 #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;
+       }
 }
 
 
index c9e4925ff2a3c107fe2402da3e46ba7d78d4876d..3aeb46debdf022e992495d61fa5a80640fa7a54c 100644 (file)
@@ -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
index ef9531bd32f7ded8bc2c3e57c9638f709bb2fab8..64f8910e64cfb7a4b8200c8de676e9942b8655ab 100644 (file)
@@ -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;
 }
 
 /*
index 67e3d3d010a6100ea14f45acf93b86974dd5350c..501a78cc3853b0eaf04c2c8590969d699528e7d4 100644 (file)
@@ -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
index 295f331dce0085a0e101f59d0e5094d7bcaf2fb5..1ec5a64dda3745c0daa267cf557ce6e285f28655 100644 (file)
@@ -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);