Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MONOTONIC time for socket recv timeout #423

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions apteryxd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
{
Expand Down
23 changes: 4 additions & 19 deletions database.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down
12 changes: 9 additions & 3 deletions internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
12 changes: 8 additions & 4 deletions rpc_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
Loading