Merge "Monitoring application wise background data usage." into tizen
[platform/core/connectivity/stc-manager.git] / src / monitor / stc-monitor.c
index b4f43b3..c2f0923 100755 (executable)
@@ -20,6 +20,7 @@
 #include "helper-nl.h"
 #include "helper-nfacct-rule.h"
 #include "helper-net-cls.h"
+#include "helper-cgroup.h"
 #include "counter.h"
 #include "table-statistics.h"
 #include "table-counters.h"
@@ -213,10 +214,12 @@ static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
        return FALSE;
 }
 
+#if 0
 static void __apps_tree_printall(void)
 {
        g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
 }
+#endif
 
 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
                                               gpointer data)
@@ -1126,10 +1129,8 @@ static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
        value.rst_state = info->rst_state;
        value.restriction_id = info->restriction_id;
 
-       if (value.rst_state != STC_RESTRICTION_EXCLUDED)
-               value.classid = get_classid_by_app_id(info->app_id ?
-                                                     info->app_id :
-                                                     STC_ALL_APP, TRUE);
+       if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
+               value.classid = get_classid_by_app_id(info->app_id, TRUE);
        else
                value.classid = STC_UNKNOWN_CLASSID;
 
@@ -1190,34 +1191,6 @@ static void __add_rstns_for_application(gchar *app_id)
                       app_id);
 }
 
-/* used only for (STC_ALL_APP/STC_BACKGROUND_APP_NAME) */
-static void __stc_monitor_add_application_by_app_id(const char *app_id)
-{
-       stc_app_key_s app_key;
-       stc_app_value_s app_value;
-
-       if (app_id == NULL)
-               return;
-
-       memset(&app_key, 0, sizeof(stc_app_key_s));
-       memset(&app_value, 0, sizeof(stc_app_value_s));
-
-       app_key.pkg_id = g_strdup(app_id);
-       app_key.app_id = g_strdup(app_id);
-
-       app_value.type = STC_APP_TYPE_NONE;
-       app_value.processes = NULL;
-       app_value.counter.in_bytes = 0;
-       app_value.counter.out_bytes = 0;
-
-       stc_monitor_application_add(app_key, app_value);
-
-       FREE(app_key.pkg_id);
-       FREE(app_key.app_id);
-
-       __apps_tree_printall();
-}
-
 stc_error_e stc_monitor_init(void)
 {
        stc_system_s *system = MALLOC0(stc_system_s, 1);
@@ -1225,6 +1198,9 @@ stc_error_e stc_monitor_init(void)
 
        ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
 
+       /* initializing cgroups */
+       cgroup_init();
+
        /* creating monitored application tree */
        system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
                                       __apps_tree_key_free,
@@ -1251,16 +1227,6 @@ stc_error_e stc_monitor_init(void)
 
        g_system = system;
 
-       /* create entry for STC_ALL_APP */
-       __stc_monitor_add_application_by_app_id(STC_ALL_APP);
-
-       /* create entry for STC_BACKGROUND_APP_NAME */
-       __stc_monitor_add_application_by_app_id(STC_BACKGROUND_APP_NAME);
-
-       /* create background cgroup */
-       g_system->background_pids = NULL;
-       create_net_background_cgroup(g_system->background_pids);
-
        /* creating restriction rules tree */
        __update_contr_cb(NULL);
 
@@ -1292,10 +1258,6 @@ stc_error_e stc_monitor_deinit(void)
                g_system->contr_timer_id = 0;
        }
 
-       /* free background pid list */
-       g_slist_free(g_system->background_pids);
-       g_system->background_pids = NULL;
-
        /* destroy monitored application tree */
        g_tree_destroy(g_system->apps);
        g_system->apps = NULL;
@@ -1407,10 +1369,6 @@ stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
        /* add pid to application cgroup */
        place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
 
-       /* add pid to background cgroup if gound state is background */
-       if (proc_value.ground == STC_APP_STATE_BACKGROUND)
-               add_pid_to_background_cgroup(proc_key.pid);
-
        return ret;
 }
 
@@ -1429,9 +1387,6 @@ stc_error_e stc_monitor_process_remove(pid_t pid)
 
        ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
 
-       /* remove pid from background cgroup */
-       remove_pid_from_background_cgroup(pid);
-
        g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
                       &context);
 
@@ -1466,10 +1421,15 @@ stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
        if (proc_lookup->ground != ground)
                proc_lookup->ground = ground;
 
-       if (ground == STC_APP_STATE_BACKGROUND)
-               add_pid_to_background_cgroup(proc_key.pid);
-       else
+       if (ground == STC_APP_STATE_BACKGROUND) {
+               char *background_app_id = g_strconcat(app_key.app_id,
+                                                    STC_BACKGROUND_APP_SUFFIX,
+                                                    NULL);
+               place_pids_to_net_cgroup(proc_key.pid, background_app_id);
+               g_free(background_app_id);
+       } else {
                place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
+       }
 
        return ret;
 }
@@ -1532,10 +1492,8 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
        value.rst_state = info->rst_state;
        value.restriction_id = info->restriction_id;
 
-       if (value.rst_state != STC_RESTRICTION_EXCLUDED)
-               value.classid = get_classid_by_app_id(info->app_id ?
-                                                     info->app_id :
-                                                     STC_ALL_APP, TRUE);
+       if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
+               value.classid = get_classid_by_app_id(info->app_id, TRUE);
        else
                value.classid = STC_UNKNOWN_CLASSID;