diff --git a/apteryxd.c b/apteryxd.c index f216b95..6687c1b 100644 --- a/apteryxd.c +++ b/apteryxd.c @@ -437,20 +437,6 @@ notify_watchers (GList *paths, GList *values, bool ack) g_list_free_full (common_watchers, (GDestroyNotify) cb_release); } -static uint64_t -calculate_timestamp (void) -{ - struct timespec tms; - uint64_t micros = 0; - if (clock_gettime(CLOCK_MONOTONIC_RAW, &tms)) { - return 0; - } - - micros = ((uint64_t)tms.tv_sec) * 1000000; - micros += tms.tv_nsec/1000; - return micros; -} - static char * get_refresher_path (const char *path, cb_info_t *refresher) { @@ -539,7 +525,7 @@ call_refreshers (const char *path, bool dry_run) return false; /* Get the time of the request */ - now = calculate_timestamp (); + now = get_time_us (); /* Call each refresher */ for (iter = refreshers; iter; iter = g_list_next (iter)) @@ -2411,7 +2397,7 @@ handle_timestamp (rpc_message msg) /* Lookup value */ if (call_refreshers (path, true) || config_tree_has_providers (path)) { - value = calculate_timestamp (); + value = get_time_us (); } else { diff --git a/database.c b/database.c index 67c33cb..3920a8f 100644 --- a/database.c +++ b/database.c @@ -45,21 +45,6 @@ struct hashtree_node *root = NULL; /* The database root */ pthread_rwlock_t db_lock = PTHREAD_RWLOCK_INITIALIZER; -static uint64_t -db_calculate_timestamp (void) -{ - struct timespec tms; - uint64_t micros = 0; - if (clock_gettime(CLOCK_MONOTONIC_RAW, &tms)) { - return 0; - } - - micros = ((uint64_t)tms.tv_sec) * 1000000; - micros += tms.tv_nsec/1000; - return micros; -} - - static uint64_t db_timestamp_no_lock (const char *path) { @@ -225,7 +210,7 @@ _db_update (struct database_node *parent_node, GNode *new_node, uint64_t ts) } /* Update times up this tree */ - uint64_t set_time = db_calculate_timestamp(); + uint64_t set_time = get_time_us(); for (struct database_node *ts_update = db_node; ts_update; ts_update = (struct database_node *)ts_update->hashtree_node.parent) { ts_update->timestamp = set_time; @@ -327,7 +312,7 @@ db_update_no_lock (GNode *new_data, uint64_t ts) bool db_add_no_lock (const char *path, const unsigned char *value, size_t length, uint64_t ts) { - uint64_t timestamp = db_calculate_timestamp(); + uint64_t timestamp = get_time_us(); if (ts != UINT64_MAX && ts < db_timestamp_no_lock (path)) return false; @@ -380,7 +365,7 @@ db_delete_no_lock (const char *path, uint64_t ts) struct hashtree_node *node = hashtree_path_to_node (root, path); if (node && node != root) { - uint64_t now = db_calculate_timestamp (); + uint64_t now = get_time_us (); struct hashtree_node *iter = node; struct hashtree_node *parent = hashtree_parent_get (node); while ((iter = hashtree_parent_get (iter)) != NULL) @@ -686,7 +671,7 @@ db_prune (const char *path) if (node) { - uint64_t now = db_calculate_timestamp (); + uint64_t now = get_time_us (); struct hashtree_node *iter = &node->hashtree_node; while ((iter = hashtree_parent_get (iter)) != NULL) { diff --git a/internal.h b/internal.h index ad49fa2..9512628 100644 --- a/internal.h +++ b/internal.h @@ -52,9 +52,15 @@ extern bool apteryx_debug; static inline uint64_t get_time_us (void) { - struct timeval tv; - gettimeofday (&tv, NULL); - return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec); + struct timespec tms; + uint64_t micros = 0; + if (clock_gettime (CLOCK_MONOTONIC_RAW, &tms)) { + return 0; + } + + micros = ((uint64_t)tms.tv_sec) * 1000000; + micros += tms.tv_nsec / 1000; + return micros; } /* Use the inode number of the namespace for mnt as our ns reference */ diff --git a/rpc_socket.c b/rpc_socket.c index 4c89227..88e6e91 100644 --- a/rpc_socket.c +++ b/rpc_socket.c @@ -140,14 +140,14 @@ rpc_socket_recv (rpc_socket sock, rpc_id id, void **data, size_t *len, uint64_t struct msg_s *m = NULL; struct timespec waitUntil; - struct timeval now; + struct timespec now; int ret = 0; if (waitUS) { - gettimeofday (&now, NULL); + clock_gettime (CLOCK_MONOTONIC, &now); waitUntil.tv_sec = now.tv_sec + (waitUS / (1000UL * 1000UL)); - waitUntil.tv_nsec = (now.tv_usec + (waitUS % (1000UL * 1000UL))) * 1000UL; + waitUntil.tv_nsec = now.tv_nsec + (waitUS % (1000UL * 1000UL)) * 1000UL; waitUntil.tv_sec += waitUntil.tv_nsec / (1000UL * 1000UL * 1000UL); waitUntil.tv_nsec %= (1000UL * 1000UL * 1000UL); } @@ -197,6 +197,8 @@ rpc_socket_recv (rpc_socket sock, rpc_id id, void **data, size_t *len, uint64_t rpc_socket rpc_socket_create (int fd, rpc_callback cb, rpc_server parent, int pid, uint64_t ns) { + pthread_condattr_t attr; + rpc_socket sock = g_malloc0 (sizeof(*sock)); sock->refcount = 1; sock->sock = fd; @@ -211,7 +213,9 @@ rpc_socket_create (int fd, rpc_callback cb, rpc_server parent, int pid, uint64_t pthread_mutex_init (&sock->in_lock, NULL); pthread_mutex_init (&sock->out_lock, NULL); pthread_mutex_init (&sock->lock, NULL); - pthread_cond_init (&sock->in_cond, NULL); + pthread_condattr_init (&attr); + pthread_condattr_setclock (&attr, CLOCK_MONOTONIC); + pthread_cond_init (&sock->in_cond, &attr); return sock; }