aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/vector/test/test.h
blob: 4334dfa53a1f288277dd0d463d6be046f36193bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* SPDX-License-Identifier: Apache-2.0
 * Copyright(c) 2021 Cisco Systems, Inc.
 */

#ifndef included_test_test_h
#define included_test_test_h

#include <vppinfra/cpu.h>
#ifdef __linux__
#include <sys/ioctl.h>
#include <linux/perf_event.h>
#endif

typedef clib_error_t *(test_fn_t) (clib_error_t *);

struct test_perf_;
typedef void (test_perf_fn_t) (int fd, struct test_perf_ *tp);

typedef struct test_perf_
{
  u64 n_ops;
  union
  {
    u64 arg0;
    void *ptr0;
  };
  union
  {
    u64 arg1;
    void *ptr1;
  };
  union
  {
    u64 arg2;
    void *ptr2;
  };
  char *op_name;
  char *name;
  test_perf_fn_t *fn;
} test_perf_t;

typedef struct test_registration_
{
  char *name;
  u8 multiarch : 1;
  test_fn_t *fn;
  test_perf_t *perf_tests;
  u32 n_perf_tests;
  struct test_registration_ *next;
} test_registration_t;

typedef struct
{
  test_registration_t *registrations[CLIB_MARCH_TYPE_N_VARIANTS];
  u32 repeat;
  u8 *filter;
  u8 *bundle;
  f64 ref_clock;
} test_main_t;
extern test_main_t test_main;

#define __test_funct_fn static __clib_noinline __clib_section (".test_func")
#define __test_perf_fn	static __clib_noinline __clib_section (".test_perf")

#define REGISTER_TEST(x)                                                      \
  test_registration_t CLIB_MARCH_SFX (__test_##x);                            \
  static void __clib_constructor CLIB_MARCH_SFX (__test_registration_##x) (   \
    void)                                                                     \
  {                                                                           \
    test_registration_t *r = &CLIB_MARCH_SFX (__test_##x);                    \
    r->next =                                                                 \
      test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)];      \
    test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)] = r;    \
  }                                                                           \
  test_registration_t CLIB_MARCH_SFX (__test_##x)

#define PERF_TESTS(...)                                                       \
  (test_perf_t[])                                                             \
  {                                                                           \
    __VA_ARGS__, {}                                                           \
  }

static_always_inline void
test_perf_event_ioctl (int fd, u32 req)
{
#ifdef __x86_64__
  asm inline("syscall"
	     :
	     : "D"(fd), "S"(req), "a"(__NR_ioctl), "d"(PERF_IOC_FLAG_GROUP)
	     : "rcx", "r11" /* registers modified by kernel */);
#else
  ioctl (fd, req, PERF_IOC_FLAG_GROUP);
#endif
}

static_always_inline void
test_perf_event_reset (int fd)
{
  test_perf_event_ioctl (fd, PERF_EVENT_IOC_RESET);
}
static_always_inline void
test_perf_event_enable (int fd)
{
  test_perf_event_ioctl (fd, PERF_EVENT_IOC_ENABLE);
}
static_always_inline void
test_perf_event_disable (int fd)
{
  test_perf_event_ioctl (fd, PERF_EVENT_IOC_DISABLE);
}

void *test_mem_alloc (uword size);
void *test_mem_alloc_and_fill_inc_u8 (uword size, u8 start, u8 mask);
void *test_mem_alloc_and_splat (uword elt_size, uword n_elts, void *elt);
void test_mem_free (void *p);

#endif