From 3167c52fa00738e3e89ee2a59407a2b0a92f8bca Mon Sep 17 00:00:00 2001 From: YoungHun Kim Date: Mon, 20 Jul 2020 11:48:06 +0900 Subject: [PATCH] Use clock_gettime() with CLOCK_MONOTONIC which is not affected by changing system time - update muse_core_get_cur_time() and dlog Change-Id: I3d194faf73d509d9f438f2cb328a61ba1cad821f --- core/include/muse_core_internal.h | 3 ++- core/src/muse_core.c | 21 +++++++++++++-------- packaging/mused.spec | 2 +- server/include/muse_server_private.h | 2 +- server/src/muse_server_log.c | 4 ++-- server/src/muse_server_private.c | 31 ++++++++++++++++--------------- 6 files changed, 35 insertions(+), 28 deletions(-) diff --git a/core/include/muse_core_internal.h b/core/include/muse_core_internal.h index 6671840..88b04d4 100644 --- a/core/include/muse_core_internal.h +++ b/core/include/muse_core_internal.h @@ -47,6 +47,7 @@ extern "C" { #include #include #include +#include #define CONFFILE SYSCONFDIR"/multimedia/mused.conf" #define MUSE_HOST "muse:hosts" @@ -192,7 +193,7 @@ void muse_core_setenv(const char *name, const char *value, int replace); bool muse_core_msg_recv_len(int fd, char *buf, int msg_len); void muse_core_update_fd_state(int fd); -void muse_core_get_cur_time(char *time_buf); +void muse_core_get_cur_time(struct timespec *time, char *time_buf); #ifdef __cplusplus } diff --git a/core/src/muse_core.c b/core/src/muse_core.c index 09f2e6a..986db19 100644 --- a/core/src/muse_core.c +++ b/core/src/muse_core.c @@ -24,7 +24,9 @@ #include #undef LOG_TAG -#define LOG_TAG "MUSED_CORE" +#define LOG_TAG "MUSED_CORE" + +#define STR_TIME_FORMAT "%m-%d %H:%M:%S" #define MUSE_WATCHDOG_CHECK_PERIOD 10 #define MUSE_WATCHDOG_CHECK_COUNT 3 @@ -708,7 +710,7 @@ void muse_core_update_fd_state(int fd) if (!fd_state_table) fd_state_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free); - muse_core_get_cur_time(atime); + muse_core_get_cur_time(NULL, atime); g_hash_table_insert(fd_state_table, GINT_TO_POINTER(fd), g_strdup(atime)); g_mutex_unlock(&fd_state_lock); @@ -745,18 +747,21 @@ void muse_core_destroy_fd_table(void) g_mutex_unlock(&fd_state_lock); } -void muse_core_get_cur_time(char *time_buf) +void muse_core_get_cur_time(struct timespec *time, char *time_buf) { char cur_time[MUSE_MSG_LEN]; struct tm newtime; - struct timeval tv; + struct timespec tv; - muse_return_if_fail(time_buf); + muse_return_if_fail(clock_gettime(CLOCK_MONOTONIC, &tv) == 0); - gettimeofday(&tv, NULL); - strftime(cur_time, MUSE_MSG_LEN, "%m-%d %H:%M:%S", localtime_r(&(tv.tv_sec), &newtime)); + if (time) + *time = tv; - snprintf(time_buf, MUSE_MSG_TIME_LEN, "%s.%03ld", cur_time, tv.tv_usec / 1000); + if (time_buf) { + strftime(cur_time, MUSE_MSG_LEN, STR_TIME_FORMAT, localtime_r(&(tv.tv_sec), &newtime)); + snprintf(time_buf, MUSE_MSG_TIME_LEN, "%s.%03ld", cur_time, tv.tv_nsec); + } } void muse_core_log_process_thread_info(int pid) diff --git a/packaging/mused.spec b/packaging/mused.spec index 7395439..234793b 100644 --- a/packaging/mused.spec +++ b/packaging/mused.spec @@ -1,6 +1,6 @@ Name: mused Summary: A multimedia daemon -Version: 0.3.121 +Version: 0.3.122 Release: 0 Group: System/Libraries License: Apache-2.0 diff --git a/server/include/muse_server_private.h b/server/include/muse_server_private.h index 7d2eb09..6add38f 100644 --- a/server/include/muse_server_private.h +++ b/server/include/muse_server_private.h @@ -94,7 +94,7 @@ typedef struct _muse_server { ms_watchdog_t *watchdog; ms_state_e state; GMutex state_lock; - struct timeval tv_s; + struct timespec tv; char instance_pid_info[MUSE_MSG_LEN_MAX]; } muse_server_t; diff --git a/server/src/muse_server_log.c b/server/src/muse_server_log.c index 29fd9be..fbec3ab 100644 --- a/server/src/muse_server_log.c +++ b/server/src/muse_server_log.c @@ -87,7 +87,7 @@ static void _ms_log_pid_time(ms_log_t *log, int pid) muse_return_if_fail(muse_core_fd_is_valid(log->fd)); muse_return_if_fail(_ms_log_pid_is_valid(pid)); - muse_core_get_cur_time(time_buf); + muse_core_get_cur_time(NULL, time_buf); snprintf(log_buf, MUSE_MSG_LEN_MAX, "[PID] %d %s", pid, time_buf); ms_log_write(log_buf); @@ -285,7 +285,7 @@ static void _ms_log_cache_latest_msg(ms_log_t *log, int pid, char *msg) g_mutex_lock(&log->lock); - muse_core_get_cur_time(time_buf); + muse_core_get_cur_time(NULL, time_buf); snprintf(log->latest_msgs[log->cur_idx], MUSE_MSG_LEN_MAX, "[P%5d] %s %s", pid, time_buf, msg); log->cur_idx = (log->cur_idx + 1) % MUSE_LOG_MSG_NUM; diff --git a/server/src/muse_server_private.c b/server/src/muse_server_private.c index b129d19..c83859b 100644 --- a/server/src/muse_server_private.c +++ b/server/src/muse_server_private.c @@ -376,12 +376,9 @@ static gboolean _ms_connection_handler(GIOChannel *source, GIOCondition conditio ms_ipc_create_data_dispatch_worker(m); } - if (gettimeofday(&muse_server->tv_s, NULL) != 0) { - LOGE("gettimeofday() failed"); - ms_terminate(SIGABRT); - } + muse_core_get_cur_time(&muse_server->tv, NULL); - LOGI("connected time %lu", muse_server->tv_s.tv_sec); + LOGW("connected time %lu", muse_server->tv.tv_sec); _ms_unlock_state(); @@ -410,9 +407,9 @@ static void _ms_check_idle_state(void) { ms_connection_t *connection = NULL; ms_config_t *conf = NULL; - struct timeval tv_c, tv_r; int instance_number, period; uint32_t elapsed_time; + struct timespec tv; static int period_idx = 1; muse_return_if_fail(muse_server); @@ -426,14 +423,12 @@ static void _ms_check_idle_state(void) period = ms_config_get_log_period(); - if ((int)muse_server->tv_s.tv_sec == 0) + if (muse_server->tv.tv_sec == 0) return; - muse_return_if_fail(gettimeofday(&tv_c, NULL) == 0); - - timersub(&tv_c, &muse_server->tv_s, &tv_r); + muse_core_get_cur_time(&tv, NULL); - elapsed_time = tv_r.tv_sec; + elapsed_time = tv.tv_sec - muse_server->tv.tv_sec; ms_connection_lock(connection); @@ -441,7 +436,7 @@ static void _ms_check_idle_state(void) if (elapsed_time >= (uint32_t)(period * period_idx)) { LOGW("[#%d] %u sec [period %d] [last connected time %lu] total number of modules = %d ( %s)", period_idx, - elapsed_time, period, muse_server->tv_s.tv_sec, instance_number, muse_server->instance_pid_info); + elapsed_time, period, muse_server->tv.tv_sec, instance_number, muse_server->instance_pid_info); period_idx++; } @@ -580,11 +575,17 @@ static void _ms_diag_deinit(void) static gboolean _ms_idle_cb(gpointer user_data) { + struct timespec tv; + if (!ms_create_ready_file()) LOGE("%s file creation is failed", MUSE_SERVER_READY); _ms_diag_init(); + muse_core_get_cur_time(&tv, NULL); + + LOGW("muse server is completed to ready %lu", tv.tv_sec); + return G_SOURCE_REMOVE; } @@ -774,7 +775,7 @@ pid_t ms_daemonize(int *notify_fd) void ms_daemonize_complete(int notify_fd) { - LOGW("Enter"); + LOGD("Enter"); muse_return_if_fail(muse_core_fd_is_valid(notify_fd)); @@ -783,10 +784,10 @@ void ms_daemonize_complete(int notify_fd) #endif write(notify_fd, MSG_DONE, strlen(MSG_DONE) + 1); - LOGI("[%d] Notify parent process that child initialization is done", notify_fd); + LOGW("[%d] Notify parent process that child initialization is done", notify_fd); close(notify_fd); - LOGW("Leave"); + LOGD("Leave"); } void ms_gst_init(char **cmd) -- 2.7.4