blob: 5b26996551b2cd33a7f743c5e5f9fa8acd45ef87 [file] [log] [blame]
Damjan Marione3e35552021-05-06 17:34:49 +02001/* 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 Marion3323e202021-12-02 11:28:57 +01009#ifdef __linux__
10#include <sys/ioctl.h>
11#include <linux/perf_event.h>
12#endif
Damjan Marione3e35552021-05-06 17:34:49 +020013
14typedef clib_error_t *(test_fn_t) (clib_error_t *);
15
Damjan Marion3323e202021-12-02 11:28:57 +010016struct test_perf_;
17typedef void (test_perf_fn_t) (int fd, struct test_perf_ *tp);
18
19typedef struct test_perf_
20{
21 u64 n_ops;
Damjan Marionde3735f2021-12-06 12:48:46 +010022 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 Marion3323e202021-12-02 11:28:57 +010037 char *op_name;
38 char *name;
39 test_perf_fn_t *fn;
40} test_perf_t;
41
Damjan Marione3e35552021-05-06 17:34:49 +020042typedef struct test_registration_
43{
44 char *name;
45 u8 multiarch : 1;
46 test_fn_t *fn;
Damjan Marion3323e202021-12-02 11:28:57 +010047 test_perf_t *perf_tests;
48 u32 n_perf_tests;
Damjan Marione3e35552021-05-06 17:34:49 +020049 struct test_registration_ *next;
50} test_registration_t;
51
Damjan Marion3323e202021-12-02 11:28:57 +010052typedef struct
53{
54 test_registration_t *registrations[CLIB_MARCH_TYPE_N_VARIANTS];
55 u32 repeat;
56} test_main_t;
57extern test_main_t test_main;
Damjan Marione3e35552021-05-06 17:34:49 +020058
Damjan Marion3323e202021-12-02 11:28:57 +010059#define __test_funct_fn static __clib_noinline __clib_section (".test_func")
60#define __test_perf_fn static __clib_noinline __clib_section (".test_perf")
Damjan Marione3e35552021-05-06 17:34:49 +020061
62#define REGISTER_TEST(x) \
63 test_registration_t CLIB_MARCH_SFX (__test_##x); \
64 static void __clib_constructor CLIB_MARCH_SFX (__test_registration_##x) ( \
65 void) \
66 { \
67 test_registration_t *r = &CLIB_MARCH_SFX (__test_##x); \
Damjan Marion3323e202021-12-02 11:28:57 +010068 r->next = \
69 test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)]; \
70 test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)] = r; \
Damjan Marione3e35552021-05-06 17:34:49 +020071 } \
72 test_registration_t CLIB_MARCH_SFX (__test_##x)
73
Damjan Marion3323e202021-12-02 11:28:57 +010074#define PERF_TESTS(...) \
75 (test_perf_t[]) \
76 { \
77 __VA_ARGS__, {} \
78 }
79
80static_always_inline void
81test_perf_event_ioctl (int fd, u32 req)
82{
83#ifdef __x86_64__
84 asm inline("syscall"
85 :
86 : "D"(fd), "S"(req), "a"(__NR_ioctl), "d"(PERF_IOC_FLAG_GROUP)
87 : "rcx", "r11" /* registers modified by kernel */);
88#else
89 ioctl (fd, req, PERF_IOC_FLAG_GROUP);
90#endif
91}
92
93static_always_inline void
94test_perf_event_reset (int fd)
95{
96 test_perf_event_ioctl (fd, PERF_EVENT_IOC_RESET);
97}
98static_always_inline void
99test_perf_event_enable (int fd)
100{
101 test_perf_event_ioctl (fd, PERF_EVENT_IOC_ENABLE);
102}
103static_always_inline void
104test_perf_event_disable (int fd)
105{
106 test_perf_event_ioctl (fd, PERF_EVENT_IOC_DISABLE);
107}
108
109void *test_mem_alloc (uword size);
110void *test_mem_alloc_and_fill_inc_u8 (uword size, u8 start, u8 mask);
111void *test_mem_alloc_and_splat (uword elt_size, uword n_elts, void *elt);
112void test_mem_free (void *p);
113
Damjan Marione3e35552021-05-06 17:34:49 +0200114#endif