Refactor dbus_heart_get_cpu_data() and heart_cpu_get_data() functions 66/180066/2
authorŁukasz Stelmach <l.stelmach@samsung.com>
Thu, 24 May 2018 12:20:51 +0000 (14:20 +0200)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Thu, 24 May 2018 13:50:09 +0000 (15:50 +0200)
Remove duplicated code from dbus_heart_get_cpu_data() leaving only DBus
interfacing code. Call heart_cpu_get_data() to retrieve data.

Change-Id: I2f19a164507e3dfedccd93b75524f1734c5850cb
Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
src/common/heart-common.h
src/heart/heart-cpu.c

index e6dc1ba05d9de9e78579814c224a1711c66632a4..8e9089c06d84e6406aab73accf07a7ccf0f0a06e 100644 (file)
@@ -94,7 +94,7 @@ int heart_battery_get_capacity_history(GArray *arrays, enum heart_data_period pe
 
 /* cpu */
 int heart_cpu_get_table(int uid, GArray *arrays, enum heart_data_period period);
-struct heart_cpu_data *heart_cpu_get_data(int uid, char *appid, enum heart_data_period period);
+struct heart_cpu_data *heart_cpu_get_data(int uid, char *appid, enum heart_data_period period, struct heart_cpu_data *data);
 int heart_cpu_get_appusage_list(int uid, GHashTable *lists, int top);
 
 /* memory */
index 51da4b5b96eba3713a3d05d989d6bab6d8ad0f88..36e3c14b39473a9f4a687196164582b20bed6f47 100644 (file)
@@ -866,11 +866,10 @@ unlock_exit:
        }
 }
 
-struct heart_cpu_data *heart_cpu_get_data(int uid, char *appid, enum heart_data_period period)
+struct heart_cpu_data *heart_cpu_get_data(int uid, char *appid, enum heart_data_period period, struct heart_cpu_data *data)
 {
        int index, i, ret;
        struct heart_cpu_table *table;
-       struct heart_cpu_data *data;
        GHashTable *hashtable;
 
        if (!appid) {
@@ -915,22 +914,20 @@ struct heart_cpu_data *heart_cpu_get_data(int uid, char *appid, enum heart_data_
                _E("pthread_mutex_lock() failed, %d", ret);
                return NULL;
        }
+
        table = g_hash_table_lookup(hashtable, (gconstpointer)appid);
-       if (!table)
-               goto unlock_exit;
-       data = malloc(sizeof(struct heart_cpu_data));
-       if (!data) {
-               _E("malloc failed");
+       if (!table) {
+               data = NULL;
                goto unlock_exit;
        }
        if (snprintf(data->appid, MAX_APPID_LENGTH, "%s", table->appid) < 0) {
                _E("snprintf failed");
-               free(data);
+               data = NULL;
                goto unlock_exit;
        }
        if (snprintf(data->pkgid, MAX_PKGNAME_LENGTH, "%s", table->pkgid) < 0) {
                _E("snprintf failed");
-               free(data);
+               data = NULL;
                goto unlock_exit;
        }
        if (period == DATA_LATEST) {
@@ -941,7 +938,7 @@ struct heart_cpu_data *heart_cpu_get_data(int uid, char *appid, enum heart_data_
                data->stime = table->stime;
                i = table->cpu_info->len;
                if (i == 0) {
-                       free(data);
+                       data = NULL;
                        goto unlock_exit;
                }
                if (i < index)
@@ -956,20 +953,13 @@ struct heart_cpu_data *heart_cpu_get_data(int uid, char *appid, enum heart_data_
                        data->stime += cpu_info->stime;
                }
        }
-       ret = pthread_mutex_unlock(&heart_cpu_mutex);
-       if (ret) {
-               _E("pthread_mutex_unlock() failed, %d", ret);
-               free(data);
-               return NULL;
-       }
-       return data;
 unlock_exit:
        ret = pthread_mutex_unlock(&heart_cpu_mutex);
        if (ret) {
                _E("pthread_mutex_unlock() failed, %d", ret);
-               return NULL;
+               data = NULL;
        }
-       return NULL;
+       return data;
 }
 
 static int compare_usage(const struct heart_app_usage *lau_a,
@@ -1198,14 +1188,11 @@ static int heart_cpu_update_appid(void *data)
 
 static void dbus_heart_get_cpu_data(GDBusMethodInvocation *invocation, GVariant *params)
 {
-       int period = -1, index, i, ret;
+       int period = -1;
        char *appid = NULL;
-       struct heart_cpu_table *table;
-
-       time_t utime = 0, stime = 0;
+       struct heart_cpu_data *data;
 
        int uid = -1;
-       GHashTable *hashtable;
 
        g_variant_get(params, "(i&si)", &uid, &appid, &period);
        if (uid < 0 || !appid || period < 0) {
@@ -1213,78 +1200,23 @@ static void dbus_heart_get_cpu_data(GDBusMethodInvocation *invocation, GVariant
                D_BUS_REPLY_NULL(invocation);
                return;
        }
-       switch (period) {
-       case DATA_LATEST:
-               index = 0;
-               break;
-       case DATA_3HOUR:
-               index = 3;
-               break;
-       case DATA_6HOUR:
-               index = 6;
-               break;
-       case DATA_12HOUR:
-               index = 12;
-               break;
-       case DATA_1DAY:
-               index = 24;
-               break;
-       default:
-               _E("Wrong message arguments! %d", period);
-               D_BUS_REPLY_NULL(invocation);
-               return;
-       }
-
-       hashtable = heart_cpu_get_user_app_list(uid);
-       if (!hashtable) {
-               _E("Fail to get app list");
-               D_BUS_REPLY_NULL(invocation);
-               return;
-       }
 
-       if (!g_hash_table_size(hashtable)) {
-               _E("hash table is empty");
+       data = malloc(sizeof(struct heart_cpu_data));
+       if (!data) {
+               _E("malloc failed");
                D_BUS_REPLY_NULL(invocation);
                return;
        }
 
-       ret = pthread_mutex_lock(&heart_cpu_mutex);
-       if (ret) {
-               _E("pthread_mutex_lock() failed, %d", ret);
+       if (heart_cpu_get_data(uid, appid, period, data) == NULL) {
+               free(data);
                D_BUS_REPLY_NULL(invocation);
                return;
        }
 
-       table = g_hash_table_lookup(hashtable, (gconstpointer)appid);
-       if (!table) {
-               D_BUS_REPLY_NULL(invocation);
-               goto unlock_exit;
-       }
-       if (period == DATA_LATEST) {
-               utime = table->total_utime;
-               stime = table->total_stime;
-       } else {
-               utime = table->utime;
-               stime = table->stime;
-               i =  table->cpu_info->len;
-               if (i < index)
-                       index = i;
-               for (i = 0; i < index; i++) {
-                       struct heart_cpu_info *ci;
-                       ci = g_array_index(table->cpu_info, struct heart_cpu_info *, i);
-                       if (!ci)
-                               break;
-                       utime += ci->utime;
-                       stime += ci->stime;
-               }
-       }
        g_dbus_method_invocation_return_value(invocation, g_variant_new("(ii)",
-                               utime, stime));
-
-unlock_exit:
-       ret = pthread_mutex_unlock(&heart_cpu_mutex);
-       if (ret)
-               _E("pthread_mutex_unlock() failed, %d", ret);
+                               data->utime, data->stime));
+       free(data);
 }
 
 static void dbus_heart_get_cpu_data_list(GDBusMethodInvocation *invocation, GVariant *params)