vppinfra: widen the scope of test_vector_funcs
[vpp.git] / src / vppinfra / 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 #include <vppinfra/perfmon/perfmon.h>
10 #ifdef __linux__
11 #include <sys/ioctl.h>
12 #include <linux/perf_event.h>
13 #endif
14
15 typedef clib_error_t *(test_fn_t) (clib_error_t *);
16
17 struct test_perf_;
18 typedef void (test_perf_fn_t) (struct test_perf_ *tp);
19
20 typedef struct test_perf_
21 {
22   int fd;
23   u64 n_ops;
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   };
39   char *name;
40   test_perf_fn_t *fn;
41 } test_perf_t;
42
43 typedef struct test_registration_
44 {
45   char *name;
46   u8 multiarch : 1;
47   test_fn_t *fn;
48   test_perf_t *perf_tests;
49   u32 n_perf_tests;
50   struct test_registration_ *next;
51 } test_registration_t;
52
53 typedef struct
54 {
55   test_registration_t *registrations[CLIB_MARCH_TYPE_N_VARIANTS];
56   u32 repeat;
57   u8 *filter;
58   u8 *bundle;
59   f64 ref_clock;
60 } test_main_t;
61 extern test_main_t test_main;
62
63 #define __test_funct_fn                                                       \
64   static __clib_noinline __clib_noclone __clib_section (".test_func")
65 #define __test_perf_fn                                                        \
66   static __clib_noinline __clib_noclone __clib_section (".test_perf")
67
68 #define REGISTER_TEST(x)                                                      \
69   test_registration_t CLIB_MARCH_SFX (__test_##x);                            \
70   static void __clib_constructor CLIB_MARCH_SFX (__test_registration_##x) (   \
71     void)                                                                     \
72   {                                                                           \
73     test_registration_t *r = &CLIB_MARCH_SFX (__test_##x);                    \
74     r->next =                                                                 \
75       test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)];      \
76     test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)] = r;    \
77   }                                                                           \
78   test_registration_t CLIB_MARCH_SFX (__test_##x)
79
80 #define PERF_TESTS(...)                                                       \
81   (test_perf_t[])                                                             \
82   {                                                                           \
83     __VA_ARGS__, {}                                                           \
84   }
85
86 static_always_inline void
87 test_perf_event_reset (test_perf_t *t)
88 {
89   clib_perfmon_ioctl (t->fd, PERF_EVENT_IOC_RESET);
90 }
91 static_always_inline void
92 test_perf_event_enable (test_perf_t *t)
93 {
94   clib_perfmon_ioctl (t->fd, PERF_EVENT_IOC_ENABLE);
95 }
96 static_always_inline void
97 test_perf_event_disable (test_perf_t *t)
98 {
99   clib_perfmon_ioctl (t->fd, PERF_EVENT_IOC_DISABLE);
100 }
101
102 void *test_mem_alloc (uword size);
103 void *test_mem_alloc_and_fill_inc_u8 (uword size, u8 start, u8 mask);
104 void *test_mem_alloc_and_splat (uword elt_size, uword n_elts, void *elt);
105 void test_mem_free (void *p);
106
107 #endif