aboutsummaryrefslogtreecommitdiff
path: root/test/performance/bench_common.c
blob: 640889503d7dd6c6390e9f729bb553edf514ddcf (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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2023 Nokia
 */

/** @cond _ODP_HIDE_FROM_DOXYGEN_ */

#include <odp_api.h>
#include <odp/helper/odph_api.h>

#include "bench_common.h"

#include <inttypes.h>
#include <stdint.h>
#include <string.h>

void bench_suite_init(bench_suite_t *suite)
{
	memset(suite, 0, sizeof(bench_suite_t));

	suite->measure_time = true;

	odp_atomic_init_u32(&suite->exit_worker, 0);
}

void bench_run_indef(bench_info_t *info, odp_atomic_u32_t *exit_thread)
{
	const char *desc;

	desc = info->desc != NULL ? info->desc : info->name;

	printf("Running odp_%s test indefinitely\n", desc);

	while (!odp_atomic_load_u32(exit_thread)) {
		int ret;

		if (info->init != NULL)
			info->init();

		ret = info->run();

		if (info->term != NULL)
			info->term();

		if (!ret)
			ODPH_ABORT("Benchmark %s failed\n", desc);
	}
}

int bench_run(void *arg)
{
	uint64_t c1, c2;
	odp_time_t t1, t2;
	bench_suite_t *suite = arg;
	const uint64_t repeat_count = suite->repeat_count;
	const odp_bool_t meas_time = suite->measure_time;
	double result;

	printf("\nAverage %s per function call\n", meas_time ? "time (nsec)" : "CPU cycles");
	printf("-------------------------------------------------\n");

	for (int j = 0; j < suite->num_bench; j++) {
		int ret;
		const char *desc;
		const bench_info_t *bench = &suite->bench[j];
		uint64_t max_rounds = suite->rounds;
		uint64_t total = 0;

		if (bench->max_rounds &&  bench->max_rounds < max_rounds)
			max_rounds = bench->max_rounds;

		/* Run selected test indefinitely */
		if (suite->indef_idx) {
			if ((j + 1) != suite->indef_idx) {
				j++;
				continue;
			}
			bench_run_indef(&suite->bench[j], &suite->exit_worker);
			return 0;
		}

		desc = bench->desc != NULL ? bench->desc : bench->name;

		/* The zeroeth round is a warmup round that will be ignored */
		for (uint64_t round = 0; round <= max_rounds; round++)  {
			if (bench->init != NULL)
				bench->init();

			if (meas_time)
				t1 = odp_time_local_strict();
			else
				c1 = odp_cpu_cycles();

			ret = bench->run();

			if (meas_time)
				t2 = odp_time_local_strict();
			else
				c2 = odp_cpu_cycles();

			if (bench->term != NULL)
				bench->term();

			if (!ret) {
				ODPH_ERR("Benchmark odp_%s failed\n", desc);
				suite->retval = -1;
				return -1;
			}

			if (odp_unlikely(round == 0))
				continue;
			if (meas_time)
				total += odp_time_diff_ns(t2, t1);
			else
				total += odp_cpu_cycles_diff(c2, c1);
		}

		/* Each benchmark runs internally 'repeat_count' times. */
		result = ((double)total) / (max_rounds * repeat_count);

		printf("[%02d] odp_%-26s: %12.2f\n", j + 1, desc, result);
		if (suite->result)
			suite->result[j] = result;
	}
	printf("\n");
	/* Print dummy result to prevent compiler to optimize it away*/
	if (suite->dummy)
		printf("(dummy result: 0x%" PRIx64 ")\n\n", suite->dummy);

	return 0;
}

void bench_tm_suite_init(bench_tm_suite_t *suite)
{
	memset(suite, 0, sizeof(bench_tm_suite_t));

	odp_atomic_init_u32(&suite->exit_worker, 0);
}

uint8_t bench_tm_func_register(bench_tm_result_t *res, const char *func_name)
{
	uint8_t num_func = res->num;

	if (num_func >= BENCH_TM_MAX_FUNC)
		ODPH_ABORT("Too many test functions (max %d)\n", BENCH_TM_MAX_FUNC);

	res->func[num_func].name = func_name;
	res->num++;

	return num_func;
}

void bench_tm_func_record(odp_time_t t2, odp_time_t t1, bench_tm_result_t *res, uint8_t id)
{
	odp_time_t diff = odp_time_diff(t2, t1);

	ODPH_ASSERT(id < BENCH_TM_MAX_FUNC);

	res->func[id].tot = odp_time_sum(res->func[id].tot, diff);

	if (odp_time_cmp(diff, res->func[id].min) < 0)
		res->func[id].min = diff;

	if (odp_time_cmp(diff, res->func[id].max) > 0)
		res->func[id].max = diff;

	res->func[id].num++;
}

static void init_result(bench_tm_result_t *res)
{
	memset(res, 0, sizeof(bench_tm_result_t));

	for (int i = 0; i < BENCH_TM_MAX_FUNC; i++) {
		res->func[i].tot = ODP_TIME_NULL;
		res->func[i].min = odp_time_local_from_ns(ODP_TIME_HOUR_IN_NS);
		res->func[i].max = ODP_TIME_NULL;
	}
}

static void print_results(bench_tm_result_t *res)
{
	for (uint8_t i = 0; i < res->num; i++) {
		uint64_t num = res->func[i].num ? res->func[i].num : 1;

		printf("     %-38s    %-12" PRIu64 " %-12" PRIu64 " %-12" PRIu64 "\n",
		       res->func[i].name,
		       odp_time_to_ns(res->func[i].min),
		       odp_time_to_ns(res->func[i].tot) / num,
		       odp_time_to_ns(res->func[i].max));
	}
}

int bench_tm_run(void *arg)
{
	bench_tm_suite_t *suite = arg;

	printf("\nLatency (nsec) per function call               min          avg          max\n");
	printf("------------------------------------------------------------------------------\n");

	for (uint32_t j = 0; j < suite->num_bench; j++) {
		const bench_tm_info_t *bench = &suite->bench[j];
		uint64_t rounds = suite->rounds;
		bench_tm_result_t res;

		/* Run only selected test case */
		if (suite->bench_idx && (j + 1) != suite->bench_idx)
			continue;

		if (bench->cond != NULL && !bench->cond()) {
			printf("[%02d] %-41s n/a          n/a          n/a\n",
			       j + 1, bench->name);
			continue;
		}

		if (bench->max_rounds && bench->max_rounds < rounds)
			rounds = bench->max_rounds;

		/*
		 * Run each test twice.
		 * Results from the first warm-up round are ignored.
		 */
		for (uint32_t i = 0; i < 2; i++) {
			if (odp_atomic_load_u32(&suite->exit_worker))
				return 0;

			init_result(&res);

			if (bench->init != NULL)
				bench->init();

			if (bench->run(&res, rounds)) {
				ODPH_ERR("Benchmark %s failed\n", bench->name);
				suite->retval = -1;
				return -1;
			}

			if (bench->term != NULL)
				bench->term();

		}
		printf("[%02d] %-26s\n", j + 1, bench->name);
		print_results(&res);
	}
	printf("\n");

	return 0;
}