}
}
-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) {
_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) {
data->stime = table->stime;
i = table->cpu_info->len;
if (i == 0) {
- free(data);
+ data = NULL;
goto unlock_exit;
}
if (i < index)
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,
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) {
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)