Blame include/memkind.h

Packit 345191
/*
Packit 345191
 * Copyright (C) 2014 - 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
#pragma once
Packit 345191
#ifdef __cplusplus
Packit 345191
extern "C" {
Packit 345191
#endif
Packit 345191
Packit 345191
#include <sys/types.h>
Packit 345191
Packit 345191
/**
Packit 345191
 * Header file for the memkind heap manager.
Packit 345191
 * More details in memkind(3) man page.
Packit 345191
 *
Packit 345191
 * API standards are described in memkind(3) man page.
Packit 345191
 */
Packit 345191
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
#define _MEMKIND_BIT(N) (1ull << N)
Packit 345191
Packit 345191
/// \brief Memkind memory types
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
typedef enum memkind_memtype_t {
Packit 345191
Packit 345191
    /**
Packit 345191
     * Select standard memory, the same as process use.
Packit 345191
     */
Packit 345191
    MEMKIND_MEMTYPE_DEFAULT = _MEMKIND_BIT(0),
Packit 345191
Packit 345191
    /**
Packit 345191
     * Select high bandwidth memory (HBM).
Packit 345191
     * There must be at least two memories with different bandwidth to
Packit 345191
     * determine the HBM.
Packit 345191
     */
Packit 345191
    MEMKIND_MEMTYPE_HIGH_BANDWIDTH = _MEMKIND_BIT(1)
Packit 345191
Packit 345191
} memkind_memtype_t;
Packit 345191
Packit 345191
#undef _MEMKIND_BIT
Packit 345191
Packit 345191
/// \brief Memkind policy
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
typedef enum memkind_policy_t {
Packit 345191
Packit 345191
    /**
Packit 345191
     * Allocate local memory.
Packit 345191
     * If there is not enough memory to satisfy the request errno is set to
Packit 345191
     * ENOMEM and the allocated pointer is set to NULL.
Packit 345191
     */
Packit 345191
    MEMKIND_POLICY_BIND_LOCAL = 0,
Packit 345191
Packit 345191
    /**
Packit 345191
     * Memory locality is ignored.
Packit 345191
     * If there is not enough memory to satisfy the request errno is set to
Packit 345191
     * ENOMEM and the allocated pointer is set to NULL.
Packit 345191
     */
Packit 345191
    MEMKIND_POLICY_BIND_ALL,
Packit 345191
Packit 345191
    /**
Packit 345191
     * Allocate preferred memory that is local.
Packit 345191
     * If there is not enough preferred memory to satisfy the request or
Packit 345191
     * preferred memory is not available, the allocation will fall back on any
Packit 345191
     * other memory.
Packit 345191
     */
Packit 345191
    MEMKIND_POLICY_PREFERRED_LOCAL,
Packit 345191
Packit 345191
    /**
Packit 345191
     * Interleave allocation across local memory.
Packit 345191
     * For n memory types the allocation will be interleaved across all of
Packit 345191
     * them.
Packit 345191
     */
Packit 345191
    MEMKIND_POLICY_INTERLEAVE_LOCAL,
Packit 345191
Packit 345191
    /**
Packit 345191
     * Interleave allocation. Locality is ignored.
Packit 345191
     * For n memory types the allocation will be interleaved across all of
Packit 345191
     * them.
Packit 345191
     */
Packit 345191
    MEMKIND_POLICY_INTERLEAVE_ALL,
Packit 345191
Packit 345191
    /**
Packit 345191
     * Max policy value.
Packit 345191
     */
Packit 345191
    MEMKIND_POLICY_MAX_VALUE
Packit 345191
Packit 345191
} memkind_policy_t;
Packit 345191
Packit 345191
/// \brief Memkind bits definition
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
/// \note The bits specify flags and masks. Bits <0,1,2,...,7> are reserved for page size, where page sizes are encoded
Packit 345191
///       by base-2 logarithm. If the page size bits are set to zero value, than default page size will be used.
Packit 345191
typedef enum memkind_bits_t {
Packit 345191
    MEMKIND_MASK_PAGE_SIZE_2MB = 21ull,  /**<  Allocations backed by 2 MB page size (2^21 = 2MB) */
Packit 345191
} memkind_bits_t;
Packit 345191
Packit 345191
/// \brief Memkind type definition
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
typedef struct memkind *memkind_t;
Packit 345191
Packit 345191
Packit 345191
/// \brief Memkind constant values
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
enum memkind_const {
Packit 345191
    MEMKIND_MAX_KIND = 512,                     /**<  Maximum number of kinds */
Packit 345191
    MEMKIND_ERROR_MESSAGE_SIZE = 128,           /**<  Error message size */
Packit 345191
    MEMKIND_PMEM_MIN_SIZE = (1024 * 1024 * 16)  /**<  The minimum size which allows to limit the file-backed memory partition */
Packit 345191
};
Packit 345191
Packit 345191
/// \brief Memkind operation statuses
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
enum {
Packit 345191
    MEMKIND_SUCCESS = 0,                        /**<  Operation success */
Packit 345191
    MEMKIND_ERROR_UNAVAILABLE = -1,             /**<  Error: Memory kind is not available */
Packit 345191
    MEMKIND_ERROR_MBIND = -2,                   /**<  Error: Call to mbind() failed */
Packit 345191
    MEMKIND_ERROR_MMAP  = -3,                   /**<  Error: Call to mmap() failed */
Packit 345191
    MEMKIND_ERROR_MALLOC = -6,                  /**<  Error: Call to malloc() failed */
Packit 345191
    MEMKIND_ERROR_ENVIRON = -12,                /**<  Error: Unable to parse environment variable */
Packit 345191
    MEMKIND_ERROR_INVALID = -13,                /**<  Error: Invalid argument */
Packit 345191
    MEMKIND_ERROR_TOOMANY = -15,                /**<  Error: Attempt to initialize more than MEMKIND_MAX_KIND number of kinds */
Packit 345191
    MEMKIND_ERROR_BADOPS = -17,                 /**<  Error: Invalid memkind_ops structure */
Packit 345191
    MEMKIND_ERROR_HUGETLB = -18,                /**<  Error: Unable to allocate huge pages */
Packit 345191
    MEMKIND_ERROR_MEMTYPE_NOT_AVAILABLE = -20,  /**<  Error: Requested memory type is not available */
Packit 345191
    MEMKIND_ERROR_OPERATION_FAILED = -21,       /**<  Error: Operation failed */
Packit 345191
    MEMKIND_ERROR_ARENAS_CREATE = -22,          /**<  Error: Call to jemalloc's arenas.create failed */
Packit 345191
    MEMKIND_ERROR_RUNTIME = -255                /**<  Error: Unspecified run-time error */
Packit 345191
};
Packit 345191
Packit 345191
/* KIND CONFIGURATION MANAGEMENT INTERFACE */
Packit 345191
Packit 345191
/// \brief Memkind memory usage policy
Packit 345191
typedef enum memkind_mem_usage_policy {
Packit 345191
    MEMKIND_MEM_USAGE_POLICY_DEFAULT      = 0,        /**<  Default memory usage */
Packit 345191
    MEMKIND_MEM_USAGE_POLICY_CONSERVATIVE = 1,        /**<  Minimize memory usage at all costs, */
Packit 345191
    MEMKIND_MEM_USAGE_POLICY_MAX_VALUE
Packit 345191
} memkind_mem_usage_policy;
Packit 345191
Packit 345191
/// \brief Memkind memory statistics type
Packit 345191
typedef enum memkind_stat_type {
Packit 345191
    MEMKIND_STAT_TYPE_RESIDENT      = 0,        /**<  Maximum number of bytes in physically resident data pages mapped */
Packit 345191
    MEMKIND_STAT_TYPE_ACTIVE        = 1,        /**<  Total number of bytes in active pages */
Packit 345191
    MEMKIND_STAT_TYPE_ALLOCATED     = 2,        /**<  Total number of allocated bytes */
Packit 345191
    MEMKIND_STAT_TYPE_MAX_VALUE
Packit 345191
} memkind_stat_type;
Packit 345191
Packit 345191
/// \brief Forward declaration of memkind configuration
Packit 345191
struct memkind_config;
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Create a memkind configuration
Packit 345191
/// \note STANDARD API
Packit 345191
/// \return Memkind configuration, NULL on failure
Packit 345191
///
Packit 345191
struct memkind_config *memkind_config_new(void);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Delete memkind configuration
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param cfg memkind configuration
Packit 345191
///
Packit 345191
void memkind_config_delete(struct memkind_config *cfg);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Update memkind configuration with path to specified directory parameter
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param cfg memkind configuration
Packit 345191
/// \param pmem_dir path to specified directory for PMEM kind
Packit 345191
///
Packit 345191
void memkind_config_set_path(struct memkind_config *cfg, const char *pmem_dir);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Update memkind configuration with PMEM kind size
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param cfg memkind configuration
Packit 345191
/// \param pmem_size size limit for PMEM kind
Packit 345191
///
Packit 345191
void memkind_config_set_size(struct memkind_config *cfg, size_t pmem_size);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Update memkind configuration with memory usage policy parameter
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param cfg memkind configuration
Packit 345191
/// \param policy memkind memory usage policy
Packit 345191
///
Packit 345191
void memkind_config_set_memory_usage_policy(struct memkind_config *cfg,
Packit 345191
                                            memkind_mem_usage_policy policy);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Create kind that allocates memory with specific memory type, memory binding policy and flags.
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
/// \note Currently implemented memory type and policy configurations:
Packit 345191
///.      {MEMKIND_MEMTYPE_DEFAULT, MEMKIND_POLICY_PREFERRED_LOCAL},
Packit 345191
///.      {MEMKIND_MEMTYPE_HIGH_BANDWIDTH, MEMKIND_POLICY_BIND_LOCAL},
Packit 345191
///       {MEMKIND_MEMTYPE_HIGH_BANDWIDTH, MEMKIND_POLICY_PREFERRED_LOCAL},
Packit 345191
///       {MEMKIND_MEMTYPE_HIGH_BANDWIDTH, MEMKIND_POLICY_INTERLEAVE_ALL},
Packit 345191
///       {MEMKIND_MEMTYPE_DEFAULT | MEMKIND_MEMTYPE_HIGH_BANDWIDTH, MEMKIND_POLICY_INTERLEAVE_ALL}.
Packit 345191
/// \param memtype_flags determine the memory types to allocate from by combination of memkind_memtype_t values.
Packit 345191
///        This field cannot have zero value.
Packit 345191
/// \param policy specify policy for page binding to memory types selected by  memtype_flags.
Packit 345191
///        This field must be set to memkind_policy_t value. If policy is set to MEMKIND_POLICY_PREFERRED_LOCAL then only one memory
Packit 345191
///        type must be selected. Note: the value cannot be set to MEMKIND_POLICY_MAX_VALUE.
Packit 345191
/// \param flags the field must be set to a combination of memkind_bits_t values.
Packit 345191
/// \param kind pointer to kind which will be created
Packit 345191
/// \return Memkind operation status, MEMKIND_SUCCESS on success, MEMKIND_ERROR_MEMTYPE_NOT_AVAILABLE or MEMKIND_ERROR_INVALID on failure
Packit 345191
///
Packit 345191
int memkind_create_kind(memkind_memtype_t memtype_flags,
Packit 345191
                        memkind_policy_t policy,
Packit 345191
                        memkind_bits_t flags,
Packit 345191
                        memkind_t *kind);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Destroy previously created kind object, which must have been returned
Packit 345191
///        by a call to memkind_create_kind() or memkind_create_pmem().
Packit 345191
///        The function has undefined behavior when the handle is invalid or
Packit 345191
///        memkind_destroy_kind(kind) was already called before
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
/// \note if the kind was returned by memkind_create_kind() all allocated memory must be freed
Packit 345191
///       before kind is destroyed, otherwise this will cause memory leak.
Packit 345191
/// \param kind specified memory kind
Packit 345191
/// \return Memkind operation status, MEMKIND_SUCCESS on success, MEMKIND_ERROR_OPERATION_FAILED on failure
Packit 345191
///
Packit 345191
int memkind_destroy_kind(memkind_t kind);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Get kind associated with allocated memory referenced by ptr
Packit 345191
/// \note STANDARD API
Packit 345191
/// \note This function has non-trivial performance overhead
Packit 345191
/// \param ptr pointer to the allocated memory
Packit 345191
/// \return Kind associated with allocated memory, NULL on failure
Packit 345191
///
Packit 345191
memkind_t memkind_detect_kind(void *ptr);
Packit 345191
Packit 345191
#include "memkind_deprecated.h"
Packit 345191
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
extern memkind_t MEMKIND_REGULAR;
Packit 345191
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
extern memkind_t MEMKIND_DEFAULT;
Packit 345191
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
extern memkind_t MEMKIND_HUGETLB;
Packit 345191
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
extern memkind_t MEMKIND_HBW;
Packit 345191
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
extern memkind_t MEMKIND_HBW_ALL;
Packit 345191
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
extern memkind_t MEMKIND_HBW_PREFERRED;
Packit 345191
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
extern memkind_t MEMKIND_HBW_HUGETLB;
Packit 345191
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
extern memkind_t MEMKIND_HBW_ALL_HUGETLB;
Packit 345191
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
extern memkind_t MEMKIND_HBW_PREFERRED_HUGETLB;
Packit 345191
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
extern memkind_t MEMKIND_HBW_INTERLEAVE;
Packit 345191
Packit 345191
/// \warning EXPERIMENTAL API
Packit 345191
extern memkind_t MEMKIND_INTERLEAVE;
Packit 345191
Packit 345191
/// \note STANDARD API
Packit 345191
extern memkind_t MEMKIND_DAX_KMEM;
Packit 345191
Packit 345191
/// \note STANDARD API
Packit 345191
extern memkind_t MEMKIND_DAX_KMEM_ALL;
Packit 345191
Packit 345191
/// \note STANDARD API
Packit 345191
extern memkind_t MEMKIND_DAX_KMEM_PREFERRED;
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Get Memkind API version
Packit 345191
/// \note STANDARD API
Packit 345191
/// \return Version number represented by a single integer number(major * 1000000 + minor * 1000 + patch)
Packit 345191
///
Packit 345191
int memkind_get_version();
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Convert error number into an error message
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param err error number
Packit 345191
/// \param msg error message
Packit 345191
/// \param size size of message
Packit 345191
///
Packit 345191
void memkind_error_message(int err, char *msg, size_t size);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Create a new PMEM (file-backed) kind of given size on top of a temporary file
Packit 345191
///        in the given directory dir
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param dir path to specified directory to temporary file
Packit 345191
/// \param max_size size limit for kind
Packit 345191
/// \param kind pointer to kind which will be created
Packit 345191
/// \return Memkind operation status, MEMKIND_SUCCESS on success, other values on failure
Packit 345191
///
Packit 345191
int memkind_create_pmem(const char *dir, size_t max_size, memkind_t *kind);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Create a new PMEM kind with given memkind configuration
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param cfg memkind configuration for specifying PMEM parameters
Packit 345191
/// \param kind pointer to kind which will be created
Packit 345191
/// \return Memkind operation status, MEMKIND_SUCCESS on success, other values on failure
Packit 345191
///
Packit 345191
int memkind_create_pmem_with_config(struct memkind_config *cfg,
Packit 345191
                                    memkind_t *kind);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Check if kind is available
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param kind specified memory kind
Packit 345191
/// \return Memkind operation status, MEMKIND_SUCCESS on success, other values on failure
Packit 345191
///
Packit 345191
int memkind_check_available(memkind_t kind);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Update memkind cached statistics
Packit 345191
/// \note STANDARD API
Packit 345191
/// \return Memkind operation status, MEMKIND_SUCCESS on success, other values on failure
Packit 345191
///
Packit 345191
int memkind_update_cached_stats(void);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Get memkind statistic
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param kind specified memory kind
Packit 345191
/// \param stat specified type of memory statistic
Packit 345191
/// \param value reference to value of memory statistic
Packit 345191
/// \return Memkind operation status, MEMKIND_SUCCESS on success, other values on failure
Packit 345191
///
Packit 345191
int memkind_get_stat(memkind_t kind, memkind_stat_type stat, size_t *value);
Packit 345191
Packit 345191
/* HEAP MANAGEMENT INTERFACE */
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Allocates size bytes of uninitialized storage of the specified kind
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param kind specified memory kind
Packit 345191
/// \param size number of bytes to allocate
Packit 345191
/// \return Pointer to the allocated memory
Packit 345191
///
Packit 345191
void *memkind_malloc(memkind_t kind, size_t size);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Obtain size of block of memory allocated with the memkind API
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param kind specified memory kind
Packit 345191
/// \param ptr pointer to the allocated memory
Packit 345191
/// \return Number of usable bytes
Packit 345191
///
Packit 345191
size_t memkind_malloc_usable_size(memkind_t kind, void *ptr);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Allocates memory of the specified kind for an array of num elements
Packit 345191
///        of size bytes each and initializes all bytes in the allocated storage to zero
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param kind specified memory kind
Packit 345191
/// \param num number of objects
Packit 345191
/// \param size specified size of each element
Packit 345191
/// \return Pointer to the allocated memory
Packit 345191
///
Packit 345191
void *memkind_calloc(memkind_t kind, size_t num, size_t size);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Allocates size bytes of the specified kind and places the address of the allocated memory
Packit 345191
///        in *memptr. The address of the allocated memory will be a multiple of alignment,
Packit 345191
///        which must be a power of two and a multiple of sizeof(void *)
Packit 345191
/// \note EXPERIMENTAL API
Packit 345191
/// \param kind specified memory kind
Packit 345191
/// \param memptr address of the allocated memory
Packit 345191
/// \param alignment specified alignment of bytes
Packit 345191
/// \param size specified size of bytes
Packit 345191
/// \return Memkind operation status, MEMKIND_SUCCESS on success, EINVAL or ENOMEM on failure
Packit 345191
///
Packit 345191
int memkind_posix_memalign(memkind_t kind, void **memptr, size_t alignment,
Packit 345191
                           size_t size);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Reallocates memory of the specified kind
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param kind specified memory kind
Packit 345191
/// \param ptr pointer to the memory block to be reallocated
Packit 345191
/// \param size new size for the memory block in bytes
Packit 345191
/// \return Pointer to the allocated memory
Packit 345191
///
Packit 345191
void *memkind_realloc(memkind_t kind, void *ptr, size_t size);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Free the memory space of the specified kind pointed by ptr
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param kind specified memory kind
Packit 345191
/// \param ptr pointer to the allocated memory
Packit 345191
///
Packit 345191
void memkind_free(memkind_t kind, void *ptr);
Packit 345191
Packit 345191
///
Packit 345191
/// \brief Try to reallocate allocation to reduce fragmentation
Packit 345191
/// \note STANDARD API
Packit 345191
/// \param kind specified memory kind
Packit 345191
/// \param ptr pointer to the allocated memory
Packit 345191
/// \return Pointer to newly transferred allocated memory
Packit 345191
///
Packit 345191
void *memkind_defrag_reallocate(memkind_t kind, void *ptr);
Packit 345191
Packit 345191
#ifdef __cplusplus
Packit 345191
}
Packit 345191
#endif