aml.h 16.3 KB
Newer Older
1 2 3
#ifndef AML_H
#define AML_H 1

4
#include <inttypes.h>
5
#include <memkind.h>
6 7 8 9 10 11
#include <numa.h>
#include <numaif.h>
#include <pthread.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
12
#include <stdlib.h>
13 14 15 16 17 18 19
#include <sys/mman.h>
#include <unistd.h>

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif

20

21 22 23 24 25
/*******************************************************************************
 * Forward Declarations:
 ******************************************************************************/

struct aml_area;
26
struct aml_binding;
27 28 29

/*******************************************************************************
 * Arenas:
30 31
 * In-memory allocator implementation. Dispatches actual memory mappings back to
 * areas.
32 33
 ******************************************************************************/

34 35 36 37 38 39
#define AML_ARENA_FLAG_ZERO 1

/* opaque handle to configuration data */
struct aml_arena_data;

struct aml_arena_ops {
40 41
	int (*register_arena)(struct aml_arena_data *, struct aml_area *);
	int (*deregister_arena)(struct aml_arena_data *);
42 43 44 45 46
	void *(*mallocx)(struct aml_arena_data *, size_t, int);
	void (*dallocx)(struct aml_arena_data *, void *, int);
	void *(*reallocx)(struct aml_arena_data *, void *, size_t, int);
};

47
struct aml_arena {
48 49
	struct aml_arena_ops *ops;
	struct aml_arena_data *data;
50 51
};

52 53
int aml_arena_register(struct aml_arena *, struct aml_area *);
int aml_arena_deregister(struct aml_arena *);
54 55 56 57 58 59 60 61 62 63 64 65 66
void *aml_arena_mallocx(struct aml_arena *, size_t, int);
void aml_arena_dallocx(struct aml_arena *, void *, int);
void *aml_arena_reallocx(struct aml_arena *, void *, size_t, int);

/*******************************************************************************
 * Jemalloc Arena:
 ******************************************************************************/
extern struct aml_arena_ops aml_arena_jemalloc_ops;

struct aml_arena_jemalloc_data {
	unsigned int uid;
	int flags;
};
67

68 69 70 71 72 73 74 75 76 77 78
#define AML_ARENA_JEMALLOC_DECL(name) \
	struct aml_arena_jemalloc_data __ ##name## _inner_data; \
	struct aml_arena name = { \
		&aml_arena_jemalloc_ops, \
		(struct aml_arena_data *)&__ ## name ## _inner_data, \
	};

#define AML_ARENA_JEMALLOC_ALLOCSIZE \
	(sizeof(struct aml_arena_jemalloc_data) + \
	 sizeof(struct aml_arena))

79 80 81 82 83 84 85 86 87

#define AML_ARENA_JEMALLOC_TYPE_REGULAR 0
#define AML_ARENA_JEMALLOC_TYPE_ALIGNED 1
#define AML_ARENA_JEMALLOC_TYPE_GENERIC 2

int aml_arena_jemalloc_create(struct aml_arena **, int type, ...);
int aml_arena_jemalloc_init(struct aml_arena *, int type, ...);
int aml_arena_jemalloc_vinit(struct aml_arena *, int type, va_list);
int aml_arena_jemalloc_destroy(struct aml_arena *);
88

89 90 91 92 93
/*******************************************************************************
 * Areas:
 * embeds information about a byte-addressable physical memory location and well
 * as binding policies over it.
 ******************************************************************************/
94

95 96 97 98 99 100 101 102 103 104 105 106
/* opaque handle to configuration data */
struct aml_area_data;

struct aml_area_ops {
	void *(*malloc)(struct aml_area_data *, size_t);
	void (*free)(struct aml_area_data *, void *);
	void *(*calloc)(struct aml_area_data *, size_t, size_t);
	void *(*realloc)(struct aml_area_data *, void *, size_t);
	void *(*acquire)(struct aml_area_data *, size_t);
	void (*release)(struct aml_area_data *, void *);
	void *(*mmap)(struct aml_area_data *, void *ptr, size_t);
	int (*available)(struct aml_area_data *);
107
	int (*binding)(struct aml_area_data *, struct aml_binding **);
108 109
};

110
struct aml_area {
111 112
	struct aml_area_ops *ops;
	struct aml_area_data *data;
113 114
};

115 116 117 118 119 120 121 122
/*******************************************************************************
 * POSIX Area:
 ******************************************************************************/

extern struct aml_area_ops aml_area_posix_ops;

struct aml_area_posix_data {
};
123

124 125 126 127 128 129 130 131 132 133 134 135
#define AML_AREA_POSIX_DECL(name) \
	struct aml_area_posix_data __ ##name## _inner_data; \
	struct aml_area name = { \
		&aml_area_posix_ops, \
		(struct aml_area_data *)&__ ## name ## _inner_data, \
	};

#define AML_AREA_POSIX_ALLOCSIZE \
	(sizeof(struct aml_area_posix_data) + \
	 sizeof(struct aml_area))

int aml_area_posix_create(struct aml_area **);
136
int aml_area_posix_vinit(struct aml_area *);
137 138
int aml_area_posix_init(struct aml_area *);
int aml_area_posix_destroy(struct aml_area *);
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
 * Linux Area:
 ******************************************************************************/

extern struct aml_area_ops aml_area_linux_ops;

struct aml_area_linux_manager_data {
	struct aml_arena *pool;
	size_t pool_size;
};

struct aml_area_linux_manager_ops {
	struct aml_arena *(*get_arena)(struct aml_area_linux_manager_data *data);
};

extern struct aml_area_linux_manager_ops aml_area_linux_manager_single_ops;

int aml_area_linux_manager_single_init(struct aml_area_linux_manager_data *,
				       struct aml_arena *);
int aml_area_linux_manager_single_destroy(struct aml_area_linux_manager_data *);

#define AML_MAX_NUMA_NODES 128
#define AML_NODEMASK_BYTES (AML_MAX_NUMA_NODES/8)
#define AML_NODEMASK_SZ (AML_NODEMASK_BYTES/sizeof(unsigned long))

165 166 167 168 169
#define AML_NODEMASK_NBITS (8*sizeof(unsigned long))
#define AML_NODEMASK_ELT(i) ((i) / AML_NODEMASK_NBITS)
#define AML_NODEMASK_BITMASK(i) ((unsigned long)1 << ((i) % AML_NODEMASK_NBITS))
#define AML_NODEMASK_ISSET(mask, i) \
	((mask[AML_NODEMASK_ELT(i)] & AML_NODEMASK_BITMASK(i)) != 0)
170 171 172 173 174 175
#define AML_NODEMASK_SET(mask, i) (mask[AML_NODEMASK_ELT(i)] |= AML_NODEMASK_BITMASK(i))
#define AML_NODEMASK_ZERO(mask) \
	do {								\
		for(unsigned int __i = 0; __i < AML_NODEMASK_SZ; __i++)	\
			mask[__i] = 0;					\
	} while(0)
176 177


178 179 180 181 182 183 184 185
struct aml_area_linux_mbind_data {
	unsigned long nodemask[AML_NODEMASK_SZ];
	int policy;
};

struct aml_area_linux_mbind_ops {
	int (*pre_bind)(struct aml_area_linux_mbind_data *);
	int (*post_bind)(struct aml_area_linux_mbind_data *, void *, size_t);
186
	int (*binding)(struct aml_area_linux_mbind_data *, struct aml_binding *);
187 188 189 190
};

int aml_area_linux_mbind_setdata(struct aml_area_linux_mbind_data *, int,
				 unsigned long *);
191
int aml_area_linux_mbind_generic_binding(struct aml_area_linux_mbind_data *,
192
					 struct aml_binding **);
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
int aml_area_linux_mbind_regular_pre_bind(struct aml_area_linux_mbind_data *);
int aml_area_linux_mbind_regular_post_bind(struct aml_area_linux_mbind_data *,
					   void *, size_t);
int aml_area_linux_mbind_mempolicy_pre_bind(struct aml_area_linux_mbind_data *);
int aml_area_linux_mbind_mempolicy_post_bind(struct aml_area_linux_mbind_data *,
					   void *, size_t);
int aml_area_linux_mbind_init(struct aml_area_linux_mbind_data *, int,
			      unsigned long *);
int aml_area_linux_mbind_destroy(struct aml_area_linux_mbind_data *);

extern struct aml_area_linux_mbind_ops aml_area_linux_mbind_regular_ops;
extern struct aml_area_linux_mbind_ops aml_area_linux_mbind_mempolicy_ops;

struct aml_area_linux_mmap_data {
	int prot;
	int flags;
	int fildes;
	off_t off;
};

struct aml_area_linux_mmap_ops {
	void *(*mmap)(struct aml_area_linux_mmap_data *, void *, size_t);
};

void *aml_area_linux_mmap_generic(struct aml_area_linux_mmap_data *, void *,
				  size_t);
int aml_area_linux_mmap_anonymous_init(struct aml_area_linux_mmap_data *);
int aml_area_linux_mmap_fd_init(struct aml_area_linux_mmap_data *, int, size_t);
int aml_area_linux_mmap_tmpfile_init(struct aml_area_linux_mmap_data *, char *,
				     size_t);
int aml_area_linux_mmap_anonymous_destroy(struct aml_area_linux_mmap_data *);
int aml_area_linux_mmap_fd_destroy(struct aml_area_linux_mmap_data *);
int aml_area_linux_mmap_tmpfile_destroy(struct aml_area_linux_mmap_data *);

extern struct aml_area_linux_mmap_ops aml_area_linux_mmap_generic_ops;

struct aml_area_linux_data {
	struct aml_area_linux_manager_data manager;
	struct aml_area_linux_mbind_data mbind;
	struct aml_area_linux_mmap_data mmap;
};

struct aml_area_linux_ops {
	struct aml_area_linux_manager_ops manager;
	struct aml_area_linux_mbind_ops mbind;
	struct aml_area_linux_mmap_ops mmap;
};

struct aml_area_linux {
	struct aml_area_linux_data data;
	struct aml_area_linux_ops ops;
};

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
#define AML_AREA_LINUX_DECL(name) \
	struct aml_area_linux __ ##name## _inner_data; \
	struct aml_area name = { \
		&aml_area_linux_ops, \
		(struct aml_area_data *)&__ ## name ## _inner_data, \
	};

#define AML_AREA_LINUX_ALLOCSIZE \
	(sizeof(struct aml_area_linux) + \
	 sizeof(struct aml_area))

#define AML_AREA_LINUX_MANAGER_TYPE_SINGLE 0

#define AML_AREA_LINUX_MBIND_TYPE_REGULAR 0
#define AML_AREA_LINUX_MBIND_TYPE_MEMPOLICY 1

#define AML_AREA_LINUX_MMAP_TYPE_ANONYMOUS 0
#define AML_AREA_LINUX_MMAP_TYPE_FD 1
#define AML_AREA_LINUX_MMAP_TYPE_TMPFILE 2

int aml_area_linux_create(struct aml_area **, int, int, int, ...);
int aml_area_linux_init(struct aml_area *, int, int, int, ...);
int aml_area_linux_vinit(struct aml_area *, int, int, int, va_list);
int aml_area_linux_destroy(struct aml_area *);
270 271 272 273 274

/*******************************************************************************
 * Generic Area API:
 * Low-level, direct access to area logic.
 * For memory allocation function, follows the POSIX spec.
275
 ******************************************************************************/
276

277 278 279 280 281 282
void *aml_area_malloc(struct aml_area *, size_t);
void aml_area_free(struct aml_area *, void *);
void *aml_area_calloc(struct aml_area *, size_t, size_t);
void *aml_area_realloc(struct aml_area *, void *, size_t);
void *aml_area_acquire(struct aml_area *, size_t);
void aml_area_release(struct aml_area *, void *);
283 284
void *aml_area_mmap(struct aml_area *, void *, size_t);
int aml_area_available(struct aml_area *);
285
int aml_area_binding(struct aml_area *, struct aml_binding **);
286

287 288 289 290 291 292
/*******************************************************************************
 * DMA Engines:
 * Low-level, direct movement of memory.
 * We haven't decided in our design how we want to deal with memcpy/move_pages
 * differences yet.
 ******************************************************************************/
293

294 295 296 297 298 299 300 301 302 303 304 305
struct aml_dma {
	int (*copy)(struct aml_dma *, void *, const void *, size_t);
	int (*move)(struct aml_dma *, struct aml_area *, struct aml_area *,
		    void *, size_t);
};

int aml_dma_init(struct aml_dma *, unsigned int);
int aml_dma_destroy(struct aml_dma *);
int aml_dma_copy(struct aml_dma *, void *, const void *, size_t);
int aml_dma_move(struct aml_dma *, struct aml_area *, struct aml_area *,
		 void *, size_t);

306

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
/*******************************************************************************
 * Tiling:
 * Representation of a data structure organization in memory.
 ******************************************************************************/

/* opaque handle to all tilings */
struct aml_tiling_data;
struct aml_tiling_iterator_data;

/*forward declarations */
struct aml_tiling_iterator_ops;
struct aml_tiling_iterator;


struct aml_tiling_ops {
	int (*create_iterator)(struct aml_tiling_data *,
			       struct aml_tiling_iterator **, int);
	int (*init_iterator)(struct aml_tiling_data *,
			     struct aml_tiling_iterator *, int);
	int (*destroy_iterator)(struct aml_tiling_data *,
				struct aml_tiling_iterator *);
328 329
	size_t (*tilesize)(struct aml_tiling_data *, int);
	void* (*tilestart)(struct aml_tiling_data *, void *, int);
330 331 332 333 334 335 336
};

struct aml_tiling {
	struct aml_tiling_ops *ops;
	struct aml_tiling_data *data;
};

337 338
size_t aml_tiling_tilesize(struct aml_tiling *, int);
void* aml_tiling_tilestart(struct aml_tiling *, void *, int);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419


int aml_tiling_create_iterator(struct aml_tiling *,
			       struct aml_tiling_iterator **, int);
int aml_tiling_init_iterator(struct aml_tiling *,
			     struct aml_tiling_iterator *, int);
int aml_tiling_destroy_iterator(struct aml_tiling *,
				struct aml_tiling_iterator *);

struct aml_tiling_iterator_ops {
	int (*reset)(struct aml_tiling_iterator_data *);
	int (*next)(struct aml_tiling_iterator_data *);
	int (*end)(struct aml_tiling_iterator_data *);
	int (*get)(struct aml_tiling_iterator_data *, va_list);
};

struct aml_tiling_iterator {
	struct aml_tiling_iterator_ops *ops;
	struct aml_tiling_iterator_data *data;
};

int aml_tiling_iterator_reset(struct aml_tiling_iterator *);
int aml_tiling_iterator_next(struct aml_tiling_iterator *);
int aml_tiling_iterator_end(struct aml_tiling_iterator *);
int aml_tiling_iterator_get(struct aml_tiling_iterator *, ...);

#define AML_TILING_TYPE_1D 0

int aml_tiling_create(struct aml_tiling **, int type, ...);
int aml_tiling_init(struct aml_tiling *, int type, ...);
int aml_tiling_vinit(struct aml_tiling *, int type, va_list);
int aml_tiling_destroy(struct aml_tiling *, int type);

/*******************************************************************************
 * Tiling 1D:
 ******************************************************************************/

extern struct aml_tiling_ops aml_tiling_1d_ops;
extern struct aml_tiling_iterator_ops aml_tiling_iterator_1d_ops;

struct aml_tiling_1d_data {
	size_t blocksize;
	size_t totalsize;
};

struct aml_tiling_iterator_1d_data {
	size_t i;
	struct aml_tiling_1d_data *tiling;
};

#define AML_TILING_1D_DECL(name) \
	struct aml_tiling_1d_data __ ##name## _inner_data; \
	struct aml_tiling name = { \
		&aml_tiling_1d_ops, \
		(struct aml_tiling_data *)&__ ## name ## _inner_data, \
	};

#define AML_TILING_ITERATOR_1D_DECL(name) \
	struct aml_tiling_iterator_1d_data __ ##name## _inner_data; \
	struct aml_tiling_iterator name = { \
		&aml_tiling_iterator_1d_ops, \
		(struct aml_tiling_iterator_data *)&__ ## name ## _inner_data, \
	};

#define AML_TILING_1D_ALLOCSIZE (sizeof(struct aml_tiling_1d_data) + \
				 sizeof(struct aml_tiling))

#define AML_TILING_ITERATOR_1D_ALLOCSIZE \
	(sizeof(struct aml_tiling_iterator_1d_data) + \
	 sizeof(struct aml_tiling_iterator))

/*******************************************************************************
 * Binding:
 * Representation of page bindings in an area
 ******************************************************************************/

/* opaque handle to all bindings */
struct aml_binding_data;

struct aml_binding_ops {
	int (*nbpages)(struct aml_binding_data *, struct aml_tiling *,
420
		       void *, int);
421
	int (*pages)(struct aml_binding_data *, void **, struct aml_tiling *,
422
		     void *, int);
423
	int (*nodes)(struct aml_binding_data *, int *, struct aml_tiling *,
424
		     void *, int);
425 426 427 428 429 430 431
};

struct aml_binding {
	struct aml_binding_ops *ops;
	struct aml_binding_data *data;
};

432 433 434
int aml_binding_nbpages(struct aml_binding *, struct aml_tiling *, void*, int);
int aml_binding_pages(struct aml_binding *, void **, struct aml_tiling *, void*, int);
int aml_binding_nodes(struct aml_binding *, int *, struct aml_tiling *, void *, int);
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

#define AML_BINDING_TYPE_SINGLE 0
#define AML_BINDING_TYPE_INTERLEAVE 1

int aml_binding_create(struct aml_binding **, int type, ...);
int aml_binding_init(struct aml_binding *, int type, ...);
int aml_binding_vinit(struct aml_binding *, int type, va_list);
int aml_binding_destroy(struct aml_binding *, int type);

/*******************************************************************************
 * Single Binding:
 * All pages on the same node
 ******************************************************************************/

extern struct aml_binding_ops aml_binding_single_ops;

struct aml_binding_single_data {
	int node;
};

#define AML_BINDING_SINGLE_DECL(name) \
	struct aml_binding_single_data __ ##name## _inner_data; \
	struct aml_binding name = { \
		&aml_binding_single_ops, \
		(struct aml_binding_data *)&__ ## name ## _inner_data, \
	};

#define AML_BINDING_SINGLE_ALLOCSIZE (sizeof(struct aml_binding_single_data) + \
				      sizeof(struct aml_binding))

/*******************************************************************************
 * Interleave Binding:
 * each page, of each tile, interleaved across nodes.
 ******************************************************************************/

extern struct aml_binding_ops aml_binding_interleave_ops;

struct aml_binding_interleave_data {
	int nodes[AML_MAX_NUMA_NODES];
	int count;
};

#define AML_BINDING_INTERLEAVE_DECL(name) \
	struct aml_binding_interleave_data __ ##name## _inner_data; \
	struct aml_binding name = { \
		&aml_binding_interleave_ops, \
		(struct aml_binding_data *)&__ ## name ## _inner_data, \
	};

#define AML_BINDING_INTERLEAVE_ALLOCSIZE \
	(sizeof(struct aml_binding_interleave_data) + \
	 sizeof(struct aml_binding))

488 489 490 491
/*******************************************************************************
 * General functions:
 * Initialize internal structures, cleanup everything at the end.
 ******************************************************************************/
492

493 494
int aml_init(int *argc, char **argv[]);
int aml_finalize(void);
495

496
#endif