File: buddy_allocator.c

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (270 lines) | stat: -rw-r--r-- 7,269 bytes parent folder | download | duplicates (9)
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
/*
 * Copyright (c) 2015-2016 Los Alamos National Security, LLC. All
 * rights reserved.
 * Copyright (c) 2015-2016 Cray Inc. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "gnix_buddy_allocator.h"
#include <criterion/criterion.h>
#include "gnix_rdma_headers.h"
#include <time.h>

#define LEN (1024 * 1024)	/* buddy_handle->len */
#define MAX_LEN (LEN / 2)	/* buddy_handle->max */
#define MIN_LEN MIN_BLOCK_SIZE

long *buf = NULL;		/* buddy_handle->base */
gnix_buddy_alloc_handle_t *buddy_handle;

struct ptrs_t {
	void *ptr;		/* ptrs alloc'd by buddy_alloc */
	uint32_t size;		/* size of the ptr */
} *ptrs;

void buddy_allocator_setup(void)
{
	int ret;

	ptrs = calloc(LEN / MIN_LEN, sizeof(struct ptrs_t));
	cr_assert(ptrs, "buddy_allocator_setup");

	buf = calloc(LEN, sizeof(long));
	cr_assert(buf, "buddy_allocator_setup");

	ret = _gnix_buddy_allocator_create(buf, LEN, MAX_LEN, &buddy_handle);
	cr_assert(!ret, "_gnix_buddy_allocator_create");
}

void buddy_allocator_teardown(void)
{
	int ret;

	ret = _gnix_buddy_allocator_destroy(buddy_handle);
	cr_assert(!ret, "_gnix_buddy_allocator_destroy");

	free(ptrs);
	free(buf);
}

/* Test invalid parameters for setup */
void buddy_allocator_setup_error(void)
{
	int ret;

	ret = _gnix_buddy_allocator_create(NULL, LEN, MAX_LEN, &buddy_handle);
	cr_assert_eq(ret, -FI_EINVAL);

	ret = _gnix_buddy_allocator_create(buf, 0, MAX_LEN, &buddy_handle);
	cr_assert_eq(ret, -FI_EINVAL);

	ret = _gnix_buddy_allocator_create(buf, LEN, LEN + 1, &buddy_handle);
	cr_assert_eq(ret, -FI_EINVAL);

	ret = _gnix_buddy_allocator_create(buf, LEN, 0, &buddy_handle);
	cr_assert_eq(ret, -FI_EINVAL);

	ret = _gnix_buddy_allocator_create(buf, LEN, MAX_LEN, NULL);
	cr_assert_eq(ret, -FI_EINVAL);
}

/* Test invalid parameters for teardown */
void buddy_allocator_teardown_error(void)
{
	int ret;

	ret = _gnix_buddy_allocator_destroy(NULL);
	cr_assert_eq(ret, -FI_EINVAL);
}

/* Sequential alloc */
void do_alloc(uint32_t len)
{
	uint32_t i = 0, ret;

	/* Allocate all the memory and write to each block */
	for (; i < LEN / len; i++) {
		ptrs[i].size = len;
		ret = _gnix_buddy_alloc(buddy_handle, &ptrs[i].ptr, len);
		cr_assert(!ret, "_gnix_buddy_alloc");
		memset(ptrs[i].ptr, 0, len);
	}

	/* Ensure that all free lists are empty */
	for (i = 0; i < buddy_handle->nlists; i++) {
		ret = dlist_empty(buddy_handle->lists + i);
		cr_assert_eq(ret, 1);
	}
}

/* Sequential free */
void do_free(uint32_t len)
{
	int i = 0, ret;

	/* Free all allocated blocks */
	for (i = 0; i < LEN / len; i++) {
		ret = _gnix_buddy_free(buddy_handle, ptrs[i].ptr, ptrs[i].size);
		cr_assert(!ret, "_gnix_buddy_free");
	}

	/* Ensure that every free list except the last is empty */
	for (i = 0; i < buddy_handle->nlists - 1; i++) {
		ret = dlist_empty(buddy_handle->lists + i);
		cr_assert_eq(ret, 1);
	}
	ret = dlist_empty(buddy_handle->lists + i);
	cr_assert_eq(ret, 0);
}

TestSuite(buddy_allocator, .init = buddy_allocator_setup,
	  .fini = buddy_allocator_teardown, .disabled = false);

/* Sequential alloc and frees */
Test(buddy_allocator, sequential_alloc_free)
{
	uint32_t i = MIN_LEN;

	for (i = MIN_LEN; i <= MAX_LEN; i *= 2) {
		do_alloc(i);
		do_free(i);
	}
}

/* Pseudo random allocs and frees */
Test(buddy_allocator, random_alloc_free)
{
	int i = 0, j = 0, ret;

	srand((unsigned) time(NULL));

	for (j = MIN_LEN; j <= MAX_LEN; j *= 2) {
		do {
			ret = rand() % 100;

			if (ret <= 49) {
				/* ~50% chance to alloc min size blocks*/
				ptrs[i].size = MIN_BLOCK_SIZE;
			} else if (ret >= 50 &&
				   ret <= 87) {
				/* ~37% chance to alloc blocks of size
				 * [MIN_BLOCK_SIZE * 2, MAX_BLOCK_SIZE / 2]
				 */
				ptrs[i].size = OFFSET(MIN_BLOCK_SIZE,
						      (rand() %
						       (buddy_handle->nlists -
							1)) + 1);
			} else {
				/* ~13% chance to alloc max size blocks */
				ptrs[i].size = buddy_handle->max;
			}

			ret = _gnix_buddy_alloc(buddy_handle, &ptrs[i].ptr,
					  ptrs[i].size);
			cr_assert_neq(ret, -FI_EINVAL);

			i++;
		} while (ret != -FI_ENOMEM);

		/* Free all allocated blocks */
		for (i -= 2; i >= 0; i--) {
			ret = _gnix_buddy_free(buddy_handle, ptrs[i].ptr,
					       ptrs[i].size);
			cr_assert(!ret, "_gnix_buddy_free");
		}

		/* Ensure that every free list except the last is empty */
		for (i = 0; i < buddy_handle->nlists - 1; i++) {
			ret = dlist_empty(buddy_handle->lists + i);
			cr_assert_eq(ret, 1);
		}
		ret = dlist_empty(buddy_handle->lists + i);
		cr_assert_eq(ret, 0);

		i = 0;
	}
}

Test(buddy_allocator, alloc_free_error)
{
	int ret;
	void *tmp;

	do_alloc(MIN_LEN);

	/* Request one additional block */
	ret = _gnix_buddy_alloc(buddy_handle, &tmp, MIN_LEN);
	cr_assert_eq(ret, -FI_ENOMEM);

	do_free(MIN_LEN);
}

/* Test invalid buddy alloc and free parameters */
Test(buddy_allocator, parameter_error)
{
	int ret;

	buddy_allocator_setup_error();
	buddy_allocator_teardown_error();

	/* BEGIN: Alloc, invalid parameters */
	ret = _gnix_buddy_alloc(NULL, ptrs->ptr, MAX_LEN);
	cr_assert_eq(ret, -FI_EINVAL);

	ret = _gnix_buddy_alloc(buddy_handle, ptrs->ptr, MAX_LEN + 1);
	cr_assert_eq(ret, -FI_EINVAL);

	ret = _gnix_buddy_alloc(buddy_handle, ptrs->ptr, 0);
	cr_assert_eq(ret, -FI_EINVAL);

	ret = _gnix_buddy_alloc(buddy_handle, NULL, MAX_LEN);
	cr_assert_eq(ret, -FI_EINVAL);
	/* END: Alloc, invalid parameters */

	/* BEGIN: Free, invalid parameters */
	ret = _gnix_buddy_free(NULL, ptrs->ptr, MAX_LEN);
	cr_assert_eq(ret, -FI_EINVAL);

	ret = _gnix_buddy_free(buddy_handle, NULL, MAX_LEN);
	cr_assert_eq(ret, -FI_EINVAL);

	ret = _gnix_buddy_free(buddy_handle, buf - 1, MAX_LEN);
	cr_assert_eq(ret, -FI_EINVAL);

	ret = _gnix_buddy_free(buddy_handle, buf + LEN, MAX_LEN);
	cr_assert_eq(ret, -FI_EINVAL);

	ret = _gnix_buddy_free(buddy_handle, buf, MAX_LEN + 1);
	cr_assert_eq(ret, -FI_EINVAL);

	ret = _gnix_buddy_free(buddy_handle, buf - 1, 0);
	cr_assert_eq(ret, -FI_EINVAL);
	/* END: Free, invalid parameters */
}