Allocate congestion monitoring memory when it starts 40/194740/1
authorYu <jiung.yu@samsung.com>
Fri, 7 Dec 2018 05:52:23 +0000 (14:52 +0900)
committerYu <jiung.yu@samsung.com>
Fri, 7 Dec 2018 05:52:41 +0000 (14:52 +0900)
Change-Id: Ifbf2896370d18782f50115594b94ce8a085a1669
Signed-off-by: Yu Jiung <jiung.yu@samsung.com>
src/inm-congestion-mon.c

index bb566d57ea8727922a1c300ca373baf6a7ec16df..d6b4390c2ba91eeaf6dcffab4827c8f874422a55 100644 (file)
 #define NETWORK_CONGESTION_MON         "/proc/net/snmp"
 #define NETWORK_CONG_MON_INTERVAL      10*1000
 
-typedef struct {
-       gboolean is_initialized;
-       GHashTable *monitoring_cbs;
-} congestion_mon_s;
-
 typedef struct {
        guint timer_source_id;
        inm_congestion_mon_callback cb;
        gpointer cb_user_data;
-} congestion_mon_data_s;
-
-congestion_mon_s cong_mon;
-static __thread congestion_mon_data_s *data = NULL;
-static __thread gboolean g_cong_mon_started;
-
-static void __destroy_cong_mon_data(gpointer user_data)
-{
-       __INM_FUNC_ENTER__;
-       congestion_mon_data_s *mon_data = (congestion_mon_data_s *)user_data;
+} congestion_mon_s;
 
-       g_source_remove(mon_data->timer_source_id);
-       g_free(mon_data);
-       __INM_FUNC_EXIT__;
-       return;
-}
+static congestion_mon_s *g_p_cong_mon = NULL;
 
 static int __inm_check_congestion_status(int *status)
 {
@@ -161,7 +143,7 @@ static gboolean __inm_cong_mon_status_callback(gpointer user_data)
 {
        int status;
 
-       if (!g_cong_mon_started) {
+       if (!g_p_cong_mon) {
                INM_LOGW("congestion monitor is stopped");
                return FALSE;
        }
@@ -172,18 +154,23 @@ static gboolean __inm_cong_mon_status_callback(gpointer user_data)
        }
        INM_LOGI("Network Congestion Status:[%d]", status);
 
-       if (data->cb)
-               data->cb(status, data->cb_user_data);
+       if (g_p_cong_mon->cb)
+               g_p_cong_mon->cb(status, g_p_cong_mon->cb_user_data);
 
        return TRUE;
 }
 
-static int __inm_attatch_cong_mon_file(congestion_mon_data_s *data)
+static int __inm_attatch_cong_mon_file()
 {
        __INM_FUNC_ENTER__;
 
-       data->timer_source_id = g_timeout_add(NETWORK_CONG_MON_INTERVAL,
-                       __inm_cong_mon_status_callback, data);
+       if (!g_p_cong_mon) {
+               INM_LOGW("congestion monitor is not started");
+               return -1;
+       }
+
+       g_p_cong_mon->timer_source_id = g_timeout_add(NETWORK_CONG_MON_INTERVAL,
+                       __inm_cong_mon_status_callback, NULL);
 
        __INM_FUNC_EXIT__;
        return 0;
@@ -193,16 +180,16 @@ int inm_congestion_monitor_start(inm_congestion_mon_callback cb, void *user_data
 {
        __INM_FUNC_ENTER__;
 
-       if (!cong_mon.is_initialized) {
-               INM_LOGW("congestion monitor is not initialized");
+       if (g_p_cong_mon) {
+               INM_LOGW("congestion monitor is started");
                __INM_FUNC_EXIT__;
-               return INM_CONG_MON_NOT_INITIALIZED;
+               return INM_CONG_MON_ALREADY_STARTED;
        }
 
-       if (g_cong_mon_started) {
-               INM_LOGW("congestion monitor is already started");
+       g_p_cong_mon = g_try_malloc0(sizeof(congestion_mon_s));
+       if (!g_p_cong_mon) {
                __INM_FUNC_EXIT__;
-               return INM_CONG_MON_ALREADY_STARTED;
+               return INM_CONG_MON_OPERATION_FAILED;
        }
 
        if (!cb) {
@@ -211,22 +198,14 @@ int inm_congestion_monitor_start(inm_congestion_mon_callback cb, void *user_data
                return INM_CONG_MON_INVALID_PARAMETER;
        }
 
-       data = (congestion_mon_data_s *)g_try_malloc0(sizeof(congestion_mon_data_s));
-       if (!data) {
-               INM_LOGE("Failed to allocate memory");
-               __INM_FUNC_EXIT__;
-               return INM_CONG_MON_OPERATION_FAILED;
-       }
+       g_p_cong_mon->cb = cb;
+       g_p_cong_mon->cb_user_data = user_data;
 
-       data->cb = cb;
-       data->cb_user_data = user_data;
-
-       if (__inm_attatch_cong_mon_file(data) < 0) {
+       if (__inm_attatch_cong_mon_file() < 0) {
                INM_LOGE("Failed to attach congestion monitor file");
                __INM_FUNC_EXIT__;
                return INM_CONG_MON_OPERATION_FAILED;
        }
-       g_cong_mon_started = TRUE;
 
        __INM_FUNC_EXIT__;
        return INM_CONG_MON_ERROR_NONE;
@@ -237,12 +216,15 @@ int inm_congestion_monitor_stop()
        int ret = INM_CONG_MON_ERROR_NONE;
        __INM_FUNC_ENTER__;
 
-       if (!cong_mon.is_initialized) {
+       if (!g_p_cong_mon) {
                INM_LOGE("congestion monitor is not initialized");
                __INM_FUNC_EXIT__;
                return INM_CONG_MON_NOT_INITIALIZED;
        }
-       g_cong_mon_started = FALSE;
+
+       REMOVE_G_SOURCE(g_p_cong_mon->timer_source_id);
+       g_free(g_p_cong_mon);
+       g_p_cong_mon = NULL;
 
        __INM_FUNC_EXIT__;
        return ret;
@@ -253,19 +235,9 @@ int inm_congestion_mon_init()
        int ret = INM_CONG_MON_ERROR_NONE;
        __INM_FUNC_ENTER__;
 
-       memset(&cong_mon, 0x00, sizeof(cong_mon));
-       cong_mon.is_initialized = TRUE;
-
-       cong_mon.monitoring_cbs = g_hash_table_new_full(g_str_hash,
-                       g_str_equal,
-                       g_free,
-                       __destroy_cong_mon_data
-                       );
-
-       if (!cong_mon.monitoring_cbs) {
-               cong_mon.is_initialized = FALSE;
+       if (g_p_cong_mon) {
                __INM_FUNC_EXIT__;
-               return INM_CONG_MON_OPERATION_FAILED;
+               return ret;
        }
 
        __INM_FUNC_EXIT__;
@@ -277,15 +249,14 @@ int inm_congestion_mon_deinit()
        int ret = INM_CONG_MON_ERROR_NONE;
        __INM_FUNC_ENTER__;
 
-       if (!cong_mon.is_initialized) {
+       if (!g_p_cong_mon) {
                __INM_FUNC_EXIT__;
-               return INM_CONG_MON_NOT_INITIALIZED;
+               return ret;
        }
 
-       cong_mon.is_initialized = FALSE;
-       g_hash_table_remove_all(cong_mon.monitoring_cbs);
-       g_hash_table_unref(cong_mon.monitoring_cbs);
-       cong_mon.monitoring_cbs = NULL;
+       REMOVE_G_SOURCE(g_p_cong_mon->timer_source_id);
+       g_free(g_p_cong_mon);
+       g_p_cong_mon = NULL;
 
        __INM_FUNC_EXIT__;
        return ret;