[ptest-runner][PATCH 1/2] Add support to specify timeout by ptest


Anibal Limon
 

The ptest-runner has a default timeout of 300 secs and can be override
usint -t option in the runner.

There is a need to specify timeout by ptest because not all ptests takes
the sametime also are affected by machine. So add support to read a file
inside ptest folder (timeout-ptest) and override global timeout with it.

For example in glib-2.0,

/usr/lib/glib-2.0/ptest/run-ptest
/usr/lib/glib-2.0/ptest/timeout-ptest

Signed-off-by: Aníbal Limón <anibal.limon@...>
---
main.c | 2 +-
ptest_list.c | 3 ++-
ptest_list.h | 3 ++-
utils.c | 69 +++++++++++++++++++++++++++++++++++++---------------
utils.h | 2 +-
5 files changed, 55 insertions(+), 24 deletions(-)

diff --git a/main.c b/main.c
index e3a1b69..467548e 100644
--- a/main.c
+++ b/main.c
@@ -158,7 +158,7 @@ main(int argc, char *argv[])
for (i = 0; i < opts.dirs_no; i ++) {
struct ptest_list *tmp;

- tmp = get_available_ptests(opts.dirs[i]);
+ tmp = get_available_ptests(opts.dirs[i], opts.timeout);
if (tmp == NULL) {
fprintf(stderr, PRINT_PTESTS_NOT_FOUND_DIR, opts.dirs[i]);
continue;
diff --git a/ptest_list.c b/ptest_list.c
index 917ef4f..b689670 100644
--- a/ptest_list.c
+++ b/ptest_list.c
@@ -166,7 +166,7 @@ ptest_list_search_by_file(struct ptest_list *head, char *run_ptest, struct stat
}

struct ptest_list *
-ptest_list_add(struct ptest_list *head, char *ptest, char *run_ptest)
+ptest_list_add(struct ptest_list *head, char *ptest, char *run_ptest, int timeout)
{
struct ptest_list *n, *p;

@@ -179,6 +179,7 @@ ptest_list_add(struct ptest_list *head, char *ptest, char *run_ptest)

n->ptest = ptest;
n->run_ptest = run_ptest;
+ n->timeout = timeout;

n->prev = NULL;
n->next = NULL;
diff --git a/ptest_list.h b/ptest_list.h
index 02a64bb..e583d9f 100644
--- a/ptest_list.h
+++ b/ptest_list.h
@@ -50,6 +50,7 @@
struct ptest_list {
char *ptest;
char *run_ptest;
+ int timeout;

struct ptest_list *next;
struct ptest_list *prev;
@@ -62,7 +63,7 @@ extern int ptest_list_free_all(struct ptest_list *);
extern int ptest_list_length(struct ptest_list *);
extern struct ptest_list *ptest_list_search(struct ptest_list *, char *);
extern struct ptest_list *ptest_list_search_by_file(struct ptest_list *, char *, struct stat);
-extern struct ptest_list *ptest_list_add(struct ptest_list *, char *, char *);
+extern struct ptest_list *ptest_list_add(struct ptest_list *, char *, char *, int);
extern struct ptest_list *ptest_list_remove(struct ptest_list *, char *, int);
extern struct ptest_list *ptest_list_extend(struct ptest_list *, struct ptest_list *);

diff --git a/utils.c b/utils.c
index 1a3c90f..424115f 100644
--- a/utils.c
+++ b/utils.c
@@ -84,9 +84,30 @@ check_allocation1(void *p, size_t size, char *file, int line, int exit_on_null)
}
}

+static inline char *
+get_ptest_file(char **ptest_file, struct stat *st_buf, const char *main_dir,
+ const char *ptest_dir, const char *file_name)
+{
+ if (asprintf(ptest_file, "%s/%s/ptest/%s",
+ main_dir, ptest_dir, file_name) == -1) {
+ return NULL;
+ }
+
+ if (stat(*ptest_file, st_buf) == -1) {
+ free(*ptest_file);
+ return NULL;
+ }
+
+ if (!S_ISREG(st_buf->st_mode)) {
+ free(*ptest_file);
+ return NULL;
+ }
+
+ return *ptest_file;
+}

struct ptest_list *
-get_available_ptests(const char *dir)
+get_available_ptests(const char *dir, int global_timeout)
{
struct ptest_list *head;
struct stat st_buf;
@@ -123,10 +144,11 @@ get_available_ptests(const char *dir)
break;
}

-
fail = 0;
for (i = 0; i < n; i++) {
char *run_ptest;
+ char *timeout_ptest;
+ int timeout;

char *d_name = strdup(namelist[i]->d_name);
CHECK_ALLOCATION(d_name, sizeof(namelist[i]->d_name), 0);
@@ -142,34 +164,38 @@ get_available_ptests(const char *dir)
continue;
}

- if (asprintf(&run_ptest, "%s/%s/ptest/run-ptest",
- realdir, d_name) == -1) {
- fail = 1;
+ if (get_ptest_file(&run_ptest, &st_buf, realdir, d_name, "run-ptest") == NULL) {
saved_errno = errno;
free(d_name);
- break;
- }
-
- if (stat(run_ptest, &st_buf) == -1) {
- free(run_ptest);
- free(d_name);
continue;
}

- if (!S_ISREG(st_buf.st_mode)) {
+ if (ptest_list_search_by_file(head, run_ptest, st_buf)) {
free(run_ptest);
free(d_name);
continue;
}

- if (ptest_list_search_by_file(head, run_ptest, st_buf)) {
- free(run_ptest);
- free(d_name);
- continue;
+ timeout = global_timeout;
+ if (get_ptest_file(&timeout_ptest, &st_buf, realdir, d_name, "timeout-ptest")) {
+ FILE *f = fopen(timeout_ptest, "r");
+
+ if (f == NULL) {
+ fail = 1;
+ saved_errno = errno;
+ free(run_ptest);
+ free(d_name);
+ free(timeout_ptest);
+ break;
+ }
+ fscanf(f, "%d", &timeout);
+ fclose(f);
+
+ free(timeout_ptest);
}

struct ptest_list *p = ptest_list_add(head,
- d_name, run_ptest);
+ d_name, run_ptest, timeout);
CHECK_ALLOCATION(p, sizeof(struct ptest_list *), 0);
if (p == NULL) {
fail = 1;
@@ -229,6 +255,7 @@ filter_ptests(struct ptest_list *head, char **ptests, int ptest_num)
for (i = 0; i < ptest_num; i++) {
char *ptest;
char *run_ptest;
+ int timeout;

n = ptest_list_search(head, ptests[i]);
if (n == NULL) {
@@ -239,13 +266,14 @@ filter_ptests(struct ptest_list *head, char **ptests, int ptest_num)

ptest = strdup(n->ptest);
run_ptest = strdup(n->run_ptest);
+ timeout = n->timeout;
if (ptest == NULL || run_ptest == NULL) {
saved_errno = errno;
fail = 1;
break;
}

- if (ptest_list_add(head_new, ptest, run_ptest) == NULL) {
+ if (ptest_list_add(head_new, ptest, run_ptest, timeout) == NULL) {
saved_errno = errno;
fail = 1;
break;
@@ -509,8 +537,7 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
fprintf(fp, "%s\n", get_stime(stime, GET_STIME_BUF_SIZE, sttime));
fprintf(fp, "BEGIN: %s\n", ptest_dir);

-
- status = wait_child(child, opts.timeout);
+ status = wait_child(child, p->timeout);

entime = time(NULL);
duration = entime - sttime;
@@ -528,6 +555,8 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,

fprintf(fp, "END: %s\n", ptest_dir);
fprintf(fp, "%s\n", get_stime(stime, GET_STIME_BUF_SIZE, entime));
+
+ free(ptest_dir);
}
PTEST_LIST_ITERATE_END
fprintf(fp, "STOP: %s\n", progname);
diff --git a/utils.h b/utils.h
index 39832e6..69940d0 100644
--- a/utils.h
+++ b/utils.h
@@ -45,7 +45,7 @@ struct ptest_options {


extern void check_allocation1(void *, size_t, char *, int, int);
-extern struct ptest_list *get_available_ptests(const char *);
+extern struct ptest_list *get_available_ptests(const char *, int);
extern int print_ptests(struct ptest_list *, FILE *);
extern struct ptest_list *filter_ptests(struct ptest_list *, char **, int);
extern int run_ptests(struct ptest_list *, const struct ptest_options,
--
2.31.0

Join yocto@lists.yoctoproject.org to automatically receive all group messages.