Add CPU stats for Battery Dump 35/227935/1
authorDewal Agarwal <d1.agarwal@samsung.com>
Tue, 17 Mar 2020 10:00:06 +0000 (15:30 +0530)
committerDewal Agarwal <d1.agarwal@samsung.com>
Tue, 17 Mar 2020 10:00:06 +0000 (15:30 +0530)
Change-Id: Iaaf36bea45b830c4876892e948a859b8c76e0518

include/bd_private.h
include/bm_server_db.h
packaging/batterymonitor.spec
src/battery_dump/bd_history_item.c
src/bm_power_engine.c
src/bm_server_db.c

index 0481866..09f5873 100644 (file)
@@ -97,6 +97,13 @@ typedef struct _gbl_bst_stat_s {
        long int high_time;
 } bd_gl_bst_stat_s;
 
+typedef struct _gbl_cpu_stat_s {
+       long int time_s;
+       long int usr_time;
+       long int sys_time;
+} bd_gl_cpu_stat_s;
+
+/*
 typedef struct _unt_ntw_stat_s {
        long int time_s;
        char *appid;
@@ -108,6 +115,7 @@ typedef struct _unt_ntw_stat_s {
        long int bt_tx_byt;
        long int app_rt;
 } bd_ul_ntw_stat_s;
+*/
 
 /* Battery Dump db enumerations */
 typedef enum {
@@ -170,6 +178,13 @@ typedef enum {
 } BD_GBSTSTAT_DB_IDX;
 
 typedef enum {
+       BD_GCPUSTAT_NONE = -1,
+       BD_GCPUSTAT_USR,
+       BD_GCPUSTAT_SYS,
+} BD_GCPUSTAT_DB_IDX;
+
+/*
+typedef enum {
        BD_UNTWSTAT_NONE = -1,
        BD_UNTWSTAT_APP,
        BD_UNTWSTAT_DN_RXB,
@@ -178,6 +193,7 @@ typedef enum {
        BD_UNTWSTAT_WIFI_TXB,
        BD_UNTWSTAT_RT,
 } BD_UNTWSTAT_DB_IDX;
+*/
 
 /* Battery Specific Enumeration */
 typedef enum {
index a538b20..c6004a8 100644 (file)
@@ -63,10 +63,7 @@ BM_RESOURCE_ID_MAX,
 "rId_device_network INT, "\
 "rId_gps INT, "\
 "rId_hrm INT, "\
-"rId_battery INT, "\
-"rId_newone INT, "\
-"rId_newtwo INT, "\
-"rId_newthree INT "\
+"rId_battery INT "\
 ");"
 
 /* Battery Monitor RESOURCE ID Vs TIME */
@@ -155,6 +152,13 @@ BM_RESOURCE_ID_MAX,
 "high_time INT "\
 ");"
 
+#define GBL_CPU_STAT_SCHEMA "create table gcpu_stat \n"\
+       "(\n"\
+"time_s LONG INT, "\
+"usr_time INT, "\
+"sys_time INT "\
+");"
+
 #define OWNER_ROOT 0
 
 /* Macros of Time Duration */
@@ -173,9 +177,10 @@ BM_RESOURCE_ID_MAX,
 #define BATTERY_MONITOR_GBL_SGT_STAT_TABLE             "gsgt_stat"
 #define BATTERY_MONITOR_GBL_WST_STAT_TABLE             "gwst_stat"
 #define BATTERY_MONITOR_GBL_BST_STAT_TABLE             "gbst_stat"
+#define BATTERY_MONITOR_GBL_CPU_STAT_TABLE             "gcpu_stat"
 #define BATTERY_MONITOR_SQLITE_SEQ                     "sqlite_sequence"
 #define BATTERY_MONITOR_SQL_LEN_MAX                    1024
-#define BATTERY_MONITOR_TABLE_COUNT                    10
+#define BATTERY_MONITOR_TABLE_COUNT                    11
 
 typedef sqlite3_stmt *bm_stmt;
 
index 832b79d..437aef7 100644 (file)
@@ -88,7 +88,7 @@ then
        sqlite3 %{TZ_SYS_DB}/.battery-monitor.db 'PRAGMA journal_mode = PERSIST;
        CREATE TABLE if not exists appid_map (id INTEGER PRIMARY KEY AUTOINCREMENT, AppId TEXT);
        CREATE TABLE if not exists appid_usage (AppId TEXT, log_time INT, rId_ble INT, rId_wifi INT, rId_cpu INT, rId_display INT, rId_device_network INT,
-       rId_gps INT, rId_hrm INT, rId_battery INT, rId_newone INT, rId_newtwo INT, rId_newthree INT);
+       rId_gps INT, rId_hrm INT, rId_battery INT);
        CREATE TABLE if not exists resourceid_usage (ResourceId TEXT, log_time INT, usage INT);
        CREATE TABLE if not exists gble_stat (time_s LONG INT, ble_idle_time INT, ble_rx_time INT, ble_tx_time INT, ble_pwi_val INT);
        CREATE TABLE if not exists gwfl_stat (time_s LONG INT, wifi_on_time INT, wifi_conn_time INT, wifi_idle_time INT,
@@ -101,6 +101,7 @@ then
        CREATE TABLE if not exists gwst_stat (time_s LONG INT, wscan_time INT, wnone_time INT, wpoor_time INT, wmed_time INT,
        wgood_time INT, wgrt_time INT);
        CREATE TABLE if not exists gbst_stat (time_s LONG INT, off_time INT, low_time INT, med_time INT, high_time INT);
+       CREATE TABLE if not exists gcpu_stat (time_s LONG INT, usr_time INT, sys_time INT);
 '
 fi
 
index 6957e16..aef2104 100644 (file)
@@ -539,6 +539,49 @@ static int bd_get_and_print_gwst_stat_entry_from_dump(int fd, long int duration)
        return ret;
 }
 
+static int bd_get_and_print_gcpu_stat_entry_from_dump(int fd, long int duration)
+{
+       ENTER;
+       bd_gl_cpu_stat_s *gcpu_stat = (bd_gl_cpu_stat_s *)calloc(1, sizeof(bd_gl_cpu_stat_s));
+       BM_CHECK_MEM_ALLOC(gcpu_stat, {});
+
+       int ret = BATTERY_MONITOR_ERROR_NONE;
+       const char *rid_str = NULL; int usage = 0;
+       rid_str = bm_get_resource_id_string(2);
+       resourceid_usage_s *res_use =
+               bm_server_query_resource_usage_resourceid(rid_str, 1, &ret);
+       if (res_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
+               _ERR("For resourceid %s", rid_str);
+               _DBG("Dump only system stats");
+       }
+       usage = res_use->usage;
+       float usagemah = (float)usage/3600;
+       ret = bm_server_battery_dump_query_from_db(gcpu_stat, 7, duration);
+       if (ret != BATTERY_MONITOR_ERROR_NONE) {
+               _ERR("Unable to fetch data from DB");
+               BM_FREE(gcpu_stat);
+               BM_FREE(res_use);
+               return ret;
+       }
+       GString *dump_p = g_string_sized_new(30);
+       BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gcpu_stat); BM_FREE(res_use);});
+       g_string_append(dump_p, TIZEN_DUMP_VERSION);
+       g_string_append(dump_p, ",0,l,");
+       g_string_append(dump_p, "cpu,");
+       g_string_append_printf(dump_p, "%ld,", gcpu_stat->usr_time);
+       g_string_append_printf(dump_p, "%ld,", gcpu_stat->sys_time);
+       g_string_append_printf(dump_p, "%0.2f", usagemah);
+       g_string_append(dump_p, "\n");
+       bm_resourceid_usage_free(res_use);
+       _DBG("%s", dump_p->str);
+       if (write(fd, dump_p->str, dump_p->len) < 0)
+               _WARN("write error");
+       g_free(dump_p->str);
+
+       EXIT;
+       return ret;
+}
+
 static int bd_get_and_print_uid_info_entry_from_dump(int fd)
 {
        ENTER;
@@ -1122,6 +1165,8 @@ int bd_print_history_item_main(int fd, int num_h_items, long long base_time, boo
                ret = bd_get_and_print_gwst_stat_entry_from_dump(fd, duration);
                if (ret != BATTERY_MONITOR_ERROR_NONE)
                        _ERR("GWST data dump failed");
+               ret = bd_get_and_print_gcpu_stat_entry_from_dump(fd, duration);
+                       _ERR("GCPU data dump failed");
                // Battery Usage Dump
                // Add usage for each of the resource
                // and each of the application
index eded0b8..670561e 100644 (file)
@@ -1086,6 +1086,19 @@ int bm_cpu_calc_power_and_commit(bm_cpu_st *handle, bool mode)
        bm_appid_usage_free(app_usage);
        bm_resourceid_usage_free(res_usage);
 
+       bd_gl_cpu_stat_s *gcpu_st = (bd_gl_cpu_stat_s *)calloc(1, sizeof(bd_gl_cpu_stat_s));
+       if (gcpu_st == NULL) {
+               _ERR("memory allocation failed");
+               return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
+       }
+       gcpu_st->time_s = ret_time; gcpu_st->usr_time = uTime;
+       gcpu_st->sys_time = sTime;
+       ret_val = bm_server_battery_dump_insert_to_db(gcpu_st, 7);
+       if (ret_val != BATTERY_MONITOR_ERROR_NONE)
+               _ERR("Insert GCPU failed");
+       else
+               _DBG("Insert GCPU successful");
+       BM_FREE(gcpu_st);
        /* Free the Hash Map */
        g_hash_table_destroy(hash);
 
index 32fe62e..933dc23 100644 (file)
@@ -496,6 +496,19 @@ static int bm_gl_bst_stat_convert_to_sql(bd_gl_bst_stat_s *gbst_st, bm_stmt hstm
        return count;
 }
 
+static int bm_gl_cpu_stat_convert_to_sql(bd_gl_cpu_stat_s *gcpu_st, bm_stmt hstmt, char *sql_value)
+{
+       ENTER;
+
+       int count = 1;
+       bm_query_bind_int(hstmt, count++, gcpu_st->time_s);
+       bm_query_bind_int(hstmt, count++, gcpu_st->usr_time);
+       bm_query_bind_int(hstmt, count++, gcpu_st->sys_time);
+
+       EXIT;
+       return count;
+}
+
 static void bm_convert_column_to_resourceid_usage(bm_stmt hstmt, resourceid_usage_s* bm_resource_type)
 {
        ENTER;
@@ -651,6 +664,17 @@ static void bm_convert_column_to_gl_bst_stat(bm_stmt hstmt, bd_gl_bst_stat_s *gb
        return;
 }
 
+static void bm_convert_column_to_gl_cpu_stat(bm_stmt hstmt, bd_gl_cpu_stat_s *gcpu_st)
+{
+       ENTER;
+       gcpu_st->time_s = -1;
+       gcpu_st->usr_time = bm_query_table_column_int(hstmt, BD_GCPUSTAT_USR);
+       gcpu_st->sys_time = bm_query_table_column_int(hstmt, BD_GCPUSTAT_SYS);
+
+       EXIT;
+       return;
+}
+
 static int bm_get_record_count(sqlite3 *bm_db_handle, const char *query)
 {
        ENTER;
@@ -710,12 +734,13 @@ static int bm_check_is_all_table_exists(sqlite3 *bm_db_handle)
        BM_MEMSET(query, 0, sizeof(query));
 
        BM_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in \
-                       ('%s', '%s', '%s', '%s','%s', '%s', '%s', '%s','%s', '%s')",
+                       ('%s', '%s', '%s', '%s','%s', '%s', '%s', '%s','%s', '%s', '%s')",
                        BATTERY_MONITOR_APP_ID_MAP_TABLE, BATTERY_MONITOR_APP_ID_USAGE_TABLE,
                        BATTERY_MONITOR_RESOURCE_ID_USAGE_TABLE, BATTERY_MONITOR_GBL_BLE_STAT_TABLE,
                        BATTERY_MONITOR_GBL_WFL_STAT_TABLE, BATTERY_MONITOR_GBL_NT_STAT_TABLE,
                        BATTERY_MONITOR_GBL_BR_STAT_TABLE, BATTERY_MONITOR_GBL_SGT_STAT_TABLE,
-                       BATTERY_MONITOR_GBL_WST_STAT_TABLE, BATTERY_MONITOR_GBL_BST_STAT_TABLE);
+                       BATTERY_MONITOR_GBL_WST_STAT_TABLE, BATTERY_MONITOR_GBL_BST_STAT_TABLE,
+                       BATTERY_MONITOR_GBL_CPU_STAT_TABLE);
 
        rc = bm_get_record_count(bm_db_handle, query);
 
@@ -857,6 +882,18 @@ static int bm_create_all_tables(sqlite3 *bm_db_handle)
                                ("bm_execute_query(bm_db_handle, %s) failed(%d, %s).\n",
                                 GBL_BST_STAT_SCHEMA, rc, bm_db_err_msg(bm_db_handle)));
        }
+       /* Create the global cpu stats table */
+       BM_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')",
+                       BATTERY_MONITOR_GBL_CPU_STAT_TABLE);
+       rc = bm_get_record_count(bm_db_handle, query);
+       if (rc <= 0) {
+               rc = bm_execute_query(bm_db_handle, GBL_CPU_STAT_SCHEMA);
+               if (rc == SQLITE_BUSY)
+                       return BATTERY_MONITOR_ERROR_DATABASE_BUSY;
+               BM_RETURN_VAL((SQLITE_OK == rc), {}, BATTERY_MONITOR_ERROR_DB_FAILED,
+                               ("bm_execute_query(bm_db_handle, %s) failed(%d, %s).\n",
+                                GBL_CPU_STAT_SCHEMA, rc, bm_db_err_msg(bm_db_handle)));
+       }
 
        EXIT;
        return error_code;
@@ -1237,6 +1274,23 @@ static int bm_battery_stat_insert_to_db(sqlite3 *bm_db_handle, void *str_data, i
                BM_RETURN_VAL((hstmt != NULL), {}, BATTERY_MONITOR_ERROR_DB_FAILED, ("bm_prepare_query() failed(%s).\n", bm_db_err_msg(bm_db_handle)));
                bm_gl_bst_stat_convert_to_sql(gbst_st, hstmt, query);
                break;
+       case 7:
+               _DBG("GCPU_DATA");
+               bd_gl_cpu_stat_s *gcpu_st = (bd_gl_cpu_stat_s *)str_data;
+               _DBG("time %ld usr %ld sys %ld", gcpu_st->time_s, gcpu_st->usr_time, gcpu_st->sys_time);
+               BM_SNPRINTF(query, sizeof(query), "INSERT INTO %s(time_s, usr_time, sys_time) values "\
+                               "(?, ?, ?)", BATTERY_MONITOR_GBL_CPU_STAT_TABLE);
+               hstmt = bm_prepare_query(bm_db_handle, query);
+               if (bm_db_err_code(bm_db_handle) == SQLITE_PERM) {
+                       _ERR("Access failed(%s)", bm_db_err_msg(bm_db_handle));
+                       return BATTERY_MONITOR_ERROR_PERMISSION_DENIED;
+               } else if (bm_db_err_code(bm_db_handle) == SQLITE_BUSY) {
+                       _ERR("Database Busy(%s)", bm_db_err_msg(bm_db_handle));
+                       return BATTERY_MONITOR_ERROR_DATABASE_BUSY;
+               }
+               BM_RETURN_VAL((hstmt != NULL), {}, BATTERY_MONITOR_ERROR_DB_FAILED, ("bm_prepare_query() failed(%s).\n", bm_db_err_msg(bm_db_handle)));
+               bm_gl_cpu_stat_convert_to_sql(gcpu_st, hstmt, query);
+               break;
        default:
                break;
        }
@@ -1690,6 +1744,20 @@ static int bm_battery_stat_query_from_db(sqlite3 *bm_db_handle, void* str_data,
                BM_CATCH_ERROR(rc == SQLITE_ROW, {}, BATTERY_MONITOR_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
                bm_convert_column_to_gl_bst_stat(hstmt, str_data);
                break;
+       case 7:
+               _DBG("GCPU_DATA");
+               BM_SNPRINTF(query, sizeof(query), "SELECT sum(usr_time), sum(sys_time) \
+               FROM %s WHERE time_s >= %ld", BATTERY_MONITOR_GBL_CPU_STAT_TABLE, duration);
+               hstmt = bm_prepare_query(bm_db_handle, query);
+               if (bm_db_err_code(bm_db_handle) == SQLITE_PERM) {
+                       _ERR("Access failed(%s)", bm_db_err_msg(bm_db_handle));
+                       error_code = BATTERY_MONITOR_ERROR_PERMISSION_DENIED;
+                       return error_code;
+               }
+               rc = bm_query_step(hstmt);
+               BM_CATCH_ERROR(rc == SQLITE_ROW, {}, BATTERY_MONITOR_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
+               bm_convert_column_to_gl_cpu_stat(hstmt, str_data);
+               break;
        default:
                break;
        }