cpu : check app termination from callback instead of request 91/251191/5 accepted/tizen_5.5_unified tizen_5.5 accepted/tizen/5.5/unified/20210113.071914 submit/tizen_5.5/20210111.093941
authorKichan Kwon <k_c.kwon@samsung.com>
Mon, 11 Jan 2021 06:06:32 +0000 (15:06 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Mon, 11 Jan 2021 08:39:21 +0000 (17:39 +0900)
- If app is not terminated, batterymonitor will endlessly request
  to AMD to check whether app is terminated or not
- To prevent overload, register callback and set app termination flag

Change-Id: I68c8ae212c892d02030f72353b9c69ecda87391e
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
plugin/cpu/src/bm_cpu_plugin.c

index 285b051c44b7a68d2f0be4acf773bcfe0825baca..ead2e764512ecd0095b06f06d11a520c1ab5380c 100644 (file)
@@ -53,6 +53,7 @@
 struct app_status {
        uint last_used_time;    // used_time = utime + stime
        uint cur_used_time;
+       bool is_terminated;
 };
 
 struct cpu_time {
@@ -86,16 +87,45 @@ void free_atm_st1(gpointer data)
        free(atm);
 }
 
-bool is_running_app(const char *appid)
+void check_app_termination(app_context_h app_context, app_context_event_e event, void *user_data)
 {
-       bool is_running = false;
-       int ret = app_manager_is_running(appid, &is_running);
+       int ret;
+       pid_t pid;
+       char *appid = NULL;
+       struct app_status *app_status = NULL;
+       char buf[BUF_SIZE];
+
+       if (event != APP_CONTEXT_EVENT_TERMINATED)
+               return;
+
+       ret = app_context_get_pid(app_context, &pid);
        if (ret != APP_MANAGER_ERROR_NONE) {
-               _E("app_manager_is_running failed (%d)", ret);
-               return false;
+               _E("app_context_get_pid failed (%d)", ret);
+               return;
+       }
+       ret = app_context_get_app_id(app_context, &appid);
+       if (ret != APP_MANAGER_ERROR_NONE) {
+               _E("app_context_get_app_id failed (%d)", ret);
+               return;
+       }
+
+       snprintf(buf, BUF_SIZE, "%s %u", appid, pid);
+       app_status = g_hash_table_lookup(running_app_list, buf);
+       if (!app_status) {
+               _D("Add running app : %s(PID=%u)", appid, pid);
+               app_status = calloc(1, sizeof(struct app_status));
+               if (!app_status) {
+                       _E("calloc failed");
+                       goto clean_appid;
+               }
+               g_hash_table_insert(running_app_list, g_strdup(buf), app_status);
        }
 
-       return is_running;
+       _D("%s(PID=%u) is terminated", appid, pid);
+       app_status->is_terminated = true;
+
+clean_appid:
+       free(appid);
 }
 
 int get_cpu_time(struct cpu_time *cpu_time)
@@ -134,6 +164,8 @@ int deinit()
                running_app_list = NULL;
        }
 
+       app_manager_unset_app_context_event_cb();
+
        EXIT;
        return BM_PLUGIN_ERROR_NONE;
 }
@@ -170,6 +202,12 @@ int init()
                goto failed;
        }
 
+       ret = app_manager_set_app_context_event_cb(check_app_termination, NULL);
+       if (ret != APP_MANAGER_ERROR_NONE) {
+               _E("app_manager_set_app_context_event_cb failed (%d)", ret);
+               goto failed;
+       }
+
        EXIT;
        return BM_PLUGIN_ERROR_NONE;
 
@@ -211,8 +249,6 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
        process_cpu_usage_s *cpu_usage;
        GHashTableIter iter;
        gpointer g_key, g_val;
-       char *cur_appid;
-       int is_terminated;
 
        /* App time map */
        GSList *atm_elem;
@@ -337,17 +373,14 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
 
                /* Insert newly launched app in the running app list */
                if (!app_status) {
-                       _D("%s(PID=%u) is newly launched", appid, pid);
-                       app_status = malloc(sizeof(struct app_status));
+                       _D("Add running app : %s(PID=%u)", appid, pid);
+                       app_status = calloc(1, sizeof(struct app_status));
                        if (!app_status) {
-                               _E("malloc failed");
+                               _E("calloc failed");
                                ret = BM_PLUGIN_ERROR_OUT_OF_MEMORY;
                                goto clean_atm;
                        }
-                       app_status->last_used_time = 0;
-                       app_status->cur_used_time = JIFFY_TO_MS(utime + stime);
                        g_hash_table_insert(running_app_list, g_strdup(buf), app_status);
-                       continue;
                }
 
                /* Update app status */
@@ -359,23 +392,9 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
        while (g_hash_table_iter_next(&iter, &g_key, &g_val)) {
                data = (const char *)g_key;
                app_status = (struct app_status *)g_val;
-               is_terminated = 0;
                sscanf(data, "%s %u", buf, &pid);
 
-               /* Check whether app is running */
-               ret = app_manager_get_app_id(pid, &cur_appid);
-               switch (ret) {
-               case APP_MANAGER_ERROR_NONE:
-                       if (strncmp(buf, cur_appid, strlen(cur_appid) + 1))
-                               is_terminated = 1;
-                       free(cur_appid);
-                       break;
-               default:
-                       is_terminated = 1;
-                       break;
-               }
-
-               if (!is_terminated) {
+               if (!app_status->is_terminated) {
                        /* Get latest CPU time */
                        ret = runtime_info_get_process_cpu_usage((int *)&pid, 1, &cpu_usage);
                        if (ret == RUNTIME_INFO_ERROR_NONE) {
@@ -419,12 +438,13 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
 
 update_list:
                /* Remove terminated app's status */
-               if (is_terminated) {
-                       _D("%s(PID=%u) is already terminated", buf, pid);
+               if (app_status->is_terminated) {
+                       _D("Remove running app : %s(PID=%u)", buf, pid);
                        g_hash_table_iter_remove(&iter);
                } else
                        app_status->last_used_time = app_status->cur_used_time;
        }
+       _I("The number of running apps : %u", g_hash_table_size(running_app_list));
 
        /* Update last requested time */
        last_requested_time = current_time;