cpu : fix time usage 27/227327/1
authorKichan Kwon <k_c.kwon@samsung.com>
Wed, 11 Mar 2020 07:56:57 +0000 (16:56 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Wed, 11 Mar 2020 07:56:57 +0000 (16:56 +0900)
- Use time instead of gettimeofday
- Set the endtime for reading DB

Change-Id: I15f2a744db1bf8438034221161a9bd552ebde1a8
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
plugin/cpu/src/bm_cpu_plugin.c

index b3428c7..60bf00b 100644 (file)
@@ -43,7 +43,7 @@ struct cpu_time {
        uint stime;
 };
 
-static struct timeval last_requested_time;
+static time_t last_requested_time;
 static struct cpu_time last_cpu_time;
 
 static GDBusConnection *dbus_connection;
@@ -128,8 +128,9 @@ int init()
        int ret;
        GError *g_err = NULL;
 
-       if (gettimeofday(&last_requested_time, NULL) != 0) {
-               _E("gettimeofday failed : %m");
+       last_requested_time = time(NULL);
+       if (last_requested_time == -1) {
+               _E("time failed : %m");
                goto failed;
        }
 
@@ -171,7 +172,7 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
        bm_cpu_st *usage_head = NULL;
        bm_cpu_st *usage = NULL;
 
-       struct timeval tv;
+       time_t current_time;
        struct cpu_time cur_cpu_time;
 
        GError *g_err = NULL;
@@ -205,9 +206,9 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
        }
 
        /* Get current time */
-       ret = gettimeofday(&tv, NULL);
-       if (ret != 0) {
-               _E("gettimeofday failed : %m");
+       current_time = time(NULL);
+       if (current_time == -1) {
+               _E("time failed : %m");
 
                EXIT;
                return BM_PLUGIN_ERROR_OUT_OF_MEMORY;
@@ -300,8 +301,8 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
        _D("Total CPU usage : %u", JIFFY_TO_MS(usage->utime + usage->stime));
 
        /* Read data from HEART-CPU DB */
-       _I("Gather CPU usage : %ld ~ %ld", last_requested_time.tv_sec, tv.tv_sec);
-       snprintf(query, 128, "SELECT appid,data FROM cpu WHERE time >= %ld", last_requested_time.tv_sec);
+       _I("Gather CPU usage : %ld ~ %ld", last_requested_time, current_time);
+       snprintf(query, 128, "SELECT appid,data FROM cpu WHERE (time >= %ld AND time < %ld)", last_requested_time, current_time);
        ret = sqlite3_prepare_v2(db, query, -1, &stmt, 0);
        while (sqlite3_step(stmt) == SQLITE_ROW) {
                appid = (const char *)sqlite3_column_text(stmt, 0);
@@ -425,8 +426,7 @@ int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
        }
 
        /* Update last requested time */
-       last_requested_time.tv_sec = tv.tv_sec;
-       last_requested_time.tv_usec = tv.tv_usec;
+       last_requested_time = current_time;
 
        /* Update last CPU time */
        last_cpu_time.utime = cur_cpu_time.utime;