Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright(c) 2021 Cisco Systems, Inc. |
| 3 | */ |
| 4 | |
| 5 | #ifndef included_test_test_h |
| 6 | #define included_test_test_h |
| 7 | |
| 8 | #include <vppinfra/cpu.h> |
Damjan Marion | d5045e6 | 2022-04-06 21:16:37 +0200 | [diff] [blame] | 9 | #include <vppinfra/perfmon/perfmon.h> |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 10 | #ifdef __linux__ |
| 11 | #include <sys/ioctl.h> |
| 12 | #include <linux/perf_event.h> |
| 13 | #endif |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 14 | |
| 15 | typedef clib_error_t *(test_fn_t) (clib_error_t *); |
| 16 | |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 17 | struct test_perf_; |
Damjan Marion | d5045e6 | 2022-04-06 21:16:37 +0200 | [diff] [blame] | 18 | typedef void (test_perf_fn_t) (struct test_perf_ *tp); |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 19 | |
| 20 | typedef struct test_perf_ |
| 21 | { |
Damjan Marion | d5045e6 | 2022-04-06 21:16:37 +0200 | [diff] [blame] | 22 | int fd; |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 23 | u64 n_ops; |
Damjan Marion | de3735f | 2021-12-06 12:48:46 +0100 | [diff] [blame] | 24 | union |
| 25 | { |
| 26 | u64 arg0; |
| 27 | void *ptr0; |
| 28 | }; |
| 29 | union |
| 30 | { |
| 31 | u64 arg1; |
| 32 | void *ptr1; |
| 33 | }; |
| 34 | union |
| 35 | { |
| 36 | u64 arg2; |
| 37 | void *ptr2; |
| 38 | }; |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 39 | char *name; |
| 40 | test_perf_fn_t *fn; |
| 41 | } test_perf_t; |
| 42 | |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 43 | typedef struct test_registration_ |
| 44 | { |
| 45 | char *name; |
| 46 | u8 multiarch : 1; |
| 47 | test_fn_t *fn; |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 48 | test_perf_t *perf_tests; |
| 49 | u32 n_perf_tests; |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 50 | struct test_registration_ *next; |
| 51 | } test_registration_t; |
| 52 | |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 53 | typedef struct |
| 54 | { |
| 55 | test_registration_t *registrations[CLIB_MARCH_TYPE_N_VARIANTS]; |
| 56 | u32 repeat; |
Damjan Marion | 68843d1 | 2021-12-06 13:21:33 +0100 | [diff] [blame] | 57 | u8 *filter; |
Damjan Marion | b97bec0 | 2021-12-12 21:43:56 +0000 | [diff] [blame] | 58 | u8 *bundle; |
Damjan Marion | f622bf4 | 2021-12-14 10:40:48 +0100 | [diff] [blame] | 59 | f64 ref_clock; |
Damjan Marion | c7d3a5b | 2023-03-16 16:55:38 +0000 | [diff] [blame] | 60 | void **allocated_mem; |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 61 | } test_main_t; |
| 62 | extern test_main_t test_main; |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 63 | |
Damjan Marion | 88019c4 | 2021-12-15 10:17:04 +0000 | [diff] [blame] | 64 | #define __test_funct_fn \ |
| 65 | static __clib_noinline __clib_noclone __clib_section (".test_func") |
| 66 | #define __test_perf_fn \ |
| 67 | static __clib_noinline __clib_noclone __clib_section (".test_perf") |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 68 | |
| 69 | #define REGISTER_TEST(x) \ |
| 70 | test_registration_t CLIB_MARCH_SFX (__test_##x); \ |
| 71 | static void __clib_constructor CLIB_MARCH_SFX (__test_registration_##x) ( \ |
| 72 | void) \ |
| 73 | { \ |
| 74 | test_registration_t *r = &CLIB_MARCH_SFX (__test_##x); \ |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 75 | r->next = \ |
| 76 | test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)]; \ |
| 77 | test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)] = r; \ |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 78 | } \ |
| 79 | test_registration_t CLIB_MARCH_SFX (__test_##x) |
| 80 | |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 81 | #define PERF_TESTS(...) \ |
| 82 | (test_perf_t[]) \ |
| 83 | { \ |
| 84 | __VA_ARGS__, {} \ |
| 85 | } |
| 86 | |
| 87 | static_always_inline void |
Damjan Marion | d5045e6 | 2022-04-06 21:16:37 +0200 | [diff] [blame] | 88 | test_perf_event_reset (test_perf_t *t) |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 89 | { |
Damjan Marion | d5045e6 | 2022-04-06 21:16:37 +0200 | [diff] [blame] | 90 | clib_perfmon_ioctl (t->fd, PERF_EVENT_IOC_RESET); |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 91 | } |
| 92 | static_always_inline void |
Damjan Marion | d5045e6 | 2022-04-06 21:16:37 +0200 | [diff] [blame] | 93 | test_perf_event_enable (test_perf_t *t) |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 94 | { |
Damjan Marion | d5045e6 | 2022-04-06 21:16:37 +0200 | [diff] [blame] | 95 | clib_perfmon_ioctl (t->fd, PERF_EVENT_IOC_ENABLE); |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 96 | } |
| 97 | static_always_inline void |
Damjan Marion | d5045e6 | 2022-04-06 21:16:37 +0200 | [diff] [blame] | 98 | test_perf_event_disable (test_perf_t *t) |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 99 | { |
Damjan Marion | d5045e6 | 2022-04-06 21:16:37 +0200 | [diff] [blame] | 100 | clib_perfmon_ioctl (t->fd, PERF_EVENT_IOC_DISABLE); |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void *test_mem_alloc (uword size); |
| 104 | void *test_mem_alloc_and_fill_inc_u8 (uword size, u8 start, u8 mask); |
| 105 | void *test_mem_alloc_and_splat (uword elt_size, uword n_elts, void *elt); |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 106 | |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 107 | #endif |