Remove busy wait 43/231143/3
authorKichan Kwon <k_c.kwon@samsung.com>
Fri, 17 Apr 2020 11:05:41 +0000 (20:05 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Fri, 17 Apr 2020 11:10:50 +0000 (20:10 +0900)
- In addition, remove ENTER/EXIT logs from data collection functions

Change-Id: Idc45299c0dc34dc338b7c6d3cd8f437496c34989
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
config/bm_params.json
include/bm_config_parser.h
src/bm_config_parser.c
src/bm_power_engine.c

index 371cb29..546c550 100644 (file)
@@ -2,13 +2,13 @@
        "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,
-               "accept_count": 5
+               "threshold_reduction": 0.00
        }
 }
index df7c6c7..c0f5f8e 100644 (file)
@@ -49,9 +49,9 @@ 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 *delete_db_period);
+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, int *accept_count);
+int bm_get_cpu_usage_checker_params(int *timeout, double *initial_threshold, double *minimum_threshold, double *threshold_reduction);
 
 #ifdef __cplusplus
 }
index df3ad02..bbc9f75 100644 (file)
@@ -551,7 +551,7 @@ END:
        return ret;
 }
 
-int bm_get_job_scheduler_params(int *data_collection_period, int *data_collection_try_period, int *delete_db_period)
+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;
 
@@ -562,6 +562,7 @@ int bm_get_job_scheduler_params(int *data_collection_period, int *data_collectio
 
        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);
@@ -587,12 +588,17 @@ int bm_get_job_scheduler_params(int *data_collection_period, int *data_collectio
                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], delete_db_period[%d]", *data_collection_period, *data_collection_try_period, *delete_db_period);
+       _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:
@@ -604,7 +610,7 @@ END:
        return ret;
 }
 
-int bm_get_cpu_usage_checker_params(int *timeout, double *initial_threshold, double *minimum_threshold, double *threshold_reduction, int *accept_count)
+int bm_get_cpu_usage_checker_params(int *timeout, double *initial_threshold, double *minimum_threshold, double *threshold_reduction)
 {
        ENTER;
 
@@ -617,7 +623,6 @@ int bm_get_cpu_usage_checker_params(int *timeout, double *initial_threshold, dou
        BM_CHECK_INPUT_PARAM(initial_threshold);
        BM_CHECK_INPUT_PARAM(minimum_threshold);
        BM_CHECK_INPUT_PARAM(threshold_reduction);
-       BM_CHECK_INPUT_PARAM(accept_count);
 
        contents = bm_read_content_from_file(BATTERY_MONITOR_CONFIG_FILE_PATH);
 
@@ -652,12 +657,7 @@ int bm_get_cpu_usage_checker_params(int *timeout, double *initial_threshold, dou
                goto END;
        *threshold_reduction = json_object_get_double(jdouble);
 
-       json_object_object_get_ex(jvalue, "accept_count", &jint);
-       if (!jint)
-               goto END;
-       *accept_count = json_object_get_int(jint);
-
-       _DBG("data collection params - timeout[%d], initial_threshold[%lf], minimum_threshold[%lf], threshold_reduction[%lf], accept_count[%d]", *timeout, *initial_threshold, *minimum_threshold, *threshold_reduction, *accept_count);
+       _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:
index 74e5baf..4763732 100644 (file)
@@ -2029,6 +2029,7 @@ enum cpu_time {
 };
 
 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];
 
@@ -2078,20 +2079,17 @@ int bm_get_up_time(double *up_time)
 
 gboolean is_cpu_idle(void)
 {
-       ENTER;
-
        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 accept_count = 5;
        static int num_cpu_core = 1;
 
        static int remaining_timeout = 60000;
        static double usage_ratio_threshold = 0.60;
 
-       int try, idx;
+       int idx;
        unsigned long long cpu_time_cur[CT_NUM];
        unsigned long long cpu_time_diff[CT_NUM];
        double up_time_cur;
@@ -2099,7 +2097,7 @@ gboolean is_cpu_idle(void)
        double usage_ratio;
 
        if (!is_parameter_loaded) {
-               if (bm_get_cpu_usage_checker_params(&timeout, &initial_threshold, &minimum_threshold, &threshold_reduction, &accept_count) != BATTERY_MONITOR_ERROR_NONE) {
+               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;
                }
@@ -2119,115 +2117,107 @@ gboolean is_cpu_idle(void)
                goto satisfy;
        }
 
-       for (try = 1; try <= accept_count; try++) {
-               usleep(data_collection_try_period * 1000);
-
-               // 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 CPU time
+       if (bm_get_cpu_time(cpu_time_cur) != BATTERY_MONITOR_ERROR_NONE) {
+               _ERR("error getting CPU time");
+               goto unsatisfy;
+       }
 
-               // Get uptime
-               if (bm_get_up_time(&up_time_cur) != BATTERY_MONITOR_ERROR_NONE) {
-                       _ERR("error getting uptime");
+       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];
+       }
 
-               if (up_time_last >= up_time_cur) {
-                       _ERR("error invalid uptime");
-                       goto unsatisfy;
-               }
+       for (idx = 0; idx < CT_NUM; idx++)
+               cpu_time_last[idx] = cpu_time_cur[idx];
 
-               up_time_diff = up_time_cur - up_time_last;
-               up_time_last = up_time_cur;
+       // Get uptime
+       if (bm_get_up_time(&up_time_cur) != BATTERY_MONITOR_ERROR_NONE) {
+               _ERR("error getting uptime");
+               goto unsatisfy;
+       }
 
-               // 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 (up_time_last >= up_time_cur) {
+               _ERR("error invalid uptime");
+               goto unsatisfy;
+       }
 
-               if (usage_ratio > usage_ratio_threshold) {
-                       _WARN("CPU usage = %.2lf%% (BUSY, criteria[%.2lf%%])", usage_ratio * 100, usage_ratio_threshold * 100);
-                       goto unsatisfy;
-               }
+       up_time_diff = up_time_cur - up_time_last;
+       up_time_last = up_time_cur;
 
-               _DBG("CPU usage = %.2lf%% (IDLE, count[%d/%d])", usage_ratio * 100, try, accept_count);
+       // 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;
 
-       EXIT;
        return TRUE;
 
 unsatisfy:
-       remaining_timeout -= (try * data_collection_try_period);
+       remaining_timeout -= data_collection_try_period;
        if (usage_ratio_threshold > minimum_threshold)
                usage_ratio_threshold -= threshold_reduction;
 
-       EXIT;
        return FALSE;
 }
 
 gboolean bm_try_request_feature_data(gpointer data)
 {
-       ENTER;
-
        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()) {
-               EXIT;
+       if (!is_cpu_idle())
                return G_SOURCE_CONTINUE;
-       }
 
-       if (status == GET_DATA) {
-               // Request data to plugin
+       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");
-                       EXIT;
                        return G_SOURCE_CONTINUE;
                }
 
                status = CALC_AND_STORE;
-
-               if (!is_cpu_idle()) {
-                       EXIT;
+               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;
                }
-       }
 
-       // 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");
-               EXIT;
+               status = GET_DATA;
+               return G_SOURCE_REMOVE;
+       default:
+               _ERR("error invalid status");
+               status = GET_DATA;
                return G_SOURCE_CONTINUE;
        }
-
-       status = GET_DATA;
-
-       EXIT;
-       return G_SOURCE_REMOVE;
 }
 
 gboolean bm_request_feature_data(gpointer data)
@@ -2284,7 +2274,7 @@ 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, &delete_db_period);
+       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;