From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id AF2155A0319 for ; Thu, 25 Jul 2024 05:47:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1721879255; bh=DFxBAz82MxZvzpWs7/cKKvQ4X+fBVaMnstRk4Wt5sEQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=OQ6mfBl5FikuxSW3S5bPQf3mCw3WQW4iHrQzjg0GYdA7cm7c3l5xQNHCpqNzXfOWo 9s3DdxfshOtUo/9a+nTzrqT9NYMMlfM11r91NcN1I7Mgvr3W6Oapckh759HDbYQdex IvytasgI9npzEm4uCxk9M3jQTDQD67l2gmOuJBkoQlitNMxAI/LZFjUQHX9xLfG8Lu t1LEFqIDkuHlWJ4fvqPyfySZlpteTxc204q3OUAomii8R7TVMXve1W2kGglEsered/ mJfLm4GIh+kCJYcK33cYMZFjPiKiiGCLN6HR7bAXVH4k6PwiDVLDU8VRtlEM9D8JMN 2TTQkVvh+uF+g== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WTxfb4WfZz4x3J; Thu, 25 Jul 2024 13:47:35 +1000 (AEST) Date: Thu, 25 Jul 2024 13:32:52 +1000 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 04/11] log: Fix sub-second part in relative log time calculation Message-ID: References: <20240724215021.3366863-1-sbrivio@redhat.com> <20240724215021.3366863-5-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="lam2Z43GStz+n+PV" Content-Disposition: inline In-Reply-To: <20240724215021.3366863-5-sbrivio@redhat.com> Message-ID-Hash: WYUYNVI7BOBLHZAWPE7R6D5S7M3RYHTD X-Message-ID-Hash: WYUYNVI7BOBLHZAWPE7R6D5S7M3RYHTD X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --lam2Z43GStz+n+PV Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jul 24, 2024 at 11:50:10PM +0200, Stefano Brivio wrote: > For some reason, in commit 01efc71ddd25 ("log, conf: Add support for > logging to file"), I added calculations for relative logging > timestamps using the difference for the seconds part only, not for > accounting for the fractional part. >=20 > Fix that by storing the initial timestamp, log_start, as a timespec > struct, and by calculating differences of both seconds and nanoseconds > from the starting time. Do this in a macro as we need the same > calculation and format in a few places. >=20 > Signed-off-by: Stefano Brivio > --- > log.c | 42 ++++++++++++++++++++++-------------------- > 1 file changed, 22 insertions(+), 20 deletions(-) >=20 > diff --git a/log.c b/log.c > index 54483e7..f57a54f 100644 > --- a/log.c > +++ b/log.c > @@ -40,12 +40,21 @@ static size_t log_written; /* Currently used bytes i= n log file */ > static size_t log_cut_size; /* Bytes to cut at start on rotation */ > static char log_header[BUFSIZ]; /* File header, written back on cuts */ > =20 > -static time_t log_start; /* Start timestamp */ > +static struct timespec log_start; /* Start timestamp */ > =20 > int log_trace; /* --trace mode enabled */ > bool log_conf_parsed; /* Logging options already parsed */ > bool log_runtime; /* Daemonised, or ready in foreground */ > =20 > +/** > + * logtime_fmt_and_arg() - Build format and arguments to print relative = log time > + * @x: Current timestamp > + */ > +#define logtime_fmt_and_arg(x) \ > + "%lli.%04lli", \ > + ((long long int)(x)->tv_sec - log_start.tv_sec), \ > + (((long long int)(x)->tv_nsec - log_start.tv_nsec) / (100L * 1000)) This doesn't look right. If x->tv_nsec - log_start.tv_nsec is negative it will produce weird results. Instead you need more complex logic to carry a sufficient difference in the nsec over into the seconds difference. --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --lam2Z43GStz+n+PV Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmahx2MACgkQzQJF27ox 2GcOXQ/+JHJDSuoClWlNRTBRZ0fdjnQ3BLynr55VowrBxVrOWxvmiAuZPfc3BVQf i6C6kGUlXUpEYvkTqTwnLeMBKpv2nZLW4wazKfDvQPIdW+Ccrpf31Ko050XjzATH RJUknyUNufhuq1snJoVwUfGNxNg4CNoa2lrNvnrlgVkPL/h8ZVo/GkZXZcw9TJ4M hMGHX94mOFl6Bq6uYkZhFdQQquMNlf/6H/TBK3aMrG+1RwrJ/7JyOi2tNBZyD6sN fXQrPPZ7PrTQo9MV2Q+DqNqMEHueocssbMc/t2ZrY3FDSdedCYkcD+4+HXg+y9RI DZ3ffbNHJSwcLt2doWUwhK0G9Mf5B3UTlqviw6z/zSWan8kHOIQGYLdUlGeabIK5 cal/4e3wcpaSASw9XQnrMyn0J8EPoyaeadlBiQIggK+4GCU6K2L3JpQ20t6YZhCK CTqcZorbdVFOlIY7HQfiNsK3xnDlmWoLCAf5VdW+rq+AHQk2c8CA+Y75SbMFFTOK a3xaql//E/cU44PhJWTNL+fa7R6a1R+exV4pHbDrxrYDBvYuqQz1HaidAaR82BKt sV2tcHe5aGBIiXX0nuSH5HPmw6iMv/PZgZ7lkE+D+mFoU0/0peJy0SLx2Ox3j7vU SpAmGPC7S9+YrS4DeOUJOrew+eebFUDMgFTOJMYq0g7KeCnI6m0= =ARgw -----END PGP SIGNATURE----- --lam2Z43GStz+n+PV--