vppinfra: auto-free test memory
[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   void **allocated_mem;
61 } test_main_t;
62 extern test_main_t test_main;
63
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")
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);                    \
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;    \
78   }                                                                           \
79   test_registration_t CLIB_MARCH_SFX (__test_##x)
80
81 #define PERF_TESTS(...)                                                       \
82   (test_perf_t[])                                                             \
83   {                                                                           \
84     __VA_ARGS__, {}                                                           \
85   }
86
87 static_always_inline void
88 test_perf_event_reset (test_perf_t *t)
89 {
90   clib_perfmon_ioctl (t->fd, PERF_EVENT_IOC_RESET);
91 }
92 static_always_inline void
93 test_perf_event_enable (test_perf_t *t)
94 {
95   clib_perfmon_ioctl (t->fd, PERF_EVENT_IOC_ENABLE);
96 }
97 static_always_inline void
98 test_perf_event_disable (test_perf_t *t)
99 {
100   clib_perfmon_ioctl (t->fd, PERF_EVENT_IOC_DISABLE);
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);
106
107 #endif