summaryrefslogtreecommitdiff
path: root/include/shared/lk/trusty_benchmark.h
blob: 8e13fcf4ee2604081cb14131a58e4b55f2017bf7 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * bench functions can be defined with the macro
 * BENCH(suite_name,bench_name,n [, params])
 * {
 *     ... bench function body ...
 * }
 *
 *  - This body will be executed n times for each params, if 4 arguments are
 *    given.
 *  - This body will be executed n times, if 3 arguments are given.
 *
 * For a suite, one is expected to also define BENCH_SETUP, BENCH_TEARDOWN.
 * For a 2-tuple (suite_name, bench_name) one is expected to also define at
 * least one BENCH_RESULT.
 *
 * BENCH_SETUP(suite_name)
 * {
 *     ... bench setup body ...
 *     return int_error_code;
 * }
 *
 * BENCH_SETUP(suite_name):
 *  - Will return 0 or NO_ERROR when it succeed.
 *  - Will be run before every execution of the BENCH body
 *  - Will cancel execution of the next BENCH body if returns non-zero.
 *    Test will be considered failed.
 *  - Will cancel execution of the next BENCH body if any ASSERT_<op> fails.
 *    Test will be considered failed.
 *  - All ASSERT_<op> macros from trusty_unittest can be used
 *  - GTEST_SKIP() maybe be called to skip the benchmark run.
 *
 * BENCH_TEARDOWN(suite_name)
 * {
 *     ... bench teardown body ...
 * }
 *
 * BENCH_TEARDOWN(suite_name):
 *  - Is executed even if BENCH_SETUP failed
 *  - Does not return any value
 *  - All ASSERT_<op> macros from trusty_unittest can be used
 *
 * BENCH_RESULT(suite_name,bench_name,res_name)
 * {
 *     ... bench result body ...
 *     return int64_t_value_of_res_name_for_last_bench_body_run;
 * }
 *
 *
 * BENCH_RESULT(suite_name,bench_name,res_name):
 *  - At least one must be defined. Can define multiple times.
 *  - Must return an int64_t
 *  - Results will be aggregated for n runs of the BENCH( ) body.
 *    Aggregation is grouped by params to min/max/avg of the n runs
 *  - res_name will be used as column title for the metric summary
 *
 * Example:
 *      BENCH_RESULT(hwcrypto, hwrng, time_ns) {
 *          return bench_get_duration_ns();
 *      }
 *
 * - The execution sequence is roughly:
 *
 *       for each param if any:
 *          BENCH_SETUP(suite_name,bench_name)
 *           repeat n times:
 *               BENCH_CONTENT
 *               for each BENCH_RESULT(suite_name,bench_name,res_name)
 *                   update the accumulators for res_name [min,max,avg]
 *           BENCH_TEARDOWN(suite_name,bench_name)
 *       Print Result Table
 *
 * NOTE:
 * When using a parameter array:
 *  - params must be an array of any type T any_name_is_fine[NB_PARAMS] = {...};
 *    The number of params is deduced from the sizeof(params)/sizeof(params[0]).
 *    So please do not dynamically allocate T* params.
 *  - params array name is up to the test writer
 *
 * The default column name for a parameter in the summary table is its index in
 * the param array. To customize it, one can define a function with the
 * following signature:
 * static void trusty_bench_get_param_name_cb(char* buf, size_t buf_size,
 * size_t param_idx);
 *
 * then assign it during BENCH_SETUP to the trusty_bench_get_param_name_cb
 * global:
 *
 * BENCH_SETUP(suite_name) {
 *   trusty_bench_get_param_name_cb = &get_param_name_cb;
 *   …
 * }
 *
 * trusty_bench_get_param_name_cb will be reset to NULL after teardown.
 *
 * See "trusty/user/app/sample/hwrng-bench/main.c" for a working and thoroughly
 * commented example
 */

#pragma once
#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdlib.h>

#include <lib/unittest/unittest.h>
#include <trusty_log.h>
#include "trusty_bench_common.h"
#include "trusty_bench_json_print.h"
#include "trusty_bench_option_cb.h"
#include "trusty_bench_print_tables.h"
#include "trusty_unittest.h"
#ifdef WITH_PTHREAD
#include <lib/thread/pthread.h>
#endif
#include <uapi/err.h>

__BEGIN_CDECLS

/**
 * struct benchmark_internal_state - Store internals for current bench.
 * @last_bench_body_duration:   nanoseconds duration of the last execution of
 *                              the bench body.
 * @cur_param_idx:              index of current parameter in param_array.
 */
static struct benchmark_internal_state {
    int64_t last_bench_body_duration;
    size_t cur_param_idx;
} bench_state;

/**
 * bench_get_duration_ns - convenience function to use in BENCH_RESULT to get
 * the duration of last bench body execution.
 *
 * Return: The duration of the last completed BENCH body in nanoseconds.
 */
static inline int64_t bench_get_duration_ns(void) {
    return bench_state.last_bench_body_duration;
}

/**
 * bench_get_param_idx - convenience function to use to get the
 * index of the current parameter BENCH_XXX is running for.
 * Return: The index of the parameter BENCH_XXX is running for.
 */
static inline size_t bench_get_param_idx(void) {
    return bench_state.cur_param_idx % trusty_cur_bench_nb_params;
}

/**
 * bench_get_cpu_idx - convenience function to use to get the
 * index of the current cpu BENCH_XXX is running for.
 * Return: The index of the cpu BENCH_XXX is running for.
 */
static inline size_t bench_get_cpu_idx(void) {
    return bench_state.cur_param_idx / trusty_cur_bench_nb_params;
}

/*
 * Helper macros to run on tests on all CPUs
 */

#ifdef WITH_PTHREAD
static int trusty_bench_multi_cpus_setup(void) {
    if (trusty_bench_nb_cpu > 1) {
        cpu_set_t cpu_set;

        CPU_ZERO(&cpu_set);
        CPU_SET(bench_state.cur_param_idx / trusty_cur_bench_nb_params,
                &cpu_set);

        return pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t),
                                      &cpu_set);
    }
    return NO_ERROR;
}

static int trusty_bench_multi_cpus_teardown(void) {
    if (trusty_bench_nb_cpu > 1) {
        cpu_set_t cpu_set;

        CPU_ZERO(&cpu_set);
        for (int i = 0; i < SMP_MAX_CPUS; i++) {
            CPU_SET(i, &cpu_set);
        }

        return pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t),
                                      &cpu_set);
    }
    return NO_ERROR;
}
#else
static int trusty_bench_multi_cpus_setup(void) {
    return NO_ERROR;
}
static int trusty_bench_multi_cpus_teardown(void) {
    return NO_ERROR;
}
#endif

/**
 * trusty_bench_update_metric -  Update the appropriate metric with the value
 * returned by BENCH_RESULT
 * @m:              The metric whose aggregate needs to be updated.
 * @val:            The value returned by BENCH_RESULT.
 */
static inline void trusty_bench_update_metric(struct bench_metric_node* m,
                                              int64_t val) {
    m->cnt += 1;
    m->tot += val;
    m->aggregates[BENCH_AGGREGATE_AVG] = m->tot / m->cnt;
    m->aggregates[BENCH_AGGREGATE_MIN] =
            MIN(m->aggregates[BENCH_AGGREGATE_MIN], val);
    m->aggregates[BENCH_AGGREGATE_MAX] =
            MAX(m->aggregates[BENCH_AGGREGATE_MAX], val);
}

/**
 * trusty_bench_run_metrics -        Run All Metric Updaters after one iteration
 * of bench function for all param/metric in the last BENCH.
 * @metric_list:        List of metrics aggregated during all BENCH runs.
 * @param_idx:          Index of the current parameter in the param_array of
 *                      BENCH.
 * @cold_run:           Are we updating metrics for the initial cold run?
 */
static inline void trusty_bench_run_metrics(struct list_node* metric_list,
                                            size_t param_idx,
                                            bool cold_run) {
    struct bench_metric_list_node* entry;

    list_for_every_entry(metric_list, entry, struct bench_metric_list_node,
                         node) {
        if (param_idx == entry->param_idx) {
            if (cold_run) {
                entry->metric.cold = entry->bench_result();
            } else {
                trusty_bench_update_metric(&entry->metric,
                                           entry->bench_result());
            }
        }
    }
}

/**
 * trusty_bench_reset_metrics -        Run All Metric Updaters after one
 * iteration of bench function for all param/metric in the last BENCH.
 * @metric_list:        List of metrics aggregated during all BENCH runs.
 * @param_idx:          Index of the current parameter in the param_array of
 *                      BENCH.
 */
static inline void trusty_bench_reset_metrics(struct list_node* metric_list,
                                              size_t param_idx) {
    struct bench_metric_list_node* entry;

    list_for_every_entry(metric_list, entry, struct bench_metric_list_node,
                         node) {
        if (param_idx == entry->param_idx) {
            trusty_bench_update_metric(&entry->metric, entry->bench_result());
        }
    }
}

/**
 * BENCH_SETUP -        Runs before every execution of the body of the BENCH
 *                      macro. Can be used to allocate memory, setup 'states',
 *                      initialize 'sessions'...
 * @suite_name:         Identifier of the current suite.
 */
#define BENCH_SETUP(suite_name)          \
    static int suite_name##_setup(void); \
    static int suite_name##_setup(void)

/**
 * BENCH_TEARDOWN -     Runs after every execution of the body of the BENCH
 *                      macro. Can be used to free memory, clear 'states',
 *                      close 'sessions'...
 * @suite_name:         Identifier of the current suite.
 */
#define BENCH_TEARDOWN(suite_name)           \
    static void suite_name##_teardown(void); \
    static void suite_name##_teardown(void)

/**
 * BENCH_RESULT -       Declare a metric name for the corresponding BENCH and
 *                      declare the functions to update it after every iteration
 * @suite_name:         Identifier of the current suite.
 * @bench_name:         Unique identifier of the Bench in the suite.
 * @metric_name:        Name of the metric to print in the result table.
 */
#define BENCH_RESULT(suite_name, bench_name, metric_name)                        \
    static int64_t update_##suite_name##_##bench_name##_##metric_name(void);     \
    static struct bench_metric_list_node                                         \
            suite_name##_##bench_name##_##metric_name##_node = {                 \
                    .node = LIST_INITIAL_CLEARED_VALUE,                          \
                    .metric = {0, 0, 0, {INT32_MAX, 0, 0}},                      \
                    .name = STRINGIFY(metric_name),                              \
                    .param_idx = 0,                                              \
                    .bench_result =                                              \
                            update_##suite_name##_##bench_name##_##metric_name}; \
    __attribute__((constructor)) void                                            \
            suite_name##_##bench_name##_##metric_name##_add(void) {              \
        list_add_tail(&suite_name##_##bench_name##_metric_list,                  \
                      &suite_name##_##bench_name##_##metric_name##_node.node);   \
    }                                                                            \
                                                                                 \
    static int64_t update_##suite_name##_##bench_name##_##metric_name(void)

/**
 * PARAM_TEST_NODES_SIMPLE -    Create the unparameterized test node lists for
 *                              BENCH
 * @suite_name:                 Identifier of the current suite.
 * @bench_name:                 Unique identifier of the Bench in the suite.
 */
#define PARAM_TEST_NODES_SIMPLE(suite_name, bench_name)                        \
    static struct test_list_node suite_name##_##bench_name##_bench_##_node = { \
            .node = LIST_INITIAL_CLEARED_VALUE,                                \
            .suite = STRINGIFY(suite_name_),                                   \
            .name = STRINGIFY(bench_name_),                                    \
            .func = suite_name##_##bench_name##_bench_,                        \
            .needs_param = 0,                                                  \
    };                                                                         \
                                                                               \
    __attribute__((constructor)) void                                          \
            suite_name##_##bench_name##_bench_##_add(void) {                   \
        list_add_tail(&_test_list,                                             \
                      &suite_name##_##bench_name##_bench_##_node.node);        \
    }

/**
 * PARAM_TEST_NODES_PARAMETRIC -    Create the unparameterized test node lists
 *                                  for BENCH
 * @suite_name:                     Identifier of the current suite.
 * @bench_name:                     Unique identifier of the Bench in the suite.
 * @params:                         identifier of the param Array for parametric
 * benches
 */
#define PARAM_TEST_NODES_PARAMETRIC(suite_name, bench_name, params)       \
    static struct test_list_node                                          \
            suite_name##_##bench_name##_bench_##params##_node = {         \
                    .node = LIST_INITIAL_CLEARED_VALUE,                   \
                    .suite = STRINGIFY(suite_name_##params),              \
                    .name = STRINGIFY(bench_name_##params),               \
                    .func = suite_name##_##bench_name##_bench_##params,   \
                    .needs_param = 0,                                     \
    };                                                                    \
                                                                          \
    __attribute__((constructor)) void                                     \
            suite_name##_##bench_name##_bench_##params##_add(void) {      \
        list_add_tail(                                                    \
                &_test_list,                                              \
                &suite_name##_##bench_name##_bench_##params##_node.node); \
    }

/**
 * set_param_metric -       Create a list of parameterized metrics out of the
 *                          existing list of non-parameterized metric.
 * @unparameterized_list:   List of metrics aggregated during all BENCH
 *                          runs.
 * @parameterized_list:     Will be filled with nb_params *
 *                          length_of(unparameterized_list) metrics with
 *                          appropriate param_idx value.
 * @nb_params:              Number of parameters of the BENCH macro.
 * Return:                  The list of parameterized metrics.
 */
static inline struct bench_metric_list_node* set_param_metric(
        struct list_node* unparameterized_list,
        struct list_node* parameterized_list,
        size_t nb_params) {
    size_t idx = 0;
    struct bench_metric_list_node* entry;
    struct bench_metric_list_node* list_pool =
            calloc(nb_params * list_length(unparameterized_list),
                   sizeof(struct bench_metric_list_node));
    if (list_pool == NULL) {
        TLOGE("Failed to Allocate memory for bench_metric_list_node!");
        return NULL;
    }
    list_for_every_entry(unparameterized_list, entry,
                         struct bench_metric_list_node, node) {
        for (size_t idx_param = 0; idx_param < nb_params; ++idx_param) {
            struct bench_metric_node tmp_metric = {0, 0, 0, {INT32_MAX, 0, 0}};

            list_pool[idx].metric = tmp_metric;
            list_pool[idx].name = entry->name;
            list_pool[idx].param_idx = idx_param;
            list_pool[idx].bench_result = entry->bench_result;
            list_add_tail(parameterized_list, &(list_pool[idx].node));
            ++idx;
        }
    }
    return list_pool;
}

/**
 * trusty_bench_get_overhead - Get Minimal overhead of the benchmark around
 * benched function
 *
 * Return:        The Value of the overhead in nanoseconds.
 */
static int64_t trusty_bench_get_overhead(void) {
    const size_t nb_runs = 100;
    int64_t start_time;
    int64_t end_time;
    int64_t res = INT64_MAX;

    for (size_t i = 0; i < nb_runs; ++i) {
        start_time = get_current_time_ns();
        end_time = get_current_time_ns();
        res = MIN(end_time - start_time, res);
    }
    return res;
}

/**
 * get_extended_bench_name - Print Status of Currently Running Bench.
 *
 * @test_name_in:   Name of the Current Unparameterized Test.
 * @test_name_out:  Name of the Current Unparameterized Test.
 *                  + "_[param_idx]"
 *
 * Return:          When successful, returns 0
 *                  If test_name_out allocation/print failed returns asprintf
 *                  return code
 */
static inline int get_extended_bench_name(const char* test_name_in,
                                          char** test_name_out) {
    int res = snprintf(NULL, 0, "%s_%zu", test_name_in,
                       bench_state.cur_param_idx);
    *test_name_out = NULL;
    if (res >= 0) {
        *test_name_out = malloc(res + 1);
        res = snprintf(*test_name_out, res + 1, "%s_%zu", test_name_in,
                       bench_state.cur_param_idx);
    }
    if (res < 0) {
        return res;
    }
    if (!test_name_out) {
        TLOGE("Cannot Allocate memory for test name\n");
        return -1;
    }
    return 0;
}

/**
 * BENCH_CORE -             Called by both parametrized and unparameterized
 * BENCH for their common part
 * @suite_name:             Identifier of the current suite.
 * @bench_name:             Unique identifier of the Bench in the suite.
 * @nb_runs:                The number of execution of its body for each param
 * @nb_params:              Number of params in params array
 * @params:                 An array T array_name[nb_params] of parameter
 * @metric_list:            List of metric nodes to update
 */
#define BENCH_CORE(suite_name, bench_name, nb_runs, nb_params, params,          \
                   metric_list)                                                 \
    reset_vertical_print_widths();                                              \
    trusty_bench_print_title(STRINGIFY(suite_name), STRINGIFY(bench_name),      \
                             STRINGIFY(params));                                \
    static trusty_bench_print_callback_t trusty_bench_print_cb =                \
            &BENCHMARK_PRINT_CB;                                                \
    trusty_cur_bench_nb_params = nb_params;                                     \
    for (size_t idx_param = 0; idx_param < (nb_params * trusty_bench_nb_cpu);   \
         ++idx_param) {                                                         \
        bench_state.cur_param_idx = idx_param;                                  \
        char* extended_test_name = NULL;                                        \
        int res_alloc = get_extended_bench_name(                                \
                STRINGIFY(bench_name##_##params), &extended_test_name);         \
        if (res_alloc < 0) {                                                    \
            TLOGE("ERROR %d expanding test name\n", res_alloc);                 \
            _test_context.all_ok = false;                                       \
            _test_context.tests_failed++;                                       \
            continue;                                                           \
        }                                                                       \
        TEST_BEGIN_FUNC(STRINGIFY(suite_name), extended_test_name);             \
                                                                                \
        int rc = trusty_bench_multi_cpus_setup();                               \
        if (rc != NO_ERROR) {                                                   \
            _test_context.skipped = true;                                       \
            _test_context.tests_skipped++;                                      \
        } else {                                                                \
            rc = suite_name##_setup();                                          \
        }                                                                       \
                                                                                \
        if (_test_context.skipped) {                                            \
            trusty_unittest_print_status(" SKIPPED");                           \
            continue;                                                           \
        } else if (rc != NO_ERROR) {                                            \
            TLOGE("ERROR %d during benchmark setup\n", rc);                     \
            _test_context.all_ok = false;                                       \
            _test_context.tests_failed++;                                       \
            continue;                                                           \
        }                                                                       \
        int64_t overhead = trusty_bench_get_overhead();                         \
                                                                                \
        /* Cold Run */                                                          \
        int64_t start_time;                                                     \
        int64_t end_time;                                                       \
        start_time = get_current_time_ns();                                     \
        int64_t res = suite_name##_##bench_name##_inner_##params();             \
        end_time = get_current_time_ns();                                       \
                                                                                \
        if (res != NO_ERROR) {                                                  \
            TLOGE("ERROR During Cold Run%" PRId64 "\n", res);                   \
            _test_context.all_ok = false;                                       \
            _test_context.tests_failed++;                                       \
            continue;                                                           \
        }                                                                       \
                                                                                \
        bench_state.last_bench_body_duration = end_time - start_time;           \
        if (5 * overhead >= bench_state.last_bench_body_duration) {             \
            trusty_unittest_printf(                                             \
                    "WARNING: Benchmark internal function is too fast %" PRId64 \
                    "ns, while the benchmark overhead is %" PRId64 "ns.",       \
                    overhead, bench_state.last_bench_body_duration);            \
        }                                                                       \
                                                                                \
        bench_state.last_bench_body_duration -= overhead;                       \
                                                                                \
        if (!_test_context.hard_fail && _test_context.all_ok) {                 \
            trusty_bench_run_metrics(&metric_list, idx_param, true);            \
        }                                                                       \
                                                                                \
        for (size_t idx_run = 0; idx_run < nb_runs; ++idx_run) {                \
            if (!_test_context.hard_fail && _test_context.all_ok) {             \
                start_time = get_current_time_ns();                             \
                res = suite_name##_##bench_name##_inner_##params();             \
                end_time = get_current_time_ns();                               \
                bench_state.last_bench_body_duration = end_time - start_time;   \
                if (overhead >= bench_state.last_bench_body_duration) {         \
                    TLOGE("Benchmark internal function is too fast %" PRId64    \
                          "ns, while the benchmark overhead is %" PRId64        \
                          "ns.",                                                \
                          overhead, bench_state.last_bench_body_duration);      \
                }                                                               \
                                                                                \
                bench_state.last_bench_body_duration -= overhead;               \
                if (res != NO_ERROR) {                                          \
                    TLOGE("ERROR %" PRId64 "\n", res);                          \
                }                                                               \
            }                                                                   \
            if (!_test_context.hard_fail && _test_context.all_ok) {             \
                trusty_bench_run_metrics(&metric_list, idx_param, false);       \
            }                                                                   \
        }                                                                       \
        suite_name##_teardown();                                                \
        rc = trusty_bench_multi_cpus_teardown();                                \
        if (rc != NO_ERROR) {                                                   \
            TLOGW("failed to reset CPU affinity: %d\n", rc);                    \
        }                                                                       \
        TEST_END_FUNC();                                                        \
        free(extended_test_name);                                               \
        extended_test_name = NULL;                                              \
    }                                                                           \
    trusty_bench_print_cb(&metric_list, (nb_params * trusty_bench_nb_cpu),      \
                          STRINGIFY(suite_name),                                \
                          STRINGIFY(bench_name##_##params));                    \
    trusty_bench_get_param_name_cb = NULL;                                      \
    trusty_bench_get_formatted_value_cb = NULL

/**
 * BENCH_PARAMETERIZED_PTR -Called when BENCH has 5 parameters. This allows
 *                          to reuse Other macros for different bench by
 * aliasing an array to a pointer
 * @suite_name:             Identifier of the current suite.
 * @bench_name:             Unique identifier of the Bench in the suite.
 * @nb_runs:                The number of execution of its body for each param
 * @params:                 An array T array_name[nb_params] of parameter
 * @nb_params:              Number of parameters in the parameter Array
 */
#define BENCH_PARAMETERIZED_PTR(nb_cpu, suite_name, bench_name, nb_runs,         \
                                params, nb_params)                               \
    static int suite_name##_##bench_name##_inner_##params(void);                 \
    static void suite_name##_##bench_name##_bench_##params(void);                \
    static struct list_node suite_name##_##bench_name##_metric_list =            \
            LIST_INITIAL_VALUE(suite_name##_##bench_name##_metric_list);         \
    static struct list_node suite_name##_##bench_name##_metric_##params##_list = \
            LIST_INITIAL_VALUE(                                                  \
                    suite_name##_##bench_name##_metric_##params##_list);         \
                                                                                 \
    static void suite_name##_##bench_name##_bench_##params(void) {               \
        trusty_bench_nb_cpu = nb_cpu;                                            \
        struct bench_metric_list_node* metric_pool = set_param_metric(           \
                &suite_name##_##bench_name##_metric_list,                        \
                &suite_name##_##bench_name##_metric_##params##_list,             \
                (nb_params * trusty_bench_nb_cpu));                              \
        if (metric_pool == NULL) {                                               \
            _test_context.hard_fail = true;                                      \
            return;                                                              \
        }                                                                        \
        BENCH_CORE(suite_name, bench_name, nb_runs, nb_params, params,           \
                   suite_name##_##bench_name##_metric_##params##_list);          \
        free(metric_pool);                                                       \
    }                                                                            \
    PARAM_TEST_NODES(suite_name, bench_name, params)                             \
                                                                                 \
    static int suite_name##_##bench_name##_inner_##params(void)

/**
 * BENCH_PARAMETERIZED -    Called when BENCH has 4 parameters
 * @suite_name:             Identifier of the current suite.
 * @bench_name:             Unique identifier of the Bench in the suite.
 * @nb_runs:                The number of execution of its body for each param
 * @params:                 An array T array_name[nb_params] of parameter
 */
#define BENCH_PARAMETERIZED(nb_cpu, suite_name, bench_name, nb_runs, params) \
    BENCH_PARAMETERIZED_PTR(nb_cpu, suite_name, bench_name, nb_runs, params, \
                            countof(params))

/**
 * BENCH_SIMPLE -       Called when BENCH has only 3 parameters.
 * @suite_name:         Identifier of the current suite.
 * @bench_name:         Unique identifier of the Bench in the suite.
 * @nb_runs:            The number of execution of its body.
 */
#define BENCH_SIMPLE(nb_cpu, suite_name, bench_name, nb_runs)            \
    static int suite_name##_##bench_name##_inner_(void);                 \
    static void suite_name##_##bench_name##_bench_(void);                \
    static struct list_node suite_name##_##bench_name##_metric_list =    \
            LIST_INITIAL_VALUE(suite_name##_##bench_name##_metric_list); \
    static void suite_name##_##bench_name##_bench_(void) {               \
        bench_state.cur_param_idx = 0;                                   \
        trusty_bench_nb_cpu = nb_cpu;                                    \
        BENCH_CORE(suite_name, bench_name, nb_runs, 1, ,                 \
                   suite_name##_##bench_name##_metric_list);             \
    }                                                                    \
                                                                         \
    PARAM_TEST_NODES(suite_name, bench_name)                             \
    static int suite_name##_##bench_name##_inner_(void)

/*
 * A few helper macros for static dispatch of BENCH
 */
#define NB_ARGS_HELPER(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
#define NB_ARGS(...) NB_ARGS_HELPER(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0)

#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
#define PRIMITIVE_CAT(a, ...) a##__VA_ARGS__

#define EVAL(...) __VA_ARGS__

/*
 * BENCH - Routing the BENCH macros depending on its number of parameters.
 */
#define BENCH_3 BENCH_SIMPLE
#define BENCH_4 BENCH_PARAMETERIZED
#define BENCH_5 BENCH_PARAMETERIZED_PTR

/**
 * BENCH - Called 3, 4 or 5 parameters. This allows
 *                          to reuse Other macros for different bench by
 * aliasing an array to a pointer
 * @suite_name:             Identifier of the current suite.
 * @bench_name:             Unique identifier of the Bench in the suite.
 * @nb_runs:                The number of execution of its body for each param
 * @params:                 [optional] An array T array_name[nb_params] of
 *                          parameter, or a pointer T*, in the latter case a 5th
 *                          parameter is needed
 * @nb_params:              [optional] if 4th parameter is a pointer, Number of
 *                          parameters in the parameter Array
 */
#define BENCH(...) CAT(BENCH_, EVAL(NB_ARGS(__VA_ARGS__)))(1, __VA_ARGS__)

/**
 * BENCH_ALL_CPU - Called 3, 4 or 5 parameters. This allows
 *                          to reuse Other macros for different bench by
 * aliasing an array to a pointer
 * @suite_name:             Identifier of the current suite.
 * @bench_name:             Unique identifier of the Bench in the suite.
 * @nb_runs:                The number of execution of its body for each param
 * @params:                 [optional] An array T array_name[nb_params] of
 *                          parameter, or a pointer T*, in the latter case a 5th
 *                          parameter is needed
 * @nb_params:              [optional] if 4th parameter is a pointer, Number of
 *                          parameters in the parameter Array
 */
#define BENCH_ALL_CPU(...) \
    CAT(BENCH_, EVAL(NB_ARGS(__VA_ARGS__)))(SMP_MAX_CPUS, __VA_ARGS__)

/*
 * PARAM_TEST_NODES - Routing the PARAM_TEST_NODES macros depending on its
 * number of parameters.
 */
#define PARAM_TEST_NODES_2 PARAM_TEST_NODES_SIMPLE
#define PARAM_TEST_NODES_3 PARAM_TEST_NODES_PARAMETRIC
#define PARAM_TEST_NODES(...) \
    CAT(PARAM_TEST_NODES_, EVAL(NB_ARGS(__VA_ARGS__)))(__VA_ARGS__)

__END_CDECLS