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 | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 9 | #ifdef __linux__ |
| 10 | #include <sys/ioctl.h> |
| 11 | #include <linux/perf_event.h> |
| 12 | #endif |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 13 | |
| 14 | typedef clib_error_t *(test_fn_t) (clib_error_t *); |
| 15 | |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 16 | struct test_perf_; |
| 17 | typedef void (test_perf_fn_t) (int fd, struct test_perf_ *tp); |
| 18 | |
| 19 | typedef struct test_perf_ |
| 20 | { |
| 21 | u64 n_ops; |
Damjan Marion | de3735f | 2021-12-06 12:48:46 +0100 | [diff] [blame] | 22 | union |
| 23 | { |
| 24 | u64 arg0; |
| 25 | void *ptr0; |
| 26 | }; |
| 27 | union |
| 28 | { |
| 29 | u64 arg1; |
| 30 | void *ptr1; |
| 31 | }; |
| 32 | union |
| 33 | { |
| 34 | u64 arg2; |
| 35 | void *ptr2; |
| 36 | }; |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 37 | char *op_name; |
| 38 | char *name; |
| 39 | test_perf_fn_t *fn; |
| 40 | } test_perf_t; |
| 41 | |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 42 | typedef struct test_registration_ |
| 43 | { |
| 44 | char *name; |
| 45 | u8 multiarch : 1; |
| 46 | test_fn_t *fn; |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 47 | test_perf_t *perf_tests; |
| 48 | u32 n_perf_tests; |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 49 | struct test_registration_ *next; |
| 50 | } test_registration_t; |
| 51 | |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 52 | typedef struct |
| 53 | { |
| 54 | test_registration_t *registrations[CLIB_MARCH_TYPE_N_VARIANTS]; |
| 55 | u32 repeat; |
Damjan Marion | 68843d1 | 2021-12-06 13:21:33 +0100 | [diff] [blame] | 56 | u8 *filter; |
Damjan Marion | b97bec0 | 2021-12-12 21:43:56 +0000 | [diff] [blame] | 57 | u8 *bundle; |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 58 | } test_main_t; |
| 59 | extern test_main_t test_main; |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 60 | |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 61 | #define __test_funct_fn static __clib_noinline __clib_section (".test_func") |
| 62 | #define __test_perf_fn static __clib_noinline __clib_section (".test_perf") |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 63 | |
| 64 | #define REGISTER_TEST(x) \ |
| 65 | test_registration_t CLIB_MARCH_SFX (__test_##x); \ |
| 66 | static void __clib_constructor CLIB_MARCH_SFX (__test_registration_##x) ( \ |
| 67 | void) \ |
| 68 | { \ |
| 69 | test_registration_t *r = &CLIB_MARCH_SFX (__test_##x); \ |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 70 | r->next = \ |
| 71 | test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)]; \ |
| 72 | test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)] = r; \ |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 73 | } \ |
| 74 | test_registration_t CLIB_MARCH_SFX (__test_##x) |
| 75 | |
Damjan Marion | 3323e20 | 2021-12-02 11:28:57 +0100 | [diff] [blame] | 76 | #define PERF_TESTS(...) \ |
| 77 | (test_perf_t[]) \ |
| 78 | { \ |
| 79 | __VA_ARGS__, {} \ |
| 80 | } |
| 81 | |
| 82 | static_always_inline void |
| 83 | test_perf_event_ioctl (int fd, u32 req) |
| 84 | { |
| 85 | #ifdef __x86_64__ |
| 86 | asm inline("syscall" |
| 87 | : |
| 88 | : "D"(fd), "S"(req), "a"(__NR_ioctl), "d"(PERF_IOC_FLAG_GROUP) |
| 89 | : "rcx", "r11" /* registers modified by kernel */); |
| 90 | #else |
| 91 | ioctl (fd, req, PERF_IOC_FLAG_GROUP); |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | static_always_inline void |
| 96 | test_perf_event_reset (int fd) |
| 97 | { |
| 98 | test_perf_event_ioctl (fd, PERF_EVENT_IOC_RESET); |
| 99 | } |
| 100 | static_always_inline void |
| 101 | test_perf_event_enable (int fd) |
| 102 | { |
| 103 | test_perf_event_ioctl (fd, PERF_EVENT_IOC_ENABLE); |
| 104 | } |
| 105 | static_always_inline void |
| 106 | test_perf_event_disable (int fd) |
| 107 | { |
| 108 | test_perf_event_ioctl (fd, PERF_EVENT_IOC_DISABLE); |
| 109 | } |
| 110 | |
| 111 | void *test_mem_alloc (uword size); |
| 112 | void *test_mem_alloc_and_fill_inc_u8 (uword size, u8 start, u8 mask); |
| 113 | void *test_mem_alloc_and_splat (uword elt_size, uword n_elts, void *elt); |
| 114 | void test_mem_free (void *p); |
| 115 | |
Damjan Marion | e3e3555 | 2021-05-06 17:34:49 +0200 | [diff] [blame] | 116 | #endif |