Add CPU stats for Battery Dump
[platform/core/system/batterymonitor.git] / src / battery_dump / bd_history_item.c
index 6957e16..aef2104 100644 (file)
@@ -539,6 +539,49 @@ static int bd_get_and_print_gwst_stat_entry_from_dump(int fd, long int duration)
        return ret;
 }
 
+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;
+       rid_str = bm_get_resource_id_string(2);
+       resourceid_usage_s *res_use =
+               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");
+       }
+       usage = res_use->usage;
+       float usagemah = (float)usage/3600;
+       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");
+               BM_FREE(gcpu_stat);
+               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);});
+       g_string_append(dump_p, TIZEN_DUMP_VERSION);
+       g_string_append(dump_p, ",0,l,");
+       g_string_append(dump_p, "cpu,");
+       g_string_append_printf(dump_p, "%ld,", gcpu_stat->usr_time);
+       g_string_append_printf(dump_p, "%ld,", gcpu_stat->sys_time);
+       g_string_append_printf(dump_p, "%0.2f", usagemah);
+       g_string_append(dump_p, "\n");
+       bm_resourceid_usage_free(res_use);
+       _DBG("%s", dump_p->str);
+       if (write(fd, dump_p->str, dump_p->len) < 0)
+               _WARN("write error");
+       g_free(dump_p->str);
+
+       EXIT;
+       return ret;
+}
+
 static int bd_get_and_print_uid_info_entry_from_dump(int fd)
 {
        ENTER;
@@ -1122,6 +1165,8 @@ int bd_print_history_item_main(int fd, int num_h_items, long long base_time, boo
                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