From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 7A2135A004F for ; Mon, 29 Jul 2024 06:23:18 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1722226981; bh=iDvy5x+8/Sq2kZCz3t12XIP4WqrXY4fxJeELkiQG3GY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hUNCXKsBqV7Zv+lc5eEaoDVFQfsk1hwuESwGwK/3snuJF7ZjWZ6RsWNlUX/AuDuuT DnFDOJ9oKZ2/SeXqcTJ7AH1TkoZE7arNCUzI0kN/c301Ro7Y0zeoXla9I7aD9x2LC4 fDDGqzpJqU48oiwA0SoxnTi5Y2+3jk4uUywwkBIRZSezmd5sA+B0fcVtITIf5dHuoS ZCqMU0qQoOLICSGokp/PAennKEfsKfxUO739TelndEpGaUy746tglbDYO0QkTkV11+ UNrWiIj4gD599UNnkGadYcQffZI99xcuij7B7IxTH9rjN/sB7kdwfIBPu3mQGg0RVQ fGdNdX414MTLw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WXQFd74V9z4x7B; Mon, 29 Jul 2024 14:23:01 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 2/2] log: Avoid duplicate calls to clock_gettime() Date: Mon, 29 Jul 2024 14:23:00 +1000 Message-ID: <20240729042300.280684-3-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240729042300.280684-1-david@gibson.dropbear.id.au> References: <20240729042300.280684-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: VHVBV7ISSDLGHMW5US6IONBBLNG4Y7G6 X-Message-ID-Hash: VHVBV7ISSDLGHMW5US6IONBBLNG4Y7G6 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: David Gibson 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: We use clock_gettime() to get a timestamp for the log in two places: - in vlogmsg(), which is used only for debug_print messages - in logfile_write() which is only used messages to the log file These cases are mutually exclusive, so we don't ever print the same message with different timestamps, but that's not particularly obvious to see. Refactor to have a single clock_gettime() call in vlogmsg() and use it for all the places we need it. Signed-off-by: David Gibson --- log.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/log.c b/log.c index eb3a780a..418bdefd 100644 --- a/log.c +++ b/log.c @@ -199,19 +199,17 @@ static int logfile_rotate(int fd, const struct timespec *now) * logfile_write() - Write entry to log file, trigger rotation if full * @newline: Append newline at the end of the message, if missing * @pri: Facility and level map, same as priority for vsyslog() + * @tp: Timestamp * @format: Same as vsyslog() format * @ap: Same as vsyslog() ap */ -static void logfile_write(bool newline, int pri, const char *format, va_list ap) +static void logfile_write(bool newline, int pri, const struct timespec *tp, + const char *format, va_list ap) { - struct timespec now; char buf[BUFSIZ]; int n; - if (clock_gettime(CLOCK_MONOTONIC, &now)) - return; - - n = snprintf(buf, BUFSIZ, logtime_fmt_and_arg(&now)); + n = snprintf(buf, BUFSIZ, logtime_fmt_and_arg(tp)); n += snprintf(buf + n, BUFSIZ - n, ": %s", logfile_prefix[pri]); n += vsnprintf(buf + n, BUFSIZ - n, format, ap); @@ -219,7 +217,7 @@ static void logfile_write(bool newline, int pri, const char *format, va_list ap) if (newline && format[strlen(format)] != '\n') n += snprintf(buf + n, BUFSIZ - n, "\n"); - if ((log_written + n >= log_size) && logfile_rotate(log_file, &now)) + if ((log_written + n >= log_size) && logfile_rotate(log_file, tp)) return; if ((n = write(log_file, buf, n)) >= 0) @@ -238,8 +236,9 @@ void vlogmsg(bool newline, int pri, const char *format, va_list ap) bool debug_print = (log_mask & LOG_MASK(LOG_DEBUG)) && log_file == -1; struct timespec tp; + clock_gettime(CLOCK_MONOTONIC, &tp); + if (debug_print) { - clock_gettime(CLOCK_MONOTONIC, &tp); fprintf(stderr, logtime_fmt_and_arg(&tp)); fprintf(stderr, ": "); } @@ -249,7 +248,7 @@ void vlogmsg(bool newline, int pri, const char *format, va_list ap) va_copy(ap2, ap); /* Don't clobber ap, we need it again */ if (log_file != -1) - logfile_write(newline, pri, format, ap2); + logfile_write(newline, pri, &tp, format, ap2); else if (!(log_mask & LOG_MASK(LOG_DEBUG))) passt_vsyslog(newline, pri, format, ap2); -- 2.45.2