From d9d3aee9ce9c873c9085406fb1d5557ea39a2b92 Mon Sep 17 00:00:00 2001 From: Dewal Agarwal Date: Thu, 23 Apr 2020 12:26:52 +0530 Subject: [PATCH] Sync code between branch - Merge remote-tracking branch 'tizen_5.5' into 'tizen' - Squashed commit to reflect the following changes: commit 97b4132247c4e4e3b33e65923004d23b0236c562 Author: Dewal Agarwal Date: Mon Apr 20 12:27:14 2020 +0530 Correct log time wrt config param Change-Id: I8f89374553c2725886bf4442fc0e62a501f9b496 Signed-off-by: Dewal Agarwal commit ea8dabe0a0ca5c36968408c425f82beeb4426265 Author: Kichan Kwon Date: Fri Apr 17 21:05:43 2020 +0900 Deny D-Bus message only having battery monitor's interface - Because different receiver can have same D-Bus ID, other process having same ID with battery monitor can be rejected - To prevent this situation, we have to limit the policy to apply only for battery monitor's D-Bus message Change-Id: If702b07cac89bbd77cc0dd9da80e3ee94bd4fa82 Signed-off-by: Kichan Kwon commit 417f4fb1a16cc61904c2ab8eeb5c8154399eb047 Author: Kichan Kwon Date: Fri Apr 17 20:05:41 2020 +0900 Remove busy wait - In addition, remove ENTER/EXIT logs from data collection functions Change-Id: Idc45299c0dc34dc338b7c6d3cd8f437496c34989 Signed-off-by: Kichan Kwon commit abc887879f36a1f8adb20f1ceb54a00a199d3fb5 Author: Kichan Kwon Date: Fri Apr 17 16:14:49 2020 +0900 Allow modifying data collection parameters via config file Change-Id: Ia88df720167ca5cd50416880f50bc1e085aff1c0 Signed-off-by: Kichan Kwon commit 7e3532d4a15994d17190cbc5c8388ee30ac94aee Author: Kichan Kwon Date: Wed Apr 8 20:24:40 2020 +0900 Try to handle plugin data when CPU is idle - Separate getting plugin data with calculating and DB restoring - Period : 3 minutes -> 10 minutes - Check whether CPU is idle before working - If CPU is busy, try on next time Change-Id: I5c27880ad01c0f1142db4d1b804a4088770f95d5 Signed-off-by: Kichan Kwon commit db2de5b07ce646f656dd3257ebb0657518dc7817 Author: Abhishek Vijay Date: Mon Apr 13 16:56:04 2020 +0530 [Non-ACR] Removing dead code Change-Id: I52e2b979ad6e89830376f54e0b77534dc9a91202 Signed-off-by: Abhishek Vijay commit a0417e1acdd7449f2c68f18bff85fdc566adf2f7 Author: Kichan Kwon Date: Fri Apr 10 16:10:28 2020 +0900 No more convert Wi-Fi time to seconds - start/endTime and scanTime are already seconds Change-Id: I1f68c7c94b155ade90a782a862084532d83f990a Signed-off-by: Kichan Kwon commit fd82cbbf8cd24be436c66d8de8842bd51aae5335 Author: Dewal Agarwal Date: Tue Mar 31 16:53:46 2020 +0530 Modify Wi-Fi Power Model - Based scan time not included in rx and tx time Change-Id: I1eb88067802074b177cd5ab2be1c6b67c4cba367 Signed-off-by: Dewal Agarwal commit 71897b45bb2131c1b9b3faf3c4dd8491721e0e43 Author: Abhishek Vijay Date: Tue Apr 7 11:48:12 2020 +0530 [Non-ACR] Correcting string name Change-Id: I79bd4acdfc7e70cd339b91cd2ed921e1c3bcde96 Signed-off-by: Abhishek Vijay commit 872c8a169f6d832d479b1f8acb9332ed2b47fe94 Author: Abhishek Vijay Date: Tue Apr 7 11:09:36 2020 +0530 [Non-ACR]fixing memory issue Change-Id: I333b77fe9f6cc6ca0f8ffd95802848045b1cd0d9 Signed-off-by: Abhishek Vijay commit 64031ef1c3a990bb40f21e4e515f7570a2399542 Author: Kichan Kwon Date: Fri Apr 3 10:46:05 2020 +0900 Convert BT time to seconds - BT plugin is changed to use milliseconds Change-Id: Id8545baf16b4318df422ce0c5359bdaf289d856b Signed-off-by: Kichan Kwon commit 36bd4331b58e87f03e3a60923eb023eae8941b6a Author: Dewal Agarwal Date: Wed Apr 1 15:11:01 2020 +0530 Code & Memory Fix Change-Id: I128b416daa0098be2802880583b91697e5044185 Signed-off-by: Dewal Agarwal commit b070cf5f97a6f6fc4a87fd1e16bad88b1a88aec7 Author: Dewal Agarwal Date: Tue Mar 31 16:53:46 2020 +0530 Modify Wi-Fi Power Model Change-Id: I1eb88067802074b177cd5ab2be1c6b67c4cba367 Signed-off-by: Dewal Agarwal commit 3820bf5b1f2f9b53bdccc1628e3d08d885081109 Author: Dewal Agarwal Date: Tue Mar 31 13:33:59 2020 +0530 Return record not found for AppId Change-Id: Iad43957b538efa33ab434578eacb6e550e0b8700 Signed-off-by: Dewal Agarwal commit 05fff2a08f2a1b841dcb85e952a128547d77ff1e Author: Dewal Agarwal Date: Mon Mar 30 10:39:41 2020 +0530 Fix Memory Leak Change-Id: Iba2001aa96ed199131cb1b29110cac65d8be42a4 Signed-off-by: Dewal Agarwal commit fadd0cff0e9b1f77963f9dc89dfe5bc2153d02df Author: Kichan Kwon Date: Thu Mar 26 10:33:46 2020 +0900 Change power profile parameters to double - Because real parameters are floating point Change-Id: Id49cfa3b5601bad8d26287ac842b85c0d89c56e4 Signed-off-by: Kichan Kwon commit f23e878c8f287ccc7deb738e0ecd7e9dad4c6d84 Author: Abhishek Vijay Date: Mon Mar 30 10:57:00 2020 +0530 [Non-ACR] Fixing memory issues Change-Id: Id05f44d81b41e68cb3043235106b990e08bc005f Signed-off-by: Abhishek Vijay commit 6d28407c370f5a947d912b5437960c64da051988 Author: Dewal Agarwal Date: Mon Mar 23 16:24:15 2020 +0530 Modify BT Power Model Change-Id: I26180b4f100fc5e979f60c104a148503ed1e9587 commit 2132a830a290e1ff53ee2a8fb2efcf898d254c11 Author: Dewal Agarwal Date: Tue Mar 24 17:14:59 2020 +0530 Add Battery Screen On/Off stats - Add "misc" field Change-Id: I1ada9dc8c350a90b2e46d7ecdcab306120257c4c Change-Id: I62465727a09362d2f641c746f2f89930af322350 --- CMakeLists.txt | 1 + config/bm_params.json | 14 ++ config/bm_power_profile_params.json | 60 ++--- include/bm_common.h | 9 +- include/bm_config_parser.h | 24 +- include/bm_dbg.h | 6 +- include/bm_plugin_interface.h | 2 + include/bm_util.h | 2 +- packaging/batterymonitor.spec | 3 + packaging/org.tizen.battery.monitor.conf | 3 +- src/battery_dump/bd_history_item.c | 137 +++++++---- src/bm_config_parser.c | 383 ++++++++++++++++++++----------- src/bm_plugin_manager.c | 7 +- src/bm_power_engine.c | 352 +++++++++++++++++++++++----- src/bm_server.c | 2 +- src/bm_util.c | 6 +- 16 files changed, 717 insertions(+), 294 deletions(-) create mode 100644 config/bm_params.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 10299eb..3ed25fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ pkg_check_modules(pkgs REQUIRED sqlite3 db-util capi-system-info + capi-system-runtime-info cynara-client cynara-session cynara-creds-gdbus diff --git a/config/bm_params.json b/config/bm_params.json new file mode 100644 index 0000000..546c550 --- /dev/null +++ b/config/bm_params.json @@ -0,0 +1,14 @@ +{ + "job_scheduler": { + "data_collection_period": 600000, + "data_collection_try_period": 1000, + "data_collection_accept_count": 5, + "delete_db_period": 86400000 + }, + "cpu_usage_checker": { + "timeout": 60000, + "initial_threshold": 0.60, + "minimum_threshold": 0.50, + "threshold_reduction": 0.00 + } +} diff --git a/config/bm_power_profile_params.json b/config/bm_power_profile_params.json index 932eae9..2d3a4f5 100644 --- a/config/bm_power_profile_params.json +++ b/config/bm_power_profile_params.json @@ -1,47 +1,47 @@ { "wifi": { - "p_wifi_rssi_0": 10, - "p_wifi_rssi_1": 20, - "p_wifi_rssi_2": 30, - "p_wifi_rssi_3": 40, - "p_wifi_rssi_4": 50, - "p_wifi_scan": 60, - "p_wifi_idle": 70, - "p_wifi_rx": 80, - "p_wifi_tx": 90 + "p_wifi_rssi_0": 10.12345, + "p_wifi_rssi_1": 20.12345, + "p_wifi_rssi_2": 30.12345, + "p_wifi_rssi_3": 40.12345, + "p_wifi_rssi_4": 50.12345, + "p_wifi_scan": 60.12345, + "p_wifi_idle": 70.12345, + "p_wifi_rx": 80.12345, + "p_wifi_tx": 90.12345 }, "bluetooth": { - "p_bt_idle": 10, - "p_bt_scan": 12, - "p_bt_connected": 13, - "p_bt_rx": 14, - "p_bt_tx": 15 + "p_bt_idle": 10.12345, + "p_bt_scan": 12.12345, + "p_bt_connected": 13.12345, + "p_bt_rx": 14.12345, + "p_bt_tx": 15.12345 }, "device_network": { - "p_dnw_sig_strength_0": 20, - "p_dnw_sig_strength_1": 21, - "p_dnw_sig_strength_2": 22, - "p_dnw_sig_strength_3": 23, - "p_dnw_sig_strength_4": 24, - "p_dnw_sig_strength_5": 25, - "p_dnw_sig_strength_6": 26, - "p_dnw_rx": 21, - "p_dnw_tx": 22 + "p_dnw_sig_strength_0": 20.12345, + "p_dnw_sig_strength_1": 21.12345, + "p_dnw_sig_strength_2": 22.12345, + "p_dnw_sig_strength_3": 23.12345, + "p_dnw_sig_strength_4": 24.12345, + "p_dnw_sig_strength_5": 25.12345, + "p_dnw_sig_strength_6": 26.12345, + "p_dnw_rx": 21.12345, + "p_dnw_tx": 22.12345 }, "display": { - "p_dp_high": 30, - "p_dp_medium": 31, - "p_dp_low": 32 + "p_dp_high": 30.12345, + "p_dp_medium": 31.12345, + "p_dp_low": 32.12345 }, "gps_sensor": { - "p_gps_inuse": 40 + "p_gps_inuse": 40.12345 }, "hrm_Sensor": { - "p_hrm_inuse": 50 + "p_hrm_inuse": 50.12345 }, "cpu": { - "p_cpu_user": 60, - "p_cpu_system": 61 + "p_cpu_user": 60.12345, + "p_cpu_system": 61.12345 }, "battery": { "p_battery": 500 diff --git a/include/bm_common.h b/include/bm_common.h index 1907181..952eabb 100644 --- a/include/bm_common.h +++ b/include/bm_common.h @@ -73,14 +73,11 @@ typedef enum { #define BM_ENUM_ID_TO_STR(x) case (x): EXIT; return (#x); #define BM_RESOURCE_ID_TO_STR(x, y) case (x): EXIT; return (y); -/* job-scheduler macros */ -#define BATTERY_MONITOR_GET_DATA_JOB_PERIOD 180000 /* (3*60*1000) interval of 3mins */ -#define BATTERY_MONITOR_DELETE_DB_JOB_PERIOD 86400000 /* (24*60*60*1000) interval of 24hrs */ - #define BM_PLUGIN_CONST_BATTERY 7 -/* power-param config file macro */ -#define BATTERY_MONITOR_CONFIG_FILE_PATH "/usr/etc/batterymonitor/config/bm_power_profile_params.json" +/* config file macro */ +#define BATTERY_MONITOR_CONFIG_FILE_PATH "/usr/etc/batterymonitor/config/bm_params.json" +#define BATTERY_MONITOR_POWER_PROFILE_CONFIG_FILE_PATH "/usr/etc/batterymonitor/config/bm_power_profile_params.json" /* privilege macro */ #define BATTERY_MONITOR_PRIVILEGE "http://tizen.org/privilege/systemmonitor" diff --git a/include/bm_config_parser.h b/include/bm_config_parser.h index 99e2281..c0f5f8e 100644 --- a/include/bm_config_parser.h +++ b/include/bm_config_parser.h @@ -28,27 +28,31 @@ extern "C" { #endif -int bm_get_wifi_power_params(int *rssi_l0, int *rssi_l1, int *rssi_l2, int *rssi_l3, int *rssi_l4, - int *scan, int *idle, int *rx, int *tx); +int bm_get_wifi_power_params(double *rssi_l0, double *rssi_l1, double *rssi_l2, double *rssi_l3, double *rssi_l4, + double *scan, double *idle, double *rx, double *tx); -int bm_get_blutooth_power_params(int *idle, int *scan, int *connected, int *rx, int *tx); +int bm_get_blutooth_power_params(double *idle, double *scan, double *connected, double *rx, double *tx); -int bm_get_device_network_power_params(int *signal_strength0, int *signal_strength1, int *signal_strength2, - int *signal_strength3, int *signal_strength4, int *signal_strength5, - int *signal_strength6, int *rx, int *tx); +int bm_get_device_network_power_params(double *signal_strength0, double *signal_strength1, double *signal_strength2, + double *signal_strength3, double *signal_strength4, double *signal_strength5, + double *signal_strength6, double *rx, double *tx); -int bm_get_display_power_params(int *high, int *medium, int *low); +int bm_get_display_power_params(double *high, double *medium, double *low); #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN -int bm_get_gps_sensor_power_params(int *gps_use); +int bm_get_gps_sensor_power_params(double *gps_use); -int bm_get_hrm_sensor_power_params(int *hrm_use); +int bm_get_hrm_sensor_power_params(double *hrm_use); #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */ -int bm_get_cpu_power_params(int *fg, int *bg); +int bm_get_cpu_power_params(double *fg, double *bg); int bm_get_battery_power_params(int *battery); +int bm_get_job_scheduler_params(int *data_collection_period, int *data_collection_try_period, int *data_collection_accept_count, int *delete_db_period); + +int bm_get_cpu_usage_checker_params(int *timeout, double *initial_threshold, double *minimum_threshold, double *threshold_reduction); + #ifdef __cplusplus } #endif diff --git a/include/bm_dbg.h b/include/bm_dbg.h index d7d1f9e..a749b9c 100644 --- a/include/bm_dbg.h +++ b/include/bm_dbg.h @@ -53,9 +53,13 @@ extern "C" #define BM_SLOGE(fmt, arg...) SECURE_LOGE(": " fmt "\n", ##arg); +#if 0 #define ENTER dlog_print(DLOG_DEBUG, LOG_TAG, "%s : %s(%d) >>>>>>>> CALLED", rindex(__FILE__, '/') + 1, __func__, __LINE__) - #define EXIT dlog_print(DLOG_DEBUG, LOG_TAG, "%s : %s(%d) <<<<<<<< ENDED", rindex(__FILE__, '/') + 1, __func__, __LINE__) +#else +#define ENTER +#define EXIT +#endif #ifdef __cplusplus } diff --git a/include/bm_plugin_interface.h b/include/bm_plugin_interface.h index e8e0b32..05ef8e9 100644 --- a/include/bm_plugin_interface.h +++ b/include/bm_plugin_interface.h @@ -139,6 +139,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 */ diff --git a/include/bm_util.h b/include/bm_util.h index a278643..e9b093d 100644 --- a/include/bm_util.h +++ b/include/bm_util.h @@ -61,7 +61,7 @@ void bm_appid_usage_free(appid_usage_s *data); void bm_resourceid_usage_free(resourceid_usage_s *data); -long int bm_get_log_time(); +long int bm_get_log_time(int bias); void bm_set_free_atm_st2_list(GSList *atm_list); diff --git a/packaging/batterymonitor.spec b/packaging/batterymonitor.spec index 437aef7..924ffcd 100644 --- a/packaging/batterymonitor.spec +++ b/packaging/batterymonitor.spec @@ -12,6 +12,7 @@ Source4: batterymonitor.manifest BuildRequires: cmake BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(capi-system-info) +BuildRequires: pkgconfig(capi-system-runtime-info) BuildRequires: pkgconfig(glib-2.0) >= 2.26 BuildRequires: pkgconfig(gio-2.0) BuildRequires: pkgconfig(gio-unix-2.0) @@ -64,6 +65,7 @@ cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \ %install %make_install mkdir -p %{buildroot}/usr/etc/batterymonitor/config +cp config/bm_params.json %{buildroot}%{power_profile_config_path}/bm_params.json cp config/bm_power_profile_params.json %{buildroot}%{power_profile_config_path}/bm_power_profile_params.json mkdir -p %{buildroot}%{_sysconfdir}/dbus-1/system.d @@ -123,6 +125,7 @@ chsmack -a "System" %{TZ_SYS_DB}/.battery-monitor.db-journal %defattr(-,root,root) %attr(755,-,-) %{_bindir}/batterymonitor-svcd %config %{_sysconfdir}/dbus-1/system.d/org.tizen.battery.monitor.conf +%config %attr(755,root,root) %{power_profile_config_path}/bm_params.json %config %attr(755,root,root) %{power_profile_config_path}/bm_power_profile_params.json %attr(644,root,root) %{_datadir}/dbus-1/system-services/* %attr(644,root,root) %{_unitdir}/batterymonitor.service diff --git a/packaging/org.tizen.battery.monitor.conf b/packaging/org.tizen.battery.monitor.conf index 07673f1..d6725a3 100644 --- a/packaging/org.tizen.battery.monitor.conf +++ b/packaging/org.tizen.battery.monitor.conf @@ -11,7 +11,8 @@ - + + diff --git a/src/battery_dump/bd_history_item.c b/src/battery_dump/bd_history_item.c index 3c443b0..bfdbc18 100644 --- a/src/battery_dump/bd_history_item.c +++ b/src/battery_dump/bd_history_item.c @@ -161,13 +161,17 @@ static int bd_get_and_print_battery_usage_from_dump(int fd, long int duration) dump_p = g_string_sized_new(30); BM_CHECK_MEM_ALLOC(dump_p, {}); + g_string_append(dump_p, TIZEN_DUMP_VERSION); // Power Capacity g_string_append_printf(dump_p, ",0,l,pws,%d,0,0,0", battery_capacity); g_string_append(dump_p, "\n"); + _DBG("%s", dump_p->str); + if (write(fd, dump_p->str, dump_p->len) < 0) _WARN("write error"); + g_string_free(dump_p, TRUE); g_hash_table_iter_init(&iter, app_map); @@ -288,16 +292,50 @@ static int bd_get_and_print_gbr_stat_entry_from_dump(int fd, long int duration) return ret; } -static int bd_get_and_print_gn_stat_entry_from_dump(int fd, long int duration) +static int bd_get_and_print_misc_stat_entry_from_dump(int fd, long int duration) { ENTER; + //Dump screen on and off time + //Modify m - field accordingly. + bd_gl_sbr_stat_s *gbr_stat = (bd_gl_sbr_stat_s *)calloc(1, sizeof(bd_gl_sbr_stat_s)); + BM_CHECK_MEM_ALLOC(gbr_stat, {}); int ret = BATTERY_MONITOR_ERROR_NONE; + ret = bm_server_battery_dump_query_from_db(gbr_stat, 3, duration); + if (ret != BATTERY_MONITOR_ERROR_NONE) { + _ERR("Unable to fetch data from DB"); + BM_FREE(gbr_stat); + return ret; + } + + GString *dump_p = g_string_sized_new(30); + BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gbr_stat); }); + g_string_append(dump_p, TIZEN_DUMP_VERSION); + g_string_append(dump_p, ",0,l,"); + g_string_append(dump_p, "m,"); + long int off_time = gbr_stat->dark; + long int on_time = (gbr_stat->dim + gbr_stat->medium + gbr_stat->bright); + g_string_append_printf(dump_p, "%ld,", on_time); + g_string_append_printf(dump_p, "%ld", off_time); + g_string_append(dump_p, "\n"); + _DBG("%s", dump_p->str); + if (write(fd, dump_p->str, dump_p->len) < 0) + _WARN("write error"); + g_string_free(dump_p, TRUE); + BM_FREE(gbr_stat); + + EXIT; + return ret; +} + +static int bd_get_and_print_gn_stat_entry_from_dump(int fd, long int duration) +{ + ENTER; // Dump Network Data bd_gl_ntw_stat_s *gn_stat = (bd_gl_ntw_stat_s *)calloc(1, sizeof(bd_gl_ntw_stat_s)); BM_CHECK_MEM_ALLOC(gn_stat, {}); - + int ret = BATTERY_MONITOR_ERROR_NONE; ret = bm_server_battery_dump_query_from_db(gn_stat, 2, duration); if (ret != BATTERY_MONITOR_ERROR_NONE) { _ERR("Unable to fetch data from DB"); @@ -324,7 +362,6 @@ static int bd_get_and_print_gn_stat_entry_from_dump(int fd, long int duration) g_string_append(dump_p, "\n"); _DBG("%s", dump_p->str);//write - if (write(fd, dump_p->str, dump_p->len) < 0) _WARN("write error"); @@ -516,8 +553,6 @@ static int bd_get_and_print_gwst_stat_entry_from_dump(int fd, long int duration) 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; @@ -526,10 +561,12 @@ static int bd_get_and_print_gcpu_stat_entry_from_dump(int fd, long int duration) 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"); + return ret; } usage = res_use->usage; float usagemah = (float)usage/3600; + 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, {BM_FREE(res_use); }); 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"); @@ -537,8 +574,9 @@ static int bd_get_and_print_gcpu_stat_entry_from_dump(int fd, long int duration) 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);}); + 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,"); @@ -550,7 +588,9 @@ static int bd_get_and_print_gcpu_stat_entry_from_dump(int fd, long int duration) _DBG("%s", dump_p->str); if (write(fd, dump_p->str, dump_p->len) < 0) _WARN("write error"); + g_string_free(dump_p, TRUE); + BM_FREE(gcpu_stat); EXIT; return ret; @@ -1112,48 +1152,53 @@ int bd_print_history_item_main(int fd, int num_h_items, long long base_time, boo if (write(fd, dump_tmp_p->str, dump_tmp_p->len) < 0) _WARN("write error"); - g_free(dump_tmp_p->str); + g_string_free(dump_tmp_p, TRUE); - //Dump i & l values after this - ret = bd_get_and_print_uid_info_entry_from_dump(fd); - if (ret != BATTERY_MONITOR_ERROR_NONE) - _ERR("UID data dump failed"); - // Get Last charge time to dump "l" field - // for the time being dumping all data - long int duration = 0; //Time of last charge - ret = bd_get_and_print_gn_stat_entry_from_dump(fd, duration); - if (ret != BATTERY_MONITOR_ERROR_NONE) - _ERR("GN dump failed"); - ret = bd_get_and_print_gwf_stat_entry_from_dump(fd, duration); - if (ret != BATTERY_MONITOR_ERROR_NONE) - _ERR("GWF data dump failed"); - ret = bd_get_and_print_gble_stat_entry_from_dump(fd, duration); - if (ret != BATTERY_MONITOR_ERROR_NONE) - _ERR("GBL data dump failed"); - ret = bd_get_and_print_gbr_stat_entry_from_dump(fd, duration); - if (ret != BATTERY_MONITOR_ERROR_NONE) - _ERR("GBR data dump failed"); - ret = bd_get_and_print_gst_stat_entry_from_dump(fd, duration); - if (ret != BATTERY_MONITOR_ERROR_NONE) - _ERR("GST data dump failed"); - 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 - ret = bd_get_and_print_battery_usage_from_dump(fd, duration); - if (ret != BATTERY_MONITOR_ERROR_NONE) - _ERR("Battery Usage failed"); + //Dump i & l values after this + ret = bd_get_and_print_uid_info_entry_from_dump(fd); + if (ret != BATTERY_MONITOR_ERROR_NONE) + _ERR("UID data dump failed"); + // Get Last charge time to dump "l" field + // for the time being dumping all data + long int duration = 0; //Time of last charge + ret = bd_get_and_print_gn_stat_entry_from_dump(fd, duration); + if (ret != BATTERY_MONITOR_ERROR_NONE) + _ERR("GN dump failed"); + ret = bd_get_and_print_gwf_stat_entry_from_dump(fd, duration); + if (ret != BATTERY_MONITOR_ERROR_NONE) + _ERR("GWF data dump failed"); + ret = bd_get_and_print_gble_stat_entry_from_dump(fd, duration); + if (ret != BATTERY_MONITOR_ERROR_NONE) + _ERR("GBL data dump failed"); + ret = bd_get_and_print_gbr_stat_entry_from_dump(fd, duration); + if (ret != BATTERY_MONITOR_ERROR_NONE) + _ERR("GBR data dump failed"); + ret = bd_get_and_print_gst_stat_entry_from_dump(fd, duration); + if (ret != BATTERY_MONITOR_ERROR_NONE) + _ERR("GST data dump failed"); + 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); + if (ret != BATTERY_MONITOR_ERROR_NONE) + _ERR("GCPU data dump failed"); + ret = bd_get_and_print_misc_stat_entry_from_dump(fd, duration); + if (ret != BATTERY_MONITOR_ERROR_NONE) + _ERR("MISc data dump failed"); + // Battery Usage Dump + // Add usage for each of the resource + // and each of the application + ret = bd_get_and_print_battery_usage_from_dump(fd, duration); + if (ret != BATTERY_MONITOR_ERROR_NONE) + _ERR("Battery Usage failed"); - _DBG("DUMP COMPLETED/DELETE HASH TABLE"); + _DBG("DUMP COMPLETED/DELETE HASH TABLE"); - g_hash_table_destroy(app_map); - h_flag = true; - EXIT; - return BATTERY_MONITOR_ERROR_NONE; + g_hash_table_destroy(app_map); + h_flag = true; + + EXIT; + return BATTERY_MONITOR_ERROR_NONE; } static int bd_dump_data_item_reset(dump_data_s* old_state) diff --git a/src/bm_config_parser.c b/src/bm_config_parser.c index 0d5112e..9641bc6 100644 --- a/src/bm_config_parser.c +++ b/src/bm_config_parser.c @@ -56,7 +56,7 @@ static char* bm_read_content_from_file(const char *file_name) goto END; } - _INFO("read [%zd] bytes from [%s]", len, file_name); + _DBG("read [%zd] bytes from [%s]", len, file_name); EXIT; return (char *)contents; @@ -64,15 +64,15 @@ END: return NULL; } -int bm_get_wifi_power_params(int *rssi_l0, int *rssi_l1, int *rssi_l2, int *rssi_l3, - int *rssi_l4, int *scan, int *idle, int *rx, int *tx) +int bm_get_wifi_power_params(double *rssi_l0, double *rssi_l1, double *rssi_l2, double *rssi_l3, + double *rssi_l4, double *scan, double *idle, double *rx, double *tx) { ENTER; int ret = BATTERY_MONITOR_ERROR_INTERNAL; char *contents = NULL; - json_object *jmain = NULL, *jint = NULL, *jvalue = NULL; + json_object *jmain = NULL, *jdouble = NULL, *jvalue = NULL; BM_CHECK_INPUT_PARAM(rssi_l0); BM_CHECK_INPUT_PARAM(rssi_l1); @@ -84,7 +84,7 @@ int bm_get_wifi_power_params(int *rssi_l0, int *rssi_l1, int *rssi_l2, int *rssi BM_CHECK_INPUT_PARAM(rx); BM_CHECK_INPUT_PARAM(tx); - contents = bm_read_content_from_file(BATTERY_MONITOR_CONFIG_FILE_PATH); + contents = bm_read_content_from_file(BATTERY_MONITOR_POWER_PROFILE_CONFIG_FILE_PATH); BM_RETURN_VAL((contents != NULL), {}, BATTERY_MONITOR_ERROR_NO_DATA, "failed to read from file"); @@ -98,53 +98,53 @@ int bm_get_wifi_power_params(int *rssi_l0, int *rssi_l1, int *rssi_l2, int *rssi if (!jvalue) goto END; - json_object_object_get_ex(jvalue, "p_wifi_rssi_0", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_wifi_rssi_0", &jdouble); + if (!jdouble) goto END; - *rssi_l0 = json_object_get_int(jint); + *rssi_l0 = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_wifi_rssi_1", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_wifi_rssi_1", &jdouble); + if (!jdouble) goto END; - *rssi_l1 = json_object_get_int(jint); + *rssi_l1 = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_wifi_rssi_2", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_wifi_rssi_2", &jdouble); + if (!jdouble) goto END; - *rssi_l2 = json_object_get_int(jint); + *rssi_l2 = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_wifi_rssi_3", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_wifi_rssi_3", &jdouble); + if (!jdouble) goto END; - *rssi_l3 = json_object_get_int(jint); + *rssi_l3 = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_wifi_rssi_4", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_wifi_rssi_4", &jdouble); + if (!jdouble) goto END; - *rssi_l4 = json_object_get_int(jint); + *rssi_l4 = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_wifi_scan", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_wifi_scan", &jdouble); + if (!jdouble) goto END; - *scan = json_object_get_int(jint); + *scan = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_wifi_idle", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_wifi_idle", &jdouble); + if (!jdouble) goto END; - *idle = json_object_get_int(jint); + *idle = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_wifi_rx", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_wifi_rx", &jdouble); + if (!jdouble) goto END; - *rx = json_object_get_int(jint); + *rx = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_wifi_tx", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_wifi_tx", &jdouble); + if (!jdouble) goto END; - *tx = json_object_get_int(jint); + *tx = json_object_get_double(jdouble); - _DBG("wifi power params - rssi_0[%d], rssi_1[%d], rssi_2[%d], \ - rssi_3[%d], rssi_4[%d], scan[%d], idle[%d], rx[%d], tx[%d]", \ + _DBG("wifi power params - rssi_0[%lf], rssi_1[%lf], rssi_2[%lf], \ + rssi_3[%lf], rssi_4[%lf], scan[%lf], idle[%lf], rx[%lf], tx[%lf]", \ *rssi_l0, *rssi_l1, *rssi_l2, *rssi_l3, *rssi_l4, *scan, \ *idle, *rx, *tx); @@ -158,14 +158,15 @@ END: return ret; } -int bm_get_blutooth_power_params(int *idle, int *scan, int *connected, int *rx, int *tx) +int bm_get_blutooth_power_params(double *idle, double *scan, double *connected, + double *rx, double *tx) { ENTER; int ret = BATTERY_MONITOR_ERROR_INTERNAL; char *contents = NULL; - json_object *jmain = NULL, *jint = NULL, *jvalue = NULL; + json_object *jmain = NULL, *jdouble = NULL, *jvalue = NULL; BM_CHECK_INPUT_PARAM(idle); BM_CHECK_INPUT_PARAM(scan); @@ -173,7 +174,7 @@ int bm_get_blutooth_power_params(int *idle, int *scan, int *connected, int *rx, BM_CHECK_INPUT_PARAM(rx); BM_CHECK_INPUT_PARAM(tx); - contents = bm_read_content_from_file(BATTERY_MONITOR_CONFIG_FILE_PATH); + contents = bm_read_content_from_file(BATTERY_MONITOR_POWER_PROFILE_CONFIG_FILE_PATH); BM_RETURN_VAL((contents != NULL), {}, BATTERY_MONITOR_ERROR_NO_DATA, "failed to read from file"); @@ -187,33 +188,33 @@ int bm_get_blutooth_power_params(int *idle, int *scan, int *connected, int *rx, if (!jvalue) goto END; - json_object_object_get_ex(jvalue, "p_bt_idle", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_bt_idle", &jdouble); + if (!jdouble) goto END; - *idle = json_object_get_int(jint); + *idle = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_bt_scan", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_bt_scan", &jdouble); + if (!jdouble) goto END; - *scan = json_object_get_int(jint); + *scan = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_bt_connected", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_bt_connected", &jdouble); + if (!jdouble) goto END; - *connected = json_object_get_int(jint); + *connected = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_bt_rx", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_bt_rx", &jdouble); + if (!jdouble) goto END; - *rx = json_object_get_int(jint); + *rx = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_bt_tx", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_bt_tx", &jdouble); + if (!jdouble) goto END; - *tx = json_object_get_int(jint); + *tx = json_object_get_double(jdouble); - _DBG("bluetooth power params - idle[%d], scan[%d], connected[%d], \ - rx[%d], tx[%d]", *idle, *scan, *connected, *rx, *tx); + _DBG("bluetooth power params - idle[%lf], scan[%lf], connected[%lf], \ + rx[%lf], tx[%lf]", *idle, *scan, *connected, *rx, *tx); ret = BATTERY_MONITOR_ERROR_NONE; END: @@ -225,16 +226,16 @@ END: return ret; } -int bm_get_device_network_power_params(int *signal_strength0, int *signal_strength1, int *signal_strength2, - int *signal_strength3, int *signal_strength4, int *signal_strength5, - int *signal_strength6, int *rx, int *tx) +int bm_get_device_network_power_params(double *signal_strength0, double *signal_strength1, + double *signal_strength2, double *signal_strength3, double *signal_strength4, + double *signal_strength5, double *signal_strength6, double *rx, double *tx) { ENTER; int ret = BATTERY_MONITOR_ERROR_INTERNAL; char *contents = NULL; - json_object *jmain = NULL, *jint = NULL, *jvalue = NULL; + json_object *jmain = NULL, *jdouble = NULL, *jvalue = NULL; BM_CHECK_INPUT_PARAM(signal_strength0); BM_CHECK_INPUT_PARAM(signal_strength1); @@ -246,7 +247,7 @@ int bm_get_device_network_power_params(int *signal_strength0, int *signal_streng BM_CHECK_INPUT_PARAM(rx); BM_CHECK_INPUT_PARAM(tx); - contents = bm_read_content_from_file(BATTERY_MONITOR_CONFIG_FILE_PATH); + contents = bm_read_content_from_file(BATTERY_MONITOR_POWER_PROFILE_CONFIG_FILE_PATH); BM_RETURN_VAL((contents != NULL), {}, BATTERY_MONITOR_ERROR_NO_DATA, "failed to read from file"); @@ -260,55 +261,55 @@ int bm_get_device_network_power_params(int *signal_strength0, int *signal_streng if (!jvalue) goto END; - json_object_object_get_ex(jvalue, "p_dnw_sig_strength_0", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_dnw_sig_strength_0", &jdouble); + if (!jdouble) goto END; - *signal_strength0 = json_object_get_int(jint); + *signal_strength0 = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_dnw_sig_strength_1", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_dnw_sig_strength_1", &jdouble); + if (!jdouble) goto END; - *signal_strength1 = json_object_get_int(jint); + *signal_strength1 = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_dnw_sig_strength_2", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_dnw_sig_strength_2", &jdouble); + if (!jdouble) goto END; - *signal_strength2 = json_object_get_int(jint); + *signal_strength2 = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_dnw_sig_strength_3", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_dnw_sig_strength_3", &jdouble); + if (!jdouble) goto END; - *signal_strength3 = json_object_get_int(jint); + *signal_strength3 = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_dnw_sig_strength_4", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_dnw_sig_strength_4", &jdouble); + if (!jdouble) goto END; - *signal_strength4 = json_object_get_int(jint); + *signal_strength4 = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_dnw_sig_strength_5", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_dnw_sig_strength_5", &jdouble); + if (!jdouble) goto END; - *signal_strength5 = json_object_get_int(jint); + *signal_strength5 = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_dnw_sig_strength_6", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_dnw_sig_strength_6", &jdouble); + if (!jdouble) goto END; - *signal_strength6 = json_object_get_int(jint); + *signal_strength6 = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_dnw_rx", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_dnw_rx", &jdouble); + if (!jdouble) goto END; - *rx = json_object_get_int(jint); + *rx = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_dnw_tx", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_dnw_tx", &jdouble); + if (!jdouble) goto END; - *tx = json_object_get_int(jint); + *tx = json_object_get_double(jdouble); - _DBG("device-network power params - signal_strength0[%d], signal_strength1[%d], signal_strength2[%d], \ - signal_strength3[%d], signal_strength4[%d], signal_strength5[%d], signal_strength6[%d], rx[%d], \ - tx[%d]", *signal_strength0, *signal_strength1, *signal_strength2, *signal_strength3, \ - *signal_strength4, *signal_strength5, *signal_strength6, *rx, *tx); + _DBG("device-network params - signal_strength [%lf, %lf, %lf, %lf, %lf, %lf, %lf], \ + rx[%lf], tx[%lf]", *signal_strength0, *signal_strength1, *signal_strength2, + *signal_strength3, *signal_strength4, *signal_strength5, *signal_strength6, + *rx, *tx); ret = BATTERY_MONITOR_ERROR_NONE; END: @@ -320,20 +321,20 @@ END: return ret; } -int bm_get_display_power_params(int *high, int *medium, int *low) +int bm_get_display_power_params(double *high, double *medium, double *low) { ENTER; int ret = BATTERY_MONITOR_ERROR_INTERNAL; char *contents = NULL; - json_object *jmain = NULL, *jint = NULL, *jvalue = NULL; + json_object *jmain = NULL, *jdouble = NULL, *jvalue = NULL; BM_CHECK_INPUT_PARAM(high); BM_CHECK_INPUT_PARAM(medium); BM_CHECK_INPUT_PARAM(low); - contents = bm_read_content_from_file(BATTERY_MONITOR_CONFIG_FILE_PATH); + contents = bm_read_content_from_file(BATTERY_MONITOR_POWER_PROFILE_CONFIG_FILE_PATH); BM_RETURN_VAL((contents != NULL), {}, BATTERY_MONITOR_ERROR_NO_DATA, "failed to read from file"); @@ -347,23 +348,23 @@ int bm_get_display_power_params(int *high, int *medium, int *low) if (!jvalue) goto END; - json_object_object_get_ex(jvalue, "p_dp_high", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_dp_high", &jdouble); + if (!jdouble) goto END; - *high = json_object_get_int(jint); + *high = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_dp_medium", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_dp_medium", &jdouble); + if (!jdouble) goto END; - *medium = json_object_get_int(jint); + *medium = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_dp_low", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_dp_low", &jdouble); + if (!jdouble) goto END; - *low = json_object_get_int(jint); + *low = json_object_get_double(jdouble); - _DBG("display power params - high[%d], medium[%d], low[%d]", - *high, *medium, *low); + _DBG("display power params - high[%lf], medium[%lf], low[%lf]", + *high, *medium, *low); ret = BATTERY_MONITOR_ERROR_NONE; END: @@ -376,18 +377,18 @@ END: } #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM -int bm_get_gps_sensor_power_params(int *gps_use) +int bm_get_gps_sensor_power_params(double *gps_use) { ENTER; int ret = BATTERY_MONITOR_ERROR_INTERNAL; char *contents = NULL; - json_object *jmain = NULL, *jint = NULL, *jvalue = NULL; + json_object *jmain = NULL, *jdouble = NULL, *jvalue = NULL; BM_CHECK_INPUT_PARAM(gps_use); - contents = bm_read_content_from_file(BATTERY_MONITOR_CONFIG_FILE_PATH); + contents = bm_read_content_from_file(BATTERY_MONITOR_POWER_PROFILE_CONFIG_FILE_PATH); BM_RETURN_VAL((contents != NULL), {}, BATTERY_MONITOR_ERROR_NO_DATA, "failed to read from file"); @@ -401,12 +402,12 @@ int bm_get_gps_sensor_power_params(int *gps_use) if (!jvalue) goto END; - json_object_object_get_ex(jvalue, "p_gps_inuse", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_gps_inuse", &jdouble); + if (!jdouble) goto END; - *gps_use = json_object_get_int(jint); + *gps_use = json_object_get_double(jdouble); - _DBG("gps-sensor power params - gps[%d]", *gps_use); + _DBG("gps-sensor power params - gps[%lf]", *gps_use); ret = BATTERY_MONITOR_ERROR_NONE; END: @@ -418,18 +419,18 @@ END: return ret; } -int bm_get_hrm_sensor_power_params(int *hrm_use) +int bm_get_hrm_sensor_power_params(double *hrm_use) { ENTER; int ret = BATTERY_MONITOR_ERROR_INTERNAL; char *contents = NULL; - json_object *jmain = NULL, *jint = NULL, *jvalue = NULL; + json_object *jmain = NULL, *jdouble = NULL, *jvalue = NULL; BM_CHECK_INPUT_PARAM(hrm_use); - contents = bm_read_content_from_file(BATTERY_MONITOR_CONFIG_FILE_PATH); + contents = bm_read_content_from_file(BATTERY_MONITOR_POWER_PROFILE_CONFIG_FILE_PATH); BM_RETURN_VAL((contents != NULL), {}, BATTERY_MONITOR_ERROR_NO_DATA, "failed to read from file"); @@ -443,12 +444,12 @@ int bm_get_hrm_sensor_power_params(int *hrm_use) if (!jvalue) goto END; - json_object_object_get_ex(jvalue, "p_hrm_inuse", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_hrm_inuse", &jdouble); + if (!jdouble) goto END; - *hrm_use = json_object_get_int(jint); + *hrm_use = json_object_get_double(jdouble); - _DBG("hrm-sensor power params - gps[%d]", *hrm_use); + _DBG("hrm-sensor power params - gps[%lf]", *hrm_use); ret = BATTERY_MONITOR_ERROR_NONE; END: @@ -461,19 +462,19 @@ END: } #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM */ -int bm_get_cpu_power_params(int *user, int *system) +int bm_get_cpu_power_params(double *user, double *system) { ENTER; int ret = BATTERY_MONITOR_ERROR_INTERNAL; char *contents = NULL; - json_object *jmain = NULL, *jint = NULL, *jvalue = NULL; + json_object *jmain = NULL, *jdouble = NULL, *jvalue = NULL; BM_CHECK_INPUT_PARAM(user); BM_CHECK_INPUT_PARAM(system); - contents = bm_read_content_from_file(BATTERY_MONITOR_CONFIG_FILE_PATH); + contents = bm_read_content_from_file(BATTERY_MONITOR_POWER_PROFILE_CONFIG_FILE_PATH); BM_RETURN_VAL((contents != NULL), {}, BATTERY_MONITOR_ERROR_NO_DATA, "failed to read from file"); @@ -487,17 +488,17 @@ int bm_get_cpu_power_params(int *user, int *system) if (!jvalue) goto END; - json_object_object_get_ex(jvalue, "p_cpu_user", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_cpu_user", &jdouble); + if (!jdouble) goto END; - *user = json_object_get_int(jint); + *user = json_object_get_double(jdouble); - json_object_object_get_ex(jvalue, "p_cpu_system", &jint); - if (!jint) + json_object_object_get_ex(jvalue, "p_cpu_system", &jdouble); + if (!jdouble) goto END; - *system = json_object_get_int(jint); + *system = json_object_get_double(jdouble); - _DBG("cpu power params - user[%d], system[%d]", *user, *system); + _DBG("cpu power params - user[%lf], system[%lf]", *user, *system); ret = BATTERY_MONITOR_ERROR_NONE; END: @@ -520,7 +521,7 @@ int bm_get_battery_power_params(int *battery) BM_CHECK_INPUT_PARAM(battery); - contents = bm_read_content_from_file(BATTERY_MONITOR_CONFIG_FILE_PATH); + contents = bm_read_content_from_file(BATTERY_MONITOR_POWER_PROFILE_CONFIG_FILE_PATH); BM_RETURN_VAL((contents != NULL), {}, BATTERY_MONITOR_ERROR_NO_DATA, "failed to read from file"); @@ -550,3 +551,127 @@ END: EXIT; return ret; } + +int bm_get_job_scheduler_params(int *data_collection_period, int *data_collection_try_period, + int *data_collection_accept_count, int *delete_db_period) +{ + ENTER; + + int ret = BATTERY_MONITOR_ERROR_INTERNAL; + + char *contents = NULL; + json_object *jmain = NULL, *jint = NULL, *jvalue = NULL; + + BM_CHECK_INPUT_PARAM(data_collection_period); + BM_CHECK_INPUT_PARAM(data_collection_try_period); + BM_CHECK_INPUT_PARAM(data_collection_accept_count); + BM_CHECK_INPUT_PARAM(delete_db_period); + + contents = bm_read_content_from_file(BATTERY_MONITOR_CONFIG_FILE_PATH); + + BM_RETURN_VAL((contents != NULL), {}, + BATTERY_MONITOR_ERROR_NO_DATA, "failed to read from file"); + + jmain = json_tokener_parse(contents); + if (!jmain) + goto END; + + json_object_object_get_ex(jmain, "job_scheduler", &jvalue); + if (!jvalue) + goto END; + + json_object_object_get_ex(jvalue, "data_collection_period", &jint); + if (!jint) + goto END; + *data_collection_period = json_object_get_int(jint); + + json_object_object_get_ex(jvalue, "data_collection_try_period", &jint); + if (!jint) + goto END; + *data_collection_try_period = json_object_get_int(jint); + + json_object_object_get_ex(jvalue, "data_collection_accept_count", &jint); + if (!jint) + goto END; + *data_collection_accept_count = json_object_get_int(jint); + + json_object_object_get_ex(jvalue, "delete_db_period", &jint); + if (!jint) + goto END; + *delete_db_period = json_object_get_int(jint); + + _DBG("job scheduler params - data_collection_period[%d], data_collection_try_period[%d], \ + data_collection_accept_count[%d], delete_db_period[%d]", *data_collection_period, + *data_collection_try_period, *data_collection_accept_count, *delete_db_period); + + ret = BATTERY_MONITOR_ERROR_NONE; +END: + g_free(contents); + if (jmain) + json_object_put(jmain); + + EXIT; + return ret; +} + +int bm_get_cpu_usage_checker_params(int *timeout, double *initial_threshold, + double *minimum_threshold, double *threshold_reduction) +{ + ENTER; + + int ret = BATTERY_MONITOR_ERROR_INTERNAL; + + char *contents = NULL; + json_object *jmain = NULL, *jint = NULL, *jdouble = NULL, *jvalue = NULL; + + BM_CHECK_INPUT_PARAM(timeout); + BM_CHECK_INPUT_PARAM(initial_threshold); + BM_CHECK_INPUT_PARAM(minimum_threshold); + BM_CHECK_INPUT_PARAM(threshold_reduction); + + contents = bm_read_content_from_file(BATTERY_MONITOR_CONFIG_FILE_PATH); + + BM_RETURN_VAL((contents != NULL), {}, + BATTERY_MONITOR_ERROR_NO_DATA, "failed to read from file"); + + jmain = json_tokener_parse(contents); + if (!jmain) + goto END; + + json_object_object_get_ex(jmain, "cpu_usage_checker", &jvalue); + if (!jvalue) + goto END; + + json_object_object_get_ex(jvalue, "timeout", &jint); + if (!jint) + goto END; + *timeout = json_object_get_int(jint); + + json_object_object_get_ex(jvalue, "initial_threshold", &jdouble); + if (!jdouble) + goto END; + *initial_threshold = json_object_get_double(jdouble); + + json_object_object_get_ex(jvalue, "minimum_threshold", &jdouble); + if (!jdouble) + goto END; + *minimum_threshold = json_object_get_double(jdouble); + + json_object_object_get_ex(jvalue, "threshold_reduction", &jdouble); + if (!jdouble) + goto END; + *threshold_reduction = json_object_get_double(jdouble); + + _DBG("data collection params - timeout[%d], initial_threshold[%lf], \ + minimum_threshold[%lf], threshold_reduction[%lf]", *timeout, *initial_threshold, + *minimum_threshold, *threshold_reduction); + + ret = BATTERY_MONITOR_ERROR_NONE; +END: + g_free(contents); + if (jmain) + json_object_put(jmain); + + EXIT; + return ret; +} diff --git a/src/bm_plugin_manager.c b/src/bm_plugin_manager.c index e047f2b..337ef4b 100644 --- a/src/bm_plugin_manager.c +++ b/src/bm_plugin_manager.c @@ -35,10 +35,8 @@ const bm_callbacks_st gps_cb; const bm_callbacks_st hrm_cb; #endif -void bm_populate_callbacks(void) +inline void bm_populate_callbacks(void) { - ENTER; - bm_receiver_callbacks[BM_DATA_TYPE_BLE] = &ble_cb; bm_receiver_callbacks[BM_DATA_TYPE_WIFI] = &wifi_cb; bm_receiver_callbacks[BM_DATA_TYPE_CPU] = &cpu_cb; @@ -49,7 +47,6 @@ void bm_populate_callbacks(void) bm_receiver_callbacks[BM_DATA_TYPE_HRM_SENSOR] = &hrm_cb; #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */ - EXIT; return; } @@ -111,8 +108,6 @@ static int bm_init_loaded_plugins() bm_plugin_info_h *bm_plugin = NULL; const bm_callbacks_st *bm_cb = NULL; - _INFO("populate callbacks"); - bm_populate_callbacks(); #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN for (id = BM_PLUGIN_ID_BLE; id < BM_PLUGIN_ID_MAX; ++id) { diff --git a/src/bm_power_engine.c b/src/bm_power_engine.c index a9edd46..2f839d2 100644 --- a/src/bm_power_engine.c +++ b/src/bm_power_engine.c @@ -19,6 +19,7 @@ #include #include +#include #include "bm_power_engine.h" #include "bm_dbg.h" @@ -37,6 +38,7 @@ static struct bm_req_feature_data_handle_flag_s *bm_req_flag_h = NULL; const double cmah = 3600; int battery_capacity = 500; int gtimeo_id[2]; +static int data_collection_period = 600000; int bm_engine_get_mah_usage_by_app_id_for_resource_id_ci(const gchar* app_id, gint resource_id, gint64 start_time, gint64 end_time, double *battery_usage) { @@ -45,7 +47,7 @@ int bm_engine_get_mah_usage_by_app_id_for_resource_id_ci(const gchar* app_id, gi BM_RETURN_VAL((resource_id < BM_PLUGIN_ID_MAX), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid resource-id param"); _DBG("usage requested - app-id[%s], resource_id[%d], start_time [%ld] end_time[%ld]", - app_id, resource_id, (long int)start_time,(long int)end_time); + app_id, resource_id, (long int)start_time, (long int)end_time); int error = BATTERY_MONITOR_ERROR_NONE; appid_usage_s *app_usage = bm_server_query_app_usage_by_appid_ci(app_id, start_time, end_time, &error); @@ -191,7 +193,6 @@ int bm_engine_get_all_resource_usage_handle_ci(const gchar* app_id, gint64 start EXIT; return BATTERY_MONITOR_ERROR_NONE; - EXIT; } void bm_engine_set_req_flag_handle(bm_plugin_id_e req_plugin_id, bool value) @@ -338,7 +339,7 @@ static int bm_insert_appid_session_usage_to_db() int ret_val = BATTERY_MONITOR_ERROR_NONE; - long int ret_time = bm_get_log_time(); + long int ret_time = bm_get_log_time(data_collection_period); g_hash_table_iter_init(&iter, gl_hash); @@ -442,15 +443,17 @@ int bm_ble_calc_power_and_commit(bm_bluetooth_st *handle, bool mode) } } } + // startTime and stopTime use seconds. So, we don't have to convert sesTime + conTime /= 1000; rx_time /= 1000; tx_time /= 1000; /* Read standard Rated Values from Device Spec File/Power Profile File */ - int sP_power_bt_idle = 0, sP_power_bt_scan = 0, sP_power_bt_conn = 0, sP_power_bt_tx = 0, sP_power_bt_rx = 0; + double sP_power_bt_idle = 0, sP_power_bt_scan = 0, sP_power_bt_conn = 0, sP_power_bt_tx = 0, sP_power_bt_rx = 0; ret_val = bm_get_blutooth_power_params(&sP_power_bt_idle, &sP_power_bt_scan, &sP_power_bt_conn, &sP_power_bt_rx, &sP_power_bt_tx); if (ret_val != BATTERY_MONITOR_ERROR_NONE) _ERR("failed to get bt power params"); - _DBG("received bt power params - idle[%d], scan[%d], connected[%d], rx[%d], tx[%d]", + _DBG("received bt power params - idle[%lf], scan[%lf], connected[%lf], rx[%lf], tx[%lf]", sP_power_bt_idle, sP_power_bt_scan, sP_power_bt_conn, sP_power_bt_tx, sP_power_bt_rx); /* BT power consumption Level - 1 at the Resource Level */ long int P_power_bt = 0, P_power_data = 0, P_power_conn = 0; @@ -491,7 +494,7 @@ int bm_ble_calc_power_and_commit(bm_bluetooth_st *handle, bool mode) long int P_power_app_bt = 0, P_power_app_data = 0, P_power_app_conn = 0; char *appid = NULL; long int apprx = 0, apptx = 0; - long int ret_time = bm_get_log_time(); + long int ret_time = bm_get_log_time(data_collection_period); /* Coefficient to divide the rx and tx time wrt rx and tx bytes */ double crtx_t = 0; @@ -577,7 +580,7 @@ int bm_ble_calc_power_and_commit(bm_bluetooth_st *handle, bool mode) _ERR("memory allocation failed"); return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY; } - gbst_st->time_s = ret_time; gbst_st->off_time = BATTERY_MONITOR_GET_DATA_JOB_PERIOD/1000; + gbst_st->time_s = ret_time; gbst_st->off_time = data_collection_period / 1000; gbst_st->off_time -= sesTime; gbst_st->low_time = 0/*(sesTime - (scTime + conTime))*/; gbst_st->med_time = 0/*scTime*/; gbst_st->high_time = 0/*conTime*/; ret_val = bm_server_battery_dump_insert_to_db(gbst_st, 6); @@ -588,14 +591,14 @@ int bm_ble_calc_power_and_commit(bm_bluetooth_st *handle, bool mode) g_hash_table_destroy(hash); /* Dump resource usage */ - _DBG("BLE usage : %ld", RX_system + TX_system + RX_app + TX_app); + _DBG("BLE usage : %ld bytes", RX_system + TX_system + RX_app + TX_app); if (RX_system > 0 || TX_system > 0 || RX_app > 0 || TX_app > 0) { history_item_s hi; bd_print_history_item_reset(&hi); hi.cmd_s = CM_USAGE; hi.usage_type = USAGE_BLUETOOTH; - hi.usage = RX_system + TX_system + RX_app + TX_app; + hi.usage = (RX_system + TX_system + RX_app + TX_app) / ((double)data_collection_period / 60000); // bytes / minute bd_store_history_item(&hi); } @@ -623,19 +626,18 @@ int bm_wifi_calc_power_and_commit(bm_wifi_st *handle, bool mode) long int sesTime = 0, scTime = 0, tl_total = 0; long int tl0 = 0, tl1 = 0, tl2 = 0, tl3 = 0, tl4 = 0; double onTime = 0; - long int RX = 0, TX = 0; + long int RX = 0, TX = 0, rx_time = 0, tx_time = 0; /* iterating over list for data accumulation */ for (wifi_data_iterator = handle->wifi_data_list; wifi_data_iterator; wifi_data_iterator = wifi_data_iterator->next) { bm_wifi_st *datalistnode = (bm_wifi_st *)(wifi_data_iterator->data); edTime = datalistnode->endTime; stTime = datalistnode->startTime; - sesTime += edTime - stTime; + sesTime += (edTime - stTime); scTime += datalistnode->scanTime; - tl0 += datalistnode->time_level_0; - tl1 += datalistnode->time_level_1; - tl2 += datalistnode->time_level_2; - tl3 += datalistnode->time_level_3; + tl0 += datalistnode->time_level_0; tl1 += datalistnode->time_level_1; + tl2 += datalistnode->time_level_2; tl3 += datalistnode->time_level_3; tl4 += datalistnode->time_level_4; + rx_time += datalistnode->rxTime; tx_time += datalistnode->txTime; wifi_atm_iterator = datalistnode->atm_list; for ( ; wifi_atm_iterator; wifi_atm_iterator = wifi_atm_iterator->next) { app_time_map_st2 *wifi_atm_node = NULL; @@ -668,20 +670,21 @@ int bm_wifi_calc_power_and_commit(bm_wifi_st *handle, bool mode) } /* Read standard Rated Values from Device Spec File/Power Profile File */ - int sP_power_wf_idle = 0, sP_power_wf_scan = 0, sP_power_wf_tl0 = 0, sP_power_wf_tl1 = 0, sP_power_wf_tl2 = 0; - int sP_power_wf_tl3 = 0, sP_power_wf_tl4 = 0, sP_power_wf_tx = 0, sP_power_wf_rx = 0; + double sP_power_wf_idle = 0, sP_power_wf_scan = 0, sP_power_wf_tl0 = 0, sP_power_wf_tl1 = 0, sP_power_wf_tl2 = 0; + double sP_power_wf_tl3 = 0, sP_power_wf_tl4 = 0, sP_power_wf_tx = 0, sP_power_wf_rx = 0; ret_val = bm_get_wifi_power_params(&sP_power_wf_tl0, &sP_power_wf_tl1, &sP_power_wf_tl2, &sP_power_wf_tl3, &sP_power_wf_tl4, &sP_power_wf_scan, &sP_power_wf_idle, &sP_power_wf_rx, &sP_power_wf_tx); if (ret_val != BATTERY_MONITOR_ERROR_NONE) _ERR("failed to get wifi power params"); - _DBG("received wifi power params - rssi_0[%d], rssi_1[%d], rssi_2[%d], rssi_3[%d], rssi_4[%d], scan[%d], \ - idle[%d], rx[%d], tx[%d]", sP_power_wf_tl0, sP_power_wf_tl1, sP_power_wf_tl2, sP_power_wf_tl3, + _DBG("received wifi power params - rssi_0[%lf], rssi_1[%lf], rssi_2[%lf], rssi_3[%lf], rssi_4[%lf], scan[%lf], \ + idle[%lf], rx[%lf], tx[%lf]", sP_power_wf_tl0, sP_power_wf_tl1, sP_power_wf_tl2, sP_power_wf_tl3, sP_power_wf_tl4, sP_power_wf_scan, sP_power_wf_idle, sP_power_wf_rx, sP_power_wf_tx); tl0 /= 1000; tl1 /= 1000; tl2 /= 1000; tl3 /= 1000; tl4 /= 1000; - scTime /= 1000; sesTime /= 1000; onTime /= 1000; + onTime /= 1000; + rx_time /= 1000; tx_time /= 1000; tl_total = tl0 + tl1 + tl2 + tl3 + tl4; /* Wifi power consumption Level - 1 at the Resource Level */ long int P_power_wifi = 0, P_power_data = 0, P_power_conn = 0; @@ -689,7 +692,11 @@ int bm_wifi_calc_power_and_commit(bm_wifi_st *handle, bool mode) (sP_power_wf_tl2 * tl2) + (sP_power_wf_tl3 * tl3) + (sP_power_wf_tl4 * tl4) + (sP_power_wf_scan * scTime) + (sP_power_wf_idle * (sesTime - (tl_total + scTime)))); - P_power_data = ((sP_power_wf_tx) * TX) + ((sP_power_wf_rx) * RX); + /* Amount based usage calculation */ +// P_power_data = ((sP_power_wf_tx) * TX) + ((sP_power_wf_rx) * RX); + + /* Use rx and tx time to calculate the usage */ + P_power_data = ((sP_power_wf_tx * tx_time) + (sP_power_wf_rx * rx_time)); P_power_wifi = P_power_conn + P_power_data; _DBG("Calculated Power for Wifi P_power_wifi(%ld), P_power_conn (%ld), P_power_data(%ld)", P_power_wifi, P_power_conn, P_power_data); @@ -701,9 +708,15 @@ int bm_wifi_calc_power_and_commit(bm_wifi_st *handle, bool mode) long int P_power_app_wifi = 0, P_power_app_data = 0, P_power_app_conn = 0; char *appid = NULL; long int apprx = 0, apptx = 0; - double apptime = 0; + 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(); + long int ret_time = bm_get_log_time(data_collection_period); + + /* Coefficient to divide the rx and tx time wrt rx and tx bytes */ + if (RX != 0) + crx_t = (double)rx_time/RX; + if (TX != 0) + ctx_t = (double)tx_time/TX; appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s)); if (app_usage == NULL) { @@ -720,7 +733,7 @@ int bm_wifi_calc_power_and_commit(bm_wifi_st *handle, bool mode) apptx = temp->tx; tx_t += apptx; apptime = temp->time; apptime /= 1000; - P_power_app_data = ((sP_power_wf_tx) * apptx) + ((sP_power_wf_rx) * apprx); + P_power_app_data = (sP_power_wf_tx * (ctx_t * apptx)) + (sP_power_wf_rx * (crx_t * apprx)); if (onTime != 0) P_power_app_conn = (P_power_conn * apptime)/onTime; //check for 0 denominator & same units @@ -803,19 +816,14 @@ int bm_wifi_calc_power_and_commit(bm_wifi_st *handle, bool mode) g_hash_table_destroy(hash); /* Dump resource usage */ - _DBG("Wi-Fi usage : %ld", RX + TX); + _DBG("Wi-Fi usage : %ld bytes", RX + TX); if (RX > 0 || TX > 0) { history_item_s hi; bd_print_history_item_reset(&hi); - struct timeval tv; - gettimeofday(&tv, NULL); - - hi.time_s = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000); - hi.cmd_s = CM_USAGE; hi.usage_type = USAGE_WIFI; - hi.usage = RX + TX; + hi.usage = (RX + TX) / ((double)data_collection_period / 60000); // bytes / minute bd_store_history_item(&hi); } @@ -870,13 +878,13 @@ int bm_cpu_calc_power_and_commit(bm_cpu_st *handle, bool mode) } } /* Read standard Rated Values from Device Spec File/Power Profile File */ - int sP_power_cpu_user = 0, sP_power_cpu_system = 0; + double sP_power_cpu_user = 0, sP_power_cpu_system = 0; ret_val = bm_get_cpu_power_params(&sP_power_cpu_user, &sP_power_cpu_system); if (ret_val != BATTERY_MONITOR_ERROR_NONE) _ERR("failed to get cpu power params"); - _DBG("received cpu power params - user[%d]\n, system[%d]\n", sP_power_cpu_user, sP_power_cpu_system); + _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); @@ -894,7 +902,7 @@ int bm_cpu_calc_power_and_commit(bm_cpu_st *handle, bool mode) long int P_power_app_cpu = 0, P_power_app_use = 0; double apptime = 0; char *appid = NULL; - long int ret_time = bm_get_log_time(); + long int ret_time = bm_get_log_time(data_collection_period); appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s)); if (app_usage == NULL) { @@ -1023,13 +1031,13 @@ int bm_display_calc_power_and_commit(bm_display_st *handle, bool mode) } /* Read standard Rated Values from Device Spec File/Power Profile File */ - int sP_power_dp_low = 0, sP_power_dp_med = 0, sP_power_dp_high = 0; + double sP_power_dp_low = 0, sP_power_dp_med = 0, sP_power_dp_high = 0; ret_val = bm_get_display_power_params(&sP_power_dp_high, &sP_power_dp_med, &sP_power_dp_low); if (ret_val != BATTERY_MONITOR_ERROR_NONE) _ERR("failed to get cpu power params"); - _DBG("received display power params - high[%d], medium[%d], low[%d]", sP_power_dp_high, sP_power_dp_med, sP_power_dp_low); + _DBG("received display power params - high[%lf], medium[%lf], low[%lf]", sP_power_dp_high, sP_power_dp_med, sP_power_dp_low); /* 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; @@ -1045,7 +1053,7 @@ int bm_display_calc_power_and_commit(bm_display_st *handle, bool mode) long int P_power_app_disp = 0, P_power_app_on = 0; char *appid = NULL; double apptime = 0; - long int ret_time = bm_get_log_time(); + long int ret_time = bm_get_log_time(data_collection_period); appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s)); if (app_usage == NULL) { @@ -1110,7 +1118,7 @@ int bm_display_calc_power_and_commit(bm_display_st *handle, bool mode) return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY; } - gbr_st->time_s = ret_time; gbr_st->dark = (BATTERY_MONITOR_GET_DATA_JOB_PERIOD/1000); + gbr_st->time_s = ret_time; gbr_st->dark = (data_collection_period / 1000); gbr_st->dark -= sesTime; gbr_st->dim = lowTime; gbr_st->medium = medTime; gbr_st->bright = highTime; @@ -1185,15 +1193,15 @@ int bm_device_network_calc_power_and_commit(bm_device_network_st *handle, bool m } /* Read standard Rated Values from Device Spec File/Power Profile File */ - int sP_power_dn_tl0 = 0, sP_power_dn_tl1 = 0, sP_power_dn_tl2 = 0, sP_power_dn_tl3 = 0, sP_power_dn_tl4 = 0, + double sP_power_dn_tl0 = 0, sP_power_dn_tl1 = 0, sP_power_dn_tl2 = 0, sP_power_dn_tl3 = 0, sP_power_dn_tl4 = 0, sP_power_dn_tl5 = 0, sP_power_dn_tl6 = 0, sP_power_dn_tx = 0, sP_power_dn_rx = 0; ret_val = bm_get_device_network_power_params(&sP_power_dn_tl0, &sP_power_dn_tl1, &sP_power_dn_tl2, &sP_power_dn_tl3, &sP_power_dn_tl4, &sP_power_dn_tl5, &sP_power_dn_tl6, &sP_power_dn_rx, &sP_power_dn_tx); if (ret_val != BATTERY_MONITOR_ERROR_NONE) _ERR("failed to get cpu power params"); - _DBG("received device-network power params - signal_0[%d]\n, signal_1[%d]\n, signal_2[%d]\n, \ - signal_3[%d]\n, signal_4[%d]\n, signal_5[%d]\n, signal_6[%d]\n, rx[%d]\n, tx[%d]\n", + _DBG("received device-network power params - signal_0[%lf]\n, signal_1[%lf]\n, signal_2[%lf]\n, \ + signal_3[%lf]\n, signal_4[%lf]\n, signal_5[%lf]\n, signal_6[%lf]\n, rx[%lf]\n, tx[%lf]\n", sP_power_dn_tl0, sP_power_dn_tl1, sP_power_dn_tl2, sP_power_dn_tl3, sP_power_dn_tl4, sP_power_dn_tl5, sP_power_dn_tl6, sP_power_dn_rx, sP_power_dn_tx); @@ -1215,7 +1223,7 @@ int bm_device_network_calc_power_and_commit(bm_device_network_st *handle, bool m char *appid = NULL; long int apprx = 0, apptx = 0; double apptime = 0; - long int ret_time = bm_get_log_time(); + long int ret_time = bm_get_log_time(data_collection_period); appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s)); if (app_usage == NULL) { @@ -1348,13 +1356,13 @@ int bm_gps_calc_power_and_commit(bm_gps_st *handle) } /* Read standard Rated Values from Device Spec File/Power Profile File */ - int sP_power_gps_conn = 0; + double sP_power_gps_conn = 0; ret_val = bm_get_gps_sensor_power_params(&sP_power_gps_conn); if (ret_val != BATTERY_MONITOR_ERROR_NONE) _ERR("failed to get gps-sensor power params"); - _DBG("gps-sensor power params - gps[%d]", sP_power_gps_conn); + _DBG("gps-sensor power params - gps[%lf]", sP_power_gps_conn); /* GPS power consumption Level - 1 at the Resource Level */ int P_power_gps = 0, P_power_conn = 0; P_power_conn = ((sP_power_gps_conn * sesTime)); @@ -1370,7 +1378,7 @@ int bm_gps_calc_power_and_commit(bm_gps_st *handle) int P_power_app_gps = 0, P_power_app_conn = 0; char *appid = NULL; int apptime = 0; - long int ret_time = bm_get_log_time(); + long int ret_time = bm_get_log_time(data_collection_period); appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s)); if (app_usage == NULL) { @@ -1486,14 +1494,14 @@ int bm_hrm_calc_power_and_commit(bm_hrm_sensor_st *handle) } } - int sP_power_hrm_conn = 0; + double sP_power_hrm_conn = 0; /* read hrm-sensor power params */ ret_val = bm_get_hrm_sensor_power_params(&sP_power_hrm_conn); if (ret_val != BATTERY_MONITOR_ERROR_NONE) _ERR("failed to get hrm-sensor power params"); - _DBG("hrm-sensor power params - gps[%d]", sP_power_hrm_conn); + _DBG("hrm-sensor power params - gps[%lf]", sP_power_hrm_conn); /* apply power calculation on the hash-map data*/ @@ -1801,11 +1809,6 @@ int bm_start_getting_feature_data(void) } } - /* parse data which is received through handles */ - ret_val = bm_get_data_from_handles(); - BM_RETURN_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), {}, - BATTERY_MONITOR_ERROR_INTERNAL, "error in parsing data from handles"); - EXIT; return ret_val; } @@ -1825,18 +1828,235 @@ int bm_clean_db_table_for_job_id(void) return ret_val; } +/** + * We don't use idle time because it can be decreased + * when CPU core is inactive. + * + * Instead, we will use uptime to calculate CPU usage + * CPU_USAGE = (CT_USER + CT_NICE + CT_SYSTEM) / (NUM_CORE * UPTIME) + */ +enum cpu_time { + CT_USER = 0, + CT_NICE, + CT_SYSTEM, +// CT_IDLE, + CT_NUM +}; + +static int data_collection_try_period = 1000; +static int data_collection_accept_count = 5; +static double up_time_last; +static unsigned long long cpu_time_last[CT_NUM]; + +int bm_get_cpu_time(unsigned long long cpu_time[CT_NUM]) +{ + int ret_val = 0; + FILE *fp = NULL; + + fp = fopen("/proc/stat", "r"); + if (!fp) { + _ERR("fopen failed : %m"); + return BATTERY_MONITOR_ERROR_NO_DATA; + } + + ret_val = fscanf(fp, "%*s %llu %llu %llu", &cpu_time[CT_USER], &cpu_time[CT_NICE], &cpu_time[CT_SYSTEM]); + fclose(fp); + + if (ret_val < CT_NUM) { + _ERR("fscanf failed : %m"); + return BATTERY_MONITOR_ERROR_NO_DATA; + } + + return BATTERY_MONITOR_ERROR_NONE; +} + +int bm_get_up_time(double *up_time) +{ + int ret_val = 0; + FILE *fp = NULL; + + fp = fopen("/proc/uptime", "r"); + if (!fp) { + _ERR("fopen failed : %m"); + return BATTERY_MONITOR_ERROR_NO_DATA; + } + + ret_val = fscanf(fp, "%lf", up_time); + fclose(fp); + + if (ret_val < 1) { + _ERR("fscanf failed : %m"); + return BATTERY_MONITOR_ERROR_NO_DATA; + } + + return BATTERY_MONITOR_ERROR_NONE; +} + +gboolean is_cpu_idle(void) +{ + static gboolean is_parameter_loaded = FALSE; + static int timeout = 60000; + static double initial_threshold = 0.60; + static double minimum_threshold = 0.50; + static double threshold_reduction = 0.0; + static int num_cpu_core = 1; + + static int remaining_timeout = 60000; + static double usage_ratio_threshold = 0.60; + + int idx; + unsigned long long cpu_time_cur[CT_NUM]; + unsigned long long cpu_time_diff[CT_NUM]; + double up_time_cur; + double up_time_diff; + double usage_ratio; + + if (!is_parameter_loaded) { + if (bm_get_cpu_usage_checker_params(&timeout, &initial_threshold, &minimum_threshold, &threshold_reduction) != BATTERY_MONITOR_ERROR_NONE) { + _ERR("error getting cpu usage checker parameters"); + return FALSE; + } + remaining_timeout = timeout; + usage_ratio_threshold = initial_threshold; + + if (runtime_info_get_processor_count(&num_cpu_core) != RUNTIME_INFO_ERROR_NONE) { + _ERR("error getting the number of cpu core"); + return FALSE; + } + + is_parameter_loaded = TRUE; + } + + if (remaining_timeout <= 0) { + _DBG("timeout!"); + goto satisfy; + } + + // Get CPU time + if (bm_get_cpu_time(cpu_time_cur) != BATTERY_MONITOR_ERROR_NONE) { + _ERR("error getting CPU time"); + goto unsatisfy; + } + + for (idx = 0; idx < CT_NUM; idx++) { + if (cpu_time_last[idx] > cpu_time_cur[idx]) { + _ERR("error invalid CPU time"); + goto unsatisfy; + } + cpu_time_diff[idx] = cpu_time_cur[idx] - cpu_time_last[idx]; + } + + for (idx = 0; idx < CT_NUM; idx++) + cpu_time_last[idx] = cpu_time_cur[idx]; + + // Get uptime + if (bm_get_up_time(&up_time_cur) != BATTERY_MONITOR_ERROR_NONE) { + _ERR("error getting uptime"); + goto unsatisfy; + } + + if (up_time_last >= up_time_cur) { + _ERR("error invalid uptime"); + goto unsatisfy; + } + + up_time_diff = up_time_cur - up_time_last; + up_time_last = up_time_cur; + + // Calculate CPU usage + usage_ratio = ((double)cpu_time_diff[CT_USER] + cpu_time_diff[CT_NICE] + cpu_time_diff[CT_SYSTEM]) / (up_time_diff * num_cpu_core * 100); + if (usage_ratio > 1.0) + usage_ratio = 1.0; + if (usage_ratio > usage_ratio_threshold) { + _WARN("CPU usage = %.2lf%% (BUSY, criteria[%.2lf%%])", usage_ratio * 100, usage_ratio_threshold * 100); + goto unsatisfy; + } + + _DBG("CPU usage = %.2lf%% (IDLE)", usage_ratio * 100); + +satisfy: + remaining_timeout = timeout; + usage_ratio_threshold = initial_threshold; + + return TRUE; + +unsatisfy: + remaining_timeout -= data_collection_try_period; + if (usage_ratio_threshold > minimum_threshold) + usage_ratio_threshold -= threshold_reduction; + + return FALSE; +} + +gboolean bm_try_request_feature_data(gpointer data) +{ + enum status { + GET_DATA = 0, + CALC_AND_STORE, + }; + + static enum status status = GET_DATA; + static int idle_count = 0; + int ret_val; + + if (!is_cpu_idle()) + return G_SOURCE_CONTINUE; + + idle_count++; + _DBG("Idle count(%d/%d)", idle_count, data_collection_accept_count); + if (idle_count < data_collection_accept_count) + return G_SOURCE_CONTINUE; + + idle_count = 0; + + switch (status) { + case GET_DATA: // Request data to plugin + ret_val = bm_start_getting_feature_data(); + if (ret_val != BATTERY_MONITOR_ERROR_NONE) { + _ERR("error requesting feature data"); + return G_SOURCE_CONTINUE; + } + + status = CALC_AND_STORE; + return G_SOURCE_CONTINUE; + case CALC_AND_STORE: // Calculate power consumption and restore to DB + ret_val = bm_get_data_from_handles(); + if (ret_val != BATTERY_MONITOR_ERROR_NONE) { + _ERR("error in parsing data from handles"); + return G_SOURCE_CONTINUE; + } + + status = GET_DATA; + return G_SOURCE_REMOVE; + default: + _ERR("error invalid status"); + status = GET_DATA; + return G_SOURCE_CONTINUE; + } +} + gboolean bm_request_feature_data(gpointer data) { ENTER; - int ret_val = BATTERY_MONITOR_ERROR_NONE; + int ret_val; - ret_val = bm_start_getting_feature_data(); - if (ret_val != BATTERY_MONITOR_ERROR_NONE) - _ERR("error requesting feature data"); + ret_val = bm_get_cpu_time(cpu_time_last); + if (ret_val != BATTERY_MONITOR_ERROR_NONE) { + _ERR("error getting cpu time"); + return G_SOURCE_CONTINUE; + } + + ret_val = bm_get_up_time(&up_time_last); + if (ret_val != BATTERY_MONITOR_ERROR_NONE) { + _ERR("error getting uptime"); + return G_SOURCE_CONTINUE; + } + + g_timeout_add(data_collection_try_period, bm_try_request_feature_data, NULL); EXIT; - return true; + return G_SOURCE_CONTINUE; } gboolean bm_delete_data_from_db(gpointer data) @@ -1850,7 +2070,7 @@ gboolean bm_delete_data_from_db(gpointer data) _ERR("error cleaning database"); EXIT; - return true; + return G_SOURCE_CONTINUE; } int initialize_power_engine(void) @@ -1858,6 +2078,7 @@ int initialize_power_engine(void) ENTER; int ret_val = BATTERY_MONITOR_ERROR_NONE; + int delete_db_period = 86400000; bm_req_flag_h = (struct bm_req_feature_data_handle_flag_s *) calloc(1, sizeof(struct bm_req_feature_data_handle_flag_s)); @@ -1868,9 +2089,20 @@ int initialize_power_engine(void) bm_set_req_flag_handles(true); + ret_val = bm_get_job_scheduler_params(&data_collection_period, &data_collection_try_period, &data_collection_accept_count, &delete_db_period); + if (ret_val != BATTERY_MONITOR_ERROR_NONE) { + _ERR("error getting job scheduler parameters"); + return ret_val; + } + + BM_RETURN_VAL((data_collection_period > 0), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid data collection period"); + BM_RETURN_VAL((data_collection_try_period > 0), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid data collection try period"); + BM_RETURN_VAL((data_collection_accept_count > 0), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid data collection accept count"); + BM_RETURN_VAL((delete_db_period > 0), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid delete db period"); + gtimeo_id[0] = 0; gtimeo_id[1] = 0; - gtimeo_id[0] = g_timeout_add(BATTERY_MONITOR_GET_DATA_JOB_PERIOD, bm_request_feature_data, NULL); - gtimeo_id[1] = g_timeout_add(BATTERY_MONITOR_DELETE_DB_JOB_PERIOD, bm_delete_data_from_db, NULL); + gtimeo_id[0] = g_timeout_add(data_collection_period, bm_request_feature_data, NULL); + gtimeo_id[1] = g_timeout_add(delete_db_period, bm_delete_data_from_db, NULL); _DBG("timeout with id %d and %d added", gtimeo_id[0], gtimeo_id[1]); ret_val = bm_clean_db_table_for_job_id(); diff --git a/src/bm_server.c b/src/bm_server.c index 789ca1e..177d9c6 100644 --- a/src/bm_server.c +++ b/src/bm_server.c @@ -436,7 +436,7 @@ static void bm_server_deinitialize() /* plugin-manager de-initialization */ deinitialize_plugin_manager(); - /* cynara deinitialization */ + /* cynara de-initialization */ deinitialize_cynara(); /* database de-initialization */ diff --git a/src/bm_util.c b/src/bm_util.c index 81b96ce..0a2b90e 100644 --- a/src/bm_util.c +++ b/src/bm_util.c @@ -392,7 +392,7 @@ void bm_resourceid_usage_free(resourceid_usage_s *data) return; } -long int bm_get_log_time() +long int bm_get_log_time(int bias) { ENTER; @@ -400,8 +400,8 @@ long int bm_get_log_time() time_t curr_time; time(&curr_time); - - ret = curr_time; + bias /= 1000; + ret = (curr_time - bias); EXIT; return ret; -- 2.7.4