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

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

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif

19

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

struct aml_area;
25
struct aml_binding;
26 27 28

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

33 34 35 36 37 38
#define AML_ARENA_FLAG_ZERO 1

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

struct aml_arena_ops {
39 40
	int (*register_arena)(struct aml_arena_data *, struct aml_area *);
	int (*deregister_arena)(struct aml_arena_data *);
41 42 43 44 45
	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);
};

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

51 52
int aml_arena_register(struct aml_arena *, struct aml_area *);
int aml_arena_deregister(struct aml_arena *);
53 54 55 56 57 58 59 60 61 62 63 64 65
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;
};
66

67 68 69 70 71 72 73 74 75 76 77
#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))

78 79 80 81 82 83 84 85 86

#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 *);
87

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

94 95 96 97 98 99 100 101 102 103 104 105
/* 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 *);
106
	int (*binding)(struct aml_area_data *, struct aml_binding **);
107 108
};

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

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

extern struct aml_area_ops aml_area_posix_ops;

struct aml_area_posix_data {
};
122

123 124 125 126 127 128 129 130 131 132 133 134
#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 **);
135
int aml_area_posix_vinit(struct aml_area *);
136 137
int aml_area_posix_init(struct aml_area *);
int aml_area_posix_destroy(struct aml_area *);
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
 * 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))

164 165 166 167 168
#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)
169 170 171 172 173 174
#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)
175 176


177 178 179 180 181 182 183 184
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);
185
	int (*binding)(struct aml_area_linux_mbind_data *, struct aml_binding *);
186 187 188 189
};

int aml_area_linux_mbind_setdata(struct aml_area_linux_mbind_data *, int,
				 unsigned long *);
190
int aml_area_linux_mbind_generic_binding(struct aml_area_linux_mbind_data *,
191
					 struct aml_binding **);
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
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;
};

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
#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 *);
269 270 271 272 273

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

276 277 278 279 280 281
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 *);
282 283
void *aml_area_mmap(struct aml_area *, void *, size_t);
int aml_area_available(struct aml_area *);
284
int aml_area_binding(struct aml_area *, struct aml_binding **);
285

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
/*******************************************************************************
 * 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 *);
307 308
	size_t (*tilesize)(struct aml_tiling_data *, int);
	void* (*tilestart)(struct aml_tiling_data *, void *, int);
309 310 311 312 313 314 315
};

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

316 317
size_t aml_tiling_tilesize(struct aml_tiling *, int);
void* aml_tiling_tilestart(struct aml_tiling *, void *, int);
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 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


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 *,
399
		       void *, int);
400
	int (*pages)(struct aml_binding_data *, void **, struct aml_tiling *,
401
		     void *, int);
402
	int (*nodes)(struct aml_binding_data *, int *, struct aml_tiling *,
403
		     void *, int);
404 405 406 407 408 409 410
};

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

411 412 413
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);
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 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

#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))

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
/*******************************************************************************
 * DMA:
 * Management of low-level movement of memory.
 ******************************************************************************/

#define AML_DMA_REQUEST_TYPE_INVALID -1
#define AML_DMA_REQUEST_TYPE_COPY 0
#define AML_DMA_REQUEST_TYPE_MOVE 1

struct aml_dma_request_data;
struct aml_dma_data;

struct aml_dma_request_ops {
	int (*copy)(struct aml_dma_data *, struct aml_dma_request_data *);
	int (*move)(struct aml_dma_data *, struct aml_dma_request_data *);
};

struct aml_dma_request {
	struct aml_dma_request_ops *ops;
	struct aml_dma_request_data *data;
};


struct aml_dma_ops {
	int (*create_request)(struct aml_dma_data *,
			      struct aml_dma_request *, int, va_list);
	int (*destroy_request)(struct aml_dma_data *, struct aml_dma_request *);
	int (*wait_request)(struct aml_dma_data *, struct aml_dma_request *);
};

struct aml_dma {
	struct aml_dma_ops *ops;
	struct aml_dma_data *data;
};

int aml_dma_copy(struct aml_dma *, ...);
int aml_dma_async_copy(struct aml_dma *, struct aml_dma_request *, ...);
int aml_dma_move(struct aml_dma *, ...);
int aml_dma_async_move(struct aml_dma *, struct aml_dma_request *, ...);
int aml_dma_wait(struct aml_dma *, struct aml_dma_request *);
int aml_dma_cancel(struct aml_dma *, struct aml_dma_request *);

/*******************************************************************************
 * Linux Sequential DMA API:
 * DMA logic implemented based on general linux API, with the caller thread
 * used as the only execution thread.
 ******************************************************************************/

struct aml_dma_request_linux_seq_data {
	int type;
	void *dest;
	void *src;
	size_t size;
	int count;
	void **pages;
	int *nodes;
};

extern struct aml_dma_ops aml_dma_linux_seq_ops;

struct aml_dma_linux_seq_data {
	size_t size;
	struct aml_dma_request_linux_seq_data *requests;
};

struct aml_dma_linux_seq_ops {
	int (*add_request)(struct aml_dma_linux_seq_data *,
			   struct aml_dma_request_linux_seq_data **);
	int (*remove_request)(struct aml_dma_linux_seq_data *,
			      struct aml_dma_request_linux_seq_data **);
};

struct aml_dma_linux_seq {
	struct aml_dma_linux_seq_ops ops;
	struct aml_dma_linux_seq_data data;
};

#define AML_DMA_LINUX_SEQ_DECL(name) \
	struct aml_dma_linux_seq __ ##name## _inner_data; \
	struct aml_dma name = { \
		&aml_dma_linux_seq_ops, \
		(struct aml_dma_data *)&__ ## name ## _inner_data, \
	};

#define AML_DMA_LINUX_SEQ_ALLOCSIZE \
	(sizeof(struct aml_dma_linux_seq) + \
	 sizeof(struct aml_dma))

int aml_dma_linux_seq_create(struct aml_dma **, ...);
int aml_dma_linux_seq_init(struct aml_dma *, ...);
int aml_dma_linux_seq_vinit(struct aml_dma *, va_list);
int aml_dma_linux_seq_destroy(struct aml_dma *);

560 561 562 563
/*******************************************************************************
 * General functions:
 * Initialize internal structures, cleanup everything at the end.
 ******************************************************************************/
564

565 566
int aml_init(int *argc, char **argv[]);
int aml_finalize(void);
567

568
#endif