From 34f0b5587e72a7f77425d9faccac8b03b363fdcc Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 29 Oct 2018 13:19:14 +0000 Subject: [PATCH] [Rework] Change time handling --- src/controller.c | 24 ++++++----------------- src/libserver/task.c | 40 +++++++++++++++++++++++++++++++++++---- src/libserver/task.h | 7 ++++--- src/lua/lua_task.c | 4 ++-- src/lua/lua_util.c | 3 +-- src/plugins/fuzzy_check.c | 4 ++-- src/rspamadm/lua_repl.c | 2 +- src/rspamd_proxy.c | 4 ++-- src/worker.c | 6 ++---- 9 files changed, 56 insertions(+), 38 deletions(-) diff --git a/src/controller.c b/src/controller.c index 26d12e14b..6875e1f3e 100644 --- a/src/controller.c +++ b/src/controller.c @@ -1522,10 +1522,9 @@ rspamd_controller_handle_lua_history (lua_State *L, if (lua_isfunction (L, -1)) { task = rspamd_task_new (session->ctx->worker, session->cfg, - session->pool, ctx->lang_det); + session->pool, ctx->lang_det, ctx->ev_base); task->resolver = ctx->resolver; - task->ev_base = ctx->ev_base; task->s = rspamd_session_create (session->pool, rspamd_controller_history_lua_fin_task, NULL, @@ -1823,11 +1822,9 @@ rspamd_controller_handle_lua (struct rspamd_http_connection_entry *conn_ent, } task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool, - ctx->lang_det); + ctx->lang_det, ctx->ev_base); task->resolver = ctx->resolver; - task->ev_base = ctx->ev_base; - task->s = rspamd_session_create (session->pool, rspamd_controller_lua_fin_task, NULL, @@ -2008,12 +2005,9 @@ rspamd_controller_handle_learn_common ( } task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool, - session->ctx->lang_det); + session->ctx->lang_det, ctx->ev_base); task->resolver = ctx->resolver; - task->ev_base = ctx->ev_base; - - task->s = rspamd_session_create (session->pool, rspamd_controller_learn_fin_task, NULL, @@ -2109,12 +2103,9 @@ rspamd_controller_handle_scan (struct rspamd_http_connection_entry *conn_ent, } task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool, - ctx->lang_det); - task->ev_base = session->ctx->ev_base; + ctx->lang_det, ctx->ev_base); task->resolver = ctx->resolver; - task->ev_base = ctx->ev_base; - task->s = rspamd_session_create (session->pool, rspamd_controller_check_fin_task, NULL, @@ -2600,9 +2591,8 @@ rspamd_controller_handle_stat_common ( ctx = session->ctx; task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool, - ctx->lang_det); + ctx->lang_det, ctx->ev_base); task->resolver = ctx->resolver; - task->ev_base = ctx->ev_base; cbdata = rspamd_mempool_alloc0 (session->pool, sizeof (*cbdata)); cbdata->conn_ent = conn_ent; cbdata->task = task; @@ -2963,11 +2953,9 @@ rspamd_controller_handle_lua_plugin (struct rspamd_http_connection_entry *conn_e } task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool, - ctx->lang_det); + ctx->lang_det, ctx->ev_base); task->resolver = ctx->resolver; - task->ev_base = ctx->ev_base; - task->s = rspamd_session_create (session->pool, rspamd_controller_lua_fin_task, NULL, diff --git a/src/libserver/task.c b/src/libserver/task.c index 061feaded..de2745701 100644 --- a/src/libserver/task.c +++ b/src/libserver/task.c @@ -62,8 +62,9 @@ rspamd_request_header_dtor (gpointer p) */ struct rspamd_task * rspamd_task_new (struct rspamd_worker *worker, struct rspamd_config *cfg, - rspamd_mempool_t *pool, - struct rspamd_lang_detector *lang_det) + rspamd_mempool_t *pool, + struct rspamd_lang_detector *lang_det, + struct event_base *ev_base) { struct rspamd_task *new_task; @@ -89,8 +90,23 @@ rspamd_task_new (struct rspamd_worker *worker, struct rspamd_config *cfg, } } + new_task->ev_base = ev_base; + +#ifdef HAVE_EVENT_NO_CACHE_TIME_FUNC + if (ev_base) { + event_base_update_cache_time (ev_base); + event_base_gettimeofday_cached (ev_base, &new_task->tv); + new_task->time_real = tv_to_double (&new_task->tv); + } + else { + gettimeofday (&new_task->tv, NULL); + new_task->time_real = tv_to_double (&new_task->tv); + } +#else gettimeofday (&new_task->tv, NULL); - new_task->time_real = rspamd_get_ticks (FALSE); + new_task->time_real = tv_to_double (&new_task->tv); +#endif + new_task->time_virtual = rspamd_get_virtual_ticks (); new_task->time_real_finish = NAN; new_task->time_virtual_finish = NAN; @@ -1645,8 +1661,24 @@ rspamd_task_profile_get (struct rspamd_task *task, const gchar *key) gboolean rspamd_task_set_finish_time (struct rspamd_task *task) { + struct timeval tv; + if (isnan (task->time_real_finish)) { - task->time_real_finish = rspamd_get_ticks (FALSE); + +#ifdef HAVE_EVENT_NO_CACHE_TIME_FUNC + if (task->ev_base) { + event_base_update_cache_time (task->ev_base); + event_base_gettimeofday_cached (task->ev_base, &tv); + task->time_real_finish = tv_to_double (&tv); + } + else { + gettimeofday (&tv, NULL); + task->time_real_finish = tv_to_double (&tv); + } +#else + gettimeofday (&tv, NULL); + task->time_real_finish = tv_to_double (&tv); +#endif task->time_virtual_finish = rspamd_get_virtual_ticks (); return TRUE; diff --git a/src/libserver/task.h b/src/libserver/task.h index c1eec96ed..005f6af26 100644 --- a/src/libserver/task.h +++ b/src/libserver/task.h @@ -212,9 +212,10 @@ struct rspamd_task { * Construct new task for worker */ struct rspamd_task *rspamd_task_new (struct rspamd_worker *worker, - struct rspamd_config *cfg, - rspamd_mempool_t *pool, - struct rspamd_lang_detector *lang_det); + struct rspamd_config *cfg, + rspamd_mempool_t *pool, + struct rspamd_lang_detector *lang_det, + struct event_base *ev_base); /** * Destroy task object and remove its IO dispatcher if it exists */ diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index 15af4cf5a..7aa4f3f18 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -1303,7 +1303,7 @@ lua_task_load_from_file (lua_State * L) err = strerror (errno); } else { - task = rspamd_task_new (NULL, cfg, NULL, NULL); + task = rspamd_task_new (NULL, cfg, NULL, NULL, NULL); task->msg.begin = map; task->msg.len = sz; rspamd_mempool_add_destructor (task->task_pool, @@ -1356,7 +1356,7 @@ lua_task_load_from_string (lua_State * L) } } - task = rspamd_task_new (NULL, cfg, NULL, NULL); + task = rspamd_task_new (NULL, cfg, NULL, NULL, NULL); task->msg.begin = g_strdup (str_message); task->msg.len = message_len; rspamd_mempool_add_destructor (task->task_pool, lua_task_free_dtor, task); diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index ace99048e..15f6c1b81 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -780,8 +780,7 @@ lua_util_process_message (lua_State *L) if (cfg != NULL && message != NULL) { base = event_init (); rspamd_init_filters (cfg, FALSE); - task = rspamd_task_new (NULL, cfg, NULL, NULL); - task->ev_base = base; + task = rspamd_task_new (NULL, cfg, NULL, NULL, base); task->msg.begin = rspamd_mempool_alloc (task->task_pool, mlen); rspamd_strlcpy ((gpointer)task->msg.begin, message, mlen); task->msg.len = mlen; diff --git a/src/plugins/fuzzy_check.c b/src/plugins/fuzzy_check.c index 1f16c5a48..79ffc70a5 100644 --- a/src/plugins/fuzzy_check.c +++ b/src/plugins/fuzzy_check.c @@ -3037,9 +3037,9 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent, struct fuzzy_ctx *fuzzy_module_ctx; /* Prepare task */ - task = rspamd_task_new (session->wrk, session->cfg, NULL, session->lang_det); + task = rspamd_task_new (session->wrk, session->cfg, NULL, + session->lang_det, conn_ent->rt->ev_base); task->cfg = ctx->cfg; - task->ev_base = conn_ent->rt->ev_base; saved = rspamd_mempool_alloc0 (session->pool, sizeof (gint)); err = rspamd_mempool_alloc0 (session->pool, sizeof (GError *)); fuzzy_module_ctx = fuzzy_get_context (ctx->cfg); diff --git a/src/rspamadm/lua_repl.c b/src/rspamadm/lua_repl.c index a43527823..6d1501100 100644 --- a/src/rspamadm/lua_repl.c +++ b/src/rspamadm/lua_repl.c @@ -431,7 +431,7 @@ rspamadm_lua_message_handler (lua_State *L, gint argc, gchar **argv) rspamd_printf ("cannot open %s: %s\n", argv[i], strerror (errno)); } else { - task = rspamd_task_new (NULL, rspamd_main->cfg, NULL, NULL); + task = rspamd_task_new (NULL, rspamd_main->cfg, NULL, NULL, NULL); if (!rspamd_task_load_message (task, NULL, map, len)) { rspamd_printf ("cannot load %s\n", argv[i]); diff --git a/src/rspamd_proxy.c b/src/rspamd_proxy.c index 77a5bb7e6..aed642752 100644 --- a/src/rspamd_proxy.c +++ b/src/rspamd_proxy.c @@ -1694,7 +1694,8 @@ rspamd_proxy_self_scan (struct rspamd_proxy_session *session) msg = session->client_message; task = rspamd_task_new (session->worker, session->ctx->cfg, - session->pool, session->ctx->lang_det); + session->pool, session->ctx->lang_det, + session->ctx->ev_base); task->flags |= RSPAMD_TASK_FLAG_MIME; task->sock = -1; @@ -1710,7 +1711,6 @@ rspamd_proxy_self_scan (struct rspamd_proxy_session *session) task->resolver = session->ctx->resolver; /* TODO: allow to disable autolearn in protocol */ task->flags |= RSPAMD_TASK_FLAG_LEARN_AUTO; - task->ev_base = session->ctx->ev_base; task->s = rspamd_session_create (task->task_pool, rspamd_proxy_task_fin, NULL, (event_finalizer_t )rspamd_task_free, task); data = rspamd_http_message_get_body (msg, &len); diff --git a/src/worker.c b/src/worker.c index 26ce63a94..544f05cbd 100644 --- a/src/worker.c +++ b/src/worker.c @@ -97,9 +97,8 @@ rspamd_worker_call_finish_handlers (struct rspamd_worker *worker) if (cfg->finish_callbacks) { ctx = worker->ctx; /* Create a fake task object for async events */ - task = rspamd_task_new (worker, cfg, NULL, NULL); + task = rspamd_task_new (worker, cfg, NULL, NULL, ctx->ev_base); task->resolver = ctx->resolver; - task->ev_base = ctx->ev_base; task->flags |= RSPAMD_TASK_FLAG_PROCESSING; task->s = rspamd_session_create (task->task_pool, rspamd_worker_finalize, @@ -364,7 +363,7 @@ accept_socket (gint fd, short what, void *arg) return; } - task = rspamd_task_new (worker, ctx->cfg, NULL, ctx->lang_det); + task = rspamd_task_new (worker, ctx->cfg, NULL, ctx->lang_det, ctx->ev_base); msg_info_task ("accepted connection from %s port %d, task ptr: %p", rspamd_inet_address_to_string (addr), @@ -395,7 +394,6 @@ accept_socket (gint fd, short what, void *arg) ctx->keys_cache, NULL); rspamd_http_connection_set_max_size (task->http_conn, task->cfg->max_message); - task->ev_base = ctx->ev_base; worker->nconns++; rspamd_mempool_add_destructor (task->task_pool, (rspamd_mempool_destruct_t)reduce_tasks_count, worker); -- 2.39.5