aml.h 15.2 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 26 27 28
/*******************************************************************************
 * Forward Declarations:
 ******************************************************************************/

struct aml_area;

/*******************************************************************************
 * 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 106 107
/* 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 *);
};

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

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

extern struct aml_area_ops aml_area_posix_ops;

struct aml_area_posix_data {
};
121

122 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 **);
int aml_area_posix_init(struct aml_area *);
int aml_area_posix_destroy(struct aml_area *);
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
 * 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))

162 163 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 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
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);
};

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

int aml_area_linux_init(struct aml_area_linux *);
int aml_area_linux_destroy(struct aml_area_linux *);

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

243 244 245 246 247 248
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 *);
249 250
void *aml_area_mmap(struct aml_area *, void *, size_t);
int aml_area_available(struct aml_area *);
251

252 253 254 255 256 257
/*******************************************************************************
 * 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.
 ******************************************************************************/
258

259 260 261 262 263 264 265 266 267 268 269 270
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);

271

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 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
/*******************************************************************************
 * 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 *);
	size_t (*tilesize)(struct aml_tiling_data *, va_list);
	void* (*tilestart)(struct aml_tiling_data *, void *, va_list);
};

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

size_t aml_tiling_tilesize(struct aml_tiling *, ...);
size_t aml_tiling_vtilesize(struct aml_tiling *, va_list);
void* aml_tiling_tilestart(struct aml_tiling *, void *, ...);
void* aml_tiling_vtilestart(struct aml_tiling *, void *, va_list);


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 *,
		       void *, va_list);
	int (*pages)(struct aml_binding_data *, void **, struct aml_tiling *,
		     void *, va_list);
	int (*nodes)(struct aml_binding_data *, int *, struct aml_tiling *,
		     void *, va_list);
};

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

int aml_binding_nbpages(struct aml_binding *, struct aml_tiling *, void*, ...);
int aml_binding_pages(struct aml_binding *, void **, struct aml_tiling *, void*, ...);
int aml_binding_nodes(struct aml_binding *, int *, struct aml_tiling *, void *, ...);

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

455 456 457 458
/*******************************************************************************
 * General functions:
 * Initialize internal structures, cleanup everything at the end.
 ******************************************************************************/
459

460 461
int aml_init(int *argc, char **argv[]);
int aml_finalize(void);
462

463
#endif