18#include <odp/helper/odph_api.h>
20#include <bench_common.h>
21#include <export_results.h>
30#define TEST_MAX_EVV_SIZE 128u
33#define TEST_EVV_SIZE 8
36#define TEST_BUF_SIZE 32
39#define TEST_UAREA_SIZE 8u
42#define TEST_REPEAT_COUNT 1000
45#define TEST_ROUNDS 100u
48#define TEST_MAX_BENCH 20
50#define BENCH_INFO(run_fn, init_fn, term_fn, alt_name) \
51 {.name = #run_fn, .run = run_fn, .init = init_fn, .term = term_fn, .desc = alt_name}
61 uint32_t max_evv_size;
78 uint32_t evv_uarea_size;
84 odp_event_t buf_event_tbl[TEST_REPEAT_COUNT][TEST_MAX_EVV_SIZE];
86 void *ptr_tbl[TEST_REPEAT_COUNT];
94 double result[TEST_MAX_BENCH];
98static args_t *gbl_args;
102 if (gbl_args == NULL)
107static int setup_sig_handler(
void)
109 struct sigaction action = { .sa_handler = sig_handler };
111 if (sigemptyset(&action.sa_mask) || sigaction(SIGINT, &action, NULL))
121 uint32_t max_evv_size = gbl_args->appl.max_evv_size;
122 uint32_t num_events = gbl_args->appl.num_events;
123 odp_event_t (*buf_event_tbl)[TEST_MAX_EVV_SIZE] = gbl_args->buf_event_tbl;
126 uint32_t num_buf, i, j;
129 for (i = 0; i < num; i++) {
132 ODPH_ABORT(
"Allocating test event vector failed\n");
137 while (num_buf < num_events) {
139 &buf[num_buf], num_events - num_buf);
141 ODPH_ABORT(
"Allocating test buffer(s) failed\n");
142 num_buf += alloc_ret;
145 for (j = 0; j < num_events; j++) {
147 evv_tbl[j] = buf_event_tbl[i][j];
154static void create_event_vectors(
void)
158 allocate_test_evv_and_contents(evv, TEST_REPEAT_COUNT);
161static void create_events(
void)
166 allocate_test_evv_and_contents(evv, TEST_REPEAT_COUNT);
167 for (
int i = 0; i < TEST_REPEAT_COUNT; i++)
175static void free_event_vector_size_set(
void)
178 uint32_t num_events = gbl_args->appl.num_events;
180 for (
int i = 0; i < TEST_REPEAT_COUNT; i++) {
182 ODPH_ABORT(
"Freeing event vector failed\n");
188static void free_event_vectors(
void)
192 for (
int i = 0; i < TEST_REPEAT_COUNT; i++) {
194 ODPH_ABORT(
"Freeing event vector failed\n");
199static void free_buf_events(
void)
201 uint32_t num_events = gbl_args->appl.num_events;
202 odp_event_t (*buf_event_tbl)[TEST_MAX_EVV_SIZE] = gbl_args->buf_event_tbl;
204 for (
int i = 0; i < TEST_REPEAT_COUNT; i++)
208static int event_vector_from_event(
void)
214 for (i = 0; i < TEST_REPEAT_COUNT; i++)
220static int event_vector_to_event(
void)
226 for (i = 0; i < TEST_REPEAT_COUNT; i++)
232static int event_vector_alloc(
void)
238 for (i = 0; i < TEST_REPEAT_COUNT; i++)
244static int event_vector_free(
void)
249 for (i = 0; i < TEST_REPEAT_COUNT; i++)
255static int event_free(
void)
260 for (i = 0; i < TEST_REPEAT_COUNT; i++)
266static int event_vector_alloc_free(
void)
271 for (i = 0; i < TEST_REPEAT_COUNT; i++) {
282static int event_vector_tbl(
void)
285 uint32_t num_events = gbl_args->appl.num_events;
289 for (
int i = 0; i < TEST_REPEAT_COUNT; i++)
292 return ret == num_events * TEST_REPEAT_COUNT;
295static int event_vector_size(
void)
298 uint32_t num_events = gbl_args->appl.num_events;
301 for (
int i = 0; i < TEST_REPEAT_COUNT; i++)
304 return ret == num_events * TEST_REPEAT_COUNT;
307static int event_vector_type(
void)
313 for (i = 0; i < TEST_REPEAT_COUNT; i++)
319static int event_vector_size_set(
void)
321 const uint32_t size = gbl_args->appl.num_events ? gbl_args->appl.num_events - 1 : 0;
325 for (i = 0; i < TEST_REPEAT_COUNT; i++)
331static int event_vector_user_area(
void)
334 void **ptr_tbl = gbl_args->ptr_tbl;
337 for (i = 0; i < TEST_REPEAT_COUNT; i++)
343static int event_vector_user_flag(
void)
348 for (
int i = 0; i < TEST_REPEAT_COUNT; i++)
354static int event_vector_user_flag_set(
void)
359 for (i = 0; i < TEST_REPEAT_COUNT; i++)
365static int event_vector_pool(
void)
371 for (i = 0; i < TEST_REPEAT_COUNT; i++)
380static void usage(
void)
383 "OpenDataPlane Event vector API microbenchmarks.\n"
385 "Optional OPTIONS:\n"
386 " -c, --cache_size <num> Pool cache size.\n"
387 " -i, --index <idx> Benchmark index to run indefinitely.\n"
388 " -r, --rounds <num> Run each test case 'num' times (default %u).\n"
389 " -m, -max_events <num> Maximum number of events in each event vector (default %u).\n"
390 " -n, --num_events <num> Number of events in each event vector (default %u).\n"
391 " -t, --time <opt> Time measurement. 0: measure CPU cycles (default), 1: measure time\n"
392 " -h, --help Display help and exit.\n\n"
393 "\n", TEST_ROUNDS, TEST_EVV_SIZE, TEST_EVV_SIZE);
403static void parse_args(
int argc,
char *argv[], appl_args_t *appl_args)
406 static const struct option longopts[] = {
407 {
"cache_size", required_argument, NULL,
'c'},
408 {
"index", required_argument, NULL,
'i'},
409 {
"rounds", required_argument, NULL,
'r'},
410 {
"max_events", required_argument, NULL,
'm'},
411 {
"num_events", required_argument, NULL,
'n'},
412 {
"time", required_argument, NULL,
't'},
413 {
"help", no_argument, NULL,
'h'},
417 static const char *shortopts =
"c:i:r:m:n:t:h";
419 appl_args->bench_idx = 0;
420 appl_args->cache_size = -1;
421 appl_args->rounds = TEST_ROUNDS;
422 appl_args->max_evv_size = TEST_EVV_SIZE;
423 appl_args->num_events = TEST_EVV_SIZE;
427 opt = getopt_long(argc, argv, shortopts, longopts, NULL);
434 appl_args->cache_size = atoi(optarg);
441 appl_args->bench_idx = atoi(optarg);
444 appl_args->rounds = atoi(optarg);
447 appl_args->max_evv_size = atoi(optarg);
450 appl_args->num_events = atoi(optarg);
453 appl_args->time = atoi(optarg);
460 if (appl_args->rounds < 1) {
461 printf(
"Invalid number test rounds: %d\n", appl_args->rounds);
465 if (appl_args->max_evv_size == 0) {
466 printf(
"Invalid event vector maximum size: %d\n", appl_args->max_evv_size);
476static void print_info(
void)
481 "odp_bench_event_vector options\n"
482 "------------------------\n");
484 printf(
"Maximum number of events in each event vector: %d\n",
485 gbl_args->appl.max_evv_size);
486 printf(
"Number of events in each event vector: %d\n",
487 gbl_args->appl.num_events);
488 printf(
"CPU mask: %s\n",
489 gbl_args->cpumask_str);
490 if (gbl_args->appl.cache_size < 0)
491 printf(
"Pool cache size: default\n");
493 printf(
"Pool cache size: %d\n",
494 gbl_args->appl.cache_size);
495 printf(
"Measurement unit: %s\n",
496 gbl_args->appl.time ?
"nsec" :
"CPU cycles");
497 printf(
"Test rounds: %u\n",
498 gbl_args->appl.rounds);
502static int bench_event_vector_export(
void *data)
504 args_t *gbl_args = data;
507 if (test_common_write(
"%s", gbl_args->appl.time ?
508 "function name,average nsec per function call\n" :
509 "function name,average cpu cycles per function call\n")) {
514 for (
int i = 0; i < gbl_args->suite.num_bench; i++) {
515 if (test_common_write(
"odp_%s,%f\n", gbl_args->suite.bench[i].desc != NULL ?
516 gbl_args->suite.bench[i].desc : gbl_args->suite.bench[i].name,
517 gbl_args->suite.result[i])) {
524 test_common_write_term();
532bench_info_t test_suite[] = {
533 BENCH_INFO(event_vector_from_event, create_events, free_event_vectors, NULL),
534 BENCH_INFO(event_vector_to_event, create_event_vectors, free_event_vectors, NULL),
535 BENCH_INFO(event_vector_alloc, NULL, free_event_vectors, NULL),
536 BENCH_INFO(event_vector_free, create_event_vectors, free_buf_events, NULL),
537 BENCH_INFO(event_free, create_events, NULL, NULL),
538 BENCH_INFO(event_vector_alloc_free, NULL, NULL, NULL),
539 BENCH_INFO(event_vector_tbl, create_event_vectors, free_event_vectors, NULL),
540 BENCH_INFO(event_vector_size, create_event_vectors, free_event_vectors, NULL),
541 BENCH_INFO(event_vector_type, create_event_vectors, free_event_vectors, NULL),
542 BENCH_INFO(event_vector_size_set, create_event_vectors, free_event_vector_size_set, NULL),
543 BENCH_INFO(event_vector_user_area, create_event_vectors, free_event_vectors, NULL),
544 BENCH_INFO(event_vector_user_flag, create_event_vectors, free_event_vectors, NULL),
545 BENCH_INFO(event_vector_user_flag_set, create_event_vectors, free_event_vectors, NULL),
546 BENCH_INFO(event_vector_pool, create_event_vectors, free_event_vectors, NULL),
550 "Result array is too small to hold all the results");
552static int create_pools(
void)
557 uint32_t buf_num = TEST_REPEAT_COUNT * gbl_args->appl.num_events;
560 ODPH_ERR(
"Error: unable to query pool capability\n");
565 ODPH_ERR(
"Error: event vector pools not supported\n");
570 ODPH_ERR(
"Error: event vector pool size not supported (max %" PRIu32
")\n",
576 ODPH_ERR(
"Error: cache size not supported (max %" PRIu32
")\n",
581 if (gbl_args->appl.cache_size != -1 &&
583 ODPH_ERR(
"Error: event vector pool cache size not supported (min %" PRIu32
")\n",
589 ODPH_ERR(
"Error: event vector size not supported (max %" PRIu32
")\n",
594 if (gbl_args->appl.num_events > gbl_args->appl.max_evv_size) {
595 ODPH_ERR(
"Error: number of event vector elements is larger than max (%u/%u)\n",
596 gbl_args->appl.num_events, gbl_args->appl.max_evv_size);
603 ODPH_ERR(
"Error: buffer pool size not supported (max %" PRIu32
")\n",
616 if (gbl_args->appl.cache_size >= 0)
620 gbl_args->evv_pool =
odp_pool_create(
"microbench event vector", &evv_pool_params);
623 ODPH_ERR(
"Error: pool create failed\n");
630 if (gbl_args->appl.num_events) {
632 buf_pool_params.
buf.
size = TEST_BUF_SIZE;
633 buf_pool_params.
buf.
num = buf_num;
636 gbl_args->buf_pool =
odp_pool_create(
"microbench buffer", &buf_pool_params);
638 ODPH_ERR(
"Error: pool create failed\n");
651int main(
int argc,
char *argv[])
653 odph_helper_options_t helper_options;
654 test_common_options_t common_options;
655 odph_thread_t worker_thread;
656 odph_thread_common_param_t thr_common;
657 odph_thread_param_t thr_param;
666 argc = odph_parse_options(argc, argv);
667 if (odph_options(&helper_options)) {
668 ODPH_ERR(
"Error: reading ODP helper options failed\n");
672 argc = test_common_parse_options(argc, argv);
673 if (test_common_options(&common_options)) {
674 ODPH_ERR(
"Error: reading test options failed\n");
679 init_param.
mem_model = helper_options.mem_model;
681 if (setup_sig_handler()) {
682 ODPH_ERR(
"Signal handler setup failed\n");
688 ODPH_ERR(
"Error: ODP global init failed\n");
694 ODPH_ERR(
"Error: ODP local init failed\n");
699 shm =
odp_shm_reserve(
"shm_args",
sizeof(args_t), ODP_CACHE_LINE_SIZE, 0);
701 ODPH_ERR(
"Error: shared mem reserve failed\n");
706 if (gbl_args == NULL) {
707 ODPH_ERR(
"Error: shared mem alloc failed\n");
711 memset(gbl_args, 0,
sizeof(args_t));
714 parse_args(argc, argv, &gbl_args->appl);
716 bench_suite_init(&gbl_args->suite);
717 gbl_args->suite.bench = test_suite;
718 gbl_args->suite.num_bench = ODPH_ARRAY_SIZE(test_suite);
719 gbl_args->suite.indef_idx = gbl_args->appl.bench_idx;
720 gbl_args->suite.rounds = gbl_args->appl.rounds;
721 gbl_args->suite.repeat_count = TEST_REPEAT_COUNT;
722 gbl_args->suite.measure_time = !!gbl_args->appl.time;
723 if (common_options.is_export)
724 gbl_args->suite.result = gbl_args->result;
728 ODPH_ERR(
"Error: unable to allocate worker thread\n");
733 sizeof(gbl_args->cpumask_str));
735 if (create_pools()) {
746 odph_thread_common_param_init(&thr_common);
747 thr_common.instance = instance;
748 thr_common.cpumask = &cpumask;
749 thr_common.share_param = 1;
751 odph_thread_param_init(&thr_param);
752 thr_param.start = bench_run;
753 thr_param.arg = &gbl_args->suite;
756 if (odph_thread_create(&worker_thread, &thr_common, &thr_param, 1) != 1) {
757 ODPH_ERR(
"Error: unable to create worker thread\n");
762 if (odph_thread_join(&worker_thread, 1) != 1) {
763 ODPH_ERR(
"Error: unable to join worker thread\n");
768 ret = gbl_args->suite.retval;
770 if (ret == 0 && common_options.is_export) {
771 if (bench_event_vector_export(gbl_args)) {
772 ODPH_ERR(
"Error: Export failed\n");
780 ODPH_ERR(
"Error: pool destroy\n");
785 ODPH_ERR(
"Error: shm free\n");
790 ODPH_ERR(
"Error: term local\n");
795 ODPH_ERR(
"Error: term global\n");
void odp_atomic_store_u32(odp_atomic_u32_t *atom, uint32_t val)
Store value to atomic uint32 variable.
odp_event_t odp_buffer_to_event(odp_buffer_t buf)
Convert buffer handle to event.
int odp_buffer_alloc_multi(odp_pool_t pool, odp_buffer_t buf[], int num)
Allocate multiple buffers.
#define odp_unlikely(x)
Branch unlikely taken.
#define ODP_UNUSED
Intentionally unused variables of functions.
void odp_cpumask_set(odp_cpumask_t *mask, int cpu)
Add CPU to mask.
int odp_cpumask_default_worker(odp_cpumask_t *mask, int num)
Default CPU mask for worker threads.
int odp_cpumask_first(const odp_cpumask_t *mask)
Find first set CPU in mask.
void odp_cpumask_zero(odp_cpumask_t *mask)
Clear entire CPU mask.
int32_t odp_cpumask_to_str(const odp_cpumask_t *mask, char *str, int32_t size)
Format a string from CPU mask.
#define ODP_CPUMASK_STR_SIZE
The maximum number of characters needed to record any CPU mask as a string (output of odp_cpumask_to_...
odp_event_t odp_event_vector_to_event(odp_event_vector_t evv)
Convert event vector handle to event.
void odp_event_vector_size_set(odp_event_vector_t evv, uint32_t size)
Set the number of events stored in a vector.
void odp_event_vector_free(odp_event_vector_t evv)
Free event vector.
odp_event_vector_t odp_event_vector_from_event(odp_event_t ev)
Get event vector handle from event.
uint32_t odp_event_vector_tbl(odp_event_vector_t evv, odp_event_t **event_tbl)
Get event vector table.
int odp_event_vector_user_flag(odp_event_vector_t evv)
Check user flag.
odp_event_vector_t odp_event_vector_alloc(odp_pool_t pool)
Allocate event vector from event vector pool.
uint32_t odp_event_vector_size(odp_event_vector_t evv)
Number of events in a vector.
#define ODP_EVENT_VECTOR_INVALID
Invalid event vector.
odp_pool_t odp_event_vector_pool(odp_event_vector_t evv)
Event vector pool.
void odp_event_vector_user_flag_set(odp_event_vector_t evv, int val)
Set user flag.
void * odp_event_vector_user_area(odp_event_vector_t evv)
Event vector user area.
odp_event_type_t odp_event_vector_type(odp_event_vector_t evv)
Type of events stored in event vector.
void odp_event_free_multi(const odp_event_t event[], int num)
Free multiple events.
void odp_event_free(odp_event_t event)
Free event.
_odp_abi_event_t * odp_event_t
ODP event.
odp_event_type_t
Event type.
void odp_init_param_init(odp_init_t *param)
Initialize the odp_init_t to default values for all fields.
#define ODP_STATIC_ASSERT(cond, msg)
Compile time assertion macro.
int odp_init_local(odp_instance_t instance, odp_thread_type_t thr_type)
Thread local ODP initialization.
int odp_init_global(odp_instance_t *instance, const odp_init_t *params, const odp_platform_init_t *platform_params)
Global ODP initialization.
int odp_term_local(void)
Thread local ODP termination.
int odp_term_global(odp_instance_t instance)
Global ODP termination.
uint64_t odp_instance_t
ODP instance ID.
odp_pool_t odp_pool_create(const char *name, const odp_pool_param_t *param)
Create a pool.
int odp_pool_capability(odp_pool_capability_t *capa)
Query pool capabilities.
void odp_pool_param_init(odp_pool_param_t *param)
Initialize pool params.
int odp_pool_destroy(odp_pool_t pool)
Destroy a pool previously created by odp_pool_create()
void odp_pool_print(odp_pool_t pool)
Print pool info.
#define ODP_POOL_INVALID
Invalid pool.
@ ODP_POOL_BUFFER
Buffer pool.
@ ODP_POOL_EVENT_VECTOR
Event vector pool.
int odp_shm_free(odp_shm_t shm)
Free a contiguous block of shared memory.
void * odp_shm_addr(odp_shm_t shm)
Shared memory block address.
#define ODP_SHM_INVALID
Invalid shared memory block.
odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align, uint32_t flags)
Reserve a contiguous block of shared memory.
void odp_sys_info_print(void)
Print system info.
@ ODP_THREAD_WORKER
Worker thread.
@ ODP_THREAD_CONTROL
Control thread.
Global initialization parameters.
odp_mem_model_t mem_model
Application memory model.
uint32_t max_num
Maximum number of buffers of any size.
uint32_t min_cache_size
Minimum size of thread local cache.
struct odp_pool_capability_t::@133 buf
Buffer pool capabilities
uint32_t max_uarea_size
Maximum user area size in bytes.
struct odp_pool_capability_t::@137 event_vector
Event vector pool capabilities.
uint32_t max_size
Maximum buffer data size in bytes.
uint32_t max_cache_size
Maximum size of thread local cache.
uint32_t max_pools
Maximum number of pools of any type (odp_pool_type_t)
uint32_t uarea_size
Minimum user area size in bytes.
uint32_t num
Number of buffers in the pool.
struct odp_pool_param_t::@142 event_vector
Parameters for event vector pools.
uint32_t cache_size
Maximum number of buffers cached locally per thread.
uint32_t size
Minimum buffer size in bytes.
odp_pool_type_t type
Pool type.
uint32_t max_size
Maximum number of handles (such as odp_packet_t) in a vector.
struct odp_pool_param_t::@138 buf
Parameters for buffer pools.