0001
0002
0003
0004
0005
0006
0007
0008 #ifndef SELFTEST_KVM_TEST_UTIL_H
0009 #define SELFTEST_KVM_TEST_UTIL_H
0010
0011 #include <stdlib.h>
0012 #include <stdarg.h>
0013 #include <stdbool.h>
0014 #include <stdio.h>
0015 #include <string.h>
0016 #include <inttypes.h>
0017 #include <errno.h>
0018 #include <unistd.h>
0019 #include <fcntl.h>
0020 #include <sys/mman.h>
0021 #include "kselftest.h"
0022
0023 static inline int _no_printf(const char *format, ...) { return 0; }
0024
0025 #ifdef DEBUG
0026 #define pr_debug(...) printf(__VA_ARGS__)
0027 #else
0028 #define pr_debug(...) _no_printf(__VA_ARGS__)
0029 #endif
0030 #ifndef QUIET
0031 #define pr_info(...) printf(__VA_ARGS__)
0032 #else
0033 #define pr_info(...) _no_printf(__VA_ARGS__)
0034 #endif
0035
0036 void print_skip(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
0037 #define __TEST_REQUIRE(f, fmt, ...) \
0038 do { \
0039 if (!(f)) \
0040 ksft_exit_skip("- " fmt "\n", ##__VA_ARGS__); \
0041 } while (0)
0042
0043 #define TEST_REQUIRE(f) __TEST_REQUIRE(f, "Requirement not met: %s", #f)
0044
0045 ssize_t test_write(int fd, const void *buf, size_t count);
0046 ssize_t test_read(int fd, void *buf, size_t count);
0047 int test_seq_read(const char *path, char **bufp, size_t *sizep);
0048
0049 void test_assert(bool exp, const char *exp_str,
0050 const char *file, unsigned int line, const char *fmt, ...)
0051 __attribute__((format(printf, 5, 6)));
0052
0053 #define TEST_ASSERT(e, fmt, ...) \
0054 test_assert((e), #e, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
0055
0056 #define ASSERT_EQ(a, b) do { \
0057 typeof(a) __a = (a); \
0058 typeof(b) __b = (b); \
0059 TEST_ASSERT(__a == __b, \
0060 "ASSERT_EQ(%s, %s) failed.\n" \
0061 "\t%s is %#lx\n" \
0062 "\t%s is %#lx", \
0063 #a, #b, #a, (unsigned long) __a, #b, (unsigned long) __b); \
0064 } while (0)
0065
0066 #define TEST_FAIL(fmt, ...) \
0067 TEST_ASSERT(false, fmt, ##__VA_ARGS__)
0068
0069 size_t parse_size(const char *size);
0070
0071 int64_t timespec_to_ns(struct timespec ts);
0072 struct timespec timespec_add_ns(struct timespec ts, int64_t ns);
0073 struct timespec timespec_add(struct timespec ts1, struct timespec ts2);
0074 struct timespec timespec_sub(struct timespec ts1, struct timespec ts2);
0075 struct timespec timespec_elapsed(struct timespec start);
0076 struct timespec timespec_div(struct timespec ts, int divisor);
0077
0078 enum vm_mem_backing_src_type {
0079 VM_MEM_SRC_ANONYMOUS,
0080 VM_MEM_SRC_ANONYMOUS_THP,
0081 VM_MEM_SRC_ANONYMOUS_HUGETLB,
0082 VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB,
0083 VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB,
0084 VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB,
0085 VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB,
0086 VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB,
0087 VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB,
0088 VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB,
0089 VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB,
0090 VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB,
0091 VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB,
0092 VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB,
0093 VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB,
0094 VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB,
0095 VM_MEM_SRC_SHMEM,
0096 VM_MEM_SRC_SHARED_HUGETLB,
0097 NUM_SRC_TYPES,
0098 };
0099
0100 #define DEFAULT_VM_MEM_SRC VM_MEM_SRC_ANONYMOUS
0101
0102 struct vm_mem_backing_src_alias {
0103 const char *name;
0104 uint32_t flag;
0105 };
0106
0107 #define MIN_RUN_DELAY_NS 200000UL
0108
0109 bool thp_configured(void);
0110 size_t get_trans_hugepagesz(void);
0111 size_t get_def_hugetlb_pagesz(void);
0112 const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i);
0113 size_t get_backing_src_pagesz(uint32_t i);
0114 bool is_backing_src_hugetlb(uint32_t i);
0115 void backing_src_help(const char *flag);
0116 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
0117 long get_run_delay(void);
0118
0119
0120
0121
0122
0123 static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t)
0124 {
0125 return vm_mem_backing_src_alias(t)->flag & MAP_SHARED;
0126 }
0127
0128
0129 static inline uint64_t align_up(uint64_t x, uint64_t size)
0130 {
0131 uint64_t mask = size - 1;
0132
0133 TEST_ASSERT(size != 0 && !(size & (size - 1)),
0134 "size not a power of 2: %lu", size);
0135 return ((x + mask) & ~mask);
0136 }
0137
0138 static inline uint64_t align_down(uint64_t x, uint64_t size)
0139 {
0140 uint64_t x_aligned_up = align_up(x, size);
0141
0142 if (x == x_aligned_up)
0143 return x;
0144 else
0145 return x_aligned_up - size;
0146 }
0147
0148 static inline void *align_ptr_up(void *x, size_t size)
0149 {
0150 return (void *)align_up((unsigned long)x, size);
0151 }
0152
0153 #endif