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;
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);
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:
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;
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);
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:
};
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];
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;
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;
}
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)
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;