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

/** @cond _ODP_HIDE_FROM_DOXYGEN_ */

#ifndef BENCH_COMMON_H
#define BENCH_COMMON_H

#ifdef __cplusplus
extern "C" {
#endif

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

#include <stdint.h>

/**
 * Check benchmark preconditions
 *
 * Returns !0 if benchmark precondition is met.
 */
typedef int (*bench_cond_fn_t)(void);

/**
 * Initialize benchmark resources
 */
typedef void (*bench_init_fn_t)(void);

/**
 * Run benchmark
 *
 * Returns >0 on success.
 */
typedef int (*bench_run_fn_t)(void);

/**
 * Release benchmark resources
 */
typedef void (*bench_term_fn_t)(void);

/* Benchmark test data */
typedef struct {
	/* Default test name */
	const char *name;

	/* Optional alternate test description */
	const char *desc;

	/* Optional precondition to run test */
	bench_cond_fn_t cond;

	/* Optional test initializer function */
	bench_init_fn_t init;

	/* Test function to run */
	bench_run_fn_t run;

	/* Optional test terminate function */
	bench_term_fn_t term;

	/* Optional test specific limit for rounds (tuning for slow implementations) */
	uint32_t max_rounds;

} bench_info_t;

/* Benchmark suite data */
typedef struct {
	/* Array of benchmark functions */
	bench_info_t *bench;

	/* Number of benchmark functions */
	int num_bench;

	/* Optional benchmark index to run indefinitely (1...num_bench) */
	int indef_idx;

	/* Suite exit value output */
	int retval;

	/* Measure time vs. CPU cycles */
	odp_bool_t measure_time;

	/* Break worker loop if set to 1 */
	odp_atomic_u32_t exit_worker;

	/* Number of API function calls per test case */
	uint64_t repeat_count;

	/* Number of rounds per test case */
	uint64_t rounds;

	/* Dummy test result output */
	uint64_t dummy;

	/* Optional test result output array */
	double *result;

} bench_suite_t;

/**
 * Initialize benchmark suite parameters
 */
void bench_suite_init(bench_suite_t *suite);

/**
 * Run selected test indefinitely
 */
void bench_run_indef(bench_info_t *info, odp_atomic_u32_t *exit_thread);

/**
 * Run test suite and print results
 *
 * The argument is of type 'bench_suite_t *'. Returns 0 on success and <0 on failure.
 */
int bench_run(void *arg);

/*
 * Timed benchmark framework
 *
 * The main difference compared to the standard benchmark suite is that all
 * latency measurements are performed inside the test cases.
 */

/* Maximum number of benchmarked functions per test case */
#define BENCH_TM_MAX_FUNC 8

/* Timed benchmark results */
typedef struct bench_tm_results_s {
	/* Results per function */
	struct {
		/* Name of function */
		const char *name;

		/* Total duration of all function calls */
		odp_time_t tot;

		/* Minimum duration */
		odp_time_t min;

		/* Maximum duration */
		odp_time_t max;

		/* Number of measurements */
		uint64_t num;

	} func[BENCH_TM_MAX_FUNC];

	/* Number of registered test functions */
	uint8_t num;

} bench_tm_result_t;

/**
 * Timed benchmark test case
 *
 * Returns 0 on success and <0 on failure.
 */
typedef int (*bench_tm_run_fn_t)(bench_tm_result_t *res, int repeat_count);

/* Timed benchmark test case */
typedef struct {
	/* Test case name */
	const char *name;

	/* Optional precondition to run test */
	bench_cond_fn_t cond;

	/* Optional test initializer function */
	bench_init_fn_t init;

	/* Test function to run */
	bench_tm_run_fn_t run;

	/* Optional test termination function */
	bench_term_fn_t term;

	/* Optional test specific limit for rounds (tuning for slow implementations) */
	uint32_t max_rounds;

} bench_tm_info_t;

/* Timed benchmark suite data */
typedef struct {
	/* Array of benchmark test cases */
	bench_tm_info_t *bench;

	/* Number of benchmark test cases */
	uint32_t num_bench;

	/* Optional benchmark index to run (1...num_bench) */
	uint32_t bench_idx;

	/* Suite exit value output */
	int retval;

	/* Number of rounds per test case */
	uint64_t rounds;

	/* Break worker loop if set to 1 */
	odp_atomic_u32_t exit_worker;

} bench_tm_suite_t;

/**
 * Initialize benchmark suite data
 */
void bench_tm_suite_init(bench_tm_suite_t *suite);

/**
 * Register function for benchmarking
 *
 * Called by each test case to register benchmarked functions. Returns function
 * ID for recording benchmark results. At most BENCH_TM_MAX_FUNC functions can
 * be registered per test case.
 */
uint8_t bench_tm_func_register(bench_tm_result_t *res, const char *func_name);

/**
 * Record results for previously registered function
 *
 * Test case must call this function every test round for each registered
 * function.
 */
void bench_tm_func_record(odp_time_t t2, odp_time_t t1, bench_tm_result_t *res, uint8_t id);

/**
 * Run timed test suite and print results
 *
 * The argument is of type 'bench_tm_suite_t *'. Returns 0 on success and <0 on failure.
 */
int bench_tm_run(void *arg);

#ifdef __cplusplus
}
#endif

#endif