Browse Source

delay: use parse_ts for parsing delay value

* delay.c (fill_delay_data): Change intval argument to struct timespec
*val, assign val to ts.
* delay.h (fill_delay_data): Update function declaration.
* filter_qualify.c (parse_delay_token): Parse input with parse_ts,
supply the resulting struct timespec to fill_delay_data.
* tests/delay.c (check_): New function for providing diagnostic in case
of check failure.
(check_delay): Use it.
* tests/delay.test: Check new delay syntax.
Eugene Syromyatnikov 1 year ago
parent
commit
6b5a9d30b9
5 changed files with 40 additions and 23 deletions
  1. 2
    3
      delay.c
  2. 1
    1
      delay.h
  3. 4
    3
      filter_qualify.c
  4. 21
    11
      tests/delay.c
  5. 12
    5
      tests/delay.test

+ 2
- 3
delay.c View File

@@ -46,7 +46,7 @@ alloc_delay_data(void)
46 46
 }
47 47
 
48 48
 void
49
-fill_delay_data(uint16_t delay_idx, int intval, bool isenter)
49
+fill_delay_data(uint16_t delay_idx, struct timespec *val, bool isenter)
50 50
 {
51 51
 	if (delay_idx >= delay_data_vec_size)
52 52
 		error_func_msg_and_die("delay_idx >= delay_data_vec_size");
@@ -57,8 +57,7 @@ fill_delay_data(uint16_t delay_idx, int intval, bool isenter)
57 57
 	else
58 58
 		ts = &(delay_data_vec[delay_idx].ts_exit);
59 59
 
60
-	ts->tv_sec = intval / 1000000;
61
-	ts->tv_nsec = intval % 1000000 * 1000;
60
+	*ts = *val;
62 61
 }
63 62
 
64 63
 static bool

+ 1
- 1
delay.h View File

@@ -9,7 +9,7 @@
9 9
 # define STRACE_DELAY_H
10 10
 
11 11
 uint16_t alloc_delay_data(void);
12
-void fill_delay_data(uint16_t delay_idx, int intval, bool isenter);
12
+void fill_delay_data(uint16_t delay_idx, struct timespec *val, bool isenter);
13 13
 bool is_delay_timer_armed(void);
14 14
 void delay_timer_expired(void);
15 15
 void arm_delay_timer(const struct tcb *);

+ 4
- 3
filter_qualify.c View File

@@ -99,14 +99,15 @@ parse_delay_token(const char *input, struct inject_opts *fopts, bool isenter)
99 99
 
100 100
        if (fopts->data.flags & flag) /* duplicate */
101 101
                return false;
102
-       long long intval = string_to_ulonglong(input);
103
-       if (intval < 0) /* couldn't parse */
102
+       struct timespec tsval;
103
+
104
+       if (parse_ts(input, &tsval) < 0) /* couldn't parse */
104 105
                return false;
105 106
 
106 107
        if (fopts->data.delay_idx == (uint16_t) -1)
107 108
                fopts->data.delay_idx = alloc_delay_data();
108 109
        /* populate .ts_enter or .ts_exit */
109
-       fill_delay_data(fopts->data.delay_idx, intval, isenter);
110
+       fill_delay_data(fopts->data.delay_idx, &tsval, isenter);
110 111
        fopts->data.flags |= flag;
111 112
 
112 113
        return true;

+ 21
- 11
tests/delay.c View File

@@ -8,6 +8,7 @@
8 8
  */
9 9
 
10 10
 #include "tests.h"
11
+#include <inttypes.h>
11 12
 #include <limits.h>
12 13
 #include <stdio.h>
13 14
 #include <stdint.h>
@@ -30,6 +31,22 @@ usecs_from_ts(const struct timespec *const ts)
30 31
     return (int64_t) ts->tv_sec * 1000000 + ts->tv_nsec / 1000;
31 32
 }
32 33
 
34
+static void
35
+check_(const int64_t got, const bool ge, const int64_t orig, const int nproc,
36
+       const int exitcode)
37
+{
38
+	const int64_t thresh = (orig * (ge ? nproc - 1 : nproc + 1)) / nproc;
39
+
40
+	if (ge ? got >= thresh : got <= thresh)
41
+		return;
42
+
43
+	fprintf(stderr, "Got delay of %" PRId64 ", %s than threshold value of "
44
+			"%" PRId64 " (expected nominal delay value is %" PRId64
45
+			")\n", got, ge ? "less" : "more", thresh, orig);
46
+
47
+	_exit(exitcode);
48
+}
49
+
33 50
 static void
34 51
 check_delay(const struct timeval *const tv0,
35 52
 	    const struct timespec *const ts,
@@ -42,17 +59,10 @@ check_delay(const struct timeval *const tv0,
42 59
 	const int64_t us  = usecs_from_ts(ts);
43 60
 	const int64_t us1 = usecs_from_tv(tv1);
44 61
 
45
-	if (us - us0 < delay_exit * (nproc - 1) / nproc)
46
-		_exit(1);
47
-
48
-	if (us - us0 > delay_exit * (nproc + 1) / nproc)
49
-		_exit(2);
50
-
51
-	if (us1 - us < delay_enter * (nproc - 1) / nproc)
52
-		_exit(3);
53
-
54
-	if (us1 - us > delay_enter * (nproc + 1) / nproc)
55
-		_exit(4);
62
+	check_(us - us0, true,  delay_exit,  nproc, 1);
63
+	check_(us - us0, false, delay_exit,  nproc, 2);
64
+	check_(us1 - us, true,  delay_enter, nproc, 3);
65
+	check_(us1 - us, false, delay_enter, nproc, 4);
56 66
 }
57 67
 
58 68
 static void

+ 12
- 5
tests/delay.test View File

@@ -9,8 +9,15 @@
9 9
 
10 10
 . "${srcdir=.}/init.sh"
11 11
 
12
-delay_enter=800000
13
-delay_exit=1600000
14
-run_strace -f -r -egettimeofday \
15
-	-einject=gettimeofday:delay_enter=$delay_enter:delay_exit=$delay_exit \
16
-	../delay 4 $delay_enter $delay_exit
12
+while read -r denter dexit denter_us dexit_us; do
13
+	[ -n "$denter" ] || continue
14
+
15
+	run_strace -f -r -egettimeofday \
16
+		-einject=gettimeofday:delay_enter=$denter:delay_exit=$dexit \
17
+		../delay 4 $denter_us $dexit_us
18
+done <<-EOF
19
+	800000 1600000  800000 1600000
20
+	8e5    1.6s     800000 1600000
21
+	800ms  1.6e+6us 800000 1600000
22
+	+8e8ns .16E7    800000 1600000
23
+EOF

Loading…
Cancel
Save