1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * tools/testing/selftests/kvm/include/test_util.h
5 * Copyright (C) 2018, Google LLC.
8 #ifndef SELFTEST_KVM_TEST_UTIL_H
9 #define SELFTEST_KVM_TEST_UTIL_H
21 #include "kselftest.h"
23 static inline int _no_printf(const char *format, ...) { return 0; }
26 #define pr_debug(...) printf(__VA_ARGS__)
28 #define pr_debug(...) _no_printf(__VA_ARGS__)
31 #define pr_info(...) printf(__VA_ARGS__)
33 #define pr_info(...) _no_printf(__VA_ARGS__)
36 void print_skip(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
38 ssize_t test_write(int fd, const void *buf, size_t count);
39 ssize_t test_read(int fd, void *buf, size_t count);
40 int test_seq_read(const char *path, char **bufp, size_t *sizep);
42 void test_assert(bool exp, const char *exp_str,
43 const char *file, unsigned int line, const char *fmt, ...)
44 __attribute__((format(printf, 5, 6)));
46 #define TEST_ASSERT(e, fmt, ...) \
47 test_assert((e), #e, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
49 #define ASSERT_EQ(a, b) do { \
50 typeof(a) __a = (a); \
51 typeof(b) __b = (b); \
52 TEST_ASSERT(__a == __b, \
53 "ASSERT_EQ(%s, %s) failed.\n" \
56 #a, #b, #a, (unsigned long) __a, #b, (unsigned long) __b); \
59 #define TEST_FAIL(fmt, ...) \
60 TEST_ASSERT(false, fmt, ##__VA_ARGS__)
62 size_t parse_size(const char *size);
64 int64_t timespec_to_ns(struct timespec ts);
65 struct timespec timespec_add_ns(struct timespec ts, int64_t ns);
66 struct timespec timespec_add(struct timespec ts1, struct timespec ts2);
67 struct timespec timespec_sub(struct timespec ts1, struct timespec ts2);
68 struct timespec timespec_elapsed(struct timespec start);
69 struct timespec timespec_div(struct timespec ts, int divisor);
71 enum vm_mem_backing_src_type {
73 VM_MEM_SRC_ANONYMOUS_THP,
74 VM_MEM_SRC_ANONYMOUS_HUGETLB,
75 VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB,
76 VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB,
77 VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB,
78 VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB,
79 VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB,
80 VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB,
81 VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB,
82 VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB,
83 VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB,
84 VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB,
85 VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB,
86 VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB,
87 VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB,
89 VM_MEM_SRC_SHARED_HUGETLB,
93 #define DEFAULT_VM_MEM_SRC VM_MEM_SRC_ANONYMOUS
95 struct vm_mem_backing_src_alias {
100 #define MIN_RUN_DELAY_NS 200000UL
102 bool thp_configured(void);
103 size_t get_trans_hugepagesz(void);
104 size_t get_def_hugetlb_pagesz(void);
105 const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i);
106 size_t get_backing_src_pagesz(uint32_t i);
107 void backing_src_help(const char *flag);
108 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
109 long get_run_delay(void);
112 * Whether or not the given source type is shared memory (as opposed to
115 static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t)
117 return vm_mem_backing_src_alias(t)->flag & MAP_SHARED;
120 #endif /* SELFTEST_KVM_TEST_UTIL_H */