aboutsummaryrefslogtreecommitdiffstats
path: root/meta-agl-bsp/recipes-support/ptest-runner/ptest-runner/0001-utils.c-Prefer-monotonic-clock-to-calculate-elapsed-.patch
blob: 6ca593be5ed736f2e13ba2f4408eeb403cd92f26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
From 41b7f4814d39c1930b1fcf0be2e247a73546fb80 Mon Sep 17 00:00:00 2001
From: Jeffrey Pautler <jeffrey.pautler@ni.com>
Date: Tue, 31 Oct 2017 14:38:39 -0500
Subject: [PATCH 1/7] utils.c: Prefer monotonic clock to calculate elapsed time

The current implementation uses the system clock to calculate how long
a ptest has been running with no output. If a ptest changes the system
clock as part of the test, that can cause the current implementation
to falsely trigger a timeout or miss an actual timeout. It is
preferrable to use a monotonic clock for calculating elapsed time in
order to avoid these issues.

This change tries to use the monotonic clock first and falls back to
the realtime clock if the monotonic clock is not supported.

Signed-off-by: Jeffrey Pautler <jeffrey.pautler@ni.com>
---
 utils.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/utils.c b/utils.c
index 6d653887e9e4..933ecedf57e8 100644
--- a/utils.c
+++ b/utils.c
@@ -257,7 +257,8 @@ wait_child(const char *ptest_dir, const char *run_ptest, pid_t pid,
 		int timeout, int *fds, FILE **fps)
 {
 	struct pollfd pfds[2];
-	time_t sentinel;
+	struct timespec sentinel;
+	clockid_t clock = CLOCK_MONOTONIC;
 	int r;
 
 	int timeouted = 0;
@@ -269,7 +270,11 @@ wait_child(const char *ptest_dir, const char *run_ptest, pid_t pid,
 	pfds[1].fd = fds[1];
 	pfds[1].events = POLLIN;
 
-	sentinel = time(NULL);
+	if (clock_gettime(clock, &sentinel) == -1) {
+		clock = CLOCK_REALTIME;
+		clock_gettime(clock, &sentinel);
+	}
+
 	while (1) {
 		waitflags = WNOHANG;
 
@@ -288,11 +293,16 @@ wait_child(const char *ptest_dir, const char *run_ptest, pid_t pid,
 					fwrite(buf, n, 1, fps[1]);
 			}
 
-			sentinel = time(NULL);
-		} else if (timeout >= 0 && ((time(NULL) - sentinel) > timeout)) {
-			timeouted = 1;
-			kill(pid, SIGKILL);
-			waitflags = 0;
+			clock_gettime(clock, &sentinel);
+		} else if (timeout >= 0) {
+			struct timespec time;
+
+			clock_gettime(clock, &time);
+			if ((time.tv_sec - sentinel.tv_sec) > timeout) {
+				timeouted = 1;
+				kill(pid, SIGKILL);
+				waitflags = 0;
+			}
 		}
 
 		if (waitpid(pid, &status, waitflags) == pid)
-- 
2.11.0