Change linear buffer to circular 57/243757/2 accepted/tizen/5.5/unified/20200914.234012 submit/tizen_5.5/20200911.133345
authorDewal Agarwal <d1.agarwal@samsung.com>
Wed, 9 Sep 2020 15:40:20 +0000 (21:10 +0530)
committerAbhishek Vijay <abhishek.v@samsung.com>
Fri, 11 Sep 2020 07:26:25 +0000 (12:56 +0530)
Change-Id: Ia2d38301a3f7a566df1a8c300405ff6d762681c5
Signed-off-by: Dewal Agarwal <d1.agarwal@samsung.com>
src/battery_dump/bd_history_item.c

index 8fe92e8..8d79620 100644 (file)
@@ -37,7 +37,7 @@
 #endif
 
 dump_data_s history_data[HISTORY_SIZE_MAX];
-int h_count = 0; int h_flag = false;
+int h_flag = false;
 char state1_map[32][4] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Wm",
        "", "Sd", "BP", "S", "Psc", "a", "s", "", "Pr", "Wr", "Ws", "Wl", "g", "w", "r"};
 char state2_map[32][5] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
@@ -48,6 +48,85 @@ char usage_map[32][4] = {"Bu", "Wu", "", "", "", "", "", "", "", "", "", "", "",
        "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
 GHashTable *app_map = NULL;
 static long int lst_charge = -1;
+static int front = -1, end = -1, is_full = 0;
+
+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");
+               is_full = 1;
+               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, int pc, history_tag_s *pool_info)
 {
@@ -55,6 +134,7 @@ static int bd_print_pool_entry(int fd, int idx, int pc, history_tag_s *pool_info
 
        BM_CHECK_INPUT_PARAM(pool_info);
        _DBG("Value from history data %d", idx);
+       _DBG("Value from history data %d", pool_info->sp_idx);
 
        GString *dump_p = g_string_sized_new(30);
        BM_CHECK_MEM_ALLOC(dump_p, {});
@@ -63,7 +143,12 @@ static int bd_print_pool_entry(int fd, int idx, int pc, history_tag_s *pool_info
        g_string_append_c(dump_p, ',');
        g_string_append(dump_p, DUMP_DATA_TAG_1);
        g_string_append_c(dump_p, ',');
-       g_string_append_printf(dump_p, "%d,", pc);
+
+       if (is_full)
+               g_string_append_printf(dump_p, "%d,", pool_info->sp_idx);
+       else
+               g_string_append_printf(dump_p, "%d,", pc);
+
        g_string_append_printf(dump_p, "%d,", pool_info->uid);
        g_string_append(dump_p, "\"");
 
@@ -108,8 +193,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; int pool_count = 0;
-       for (int i = 0; i < log_count; i++) {
+       int pool_count = 0, 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;
@@ -700,13 +790,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;
@@ -829,7 +913,7 @@ 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;
@@ -844,11 +928,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;
        h_flag = false; //Lock
 
@@ -869,8 +948,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");
@@ -1275,29 +1360,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;
@@ -1305,7 +1367,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);
@@ -1323,7 +1385,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; is_full = 0;
+       h_flag = true;
+
        EXIT;
        return ret;
 }
@@ -1333,16 +1397,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)
@@ -1362,60 +1428,36 @@ 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);
 
-       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;
-               }
-*/
-               int ret = bd_free_history_data_memory();
-               if (ret != BATTERY_MONITOR_ERROR_NONE) {
-                       _WARN("Error while memory free %x", ret);
-                       return ret;
-               }
-               history_item_s ts;
-               ret = bd_print_history_item_reset(&ts);
-               if (ret == BATTERY_MONITOR_ERROR_NONE) {
-                       ts.cmd_s = CM_CRR_TIME;
-                       ret = bd_store_history_item(&ts);
-                       if (ret != BATTERY_MONITOR_ERROR_NONE)
-                               _WARN("Init Node addition failed");
-               }
-       }
 
        EXIT;
        return BATTERY_MONITOR_ERROR_NONE;
@@ -1480,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; is_full = 0;
        lst_charge = -1;
 #ifdef DUMP_DUMMY
        bd_dummy_test_fn();