vppinfra: toeplitz hash four in parallel
[vpp.git] / src / vppinfra / vector / test / test.h
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>
9 #ifdef __linux__
10 #include <sys/ioctl.h>
11 #include <linux/perf_event.h>
12 #endif
13
14 typedef clib_error_t *(test_fn_t) (clib_error_t *);
15
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;
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   };
37   char *op_name;
38   char *name;
39   test_perf_fn_t *fn;
40 } test_perf_t;
41
42 typedef struct test_registration_
43 {
44   char *name;
45   u8 multiarch : 1;
46   test_fn_t *fn;
47   test_perf_t *perf_tests;
48   u32 n_perf_tests;
49   struct test_registration_ *next;
50 } test_registration_t;
51
52 typedef struct
53 {
54   test_registration_t *registrations[CLIB_MARCH_TYPE_N_VARIANTS];
55   u32 repeat;
56   u8 *filter;
57   u8 *bundle;
58   f64 ref_clock;
59 } test_main_t;
60 extern test_main_t test_main;
61
62 #define __test_funct_fn                                                       \
63   static __clib_noinline __clib_noclone __clib_section (".test_func")
64 #define __test_perf_fn                                                        \
65   static __clib_noinline __clib_noclone __clib_section (".test_perf")
66
67 #define REGISTER_TEST(x)                                                      \
68   test_registration_t CLIB_MARCH_SFX (__test_##x);                            \
69   static void __clib_constructor CLIB_MARCH_SFX (__test_registration_##x) (   \
70     void)                                                                     \
71   {                                                                           \
72     test_registration_t *r = &CLIB_MARCH_SFX (__test_##x);                    \
73     r->next =                                                                 \
74       test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)];      \
75     test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)] = r;    \
76   }                                                                           \
77   test_registration_t CLIB_MARCH_SFX (__test_##x)
78
79 #define PERF_TESTS(...)                                                       \
80   (test_perf_t[])                                                             \
81   {                                                                           \
82     __VA_ARGS__, {}                                                           \
83   }
84
85 static_always_inline void
86 test_perf_event_ioctl (int fd, u32 req)
87 {
88 #ifdef __x86_64__
89   asm inline("syscall"
90              :
91              : "D"(fd), "S"(req), "a"(__NR_ioctl), "d"(PERF_IOC_FLAG_GROUP)
92              : "rcx", "r11" /* registers modified by kernel */);
93 #else
94   ioctl (fd, req, PERF_IOC_FLAG_GROUP);
95 #endif
96 }
97
98 static_always_inline void
99 test_perf_event_reset (int fd)
100 {
101   test_perf_event_ioctl (fd, PERF_EVENT_IOC_RESET);
102 }
103 static_always_inline void
104 test_perf_event_enable (int fd)
105 {
106   test_perf_event_ioctl (fd, PERF_EVENT_IOC_ENABLE);
107 }
108 static_always_inline void
109 test_perf_event_disable (int fd)
110 {
111   test_perf_event_ioctl (fd, PERF_EVENT_IOC_DISABLE);
112 }
113
114 void *test_mem_alloc (uword size);
115 void *test_mem_alloc_and_fill_inc_u8 (uword size, u8 start, u8 mask);
116 void *test_mem_alloc_and_splat (uword elt_size, uword n_elts, void *elt);
117 void test_mem_free (void *p);
118
119 #endif