changeset 100:2cb2ec63a098

iothread: be more deliberate with getting timestamps of the first byte Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Wed, 13 Sep 2023 10:36:23 -0400
parents 5e4137c51146
children 53d4eab866b8
files iothread.c
diffstat 1 files changed, 25 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/iothread.c	Wed Sep 13 10:19:28 2023 -0400
+++ b/iothread.c	Wed Sep 13 10:36:23 2023 -0400
@@ -45,13 +45,29 @@
 static bool poll_version;
 static uint32_t seq; /* message sequence number */
 
+static inline int read_with_ts(int fd, void *data, size_t len,
+			       uint64_t *start_time, uint64_t *start_tick)
+{
+	int ret;
+
+	asm volatile(""); /* compiler barrier */
+
+	ret = xread(fd, data, len);
+	*start_time = gettime();
+	*start_tick = gettick();
+
+	asm volatile(""); /* compiler barrier */
+
+	return ret;
+}
+
 /*
  * read until we get the beginning of a NMEA or UBX message
  *
  * Returns a negated errno on error, 0 if we found a UBX message sync byte,
  * and 1 if we found a NMEA sync byte.
  */
-static int sync_reader(void)
+static int sync_reader(uint64_t *start_time, uint64_t *start_tick)
 {
 	size_t skipped;
 
@@ -61,7 +77,8 @@
 		uint8_t byte;
 		int ret;
 
-		ret = xread(input_file, &byte, sizeof(byte));
+		ret = read_with_ts(input_file, &byte, sizeof(byte),
+				   start_time, start_tick);
 		if (ret)
 			return ret;
 
@@ -223,10 +240,12 @@
 static void *iothread_reader(void *data)
 {
 	uint64_t last_poll_time;
+	uint64_t start_tick;
+	uint64_t start_time;
 	uint8_t byte;
 	int ret;
 
-	ret = sync_reader();
+	ret = sync_reader(&start_time, &start_tick);
 	if (ret < 0) {
 		fprintf(stderr, "Failed to synchronize reader: %s\n",
 			xstrerror(ret));
@@ -237,16 +256,10 @@
 	byte = (ret == 0) ? UBX_SYNC_BYTE_1 : '$';
 
 	/* setup already got it all */
-	last_poll_time = gettime();
+	last_poll_time = start_time;
 
 	/* read a NMEA or UBX message & process it */
 	for (;;) {
-		uint64_t start_tick;
-		uint64_t start_time;
-
-		start_tick = gettick();
-		start_time = gettime();
-
 		if (byte == UBX_SYNC_BYTE_1) {
 			ret = read_ubx(start_time, start_tick);
 		} else if (byte == '$') {
@@ -287,7 +300,8 @@
 		}
 
 		/* read the sync byte for the next iteration */
-		ret = xread(input_file, &byte, sizeof(byte));
+		ret = read_with_ts(input_file, &byte, sizeof(byte), &start_time,
+				   &start_tick);
 		if (ret) {
 			fprintf(stderr, "Failed to read message sync byte: %s\n",
 				xstrerror(ret));