Skip to contentSkip to conversation

eal: fix use wrong time API

PATCH Superseded Min Hu (Connor) 4 participants 18 messages Apr 28, 2021 – May 12, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal: fix use wrong time API

From: Chengwen Feng <[email protected]>

Currently, the mp uses gettimeofday() API to get the time, and used as timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic attribution.

Fixes 783b6e54971d ("eal: add synchronous multi-process communication")
Fixes f05e26051c15 ("eal: add IPC asynchronous request")
---
 lib/eal/common/eal_common_proc.c | 45 +++++++++++++++++-----------------------
 1 file changed, 19 insertions(+), 26 deletions(-)
lib/eal/common/eal_common_proc.c +19 -26
index 6d1af3c..7f08826 100644
@@ -40,6 +40,12 @@ static char mp_dir_path[PATH_MAX]; /* The directory path for all mp sockets */
 static pthread_mutex_t mp_mutex_action = PTHREAD_MUTEX_INITIALIZER;
 static char peer_name[PATH_MAX];
 
+#ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
+#define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
+#else
+#define CLOCK_TYPE_ID CLOCK_MONOTONIC
+#endif
+
 struct action_entry {
 	TAILQ_ENTRY(action_entry) next;
 	char action_name[RTE_MP_MAX_NAME_LEN];
@@ -490,14 +496,8 @@ async_reply_handle_thread_unsafe(void *arg)
 	struct pending_request *req = (struct pending_request *)arg;
 	enum async_action action;
 	struct timespec ts_now;
-	struct timeval now;
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_TYPE_ID, &ts_now);
 
 	action = process_async_request(req, &ts_now);
 
@@ -896,6 +896,7 @@ mp_request_sync(const char *dst, struct rte_mp_msg *req,
 	       struct rte_mp_reply *reply, const struct timespec *ts)
 {
 	int ret;
+	pthread_condattr_t attr;
 	struct rte_mp_msg msg, *tmp;
 	struct pending_request pending_req, *exist;
 
@@ -904,7 +905,9 @@ mp_request_sync(const char *dst, struct rte_mp_msg *req,
 	strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
 	pending_req.request = req;
 	pending_req.reply = &msg;
-	pthread_cond_init(&pending_req.sync.cond, NULL);
+	pthread_condattr_init(&attr);
+	pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+	pthread_cond_init(&pending_req.sync.cond, &attr);
 
 	exist = find_pending_request(dst, req->name);
 	if (exist) {
@@ -967,8 +970,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 	int dir_fd, ret = -1;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
-	struct timespec end;
+	struct timespec now, end;
 	const struct internal_config *internal_conf =
 		eal_get_internal_configuration();
 
@@ -987,15 +989,10 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		goto end;
-	}
-
-	end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_TYPE_ID, &now);
+	end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end.tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 
 	/* for secondary process, send request to the primary process only */
 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
@@ -1069,7 +1066,7 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 	int dir_fd, ret = 0;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
+	struct timespec now;
 	struct timespec *end;
 	bool dummy_used = false;
 	const struct internal_config *internal_conf =
@@ -1086,11 +1083,6 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		return -1;
-	}
 	copy = calloc(1, sizeof(*copy));
 	dummy = calloc(1, sizeof(*dummy));
 	param = calloc(1, sizeof(*param));
@@ -1108,9 +1100,10 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 	end = &param->end;
 	reply = &param->user_reply;
 
-	end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_TYPE_ID, &now);
+	end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end->tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 	reply->nb_sent = 0;
 	reply->nb_received = 0;
 	reply->msgs = NULL;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 35 lines

From: dev [mailto:[email protected]] On Behalf Of Min Hu (Connor)
Sent: Wednesday, April 28, 2021 10:13 AM

From: Chengwen Feng <[email protected]>

Currently, the mp uses gettimeofday() API to get the time, and used as timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic attribution.

Fixes 783b6e54971d ("eal: add synchronous multi-process

communication")

Fixes f05e26051c15 ("eal: add IPC asynchronous request")
---
 lib/eal/common/eal_common_proc.c | 45 +++++++++++++++++---------------
--------
 1 file changed, 19 insertions(+), 26 deletions(-)
lib/eal/common/eal_common_proc.c +0 -0
index 6d1af3c..7f08826 100644
@@ -40,6 +40,12 @@ static char mp_dir_path[PATH_MAX]; /* The directory

path for all mp sockets */
static pthread_mutex_t mp_mutex_action = PTHREAD_MUTEX_INITIALIZER; static char peer_name[PATH_MAX];

lib/eal/common/eal_common_proc.c static char mp_dir_path[PATH_MAX]; /* The directory path for all mp sockets */ · line 47

Quoted 5 lines
lib/eal/common/eal_common_proc.c +5 -0
+#ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
+#define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
+#else
+#define CLOCK_TYPE_ID CLOCK_MONOTONIC
+#endif

Just out of curiosity: Why do you prefer CLOCK_MONOTONIC_RAW over CLOCK_MONOTONIC?

Quoted 30 lines
lib/eal/common/eal_common_proc.c +3 -7
+
 struct action_entry {
 	TAILQ_ENTRY(action_entry) next;
 	char action_name[RTE_MP_MAX_NAME_LEN];
@@ -490,14 +496,8 @@ async_reply_handle_thread_unsafe(void *arg)
 	struct pending_request *req = (struct pending_request *)arg;
 	enum async_action action;
 	struct timespec ts_now;
-	struct timeval now;
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_TYPE_ID, &ts_now);
 
 	action = process_async_request(req, &ts_now);
 
@@ -896,6 +896,7 @@ mp_request_sync(const char *dst, struct rte_mp_msg
 *req,
 	       struct rte_mp_reply *reply, const struct timespec *ts)
 {
 	int ret;
+	pthread_condattr_t attr;
 	struct rte_mp_msg msg, *tmp;
 	struct pending_request pending_req, *exist;

lib/eal/common/eal_common_proc.c mp_request_sync(const char *dst, struct rte_mp_msg · line 910

Quoted 7 lines
lib/eal/common/eal_common_proc.c +2 -1
@@ -904,7 +905,9 @@ mp_request_sync(const char *dst, struct rte_mp_msg
 *req,
 	strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
 	pending_req.request = req;
 	pending_req.reply = &msg;
-	pthread_cond_init(&pending_req.sync.cond, NULL);
+	pthread_condattr_init(&attr);
+	pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);

Shouldn't CLOCK_MONOTONIC be CLOCK_TYPE_ID here too?

Quoted 76 lines
lib/eal/common/eal_common_proc.c +1 -0
+	pthread_cond_init(&pending_req.sync.cond, &attr);
 
 	exist = find_pending_request(dst, req->name);
lib/eal/common/eal_common_proc.c +4 -10
 	if (exist) {
@@ -967,8 +970,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct
 rte_mp_reply *reply,
 	int dir_fd, ret = -1;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
-	struct timespec end;
+	struct timespec now, end;
 	const struct internal_config *internal_conf =
 		eal_get_internal_configuration();
@@ -987,15 +989,10 @@ rte_mp_request_sync(struct rte_mp_msg *req,
 struct rte_mp_reply *reply,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		goto end;
-	}
-
-	end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_TYPE_ID, &now);
+	end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end.tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 
 	/* for secondary process, send request to the primary process

only */

lib/eal/common/eal_common_proc.c +1 -1
 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
@@ -1069,7 +1066,7 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
 	int dir_fd, ret = 0;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
+	struct timespec now;
 	struct timespec *end;
 	bool dummy_used = false;
lib/eal/common/eal_common_proc.c +0 -5
 	const struct internal_config *internal_conf =
@@ -1086,11 +1083,6 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		return -1;
-	}
 	copy = calloc(1, sizeof(*copy));
 	dummy = calloc(1, sizeof(*dummy));
lib/eal/common/eal_common_proc.c +3 -2
 	param = calloc(1, sizeof(*param));
@@ -1108,9 +1100,10 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
 	end = &param->end;
 	reply = &param->user_reply;
 
-	end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_TYPE_ID, &now);
+	end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end->tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 	reply->nb_sent = 0;
 	reply->nb_received = 0;

reply->msgs = NULL;
--
2.7.4

#2 lore raw
Quoted 35 lines · Morten Brørup
Quoted 35 lines

From: dev [mailto:[email protected]] On Behalf Of Min Hu (Connor)
Sent: Wednesday, April 28, 2021 10:13 AM

From: Chengwen Feng <[email protected]>

Currently, the mp uses gettimeofday() API to get the time, and used as timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic attribution.

Fixes 783b6e54971d ("eal: add synchronous multi-process

communication")

Fixes f05e26051c15 ("eal: add IPC asynchronous request")
---
 lib/eal/common/eal_common_proc.c | 45 +++++++++++++++++---------------
--------
 1 file changed, 19 insertions(+), 26 deletions(-)
lib/eal/common/eal_common_proc.c +0 -0
index 6d1af3c..7f08826 100644
@@ -40,6 +40,12 @@ static char mp_dir_path[PATH_MAX]; /* The directory

path for all mp sockets */
static pthread_mutex_t mp_mutex_action = PTHREAD_MUTEX_INITIALIZER; static char peer_name[PATH_MAX];

Quoted 8 lines

lib/eal/common/eal_common_proc.c static char mp_dir_path[PATH_MAX]; /* The directory path for all mp sockets */ · line 47

Quoted 5 lines
lib/eal/common/eal_common_proc.c +5 -0
+#ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
+#define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
+#else
+#define CLOCK_TYPE_ID CLOCK_MONOTONIC
+#endif

Just out of curiosity: Why do you prefer CLOCK_MONOTONIC_RAW over CLOCK_MONOTONIC?

there may slightly difference, the CLOCK_MONOTONIC_RAW is totally local oscillator (pls see below link), just preferred in engineering practice.
https://stackoverflow.com/questions/14270300/what-is-the-difference-between-clock-monotonic-clock-monotonic-raw

Quoted 29 lines
Quoted 29 lines
lib/eal/common/eal_common_proc.c +3 -7
+
 struct action_entry {
 	TAILQ_ENTRY(action_entry) next;
 	char action_name[RTE_MP_MAX_NAME_LEN];
@@ -490,14 +496,8 @@ async_reply_handle_thread_unsafe(void *arg)
 	struct pending_request *req = (struct pending_request *)arg;
 	enum async_action action;
 	struct timespec ts_now;
-	struct timeval now;
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_TYPE_ID, &ts_now);
 
 	action = process_async_request(req, &ts_now);
 
@@ -896,6 +896,7 @@ mp_request_sync(const char *dst, struct rte_mp_msg
 *req,
 	       struct rte_mp_reply *reply, const struct timespec *ts)
 {
 	int ret;
+	pthread_condattr_t attr;
 	struct rte_mp_msg msg, *tmp;
 	struct pending_request pending_req, *exist;
Quoted 10 lines

lib/eal/common/eal_common_proc.c mp_request_sync(const char *dst, struct rte_mp_msg · line 910

Quoted 8 lines
lib/eal/common/eal_common_proc.c +2 -1
@@ -904,7 +905,9 @@ mp_request_sync(const char *dst, struct rte_mp_msg
 *req,
 	strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
 	pending_req.request = req;
 	pending_req.reply = &msg;
-	pthread_cond_init(&pending_req.sync.cond, NULL);
+	pthread_condattr_init(&attr);
+	pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);

Shouldn't CLOCK_MONOTONIC be CLOCK_TYPE_ID here too?

After reading the source code, it only support CLOCK_MONOTONIC and CLOCK_REALTIME (pls see below link), so cant't use CLOCK_TYPE_ID here.
https://code.woboq.org/userspace/glibc/nptl/pthread_condattr_setclock.c.html#pthread_condattr_setclock

will fix in v2 by make CLOCK_TYPE_ID equal CLOCK_MONOTONIC.

thanks

Quoted 78 lines
Quoted 77 lines
lib/eal/common/eal_common_proc.c +1 -0
+	pthread_cond_init(&pending_req.sync.cond, &attr);
 
 	exist = find_pending_request(dst, req->name);
lib/eal/common/eal_common_proc.c +4 -10
 	if (exist) {
@@ -967,8 +970,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct
 rte_mp_reply *reply,
 	int dir_fd, ret = -1;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
-	struct timespec end;
+	struct timespec now, end;
 	const struct internal_config *internal_conf =
 		eal_get_internal_configuration();
@@ -987,15 +989,10 @@ rte_mp_request_sync(struct rte_mp_msg *req,
 struct rte_mp_reply *reply,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		goto end;
-	}
-
-	end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_TYPE_ID, &now);
+	end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end.tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 
 	/* for secondary process, send request to the primary process

only */

lib/eal/common/eal_common_proc.c +1 -1
 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
@@ -1069,7 +1066,7 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
 	int dir_fd, ret = 0;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
+	struct timespec now;
 	struct timespec *end;
 	bool dummy_used = false;
lib/eal/common/eal_common_proc.c +0 -5
 	const struct internal_config *internal_conf =
@@ -1086,11 +1083,6 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		return -1;
-	}
 	copy = calloc(1, sizeof(*copy));
 	dummy = calloc(1, sizeof(*dummy));
lib/eal/common/eal_common_proc.c +3 -2
 	param = calloc(1, sizeof(*param));
@@ -1108,9 +1100,10 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
 	end = &param->end;
 	reply = &param->user_reply;
 
-	end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_TYPE_ID, &now);
+	end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end->tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 	reply->nb_sent = 0;
 	reply->nb_received = 0;

reply->msgs = NULL;
--
2.7.4

#3 lore raw
Quoted 50 lines

From: dev [mailto:[email protected]] On Behalf Of fengchengwen
Sent: Wednesday, April 28, 2021 12:36 PM

Quoted 6 lines · Morten Brørup
Quoted 6 lines

From: dev [mailto:[email protected]] On Behalf Of Min Hu (Connor)
Sent: Wednesday, April 28, 2021 10:13 AM

From: Chengwen Feng <[email protected]>

Currently, the mp uses gettimeofday() API to get the time, and used

as

Quoted 6 lines
Quoted 6 lines

timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic

attribution.

Quoted 10 lines
Quoted 10 lines
Fixes 783b6e54971d ("eal: add synchronous multi-process

communication")

Fixes f05e26051c15 ("eal: add IPC asynchronous request")
---
 lib/eal/common/eal_common_proc.c | 45 +++++++++++++++++------------

---

Quoted 9 lines

lib/eal/common/eal_common_proc.c static char mp_dir_path[PATH_MAX]; /* The

Quoted 9 lines

--------

 1 file changed, 19 insertions(+), 26 deletions(-)
lib/eal/common/eal_common_proc.c +0 -0
index 6d1af3c..7f08826 100644
@@ -40,6 +40,12 @@ static char mp_dir_path[PATH_MAX]; /* The

directory

Quoted 11 lines

lib/eal/common/eal_common_proc.c static char mp_dir_path[PATH_MAX]; /* The directory path for all mp sockets */ · line 47

Quoted 9 lines

path for all mp sockets */
static pthread_mutex_t mp_mutex_action = PTHREAD_MUTEX_INITIALIZER; static char peer_name[PATH_MAX];

lib/eal/common/eal_common_proc.c +5 -0
+#ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
+#define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
+#else
+#define CLOCK_TYPE_ID CLOCK_MONOTONIC
+#endif

Just out of curiosity: Why do you prefer CLOCK_MONOTONIC_RAW over

Quoted 8 lines

CLOCK_MONOTONIC?

Quoted 1 line

there may slightly difference, the CLOCK_MONOTONIC_RAW is totally local oscillator
(pls see below link), just preferred in engineering practice.
https://stackoverflow.com/questions/14270300/what-is-the-difference-
between-clock-monotonic-clock-monotonic-raw

Interesting link! Following the treads there, it looks like CLOCK_MONOTONIC had a bug in some old kernel versions, where in certain circumstances it could jump slightly backwards.

That bug seems to have been fixed, so CLOCK_MONOTONIC should be safe to use. Source: https://bugzilla.redhat.com/show_bug.cgi?id=448449

Quoted 43 lines
Quoted 19 lines
Quoted 19 lines
lib/eal/common/eal_common_proc.c +2 -7
+
 struct action_entry {
 	TAILQ_ENTRY(action_entry) next;
 	char action_name[RTE_MP_MAX_NAME_LEN];
@@ -490,14 +496,8 @@ async_reply_handle_thread_unsafe(void *arg)
 	struct pending_request *req = (struct pending_request *)arg;
 	enum async_action action;
 	struct timespec ts_now;
-	struct timeval now;
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_TYPE_ID, &ts_now);
 
Quoted 3 lines

lib/eal/common/eal_common_proc.c mp_request_sync(const char *dst, struct

Quoted 3 lines
lib/eal/common/eal_common_proc.c +0 -0
 	action = process_async_request(req, &ts_now);
 
@@ -896,6 +896,7 @@ mp_request_sync(const char *dst, struct

rte_mp_msg

Quoted 9 lines

lib/eal/common/eal_common_proc.c mp_request_sync(const char *dst, struct

Quoted 9 lines
lib/eal/common/eal_common_proc.c +1 -0
 *req,
 	       struct rte_mp_reply *reply, const struct timespec *ts)
 {
 	int ret;
+	pthread_condattr_t attr;
 	struct rte_mp_msg msg, *tmp;
 	struct pending_request pending_req, *exist;
@@ -904,7 +905,9 @@ mp_request_sync(const char *dst, struct

rte_mp_msg

Quoted 9 lines

lib/eal/common/eal_common_proc.c mp_request_sync(const char *dst, struct rte_mp_msg *req, · line 909

Quoted 7 lines
lib/eal/common/eal_common_proc.c +2 -1
 *req,
 	strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
 	pending_req.request = req;
 	pending_req.reply = &msg;
-	pthread_cond_init(&pending_req.sync.cond, NULL);
+	pthread_condattr_init(&attr);
+	pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);

Shouldn't CLOCK_MONOTONIC be CLOCK_TYPE_ID here too?

Quoted 7 lines

After reading the source code, it only support CLOCK_MONOTONIC and CLOCK_REALTIME
(pls see below link), so cant't use CLOCK_TYPE_ID here.
https://code.woboq.org/userspace/glibc/nptl/pthread_condattr_setclock.c
.html#pthread_condattr_setclock

will fix in v2 by make CLOCK_TYPE_ID equal CLOCK_MONOTONIC.

OK, then just get rid of the CLOCK_TYPE_ID definition and use CLOCK_MONOTONIC instead.

Quoted 83 lines

thanks

Quoted 6 lines

lib/eal/common/eal_common_proc.c rte_mp_request_sync(struct rte_mp_msg *req,

Quoted 6 lines
lib/eal/common/eal_common_proc.c +1 -0
+	pthread_cond_init(&pending_req.sync.cond, &attr);
 
 	exist = find_pending_request(dst, req->name);
lib/eal/common/eal_common_proc.c +0 -0
 	if (exist) {
@@ -967,8 +970,7 @@ rte_mp_request_sync(struct rte_mp_msg *req,

struct

Quoted 72 lines
Quoted 71 lines
lib/eal/common/eal_common_proc.c +4 -10
 rte_mp_reply *reply,
 	int dir_fd, ret = -1;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
-	struct timespec end;
+	struct timespec now, end;
 	const struct internal_config *internal_conf =
 		eal_get_internal_configuration();
@@ -987,15 +989,10 @@ rte_mp_request_sync(struct rte_mp_msg *req,
 struct rte_mp_reply *reply,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		goto end;
-	}
-
-	end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_TYPE_ID, &now);
+	end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end.tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 
 	/* for secondary process, send request to the primary process

only */

lib/eal/common/eal_common_proc.c +1 -1
 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
@@ -1069,7 +1066,7 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
 	int dir_fd, ret = 0;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
+	struct timespec now;
 	struct timespec *end;
 	bool dummy_used = false;
lib/eal/common/eal_common_proc.c +0 -5
 	const struct internal_config *internal_conf =
@@ -1086,11 +1083,6 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		return -1;
-	}
 	copy = calloc(1, sizeof(*copy));
 	dummy = calloc(1, sizeof(*dummy));
lib/eal/common/eal_common_proc.c +3 -2
 	param = calloc(1, sizeof(*param));
@@ -1108,9 +1100,10 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
 	end = &param->end;
 	reply = &param->user_reply;
 
-	end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_TYPE_ID, &now);
+	end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end->tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 	reply->nb_sent = 0;
 	reply->nb_received = 0;

reply->msgs = NULL;
--
2.7.4

#4 lore raw

Hi, Morten,
fixed in v2, thanks.

Quoted 200 lines · Morten Brørup
Quoted 50 lines

From: dev [mailto:[email protected]] On Behalf Of fengchengwen
Sent: Wednesday, April 28, 2021 12:36 PM

Quoted 6 lines · Morten Brørup
Quoted 6 lines

From: dev [mailto:[email protected]] On Behalf Of Min Hu (Connor)
Sent: Wednesday, April 28, 2021 10:13 AM

From: Chengwen Feng <[email protected]>

Currently, the mp uses gettimeofday() API to get the time, and used

as

Quoted 6 lines
Quoted 6 lines

timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic

attribution.

Quoted 10 lines
Quoted 10 lines
Fixes 783b6e54971d ("eal: add synchronous multi-process

communication")

Fixes f05e26051c15 ("eal: add IPC asynchronous request")
---
  lib/eal/common/eal_common_proc.c | 45 +++++++++++++++++------------

---

Quoted 9 lines

lib/eal/common/eal_common_proc.c static char mp_dir_path[PATH_MAX]; /* The

Quoted 9 lines

--------

  1 file changed, 19 insertions(+), 26 deletions(-)
lib/eal/common/eal_common_proc.c +0 -0
index 6d1af3c..7f08826 100644
@@ -40,6 +40,12 @@ static char mp_dir_path[PATH_MAX]; /* The

directory

Quoted 11 lines

lib/eal/common/eal_common_proc.c static char mp_dir_path[PATH_MAX]; /* The directory path for all mp sockets */ · line 47

Quoted 9 lines

path for all mp sockets */
static pthread_mutex_t mp_mutex_action = PTHREAD_MUTEX_INITIALIZER; static char peer_name[PATH_MAX];

lib/eal/common/eal_common_proc.c +5 -0
+#ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
+#define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
+#else
+#define CLOCK_TYPE_ID CLOCK_MONOTONIC
+#endif

Just out of curiosity: Why do you prefer CLOCK_MONOTONIC_RAW over

Quoted 8 lines

CLOCK_MONOTONIC?

Quoted 1 line

there may slightly difference, the CLOCK_MONOTONIC_RAW is totally local oscillator
(pls see below link), just preferred in engineering practice.
https://stackoverflow.com/questions/14270300/what-is-the-difference-
between-clock-monotonic-clock-monotonic-raw

Interesting link! Following the treads there, it looks like CLOCK_MONOTONIC had a bug in some old kernel versions, where in certain circumstances it could jump slightly backwards.

That bug seems to have been fixed, so CLOCK_MONOTONIC should be safe to use. Source: https://bugzilla.redhat.com/show_bug.cgi?id=448449

Quoted 43 lines
Quoted 19 lines
Quoted 19 lines
lib/eal/common/eal_common_proc.c +2 -7
+
  struct action_entry {
  	TAILQ_ENTRY(action_entry) next;
  	char action_name[RTE_MP_MAX_NAME_LEN];
@@ -490,14 +496,8 @@ async_reply_handle_thread_unsafe(void *arg)
  	struct pending_request *req = (struct pending_request *)arg;
  	enum async_action action;
  	struct timespec ts_now;
-	struct timeval now;
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_TYPE_ID, &ts_now);
 
Quoted 3 lines

lib/eal/common/eal_common_proc.c mp_request_sync(const char *dst, struct

Quoted 3 lines
lib/eal/common/eal_common_proc.c +0 -0
  	action = process_async_request(req, &ts_now);
 
@@ -896,6 +896,7 @@ mp_request_sync(const char *dst, struct

rte_mp_msg

Quoted 9 lines

lib/eal/common/eal_common_proc.c mp_request_sync(const char *dst, struct

Quoted 9 lines
lib/eal/common/eal_common_proc.c +1 -0
 *req,
  	       struct rte_mp_reply *reply, const struct timespec *ts)
  {
  	int ret;
+	pthread_condattr_t attr;
  	struct rte_mp_msg msg, *tmp;
  	struct pending_request pending_req, *exist;
@@ -904,7 +905,9 @@ mp_request_sync(const char *dst, struct

rte_mp_msg

Quoted 9 lines

lib/eal/common/eal_common_proc.c mp_request_sync(const char *dst, struct rte_mp_msg *req, · line 909

Quoted 7 lines
lib/eal/common/eal_common_proc.c +2 -1
 *req,
  	strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
  	pending_req.request = req;
  	pending_req.reply = &msg;
-	pthread_cond_init(&pending_req.sync.cond, NULL);
+	pthread_condattr_init(&attr);
+	pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);

Shouldn't CLOCK_MONOTONIC be CLOCK_TYPE_ID here too?

Quoted 7 lines

After reading the source code, it only support CLOCK_MONOTONIC and CLOCK_REALTIME
(pls see below link), so cant't use CLOCK_TYPE_ID here.
https://code.woboq.org/userspace/glibc/nptl/pthread_condattr_setclock.c
.html#pthread_condattr_setclock

will fix in v2 by make CLOCK_TYPE_ID equal CLOCK_MONOTONIC.

OK, then just get rid of the CLOCK_TYPE_ID definition and use CLOCK_MONOTONIC instead.

Quoted 83 lines

thanks

Quoted 6 lines

lib/eal/common/eal_common_proc.c rte_mp_request_sync(struct rte_mp_msg *req,

Quoted 6 lines
lib/eal/common/eal_common_proc.c +1 -0
+	pthread_cond_init(&pending_req.sync.cond, &attr);
 
  	exist = find_pending_request(dst, req->name);
  	if (exist) {
lib/eal/common/eal_common_proc.c +0 -0
@@ -967,8 +970,7 @@ rte_mp_request_sync(struct rte_mp_msg *req,

struct

Quoted 72 lines
Quoted 71 lines
lib/eal/common/eal_common_proc.c +4 -10
 rte_mp_reply *reply,
  	int dir_fd, ret = -1;
  	DIR *mp_dir;
  	struct dirent *ent;
-	struct timeval now;
-	struct timespec end;
+	struct timespec now, end;
  	const struct internal_config *internal_conf =
  		eal_get_internal_configuration();
@@ -987,15 +989,10 @@ rte_mp_request_sync(struct rte_mp_msg *req,
 struct rte_mp_reply *reply,
  		return -1;
  	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		goto end;
-	}
-
-	end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_TYPE_ID, &now);
+	end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
  	end.tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 
  	/* for secondary process, send request to the primary process

only */
if (rte_eal_process_type() == RTE_PROC_SECONDARY) {

lib/eal/common/eal_common_proc.c +1 -1
@@ -1069,7 +1066,7 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
  	int dir_fd, ret = 0;
  	DIR *mp_dir;
  	struct dirent *ent;
-	struct timeval now;
+	struct timespec now;
  	struct timespec *end;
  	bool dummy_used = false;

const struct internal_config *internal_conf =

lib/eal/common/eal_common_proc.c +0 -5
@@ -1086,11 +1083,6 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
  		return -1;
  	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		return -1;
-	}
  	copy = calloc(1, sizeof(*copy));
  	dummy = calloc(1, sizeof(*dummy));
  	param = calloc(1, sizeof(*param));
lib/eal/common/eal_common_proc.c +3 -2
@@ -1108,9 +1100,10 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
  	end = &param->end;
  	reply = &param->user_reply;
 
-	end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_TYPE_ID, &now);
+	end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
  	end->tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
  	reply->nb_sent = 0;
  	reply->nb_received = 0;

reply->msgs = NULL;
--
2.7.4

#5 lore raw

PATCH v2 1/1 eal: fix use wrong time API

In reply to Min Hu (Connor)

Currently, the mp uses gettimeofday() API to get the time, and used as timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic attribution.

Fixes 783b6e54971d ("eal: add synchronous multi-process communication")
Fixes f05e26051c15 ("eal: add IPC asynchronous request")

---
v2:
* get rid of the CLOCK_TYPE_ID definition and use CLOCK_MONOTONIC instead.

---
 lib/eal/common/eal_common_proc.c | 39 +++++++++++++--------------------------
 1 file changed, 13 insertions(+), 26 deletions(-)
lib/eal/common/eal_common_proc.c +13 -26
index 6d1af3c..6af8ba2 100644
@@ -490,14 +490,8 @@ async_reply_handle_thread_unsafe(void *arg)
 	struct pending_request *req = (struct pending_request *)arg;
 	enum async_action action;
 	struct timespec ts_now;
-	struct timeval now;
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_MONOTONIC, &ts_now);
 
 	action = process_async_request(req, &ts_now);
 
@@ -896,6 +890,7 @@ mp_request_sync(const char *dst, struct rte_mp_msg *req,
 	       struct rte_mp_reply *reply, const struct timespec *ts)
 {
 	int ret;
+	pthread_condattr_t attr;
 	struct rte_mp_msg msg, *tmp;
 	struct pending_request pending_req, *exist;
 
@@ -904,7 +899,9 @@ mp_request_sync(const char *dst, struct rte_mp_msg *req,
 	strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
 	pending_req.request = req;
 	pending_req.reply = &msg;
-	pthread_cond_init(&pending_req.sync.cond, NULL);
+	pthread_condattr_init(&attr);
+	pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+	pthread_cond_init(&pending_req.sync.cond, &attr);
 
 	exist = find_pending_request(dst, req->name);
 	if (exist) {
@@ -967,8 +964,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 	int dir_fd, ret = -1;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
-	struct timespec end;
+	struct timespec now, end;
 	const struct internal_config *internal_conf =
 		eal_get_internal_configuration();
 
@@ -987,15 +983,10 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		goto end;
-	}
-
-	end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_MONOTONIC, &now);
+	end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end.tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 
 	/* for secondary process, send request to the primary process only */
 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
@@ -1069,7 +1060,7 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 	int dir_fd, ret = 0;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
+	struct timespec now;
 	struct timespec *end;
 	bool dummy_used = false;
 	const struct internal_config *internal_conf =
@@ -1086,11 +1077,6 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		return -1;
-	}
 	copy = calloc(1, sizeof(*copy));
 	dummy = calloc(1, sizeof(*dummy));
 	param = calloc(1, sizeof(*param));
@@ -1108,9 +1094,10 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 	end = &param->end;
 	reply = &param->user_reply;
 
-	end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_MONOTONIC, &now);
+	end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end->tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 	reply->nb_sent = 0;
 	reply->nb_received = 0;
 	reply->msgs = NULL;
Signature · 1 line
2.7.4
Acked-by on v2 1/1
#6 lore raw
Quoted 138 lines

From: dev [mailto:[email protected]] On Behalf Of Min Hu (Connor)
Sent: Thursday, April 29, 2021 4:11 AM
To: [email protected]

Currently, the mp uses gettimeofday() API to get the time, and used as timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic attribution.

Fixes 783b6e54971d ("eal: add synchronous multi-process

communication")

Fixes f05e26051c15 ("eal: add IPC asynchronous request")

---
v2:
* get rid of the CLOCK_TYPE_ID definition and use CLOCK_MONOTONIC instead.

---
 lib/eal/common/eal_common_proc.c | 39 +++++++++++++-------------------
-------
 1 file changed, 13 insertions(+), 26 deletions(-)
lib/eal/common/eal_common_proc.c +5 -8
index 6d1af3c..6af8ba2 100644
@@ -490,14 +490,8 @@ async_reply_handle_thread_unsafe(void *arg)
 	struct pending_request *req = (struct pending_request *)arg;
 	enum async_action action;
 	struct timespec ts_now;
-	struct timeval now;
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_MONOTONIC, &ts_now);
 
 	action = process_async_request(req, &ts_now);
 
@@ -896,6 +890,7 @@ mp_request_sync(const char *dst, struct rte_mp_msg
 *req,
 	       struct rte_mp_reply *reply, const struct timespec *ts)
 {
 	int ret;
+	pthread_condattr_t attr;
 	struct rte_mp_msg msg, *tmp;
 	struct pending_request pending_req, *exist;
@@ -904,7 +899,9 @@ mp_request_sync(const char *dst, struct rte_mp_msg
 *req,
 	strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
 	pending_req.request = req;
 	pending_req.reply = &msg;
-	pthread_cond_init(&pending_req.sync.cond, NULL);
+	pthread_condattr_init(&attr);
+	pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+	pthread_cond_init(&pending_req.sync.cond, &attr);
 
 	exist = find_pending_request(dst, req->name);
lib/eal/common/eal_common_proc.c +4 -10
 	if (exist) {
@@ -967,8 +964,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct
 rte_mp_reply *reply,
 	int dir_fd, ret = -1;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
-	struct timespec end;
+	struct timespec now, end;
 	const struct internal_config *internal_conf =
 		eal_get_internal_configuration();
@@ -987,15 +983,10 @@ rte_mp_request_sync(struct rte_mp_msg *req,
 struct rte_mp_reply *reply,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		goto end;
-	}
-
-	end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_MONOTONIC, &now);
+	end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end.tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 
 	/* for secondary process, send request to the primary process

only */

lib/eal/common/eal_common_proc.c +1 -1
 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
@@ -1069,7 +1060,7 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
 	int dir_fd, ret = 0;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
+	struct timespec now;
 	struct timespec *end;
 	bool dummy_used = false;
lib/eal/common/eal_common_proc.c +0 -5
 	const struct internal_config *internal_conf =
@@ -1086,11 +1077,6 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		return -1;
-	}
 	copy = calloc(1, sizeof(*copy));
 	dummy = calloc(1, sizeof(*dummy));
lib/eal/common/eal_common_proc.c +3 -2
 	param = calloc(1, sizeof(*param));
@@ -1108,9 +1094,10 @@ rte_mp_request_async(struct rte_mp_msg *req,
 const struct timespec *ts,
 	end = &param->end;
 	reply = &param->user_reply;
 
-	end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_MONOTONIC, &now);
+	end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end->tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 	reply->nb_sent = 0;
 	reply->nb_received = 0;
Quoted 4 lines

reply->msgs = NULL;
--
2.7.4

#7 lore raw
In reply to Min Hu (Connor)

29/04/2021 04:10, Min Hu (Connor):

Quoted 15 lines

Currently, the mp uses gettimeofday() API to get the time, and used as timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic attribution.

Fixes 783b6e54971d ("eal: add synchronous multi-process communication")
Fixes f05e26051c15 ("eal: add IPC asynchronous request")

---

[...]

lib/eal/common/eal_common_proc.c async_reply_handle_thread_unsafe(void *arg) · line 494

Quoted 9 lines
lib/eal/common/eal_common_proc.c +1 -6
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_MONOTONIC, &ts_now);

Why not testing the return value?

I think this change would not be appropriate after -rc1. If you agree, I will postpone to DPDK 21.08.

#8 lore raw
Quoted 23 lines

From: dev [mailto:[email protected]] On Behalf Of Thomas Monjalon
Sent: Tuesday, May 4, 2021 6:50 PM

29/04/2021 04:10, Min Hu (Connor):

Quoted 1 line

Currently, the mp uses gettimeofday() API to get the time, and used

as

Quoted 6 lines

timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic

attribution.

Quoted 2 lines
Fixes 783b6e54971d ("eal: add synchronous multi-process

communication")

Quoted 6 lines
Fixes f05e26051c15 ("eal: add IPC asynchronous request")

---

[...]

Quoted 11 lines

lib/eal/common/eal_common_proc.c async_reply_handle_thread_unsafe(void *arg) · line 494

Quoted 9 lines
lib/eal/common/eal_common_proc.c +1 -6
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_MONOTONIC, &ts_now);

Why not testing the return value?

Because it is guaranteed not to fail. Ref:
https://linux.die.net/man/3/clock_gettime
https://www.freebsd.org/cgi/man.cgi?query=clock_gettime

Quoted 3 lines

I think this change would not be appropriate after -rc1. If you agree, I will postpone to DPDK 21.08.

It does fix a serious bug, where IPC timeouts can incorrectly happen. And this is not a theoretical bug; I have seen errors happen due to using the wrong clock source in other projects.

However, I have no clue if these IPC library functions are important or not. So I have no qualified opinion about postponing the change.

#9 lore raw

04/05/2021 21:12, Morten Brørup:

Quoted 35 lines
Quoted 23 lines

From: dev [mailto:[email protected]] On Behalf Of Thomas Monjalon
Sent: Tuesday, May 4, 2021 6:50 PM

29/04/2021 04:10, Min Hu (Connor):

Quoted 1 line

Currently, the mp uses gettimeofday() API to get the time, and used

as

Quoted 6 lines

timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic

attribution.

Quoted 2 lines
Fixes 783b6e54971d ("eal: add synchronous multi-process

communication")

Quoted 6 lines
Fixes f05e26051c15 ("eal: add IPC asynchronous request")

---

[...]

Quoted 11 lines

lib/eal/common/eal_common_proc.c async_reply_handle_thread_unsafe(void *arg) · line 494

Quoted 9 lines
lib/eal/common/eal_common_proc.c +1 -6
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_MONOTONIC, &ts_now);

Why not testing the return value?

Quoted 3 lines

Because it is guaranteed not to fail. Ref:
https://linux.die.net/man/3/clock_gettime
https://www.freebsd.org/cgi/man.cgi?query=clock_gettime

I see "return 0 for success, or -1 for failure".
Where is it said it cannot fail?

Quoted 6 lines
Quoted 2 lines

I think this change would not be appropriate after -rc1. If you agree, I will postpone to DPDK 21.08.

It does fix a serious bug, where IPC timeouts can incorrectly happen. And this is not a theoretical bug; I have seen errors happen due to using the wrong clock source in other projects.

However, I have no clue if these IPC library functions are important or not. So I have no qualified opinion about postponing the change.

I think nobody hit such bug with DPDK IPC.

#10 lore raw
Quoted 42 lines

From: dev [mailto:[email protected]] On Behalf Of Thomas Monjalon
Sent: Wednesday, May 5, 2021 8:14 AM

04/05/2021 21:12, Morten Brørup:

Quoted 1 line
Quoted 1 line

From: dev [mailto:[email protected]] On Behalf Of Thomas

Monjalon

Quoted 4 lines
Quoted 4 lines

Sent: Tuesday, May 4, 2021 6:50 PM

29/04/2021 04:10, Min Hu (Connor):

Quoted 1 line

Currently, the mp uses gettimeofday() API to get the time, and

used

Quoted 4 lines
Quoted 4 lines

as

Quoted 3 lines

timeout parameter.

But the time which gets from gettimeofday() API isn't

monotonically

Quoted 26 lines
Quoted 25 lines
Quoted 3 lines

increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic

attribution.

Quoted 2 lines
Fixes 783b6e54971d ("eal: add synchronous multi-process

communication")

Quoted 6 lines
Fixes f05e26051c15 ("eal: add IPC asynchronous request")

---

[...]

lib/eal/common/eal_common_proc.c async_reply_handle_thread_unsafe(void *arg) · line 494

Quoted 9 lines
lib/eal/common/eal_common_proc.c +1 -6
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_MONOTONIC, &ts_now);

Why not testing the return value?

Quoted 6 lines
Quoted 3 lines

Because it is guaranteed not to fail. Ref:
https://linux.die.net/man/3/clock_gettime
https://www.freebsd.org/cgi/man.cgi?query=clock_gettime

I see "return 0 for success, or -1 for failure".
Where is it said it cannot fail?

I'm sorry about being unclear. Referring to the "Errors" chapter in the function's man page, this function call is guaranteed not to fail with these parameters. So there is no need to check the return value.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

I think this change would not be appropriate after -rc1. If you agree, I will postpone to DPDK 21.08.

Quoted 9 lines
Quoted 1 line

It does fix a serious bug, where IPC timeouts can incorrectly happen.

And this is not a theoretical bug; I have seen errors happen due to using the wrong clock source in other projects.

Quoted 2 lines

However, I have no clue if these IPC library functions are important

or not. So I have no qualified opinion about postponing the change.

I think nobody hit such bug with DPDK IPC.

Then I don't object to postponing.

#11 lore raw

05/05/2021 08:26, Morten Brørup:

Quoted 42 lines
Quoted 42 lines

From: dev [mailto:[email protected]] On Behalf Of Thomas Monjalon
Sent: Wednesday, May 5, 2021 8:14 AM

04/05/2021 21:12, Morten Brørup:

Quoted 1 line
Quoted 1 line

From: dev [mailto:[email protected]] On Behalf Of Thomas

Monjalon

Quoted 4 lines
Quoted 4 lines

Sent: Tuesday, May 4, 2021 6:50 PM

29/04/2021 04:10, Min Hu (Connor):

Quoted 1 line

Currently, the mp uses gettimeofday() API to get the time, and

used

Quoted 4 lines
Quoted 4 lines

as

Quoted 3 lines

timeout parameter.

But the time which gets from gettimeofday() API isn't

monotonically

Quoted 26 lines
Quoted 25 lines
Quoted 3 lines

increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic

attribution.

Quoted 2 lines
Fixes 783b6e54971d ("eal: add synchronous multi-process

communication")

Quoted 6 lines
Fixes f05e26051c15 ("eal: add IPC asynchronous request")

---

[...]

lib/eal/common/eal_common_proc.c async_reply_handle_thread_unsafe(void *arg) · line 494

Quoted 9 lines
lib/eal/common/eal_common_proc.c +1 -6
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_MONOTONIC, &ts_now);

Why not testing the return value?

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Because it is guaranteed not to fail. Ref:
https://linux.die.net/man/3/clock_gettime
https://www.freebsd.org/cgi/man.cgi?query=clock_gettime

I see "return 0 for success, or -1 for failure".
Where is it said it cannot fail?

I'm sorry about being unclear. Referring to the "Errors" chapter in the function's man page, this function call is guaranteed not to fail with these parameters. So there is no need to check the return value.

I don't agree.
Especially for this error:
"The clk_id specified is not supported on this system." How can you be sure it is always supported for any system we try to run this code now and in future experiments?

#12 lore raw
Quoted 57 lines

From: Thomas Monjalon [mailto:[email protected]]
Sent: Wednesday, May 5, 2021 8:51 AM

05/05/2021 08:26, Morten Brørup:

Quoted 1 line
Quoted 1 line

From: dev [mailto:[email protected]] On Behalf Of Thomas

Monjalon

Quoted 9 lines
Quoted 9 lines

Sent: Wednesday, May 5, 2021 8:14 AM

04/05/2021 21:12, Morten Brørup:

Quoted 1 line
Quoted 1 line

From: dev [mailto:[email protected]] On Behalf Of Thomas

Monjalon

Quoted 4 lines
Quoted 4 lines

Sent: Tuesday, May 4, 2021 6:50 PM

29/04/2021 04:10, Min Hu (Connor):

Quoted 1 line

Currently, the mp uses gettimeofday() API to get the time,

and

Quoted 7 lines
Quoted 7 lines

used

Quoted 4 lines
Quoted 4 lines

as

Quoted 3 lines

timeout parameter.

But the time which gets from gettimeofday() API isn't

monotonically

Quoted 1 line
Quoted 1 line
Quoted 1 line

increasing. The process may fail if the system time is

changed.

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 2 lines

This fixes it by using clock_gettime() API with monotonic

attribution.

Quoted 2 lines
Fixes 783b6e54971d ("eal: add synchronous multi-process

communication")

Quoted 6 lines
Fixes f05e26051c15 ("eal: add IPC asynchronous request")

---

[...]

lib/eal/common/eal_common_proc.c async_reply_handle_thread_unsafe(void *arg) · line 494

Quoted 9 lines
lib/eal/common/eal_common_proc.c +1 -6
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_MONOTONIC, &ts_now);

Why not testing the return value?

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Because it is guaranteed not to fail. Ref:
https://linux.die.net/man/3/clock_gettime
https://www.freebsd.org/cgi/man.cgi?query=clock_gettime

I see "return 0 for success, or -1 for failure".
Where is it said it cannot fail?

I'm sorry about being unclear. Referring to the "Errors" chapter in

Quoted 9 lines

the function's man page, this function call is guaranteed not to fail with these parameters. So there is no need to check the return value.

I don't agree.
Especially for this error:
"The clk_id specified is not supported on this system." How can you be sure it is always supported for any system we try to run this code now and in future experiments?

I referred to both Linux and BSD man pages because they say that it is defined there. This should cover current supported systems, excl. Windows, which doesn't have gettimeofday() or clock_gettime() anyway.

However, for future operating systems that might be supported by DPDK, you are correct that CLOCK_MONOTONIC is optional.
Ref: https://pubs.opengroup.org/onlinepubs/009695399/basedefs/time.h.html

This can be fixed by adding:
#if !defined(CLOCK_MONOTONIC)
#error CLOCK_MONOTONIC is not defined.
#endif

If we only check the return value at runtime, the function would fail every time on a system without CLOCK_MONOTONIC, and eal_common_proc.c would need to be fixed anyway. It's better to catch at compile time.

#13 lore raw

PATCH v3 1/1 eal: fix use wrong time API

In reply to Min Hu (Connor)

Currently, the mp uses gettimeofday() API to get the time, and used as timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic attribution.

Fixes 783b6e54971d ("eal: add synchronous multi-process communication")
Fixes f05e26051c15 ("eal: add IPC asynchronous request")

---
v3:
* add acked-by.
* change patch's author.

v2:
* get rid of the CLOCK_TYPE_ID definition and use CLOCK_MONOTONIC instead.

---
 lib/eal/common/eal_common_proc.c | 39 +++++++++++++--------------------------
 1 file changed, 13 insertions(+), 26 deletions(-)
lib/eal/common/eal_common_proc.c +13 -26
index 6d1af3c..6af8ba2 100644
@@ -490,14 +490,8 @@ async_reply_handle_thread_unsafe(void *arg)
 	struct pending_request *req = (struct pending_request *)arg;
 	enum async_action action;
 	struct timespec ts_now;
-	struct timeval now;
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_MONOTONIC, &ts_now);
 
 	action = process_async_request(req, &ts_now);
 
@@ -896,6 +890,7 @@ mp_request_sync(const char *dst, struct rte_mp_msg *req,
 	       struct rte_mp_reply *reply, const struct timespec *ts)
 {
 	int ret;
+	pthread_condattr_t attr;
 	struct rte_mp_msg msg, *tmp;
 	struct pending_request pending_req, *exist;
 
@@ -904,7 +899,9 @@ mp_request_sync(const char *dst, struct rte_mp_msg *req,
 	strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
 	pending_req.request = req;
 	pending_req.reply = &msg;
-	pthread_cond_init(&pending_req.sync.cond, NULL);
+	pthread_condattr_init(&attr);
+	pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+	pthread_cond_init(&pending_req.sync.cond, &attr);
 
 	exist = find_pending_request(dst, req->name);
 	if (exist) {
@@ -967,8 +964,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 	int dir_fd, ret = -1;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
-	struct timespec end;
+	struct timespec now, end;
 	const struct internal_config *internal_conf =
 		eal_get_internal_configuration();
 
@@ -987,15 +983,10 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		goto end;
-	}
-
-	end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_MONOTONIC, &now);
+	end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end.tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 
 	/* for secondary process, send request to the primary process only */
 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
@@ -1069,7 +1060,7 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 	int dir_fd, ret = 0;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
+	struct timespec now;
 	struct timespec *end;
 	bool dummy_used = false;
 	const struct internal_config *internal_conf =
@@ -1086,11 +1077,6 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		return -1;
-	}
 	copy = calloc(1, sizeof(*copy));
 	dummy = calloc(1, sizeof(*dummy));
 	param = calloc(1, sizeof(*param));
@@ -1108,9 +1094,10 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 	end = &param->end;
 	reply = &param->user_reply;
 
-	end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_MONOTONIC, &now);
+	end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end->tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 	reply->nb_sent = 0;
 	reply->nb_received = 0;
 	reply->msgs = NULL;
Signature · 1 line
2.8.1
#14 lore raw

05/05/2021 05:43, Chengwen Feng:

Quoted 12 lines

Currently, the mp uses gettimeofday() API to get the time, and used as timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic attribution.

Fixes 783b6e54971d ("eal: add synchronous multi-process communication")
Fixes f05e26051c15 ("eal: add IPC asynchronous request")
Quoted 7 lines

---
v3:
* add acked-by.
* change patch's author.

I did some comments on v2 about potential errors to catch, but you sent this v3 without participating in v2 discussion.

#15 lore raw
Quoted 21 lines · Thomas Monjalon

05/05/2021 05:43, Chengwen Feng:

Quoted 12 lines

Currently, the mp uses gettimeofday() API to get the time, and used as timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic attribution.

Fixes 783b6e54971d ("eal: add synchronous multi-process communication")
Fixes f05e26051c15 ("eal: add IPC asynchronous request")
Quoted 7 lines

---
v3:
* add acked-by.
* change patch's author.

Quoted 4 lines

I did some comments on v2 about potential errors to catch, but you sent this v3 without participating in v2 discussion.

Fixed in v4, thanks

Quoted 3 lines

.

#16 lore raw

PATCH v4 1/1 eal: fix use wrong time API

In reply to Min Hu (Connor)

Currently, the mp uses gettimeofday() API to get the time, and used as timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic attribution.

Fixes 783b6e54971d ("eal: add synchronous multi-process communication")
Fixes f05e26051c15 ("eal: add IPC asynchronous request")

---
v4:
* add check clock_gettime() API's retcode
v3:
* add acked-by.
* change patch's author.
v2:
* get rid of the CLOCK_TYPE_ID definition and use CLOCK_MONOTONIC instead.

---
 lib/eal/common/eal_common_proc.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
lib/eal/common/eal_common_proc.c +13 -14
index 6d1af3c..dc4a2ef 100644
@@ -490,14 +490,11 @@ async_reply_handle_thread_unsafe(void *arg)
 	struct pending_request *req = (struct pending_request *)arg;
 	enum async_action action;
 	struct timespec ts_now;
-	struct timeval now;
 
-	if (gettimeofday(&now, NULL) < 0) {
+	if (clock_gettime(CLOCK_MONOTONIC, &ts_now) < 0) {
 		RTE_LOG(ERR, EAL, "Cannot get current time\n");
 		goto no_trigger;
 	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
 
 	action = process_async_request(req, &ts_now);
 
@@ -896,6 +893,7 @@ mp_request_sync(const char *dst, struct rte_mp_msg *req,
 	       struct rte_mp_reply *reply, const struct timespec *ts)
 {
 	int ret;
+	pthread_condattr_t attr;
 	struct rte_mp_msg msg, *tmp;
 	struct pending_request pending_req, *exist;
 
@@ -904,7 +902,9 @@ mp_request_sync(const char *dst, struct rte_mp_msg *req,
 	strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
 	pending_req.request = req;
 	pending_req.reply = &msg;
-	pthread_cond_init(&pending_req.sync.cond, NULL);
+	pthread_condattr_init(&attr);
+	pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+	pthread_cond_init(&pending_req.sync.cond, &attr);
 
 	exist = find_pending_request(dst, req->name);
 	if (exist) {
@@ -967,8 +967,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 	int dir_fd, ret = -1;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
-	struct timespec end;
+	struct timespec now, end;
 	const struct internal_config *internal_conf =
 		eal_get_internal_configuration();
 
@@ -987,15 +986,15 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
+	if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
 		RTE_LOG(ERR, EAL, "Failed to get current time\n");
 		rte_errno = errno;
 		goto end;
 	}
 
-	end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end.tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 
 	/* for secondary process, send request to the primary process only */
 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
@@ -1069,7 +1068,7 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 	int dir_fd, ret = 0;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
+	struct timespec now;
 	struct timespec *end;
 	bool dummy_used = false;
 	const struct internal_config *internal_conf =
@@ -1086,7 +1085,7 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
+	if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
 		RTE_LOG(ERR, EAL, "Failed to get current time\n");
 		rte_errno = errno;
 		return -1;
@@ -1108,9 +1107,9 @@ rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 	end = &param->end;
 	reply = &param->user_reply;
 
-	end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end->tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 	reply->nb_sent = 0;
 	reply->nb_received = 0;
 	reply->msgs = NULL;
Signature · 1 line
2.8.1
#17 lore raw

11/05/2021 12:41, Chengwen Feng:

Quoted 8 lines

Currently, the mp uses gettimeofday() API to get the time, and used as timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic attribution.

Quoted 7 lines
Fixes 783b6e54971d ("eal: add synchronous multi-process communication")
Fixes f05e26051c15 ("eal: add IPC asynchronous request")

Applied, thanks.