cpu : consider terminated app and multiple instance of same app 97/230097/3
authorKichan Kwon <k_c.kwon@samsung.com>
Wed, 8 Apr 2020 02:40:32 +0000 (11:40 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Wed, 8 Apr 2020 07:21:13 +0000 (16:21 +0900)
- Terminated app : ignore -> check until last bg time
- Multiple instance : consider as same app -> individual app

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

index 9b63c6e0e7945b8e442bd0f101b71f952c8a9a2c..6495993ab622a5028f4342126573b85af7df3170 100644 (file)
 "      GROUP BY appid, pid"    \
 "      HAVING MAX(time)"
 
+#define BUF_SIZE 512
+
 struct app_status {
-       pid_t pid;
-       time_t used_time;       // utime + stime
+       uint last_used_time;    // used_time = utime + stime
+       uint cur_used_time;
 };
 
 struct cpu_time {
@@ -183,6 +185,7 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
        ENTER;
 
        int ret;
+       char buf[BUF_SIZE];
 
        bm_cpu_st *usage_head = NULL;
        bm_cpu_st *usage = NULL;
@@ -196,7 +199,6 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
        /* Sqlite */
        sqlite3 *db;
        sqlite3_stmt *stmt;
-       char query[512];
 
        /* DB row */
        const char *appid;
@@ -204,16 +206,18 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
        unsigned int utime, stime, pid, status;
 
        /* App status */
-       struct app_status *app_status, *new_app_status;
+       struct app_status *app_status;
        int elapsed;
        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;
        app_time_map_st1 *app_time_map;
 
-
        /* Check argument */
        if (!handle || type != BM_DATA_TYPE_CPU) {
                _E("Invalid argument");
@@ -317,8 +321,8 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
 
        /* Read data from HEART-CPU DB */
        _I("Gather CPU usage : %ld ~ %ld", last_requested_time, current_time);
-       snprintf(query, 512, SQLITE_QUERY, last_requested_time, current_time);
-       ret = sqlite3_prepare_v2(db, query, -1, &stmt, 0);
+       snprintf(buf, BUF_SIZE, SQLITE_QUERY, last_requested_time, current_time);
+       ret = sqlite3_prepare_v2(db, buf, -1, &stmt, 0);
        while (sqlite3_step(stmt) == SQLITE_ROW) {
                appid = (const char *)sqlite3_column_text(stmt, 0);
                data = (const char *)sqlite3_column_text(stmt, 1);
@@ -326,118 +330,97 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
 
                //_D("appid(%s), utime(%u), stime(%u), pid(%u), status(%u)", appid, utime, stime, pid, status);
 
-               /* Ignore terminated app */
-               if (!is_running_app(appid)) {
-                       _D("%s is already terminated", appid);
-                       g_hash_table_remove(running_app_list, appid);
-                       continue;
-               }
-
-               app_status = g_hash_table_lookup(running_app_list, appid);
+               snprintf(buf, BUF_SIZE, "%s %u", appid, pid);
+               app_status = g_hash_table_lookup(running_app_list, buf);
 
                /* Insert newly launched app in the running app list */
                if (!app_status) {
-                       _D("%s is newly launched", appid);
-                       new_app_status = malloc(sizeof(struct app_status));
-                       if (!new_app_status) {
+                       _D("%s(PID=%u) is newly launched", appid, pid);
+                       app_status = malloc(sizeof(struct app_status));
+                       if (!app_status) {
                                _E("malloc failed");
                                ret = BM_PLUGIN_ERROR_OUT_OF_MEMORY;
                                goto clean_atm;
                        }
-                       new_app_status->pid = pid;
-                       new_app_status->used_time = JIFFY_TO_MS(utime + stime);
-                       g_hash_table_insert(running_app_list, g_strdup(appid), new_app_status);
-                       continue;
-               }
-
-               /* Initialize restarted app */
-               if (app_status->pid != pid) {
-                       _D("%s is restarted", appid);
-                       app_status->pid = pid;
-                       app_status->used_time = JIFFY_TO_MS(utime + stime);
-                       continue;
-               }
-
-               /* Ignore CPU unused app */
-               elapsed = JIFFY_TO_MS(utime + stime) - app_status->used_time;
-               if (elapsed <= 0) {
-                       _D("%s doesn't use CPU. Ignore it", appid);
+                       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;
                }
 
-               _D("%s uses CPU (+%d ms)", appid, elapsed);
-
-               /* Insert the app using CPU in the ATM list */
-               if (!g_slist_find_custom(usage->atm_list, appid, find_app_time)) {
-                       app_time_map = malloc(sizeof(app_time_map_st1));
-                       if (!app_time_map) {
-                               _E("malloc failed");
-                               ret = BM_PLUGIN_ERROR_OUT_OF_MEMORY;
-                               goto clean_atm;
-                       }
-                       app_time_map->app_id = g_strdup(appid);
-                       if (!app_time_map->app_id) {
-                               _E("g_strdup failed");
-                               ret = BM_PLUGIN_ERROR_OUT_OF_MEMORY;
-                               goto clean_atm;
-                       }
-                       app_time_map->time = elapsed;
-                       usage->atm_list = g_slist_append(usage->atm_list, app_time_map);
-               }
-
                /* Update app status */
-               app_status->used_time += elapsed;
+               app_status->cur_used_time = JIFFY_TO_MS(utime + stime);
        }
 
        /* Update all running apps with current value */
        g_hash_table_iter_init(&iter, running_app_list);
        while (g_hash_table_iter_next(&iter, &g_key, &g_val)) {
-               appid = (const char *)g_key;
+               data = (const char *)g_key;
                app_status = (struct app_status *)g_val;
-
-               /* Ignore terminated app */
-               if (!is_running_app(appid)) {
-                       _D("%s is already terminated", appid);
-                       g_hash_table_iter_remove(&iter);
-                       continue;
+               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;
                }
 
-               /* Get latest CPU time */
-               ret = runtime_info_get_process_cpu_usage(&app_status->pid, 1, &cpu_usage);
-               if (ret != RUNTIME_INFO_ERROR_NONE) {
-                       _E("runtime_info_get_process_cpu_usage for %u failed(%d)", app_status->pid, ret);
-                       continue;
+               if (!is_terminated) {
+                       /* Get latest CPU time */
+                       ret = runtime_info_get_process_cpu_usage((int *)&pid, 1, &cpu_usage);
+                       if (ret == RUNTIME_INFO_ERROR_NONE) {
+                               app_status->cur_used_time = JIFFY_TO_MS(cpu_usage->utime + cpu_usage->stime);
+                               free(cpu_usage);
+                       } else
+                               _W("runtime_info_get_process_cpu_usage for %u failed(%d)", pid, ret);
                }
 
                /* Ignore CPU unused app */
-               elapsed = JIFFY_TO_MS(cpu_usage->utime + cpu_usage->stime) - app_status->used_time;
-               free(cpu_usage);
+               elapsed = app_status->cur_used_time - app_status->last_used_time;
                if (elapsed <= 0) {
-                       _D("%s doesn't use CPU. Ignore it", appid);
+                       _D("%s(PID=%u) doesn't use CPU. Ignore it", buf, pid);
                        continue;
                }
 
-               _D("%s uses CPU (+%d ms)", appid, elapsed);
+               _I("%s(PID=%u) uses CPU (+%d ms)", buf, pid, elapsed);
 
                /* Insert the app using CPU in the ATM list */
-               if (!g_slist_find_custom(usage->atm_list, appid, find_app_time)) {
+               atm_elem = g_slist_find_custom(usage->atm_list, buf, find_app_time);
+               if (atm_elem)
+                       app_time_map = atm_elem->data;
+               else {
                        app_time_map = malloc(sizeof(app_time_map_st1));
                        if (!app_time_map) {
                                _E("malloc failed");
                                ret = BM_PLUGIN_ERROR_OUT_OF_MEMORY;
                                goto clean_atm;
                        }
-                       app_time_map->app_id = g_strdup(appid);
+                       app_time_map->app_id = g_strdup(buf);
                        if (!app_time_map->app_id) {
                                _E("g_strdup failed");
                                ret = BM_PLUGIN_ERROR_OUT_OF_MEMORY;
                                goto clean_atm;
                        }
-                       app_time_map->time = elapsed;
+                       app_time_map->time = 0;
                        usage->atm_list = g_slist_append(usage->atm_list, app_time_map);
                }
 
-               app_status->used_time += elapsed;
+               app_time_map->time += elapsed;
+
+               /* Remove terminated app's status */
+               if (is_terminated) {
+                       _D("%s(PID=%u) is already terminated", buf, pid);
+                       g_hash_table_iter_remove(&iter);
+               } else
+                       app_status->last_used_time = app_status->cur_used_time;
        }
 
        /* Update last requested time */