public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 1/4] util: Some corrections for timespec_diff_us
Date: Tue,  6 Aug 2024 16:18:36 +1000	[thread overview]
Message-ID: <20240806061839.1950144-2-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240806061839.1950144-1-david@gibson.dropbear.id.au>

The comment for timespec_diff_us() claims it will wrap after 2^64µs.  This
is incorrect for two reasons:
  * It returns a long long, which is probably 64-bits, but might not be
  * It returns a signed value, so even if it is 64 bits it will wrap after
    2^63µs

Correct the comment and use an explicitly 64-bit type to avoid that
imprecision.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 util.c | 4 ++--
 util.h | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/util.c b/util.c
index f2e26a7a..0cc908a5 100644
--- a/util.c
+++ b/util.c
@@ -244,9 +244,9 @@ void sock_probe_mem(struct ctx *c)
  * @a:		Minuend timestamp
  * @b:		Subtrahend timestamp
  *
- * Return: difference in microseconds (wraps after 2^64 / 10^6s ~= 585k years)
+ * Return: difference in microseconds (wraps after 2^63 / 10^6s ~= 292k years)
  */
-long long timespec_diff_us(const struct timespec *a, const struct timespec *b)
+int64_t timespec_diff_us(const struct timespec *a, const struct timespec *b)
 {
 	if (a->tv_nsec < b->tv_nsec) {
 		return (b->tv_nsec - a->tv_nsec) / 1000 +
diff --git a/util.h b/util.h
index b7541ce2..d0fec7b3 100644
--- a/util.h
+++ b/util.h
@@ -168,7 +168,7 @@ int sock_l4(const struct ctx *c, sa_family_t af, enum epoll_type type,
 	    uint32_t data);
 void sock_probe_mem(struct ctx *c);
 long timespec_diff_ms(const struct timespec *a, const struct timespec *b);
-long long timespec_diff_us(const struct timespec *a, const struct timespec *b);
+int64_t timespec_diff_us(const struct timespec *a, const struct timespec *b);
 void bitmap_set(uint8_t *map, unsigned bit);
 void bitmap_clear(uint8_t *map, unsigned bit);
 bool bitmap_isset(const uint8_t *map, unsigned bit);
-- 
@@ -168,7 +168,7 @@ int sock_l4(const struct ctx *c, sa_family_t af, enum epoll_type type,
 	    uint32_t data);
 void sock_probe_mem(struct ctx *c);
 long timespec_diff_ms(const struct timespec *a, const struct timespec *b);
-long long timespec_diff_us(const struct timespec *a, const struct timespec *b);
+int64_t timespec_diff_us(const struct timespec *a, const struct timespec *b);
 void bitmap_set(uint8_t *map, unsigned bit);
 void bitmap_clear(uint8_t *map, unsigned bit);
 bool bitmap_isset(const uint8_t *map, unsigned bit);
-- 
2.45.2


  reply	other threads:[~2024-08-06  6:18 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-06  6:18 [PATCH v2 0/4] Fix several small errors in log time handling David Gibson
2024-08-06  6:18 ` David Gibson [this message]
2024-08-06  6:18 ` [PATCH v2 2/4] log: Correct formatting of timestamps David Gibson
2024-08-06 19:08   ` Stefano Brivio
2024-08-07  1:06     ` David Gibson
2024-08-07  8:17       ` Stefano Brivio
2024-08-06  6:18 ` [PATCH v2 3/4] log: Handle errors from clock_gettime() David Gibson
2024-08-06  6:18 ` [PATCH v2 4/4] log: Avoid duplicate calls to logtime() David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240806061839.1950144-2-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).