Commit 9059049c authored by Swann Perarnau's avatar Swann Perarnau
Browse files

[refactor] convert area to new errors, create

Convert area/area_linux to new error handler.
Implement #31 for area.
Checkpatch area

Note that area_linux is still using a dynamic data structure no matter
what: a bitmask to the set of authorized nodes. The conversion to
aml_bitmap wouldn't help as there's no default value available for all
nodes on that type (similar to numa_all_nodes_ptr).
parent e243f7eb
......@@ -13,6 +13,9 @@
# files
include/aml.h
include/aml/area/linux.h
include/aml/utils/error.h
src/aml.c
src/area/area.c
src/area/linux.c
src/utils/error.c
......@@ -34,10 +34,10 @@ int main(int argc, char *argv[])
long int N = atol(argv[3]);
unsigned long memsize = sizeof(double)*N*N;
slow = aml_area_linux_create(AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
aml_area_linux_create(&slow, AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
&slowb, AML_AREA_LINUX_BINDING_FLAG_BIND);
assert(slow != NULL);
fast = aml_area_linux_create(AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
aml_area_linux_create(&fast, AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
&fastb, AML_AREA_LINUX_BINDING_FLAG_BIND);
assert(fast != NULL);
......@@ -65,8 +65,8 @@ int main(int argc, char *argv[])
aml_area_munmap(slow, a, memsize);
aml_area_munmap(slow, b, memsize);
aml_area_munmap(fast, c, memsize);
aml_area_linux_destroy(slow);
aml_area_linux_destroy(fast);
aml_area_linux_destroy(&slow);
aml_area_linux_destroy(&fast);
aml_finalize();
return 0;
}
......@@ -77,10 +77,10 @@ int main(int argc, char* argv[])
assert(!aml_tiling_init(&tiling_col, AML_TILING_TYPE_2D_COLMAJOR,
tilesize, memsize, N/T , N/T));
slow = aml_area_linux_create(AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
aml_area_linux_create(&slow, AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
&slowb, AML_AREA_LINUX_BINDING_FLAG_BIND);
assert(slow != NULL);
fast = aml_area_linux_create(AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
aml_area_linux_create(&fast, AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
&fastb, AML_AREA_LINUX_BINDING_FLAG_BIND);
assert(fast != NULL);
......@@ -154,8 +154,8 @@ int main(int argc, char* argv[])
aml_area_munmap(slow, a, memsize);
aml_area_munmap(slow, b, memsize);
aml_area_munmap(fast, c, memsize);
aml_area_linux_destroy(slow);
aml_area_linux_destroy(fast);
aml_area_linux_destroy(&slow);
aml_area_linux_destroy(&fast);
aml_tiling_destroy(&tiling_row, AML_TILING_TYPE_2D_ROWMAJOR);
aml_tiling_destroy(&tiling_col, AML_TILING_TYPE_2D_ROWMAJOR);
aml_finalize();
......
......@@ -105,10 +105,10 @@ int main(int argc, char* argv[])
assert(!aml_tiling_init(&tiling_prefetch, AML_TILING_TYPE_1D,
tilesize*(N/T), memsize));
slow = aml_area_linux_create(AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
aml_area_linux_create(&slow, AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
&slowb, AML_AREA_LINUX_BINDING_FLAG_BIND);
assert(slow != NULL);
fast = aml_area_linux_create(AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
aml_area_linux_create(&fast, AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
&fastb, AML_AREA_LINUX_BINDING_FLAG_BIND);
assert(fast != NULL);
......@@ -188,8 +188,8 @@ int main(int argc, char* argv[])
aml_area_munmap(slow, a, memsize);
aml_area_munmap(slow, b, memsize);
aml_area_munmap(fast, c, memsize);
aml_area_linux_destroy(slow);
aml_area_linux_destroy(fast);
aml_area_linux_destroy(&slow);
aml_area_linux_destroy(&fast);
aml_tiling_destroy(&tiling_row, AML_TILING_TYPE_2D_ROWMAJOR);
aml_tiling_destroy(&tiling_col, AML_TILING_TYPE_2D_ROWMAJOR);
aml_tiling_destroy(&tiling_prefetch, AML_TILING_TYPE_1D);
......
......@@ -64,10 +64,10 @@ int main(int argc, char *argv[])
/* initialize all the supporting struct */
assert(!aml_tiling_init(&tiling, AML_TILING_TYPE_1D, tilesz, memsize));
slow = aml_area_linux_create(AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
aml_area_linux_create(&slow, AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
&slowb, AML_AREA_LINUX_BINDING_FLAG_BIND);
assert(slow != NULL);
fast = aml_area_linux_create(AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
aml_area_linux_create(&fast, AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
&fastb, AML_AREA_LINUX_BINDING_FLAG_BIND);
assert(fast != NULL);
assert(!aml_dma_linux_par_init(&dma, numthreads*2, numthreads));
......@@ -124,8 +124,8 @@ int main(int argc, char *argv[])
aml_area_munmap(slow, a, memsize);
aml_area_munmap(slow, b, memsize);
aml_area_munmap(fast, c, memsize);
aml_area_linux_destroy(slow);
aml_area_linux_destroy(fast);
aml_area_linux_destroy(&slow);
aml_area_linux_destroy(&fast);
aml_tiling_destroy(&tiling, AML_TILING_TYPE_1D);
aml_finalize();
return 0;
......
......@@ -95,10 +95,10 @@ int main(int argc, char *argv[])
/* initialize all the supporting struct */
assert(!aml_tiling_init(&tiling, AML_TILING_TYPE_1D, tilesz, memsize));
slow = aml_area_linux_create(AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
aml_area_linux_create(&slow, AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
&slowb, AML_AREA_LINUX_BINDING_FLAG_BIND);
assert(slow != NULL);
fast = aml_area_linux_create(AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
aml_area_linux_create(&fast, AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
&fastb, AML_AREA_LINUX_BINDING_FLAG_BIND);
assert(fast != NULL);
assert(!aml_dma_linux_seq_init(&dma, numthreads*2));
......@@ -137,8 +137,8 @@ int main(int argc, char *argv[])
aml_area_munmap(slow, a, memsize);
aml_area_munmap(slow, b, memsize);
aml_area_munmap(fast, c, memsize);
aml_area_linux_destroy(slow);
aml_area_linux_destroy(fast);
aml_area_linux_destroy(&slow);
aml_area_linux_destroy(&fast);
aml_tiling_destroy(&tiling, AML_TILING_TYPE_1D);
aml_finalize();
return 0;
......
......@@ -101,10 +101,10 @@ int main(int argc, char *argv[])
/* initialize all the supporting struct */
assert(!aml_tiling_init(&tiling, AML_TILING_TYPE_1D, tilesz, memsize));
slow = aml_area_linux_create(AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
aml_area_linux_create(&slow, AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
&slowb, AML_AREA_LINUX_BINDING_FLAG_BIND);
assert(slow != NULL);
fast = aml_area_linux_create(AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
aml_area_linux_create(&fast, AML_AREA_LINUX_MMAP_FLAG_PRIVATE,
&fastb, AML_AREA_LINUX_BINDING_FLAG_BIND);
assert(fast != NULL);
assert(!aml_dma_linux_seq_init(&dma, (size_t)numthreads*4));
......@@ -148,8 +148,8 @@ int main(int argc, char *argv[])
aml_area_munmap(slow, a, memsize);
aml_area_munmap(slow, b, memsize);
aml_area_munmap(fast, c, memsize);
aml_area_linux_destroy(slow);
aml_area_linux_destroy(fast);
aml_area_linux_destroy(&slow);
aml_area_linux_destroy(&fast);
aml_tiling_destroy(&tiling, AML_TILING_TYPE_1D);
aml_finalize();
return 0;
......
......@@ -37,38 +37,12 @@ struct aml_area_linux_data {
};
/* Default linux area with private mapping and no binding. */
extern const struct aml_area aml_area_linux;
#define AML_AREA_LINUX_DECL(name) \
struct aml_area_linux_data __ ##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_data) + \
sizeof(struct aml_area))
/**
* Initialize area data with struct aml_area_linux_binding. Subsequent calls to
* aml_area_mmap() with this returned area will apply binding settings.
* Returns NULL on failure with aml_errno set to:
* - AML_AREA_ENOMEM if there is not enough memory available for the operation
* - AML_AREA_EINVAL flags were not one of linux area flags.
* - AML_AREA_EDOM if binding nodeset is out of allowed nodeset.
**/
struct aml_area* aml_area_linux_create(const int mmap_flags,
const struct aml_bitmap *nodemask,
const int binding_flags);
extern struct aml_area aml_area_linux;
/**
* Destroy area data containing struct aml_area_linux_binding
**/
void
aml_area_linux_destroy(struct aml_area* area);
/*******************************************************************************
* Linux operators
*******************************************************************************/
/**
* Bind memory of size "size" pointed by "ptr" to binding set in "bind".
......@@ -107,8 +81,64 @@ aml_area_linux_mmap(const struct aml_area_data *area_data,
* error investigations.
**/
int
aml_area_linux_munmap(const struct aml_area_data* area_data,
aml_area_linux_munmap(const struct aml_area_data *area_data,
void *ptr,
const size_t size);
/*******************************************************************************
* create/destroy and others
*******************************************************************************/
#define AML_AREA_LINUX_DECL(name) \
struct aml_area_linux_data __ ##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_data) + \
sizeof(struct aml_area))
/**
* Allocate and initialize a struct aml_area implemented by aml_area_linux
* operations.
* @param[out] area pointer to an uninitialized struct aml_area pointer to
* receive the new area.
* @param[in] mmap_flags flags to use when retrieving virtual memory with mmap
* @param[in] binding_flags, flags to use when binding memory.
* @param[in] nodemask list of memory nodes to use. Default to allowed memory
* nodes if NULL.
* @return On success, returns 0 and area points to the new aml_area.
* @return On failure, sets area to NULL and returns one of AML error codes:
* - AML_ENOMEM if there wasn't enough memory available.
* - AML_EINVAL if inputs flags were invalid.
* - AML_EDOM the nodemask provided is out of bounds (allowed nodeset).
**/
int aml_area_linux_create(struct aml_area **area, const int mmap_flags,
const struct aml_bitmap *nodemask,
const int binding_flags);
/**
* Initialize a struct aml_area declared using the AML_AREA_LINUX_DECL macro.
* See aml_area_linux_create for details on arguments.
*/
int aml_area_linux_init(struct aml_area *area, const int mmap_flags,
const struct aml_bitmap *nodemask,
const int binding_flags);
/**
* Finalize a struct aml_area initialized with aml_area_linux_init.
*/
void aml_area_linux_fini(struct aml_area *area);
/**
* Destroy (finalize and free resources) a struct aml_area created by
* aml_area_linux_create.
*
* @param area is NULL after this call.
**/
void aml_area_linux_destroy(struct aml_area **area);
#endif //AML_AREA_LINUX_NUMA_H
......@@ -8,24 +8,21 @@
* SPDX-License-Identifier: BSD-3-Clause
*******************************************************************************/
#include <stdlib.h>
#include "aml.h"
#include <stdlib.h>
void*
aml_area_mmap(const struct aml_area *area,
void **ptr,
size_t size)
void *aml_area_mmap(const struct aml_area *area, void **ptr, size_t size)
{
if(size == 0)
if (size == 0)
return NULL;
if(area == NULL){
aml_errno = AML_AREA_EINVAL;
if (area == NULL) {
aml_errno = AML_EINVAL;
return NULL;
}
if(area->ops->mmap == NULL){
aml_errno = AML_AREA_ENOTSUP;
if (area->ops->mmap == NULL) {
aml_errno = AML_ENOTSUP;
return NULL;
}
......@@ -33,19 +30,16 @@ aml_area_mmap(const struct aml_area *area,
return area->ops->mmap(area->data, ptr, size);
}
int
aml_area_munmap(const struct aml_area *area,
void *ptr,
size_t size)
int aml_area_munmap(const struct aml_area *area, void *ptr, size_t size)
{
if(ptr == NULL || size == 0)
if (ptr == NULL || size == 0)
return AML_SUCCESS;
if(area == NULL)
return AML_AREA_EINVAL;
if (area == NULL)
return -AML_EINVAL;
if(area->ops->munmap == NULL)
return AML_AREA_ENOTSUP;
if (area->ops->munmap == NULL)
return -AML_ENOTSUP;
return area->ops->munmap(area->data, ptr, size);
}
......
......@@ -15,108 +15,6 @@
#define AML_AREA_LINUX_MBIND_FLAGS MPOL_MF_MOVE
static int aml_area_linux_check_mmap_flags(const int mmap_flags){
switch(mmap_flags){
case AML_AREA_LINUX_MMAP_FLAG_PRIVATE:
break;
case AML_AREA_LINUX_MMAP_FLAG_SHARED:
break;
default:
return 0;
}
return 1;
}
static int aml_area_linux_check_binding_flags(const int binding_flags){
switch(binding_flags){
case AML_AREA_LINUX_BINDING_FLAG_BIND:
break;
case AML_AREA_LINUX_BINDING_FLAG_INTERLEAVE:
break;
case AML_AREA_LINUX_BINDING_FLAG_PREFERRED:
break;
default:
return 0;
}
return 1;
}
struct aml_area* aml_area_linux_create(const int mmap_flags,
const struct aml_bitmap *nodemask,
const int binding_flags)
{
struct aml_area_linux_data *d;
struct aml_area *area;
/* Check flags */
if(!aml_area_linux_check_mmap_flags(mmap_flags) ||
!aml_area_linux_check_binding_flags(binding_flags)){
aml_errno = AML_AREA_EINVAL;
return NULL;
}
d = malloc(sizeof(*d));
if(d == NULL){
aml_errno = AML_AREA_ENOMEM;
return NULL;
}
/* Check/set nodemask */
d->nodeset = numa_get_mems_allowed();
if(d->nodeset == NULL){
aml_errno = AML_AREA_ENOMEM;
goto err_with_data;
}
if(nodemask != NULL){
int aml_last = aml_bitmap_last(nodemask);
int allowed_last = numa_bitmask_weight(d->nodeset);
while(!numa_bitmask_isbitset(d->nodeset, --allowed_last));
if(aml_last > allowed_last){
aml_errno = AML_AREA_EDOM;
goto err_with_nodeset;
}
aml_bitmap_copy_to_ulong(nodemask,
d->nodeset->maskp,
d->nodeset->size);
}
/* set area_data and area */
d->binding_flags = binding_flags;
d->mmap_flags = mmap_flags;
area = malloc(sizeof(*area));
if(area == NULL){
aml_errno = AML_AREA_ENOMEM;
goto err_with_nodeset;
}
area->data = (struct aml_area_data*)d;
area->ops = &aml_area_linux_ops;
return area;
err_with_nodeset:
numa_free_nodemask(d->nodeset);
err_with_data:
free(d);
return NULL;
}
void
aml_area_linux_destroy(struct aml_area* area){
if(area == NULL || area->data == NULL)
return;
struct aml_area_linux_data *d = (struct aml_area_linux_data *)area->data;
if(d->nodeset != NULL)
numa_free_nodemask(d->nodeset);
free(d);
}
int
aml_area_linux_mbind(struct aml_area_linux_data *bind,
void *ptr,
......@@ -124,10 +22,12 @@ aml_area_linux_mbind(struct aml_area_linux_data *bind,
{
struct bitmask *nodeset;
if(bind->nodeset != NULL)
assert(bind != NULL);
nodeset = bind->nodeset;
else
nodeset = numa_get_mems_allowed();
if (nodeset == NULL)
nodeset = numa_all_nodes_ptr;
long err = mbind(ptr,
size,
......@@ -136,9 +36,9 @@ aml_area_linux_mbind(struct aml_area_linux_data *bind,
nodeset->size,
AML_AREA_LINUX_MBIND_FLAGS);
if(err == 0)
if (err == 0)
return AML_SUCCESS;
return AML_FAILURE;
return -AML_FAILURE;
}
int
......@@ -150,8 +50,8 @@ aml_area_linux_check_binding(struct aml_area_linux_data *area_data,
struct bitmask *nodeset;
nodeset = numa_allocate_nodemask();
if(nodeset == NULL)
return AML_AREA_ENOMEM;
if (nodeset == NULL)
return -AML_ENOMEM;
err = get_mempolicy(&mode,
nodeset->maskp,
......@@ -159,20 +59,23 @@ aml_area_linux_check_binding(struct aml_area_linux_data *area_data,
ptr,
AML_AREA_LINUX_MBIND_FLAGS);
if(err < 0){
err = AML_AREA_EINVAL;
if (err < 0) {
err = -AML_EINVAL;
goto out;
}
err = 1;
if(mode != area_data->binding_flags)
if (mode != area_data->binding_flags)
err = 0;
for(i=0; i<numa_max_possible_node(); i++){
for (i = 0; i < numa_max_possible_node(); i++) {
int ptr_set = numa_bitmask_isbitset(nodeset, i);
int bitmask_set = numa_bitmask_isbitset(area_data->nodeset, i);
if(mode == AML_AREA_LINUX_BINDING_FLAG_BIND && ptr_set != bitmask_set)
if (mode == AML_AREA_LINUX_BINDING_FLAG_BIND &&
ptr_set != bitmask_set)
goto binding_failed;
if(mode == AML_AREA_LINUX_BINDING_FLAG_INTERLEAVE && ptr_set && !bitmask_set)
if (mode == AML_AREA_LINUX_BINDING_FLAG_INTERLEAVE &&
ptr_set && !bitmask_set)
goto binding_failed;
}
......@@ -184,19 +87,17 @@ aml_area_linux_check_binding(struct aml_area_linux_data *area_data,
return err;
}
void* aml_area_linux_mmap(const struct aml_area_data *area_data,
void *aml_area_linux_mmap(const struct aml_area_data *area_data,
void *ptr,
size_t size)
{
struct aml_area_linux_data *data = (struct aml_area_linux_data *) area_data;
struct aml_area_linux_data *data =
(struct aml_area_linux_data *) area_data;
void * out = mmap(ptr,
size,
PROT_READ|PROT_WRITE,
data->mmap_flags,
0, 0);
void *out = mmap(ptr, size, PROT_READ|PROT_WRITE,
data->mmap_flags, 0, 0);
if(out == MAP_FAILED){
if (out == MAP_FAILED) {
out = NULL;
aml_errno = AML_FAILURE;
}
......@@ -204,34 +105,35 @@ void* aml_area_linux_mmap(const struct aml_area_data *area_data,
return out;
}
int
aml_area_linux_munmap(__attribute__ ((unused)) const struct aml_area_data* area_data,
void *ptr,
const size_t size)
int aml_area_linux_munmap(
__attribute__ ((unused)) const struct aml_area_data *area_data,
void *ptr, const size_t size)
{
int err = munmap(ptr, size);
if(err == -1)
return AML_FAILURE;
if (err == -1)
return -AML_FAILURE;
return AML_SUCCESS;
}
static void*
aml_area_linux_mmap_mbind(const struct aml_area_data *area_data,
void *aml_area_linux_mmap_mbind(const struct aml_area_data *area_data,
void *ptr,
size_t size)
{
void * out = aml_area_linux_mmap(area_data, ptr, size);
void *out = aml_area_linux_mmap(area_data, ptr, size);
if(out == NULL)
if (out == NULL)
return NULL;
struct aml_area_linux_data *data = (struct aml_area_linux_data *) area_data;
struct aml_area_linux_data *data =
(struct aml_area_linux_data *) area_data;
if(data->nodeset != NULL || data->binding_flags != MPOL_DEFAULT){
if (data->nodeset != NULL || data->binding_flags != MPOL_DEFAULT) {
int err = aml_area_linux_mbind(data, out, size);
if(err != AML_SUCCESS){
aml_errno = err;
if (err != AML_SUCCESS) {
aml_errno = -err;
munmap(out, size);
return NULL;
}
......@@ -240,11 +142,142 @@ aml_area_linux_mmap_mbind(const struct aml_area_data *area_data,
return out;
}
/*****************************************************************************
/*******************************************************************************
* Areas Initialization
******************************************************************************/
static int aml_area_linux_check_mmap_flags(const int mmap_flags)
{
switch (mmap_flags) {
case AML_AREA_LINUX_MMAP_FLAG_PRIVATE:
break;
case AML_AREA_LINUX_MMAP_FLAG_SHARED:
break;
default:
return 0;
}
return 1;
}
static int aml_area_linux_check_binding_flags(const int binding_flags)
{
switch (binding_flags) {
case AML_AREA_LINUX_BINDING_FLAG_BIND:
break;
case AML_AREA_LINUX_BINDING_FLAG_INTERLEAVE:
break;
case AML_AREA_LINUX_BINDING_FLAG_PREFERRED:
break;
default:
return 0;
}
return 1;
}
int aml_area_linux_create(struct aml_area **area, const int mmap_flags,
const struct aml_bitmap *nodemask,
const int binding_flags)
{
struct aml_area *ret = NULL;
intptr_t baseptr, dataptr;
int err = AML_SUCCESS;
baseptr = (intptr_t) calloc(1, AML_AREA_LINUX_ALLOCSIZE);
if (baseptr == 0) {
*area = NULL;