summaryrefslogtreecommitdiffstats
path: root/tools/perf/tests/time-utils-test.c
blob: 38df10373c1e17040ffbac87a5848ca5efded419 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// SPDX-License-Identifier: GPL-2.0
#include <linux/compiler.h>
#include <linux/time64.h>
#include <inttypes.h>
#include <string.h>
#include "time-utils.h"
#include "evlist.h"
#include "session.h"
#include "debug.h"
#include "tests.h"

static bool test__parse_nsec_time(const char *str, u64 expected)
{
	u64 ptime;
	int err;

	pr_debug("\nparse_nsec_time(\"%s\")\n", str);

	err = parse_nsec_time(str, &ptime);
	if (err) {
		pr_debug("error %d\n", err);
		return false;
	}

	if (ptime != expected) {
		pr_debug("Failed. ptime %" PRIu64 " expected %" PRIu64 "\n",
			 ptime, expected);
		return false;
	}

	pr_debug("%" PRIu64 "\n", ptime);

	return true;
}

static bool test__perf_time__parse_str(const char *ostr, u64 start, u64 end)
{
	struct perf_time_interval ptime;
	int err;

	pr_debug("\nperf_time__parse_str(\"%s\")\n", ostr);

	err = perf_time__parse_str(&ptime, ostr);
	if (err) {
		pr_debug("Error %d\n", err);
		return false;
	}

	if (ptime.start != start || ptime.end != end) {
		pr_debug("Failed. Expected %" PRIu64 " to %" PRIu64 "\n",
			 start, end);
		return false;
	}

	return true;
}

#define TEST_MAX 64

struct test_data {
	const char *str;
	u64 first;
	u64 last;
	struct perf_time_interval ptime[TEST_MAX];
	int num;
	u64 skip[TEST_MAX];
	u64 noskip[TEST_MAX];
};

static bool test__perf_time__parse_for_ranges(struct test_data *d)
{
	struct evlist evlist = {
		.first_sample_time = d->first,
		.last_sample_time = d->last,
	};
	struct perf_session session = { .evlist = &evlist };
	struct perf_time_interval *ptime = NULL;
	int range_size, range_num;
	bool pass = false;
	int i, err;

	pr_debug("\nperf_time__parse_for_ranges(\"%s\")\n", d->str);

	if (strchr(d->str, '%'))
		pr_debug("first_sample_time %" PRIu64 " last_sample_time %" PRIu64 "\n",
			 d->first, d->last);

	err = perf_time__parse_for_ranges(d->str, &session, &ptime, &range_size,
					  &range_num);
	if (err) {
		pr_debug("error %d\n", err);
		goto out;
	}

	if (range_size < d->num || range_num != d->num) {
		pr_debug("bad size: range_size %d range_num %d expected num %d\n",
			 range_size, range_num, d->num);
		goto out;
	}

	for (i = 0; i < d->num; i++) {
		if (ptime[i].start != d->ptime[i].start ||
		    ptime[i].end != d->ptime[i].end) {
			pr_debug("bad range %d expected %" PRIu64 " to %" PRIu64 "\n",
				 i, d->ptime[i].start, d->ptime[i].end);
			goto out;
		}
	}

	if (perf_time__ranges_skip_sample(ptime, d->num, 0)) {
		pr_debug("failed to keep 0\n");
		goto out;
	}

	for (i = 0; i < TEST_MAX; i++) {
		if (d->skip[i] &&
		    !perf_time__ranges_skip_sample(ptime, d->num, d->skip[i])) {
			pr_debug("failed to skip %" PRIu64 "\n", d->skip[i]);
			goto out;
		}
		if (d->noskip[i] &&
		    perf_time__ranges_skip_sample(ptime, d->num, d->noskip[i])) {
			pr_debug("failed to keep %" PRIu64 "\n", d->noskip[i]);
			goto out;
		}
	}

	pass = true;
out:
	free(ptime);
	return pass;
}

static int test__time_utils(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
{
	bool pass = true;

	pass &= test__parse_nsec_time("0", 0);
	pass &= test__parse_nsec_time("1", 1000000000ULL);
	pass &= test__parse_nsec_time("0.000000001", 1);
	pass &= test__parse_nsec_time("1.000000001", 1000000001ULL);
	pass &= test__parse_nsec_time("123456.123456", 123456123456000ULL);
	pass &= test__parse_nsec_time("1234567.123456789", 1234567123456789ULL);
	pass &= test__parse_nsec_time("18446744073.709551615",
				      0xFFFFFFFFFFFFFFFFULL);

	pass &= test__perf_time__parse_str("1234567.123456789,1234567.123456789",
					   1234567123456789ULL, 1234567123456789ULL);
	pass &= test__perf_time__parse_str("1234567.123456789,1234567.123456790",
					   1234567123456789ULL, 1234567123456790ULL);
	pass &= test__perf_time__parse_str("1234567.123456789,",
					   1234567123456789ULL, 0);
	pass &= test__perf_time__parse_str(",1234567.123456789",
					   0, 1234567123456789ULL);
	pass &= test__perf_time__parse_str("0,1234567.123456789",
					   0, 1234567123456789ULL);

	{
		u64 b = 1234567123456789ULL;
		struct test_data d = {
			.str   = "1234567.123456789,1234567.123456790",
			.ptime = { {b, b + 1}, },
			.num = 1,
			.skip = { b - 1, b + 2, },
			.noskip = { b, b + 1, },
		};

		pass &= test__perf_time__parse_for_ranges(&d);
	}

	{
		u64 b = 1234567123456789ULL;
		u64 c = 7654321987654321ULL;
		u64 e = 8000000000000000ULL;
		struct test_data d = {
			.str   = "1234567.123456789,1234567.123456790 "
				 "7654321.987654321,7654321.987654444 "
				 "8000000,8000000.000000005",
			.ptime = { {b, b + 1}, {c, c + 123}, {e, e + 5}, },
			.num = 3,
			.skip = { b - 1, b + 2, c - 1, c + 124, e - 1, e + 6 },
			.noskip = { b, b + 1, c, c + 123, e, e + 5 },
		};

		pass &= test__perf_time__parse_for_ranges(&d);
	}

	{
		u64 b = 7654321ULL * NSEC_PER_SEC;
		struct test_data d = {
			.str    = "10%/1",
			.first  = b,
			.last   = b + 100,
			.ptime  = { {b, b + 9}, },
			.num    = 1,
			.skip   = { b - 1, b + 10, },
			.noskip = { b, b + 9, },
		};

		pass &= test__perf_time__parse_for_ranges(&d);
	}

	{
		u64 b = 7654321ULL * NSEC_PER_SEC;
		struct test_data d = {
			.str    = "10%/2",
			.first  = b,
			.last   = b + 100,
			.ptime  = { {b + 10, b + 19}, },
			.num    = 1,
			.skip   = { b + 9, b + 20, },
			.noskip = { b + 10, b + 19, },
		};

		pass &= test__perf_time__parse_for_ranges(&d);
	}

	{
		u64 b = 11223344ULL * NSEC_PER_SEC;
		struct test_data d = {
			.str    = "10%/1,10%/2",
			.first  = b,
			.last   = b + 100,
			.ptime  = { {b, b + 9}, {b + 10, b + 19}, },
			.num    = 2,
			.skip   = { b - 1, b + 20, },
			.noskip = { b, b + 8, b + 9, b + 10, b + 11, b + 12, b + 19, },
		};

		pass &= test__perf_time__parse_for_ranges(&d);
	}

	{
		u64 b = 11223344ULL * NSEC_PER_SEC;
		struct test_data d = {
			.str    = "10%/1,10%/3,10%/10",
			.first  = b,
			.last   = b + 100,
			.ptime  = { {b, b + 9}, {b + 20, b + 29}, { b + 90, b + 100}, },
			.num    = 3,
			.skip   = { b - 1, b + 10, b + 19, b + 30, b + 89, b + 101 },
			.noskip = { b, b + 9, b + 20, b + 29, b + 90, b + 100},
		};

		pass &= test__perf_time__parse_for_ranges(&d);
	}

	pr_debug("\n");

	return pass ? 0 : TEST_FAIL;
}

DEFINE_SUITE("time utils", time_utils);