Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH i-g-t] lib/igt_core: buffer signal-safe log output per line
@ 2026-09-07  5:20 Jeevan B
  2026-09-07  6:59 ` ✓ i915.CI.BAT: success for " Patchwork
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Jeevan B @ 2026-09-07  5:20 UTC (permalink / raw)
  To: igt-dev; +Cc: Jeevan B

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.

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

diff --git a/lib/igt_core.c b/lib/igt_core.c
index af9d93762..e376a972c 100644
--- a/lib/igt_core.c
+++ b/lib/igt_core.c
@@ -2105,13 +2105,37 @@ 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 (e.g. forked children all dumping a backtrace after
+ * receiving the same fatal signal) interleave on the shared stderr/
+ * runner fd and produce unreadable, garbled logs.
+ */
+static char xputch_buf[256];
+static 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 +2190,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


^ permalink raw reply related	[flat|nested] 8+ messages in thread
* [PATCH i-g-t] lib/igt_core: buffer signal-safe log output per line
@ 2026-09-08  5:01 Jeevan B
  2026-09-09 17:28 ` Kamil Konieczny
  0 siblings, 1 reply; 8+ messages in thread
From: Jeevan B @ 2026-09-08  5:01 UTC (permalink / raw)
  To: igt-dev; +Cc: kamil.konieczny, Jeevan B

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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-09-09 17:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-07  5:20 [PATCH i-g-t] lib/igt_core: buffer signal-safe log output per line Jeevan B
2026-09-07  6:59 ` ✓ i915.CI.BAT: success for " Patchwork
2026-09-07  7:05 ` ✓ Xe.CI.BAT: " Patchwork
2026-09-07  8:28 ` ✗ Xe.CI.FULL: failure " Patchwork
2026-09-07  9:38 ` [PATCH i-g-t] " Kamil Konieczny
2026-09-07 12:41 ` ✗ i915.CI.Full: failure for " Patchwork
  -- strict thread matches above, loose matches on Subject: below --
2026-09-08  5:01 [PATCH i-g-t] " Jeevan B
2026-09-09 17:28 ` Kamil Konieczny

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox