Record System base usage 55/231255/1
authorDewal Agarwal <d1.agarwal@samsung.com>
Mon, 20 Apr 2020 13:22:25 +0000 (18:52 +0530)
committerDewal Agarwal <d1.agarwal@samsung.com>
Mon, 20 Apr 2020 13:22:25 +0000 (18:52 +0530)
Change-Id: Ib62082b047e4a2d39fa2b3e2bfad6e6019ce1e10
Signed-off-by: Dewal Agarwal <d1.agarwal@samsung.com>
include/bm_server_db.h
packaging/batterymonitor.spec
src/bm_power_engine.c
src/bm_server_db.c

index 887105b..a8b0ce0 100644 (file)
@@ -38,11 +38,9 @@ BM_RESOURCE_ID_DEVICE_NETWORK,
 BM_RESOURCE_ID_GPS_SENSOR,
 BM_RESOURCE_ID_ID_HRM_SENSOR,
 BM_RESOURCE_ID_BATTERY,
-BM_RESOURCE_ID_MAX,
-*/
+BM_RESOURCE_ID_MAX,*/
 
 /* Battery Monitor AppId Vs Integer Map */
-/* TO DO: Cleaning of redundant records from DB */
 #define APP_ID_MAP_SCHEMA "create table appid_map \n"\
        "(\n"\
 "id INTEGER PRIMARY KEY AUTOINCREMENT, "\
@@ -65,7 +63,6 @@ BM_RESOURCE_ID_MAX,
 ");"
 
 /* Battery Monitor RESOURCE ID Vs TIME */
-/* TO DO: Adding Enum for Resources */
 #define RESOURCE_ID_USAGE_SCHEMA "create table resourceid_usage \n"\
        "(\n"\
 "ResourceId TEXT, "\
@@ -73,6 +70,13 @@ BM_RESOURCE_ID_MAX,
 "usage INT "\
 ");"
 
+#define RES_ID_BASE_USAGE_SCHEMA "create table res_base_usage \n"\
+       "(\n"\
+"ResourceId TEXT, "\
+"log_time INT, "\
+"usage INT "\
+");"
+
 #define GBL_BLE_STAT_SCHEMA "create table gble_stat \n"\
        "(\n"\
 "time_s LONG INT, "\
@@ -82,7 +86,6 @@ BM_RESOURCE_ID_MAX,
 "ble_pwi_val INT "\
 ");"
 
-
 #define GBL_WFL_STAT_SCHEMA "create table gwfl_stat \n"\
        "(\n"\
 "time_s LONG INT, "\
@@ -168,6 +171,7 @@ BM_RESOURCE_ID_MAX,
 #define BATTERY_MONITOR_APP_ID_MAP_TABLE               "appid_map"
 #define BATTERY_MONITOR_APP_ID_USAGE_TABLE             "appid_usage"
 #define BATTERY_MONITOR_RESOURCE_ID_USAGE_TABLE                "resourceid_usage"
+#define BATTERY_MONITOR_RES_ID_BASE_USAGE_TABLE                "res_base_usage"
 #define BATTERY_MONITOR_GBL_BLE_STAT_TABLE             "gble_stat"
 #define BATTERY_MONITOR_GBL_WFL_STAT_TABLE             "gwfl_stat"
 #define BATTERY_MONITOR_GBL_NT_STAT_TABLE              "gn_stat"
@@ -178,7 +182,7 @@ BM_RESOURCE_ID_MAX,
 #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                    11
+#define BATTERY_MONITOR_TABLE_COUNT                    12
 
 typedef sqlite3_stmt *bm_stmt;
 
@@ -199,6 +203,8 @@ resourceid_usage_s *bm_server_query_resource_usage_resourceid_ci(const char *res
 
 int bm_server_resource_usage_insert_to_db(resourceid_usage_s *bm_resource_type);
 
+int bm_server_resource_base_usage_insert_to_db(resourceid_usage_s *bm_resource_type);
+
 int bm_server_battery_dump_insert_to_db(void* str_data, int type);
 
 int bm_server_battery_dump_query_from_db(void *str_data, int type, long int duration);
index 924ffcd..23c9a07 100644 (file)
@@ -92,6 +92,7 @@ then
        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);
        CREATE TABLE if not exists resourceid_usage (ResourceId TEXT, log_time INT, usage INT);
+       CREATE TABLE if not exists res_base_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,
        wifi_rx_time INT, wifi_tx_time INT, wifi_pwi_val INT);
index 253d9b7..3dfe054 100644 (file)
@@ -495,6 +495,7 @@ int bm_ble_calc_power_and_commit(bm_bluetooth_st *handle, bool mode)
        char *appid = NULL;
        long int apprx = 0, apptx = 0;
        long int ret_time = bm_get_log_time(data_collection_period);
+       long int total_app_pw = 0, base_res_pw = 0;
 
        /* Coefficient to divide the rx and tx time wrt rx and tx bytes */
        double crtx_t = 0;
@@ -516,7 +517,6 @@ int bm_ble_calc_power_and_commit(bm_bluetooth_st *handle, bool mode)
                appid = temp->app_id;
                apprx = temp->rx; apptx = temp->tx;
                apptime = temp->time;
-               /* Coefficient of unit byte total transmitted bytes */
 //             P_power_app_data = (sP_power_bt_tx * (crx_t * apptx)) + (sP_power_bt_rx * (ctx_t * apprx));
 //             if (onTime != 0)
 //                     P_power_app_conn = (P_power_conn * apptime)/onTime; //check for 0 denominator & same units
@@ -526,6 +526,7 @@ int bm_ble_calc_power_and_commit(bm_bluetooth_st *handle, bool mode)
                app_usage->AppId = appid;
                app_usage->rId_ble = P_power_app_bt;
                app_usage->log_time = ret_time;
+               total_app_pw += P_power_app_bt;
                /* Call Insert Function */
                _DBG("Calculated Power for Appid(%s) -  P_power_app_bt(%ld), P_power_app_conn(%ld), P_power_app_data(%ld)",
                                        appid, P_power_app_bt, P_power_app_conn, P_power_app_data);
@@ -538,8 +539,6 @@ int bm_ble_calc_power_and_commit(bm_bluetooth_st *handle, bool mode)
                        ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_ble, BM_PLUGIN_ID_BLE);
                if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                        _ERR("Insert of BT App failed ");
-               else
-                       _DBG("Insert successful");
        }
 
        /* commit to database the output after power calculation */
@@ -553,11 +552,17 @@ int bm_ble_calc_power_and_commit(bm_bluetooth_st *handle, bool mode)
        res_usage->ResourceId = BM_RID_BLE;
        res_usage->log_time = ret_time;
        res_usage->usage = P_power_bt;
-
        ret_val = bm_server_resource_usage_insert_to_db(res_usage);
        if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                _ERR("Insert of BT resource failed ");
 
+       if (P_power_bt > total_app_pw) {
+               base_res_pw = (P_power_bt - total_app_pw);
+               res_usage->usage = base_res_pw;
+               ret_val = bm_server_resource_base_usage_insert_to_db(res_usage);
+               if (ret_val != BATTERY_MONITOR_ERROR_NONE)
+                       _ERR("Insert of BT base resource failed ");
+       }
        bm_appid_usage_free(app_usage);
        bm_resourceid_usage_free(res_usage);
 
@@ -711,6 +716,7 @@ int bm_wifi_calc_power_and_commit(bm_wifi_st *handle, bool mode)
        double apptime = 0, crx_t = 0, ctx_t = 0;
        long int rx_t = 0, tx_t = 0;
        long int ret_time = bm_get_log_time(data_collection_period);
+       long int total_app_pw = 0, base_res_pw = 0;
 
        /* Coefficient to divide the rx and tx time wrt rx and tx bytes */
        if (RX != 0)
@@ -742,6 +748,7 @@ int bm_wifi_calc_power_and_commit(bm_wifi_st *handle, bool mode)
                app_usage->AppId = appid;
                app_usage->rId_wifi = P_power_app_wifi;
                app_usage->log_time = ret_time;
+               total_app_pw += P_power_app_wifi;
                /* Call Insert Function */
                _DBG("Calculated Power for Appid(%s) -  P_power_app_wifi(%ld), P_power_app_conn(%ld), P_power_app_data(%ld)",
                                        appid, P_power_app_wifi, P_power_app_conn, P_power_app_data);
@@ -755,8 +762,6 @@ int bm_wifi_calc_power_and_commit(bm_wifi_st *handle, bool mode)
                        ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_wifi, BM_PLUGIN_ID_WIFI);
                if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                        _ERR("Insert of Wifi App failed ");
-               else
-                       _DBG("Insert successful");
        }
 
        /* commit to database the output after power calculation */
@@ -770,13 +775,17 @@ int bm_wifi_calc_power_and_commit(bm_wifi_st *handle, bool mode)
        res_usage->ResourceId = BM_RID_WIFI;
        res_usage->log_time = ret_time;
        res_usage->usage = P_power_wifi;
-
        ret_val = bm_server_resource_usage_insert_to_db(res_usage);
        if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                _ERR("Insert of Wifi resource failed ");
-       else
-               _DBG("Insert successful");
 
+       if (P_power_wifi > total_app_pw) {
+               base_res_pw = (P_power_wifi - total_app_pw);
+               res_usage->usage = base_res_pw;
+               ret_val = bm_server_resource_base_usage_insert_to_db(res_usage);
+               if (ret_val != BATTERY_MONITOR_ERROR_NONE)
+                       _ERR("Insert of Wifi base resource failed ");
+       }
        bm_appid_usage_free(app_usage);
        bm_resourceid_usage_free(res_usage);
 
@@ -892,7 +901,6 @@ int bm_cpu_calc_power_and_commit(bm_cpu_st *handle, bool mode)
        _DBG("received cpu power params - user[%lf]\n, system[%lf]\n", sP_power_cpu_user, sP_power_cpu_system);
        /* CPU power consumption Level - 1 at the Resource Level */
        long int P_power_cpu = 0, P_power_user = 0, P_power_system = 0;
-       _DBG("%ld %ld %lf", uTime, sTime, onTime);
        uTime /= 1000; sTime /= 1000; onTime /= 1000;
        P_power_user = ((sP_power_cpu_user * uTime));
        P_power_system = ((sP_power_cpu_system * sTime));
@@ -908,6 +916,7 @@ int bm_cpu_calc_power_and_commit(bm_cpu_st *handle, bool mode)
        double apptime = 0;
        char *appid = NULL;
        long int ret_time = bm_get_log_time(data_collection_period);
+       long int total_app_pw = 0, base_res_pw = 0;
 
        appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s));
        if (app_usage == NULL) {
@@ -929,6 +938,7 @@ int bm_cpu_calc_power_and_commit(bm_cpu_st *handle, bool mode)
                app_usage->AppId = appid;
                app_usage->rId_cpu = P_power_app_cpu;
                app_usage->log_time = ret_time;
+               total_app_pw += P_power_app_cpu;
                /* Call Insert Function */
                _DBG("Calculated Power for Appid (%s) P_power_app_cpu (%ld),  P_power_app_use (%ld)", appid, P_power_app_cpu, P_power_app_use);
 
@@ -942,8 +952,6 @@ int bm_cpu_calc_power_and_commit(bm_cpu_st *handle, bool mode)
                /* Error Check */
                if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                        _ERR("Insert of CPU App failed ");
-               else
-                       _DBG("Insert successful");
        }
 
        resourceid_usage_s *res_usage = (resourceid_usage_s *)calloc(1, sizeof(resourceid_usage_s));
@@ -956,11 +964,17 @@ int bm_cpu_calc_power_and_commit(bm_cpu_st *handle, bool mode)
        res_usage->ResourceId = BM_RID_CPU;
        res_usage->log_time = ret_time;
        res_usage->usage = P_power_cpu;
-
        ret_val = bm_server_resource_usage_insert_to_db(res_usage);
        if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                _ERR("Insert of CPU resource failed ");
 
+       if (P_power_cpu > total_app_pw) {
+               base_res_pw = (P_power_cpu - total_app_pw);
+               res_usage->usage = base_res_pw;
+               ret_val = bm_server_resource_base_usage_insert_to_db(res_usage);
+               if (ret_val != BATTERY_MONITOR_ERROR_NONE)
+                       _ERR("Insert of CPU base resource failed ");
+       }
        bm_appid_usage_free(app_usage);
        bm_resourceid_usage_free(res_usage);
 
@@ -974,8 +988,6 @@ int bm_cpu_calc_power_and_commit(bm_cpu_st *handle, bool mode)
        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);
@@ -1046,7 +1058,6 @@ int bm_display_calc_power_and_commit(bm_display_st *handle, bool mode)
        /* Display power consumption Level - 1 at the Resource Level */
        lowTime /= 1000; medTime /= 1000; highTime /= 1000; onTime /= 1000;
        long int P_power_disp = 0, P_power_on = 0;
-       _DBG("Total Time in s low %ld med %ld high %ld session %ld onTime %lf", lowTime, medTime, highTime, sesTime, onTime);
        P_power_on = ((sP_power_dp_low * lowTime) + (sP_power_dp_med * medTime) + (sP_power_dp_high * highTime));
        P_power_disp = P_power_on;
        _DBG("Calculated Power for Display P_power_disp (%ld),  P_power_on (%ld)", P_power_disp, P_power_on);
@@ -1059,6 +1070,7 @@ int bm_display_calc_power_and_commit(bm_display_st *handle, bool mode)
        char *appid = NULL;
        double apptime = 0;
        long int ret_time = bm_get_log_time(data_collection_period);
+       long int total_app_pw = 0, base_res_pw = 0;
 
        appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s));
        if (app_usage == NULL) {
@@ -1080,6 +1092,7 @@ int bm_display_calc_power_and_commit(bm_display_st *handle, bool mode)
                app_usage->AppId = appid;
                app_usage->rId_display = P_power_app_disp;
                app_usage->log_time = ret_time;
+               total_app_pw += P_power_app_disp;
                /* Call Insert Function */
                _DBG("Calculated Power for Appid (%s) P_power_app_disp(%ld),  P_power_app_on (%ld)", appid, P_power_app_disp, P_power_app_on);
                if (mode) {
@@ -1092,8 +1105,6 @@ int bm_display_calc_power_and_commit(bm_display_st *handle, bool mode)
                /* Error Check */
                if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                        _ERR("Insert of Display App failed ");
-               else
-                       _DBG("Insert successful");
        }
 
        /* commit to database the output after power calculation */
@@ -1107,13 +1118,17 @@ int bm_display_calc_power_and_commit(bm_display_st *handle, bool mode)
        res_usage->ResourceId = BM_RID_DISP;
        res_usage->log_time = ret_time;
        res_usage->usage = P_power_disp;
-
        ret_val = bm_server_resource_usage_insert_to_db(res_usage);
        if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                _ERR("Insert of Display resource failed ");
-       else
-               _DBG("Insert successful");
 
+       if (P_power_disp > total_app_pw) {
+               base_res_pw = (P_power_disp - total_app_pw);
+               res_usage->usage = base_res_pw;
+               ret_val = bm_server_resource_base_usage_insert_to_db(res_usage);
+               if (ret_val != BATTERY_MONITOR_ERROR_NONE)
+                       _ERR("Insert of Display base resource failed ");
+       }
        bm_appid_usage_free(app_usage);
        bm_resourceid_usage_free(res_usage);
 
@@ -1229,6 +1244,7 @@ int bm_device_network_calc_power_and_commit(bm_device_network_st *handle, bool m
        long int apprx = 0, apptx = 0;
        double apptime = 0;
        long int ret_time = bm_get_log_time(data_collection_period);
+       long int total_app_pw = 0, base_res_pw = 0;
 
        appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s));
        if (app_usage == NULL) {
@@ -1251,6 +1267,7 @@ int bm_device_network_calc_power_and_commit(bm_device_network_st *handle, bool m
                app_usage->AppId = appid;
                app_usage->rId_device_network = P_power_app_dntw;
                app_usage->log_time = ret_time;
+               total_app_pw += P_power_app_dntw;
                /* Call Insert Function */
                _DBG("Calculated Power for Appid (%s) P_power_app_dntw(%ld),  P_power_app_conn (%ld), P_power_app_data(%ld)", appid, P_power_app_dntw, P_power_app_conn, P_power_app_data);
                if (mode) {
@@ -1262,8 +1279,6 @@ int bm_device_network_calc_power_and_commit(bm_device_network_st *handle, bool m
                        ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_device_network, BM_PLUGIN_ID_DEVICE_NETWORK);
                if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                        _ERR("Insert of Data Ntwk App failed ");
-               else
-                       _DBG("Insert successful");
        }
 
        /* commit to database the output after power calculation */
@@ -1277,13 +1292,17 @@ int bm_device_network_calc_power_and_commit(bm_device_network_st *handle, bool m
        res_usage->ResourceId = BM_RID_DNTW;
        res_usage->log_time = ret_time;
        res_usage->usage = P_power_dntw;
-
        ret_val = bm_server_resource_usage_insert_to_db(res_usage);
        if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                _ERR("Insert of Data Network resource failed ");
-       else
-               _DBG("Insert successful");
 
+       if (P_power_dntw > total_app_pw) {
+               base_res_pw = (P_power_dntw - total_app_pw);
+               res_usage->usage = base_res_pw;
+               ret_val = bm_server_resource_base_usage_insert_to_db(res_usage);
+               if (ret_val != BATTERY_MONITOR_ERROR_NONE)
+                       _ERR("Insert of Data Network base resource failed ");
+       }
        bm_appid_usage_free(app_usage);
        bm_resourceid_usage_free(res_usage);
 
@@ -1299,8 +1318,6 @@ int bm_device_network_calc_power_and_commit(bm_device_network_st *handle, bool m
        ret_val = bm_server_battery_dump_insert_to_db(gst_st, 4);
        if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                _ERR("Insert of GWFL failed ");
-       else
-               _DBG("Insert successful");
        BM_FREE(gst_st);
 
        /* Free the Hash Map */
@@ -1384,6 +1401,7 @@ int bm_gps_calc_power_and_commit(bm_gps_st *handle)
        char *appid = NULL;
        int apptime = 0;
        long int ret_time = bm_get_log_time(data_collection_period);
+       long int total_app_pw = 0, base_res_pw = 0;
 
        appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s));
        if (app_usage == NULL) {
@@ -1395,8 +1413,7 @@ int bm_gps_calc_power_and_commit(bm_gps_st *handle)
                P_power_app_gps = 0; P_power_app_conn = 0;
                apptime = 0;
                app_time_map_st1 *temp = (app_time_map_st1 *)value;
-               appid = temp->app_id;
-               apptime = temp->time;
+               appid = temp->app_id; apptime = temp->time;
 
                if (onTime != 0)
                        P_power_app_conn = (P_power_conn * apptime)/onTime; //check for 0 denominator & same units
@@ -1405,13 +1422,12 @@ int bm_gps_calc_power_and_commit(bm_gps_st *handle)
                app_usage->AppId = appid;
                app_usage->rId_gps = P_power_app_gps;
                app_usage->log_time = ret_time;
+               total_app_pw += P_power_app_gps;
                /* Call Insert Function */
                _DBG("Calculated Power for Appid (%s) P_power_app_gps (%d),  P_power_app_conn (%d)", appid, P_power_app_gps, P_power_app_conn);
                ret_val = bm_server_app_usage_insert_to_db(app_usage);
                if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                        _ERR("Insert of GPS App failed ");
-               else
-                       _DBG("Insert successful");
        }
 
        /* commit to database the output after power calculation */
@@ -1425,13 +1441,17 @@ int bm_gps_calc_power_and_commit(bm_gps_st *handle)
        res_usage->ResourceId = BM_RID_GPS;
        res_usage->log_time = ret_time;
        res_usage->usage = P_power_gps;
-
        ret_val = bm_server_resource_usage_insert_to_db(res_usage);
        if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                _ERR("Insert of GPS resource failed ");
-       else
-               _DBG("Insert successful");
 
+       if (P_power_gps > total_app_pw) {
+               base_res_pw = (P_power_gps - total_app_pw);
+               res_usage->usage = base_res_pw;
+               ret_val = bm_server_resource_base_usage_insert_to_db(res_usage);
+               if (ret_val != BATTERY_MONITOR_ERROR_NONE)
+                       _ERR("Insert of GPS base resource failed ");
+       }
        bm_appid_usage_free(app_usage);
        bm_resourceid_usage_free(res_usage);
 
index da75f4f..35c0b20 100644 (file)
@@ -759,18 +759,18 @@ 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', '%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_CPU_STAT_TABLE);
+                       BATTERY_MONITOR_RESOURCE_ID_USAGE_TABLE, BATTERY_MONITOR_RES_ID_BASE_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_CPU_STAT_TABLE);
 
        rc = bm_get_record_count(bm_db_handle, query);
 
        if (rc != BATTERY_MONITOR_TABLE_COUNT)
-               _ERR("Table count is not matched rc=%d\n", rc);
+               _ERR("Table count is not matched rc = %d\n", rc);
 
        EXIT;
        return rc;
@@ -821,6 +821,18 @@ static int bm_create_all_tables(sqlite3 *bm_db_handle)
                        ("bm_execute_query(bm_db_handle, %s) failed(%d, %s).\n",
                         RESOURCE_ID_USAGE_SCHEMA, rc, bm_db_err_msg(bm_db_handle)));
        }
+       /* Create the resource base usage table */
+       BM_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')",
+                                               BATTERY_MONITOR_RES_ID_BASE_USAGE_TABLE);
+       rc = bm_get_record_count(bm_db_handle, query);
+       if (rc <= 0) {
+               rc = bm_execute_query(bm_db_handle, RES_ID_BASE_USAGE_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",
+                       RES_ID_BASE_USAGE_SCHEMA, rc, bm_db_err_msg(bm_db_handle)));
+       }
 
        /* Battery Dump Tables */
        /* Create the global bluetooth stats table */
@@ -1030,55 +1042,6 @@ static int bm_db_close(void)
        return ret;
 }
 
-/*
-int bm_get_appint_by_app_name(sqlite3 *bm_db_handle, const char *AppId)
-{
-       ENTER;
-
-       char query[BATTERY_MONITOR_SQL_LEN_MAX] = {0, };
-       int rc = -1;
-       int ncount = 0;
-       bm_stmt pStmt = NULL;
-
-       BM_RETURN_VAL((bm_db_handle != NULL), {}, BATTERY_MONITOR_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
-       BM_RETURN_VAL((AppId != NULL), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, ("AppId is NULL."));
-
-       BM_MEMSET(query, 0x00, sizeof(query));
-       BM_SNPRINTF(query, sizeof(query), "SELECT AppId FROM %s WHERE AppName='%s'", BATTERY_MONITOR_APP_ID_MAP_TABLE, AppId);
-
-       rc = sqlite3_prepare_v2(bm_db_handle, query, strlen(query), &pStmt, NULL);
-
-       if (SQLITE_BUSY == rc) {
-               _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, bm_db_err_msg(bm_db_handle));
-               sqlite3_finalize(pStmt);
-               return BATTERY_MONITOR_ERROR_DATABASE_BUSY;
-       } else if (SQLITE_OK != rc) {
-               _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, bm_db_err_msg(bm_db_handle));
-               sqlite3_finalize(pStmt);
-               return BATTERY_MONITOR_ERROR_DB_FAILED;
-       }
-
-       rc = sqlite3_step(pStmt);
-       if (SQLITE_BUSY == rc) {
-               _ERR("sqlite3_step() failed(%d, %s).", rc, bm_db_err_msg(bm_db_handle));
-               sqlite3_finalize(pStmt);
-               return BATTERY_MONITOR_ERROR_DATABASE_BUSY;
-       } else if (SQLITE_ROW != rc) {
-               _ERR("sqlite3_step() failed(%d, %s).", rc, bm_db_err_msg(bm_db_handle));
-               sqlite3_finalize(pStmt);
-               return BATTERY_MONITOR_ERROR_DB_FAILED;
-       }
-
-       ncount = sqlite3_column_int(pStmt, 1);
-
-       _INFO("bm appint for appname [%d]", ncount);
-       sqlite3_finalize(pStmt);
-
-       EXIT;
-       return ncount;
-}
-*/
-
 static gboolean app_name_check_duplicated(sqlite3 *bm_db_handle, const char *app_id, int *error_code)
 {
        ENTER;
@@ -1385,6 +1348,54 @@ static int bm_resourceid_usage_insert_to_db(sqlite3 *bm_db_handle, resourceid_us
        return error_code;
 }
 
+static int bm_resourceid_base_usage_insert_to_db(sqlite3 *bm_db_handle, resourceid_usage_s *bm_resource_type)
+{
+       ENTER;
+
+       int rc = 0;
+       char query[BATTERY_MONITOR_SQL_LEN_MAX] = {0, };
+       int error_code = BATTERY_MONITOR_ERROR_NONE;
+       bm_stmt hstmt = NULL;
+
+       BM_RETURN_VAL((bm_db_handle != NULL), {}, BATTERY_MONITOR_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
+       BM_RETURN_VAL((bm_resource_type != NULL), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, ("The battery monitor handle is NULL"));
+
+       /* check mandatory field(Resource id)*/
+       if (!bm_resource_type->ResourceId)
+               return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
+
+       BM_MEMSET(query, 0x00, sizeof(query));
+       BM_SNPRINTF(query, sizeof(query), "INSERT INTO %s( ResourceId, log_time, usage ) values " "(?, ?, ?)", BATTERY_MONITOR_RES_ID_BASE_USAGE_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_resourceid_usage_convert_to_sql(bm_resource_type, hstmt, query);
+       rc = bm_query_step(hstmt);
+       if (rc == SQLITE_BUSY) {
+               _ERR("bm_db_query_step() failed(%d, %s)", rc, bm_db_err_msg(bm_db_handle));
+               error_code = BATTERY_MONITOR_ERROR_DATABASE_BUSY;
+       } else if (rc != SQLITE_DONE) {
+               _ERR("bm_db_query_step() failed(%d, %s)", rc, bm_db_err_msg(bm_db_handle));
+               error_code = BATTERY_MONITOR_ERROR_DB_FAILED;
+       }
+
+       rc = bm_query_finalize(hstmt);
+       BM_RETURN_VAL((rc == BATTERY_MONITOR_ERROR_NONE), {}, rc, ("finalize error"));
+       hstmt = NULL;
+
+       EXIT;
+       return error_code;
+}
+
 int bm_appid_map_insert_to_db(sqlite3 *bm_db_handle, const char *app_id)
 {
        ENTER;
@@ -2213,6 +2224,49 @@ int bm_server_resource_usage_insert_to_db(resourceid_usage_s *bm_resource_type)
        return BATTERY_MONITOR_ERROR_NONE;
 }
 
+int bm_server_resource_base_usage_insert_to_db(resourceid_usage_s *bm_resource_type)
+{
+       ENTER;
+
+       int error_code = BATTERY_MONITOR_ERROR_NONE;
+       int ret_transaction = 0;
+
+       BM_RETURN_VAL((g_hBatteryMonitorDB != NULL), {}, BATTERY_MONITOR_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
+       BM_RETURN_VAL((bm_resource_type != NULL), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, ("The Battery Monitor Handle is NULL"));
+
+       pthread_mutex_lock(&battery_monitor_mutex);
+
+       /* Transaction Control Required */
+       ret_transaction = bm_begin_transaction(g_hBatteryMonitorDB);
+       if (bm_db_err_code(g_hBatteryMonitorDB) == SQLITE_PERM) {
+               pthread_mutex_unlock(&battery_monitor_mutex);
+               _ERR("Access failed(%s)", bm_db_err_msg(g_hBatteryMonitorDB));
+               return BATTERY_MONITOR_ERROR_PERMISSION_DENIED;
+       }
+       if (ret_transaction == BATTERY_MONITOR_ERROR_DATABASE_BUSY) {
+               _ERR("Resource Base Usage insert:bm_begin_transaction fail %d\n", ret_transaction);
+               pthread_mutex_unlock(&battery_monitor_mutex);
+               return BATTERY_MONITOR_ERROR_DATABASE_BUSY;
+       } else if (ret_transaction != BATTERY_MONITOR_ERROR_NONE) {
+               _ERR("Resource Base Usage insert:bm_begin_transaction fail %d\n", ret_transaction);
+               pthread_mutex_unlock(&battery_monitor_mutex);
+               return ret_transaction;
+       }
+
+       error_code = bm_resourceid_base_usage_insert_to_db(g_hBatteryMonitorDB, bm_resource_type);
+       if (error_code != BATTERY_MONITOR_ERROR_NONE) {
+               ret_transaction = bm_end_transaction(g_hBatteryMonitorDB, false);
+               _ERR("INSERT resource base usage fail, rollback insert query(%d)!!!!\n", ret_transaction);
+               pthread_mutex_unlock(&battery_monitor_mutex);
+               return error_code;
+       }
+       pthread_mutex_unlock(&battery_monitor_mutex);
+       bm_end_transaction(g_hBatteryMonitorDB, true);
+
+       EXIT;
+       return BATTERY_MONITOR_ERROR_NONE;
+}
+
 int bm_server_app_usage_insert_to_db(appid_usage_s *bm_app_type)
 {
        ENTER;
@@ -2395,6 +2449,13 @@ int bm_server_delete_table_by_time_interval(void)
                pthread_mutex_unlock(&battery_monitor_mutex);
                return error_code;
        }
+       error_code = bm_delete_table_by_time_interval(g_hBatteryMonitorDB, BATTERY_MONITOR_RES_ID_BASE_USAGE_TABLE);
+       if (error_code != BATTERY_MONITOR_ERROR_NONE) {
+               ret_transaction = bm_end_transaction(g_hBatteryMonitorDB, false);
+               _ERR("Delete old resource base usage fail, rollback delete query(%d)!!!!\n", ret_transaction);
+               pthread_mutex_unlock(&battery_monitor_mutex);
+               return error_code;
+       }
        error_code = bm_delete_table_by_time_interval(g_hBatteryMonitorDB, BATTERY_MONITOR_GBL_BLE_STAT_TABLE);
        if (error_code != BATTERY_MONITOR_ERROR_NONE) {
                ret_transaction = bm_end_transaction(g_hBatteryMonitorDB, false);