Change linear buffer to circular 09/244509/1 submit/tizen/20200925.104303
authorDewal Agarwal <d1.agarwal@samsung.com>
Mon, 21 Sep 2020 06:24:09 +0000 (11:54 +0530)
committerDewal Agarwal <d1.agarwal@samsung.com>
Mon, 21 Sep 2020 06:24:09 +0000 (11:54 +0530)
Change-Id: I242d847ed34631506025b900203974247879a28c
Signed-off-by: Dewal Agarwal <d1.agarwal@samsung.com>
src/battery_dump/bd_history_item.c

index 910a3ab2fe078d8d8ff1faa76549714bbee2ef8b..f3c7114b59b2cf36c2c90108b2686ede85d8cee0 100644 (file)
@@ -37,7 +37,7 @@
 #endif
 
 static dump_data_s history_data[HISTORY_SIZE_MAX];
-static int h_count = 0, h_flag = false;
+static int front = -1, end = -1, h_flag = false;
 
 /*Bit and string map*/
 static char state1_map[32][4] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Wm",
@@ -55,6 +55,83 @@ static long int rst_time = -1;
 
 static long long min_idle_check_time;
 
+static int bd_dump_data_item_reset(dump_data_s* old_state)
+{
+       ENTER;
+       BM_CHECK_INPUT_PARAM(old_state);
+
+       old_state->time_s = -1;
+       old_state->time_current = -1;
+       old_state->dump_1 = 0;
+       old_state->dump_2 = 0;
+       old_state->state_1 = 0;
+       old_state->state_2 = 0;
+       old_state->battery_chargeUAh = 0;
+       old_state->usage_type = 0;
+       old_state->usage = 0;
+       old_state->event_code = ET_NONE;
+       old_state->event_tag = NULL;
+       old_state->wakelock_tag = NULL;
+       old_state->wakereason_tag = NULL;
+
+       EXIT;
+       return BATTERY_MONITOR_ERROR_NONE;
+}
+
+static void bd_clean_circ_buffer_front(int front_indx)
+{
+       ENTER;
+
+       if (history_data[front_indx].wakelock_tag != NULL) {
+               g_string_free(history_data[front_indx].wakelock_tag->string_info, TRUE);
+               free(history_data[front_indx].wakelock_tag);
+       }
+       if (history_data[front_indx].wakereason_tag != NULL) {
+               g_string_free(history_data[front_indx].wakereason_tag->string_info, TRUE);
+               free(history_data[front_indx].wakereason_tag);
+       }
+       if (history_data[front_indx].event_tag != NULL) {
+               g_string_free(history_data[front_indx].event_tag->string_info, TRUE);
+               free(history_data[front_indx].event_tag);
+       }
+
+       bd_dump_data_item_reset(&history_data[front_indx]);
+
+       EXIT;
+       return;
+}
+
+static int bd_get_index()
+{
+       ENTER;
+
+       if ((front == 0 && end == HISTORY_SIZE_MAX - 1) || (front == end + 1)) {
+               _DBG("buffer is full");
+               bd_clean_circ_buffer_front(front);
+               front = (front + 1) % HISTORY_SIZE_MAX;
+               /* Prepare TIME Node */
+               history_data[front].dump_1 = 0;
+               history_data[front].dump_1 |= CM_CRR_TIME;
+               history_data[front].time_current = history_data[front].time_s;
+               history_data[front].state_1 = 0;
+               history_data[front].state_2 = 0;
+               history_data[front].event_code = ET_NONE;
+       }
+
+       if (front == -1) {
+               _DBG("buffer is empty");
+               front = end = 0;
+       } else {
+               if (end == HISTORY_SIZE_MAX - 1)
+                       end = 0;
+               else
+                       end = end + 1;
+       }
+
+       EXIT;
+       return end;
+}
+
 static int bd_print_pool_entry(int fd, int idx, history_tag_s *pool_info)
 {
        ENTER;
@@ -104,8 +181,13 @@ static int bd_get_pool_entry_from_dump(int fd)
        g_string_free(dump_p, TRUE);
 
        int ret = BATTERY_MONITOR_ERROR_NONE;
-       int log_count = h_count;
-       for (int i = 0; i < log_count; i++) {
+       int index_s = front, index_e = end, i = 0;
+
+       if (index_s > index_e)
+               index_e = HISTORY_SIZE_MAX + index_e;
+
+       for (int k = index_s; k < index_e; k++) {
+               i = k % HISTORY_SIZE_MAX;
                history_tag_s *pool_info = NULL;
                if (history_data[i].event_tag != NULL && (history_data[i].event_code & ET_FLAG_START)) {
                        pool_info = history_data[i].event_tag;
@@ -643,13 +725,7 @@ static int bd_get_history_detail_from_dump(int index, history_item_s* dump_node)
        ENTER;
        BM_CHECK_INPUT_PARAM(dump_node);
 
-       if (h_count >= HISTORY_SIZE_MAX || index > h_count) {
-               _ERR("Some Error occured with global index");
-               return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
-       }
-
        _DBG("Value of index %d", index);
-       _DBG("Value of h_count %d", h_count);
 
        long long dvar1 = 0, dvar2 = 0;
        dvar1 = history_data[index].dump_1;
@@ -746,7 +822,7 @@ static int bd_get_base_time_from_dump(long long *base_time)
 {
        ENTER;
 
-       *base_time = history_data[0].time_s;
+       *base_time = history_data[front].time_s;
 
        EXIT;
        return BATTERY_MONITOR_ERROR_NONE;
@@ -761,11 +837,6 @@ int bd_print_history_item_main(int fd, int num_h_items, long long base_time, boo
                return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
        }
 
-       if (h_count >= HISTORY_SIZE_MAX) {
-               _ERR("Some Error occured with global index");
-               return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
-       }
-
        int ret = BATTERY_MONITOR_ERROR_NONE;
 
        history_item_s new_state;
@@ -785,8 +856,14 @@ int bd_print_history_item_main(int fd, int num_h_items, long long base_time, boo
        if (ret != BATTERY_MONITOR_ERROR_NONE)
                _ERR("Base time error");
 
-       int log_count = h_count; GString *dump_p = NULL;
-       for (int i = 0; i < log_count; i++) {
+       GString *dump_p = NULL;
+
+       int index_s = front, index_e = end, i = 0;
+       if (index_s > index_e)
+               index_e = HISTORY_SIZE_MAX + index_e;
+
+       for (int k = index_s; k < index_e; k++) {
+               i = k % HISTORY_SIZE_MAX;
                ret = bd_get_history_detail_from_dump(i, &new_state);
                if (ret != BATTERY_MONITOR_ERROR_NONE) {
                        _ERR("Decode Failed");
@@ -1178,29 +1255,6 @@ int bd_print_history_item_main(int fd, int num_h_items, long long base_time, boo
        return BATTERY_MONITOR_ERROR_NONE;
 }
 
-static int bd_dump_data_item_reset(dump_data_s* old_state)
-{
-       ENTER;
-       BM_CHECK_INPUT_PARAM(old_state);
-
-       old_state->time_s = -1;
-       old_state->time_current = -1;
-       old_state->dump_1 = 0;
-       old_state->dump_2 = 0;
-       old_state->state_1 = 0;
-       old_state->state_2 = 0;
-       old_state->battery_chargeUAh = 0;
-       old_state->usage_type = 0;
-       old_state->usage = 0;
-       old_state->event_code = ET_NONE;
-       old_state->event_tag = NULL;
-       old_state->wakelock_tag = NULL;
-       old_state->wakereason_tag = NULL;
-
-       EXIT;
-       return BATTERY_MONITOR_ERROR_NONE;
-}
-
 static int bd_free_history_data_memory()
 {
        ENTER;
@@ -1208,7 +1262,7 @@ static int bd_free_history_data_memory()
        int ret = BATTERY_MONITOR_ERROR_NONE;
        h_flag = false; //Lock
        _DBG("Free History Data Memory");
-       for (int i = 0; i < h_count; i++) {
+       for (int i = 0; i < HISTORY_SIZE_MAX; i++) {
                if (history_data[i].wakelock_tag != NULL) {
                        g_string_free(history_data[i].wakelock_tag->string_info, TRUE);
                        free(history_data[i].wakelock_tag);
@@ -1226,7 +1280,9 @@ static int bd_free_history_data_memory()
                        _ERR("Internal error %d while freeing at idx %d", ret , i);
        }
 
-       h_count = 0; h_flag = true;
+       front = -1; end = -1;
+       h_flag = true;
+
        EXIT;
        return ret;
 }
@@ -1236,16 +1292,18 @@ static int bd_set_history_from_listener(history_item_s* nrec)
        ENTER;
        BM_CHECK_INPUT_PARAM(nrec);
 
+       int ind = bd_get_index();
+       if (ind < 0 || ind > HISTORY_SIZE_MAX - 1)
+               return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
+
        long long dvar1 = 0, dvar2 = 0;
        struct timeval tv;
        gettimeofday(&tv, NULL);
 
-       time_t current_time = tv.tv_sec;
-
 #ifdef DUMP_DUMMY
-       history_data[h_count].time_s = nrec->time_s;
+       history_data[ind].time_s = nrec->time_s;
 #else
-       history_data[h_count].time_s = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
+       history_data[ind].time_s = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
 #endif
 
        if (nrec->battery_plugtype == BD_BPLT_AC || nrec->battery_plugtype == BD_BPLT_WL)
@@ -1267,53 +1325,38 @@ static int bd_set_history_from_listener(history_item_s* nrec)
        _DBG("Value of encoded value 1 %lld", dvar1);
        _DBG("Value of encoded value 2 %lld", dvar2);
 
-       history_data[h_count].dump_1 = dvar1;
-       history_data[h_count].dump_2 = dvar2;
-       history_data[h_count].battery_chargeUAh = nrec->battery_charge;
+       history_data[ind].dump_1 = dvar1;
+       history_data[ind].dump_2 = dvar2;
+       history_data[ind].battery_chargeUAh = nrec->battery_charge;
 
        if (nrec->wakelock_tag != NULL)
-               history_data[h_count].wakelock_tag = nrec->wakelock_tag;
+               history_data[ind].wakelock_tag = nrec->wakelock_tag;
        if (nrec->wakereason_tag != NULL)
-               history_data[h_count].wakereason_tag = nrec->wakereason_tag;
+               history_data[ind].wakereason_tag = nrec->wakereason_tag;
        if (nrec->event_code != ET_NONE) {
                if (nrec->event_tag == NULL) {
                        _ERR("ET is NULL but Event Code is not");
-                       BM_FREE(history_data[h_count].wakelock_tag);
-                       BM_FREE(history_data[h_count].wakereason_tag);
-                       bd_dump_data_item_reset(&history_data[h_count]);
+                       BM_FREE(history_data[ind].wakelock_tag);
+                       BM_FREE(history_data[ind].wakereason_tag);
+                       bd_dump_data_item_reset(&history_data[ind]);
                        return BATTERY_MONITOR_ERROR_NO_DATA;
                }
-               history_data[h_count].event_code = nrec->event_code;
-               history_data[h_count].event_tag = nrec->event_tag;
+               history_data[ind].event_code = nrec->event_code;
+               history_data[ind].event_tag = nrec->event_tag;
        }
        if (nrec->cmd_s == CM_CRR_TIME || nrec->cmd_s == CM_RST)
-               history_data[h_count].time_current = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
+               history_data[ind].time_current = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
        else if (nrec->cmd_s == CM_USAGE)
-               history_data[h_count].time_current = nrec->time_current;
+               history_data[ind].time_current = nrec->time_current;
 
-       history_data[h_count].state_1 = nrec->state_1;
-       history_data[h_count].state_2 = nrec->state_2;
+       history_data[ind].state_1 = nrec->state_1;
+       history_data[ind].state_2 = nrec->state_2;
 
-       history_data[h_count].usage_type = nrec->usage_type;
-       history_data[h_count].usage = nrec->usage;
+       history_data[ind].usage_type = nrec->usage_type;
+       history_data[ind].usage = nrec->usage;
 
        nrec->event_tag = NULL; nrec->wakereason_tag = NULL;
        nrec->wakelock_tag = NULL;
-       if (h_count < HISTORY_SIZE_MAX - 1)
-               h_count++;
-       else {
-               // FIXME modify 1st argument (fd)
-               int ret = bd_print_history_item_main(-1, 0, current_time, 1);
-               if (ret != BATTERY_MONITOR_ERROR_NONE) {
-                       h_count = 0;
-                       return ret;
-               }
-               ret = bd_free_history_data_memory();
-               if (ret != BATTERY_MONITOR_ERROR_NONE) {
-                       _WARN("Error while memory free %x", ret);
-                       return ret;
-               }
-       }
 
        EXIT;
        return BATTERY_MONITOR_ERROR_NONE;
@@ -1338,10 +1381,14 @@ static int bd_reset_history()
                if (ret != BATTERY_MONITOR_ERROR_NONE)
                        _WARN("Init After Reset failed");
        }
-       /* Reset History Stats */
+       /* Reset History Stats
+        * Reset time supercedes last charge time
+        * */
        lst_charge = rst_time;
 
-       /* Call Battery Listener Reset Function from here */
+       /* Call Battery Listener Reset Function from here
+        * Add listener reset call
+        * */
 
        EXIT;
        return ret;
@@ -1475,7 +1522,7 @@ int bd_initialize_battery_dump()
 
        int ret = BATTERY_MONITOR_ERROR_NONE;
 
-       h_count = 0; h_flag = true;
+       front = -1; end = -1; h_flag = true;
        lst_charge = -1;
 #ifdef DUMP_DUMMY
        bd_dummy_test_fn();