From: Jaehyun Kim Date: Mon, 2 Jun 2025 12:31:56 +0000 (+0900) Subject: Fix excessive CPU usage issue in DNS monitoring X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fheads%2Ftizen;p=platform%2Fcore%2Fconnectivity%2Finm-manager.git Fix excessive CPU usage issue in DNS monitoring Change-Id: Ib437bf5ec0996e123fb54c428d3f90a353dc27e7 Signed-off-by: Jaehyun Kim --- diff --git a/packaging/inm-manager.spec b/packaging/inm-manager.spec index f87557d..7a13f84 100644 --- a/packaging/inm-manager.spec +++ b/packaging/inm-manager.spec @@ -2,7 +2,7 @@ Name: inm-manager Summary: INM(Intelligent Network Monitoring) daemon -Version: 0.0.34 +Version: 0.0.35 Release: 1 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/inm-dns-lookup.c b/src/inm-dns-lookup.c index a950338..cdb4ee1 100644 --- a/src/inm-dns-lookup.c +++ b/src/inm-dns-lookup.c @@ -36,6 +36,7 @@ #define ARES_DEFAULT_TRIAL_TIMEOUT_MS 500 #define ARES_DEFAULT_TIMEOUT_MS 5000 #define ARES_DEFAULT_TRY 10 +#define ARES_PROCESS_INTERVAL_USEC 100000 #define DEFAULT_TEST_URL "www.tizen.org" #define DNS_INTERVAL_MIN 1000 @@ -156,6 +157,8 @@ static gboolean __sock_channel_io_cb(GIOChannel *source, if (g_default_dns_lookup_data->is_lookup_finished) __destroy_default_dns_lookup_data(); + g_usleep(ARES_PROCESS_INTERVAL_USEC); + return TRUE; } @@ -292,33 +295,48 @@ static void __sock_state_cb(void *data, int fd, int read, int write) return; } -static int __ares_set_opt(inm_dns_lookup_s *lookup_data) +static int __ares_set_opt(inm_dns_lookup_s *lookup_data, int monitor_interval) { struct ares_options *p_opts = NULL; p_opts = g_try_malloc0(sizeof(struct ares_options)); if (!p_opts) { - INM_LOGI(""); + INM_LOGI("Out of memory"); return -1; } - p_opts->sock_state_cb = __sock_state_cb; - p_opts->sock_state_cb_data = (void *)lookup_data; - p_opts->timeout = ARES_DEFAULT_TRIAL_TIMEOUT_MS; - p_opts->tries = ARES_DEFAULT_TRY; + if (monitor_interval) { + int monitor_timeout = ARES_DEFAULT_TIMEOUT_MS; + + if (ARES_DEFAULT_TIMEOUT_MS >= monitor_interval) + monitor_timeout = (int)(monitor_interval * 0.8); + + p_opts->timeout = monitor_timeout; + p_opts->sock_state_cb = __sock_state_cb; + p_opts->sock_state_cb_data = (void *)lookup_data; + } else { + p_opts->sock_state_cb = __sock_state_cb; + p_opts->sock_state_cb_data = (void *)lookup_data; + p_opts->timeout = ARES_DEFAULT_TRIAL_TIMEOUT_MS; + p_opts->tries = ARES_DEFAULT_TRY; + } lookup_data->ch_options = p_opts; return 0; } -static inline int __init_ares_channel(inm_dns_lookup_s *dns_lookup_data) +static inline int __init_ares_channel(inm_dns_lookup_s *dns_lookup_data, int monitor_interval) { ares_channel ch; int opt_mask = 0; int ret; - opt_mask = ARES_OPT_TIMEOUTMS | ARES_OPT_TRIES | ARES_OPT_SOCK_STATE_CB; + if (monitor_interval) + opt_mask = ARES_OPT_TIMEOUTMS | ARES_OPT_SOCK_STATE_CB; + else + opt_mask = ARES_OPT_TIMEOUTMS | ARES_OPT_TRIES | ARES_OPT_SOCK_STATE_CB; + ret = ares_init_options(&ch, dns_lookup_data->ch_options, opt_mask); if (ret != ARES_SUCCESS) { PRINT_LOG("ares_init_options: %s\n", ares_strerror(ret)); @@ -331,26 +349,26 @@ static inline int __init_ares_channel(inm_dns_lookup_s *dns_lookup_data) return 0; } -static inline int __create_default_dns_lookup_data() +static inline int __create_default_dns_lookup_data(int monitor_interval) { inm_dns_lookup_s *dns_lookup_data = NULL; int ret; dns_lookup_data = g_try_malloc0(sizeof(inm_dns_lookup_s)); if (!dns_lookup_data) { - INM_LOGI(""); + INM_LOGI("Out of memory"); return -1; } - ret = __ares_set_opt(dns_lookup_data); + ret = __ares_set_opt(dns_lookup_data, monitor_interval); if (ret != 0) { - INM_LOGI(""); + INM_LOGI("__ares_set_opt() is failed"); g_free(dns_lookup_data); return -1; } - ret = __init_ares_channel(dns_lookup_data); + ret = __init_ares_channel(dns_lookup_data, monitor_interval); if (ret != 0) { - INM_LOGI(""); + INM_LOGI("__init_ares_channel() is failed"); g_free(dns_lookup_data->ch_options); g_free(dns_lookup_data); return -1; @@ -490,14 +508,14 @@ int inm_default_dns_state_monitor(void) __destroy_default_dns_lookup_data(); } - ret = __create_default_dns_lookup_data(); + interval = __get_dns_check_interval_value(); + + ret = __create_default_dns_lookup_data(interval); if (ret != 0) { INM_LOGI("Operation Failed"); return INM_DNS_LOOKUP_ERROR_OPERATION_FAILED; } - interval = __get_dns_check_interval_value(); - __dns_state_monitor(NULL); g_default_dns_lookup_data->ares_timer_source_id = @@ -535,7 +553,7 @@ int inm_default_dns_lookup_start(inm_default_dns_lookup_callback cb, gpointer us return INM_DNS_LOOKUP_ERROR_IN_PROGRESS; } - ret = __create_default_dns_lookup_data(); + ret = __create_default_dns_lookup_data(0); if (ret != 0) { INM_LOGI(""); return INM_DNS_LOOKUP_ERROR_OPERATION_FAILED;