summaryrefslogtreecommitdiff
path: root/lib/memcpy.c
blob: a552134357131e2e2438deb44764fc61b6ec06be (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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "memcpy.h"
#include "rand.h"
#include "../fio_time.h"
#include "../gettime.h"
#include "../os/os.h"

#define BUF_SIZE	32 * 1024 * 1024ULL

#define NR_ITERS	64

struct memcpy_test {
	const char *name;
	void *src;
	void *dst;
	size_t size;
};

static struct memcpy_test tests[] = {
	{
		.name		= "8 bytes",
		.size		= 8,
	},
	{
		.name		= "16 bytes",
		.size		= 16,
	},
	{
		.name		= "96 bytes",
		.size		= 96,
	},
	{
		.name		= "128 bytes",
		.size		= 128,
	},
	{
		.name		= "256 bytes",
		.size		= 256,
	},
	{
		.name		= "512 bytes",
		.size		= 512,
	},
	{
		.name		= "2048 bytes",
		.size		= 2048,
	},
	{
		.name		= "8192 bytes",
		.size		= 8192,
	},
	{
		.name		= "131072 bytes",
		.size		= 131072,
	},
	{
		.name		= "262144 bytes",
		.size		= 262144,
	},
	{
		.name		= "524288 bytes",
		.size		= 524288,
	},
	{
		.name		= NULL,
	},
};

struct memcpy_type {
	const char *name;
	unsigned int mask;
	void (*fn)(struct memcpy_test *);
};

enum {
	T_MEMCPY	= 1U << 0,
	T_MEMMOVE	= 1U << 1,
	T_SIMPLE	= 1U << 2,
	T_HYBRID	= 1U << 3,
};

#define do_test(test, fn)	do {					\
	size_t left, this;						\
	void *src, *dst;						\
	int i;								\
									\
	for (i = 0; i < NR_ITERS; i++) {				\
		left = BUF_SIZE;					\
		src = test->src;					\
		dst = test->dst;					\
		while (left) {						\
			this = test->size;				\
			if (this > left)				\
				this = left;				\
			(fn)(dst, src, this);				\
			left -= this;					\
			src += this;					\
			dst += this;					\
		}							\
	}								\
} while (0)

static void t_memcpy(struct memcpy_test *test)
{
	do_test(test, memcpy);
}

static void t_memmove(struct memcpy_test *test)
{
	do_test(test, memmove);
}

static void simple_memcpy(void *dst, void const *src, size_t len)
{
 	char *d = dst;
	const char *s = src;

	while (len--)
		*d++ = *s++;
}

static void t_simple(struct memcpy_test *test)
{
	do_test(test, simple_memcpy);
}

static void t_hybrid(struct memcpy_test *test)
{
	if (test->size >= 64)
		do_test(test, simple_memcpy);
	else
		do_test(test, memcpy);
}

static struct memcpy_type t[] = {
	{
		.name = "memcpy",
		.mask = T_MEMCPY,
		.fn = t_memcpy,
	},
	{
		.name = "memmove",
		.mask = T_MEMMOVE,
		.fn = t_memmove,
	},
	{
		.name = "simple",
		.mask = T_SIMPLE,
		.fn = t_simple,
	},
	{
		.name = "hybrid",
		.mask = T_HYBRID,
		.fn = t_hybrid,
	},
	{
		.name = NULL,
	},
};

static unsigned int get_test_mask(const char *type)
{
	char *ostr, *str = strdup(type);
	unsigned int mask;
	char *name;
	int i;

	ostr = str;
	mask = 0;
	while ((name = strsep(&str, ",")) != NULL) {
		for (i = 0; t[i].name; i++) {
			if (!strcmp(t[i].name, name)) {
				mask |= t[i].mask;
				break;
			}
		}
	}

	free(ostr);
	return mask;
}

static int list_types(void)
{
	int i;

	for (i = 0; t[i].name; i++)
		printf("%s\n", t[i].name);

	return 1;
}

static int setup_tests(void)
{
	struct memcpy_test *test;
	struct frand_state state;
	void *src, *dst;
	int i;

	if (!tests[0].name)
		return 0;

	src = malloc(BUF_SIZE);
	dst = malloc(BUF_SIZE);
	if (!src || !dst) {
		free(src);
		free(dst);
		return 1;
	}

	init_rand_seed(&state, 0x8989, 0);
	fill_random_buf(&state, src, BUF_SIZE);

	for (i = 0; tests[i].name; i++) {
		test = &tests[i];
		test->src = src;
		test->dst = dst;
	}

	return 0;
}

static void free_tests(void)
{
	free(tests[0].src);
	free(tests[0].dst);
}

int fio_memcpy_test(const char *type)
{
	unsigned int test_mask = 0;
	int j, i;

	if (!type)
		test_mask = ~0U;
	else if (!strcmp(type, "help") || !strcmp(type, "list"))
		return list_types();
	else
		test_mask = get_test_mask(type);

	if (!test_mask) {
		fprintf(stderr, "fio: unknown hash `%s`. Available:\n", type);
		return list_types();
	}

	if (setup_tests()) {
		fprintf(stderr, "setting up mem regions failed\n");
		return 1;
	}

	for (i = 0; t[i].name; i++) {
		struct timespec ts;
		double mb_sec;
		uint64_t usec;

		if (!(t[i].mask & test_mask))
			continue;

		/*
		 * For first run, make sure CPUs are spun up and that
		 * we've touched the data.
		 */
		usec_spin(100000);
		t[i].fn(&tests[0]);

		printf("%s\n", t[i].name);

		for (j = 0; tests[j].name; j++) {
			fio_gettime(&ts, NULL);
			t[i].fn(&tests[j]);
			usec = utime_since_now(&ts);

			if (usec) {
				unsigned long long mb = NR_ITERS * BUF_SIZE;

				mb_sec = (double) mb / (double) usec;
				mb_sec /= (1.024 * 1.024);
				printf("\t%s:\t%8.2f MiB/sec\n", tests[j].name, mb_sec);
			} else
				printf("\t%s:inf MiB/sec\n", tests[j].name);
		}
	}

	free_tests();
	return 0;
}