Sync Plugin Code & Enhancements 95/235195/2 accepted/tizen/unified/20200603.150023 submit/tizen/20200602.140526
authorDewal Agarwal <d1.agarwal@samsung.com>
Tue, 2 Jun 2020 12:15:52 +0000 (17:45 +0530)
committerDewal Agarwal <d1.agarwal@samsung.com>
Tue, 2 Jun 2020 12:18:50 +0000 (17:48 +0530)
Squashed commit from tizen_5.5 to tizen branch

commit f61bb48fdd7d46e49b6f0b0bb23ab7fc7c67b1f5
Author: Kichan Kwon <k_c.kwon@samsung.com>
Date:   Fri May 22 17:34:50 2020 +0900

    cpu : fix memory leak

    Change-Id: I1b34d9da3f41068c02a8410751e0bd42d40c189d
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
commit 1c64d0f278b3639b6eaf24bfd14c153ef8740ea3
Author: Kichan Kwon <k_c.kwon@samsung.com>
Date:   Thu May 21 19:17:50 2020 +0900

    bm-bt-plugin : free atm_list

    Change-Id: I48ab202e33d4ee473e67c5d29f79d52a1dac7a78
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
commit ec3ddc9f27cc610eda71f37cde4ce55e7694819f
Merge: dcaca1c 6cb41fb
Author: Kwon <k_c.kwon@samsung.com>
Date:   Fri Apr 10 11:19:42 2020 +0000

    Merge "cpu : consider terminated app and multiple instance of same app" into tizen_5.5

commit dcaca1c000ac3cc69ce272e8d7a90df197867ec8
Merge: f7833fc 89f71ef
Author: Kwon <k_c.kwon@samsung.com>
Date:   Fri Apr 10 11:19:39 2020 +0000

    Merge "cpu : enhance SQLite query" into tizen_5.5

commit f7833fcd2d8139b2647bbdf0f8aa7e162bcf4796
Author: Jaehyun Kim <jeik01.kim@samsung.com>
Date:   Fri Apr 10 11:36:25 2020 +0900

    Add the Rx / Tx time in Wi-Fi power model

    Change-Id: I5f220b1bb1e575df06891c1cd19c87679777f2bd
Signed-off-by: Jaehyun Kim <jeik01.kim@samsung.com>
commit 6cb41fb37ae07350e3b704b5173bd171bc7f3188
Author: Kichan Kwon <k_c.kwon@samsung.com>
Date:   Wed Apr 8 11:40:32 2020 +0900

    cpu : consider terminated app and multiple instance of same app

    - 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>
commit 89f71ef285532ba465041b4f03b49cdfd3d0a7e1
Author: Kichan Kwon <k_c.kwon@samsung.com>
Date:   Tue Apr 7 13:50:07 2020 +0900

    cpu : enhance SQLite query

    - Get the only latest row of each (appid,pid)

    Change-Id: Iebfc38816d494198e70c6aa24aedd539eb7e392d
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
Change-Id: Ibfa436af30882a45e75c8348f109f6b76386b028

plugin/bm-bt-plugin/src/bm_bt_plugin.c
plugin/common/include/bm_plugin_interface.h
plugin/cpu/src/bm_cpu_plugin.c
plugin/wifi/src/bm_wifi_plugin.c

index 9c1e93a..83660cb 100644 (file)
@@ -180,6 +180,8 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
                                bluetooth_record_head->bt_data_list, bluetooth_record);
        *handle = bluetooth_record_head;
 
+       g_slist_free_full(battery_data.atm_list, g_free);
+
        EXIT;
        return BM_PLUGIN_ERROR_NONE;
 }
index c2350a3..6a0aa21 100644 (file)
@@ -140,6 +140,8 @@ typedef struct {
        uint time_level_2;              /**< Total time in milliseconds during which rssi was on level 2 */
        uint time_level_3;              /**< Total time in milliseconds during which rssi was on level 3 */
        uint time_level_4;              /**< Total time in milliseconds during which rssi was on level 4 */
+       uint rxTime;                    /**< Total time in milliseconds (RX) mode */
+       uint txTime;                    /**< Total time in milliseconds (TX) mode */
        time_t startTime;               /**< start time of feature data collection session */
        time_t endTime;                 /**< end time of feature data collection session */
        uint scanTime;                  /**< Total time spent by the resource in (scan) mode during the session */
index 60bf00b..370c706 100644 (file)
 #define JIFFY_TO_MS_FACTOR 10  // 1000 / 100
 #define JIFFY_TO_MS(x) ((x) >= UINT_MAX / JIFFY_TO_MS_FACTOR ? UINT_MAX : (x) * JIFFY_TO_MS_FACTOR)
 
+#define DATA_FROM_2ND_TOKEN "SUBSTR(data, INSTR(data, ' ') + 1)"
+#define SQLITE_QUERY   \
+"SELECT"       \
+"      appid, data,"   \
+"      SUBSTR(data, len_utime + len_stime + 1, len_pid - 1) AS pid"    \
+"      FROM (SELECT *,"        \
+"              INSTR(data, ' ') AS len_utime," \
+"              INSTR(" DATA_FROM_2ND_TOKEN ", ' ') AS len_stime,"      \
+"              INSTR(SUBSTR(" DATA_FROM_2ND_TOKEN ", INSTR(" DATA_FROM_2ND_TOKEN ", ' ') + 1), ' ') AS len_pid"        \
+"              FROM cpu"       \
+"      )"      \
+"      WHERE time BETWEEN %ld AND %ld" \
+"      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 {
@@ -168,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;
@@ -181,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[128];
 
        /* DB row */
        const char *appid;
@@ -189,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");
@@ -267,6 +286,8 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
                        _W("g_dbus_connection_send_message_with_reply_sync failed");
        }
 
+       g_object_unref(msg_req);
+
        /* Open HEART-CPU DB */
        ret = sqlite3_open("/opt/usr/home/owner/.applications/dbspace/.resourced-heart-default.db", &db);
        if (ret != SQLITE_OK) {
@@ -302,8 +323,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, 128, "SELECT appid,data FROM cpu WHERE (time >= %ld AND time < %ld)", 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);
@@ -311,118 +332,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);
+                       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;
                }
 
-               /* 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);
-                       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 */
index 5c35fa0..0edb4b0 100755 (executable)
@@ -41,6 +41,8 @@
 #define BATTERY_WIFI_TIME_LEVEL_2   "time_level_2"
 #define BATTERY_WIFI_TIME_LEVEL_3   "time_level_3"
 #define BATTERY_WIFI_TIME_LEVEL_4   "time_level_4"
+#define BATTERY_WIFI_RX_TIME        "rx_time"
+#define BATTERY_WIFI_TX_TIME        "tx_time"
 #define BATTERY_WIFI_START_TIME     "start_time"
 #define BATTERY_WIFI_END_TIME       "end_time"
 #define BATTERY_WIFI_SCAN_TIME      "scan_time"
@@ -141,6 +143,10 @@ static void __bm_extract_wifi(const char *key, GVariant *value, void *user_data)
                wifi_data->time_level_3 = g_variant_get_uint32(value);
        } else if (!g_strcmp0(key, BATTERY_WIFI_TIME_LEVEL_4)) {
                wifi_data->time_level_4 = g_variant_get_uint32(value);
+       } else if (!g_strcmp0(key, BATTERY_WIFI_RX_TIME)) {
+               wifi_data->rxTime = g_variant_get_uint32(value);
+       } else if (!g_strcmp0(key, BATTERY_WIFI_TX_TIME)) {
+               wifi_data->txTime = g_variant_get_uint32(value);
        } else if (!g_strcmp0(key, BATTERY_WIFI_START_TIME)) {
                wifi_data->startTime = g_variant_get_uint32(value);
        } else if (!g_strcmp0(key, BATTERY_WIFI_END_TIME)) {
@@ -240,10 +246,10 @@ static void __bm_wifi_print_list(bm_data_h handle)
                GSList *atm_list = NULL;
                GSList *data_list = NULL;
 
-               _D("wifi start[%ld] end[%ld] scan[%d] rssi[%d/%d/%d/%d/%d]",
+               _D("wifi start[%ld] end[%ld] scan[%d] rssi[%d/%d/%d/%d/%d] rx[%d] tx[%d]",
                        wifi_data->startTime, wifi_data->endTime, wifi_data->scanTime,
                        wifi_data->time_level_0, wifi_data->time_level_1, wifi_data->time_level_2,
-                       wifi_data->time_level_3, wifi_data->time_level_4);
+                       wifi_data->time_level_3, wifi_data->time_level_4, wifi_data->rxTime, wifi_data->txTime);
 
                for (atm_list = wifi_data->atm_list; atm_list != NULL; atm_list = atm_list->next) {
                        app_time_map_st2 *app_data = (app_time_map_st2 *)atm_list->data;