public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Richard Fitzgerald <rf@opensource.cirrus.com>
To: <brendan.higgins@linux.dev>, <davidgow@google.com>, <rmoar@google.com>
Cc: <linux-kselftest@vger.kernel.org>, <kunit-dev@googlegroups.com>,
	<linux-kernel@vger.kernel.org>, <patches@opensource.cirrus.com>,
	Richard Fitzgerald <rf@opensource.cirrus.com>
Subject: [PATCH v3 7/7] kunit: Don't waste first attempt to format string in kunit_log_append()
Date: Wed, 9 Aug 2023 16:54:38 +0100	[thread overview]
Message-ID: <20230809155438.22470-8-rf@opensource.cirrus.com> (raw)
In-Reply-To: <20230809155438.22470-1-rf@opensource.cirrus.com>

It's wasteful to call vsnprintf() only to figure out the length of the
string. The string might fit in the available buffer space but we have to
vsnprintf() again to do that.

Instead, attempt to format the string to the available buffer at the same
time as finding the string length. Only if the string didn't fit the
available space is it necessary to extend the log and format the string
again to a new fragment buffer.

Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
---
 lib/kunit/test.c | 33 +++++++++++++++++----------------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 28d0048d6171..230ec5e9824f 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -196,11 +196,21 @@ void kunit_log_append(struct list_head *log, const char *fmt, ...)
 	if (!log)
 		return;
 
-	/* Evaluate length of line to add to log */
+	frag = list_last_entry(log, struct kunit_log_frag, list);
+	log_len = strlen(frag->buf);
+	len_left = sizeof(frag->buf) - log_len - 1;
+
+	/* Attempt to print formatted line to current fragment */
 	va_start(args, fmt);
-	len = vsnprintf(NULL, 0, fmt, args) + 1;
+	len = vsnprintf(frag->buf + log_len, len_left, fmt, args) + 1;
 	va_end(args);
 
+	if (len <= len_left)
+		goto out_newline;
+
+	/* Abandon the truncated result of vsnprintf */
+	frag->buf[log_len] = '\0';
+
 	if (len > sizeof(frag->buf) - 1) {
 		va_start(args, fmt);
 		tmp = kvasprintf(GFP_KERNEL, fmt, args);
@@ -212,24 +222,15 @@ void kunit_log_append(struct list_head *log, const char *fmt, ...)
 		return;
 	}
 
-	frag = list_last_entry(log, struct kunit_log_frag, list);
-	log_len = strlen(frag->buf);
-	len_left = sizeof(frag->buf) - log_len - 1;
-
-	if (len > len_left) {
-		frag = kunit_log_extend(log);
-		if (!frag)
-			return;
-
-		len_left = sizeof(frag->buf) - 1;
-		log_len = 0;
-	}
+	frag = kunit_log_extend(log);
+	if (!frag)
+		return;
 
 	/* Print formatted line to the log */
 	va_start(args, fmt);
-	vsnprintf(frag->buf + log_len, min(len, len_left), fmt, args);
+	vsnprintf(frag->buf, sizeof(frag->buf) - 1, fmt, args);
 	va_end(args);
-
+out_newline:
 	/* Add newline to end of log if not already present. */
 	kunit_log_newline(frag);
 }
-- 
2.30.2


  parent reply	other threads:[~2023-08-09 15:55 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-09 15:54 [PATCH v3 0/7] kunit: Add dynamically-extending log Richard Fitzgerald
2023-08-09 15:54 ` [PATCH v3 1/7] kunit: Replace fixed-size log with dynamically-extending buffer Richard Fitzgerald
2023-08-09 21:10   ` Rae Moar
2023-08-10 14:00     ` Richard Fitzgerald
2023-08-09 15:54 ` [PATCH v3 2/7] kunit: kunit-test: Add test cases for extending log buffer Richard Fitzgerald
2023-08-09 21:10   ` Rae Moar
2023-08-10 14:18     ` Richard Fitzgerald
2023-08-09 15:54 ` [PATCH v3 3/7] kunit: Handle logging of lines longer than the fragment buffer size Richard Fitzgerald
2023-08-10 14:38   ` David Gow
2023-08-10 15:09     ` Richard Fitzgerald
2023-08-11  8:27       ` David Gow
2023-08-10 22:41     ` Rae Moar
2023-08-09 15:54 ` [PATCH v3 4/7] kunit: kunit-test: Test logging a line that exactly fills a fragment Richard Fitzgerald
2023-08-09 21:22   ` Rae Moar
2023-08-10 14:24     ` Richard Fitzgerald
2023-08-09 15:54 ` [PATCH v3 5/7] kunit: kunit-test: Add test cases for logging very long lines Richard Fitzgerald
2023-08-10 22:58   ` Rae Moar
2023-08-09 15:54 ` [PATCH v3 6/7] kunit: kunit-test: Add test of logging only a newline Richard Fitzgerald
2023-08-10 23:02   ` Rae Moar
2023-08-09 15:54 ` Richard Fitzgerald [this message]
2023-08-10 23:53   ` [PATCH v3 7/7] kunit: Don't waste first attempt to format string in kunit_log_append() Rae Moar
2023-08-11  8:27   ` David Gow

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=20230809155438.22470-8-rf@opensource.cirrus.com \
    --to=rf@opensource.cirrus.com \
    --cc=brendan.higgins@linux.dev \
    --cc=davidgow@google.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=patches@opensource.cirrus.com \
    --cc=rmoar@google.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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox