From 97d27b9803cf6223ebdd5fbbda86853236cd358c Mon Sep 17 00:00:00 2001 From: Kichan Kwon Date: Wed, 21 Dec 2016 17:10:31 +0900 Subject: [PATCH] heart : add dbus method about HEART-CPU sync - org.tizen.resourced.logging.SyncCpuData - It will update cache and write into the DB - Return value : 0(success), -1(fail), TID(running) Change-Id: Ic7fc4b8f3dc86a4b6ccd38ee1db7e2789b8e9589 Signed-off-by: Kichan Kwon --- src/heart/heart-cpu.c | 21 ++++++++++++++ src/heart/include/logging.h | 2 ++ src/heart/logging.c | 67 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/src/heart/heart-cpu.c b/src/heart/heart-cpu.c index f17dea2..f6a6aff 100644 --- a/src/heart/heart-cpu.c +++ b/src/heart/heart-cpu.c @@ -1408,6 +1408,26 @@ static DBusMessage *edbus_heart_update_cpu_data(E_DBus_Object *obj, DBusMessage return reply; } +static DBusMessage *edbus_heart_sync_cpu_data(E_DBus_Object *obj, DBusMessage *msg) +{ + int ret = 0; + DBusMessage *reply; + DBusMessageIter iter; + + heart_cpu_update_app_list(NULL); + + reply = dbus_message_new_method_return(msg); + + ret = logging_sync_and_reply(reply); + if (!ret) + return NULL; + ret = -ret; + + dbus_message_iter_init_append(reply, &iter); + dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret); + return reply; +} + static DBusMessage *edbus_heart_save_to_file(E_DBus_Object *obj, DBusMessage *msg) { int ret; @@ -1442,6 +1462,7 @@ static struct edbus_method edbus_methods[] = { { "GetCpuDataList", "ii", "a(sii)", edbus_heart_get_cpu_data_list }, { "ResetCpuData", NULL, "i", edbus_heart_reset_cpu_data }, { "UpdateCpuData", NULL, "i", edbus_heart_update_cpu_data }, + { "SyncCpuData", NULL, "i", edbus_heart_sync_cpu_data }, { "SaveCpuData", NULL, "i", edbus_heart_save_to_file }, }; diff --git a/src/heart/include/logging.h b/src/heart/include/logging.h index 2e269ad..f9862ac 100644 --- a/src/heart/include/logging.h +++ b/src/heart/include/logging.h @@ -29,6 +29,7 @@ #include #include #include "const.h" +#include "edbus-handler.h" #ifndef CLOCK_BOOTTIME #define CLOCK_BOOTTIME 7 @@ -130,6 +131,7 @@ int logging_read_foreach(char *name, char *appid, char *pkgid, time_t start_time, time_t end_time, logging_info_cb callback, void *user_data); void logging_update(int force); void logging_save_to_storage(int force); +int logging_sync_and_reply(DBusMessage *reply); int logging_leveldb_put(char *key, unsigned int key_len, char *value, unsigned int value_len); int logging_leveldb_putv(char *key, unsigned int key_len, const char *fmt, ...); int logging_leveldb_read(char *key, unsigned int key_len, char *value, unsigned int value_len); diff --git a/src/heart/logging.c b/src/heart/logging.c index 2652038..a83b4f2 100644 --- a/src/heart/logging.c +++ b/src/heart/logging.c @@ -121,6 +121,8 @@ struct logging_listerner { static const struct module_ops logging_modules_ops; +static pthread_t logging_sync_thread = 0; + static pthread_t logging_data_thread = 0; static pthread_mutex_t logging_data_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t logging_data_cond = PTHREAD_COND_INITIALIZER; @@ -1318,6 +1320,71 @@ void logging_save_to_storage(int force) } } +static void *logging_sync_thread_main(void *arg) +{ + int ret = 0; + DBusMessage *reply = (DBusMessage *)arg; + DBusMessageIter iter; + + ret = pthread_mutex_lock(&logging_data_mutex); + if (ret) { + _E("logging sync thread::pthread_mutex_lock() failed (%d)", ret); + ret = -1; + goto reply; + } + + logging_save_to_storage(true); + + ret = pthread_mutex_unlock(&logging_data_mutex); + if (ret) { + _E("logging sync thread::pthread_mutex_unlock() failed (%d)", ret); + ret = -1; + goto reply; + } + + ret = pthread_mutex_lock(&logging_update_mutex); + if (ret) { + _E("logging sync thread::pthread_mutex_lock() failed (%d)", ret); + ret = -1; + goto reply; + } + + logging_update(true); + + ret = pthread_mutex_unlock(&logging_update_mutex); + if (ret) { + _E("logging sync thread::pthread_mutex_unlock() failed (%d)", ret); + ret = -1; + } + +reply: + + logging_sync_thread = 0; + + dbus_message_iter_init_append(reply, &iter); + dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret); + + if (dbus_message_reply(reply) != RESOURCED_ERROR_NONE) + _E("Failed to reply sync request"); + + return NULL; +} + +int logging_sync_and_reply(DBusMessage *reply) +{ + if (logging_sync_thread) { + _I("logging sync thread %u is already running", (unsigned)logging_sync_thread); + return logging_sync_thread; + } + + if (!pthread_create(&logging_sync_thread, NULL, (void *)logging_sync_thread_main, reply)) + return 0; + + _E("Failed to create logging sync thread"); + logging_sync_thread = 0; + return -1; +} + static void *logging_data_thread_main(void *arg) { int ret = 0; -- 2.7.4