1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * tools/testing/selftests/kvm/include/test_util.h 4 * 5 * Copyright (C) 2018, Google LLC. 6 */ 7 8 #ifndef SELFTEST_KVM_TEST_UTIL_H 9 #define SELFTEST_KVM_TEST_UTIL_H 10 11 #include <stdlib.h> 12 #include <stdarg.h> 13 #include <stdbool.h> 14 #include <stdio.h> 15 #include <string.h> 16 #include <inttypes.h> 17 #include <errno.h> 18 #include <unistd.h> 19 #include <fcntl.h> 20 #include "kselftest.h" 21 22 static inline int _no_printf(const char *format, ...) { return 0; } 23 24 #ifdef DEBUG 25 #define pr_debug(...) printf(__VA_ARGS__) 26 #else 27 #define pr_debug(...) _no_printf(__VA_ARGS__) 28 #endif 29 #ifndef QUIET 30 #define pr_info(...) printf(__VA_ARGS__) 31 #else 32 #define pr_info(...) _no_printf(__VA_ARGS__) 33 #endif 34 35 void print_skip(const char *fmt, ...) __attribute__((format(printf, 1, 2))); 36 37 ssize_t test_write(int fd, const void *buf, size_t count); 38 ssize_t test_read(int fd, void *buf, size_t count); 39 int test_seq_read(const char *path, char **bufp, size_t *sizep); 40 41 void test_assert(bool exp, const char *exp_str, 42 const char *file, unsigned int line, const char *fmt, ...) 43 __attribute__((format(printf, 5, 6))); 44 45 #define TEST_ASSERT(e, fmt, ...) \ 46 test_assert((e), #e, __FILE__, __LINE__, fmt, ##__VA_ARGS__) 47 48 #define ASSERT_EQ(a, b) do { \ 49 typeof(a) __a = (a); \ 50 typeof(b) __b = (b); \ 51 TEST_ASSERT(__a == __b, \ 52 "ASSERT_EQ(%s, %s) failed.\n" \ 53 "\t%s is %#lx\n" \ 54 "\t%s is %#lx", \ 55 #a, #b, #a, (unsigned long) __a, #b, (unsigned long) __b); \ 56 } while (0) 57 58 #define TEST_FAIL(fmt, ...) \ 59 TEST_ASSERT(false, fmt, ##__VA_ARGS__) 60 61 size_t parse_size(const char *size); 62 63 int64_t timespec_to_ns(struct timespec ts); 64 struct timespec timespec_add_ns(struct timespec ts, int64_t ns); 65 struct timespec timespec_add(struct timespec ts1, struct timespec ts2); 66 struct timespec timespec_sub(struct timespec ts1, struct timespec ts2); 67 struct timespec timespec_elapsed(struct timespec start); 68 struct timespec timespec_div(struct timespec ts, int divisor); 69 70 enum vm_mem_backing_src_type { 71 VM_MEM_SRC_ANONYMOUS, 72 VM_MEM_SRC_ANONYMOUS_THP, 73 VM_MEM_SRC_ANONYMOUS_HUGETLB, 74 VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB, 75 VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB, 76 VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB, 77 VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB, 78 VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB, 79 VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB, 80 VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB, 81 VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB, 82 VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB, 83 VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB, 84 VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB, 85 VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB, 86 VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB, 87 NUM_SRC_TYPES, 88 }; 89 90 struct vm_mem_backing_src_alias { 91 const char *name; 92 uint32_t flag; 93 }; 94 95 bool thp_configured(void); 96 size_t get_trans_hugepagesz(void); 97 size_t get_def_hugetlb_pagesz(void); 98 const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i); 99 size_t get_backing_src_pagesz(uint32_t i); 100 void backing_src_help(void); 101 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name); 102 103 #endif /* SELFTEST_KVM_TEST_UTIL_H */ 104