1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/time64.h>
6 #include "time-utils.h"
12 static bool test__parse_nsec_time(const char *str
, u64 expected
)
17 pr_debug("\nparse_nsec_time(\"%s\")\n", str
);
19 err
= parse_nsec_time(str
, &ptime
);
21 pr_debug("error %d\n", err
);
25 if (ptime
!= expected
) {
26 pr_debug("Failed. ptime %" PRIu64
" expected %" PRIu64
"\n",
31 pr_debug("%" PRIu64
"\n", ptime
);
36 static bool test__perf_time__parse_str(const char *ostr
, u64 start
, u64 end
)
38 struct perf_time_interval ptime
;
41 pr_debug("\nperf_time__parse_str(\"%s\")\n", ostr
);
43 err
= perf_time__parse_str(&ptime
, ostr
);
45 pr_debug("Error %d\n", err
);
49 if (ptime
.start
!= start
|| ptime
.end
!= end
) {
50 pr_debug("Failed. Expected %" PRIu64
" to %" PRIu64
"\n",
64 struct perf_time_interval ptime
[TEST_MAX
];
70 static bool test__perf_time__parse_for_ranges(struct test_data
*d
)
72 struct evlist evlist
= {
73 .first_sample_time
= d
->first
,
74 .last_sample_time
= d
->last
,
76 struct perf_session session
= { .evlist
= &evlist
};
77 struct perf_time_interval
*ptime
= NULL
;
78 int range_size
, range_num
;
82 pr_debug("\nperf_time__parse_for_ranges(\"%s\")\n", d
->str
);
84 if (strchr(d
->str
, '%'))
85 pr_debug("first_sample_time %" PRIu64
" last_sample_time %" PRIu64
"\n",
88 err
= perf_time__parse_for_ranges(d
->str
, &session
, &ptime
, &range_size
,
91 pr_debug("error %d\n", err
);
95 if (range_size
< d
->num
|| range_num
!= d
->num
) {
96 pr_debug("bad size: range_size %d range_num %d expected num %d\n",
97 range_size
, range_num
, d
->num
);
101 for (i
= 0; i
< d
->num
; i
++) {
102 if (ptime
[i
].start
!= d
->ptime
[i
].start
||
103 ptime
[i
].end
!= d
->ptime
[i
].end
) {
104 pr_debug("bad range %d expected %" PRIu64
" to %" PRIu64
"\n",
105 i
, d
->ptime
[i
].start
, d
->ptime
[i
].end
);
110 if (perf_time__ranges_skip_sample(ptime
, d
->num
, 0)) {
111 pr_debug("failed to keep 0\n");
115 for (i
= 0; i
< TEST_MAX
; i
++) {
117 !perf_time__ranges_skip_sample(ptime
, d
->num
, d
->skip
[i
])) {
118 pr_debug("failed to skip %" PRIu64
"\n", d
->skip
[i
]);
122 perf_time__ranges_skip_sample(ptime
, d
->num
, d
->noskip
[i
])) {
123 pr_debug("failed to keep %" PRIu64
"\n", d
->noskip
[i
]);
134 int test__time_utils(struct test
*t __maybe_unused
, int subtest __maybe_unused
)
138 pass
&= test__parse_nsec_time("0", 0);
139 pass
&= test__parse_nsec_time("1", 1000000000ULL);
140 pass
&= test__parse_nsec_time("0.000000001", 1);
141 pass
&= test__parse_nsec_time("1.000000001", 1000000001ULL);
142 pass
&= test__parse_nsec_time("123456.123456", 123456123456000ULL);
143 pass
&= test__parse_nsec_time("1234567.123456789", 1234567123456789ULL);
144 pass
&= test__parse_nsec_time("18446744073.709551615",
145 0xFFFFFFFFFFFFFFFFULL
);
147 pass
&= test__perf_time__parse_str("1234567.123456789,1234567.123456789",
148 1234567123456789ULL, 1234567123456789ULL);
149 pass
&= test__perf_time__parse_str("1234567.123456789,1234567.123456790",
150 1234567123456789ULL, 1234567123456790ULL);
151 pass
&= test__perf_time__parse_str("1234567.123456789,",
152 1234567123456789ULL, 0);
153 pass
&= test__perf_time__parse_str(",1234567.123456789",
154 0, 1234567123456789ULL);
155 pass
&= test__perf_time__parse_str("0,1234567.123456789",
156 0, 1234567123456789ULL);
159 u64 b
= 1234567123456789ULL;
160 struct test_data d
= {
161 .str
= "1234567.123456789,1234567.123456790",
162 .ptime
= { {b
, b
+ 1}, },
164 .skip
= { b
- 1, b
+ 2, },
165 .noskip
= { b
, b
+ 1, },
168 pass
&= test__perf_time__parse_for_ranges(&d
);
172 u64 b
= 1234567123456789ULL;
173 u64 c
= 7654321987654321ULL;
174 u64 e
= 8000000000000000ULL;
175 struct test_data d
= {
176 .str
= "1234567.123456789,1234567.123456790 "
177 "7654321.987654321,7654321.987654444 "
178 "8000000,8000000.000000005",
179 .ptime
= { {b
, b
+ 1}, {c
, c
+ 123}, {e
, e
+ 5}, },
181 .skip
= { b
- 1, b
+ 2, c
- 1, c
+ 124, e
- 1, e
+ 6 },
182 .noskip
= { b
, b
+ 1, c
, c
+ 123, e
, e
+ 5 },
185 pass
&= test__perf_time__parse_for_ranges(&d
);
189 u64 b
= 7654321ULL * NSEC_PER_SEC
;
190 struct test_data d
= {
194 .ptime
= { {b
, b
+ 9}, },
196 .skip
= { b
- 1, b
+ 10, },
197 .noskip
= { b
, b
+ 9, },
200 pass
&= test__perf_time__parse_for_ranges(&d
);
204 u64 b
= 7654321ULL * NSEC_PER_SEC
;
205 struct test_data d
= {
209 .ptime
= { {b
+ 10, b
+ 19}, },
211 .skip
= { b
+ 9, b
+ 20, },
212 .noskip
= { b
+ 10, b
+ 19, },
215 pass
&= test__perf_time__parse_for_ranges(&d
);
219 u64 b
= 11223344ULL * NSEC_PER_SEC
;
220 struct test_data d
= {
221 .str
= "10%/1,10%/2",
224 .ptime
= { {b
, b
+ 9}, {b
+ 10, b
+ 19}, },
226 .skip
= { b
- 1, b
+ 20, },
227 .noskip
= { b
, b
+ 8, b
+ 9, b
+ 10, b
+ 11, b
+ 12, b
+ 19, },
230 pass
&= test__perf_time__parse_for_ranges(&d
);
234 u64 b
= 11223344ULL * NSEC_PER_SEC
;
235 struct test_data d
= {
236 .str
= "10%/1,10%/3,10%/10",
239 .ptime
= { {b
, b
+ 9}, {b
+ 20, b
+ 29}, { b
+ 90, b
+ 100}, },
241 .skip
= { b
- 1, b
+ 10, b
+ 19, b
+ 30, b
+ 89, b
+ 101 },
242 .noskip
= { b
, b
+ 9, b
+ 20, b
+ 29, b
+ 90, b
+ 100},
245 pass
&= test__perf_time__parse_for_ranges(&d
);
250 return pass
? 0 : TEST_FAIL
;