Skip to contentSkip to conversation

lib: print recent log output if a test fails

PATCH In review Thomas Wood 2 participants 13 messages Dec 17, 2014 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 lib: print recent log output if a test fails

Print recent log entries if a test or subtest fails and the current log level is set to info.

v2: Write log to stderr after test or subtest failure and make log
buffering thread safe.

---
 lib/igt_core.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 96 insertions(+), 7 deletions(-)
lib/igt_core.c +96 -7
index 08e0c47..457b3b1 100644
@@ -50,6 +50,8 @@
 #include <termios.h>
 #include <errno.h>
 #include <time.h>
+#include <limits.h>
+#include <pthread.h>
 
 #include "drmtest.h"
 #include "intel_chipset.h"
@@ -228,8 +230,18 @@ enum {
  OPT_HELP = 'h'
 };
 
+static const char *command_str;
+static int igt_exitcode = IGT_EXIT_SUCCESS;
+
 static char* igt_log_domain_filter;
 
+static struct {
+	char *entries[256];
+	uint8_t start, end;
+} log_buffer;
+static pthread_mutex_t log_buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+
 __attribute__((format(printf, 1, 2)))
 static void kmsg(const char *format, ...)
 #define KERN_EMER	"<0>"
@@ -353,6 +365,52 @@ static void low_mem_killer_disable(bool disable)
 	chmod(adj_fname, buf.st_mode);
 }
 
+#define write_log_msg(x) write(STDERR_FILENO, x, strlen(x))
+
+/* this function must be safe to be called inside a signal handler */
+static void write_log(void)
+{
+	uint8_t i;
+	char *last_line;
+
+	pthread_mutex_lock(&log_buffer_mutex);
+
+	/* don't write an empty log */
+	if (log_buffer.start == log_buffer.end) {
+		pthread_mutex_unlock(&log_buffer_mutex);
+		return;
+	}
+
+	if (in_subtest) {
+		write_log_msg("--- Subtest ");
+		write_log_msg(in_subtest);
+	} else {
+		write_log_msg("--- Test ");
+		write_log_msg(command_str);
+	}
+	write_log_msg(" failed.\n");
+	write_log_msg("--- Log Start\n");
+
+	i = log_buffer.start;
+	do {
+		last_line = log_buffer.entries[i];
+		write_log_msg(last_line);
+
+		/* ensure each line ends in a newline */
+		for (int pos = 0; last_line[pos] != '\0'; pos++) {
+			if (last_line[pos] != '\n'
+			    && last_line[pos + 1] == '\0')
+				write_log_msg("\n");
+		}
+
+		i++;
+	} while (i != log_buffer.start && i != log_buffer.end);
+
+	write_log_msg("--- Log End\n");
+
+	pthread_mutex_unlock(&log_buffer_mutex);
+}
+
 bool igt_exit_called;
 static void common_exit_handler(int sig)
 {
@@ -361,6 +419,14 @@ static void common_exit_handler(int sig)
 	/* When not killed by a signal check that igt_exit() has been properly
 	 * called. */
 	assert(sig != 0 || igt_exit_called);
+
+	/* write the log out to a file if a signal was received or a simple
+	 * test failed (tests with subtests write the log after the subtest
+	 * failure) */
+	if (sig || (!test_with_subtests
+		    && igt_exitcode != IGT_EXIT_SUCCESS
+		    && igt_exitcode != IGT_EXIT_SKIP))
+		write_log();
 }
 
 static void print_test_description(void)
@@ -383,8 +449,6 @@ static void print_version(void)
 		uts.sysname, uts.release, uts.machine);
 }
 
-static const char *command_str;
-
 static void print_usage(const char *help_str, bool output_on_stderr)
 {
 	FILE *f = output_on_stderr ? stderr : stdout;
@@ -728,7 +792,6 @@ bool igt_only_list_subtests(void)
 static bool skipped_one = false;
 static bool succeeded_one = false;
 static bool failed_one = false;
-static int igt_exitcode = IGT_EXIT_SUCCESS;
 
 static void exit_subtest(const char *) __attribute__((noreturn));
 static void exit_subtest(const char *result)
@@ -862,6 +925,7 @@ void igt_fail(int exitcode)
 		exit(exitcode);
 
 	if (in_subtest) {
+		write_log();
 		if (exitcode == IGT_EXIT_TIMEOUT)
 			exit_subtest("TIMEOUT");
 		else
@@ -1460,6 +1524,7 @@ void igt_log(const char *domain, enum igt_log_level level, const char *format, .
 void igt_vlog(const char *domain, enum igt_log_level level, const char *format, va_list args)
 {
 	FILE *file;
+	char *line;
 	const char *program_name;
 	const char *igt_log_level_str[] = {
 		"DEBUG",
@@ -1479,16 +1544,37 @@ void igt_vlog(const char *domain, enum igt_log_level level, const char *format,
 	if (list_subtests)
 		return;
 
+	vasprintf(&line, format, args);
+
+	/* save log output when log level is info */
+	if (igt_log_level == IGT_LOG_INFO) {
+		pthread_mutex_lock(&log_buffer_mutex);
+
+		free(log_buffer.entries[log_buffer.end]);
+		asprintf(&log_buffer.entries[log_buffer.end],
+			 "(%s:%d) %s%s%s: %s",
+			 program_name, getpid(),
+			 (domain) ? domain : "",
+			 (domain) ? "-" : "",
+			 igt_log_level_str[level],
+			 line);
+		log_buffer.end++;
+		if (log_buffer.end == log_buffer.start)
+			log_buffer.start++;
+
+		pthread_mutex_unlock(&log_buffer_mutex);
+	}
+
 	if (igt_log_level > level)
-		return;
+		goto out;
 
 	if (igt_log_domain_filter) {
 		/* if null domain and filter is not "application", return */
 		if (!domain && strcmp(igt_log_domain_filter, "application"))
-			return;
+			goto out;
 		/* else if domain and filter do not match, return */
 		else if (domain && strcmp(igt_log_domain_filter, domain))
-			return;
+			goto out;
 	}
 
 	if (level == IGT_LOG_WARN) {
@@ -1503,7 +1589,10 @@ void igt_vlog(const char *domain, enum igt_log_level level, const char *format,
 			(domain) ? domain : "", (domain) ? "-" : "",
 			igt_log_level_str[level]);
 	}
-	vfprintf(file, format, args);
+	fwrite(line, sizeof(char), strlen(line), file);
+
+out:
+	free(line);
 }
 
 static void igt_alarm_handler(int signal)
Signature · 6 lines
2.1.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

PATCH 2/5 lib: add a critical warning level

---
 lib/igt_core.c | 10 ++++++----
 lib/igt_core.h | 10 ++++++++++
 2 files changed, 16 insertions(+), 4 deletions(-)
lib/igt_core.c +6 -4
index 457b3b1..6159a8c 100644
@@ -1494,9 +1494,10 @@ void igt_skip_on_simulation(void)
  * between SUCESS and FAILURE.
  *
  * The log level can be set through the IGT_LOG_LEVEL environment variable with
- * values "debug", "info", "warn" and "none". By default verbose debug message
- * are disabled. "none" completely disables all output and is not recommended
- * since crucial issues only reported at the IGT_LOG_WARN level are ignored.
+ * values "debug", "info", "warn", "critical" and "none". By default verbose
+ * debug message are disabled. "none" completely disables all output and is not
+ * recommended since crucial issues only reported at the IGT_LOG_WARN level are
+ * ignored.
  */
 void igt_log(const char *domain, enum igt_log_level level, const char *format, ...)
 {
@@ -1530,6 +1531,7 @@ void igt_vlog(const char *domain, enum igt_log_level level, const char *format,
 		"DEBUG",
 		"INFO",
 		"WARNING",
+		"CRITICAL",
 		"NONE"
 	};
 
@@ -1577,7 +1579,7 @@ void igt_vlog(const char *domain, enum igt_log_level level, const char *format,
 			goto out;
 	}
 
-	if (level == IGT_LOG_WARN) {
+	if (level > IGT_LOG_WARN) {
 		file = stderr;
 		fflush(stdout);
 	}
lib/igt_core.h +10 -0
index 5c5ee25..f184d75 100644
@@ -520,6 +520,7 @@ enum igt_log_level {
 	IGT_LOG_DEBUG,
 	IGT_LOG_INFO,
 	IGT_LOG_WARN,
+	IGT_LOG_CRITICAL,
 	IGT_LOG_NONE,
 };
 __attribute__((format(printf, 3, 4)))
@@ -550,6 +551,15 @@ void igt_vlog(const char *domain, enum igt_log_level level, const char *format,
  * Wrapper for igt_log() for message at the IGT_LOG_WARN level.
  */
 #define igt_warn(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_WARN, f)
+
+/**
+ * igt_critical:
+ * @...: format string and optional arguments
+ *
+ * Wrapper for igt_log() for message at the IGT_LOG_CRITICAL level.
+ */
+#define igt_critical(f...) igt_log(IGT_LOG_DOMAIN, IGT_LOG_CRITICAL, f)
+
 extern enum igt_log_level igt_log_level;
 
 /**
Signature · 6 lines
2.1.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw

PATCH 3/5 lib: use critical log level for assertion failure messages

In reply to Thomas Wood
---
 lib/igt_core.c | 19 ++++++++-----------
 lib/igt_core.h | 21 +++++++++++----------
 2 files changed, 19 insertions(+), 21 deletions(-)
lib/igt_core.c +8 -11
index 6159a8c..b98cb41 100644
@@ -951,27 +951,24 @@ static bool run_under_gdb(void)
 		strncmp(basename(buf), "gdb", 3) == 0);
 }
 
-void __igt_fail_assert(int exitcode, const char *file,
+void __igt_fail_assert(int exitcode, const char *domain, const char *file,
 		       const int line, const char *func, const char *assertion,
 		       const char *f, ...)
 {
 	va_list args;
 	int err = errno;
-	char *err_str = NULL;
 
+	igt_log(domain, IGT_LOG_CRITICAL,
+		"Test assertion failure function %s, file %s:%i:\n", func, file,
+		line);
+	igt_log(domain, IGT_LOG_CRITICAL, "Failed assertion: %s\n", assertion);
 	if (err)
-		asprintf(&err_str, "Last errno: %i, %s\n", err, strerror(err));
-
-	printf("Test assertion failure function %s, file %s:%i:\n"
-	       "Failed assertion: %s\n"
-	       "%s",
-	       func, file, line, assertion, err_str ?: "");
-
-	free(err_str);
+		igt_log(domain, IGT_LOG_CRITICAL, "Last errno: %i, %s\n", err,
+			strerror(err));
 
 	if (f) {
 		va_start(args, f);
-		vprintf(f, args);
+		igt_vlog(domain, IGT_LOG_CRITICAL, f, args);
 		va_end(args);
 	}
 
lib/igt_core.h +11 -10
index f184d75..bc4ba0a 100644
@@ -38,6 +38,11 @@
 #include <stdarg.h>
 #include <getopt.h>
 
+#ifndef IGT_LOG_DOMAIN
+#define IGT_LOG_DOMAIN (NULL)
+#endif
+
+
 extern const char* __igt_test_description __attribute__((weak));
 
 /**
@@ -215,8 +220,8 @@ void __igt_skip_check(const char *file, const int line,
 void igt_success(void);
 
 void igt_fail(int exitcode) __attribute__((noreturn));
-__attribute__((format(printf, 6, 7)))
-void __igt_fail_assert(int exitcode, const char *file,
+__attribute__((format(printf, 7, 8)))
+void __igt_fail_assert(int exitcode, const char *domain, const char *file,
 		       const int line, const char *func, const char *assertion,
 		       const char *format, ...)
 	__attribute__((noreturn));
@@ -232,7 +237,7 @@ void igt_exit(void) __attribute__((noreturn));
  */
 #define igt_assert(expr) \
 	do { if (!(expr)) \
-		__igt_fail_assert(99, __FILE__, __LINE__, __func__, #expr , NULL); \
+		__igt_fail_assert(99, IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, #expr , NULL); \
 	} while (0)
 
 /**
@@ -249,7 +254,7 @@ void igt_exit(void) __attribute__((noreturn));
  */
 #define igt_assert_f(expr, f...) \
 	do { if (!(expr)) \
-		__igt_fail_assert(99, __FILE__, __LINE__, __func__, #expr , f); \
+		__igt_fail_assert(99, IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, #expr , f); \
 	} while (0)
 
 /**
@@ -294,7 +299,7 @@ void igt_exit(void) __attribute__((noreturn));
 	do { \
 		int __n1 = (n1), __n2 = (n2); \
 		if (__n1 cmp __n2) ; else \
-		__igt_fail_assert(99, __FILE__, __LINE__, __func__, \
+		__igt_fail_assert(99, IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \
 				  #n1 " " #cmp " " #n2, \
 				  "error: %d " #ncmp " %d\n", __n1, __n2); \
 	} while (0)
@@ -303,7 +308,7 @@ void igt_exit(void) __attribute__((noreturn));
 	do { \
 		uint32_t __n1 = (n1), __n2 = (n2); \
 		if (__n1 cmp __n2) ; else \
-		__igt_fail_assert(99, __FILE__, __LINE__, __func__, \
+		__igt_fail_assert(99, IGT_LOG_DOMAIN, __FILE__, __LINE__, __func__, \
 				  #n1 " " #cmp " " #n2, \
 				  "error: %#x " #ncmp " %#x\n", __n1, __n2); \
 	} while (0)
@@ -512,10 +517,6 @@ bool igt_run_in_simulation(void);
 void igt_skip_on_simulation(void);
 
 /* structured logging */
-#ifndef IGT_LOG_DOMAIN
-#define IGT_LOG_DOMAIN (NULL)
-#endif
-
 enum igt_log_level {
 	IGT_LOG_DEBUG,
 	IGT_LOG_INFO,
Signature · 6 lines
2.1.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#3 lore raw

PATCH 4/5 lib: clear log buffer after writing it out or starting a new subtest

In reply to Thomas Wood
---
 lib/igt_core.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
lib/igt_core.c +21 -0
index b98cb41..925d78c 100644
@@ -366,6 +366,23 @@ static void low_mem_killer_disable(bool disable)
 }
 
 #define write_log_msg(x) write(STDERR_FILENO, x, strlen(x))
+static void clear_log(void)
+{
+	uint8_t i;
+
+	pthread_mutex_lock(&log_buffer_mutex);
+
+	i = 0;
+	do {
+		free(log_buffer.entries[i]);
+		log_buffer.entries[i] = NULL;
+		i++;
+	} while (i != 0);
+	log_buffer.start = 0;
+	log_buffer.end = 0;
+
+	pthread_mutex_unlock(&log_buffer_mutex);
+}
 
 /* this function must be safe to be called inside a signal handler */
 static void write_log(void)
@@ -409,6 +426,8 @@ static void write_log(void)
 	write_log_msg("--- Log End\n");
 
 	pthread_mutex_unlock(&log_buffer_mutex);
+
+	clear_log();
 }
 
 bool igt_exit_called;
@@ -761,6 +780,8 @@ bool __igt_run_subtest(const char *subtest_name)
 		return false;
 	}
 
+	clear_log();
+
 	kmsg(KERN_INFO "%s: starting subtest %s\n", command_str, subtest_name);
 
 	gettime(&subtest_time);
Signature · 6 lines
2.1.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#4 lore raw

Re: [PATCH i-g-t 4/5] lib: clear log buffer after writing it out or starting a new subtest

Quoted 19 lines · Thomas Wood
---
 lib/igt_core.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
lib/igt_core.c +6 -0
index b98cb41..925d78c 100644
@@ -366,6 +366,23 @@ static void low_mem_killer_disable(bool disable)
 }
 
 #define write_log_msg(x) write(STDERR_FILENO, x, strlen(x))
+static void clear_log(void)
+{
+	uint8_t i;
+
+	pthread_mutex_lock(&log_buffer_mutex);
+

lib/igt_core.c static void low_mem_killer_disable(bool disable) · line 384

Quoted 10 lines
lib/igt_core.c +10 -0
+	i = 0;
+	do {
+		free(log_buffer.entries[i]);
+		log_buffer.entries[i] = NULL;
+		i++;
+	} while (i != 0);
+	log_buffer.start = 0;
+	log_buffer.end = 0;
+
+	pthread_mutex_unlock(&log_buffer_mutex);

Too much work. Just log_buffer.start = log_buffer.end under the mutex. The freeing of old entries will be done by new writers, or by process termination.
-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#5 lore raw

PATCH 5/5 lib: print a message when receiving a handled signal

In reply to Thomas Wood
---
 lib/igt_core.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
lib/igt_core.c +19 -0
index 925d78c..1307fa8 100644
@@ -366,6 +366,7 @@ static void low_mem_killer_disable(bool disable)
 }
 
 #define write_log_msg(x) write(STDERR_FILENO, x, strlen(x))
+#define write_log_char(x) write(STDERR_FILENO, &x, 1)
 static void clear_log(void)
 {
 	uint8_t i;
@@ -439,6 +440,24 @@ static void common_exit_handler(int sig)
 	 * called. */
 	assert(sig != 0 || igt_exit_called);
 
+	if (sig) {
+		int sigtmp;
+		char c;
+		int start = 1;
+
+		write_log_msg("--- Received signal ");
+
+		/* write out the signal number, in a signal safe manner */
+		sigtmp = sig;
+		for (sigtmp = sig; sigtmp > 10; sigtmp /= 10)
+			start *= 10;
+		for (; start != 0; start /= 10) {
+			c = '0' + (sig / start) % 10;
+			write_log_char(c);
+		}
+		write_log_msg("\n");
+	}
+
 	/* write the log out to a file if a signal was received or a simple
 	 * test failed (tests with subtests write the log after the subtest
 	 * failure) */
Signature · 6 lines
2.1.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#6 lore raw

Re: [PATCH i-g-t 5/5] lib: print a message when receiving a handled signal

Quoted 28 lines · Thomas Wood
---
 lib/igt_core.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
lib/igt_core.c +8 -0
index 925d78c..1307fa8 100644
@@ -366,6 +366,7 @@ static void low_mem_killer_disable(bool disable)
 }
 
 #define write_log_msg(x) write(STDERR_FILENO, x, strlen(x))
+#define write_log_char(x) write(STDERR_FILENO, &x, 1)
 static void clear_log(void)
 {
 	uint8_t i;
@@ -439,6 +440,24 @@ static void common_exit_handler(int sig)
 	 * called. */
 	assert(sig != 0 || igt_exit_called);
 
+	if (sig) {
+		int sigtmp;
+		char c;
+		int start = 1;
+
+		write_log_msg("--- Received signal ");
+

lib/igt_core.c static void common_exit_handler(int sig) · line 459

Quoted 10 lines
lib/igt_core.c +10 -0
+		/* write out the signal number, in a signal safe manner */
+		sigtmp = sig;
+		for (sigtmp = sig; sigtmp > 10; sigtmp /= 10)
+			start *= 10;
+		for (; start != 0; start /= 10) {
+			c = '0' + (sig / start) % 10;
+			write_log_char(c);
+		}
+		write_log_msg("\n");
+	}

char buf[] = "123\n";
int i = 3;
for (uint8_t s = sig; s; s /= 10)

	buf[--i] = '0' + s % 10;

write_log_msg("--- Received signal ");
write_log_msg(buf + i); // or combined
-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#7 lore raw
In reply to Thomas Wood
Quoted 67 lines · Thomas Wood

Print recent log entries if a test or subtest fails and the current log level is set to info.

v2: Write log to stderr after test or subtest failure and make log
buffering thread safe.

---
 lib/igt_core.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 96 insertions(+), 7 deletions(-)
lib/igt_core.c +32 -0
index 08e0c47..457b3b1 100644
@@ -50,6 +50,8 @@
 #include <termios.h>
 #include <errno.h>
 #include <time.h>
+#include <limits.h>
+#include <pthread.h>
 
 #include "drmtest.h"
 #include "intel_chipset.h"
@@ -228,8 +230,18 @@ enum {
  OPT_HELP = 'h'
 };
 
+static const char *command_str;
+static int igt_exitcode = IGT_EXIT_SUCCESS;
+
 static char* igt_log_domain_filter;
 
+static struct {
+	char *entries[256];
+	uint8_t start, end;
+} log_buffer;
+static pthread_mutex_t log_buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+
 __attribute__((format(printf, 1, 2)))
 static void kmsg(const char *format, ...)
 #define KERN_EMER	"<0>"
@@ -353,6 +365,52 @@ static void low_mem_killer_disable(bool disable)
 	chmod(adj_fname, buf.st_mode);
 }
 
+#define write_log_msg(x) write(STDERR_FILENO, x, strlen(x))
+
+/* this function must be safe to be called inside a signal handler */
+static void write_log(void)
+{
+	uint8_t i;
+	char *last_line;
+
+	pthread_mutex_lock(&log_buffer_mutex);
+
+	/* don't write an empty log */
+	if (log_buffer.start == log_buffer.end) {
+		pthread_mutex_unlock(&log_buffer_mutex);
+		return;
+	}
+
+	if (in_subtest) {
+		write_log_msg("--- Subtest ");
+		write_log_msg(in_subtest);
+	} else {

lib/igt_core.c static void low_mem_killer_disable(bool disable) · line 397

Quoted 10 lines
lib/igt_core.c +10 -0
+		write_log_msg("--- Test ");
+		write_log_msg(command_str);
+	}
+	write_log_msg(" failed.\n");
+	write_log_msg("--- Log Start\n");
+
+	i = log_buffer.start;
+	do {
+		last_line = log_buffer.entries[i];
+		write_log_msg(last_line);

What happens if nothing is in the log:
log_buffer.start == log_buffer.end == 0

last_line[pos] is a *NULL, as may strlen(NULL).

When something is in the log, start != end, so this can just be a simple

if (log_buffer.start == log_buffer.end)

	return;

i = log_buffer.start;
do {

lib/igt_core.c static void low_mem_killer_disable(bool disable) · line 404

Quoted 7 lines
lib/igt_core.c +7 -0
+
+		/* ensure each line ends in a newline */
+		for (int pos = 0; last_line[pos] != '\0'; pos++) {
+			if (last_line[pos] != '\n'
+			    && last_line[pos + 1] == '\0')
+				write_log_msg("\n");
+		}

pos = strlen(last_line);
if (pos > 0 && last_line[pos-1] != '\n')

	write_log_msg("\n");

if (i == log_buffer.end)

	break;

i++;
} while (1);

It's not the neatest of loops, but it should be effective.

lib/igt_core.c static void low_mem_killer_disable(bool disable) · line 409

Quoted 4 lines
lib/igt_core.c +4 -0
+		i++;
+	} while (i != log_buffer.start && i != log_buffer.end);
+
+	write_log_msg("--- Log End\n");

After it has dumped the log, do you want to reset the log_buffer? i.e. log_buffer.start = log_buffer.end;

lib/igt_core.c static void low_mem_killer_disable(bool disable) · line 409

Quoted 1 line
lib/igt_core.c +1 -0
+	pthread_mutex_unlock(&log_buffer_mutex);

I was worried by taking locks inside a signal handler, but this looks like it could be made safe, and with a bit more work, you can make the other half robust as well:

char *new, *old;

asprintf(&new,

	 "(%s:%d) %s%s%s: %s",
	 program_name, getpid(),
	 (domain) ? domain : "",
	 (domain) ? "-" : "",
	 igt_log_level_str[level],
	 line);

pthread_mutex_lock(&log_buffer_mutex);
old = log_buffer.entries[log_buffer.end];
log_bufffer.entries[log_buffer.end] = new;
if (++log_buffer.end == log_buffer.start)

	log_buffer.start++;

pthread_mutex_unlock(&log_buffer_mutex);

free(old);

i.e. only do the pointer head/tail assignment inside the lock to minimise exposure.
-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#8 lore raw
Quoted 1 line · Chris Wilson

char *new, *old;

sigset_t block_signals, save_signals;

Quoted 7 lines

asprintf(&new,

	 "(%s:%d) %s%s%s: %s",
	 program_name, getpid(),
	 (domain) ? domain : "",
	 (domain) ? "-" : "",
	 igt_log_level_str[level],
	 line);

sigfillset(&block_signals); pthread_sigmask(SIG_SETMASK, &block_signals, &save_signals);

Quoted 7 lines

pthread_mutex_lock(&log_buffer_mutex);
old = log_buffer.entries[log_buffer.end];
log_bufffer.entries[log_buffer.end] = new;
if (++log_buffer.end == log_buffer.start)

	log_buffer.start++;

pthread_mutex_unlock(&log_buffer_mutex);

pthread_sigmask(SIG_SETMASK, &save_signals, NULL);

Quoted 1 line

free(old);

That will at least stop SIGARLM and friends firing whilst we poke around inside log_buffer. It might make a test result unstable, but will prevent that same instability from causing a lockup of igt. -Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#9 lore raw
In reply to Chris Wilson
Quoted 82 lines · Chris Wilson
Quoted 67 lines · Thomas Wood

Print recent log entries if a test or subtest fails and the current log level is set to info.

v2: Write log to stderr after test or subtest failure and make log
buffering thread safe.

---
 lib/igt_core.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 96 insertions(+), 7 deletions(-)
lib/igt_core.c +32 -0
index 08e0c47..457b3b1 100644
@@ -50,6 +50,8 @@
 #include <termios.h>
 #include <errno.h>
 #include <time.h>
+#include <limits.h>
+#include <pthread.h>
 
 #include "drmtest.h"
 #include "intel_chipset.h"
@@ -228,8 +230,18 @@ enum {
  OPT_HELP = 'h'
 };
 
+static const char *command_str;
+static int igt_exitcode = IGT_EXIT_SUCCESS;
+
 static char* igt_log_domain_filter;
 
+static struct {
+     char *entries[256];
+     uint8_t start, end;
+} log_buffer;
+static pthread_mutex_t log_buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+
 __attribute__((format(printf, 1, 2)))
 static void kmsg(const char *format, ...)
 #define KERN_EMER    "<0>"
@@ -353,6 +365,52 @@ static void low_mem_killer_disable(bool disable)
      chmod(adj_fname, buf.st_mode);
 }
 
+#define write_log_msg(x) write(STDERR_FILENO, x, strlen(x))
+
+/* this function must be safe to be called inside a signal handler */
+static void write_log(void)
+{
+     uint8_t i;
+     char *last_line;
+
+     pthread_mutex_lock(&log_buffer_mutex);
+
+     /* don't write an empty log */
+     if (log_buffer.start == log_buffer.end) {
+             pthread_mutex_unlock(&log_buffer_mutex);
+             return;
+     }
+
+     if (in_subtest) {
+             write_log_msg("--- Subtest ");
+             write_log_msg(in_subtest);
+     } else {

lib/igt_core.c static void low_mem_killer_disable(bool disable) · line 397

Quoted 10 lines
lib/igt_core.c +10 -0
+             write_log_msg("--- Test ");
+             write_log_msg(command_str);
+     }
+     write_log_msg(" failed.\n");
+     write_log_msg("--- Log Start\n");
+
+     i = log_buffer.start;
+     do {
+             last_line = log_buffer.entries[i];
+             write_log_msg(last_line);

What happens if nothing is in the log:
log_buffer.start == log_buffer.end == 0

Quoted 6 lines

last_line[pos] is a *NULL, as may strlen(NULL).

When something is in the log, start != end, so this can just be a simple

if (log_buffer.start == log_buffer.end)

        return;

Yes, this check is done at the start of the function.

Quoted 11 lines

i = log_buffer.start;
do {

lib/igt_core.c static void low_mem_killer_disable(bool disable) · line 404

Quoted 7 lines
lib/igt_core.c +7 -0
+
+             /* ensure each line ends in a newline */
+             for (int pos = 0; last_line[pos] != '\0'; pos++) {
+                     if (last_line[pos] != '\n'
+                         && last_line[pos + 1] == '\0')
+                             write_log_msg("\n");
+             }
Quoted 3 lines

pos = strlen(last_line);
if (pos > 0 && last_line[pos-1] != '\n')

        write_log_msg("\n");

The for loop above was to avoid using strlen (which is not listed as signal safe), but strlen is being used in write_log_msg, so this would need further work to make it safe too.

Quoted 9 lines

if (i == log_buffer.end)

        break;

i++;
} while (1);

It's not the neatest of loops, but it should be effective.

Quoted 7 lines

lib/igt_core.c static void low_mem_killer_disable(bool disable) · line 409

Quoted 4 lines
lib/igt_core.c +4 -0
+             i++;
+     } while (i != log_buffer.start && i != log_buffer.end);
+
+     write_log_msg("--- Log End\n");

After it has dumped the log, do you want to reset the log_buffer? i.e. log_buffer.start = log_buffer.end;

Yes, this is added in patch 4 of this series.

Quoted 6 lines

lib/igt_core.c static void low_mem_killer_disable(bool disable) · line 409

Quoted 2 lines
lib/igt_core.c +1 -0
+     pthread_mutex_unlock(&log_buffer_mutex);

I was worried by taking locks inside a signal handler, but this looks like it could be made safe, and with a bit more work, you can make the other half robust as well:

Yes, this is problematic. It will probably be worth adding some library test cases to try and ensure the default signal handlers are as safe as possible.

Quoted 24 lines

char *new, *old;

asprintf(&new,

         "(%s:%d) %s%s%s: %s",
         program_name, getpid(),
         (domain) ? domain : "",
         (domain) ? "-" : "",
         igt_log_level_str[level],
         line);

pthread_mutex_lock(&log_buffer_mutex);
old = log_buffer.entries[log_buffer.end];
log_bufffer.entries[log_buffer.end] = new;
if (++log_buffer.end == log_buffer.start)

        log_buffer.start++;

pthread_mutex_unlock(&log_buffer_mutex);

free(old);

i.e. only do the pointer head/tail assignment inside the lock to minimise exposure.
-Chris

Quoted 6 lines

--
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

#10 lore raw
Quoted 83 lines · Thomas Wood
Quoted 82 lines · Chris Wilson
Quoted 67 lines · Thomas Wood

Print recent log entries if a test or subtest fails and the current log level is set to info.

v2: Write log to stderr after test or subtest failure and make log
buffering thread safe.

---
 lib/igt_core.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 96 insertions(+), 7 deletions(-)
lib/igt_core.c +32 -0
index 08e0c47..457b3b1 100644
@@ -50,6 +50,8 @@
 #include <termios.h>
 #include <errno.h>
 #include <time.h>
+#include <limits.h>
+#include <pthread.h>
 
 #include "drmtest.h"
 #include "intel_chipset.h"
@@ -228,8 +230,18 @@ enum {
  OPT_HELP = 'h'
 };
 
+static const char *command_str;
+static int igt_exitcode = IGT_EXIT_SUCCESS;
+
 static char* igt_log_domain_filter;
 
+static struct {
+     char *entries[256];
+     uint8_t start, end;
+} log_buffer;
+static pthread_mutex_t log_buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+
 __attribute__((format(printf, 1, 2)))
 static void kmsg(const char *format, ...)
 #define KERN_EMER    "<0>"
@@ -353,6 +365,52 @@ static void low_mem_killer_disable(bool disable)
      chmod(adj_fname, buf.st_mode);
 }
 
+#define write_log_msg(x) write(STDERR_FILENO, x, strlen(x))
+
+/* this function must be safe to be called inside a signal handler */
+static void write_log(void)
+{
+     uint8_t i;
+     char *last_line;
+
+     pthread_mutex_lock(&log_buffer_mutex);
+
+     /* don't write an empty log */
+     if (log_buffer.start == log_buffer.end) {
+             pthread_mutex_unlock(&log_buffer_mutex);
+             return;
+     }
+
+     if (in_subtest) {
+             write_log_msg("--- Subtest ");
+             write_log_msg(in_subtest);
+     } else {

lib/igt_core.c static void low_mem_killer_disable(bool disable) · line 397

Quoted 10 lines
lib/igt_core.c +10 -0
+             write_log_msg("--- Test ");
+             write_log_msg(command_str);
+     }
+     write_log_msg(" failed.\n");
+     write_log_msg("--- Log Start\n");
+
+     i = log_buffer.start;
+     do {
+             last_line = log_buffer.entries[i];
+             write_log_msg(last_line);

What happens if nothing is in the log:
log_buffer.start == log_buffer.end == 0

Quoted 8 lines
Quoted 6 lines

last_line[pos] is a *NULL, as may strlen(NULL).

When something is in the log, start != end, so this can just be a simple

if (log_buffer.start == log_buffer.end)

        return;

Yes, this check is done at the start of the function.

But then won't you get different output if a subtest fails without actually adding a log message? Is that desirable? (At least my primary concern that there isn't a *NULL is addressed. And the looping logic is correct, but I think we aren't using as many entries as possible...)

Maybe give an example of how the extra logging works in practice? (Some before/after output of a failing igt test.) -Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#11 lore raw
In reply to Thomas Wood
Quoted 5 lines · Thomas Wood
Quoted 2 lines · Chris Wilson

After it has dumped the log, do you want to reset the log_buffer? i.e. log_buffer.start = log_buffer.end;

Yes, this is added in patch 4 of this series.

Does that actually cover the case of a multithreaded/forked test case with multiple failures? iirc, that can still generate multiple sets of failure reports. I thought patch 4 only cleared the log between tests. -Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#12 lore raw
In reply to Thomas Wood
Quoted 5 lines · Thomas Wood

Print recent log entries if a test or subtest fails and the current log level is set to info.

v2: Write log to stderr after test or subtest failure and make log
buffering thread safe.

More of a general style bikeshed:

Would log_write(), __log_write_msg(), __log_write_char(), log_clear() be better, for they all operate on the implicit log_buffer. -Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx