]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Allow to explicitly set events backend
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 27 Oct 2019 09:42:59 +0000 (09:42 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 27 Oct 2019 09:42:59 +0000 (09:42 +0000)
src/libserver/cfg_file.h
src/libserver/cfg_rcl.c
src/libserver/cfg_utils.c
src/libserver/worker_util.c
src/rspamd.c

index 3ee832d5ac7e72e9db283480972cb3cd4983bcb5..0aa8df36c34df92bd1d916fd17120d750bd2a827 100644 (file)
@@ -439,6 +439,7 @@ struct rspamd_config {
        gchar *history_file;                           /**< file to save rolling history                                                */
        gchar *tld_file;                               /**< file to load effective tld list from                                */
        gchar *hs_cache_dir;                           /**< directory to save hyperscan databases                               */
+       gchar *events_backend;                         /**< string representation of the events backend used    */
 
        gdouble dns_timeout;                            /**< timeout in milliseconds for waiting for dns reply  */
        guint32 dns_retransmits;                        /**< maximum retransmits count                                                  */
@@ -808,6 +809,9 @@ struct rspamd_action *rspamd_config_get_action (struct rspamd_config *cfg,
 struct rspamd_action *rspamd_config_get_action_by_type (struct rspamd_config *cfg,
                                                                                                                enum rspamd_action_type type);
 
+int rspamd_config_ev_backend_get (struct rspamd_config *cfg);
+const gchar * rspamd_config_ev_backend_to_string (int ev_backend, gboolean *effective);
+
 #define msg_err_config(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
         cfg->cfg_pool->tag.tagname, cfg->checksum, \
         G_STRFUNC, \
index 7936c872546b4dc56639703b4eefbdf98b1a20bd..bc2d50bb22b527caf09ee9b2babdb9d6585ea83e 100644 (file)
@@ -2207,6 +2207,12 @@ rspamd_rcl_config_init (struct rspamd_config *cfg, GHashTable *skip_sections)
                                G_STRUCT_OFFSET (struct rspamd_config, max_blas_threads),
                                RSPAMD_CL_FLAG_INT_32,
                                "Maximum number of Blas threads for learning neural networks (default: 1)");
+               rspamd_rcl_add_default_handler (sub,
+                               "events_backend",
+                               rspamd_rcl_parse_struct_string,
+                               G_STRUCT_OFFSET (struct rspamd_config, events_backend),
+                               0,
+                               "Events backend to use: kqueue, epoll, select, poll or auto (default: auto)");
 
                /* Neighbours configuration */
                rspamd_rcl_add_section_doc (&sub->subsections, "neighbours", "name",
index 98a50bea3d418c717da47b88395bd3791ac9cead..59840847b754ceed8af78725db6b521374fc2c2b 100644 (file)
@@ -2511,3 +2511,79 @@ rspamd_config_register_settings_id (struct rspamd_config *cfg,
                DL_APPEND (cfg->setting_ids, elt);
        }
 }
+
+int
+rspamd_config_ev_backend_get (struct rspamd_config *cfg)
+{
+       if (cfg == NULL || cfg->events_backend == NULL) {
+               return ev_supported_backends ();
+       }
+
+       if (strcmp (cfg->events_backend, "auto") == 0) {
+               return ev_supported_backends ();
+       }
+       else if (strcmp (cfg->events_backend, "epoll") == 0) {
+               if (ev_supported_backends () & EVBACKEND_EPOLL) {
+                       return EVBACKEND_EPOLL;
+               }
+               else {
+                       msg_warn_config ("unsupported events_backend: %s; defaulting to auto",
+                                       cfg->events_backend);
+                       return ev_supported_backends ();
+               }
+       }
+       else if (strcmp (cfg->events_backend, "kqueue") == 0) {
+               if (ev_supported_backends () & EVBACKEND_KQUEUE) {
+                       return EVBACKEND_KQUEUE;
+               }
+               else {
+                       msg_warn_config ("unsupported events_backend: %s; defaulting to auto",
+                                       cfg->events_backend);
+                       return ev_supported_backends ();
+               }
+       }
+       else if (strcmp (cfg->events_backend, "poll") == 0) {
+               return EVBACKEND_POLL;
+       }
+       else if (strcmp (cfg->events_backend, "select") == 0) {
+               return EVBACKEND_SELECT;
+       }
+       else {
+               msg_warn_config ("unknown events_backend: %s; defaulting to auto",
+                               cfg->events_backend);
+       }
+
+       return EVBACKEND_ALL;
+}
+
+const gchar *
+rspamd_config_ev_backend_to_string (int ev_backend, gboolean *effective)
+{
+#define SET_EFFECTIVE(b) do { if ((effective) != NULL) *(effective) = b; } while(0)
+
+       if ((ev_backend & EVBACKEND_ALL) == EVBACKEND_ALL) {
+               SET_EFFECTIVE (TRUE);
+               return "auto";
+       }
+
+       if (ev_backend & EVBACKEND_EPOLL) {
+               SET_EFFECTIVE (TRUE);
+               return "epoll";
+       }
+       if (ev_backend & EVBACKEND_KQUEUE) {
+               SET_EFFECTIVE (TRUE);
+               return "kqueue";
+       }
+       if (ev_backend & EVBACKEND_POLL) {
+               SET_EFFECTIVE (FALSE);
+               return "poll";
+       }
+       if (ev_backend & EVBACKEND_SELECT) {
+               SET_EFFECTIVE (FALSE);
+               return "select";
+       }
+
+       SET_EFFECTIVE (FALSE);
+       return "unknown";
+#undef SET_EFFECTIVE
+}
\ No newline at end of file
index 630d1fbbd4773388ba478085eeb0a5fcf48869b3..c45060aa6ce03f0a83a8c79302ec87d96fc989ca 100644 (file)
@@ -361,7 +361,8 @@ rspamd_prepare_worker (struct rspamd_worker *worker, const char *name,
        worker->signal_events = g_hash_table_new_full (g_direct_hash, g_direct_equal,
                        NULL, rspamd_sigh_free);
 
-       event_loop = ev_loop_new (EVBACKEND_ALL|EVFLAG_SIGNALFD);
+       event_loop = ev_loop_new (rspamd_config_ev_backend_get (worker->srv->cfg) |
+                       EVFLAG_SIGNALFD);
 
        worker->srv->event_loop = event_loop;
 
index f726865e798937618b16fda542bc52cbce703579..4b33e75770b74190868c6d52de1a2d6b4f6f3d5c 100644 (file)
@@ -1387,42 +1387,19 @@ main (gint argc, gchar **argv, gchar **env)
        rspamd_main->workers = g_hash_table_new (g_direct_hash, g_direct_equal);
 
        /* Init event base */
-       event_loop = ev_default_loop (EVFLAG_SIGNALFD|EVBACKEND_ALL);
+       event_loop = ev_default_loop (EVFLAG_SIGNALFD|
+                       rspamd_config_ev_backend_get (rspamd_main->cfg));
        rspamd_main->event_loop = event_loop;
 
        if (event_loop) {
-               unsigned loop_type = ev_backend (event_loop);
-               const gchar *loop_str = "unknown";
-               gboolean poor_backend = TRUE;
-
-               switch (loop_type) {
-               case EVBACKEND_EPOLL:
-                       loop_str = "epoll";
-                       poor_backend = FALSE;
-                       break;
-               case EVBACKEND_POLL:
-                       loop_str = "poll";
-                       break;
-               case EVBACKEND_SELECT:
-                       loop_str = "select";
-                       break;
-               case EVBACKEND_KQUEUE:
-                       loop_str = "kqueue";
-                       poor_backend = FALSE;
-                       break;
-               case EVBACKEND_PORT:
-                       loop_str = "port";
-                       poor_backend = FALSE;
-                       break;
-               case EVBACKEND_DEVPOLL:
-                       loop_str = "/dev/poll";
-                       poor_backend = FALSE;
-                       break;
-               default:
-                       break;
-               }
+               int loop_type = ev_backend (event_loop);
+               gboolean effective_backend;
+               const gchar *loop_str;
+
+               loop_str =
+                               rspamd_config_ev_backend_to_string (loop_type, &effective_backend);
 
-               if (poor_backend) {
+               if (!effective_backend) {
                        msg_warn_main ("event loop uses non-optimal backend: %s", loop_str);
                }
                else {