All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeevan B <jeevan.b@intel.com>
To: igt-dev@lists.freedesktop.org
Cc: kamil.konieczny@intel.com, Jeevan B <jeevan.b@intel.com>
Subject: [PATCH i-g-t] lib/igt_core: buffer signal-safe log output per line
Date: Tue,  8 Sep 2026 10:31:00 +0530	[thread overview]
Message-ID: <20260908050100.4034282-1-jeevan.b@intel.com> (raw)

xputch() wrote one byte at a time via write(STDERR_FILENO, &c, 1) from
the async-signal-safe backtrace printer. When a fatal signal hits
multiple processes/threads at once (e.g. forked test children all
dumping a backtrace after the same signal), their single-byte writes
interleave on the shared stderr/runner fd, producing garbled or
truncated output. This showed up as tests (e.g.
igt@kms_cursor_legacy@*) being reported as incomplete with no useful
logs.

Buffer output in xputch() and flush it with a single write() (or
log_to_runner_sig_safe() call) per line via a new xflush(), instead of
one syscall per character. xprintfmt() now also flushes on reaching
the end of the format string, so a trailing partial line without a
newline is still emitted.

The buffer is thread-local (__thread) so concurrent threads of the
same process don't race on it; plain TLS access remains
async-signal-safe, unlike a lock.

v2: Make the xputch buffer thread-local instead of a plain static.

Assisted-by: Claude:claude-sonnet-4-5
Signed-off-by: Jeevan B <jeevan.b@intel.com>
---
 lib/igt_core.c | 34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)

diff --git a/lib/igt_core.c b/lib/igt_core.c
index af9d93762..4efddb908 100644
--- a/lib/igt_core.c
+++ b/lib/igt_core.c
@@ -2105,13 +2105,40 @@ static void write_stderr(const char *str)
 #ifdef HAVE_LIBUNWIND
 static const char hex[] = "0123456789abcdef";
 
+/*
+ * Buffer output and flush it in one write() per line, instead of one
+ * write() per character. Concurrent single-byte writes from multiple
+ * processes/threads (e.g. forked children, or multiple threads of the
+ * same process, all dumping a backtrace after receiving the same
+ * fatal signal) interleave on the shared stderr/runner fd and produce
+ * unreadable, garbled logs. The buffer is thread-local so concurrent
+ * threads don't race on it; plain TLS access is async-signal-safe,
+ * unlike a lock.
+ */
+static __thread char xputch_buf[256];
+static __thread size_t xputch_buf_len;
+
 static void
-xputch(int c)
+xflush(void)
 {
+	if (!xputch_buf_len)
+		return;
+
 	if (runner_connected())
-		log_to_runner_sig_safe((const void *) &c, 1);
+		log_to_runner_sig_safe(xputch_buf, xputch_buf_len);
 	else
-		igt_ignore_warn(write(STDERR_FILENO, (const void *) &c, 1));
+		igt_ignore_warn(write(STDERR_FILENO, xputch_buf, xputch_buf_len));
+
+	xputch_buf_len = 0;
+}
+
+static void
+xputch(int c)
+{
+	xputch_buf[xputch_buf_len++] = c;
+
+	if (c == '\n' || xputch_buf_len == sizeof(xputch_buf))
+		xflush();
 }
 
 static int
@@ -2166,6 +2193,7 @@ xprintfmt(const char *fmt, va_list ap)
 	while (1) {
 		while ((ch = *(const unsigned char *) fmt++) != '%') {
 			if (ch == '\0') {
+				xflush();
 				return;
 			}
 			xputch(ch);
-- 
2.43.0


             reply	other threads:[~2026-09-08  5:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-08  5:01 Jeevan B [this message]
2026-09-08 16:11 ` ✓ Xe.CI.BAT: success for lib/igt_core: buffer signal-safe log output per line (rev2) Patchwork
2026-09-08 16:26 ` ✓ i915.CI.BAT: " Patchwork
2026-09-08 21:32 ` ✓ Xe.CI.FULL: " Patchwork
2026-09-09  6:57 ` ✗ i915.CI.Full: failure " Patchwork
2026-09-09 17:28 ` [PATCH i-g-t] lib/igt_core: buffer signal-safe log output per line Kamil Konieczny
  -- strict thread matches above, loose matches on Subject: below --
2026-09-07  5:20 Jeevan B
2026-09-07  9:38 ` Kamil Konieczny

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=20260908050100.4034282-1-jeevan.b@intel.com \
    --to=jeevan.b@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=kamil.konieczny@intel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.