|
Packit |
345191 |
/*
|
|
Packit |
345191 |
* Copyright (C) 2019 Intel Corporation.
|
|
Packit |
345191 |
* All rights reserved.
|
|
Packit |
345191 |
*
|
|
Packit |
345191 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
345191 |
* modification, are permitted provided that the following conditions are met:
|
|
Packit |
345191 |
* 1. Redistributions of source code must retain the above copyright notice(s),
|
|
Packit |
345191 |
* this list of conditions and the following disclaimer.
|
|
Packit |
345191 |
* 2. Redistributions in binary form must reproduce the above copyright notice(s),
|
|
Packit |
345191 |
* this list of conditions and the following disclaimer in the documentation
|
|
Packit |
345191 |
* and/or other materials provided with the distribution.
|
|
Packit |
345191 |
*
|
|
Packit |
345191 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
|
|
Packit |
345191 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
Packit |
345191 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
Packit |
345191 |
* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
Packit |
345191 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit |
345191 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
Packit |
345191 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
Packit |
345191 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
Packit |
345191 |
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
Packit |
345191 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
345191 |
*/
|
|
Packit |
345191 |
|
|
Packit |
345191 |
#include <memkind/internal/memkind_bandwidth.h>
|
|
Packit |
345191 |
#include <memkind/internal/memkind_dax_kmem.h>
|
|
Packit |
345191 |
#include <memkind/internal/memkind_default.h>
|
|
Packit |
345191 |
#include <memkind/internal/memkind_arena.h>
|
|
Packit |
345191 |
#include <memkind/internal/memkind_private.h>
|
|
Packit |
345191 |
#include <memkind/internal/memkind_log.h>
|
|
Packit |
345191 |
#include <memkind/internal/heap_manager.h>
|
|
Packit |
345191 |
|
|
Packit |
345191 |
#include "config.h"
|
|
Packit |
345191 |
#include <pthread.h>
|
|
Packit |
345191 |
#include <numa.h>
|
|
Packit |
345191 |
#include <errno.h>
|
|
Packit |
345191 |
|
|
Packit |
345191 |
struct dax_closest_numanode_t {
|
|
Packit |
345191 |
int init_err;
|
|
Packit |
345191 |
int num_cpu;
|
|
Packit |
345191 |
struct vec_cpu_node *closest_numanode;
|
|
Packit |
345191 |
};
|
|
Packit |
345191 |
|
|
Packit |
345191 |
#define NODE_VARIANT_MULTIPLE 0
|
|
Packit |
345191 |
#define NODE_VARIANT_SINGLE 1
|
|
Packit |
345191 |
#define NODE_VARIANT_MAX 2
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static struct dax_closest_numanode_t
|
|
Packit |
345191 |
memkind_dax_kmem_closest_numanode_g[NODE_VARIANT_MAX];
|
|
Packit |
345191 |
static pthread_once_t memkind_dax_kmem_closest_numanode_once_g[NODE_VARIANT_MAX]
|
|
Packit |
345191 |
= {PTHREAD_ONCE_INIT};
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static void memkind_dax_kmem_closest_numanode_init(void);
|
|
Packit |
345191 |
static void memkind_dax_kmem_preferred_closest_numanode_init(void);
|
|
Packit |
345191 |
#ifdef MEMKIND_DAXCTL_KMEM
|
|
Packit |
345191 |
#include <daxctl/libdaxctl.h>
|
|
Packit |
345191 |
#ifndef daxctl_region_foreach_safe
|
|
Packit |
345191 |
#define daxctl_region_foreach_safe(ctx, region, _region) \
|
|
Packit |
345191 |
for (region = daxctl_region_get_first(ctx), \
|
|
Packit |
345191 |
_region = region ? daxctl_region_get_next(region) : NULL; \
|
|
Packit |
345191 |
region != NULL; \
|
|
Packit |
345191 |
region = _region, \
|
|
Packit |
345191 |
_region = _region ? daxctl_region_get_next(_region) : NULL)
|
|
Packit |
345191 |
#endif
|
|
Packit |
345191 |
|
|
Packit |
345191 |
#ifndef daxctl_dev_foreach_safe
|
|
Packit |
345191 |
#define daxctl_dev_foreach_safe(region, dev, _dev) \
|
|
Packit |
345191 |
for (dev = daxctl_dev_get_first(region), \
|
|
Packit |
345191 |
_dev = dev ? daxctl_dev_get_next(dev) : NULL; \
|
|
Packit |
345191 |
dev != NULL; \
|
|
Packit |
345191 |
dev = _dev, \
|
|
Packit |
345191 |
_dev = _dev ? daxctl_dev_get_next(_dev) : NULL)
|
|
Packit |
345191 |
#endif
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static int get_dax_kmem_nodes(struct bitmask *dax_kmem_node_mask)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
struct daxctl_region *region, *_region;
|
|
Packit |
345191 |
struct daxctl_dev *dev, *_dev;
|
|
Packit |
345191 |
struct daxctl_ctx *ctx;
|
|
Packit |
345191 |
|
|
Packit |
345191 |
int rc = daxctl_new(&ctx;;
|
|
Packit |
345191 |
if (rc < 0)
|
|
Packit |
345191 |
return MEMKIND_ERROR_UNAVAILABLE;
|
|
Packit |
345191 |
|
|
Packit |
345191 |
daxctl_region_foreach_safe(ctx, region, _region) {
|
|
Packit |
345191 |
daxctl_dev_foreach_safe(region, dev, _dev) {
|
|
Packit |
345191 |
struct daxctl_memory *mem = daxctl_dev_get_memory(dev);
|
|
Packit |
345191 |
if (mem) {
|
|
Packit |
345191 |
numa_bitmask_setbit(dax_kmem_node_mask,
|
|
Packit |
345191 |
(unsigned)daxctl_dev_get_target_node(dev));
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
daxctl_unref(ctx);
|
|
Packit |
345191 |
|
|
Packit |
345191 |
return (numa_bitmask_weight(dax_kmem_node_mask) != 0) ? MEMKIND_SUCCESS :
|
|
Packit |
345191 |
MEMKIND_ERROR_UNAVAILABLE;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static int fill_dax_kmem_values_automatic(int *bandwidth)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
return bandwidth_fill(bandwidth, get_dax_kmem_nodes);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
#else
|
|
Packit |
345191 |
static int fill_dax_kmem_values_automatic(int *bandwidth)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
log_err("DAX KMEM nodes cannot be automatically detected.");
|
|
Packit |
345191 |
return MEMKIND_ERROR_OPERATION_FAILED;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
#endif
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static int memkind_dax_kmem_check_available(struct memkind *kind)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
return kind->ops->get_mbind_nodemask(kind, NULL, 0);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static int memkind_dax_kmem_get_mbind_nodemask(struct memkind *kind,
|
|
Packit |
345191 |
unsigned long *nodemask, unsigned long maxnode)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
struct dax_closest_numanode_t *g =
|
|
Packit |
345191 |
&memkind_dax_kmem_closest_numanode_g[NODE_VARIANT_MULTIPLE];
|
|
Packit |
345191 |
pthread_once(&memkind_dax_kmem_closest_numanode_once_g[NODE_VARIANT_MULTIPLE],
|
|
Packit |
345191 |
memkind_dax_kmem_closest_numanode_init);
|
|
Packit |
345191 |
if (MEMKIND_LIKELY(!g->init_err)) {
|
|
Packit |
345191 |
g->init_err = set_bitmask_for_current_closest_numanode(nodemask, maxnode,
|
|
Packit |
345191 |
g->closest_numanode, g->num_cpu);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
return g->init_err;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static int memkind_dax_kmem_get_preferred_mbind_nodemask(struct memkind *kind,
|
|
Packit |
345191 |
unsigned long *nodemask, unsigned long maxnode)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
struct dax_closest_numanode_t *g =
|
|
Packit |
345191 |
&memkind_dax_kmem_closest_numanode_g[NODE_VARIANT_SINGLE];
|
|
Packit |
345191 |
pthread_once(&memkind_dax_kmem_closest_numanode_once_g[NODE_VARIANT_SINGLE],
|
|
Packit |
345191 |
memkind_dax_kmem_preferred_closest_numanode_init);
|
|
Packit |
345191 |
if (MEMKIND_LIKELY(!g->init_err)) {
|
|
Packit |
345191 |
g->init_err = set_bitmask_for_current_closest_numanode(nodemask, maxnode,
|
|
Packit |
345191 |
g->closest_numanode, g->num_cpu);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
return g->init_err;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
MEMKIND_EXPORT int memkind_dax_kmem_all_get_mbind_nodemask(struct memkind *kind,
|
|
Packit |
345191 |
unsigned long *nodemask, unsigned long maxnode)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
struct dax_closest_numanode_t *g =
|
|
Packit |
345191 |
&memkind_dax_kmem_closest_numanode_g[NODE_VARIANT_MULTIPLE];
|
|
Packit |
345191 |
pthread_once(&memkind_dax_kmem_closest_numanode_once_g[NODE_VARIANT_MULTIPLE],
|
|
Packit |
345191 |
memkind_dax_kmem_closest_numanode_init);
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (MEMKIND_LIKELY(!g->init_err)) {
|
|
Packit |
345191 |
set_bitmask_for_all_closest_numanodes(nodemask, maxnode, g->closest_numanode,
|
|
Packit |
345191 |
g->num_cpu);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
return g->init_err;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static void memkind_dax_kmem_closest_numanode_init(void)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
struct dax_closest_numanode_t *g =
|
|
Packit |
345191 |
&memkind_dax_kmem_closest_numanode_g[NODE_VARIANT_MULTIPLE];
|
|
Packit |
345191 |
g->num_cpu = numa_num_configured_cpus();
|
|
Packit |
345191 |
g->closest_numanode = NULL;
|
|
Packit |
345191 |
g->init_err = set_closest_numanode(fill_dax_kmem_values_automatic,
|
|
Packit |
345191 |
"MEMKIND_DAX_KMEM_NODES", &g->closest_numanode, g->num_cpu, false);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static void memkind_dax_kmem_preferred_closest_numanode_init(void)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
struct dax_closest_numanode_t *g =
|
|
Packit |
345191 |
&memkind_dax_kmem_closest_numanode_g[NODE_VARIANT_SINGLE];
|
|
Packit |
345191 |
g->num_cpu = numa_num_configured_cpus();
|
|
Packit |
345191 |
g->closest_numanode = NULL;
|
|
Packit |
345191 |
g->init_err = set_closest_numanode(fill_dax_kmem_values_automatic,
|
|
Packit |
345191 |
"MEMKIND_DAX_KMEM_NODES", &g->closest_numanode, g->num_cpu, true);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static void memkind_dax_kmem_init_once(void)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
memkind_init(MEMKIND_DAX_KMEM, true);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static void memkind_dax_kmem_all_init_once(void)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
memkind_init(MEMKIND_DAX_KMEM_ALL, true);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static void memkind_dax_kmem_preferred_init_once(void)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
memkind_init(MEMKIND_DAX_KMEM_PREFERRED, true);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
MEMKIND_EXPORT struct memkind_ops MEMKIND_DAX_KMEM_OPS = {
|
|
Packit |
345191 |
.create = memkind_arena_create,
|
|
Packit |
345191 |
.destroy = memkind_default_destroy,
|
|
Packit |
345191 |
.malloc = memkind_arena_malloc,
|
|
Packit |
345191 |
.calloc = memkind_arena_calloc,
|
|
Packit |
345191 |
.posix_memalign = memkind_arena_posix_memalign,
|
|
Packit |
345191 |
.realloc = memkind_arena_realloc,
|
|
Packit |
345191 |
.free = memkind_arena_free,
|
|
Packit |
345191 |
.check_available = memkind_dax_kmem_check_available,
|
|
Packit |
345191 |
.mbind = memkind_default_mbind,
|
|
Packit |
345191 |
.get_mmap_flags = memkind_default_get_mmap_flags,
|
|
Packit |
345191 |
.get_mbind_mode = memkind_default_get_mbind_mode,
|
|
Packit |
345191 |
.get_mbind_nodemask = memkind_dax_kmem_get_mbind_nodemask,
|
|
Packit |
345191 |
.get_arena = memkind_thread_get_arena,
|
|
Packit |
345191 |
.init_once = memkind_dax_kmem_init_once,
|
|
Packit |
345191 |
.malloc_usable_size = memkind_default_malloc_usable_size,
|
|
Packit |
345191 |
.finalize = memkind_arena_finalize,
|
|
Packit |
345191 |
.get_stat = memkind_arena_get_kind_stat,
|
|
Packit |
345191 |
.defrag_reallocate = memkind_arena_defrag_reallocate
|
|
Packit |
345191 |
};
|
|
Packit |
345191 |
|
|
Packit |
345191 |
MEMKIND_EXPORT struct memkind_ops MEMKIND_DAX_KMEM_ALL_OPS = {
|
|
Packit |
345191 |
.create = memkind_arena_create,
|
|
Packit |
345191 |
.destroy = memkind_default_destroy,
|
|
Packit |
345191 |
.malloc = memkind_arena_malloc,
|
|
Packit |
345191 |
.calloc = memkind_arena_calloc,
|
|
Packit |
345191 |
.posix_memalign = memkind_arena_posix_memalign,
|
|
Packit |
345191 |
.realloc = memkind_arena_realloc,
|
|
Packit |
345191 |
.free = memkind_arena_free,
|
|
Packit |
345191 |
.check_available = memkind_dax_kmem_check_available,
|
|
Packit |
345191 |
.mbind = memkind_default_mbind,
|
|
Packit |
345191 |
.get_mmap_flags = memkind_default_get_mmap_flags,
|
|
Packit |
345191 |
.get_mbind_mode = memkind_default_get_mbind_mode,
|
|
Packit |
345191 |
.get_mbind_nodemask = memkind_dax_kmem_all_get_mbind_nodemask,
|
|
Packit |
345191 |
.get_arena = memkind_thread_get_arena,
|
|
Packit |
345191 |
.init_once = memkind_dax_kmem_all_init_once,
|
|
Packit |
345191 |
.malloc_usable_size = memkind_default_malloc_usable_size,
|
|
Packit |
345191 |
.finalize = memkind_arena_finalize,
|
|
Packit |
345191 |
.get_stat = memkind_arena_get_kind_stat,
|
|
Packit |
345191 |
.defrag_reallocate = memkind_arena_defrag_reallocate
|
|
Packit |
345191 |
};
|
|
Packit |
345191 |
|
|
Packit |
345191 |
MEMKIND_EXPORT struct memkind_ops MEMKIND_DAX_KMEM_PREFERRED_OPS = {
|
|
Packit |
345191 |
.create = memkind_arena_create,
|
|
Packit |
345191 |
.destroy = memkind_default_destroy,
|
|
Packit |
345191 |
.malloc = memkind_arena_malloc,
|
|
Packit |
345191 |
.calloc = memkind_arena_calloc,
|
|
Packit |
345191 |
.posix_memalign = memkind_arena_posix_memalign,
|
|
Packit |
345191 |
.realloc = memkind_arena_realloc,
|
|
Packit |
345191 |
.free = memkind_arena_free,
|
|
Packit |
345191 |
.check_available = memkind_dax_kmem_check_available,
|
|
Packit |
345191 |
.mbind = memkind_default_mbind,
|
|
Packit |
345191 |
.get_mmap_flags = memkind_default_get_mmap_flags,
|
|
Packit |
345191 |
.get_mbind_mode = memkind_preferred_get_mbind_mode,
|
|
Packit |
345191 |
.get_mbind_nodemask = memkind_dax_kmem_get_preferred_mbind_nodemask,
|
|
Packit |
345191 |
.get_arena = memkind_thread_get_arena,
|
|
Packit |
345191 |
.init_once = memkind_dax_kmem_preferred_init_once,
|
|
Packit |
345191 |
.malloc_usable_size = memkind_default_malloc_usable_size,
|
|
Packit |
345191 |
.finalize = memkind_arena_finalize,
|
|
Packit |
345191 |
.get_stat = memkind_arena_get_kind_stat,
|
|
Packit |
345191 |
.defrag_reallocate = memkind_arena_defrag_reallocate
|
|
Packit |
345191 |
};
|