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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* tools/testing/selftests/kvm/lib/test_util.c
*
* Copyright (C) 2020, Google LLC.
*/
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include "linux/kernel.h"
#include "test_util.h"
/*
* Parses "[0-9]+[kmgt]?".
*/
size_t parse_size(const char *size)
{
size_t base;
char *scale;
int shift = 0;
TEST_ASSERT(size && isdigit(size[0]), "Need at least one digit in '%s'", size);
base = strtoull(size, &scale, 0);
TEST_ASSERT(base != ULLONG_MAX, "Overflow parsing size!");
switch (tolower(*scale)) {
case 't':
shift = 40;
break;
case 'g':
shift = 30;
break;
case 'm':
shift = 20;
break;
case 'k':
shift = 10;
break;
case 'b':
case '\0':
shift = 0;
break;
default:
TEST_ASSERT(false, "Unknown size letter %c", *scale);
}
TEST_ASSERT((base << shift) >> shift == base, "Overflow scaling size!");
return base << shift;
}
int64_t timespec_to_ns(struct timespec ts)
{
return (int64_t)ts.tv_nsec + 1000000000LL * (int64_t)ts.tv_sec;
}
struct timespec timespec_add_ns(struct timespec ts, int64_t ns)
{
struct timespec res;
res.tv_nsec = ts.tv_nsec + ns;
res.tv_sec = ts.tv_sec + res.tv_nsec / 1000000000LL;
res.tv_nsec %= 1000000000LL;
return res;
}
struct timespec timespec_add(struct timespec ts1, struct timespec ts2)
{
int64_t ns1 = timespec_to_ns(ts1);
int64_t ns2 = timespec_to_ns(ts2);
return timespec_add_ns((struct timespec){0}, ns1 + ns2);
}
struct timespec timespec_sub(struct timespec ts1, struct timespec ts2)
{
int64_t ns1 = timespec_to_ns(ts1);
int64_t ns2 = timespec_to_ns(ts2);
return timespec_add_ns((struct timespec){0}, ns1 - ns2);
}
struct timespec timespec_elapsed(struct timespec start)
{
struct timespec end;
clock_gettime(CLOCK_MONOTONIC, &end);
return timespec_sub(end, start);
}
struct timespec timespec_div(struct timespec ts, int divisor)
{
int64_t ns = timespec_to_ns(ts) / divisor;
return timespec_add_ns((struct timespec){0}, ns);
}
void print_skip(const char *fmt, ...)
{
va_list ap;
assert(fmt);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
puts(", skipping test");
}
const struct vm_mem_backing_src_alias backing_src_aliases[] = {
{"anonymous", VM_MEM_SRC_ANONYMOUS,},
{"anonymous_thp", VM_MEM_SRC_ANONYMOUS_THP,},
{"anonymous_hugetlb", VM_MEM_SRC_ANONYMOUS_HUGETLB,},
};
bool thp_configured(void)
{
int ret;
struct stat statbuf;
ret = stat("/sys/kernel/mm/transparent_hugepage", &statbuf);
TEST_ASSERT(ret == 0 || (ret == -1 && errno == ENOENT),
"Error in stating /sys/kernel/mm/transparent_hugepage");
return ret == 0;
}
size_t get_trans_hugepagesz(void)
{
size_t size;
FILE *f;
TEST_ASSERT(thp_configured(), "THP is not configured in host kernel");
f = fopen("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", "r");
TEST_ASSERT(f != NULL, "Error in opening transparent_hugepage/hpage_pmd_size");
fscanf(f, "%ld", &size);
fclose(f);
return size;
}
size_t get_def_hugetlb_pagesz(void)
{
char buf[64];
const char *tag = "Hugepagesize:";
FILE *f;
f = fopen("/proc/meminfo", "r");
TEST_ASSERT(f != NULL, "Error in opening /proc/meminfo");
while (fgets(buf, sizeof(buf), f) != NULL) {
if (strstr(buf, tag) == buf) {
fclose(f);
return strtoull(buf + strlen(tag), NULL, 10) << 10;
}
}
if (feof(f))
TEST_FAIL("HUGETLB is not configured in host kernel");
else
TEST_FAIL("Error in reading /proc/meminfo");
fclose(f);
return 0;
}
void backing_src_help(void)
{
int i;
printf("Available backing src types:\n");
for (i = 0; i < ARRAY_SIZE(backing_src_aliases); i++)
printf("\t%s\n", backing_src_aliases[i].name);
}
enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name)
{
int i;
for (i = 0; i < ARRAY_SIZE(backing_src_aliases); i++)
if (!strcmp(type_name, backing_src_aliases[i].name))
return backing_src_aliases[i].type;
backing_src_help();
TEST_FAIL("Unknown backing src type: %s", type_name);
return -1;
}
|