[PATCH i-g-t 5/6] Change logic of ktap parser to run on a thread
Dominik Karol Piatkowski
dominik.karol.piatkowski at intel.com
Thu Apr 27 08:40:05 UTC 2023
The ktap parser should be listening and parsing messages as the tests
are executed, and not after the end of module load.
v1 -> v2:
- fix coding style
- remove usleep
- add error check logic
- follow the structure of igt_kselftests more closely
Signed-off-by: Dominik Karol Piątkowski <dominik.karol.piatkowski at intel.com>
Cc: Janusz Krzysztofik <janusz.krzysztofik at linux.intel.com>
Cc: Mauro Carvalho Chehab <mauro.chehab at linux.intel.com>
---
lib/igt_kmod.c | 98 ++++++++++++-------
lib/igt_ktap.c | 259 +++++++++++++++++++++++++++++++++++++++----------
lib/igt_ktap.h | 21 +++-
3 files changed, 287 insertions(+), 91 deletions(-)
diff --git a/lib/igt_kmod.c b/lib/igt_kmod.c
index 21e801bd..5a4862ac 100644
--- a/lib/igt_kmod.c
+++ b/lib/igt_kmod.c
@@ -758,10 +758,11 @@ int igt_kunit(const char *module_name, const char *opts)
{
struct igt_ktest tst;
struct kmod_module *kunit_kmod;
- char record[BUF_LEN + 1];
FILE *f;
bool is_builtin;
int ret;
+ struct ktap_test_results *results;
+ struct ktap_test_results_element *temp;
ret = IGT_EXIT_INVALID;
@@ -771,55 +772,78 @@ int igt_kunit(const char *module_name, const char *opts)
return ret;
}
- if (igt_ktest_begin(&tst) != 0) {
- igt_warn("Unable to begin ktest for %s\n", module_name);
+ igt_fixture {
+ if (igt_ktest_begin(&tst) != 0) {
+ igt_warn("Unable to begin ktest for %s\n", module_name);
- igt_ktest_fini(&tst);
- return ret;
- }
+ igt_ktest_fini(&tst);
+ return ret;
+ }
- if (tst.kmsg < 0) {
- igt_warn("Could not open /dev/kmsg\n");
- goto unload;
- }
+ if (tst.kmsg < 0) {
+ igt_warn("Could not open /dev/kmsg\n");
+ goto unload;
+ }
- if (lseek(tst.kmsg, 0, SEEK_END)) {
- igt_warn("Could not seek the end of /dev/kmsg\n");
- goto unload;
- }
+ if (lseek(tst.kmsg, 0, SEEK_END)) {
+ igt_warn("Could not seek the end of /dev/kmsg\n");
+ goto unload;
+ }
- f = fdopen(tst.kmsg, "r");
+ f = fdopen(tst.kmsg, "r");
- if (f == NULL) {
- igt_warn("Could not turn /dev/kmsg file descriptor into a FILE pointer\n");
- goto unload;
- }
+ if (f == NULL) {
+ igt_warn("Could not turn /dev/kmsg file descriptor into a FILE pointer\n");
+ goto unload;
+ }
- /* The KUnit module is required for running any KUnit tests */
- if (igt_kmod_load("kunit", NULL) != 0 ||
- kmod_module_new_from_name(kmod_ctx(), "kunit", &kunit_kmod) != 0) {
- igt_warn("Unable to load KUnit\n");
- igt_fail(IGT_EXIT_FAILURE);
- }
+ /* The KUnit module is required for running any KUnit tests */
+ if (igt_kmod_load("kunit", NULL) != 0 ||
+ kmod_module_new_from_name(kmod_ctx(), "kunit", &kunit_kmod) != 0) {
+ igt_warn("Unable to load KUnit\n");
+ igt_fail(IGT_EXIT_FAILURE);
+ }
- is_builtin = kmod_module_get_initstate(kunit_kmod) == KMOD_MODULE_BUILTIN;
+ is_builtin = kmod_module_get_initstate(kunit_kmod) == KMOD_MODULE_BUILTIN;
- if (igt_kmod_load(module_name, opts) != 0) {
- igt_warn("Unable to load %s module\n", module_name);
- igt_fail(IGT_EXIT_FAILURE);
- }
+ results = ktap_parser_start(f, is_builtin);
+
+ if (igt_kmod_load(module_name, opts) != 0) {
+ igt_warn("Unable to load %s module\n", module_name);
+ ret = ktap_parser_stop();
+ igt_fail(IGT_EXIT_FAILURE);
+ }
+
+ while (READ_ONCE(results->still_running))
+ if (READ_ONCE(results->head) != NULL) {
+ pthread_mutex_lock(&results->mutex);
+
+ igt_subtest(results->head->test_name) {
+ if (READ_ONCE(results->head->passed))
+ igt_success();
+ else
+ igt_fail(IGT_EXIT_FAILURE);
+ }
+
+ temp = results->head;
+ results->head = results->head->next;
+ free(temp);
- ret = igt_ktap_parser(f, record, is_builtin);
- if (ret != 0)
- ret = IGT_EXIT_ABORT;
+ pthread_mutex_unlock(&results->mutex);
+ }
unload:
- igt_ktest_end(&tst);
+ igt_ktest_end(&tst);
- igt_ktest_fini(&tst);
+ igt_ktest_fini(&tst);
+
+ ret = ktap_parser_stop();
- if (ret == 0)
- igt_success();
+ if (ret != 0)
+ ret = IGT_EXIT_ABORT;
+ if (ret == 0)
+ igt_success();
+ }
return ret;
}
diff --git a/lib/igt_ktap.c b/lib/igt_ktap.c
index 117598fa..3a396ea4 100644
--- a/lib/igt_ktap.c
+++ b/lib/igt_ktap.c
@@ -5,11 +5,17 @@
#include <ctype.h>
#include <limits.h>
+#include <libkmod.h>
+#include <pthread.h>
+#include <errno.h>
+#include <time.h>
#include "igt_aux.h"
#include "igt_core.h"
#include "igt_ktap.h"
+
+
static int log_to_end(enum igt_log_level level, FILE *f,
char *record, const char *format, ...) __attribute__((format(printf, 4, 5)));
@@ -30,6 +36,7 @@ static int log_to_end(enum igt_log_level level, FILE *f,
{
va_list args;
const char *lend;
+ int f_fd = fileno(f);
va_start(args, format);
igt_vlog(IGT_LOG_DOMAIN, level, format, args);
@@ -38,10 +45,25 @@ static int log_to_end(enum igt_log_level level, FILE *f,
lend = strchrnul(record, '\n');
while (*lend == '\0') {
igt_log(IGT_LOG_DOMAIN, level, "%s", record);
- if (fgets(record, BUF_LEN, f) == NULL) {
+
+ while (read(f_fd, record, BUF_LEN) < 0) {
+ if (errno == EINTR)
+ continue;
+
+ if (errno == EPIPE) {
+ igt_warn("kmsg truncated: too many messages. You may want to increase log_buf_len in kmcdline\n");
+ return -2;
+ }
+
+ if (errno == EAGAIN) {
+ igt_warn("kmsg read failed: no records available\n");
+ return -2;
+ }
+
igt_warn("kmsg truncated: unknown error (%m)\n");
return -2;
}
+
lend = strchrnul(record, '\n');
}
return 0;
@@ -96,6 +118,7 @@ static int find_next_tap_subtest(FILE *fp, char *record, bool is_builtin)
const char *test_lookup_str, *subtest_lookup_str, *name_rptr, *version_rptr;
char test_name[BUF_LEN + 1];
long test_count;
+ int fp_fd = fileno(fp);
test_name[0] = '\0';
test_name[BUF_LEN] = '\0';
@@ -117,7 +140,20 @@ static int find_next_tap_subtest(FILE *fp, char *record, bool is_builtin)
igt_info("%s", version_rptr);
- if (fgets(record, BUF_LEN, fp) == NULL) {
+ while (read(fp_fd, record, BUF_LEN) < 0) {
+ if (errno == EINTR)
+ continue;
+
+ if (errno == EPIPE) {
+ igt_warn("kmsg truncated: too many messages. You may want to increase log_buf_len in kmcdline\n");
+ return -2;
+ }
+
+ if (errno == EAGAIN) {
+ igt_warn("kmsg read failed: no records available\n");
+ return -2;
+ }
+
igt_warn("kmsg truncated: unknown error (%m)\n");
return -2;
}
@@ -139,10 +175,25 @@ static int find_next_tap_subtest(FILE *fp, char *record, bool is_builtin)
igt_info("Missing test name\n");
} else {
strncpy(test_name, name_rptr, BUF_LEN);
- if (fgets(record, BUF_LEN, fp) == NULL) {
+
+ while (read(fp_fd, record, BUF_LEN) < 0) {
+ if (errno == EINTR)
+ continue;
+
+ if (errno == EPIPE) {
+ igt_warn("kmsg truncated: too many messages. You may want to increase log_buf_len in kmcdline\n");
+ return -2;
+ }
+
+ if (errno == EAGAIN) {
+ igt_warn("kmsg read failed: no records available\n");
+ return -2;
+ }
+
igt_warn("kmsg truncated: unknown error (%m)\n");
return -2;
}
+
/* now we can be sure we found tests */
if (!is_builtin)
igt_info("KUnit is not built-in, skipping version check...\n");
@@ -253,6 +304,20 @@ static int parse_kmsg_for_tap(FILE *fp, char *record, char *test_name)
return 1;
}
+struct ktap_parser_args {
+ FILE *fp;
+ bool is_builtin;
+ volatile bool is_running;
+ int ret;
+} ktap_args;
+
+static struct ktap_test_results results;
+
+static double elapsed(const struct timespec *start, const struct timespec *end)
+{
+ return (end->tv_sec - start->tv_sec) + 1e-9*(end->tv_nsec - start->tv_nsec);
+}
+
/**
* igt_ktap_parser:
* @fp: FILE pointer
@@ -264,71 +329,159 @@ static int parse_kmsg_for_tap(FILE *fp, char *record, char *test_name)
*
* Returns: IGT default codes
*/
-int igt_ktap_parser(FILE *fp, char *record, bool is_builtin)
+void *igt_ktap_parser(void *unused)
{
+ FILE *fp = ktap_args.fp;
+ int fp_fd = fileno(fp);
+ char record[BUF_LEN + 1];
+ bool is_builtin = ktap_args.is_builtin;
+
+ struct ktap_test_results_element *r, *temp;
+ struct timespec last_kmsg, current;
+
char test_name[BUF_LEN + 1];
bool failed_tests, found_tests;
int sublevel = 0;
- test_name[0] = '\0';
- test_name[BUF_LEN] = '\0';
-
failed_tests = false;
found_tests = false;
- while (sublevel >= 0) {
- if (fgets(record, BUF_LEN, fp) == NULL) {
- if (!found_tests)
- igt_warn("kmsg truncated: unknown error (%m)\n");
- break;
- }
+ clock_gettime(CLOCK_MONOTONIC, &last_kmsg);
+ clock_gettime(CLOCK_MONOTONIC, ¤t);
- switch (find_next_tap_subtest(fp, record, is_builtin)) {
- case -2:
- /* no more data to read */
- return IGT_EXIT_FAILURE;
- case -1:
- /* no test found, so we keep parsing */
- break;
- case 0:
- /*
- * tests found, but they're missing info, so we might
- * have read into test output
- */
- found_tests = true;
- sublevel++;
- break;
- default:
- if (fgets(record, BUF_LEN, fp) == NULL) {
- igt_warn("kmsg truncated: unknown error (%m)\n");
- return -2;
+igt_ktap_parser_start:
+ while (ktap_args.is_running) {
+ test_name[0] = '\0';
+ test_name[BUF_LEN] = '\0';
+
+ if (read(fp_fd, record, BUF_LEN) < 0) {
+ clock_gettime(CLOCK_MONOTONIC, ¤t);
+ if (elapsed(&last_kmsg, ¤t) > 10)
+ results.still_running = false;
+
+ if (errno == EAGAIN)
+ /* No records available */
+ continue;
+
+ if (errno == EINTR)
+ continue;
+
+ if (errno == EPIPE) {
+ igt_warn("kmsg truncated: too many messages. You may want to increase log_buf_len in kmcdline\n");
+ continue;
}
- found_tests = true;
- sublevel++;
- break;
}
- switch (parse_kmsg_for_tap(fp, record, test_name)) {
- case -2:
- return IGT_EXIT_FAILURE;
- case -1:
- sublevel--;
- failed_tests = true;
- igt_subtest(test_name)
- igt_fail(IGT_EXIT_FAILURE);
- test_name[0] = '\0';
- break;
- case 0: /* fallthrough */
- igt_subtest(test_name)
- igt_success();
- test_name[0] = '\0';
- default:
- break;
+ clock_gettime(CLOCK_MONOTONIC, &last_kmsg);
+
+ while (sublevel >= 0) {
+ switch (find_next_tap_subtest(fp, record, is_builtin)) {
+ case -2:
+ /* no more data to read */
+ goto igt_ktap_parser_start;
+ case -1:
+ /* no test found, so we keep parsing */
+ break;
+ case 0:
+ /*
+ * tests found, but they're missing info, so we might
+ * have read into test output
+ */
+ found_tests = true;
+ sublevel++;
+ break;
+ default:
+ while (read(fp_fd, record, BUF_LEN) < 0) {
+ if (errno == EINTR)
+ continue;
+
+ if (errno == EAGAIN) {
+ igt_warn("kmsg read failed: no records available\n");
+ goto igt_ktap_parser_start;
+ }
+
+ if (errno == EPIPE) {
+ igt_warn("kmsg truncated: too many messages. You may want to increase log_buf_len in kmcdline\n");
+ goto igt_ktap_parser_start;
+ }
+
+ igt_warn("kmsg truncated: unknown error (%m)\n");
+ goto igt_ktap_parser_start;
+ }
+
+ found_tests = true;
+ sublevel++;
+ break;
+ }
+
+ switch (parse_kmsg_for_tap(fp, record, test_name)) {
+ case -2:
+ goto igt_ktap_parser_start;
+ case -1:
+ sublevel--;
+ failed_tests = true;
+
+ r = malloc(sizeof(*r));
+
+ memcpy(r->test_name, test_name, BUF_LEN);
+ r->test_name[BUF_LEN] = '\0';
+
+ r->passed = false;
+ r->next = NULL;
+
+ pthread_mutex_lock(&results.mutex);
+ if (results.head == NULL) {
+ results.head = r;
+ } else {
+ temp = results.head;
+ while (temp->next != NULL)
+ temp = temp->next;
+ temp->next = r;
+ }
+ pthread_mutex_unlock(&results.mutex);
+
+ test_name[0] = '\0';
+ break;
+ case 0: /* fallthrough */
+ igt_subtest(test_name)
+ igt_success();
+ test_name[0] = '\0';
+ default:
+ break;
+ }
}
+
+ if (sublevel < 0)
+ ktap_args.is_running = false;
}
if (failed_tests || !found_tests)
- return IGT_EXIT_FAILURE;
+ ktap_args.ret = IGT_EXIT_FAILURE;
+ else
+ ktap_args.ret = IGT_EXIT_SUCCESS;
+
+ return NULL;
+}
- return IGT_EXIT_SUCCESS;
+static pthread_t ktap_parser_thread;
+
+struct ktap_test_results *ktap_parser_start(FILE *fp, bool is_builtin)
+{
+ results.head = NULL;
+ pthread_mutex_init(&results.mutex, NULL);
+ results.still_running = true;
+
+ ktap_args.fp = fp;
+ ktap_args.is_builtin = is_builtin;
+ ktap_args.is_running = true;
+ pthread_create(&ktap_parser_thread, NULL, igt_ktap_parser, NULL);
+
+ return &results;
+}
+
+int ktap_parser_stop(void)
+{
+ ktap_args.is_running = false;
+ pthread_join(ktap_parser_thread, NULL);
+ return ktap_args.ret;
}
diff --git a/lib/igt_ktap.h b/lib/igt_ktap.h
index b2f69df2..34fe0957 100644
--- a/lib/igt_ktap.h
+++ b/lib/igt_ktap.h
@@ -26,6 +26,25 @@
#define BUF_LEN 4096
-int igt_ktap_parser(FILE *fp, char *record, bool is_builtin);
+#include <pthread.h>
+
+void *igt_ktap_parser(void *unused);
+
+typedef struct ktap_test_results_element {
+ char test_name[BUF_LEN + 1];
+ bool passed;
+ struct ktap_test_results_element *next;
+} ktap_test_results_element;
+
+struct ktap_test_results {
+ ktap_test_results_element *head;
+ pthread_mutex_t mutex;
+ bool still_running;
+};
+
+
+
+struct ktap_test_results *ktap_parser_start(FILE *fp, bool is_builtin);
+int ktap_parser_stop(void);
#endif /* IGT_KTAP_H */
--
2.34.1
More information about the Intel-gfx-trybot
mailing list