Blame include/hbwmalloc.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 <stdlib.h>
Packit 345191
/*
Packit 345191
 *  Header file for the high bandwidth memory interface.
Packit 345191
 *
Packit 345191
 *  This file defines the external API's and enumerations for the
Packit 345191
 *  hbwmalloc library.  These interfaces define a heap manager that
Packit 345191
 *  targets the high bandwidth memory numa nodes.
Packit 345191
 *
Packit 345191
 *  hbwmalloc.h functionality is considered as stable API (STANDARD API).
Packit 345191
 *
Packit 345191
 *  Please read hbwmalloc(3) man page for or more details.
Packit 345191
 */
Packit 345191
Packit 345191
/*
Packit 345191
 *  Fallback policy.
Packit 345191
 *
Packit 345191
 *  Policy that determines behavior when there is not enough free high
Packit 345191
 *  bandwidth memory to satisfy a user request.  This enum is used with
Packit 345191
 *  hbw_get_policy() and hbw_set_policy().
Packit 345191
 */
Packit 345191
typedef enum {
Packit 345191
    /*
Packit 345191
     *  If insufficient high bandwidth memory pages on nearest NUMA node are
Packit 345191
     *  available then OOM killer will be triggered.
Packit 345191
     */
Packit 345191
    HBW_POLICY_BIND = 1,
Packit 345191
    /*
Packit 345191
     *  If insufficient high bandwidth memory pages are available fall
Packit 345191
     *  back on standard memory pages.
Packit 345191
     */
Packit 345191
    HBW_POLICY_PREFERRED = 2,
Packit 345191
    /*
Packit 345191
     *  Interleave pages across high bandwidth nodes. If insufficient memory
Packit 345191
     *  pages are available then OOM killer will be triggered.
Packit 345191
     */
Packit 345191
    HBW_POLICY_INTERLEAVE = 3,
Packit 345191
    /*
Packit 345191
     *  If insufficient high bandwidth memory pages are available then
Packit 345191
     *  OOM killer will be triggered.
Packit 345191
     */
Packit 345191
    HBW_POLICY_BIND_ALL = 4,
Packit 345191
} hbw_policy_t;
Packit 345191
Packit 345191
/*
Packit 345191
 *  Page size selection.
Packit 345191
 *
Packit 345191
 *  The hbw_posix_memalign_psize() API gives the user the option to
Packit 345191
 *  select the page size from this enumerated list.
Packit 345191
 */
Packit 345191
typedef enum {
Packit 345191
Packit 345191
    /*
Packit 345191
     * The four kilobyte page size option. Note that with transparent huge
Packit 345191
     * pages enabled these allocations may be promoted by the operating system
Packit 345191
     * to two megabyte pages.
Packit 345191
     */
Packit 345191
    HBW_PAGESIZE_4KB           = 1,
Packit 345191
Packit 345191
    /*
Packit 345191
     * The two megabyte page size option.
Packit 345191
     */
Packit 345191
    HBW_PAGESIZE_2MB           = 2,
Packit 345191
    /*
Packit 345191
     * This option is deprecated.
Packit 345191
     * Allocate high bandwidth memory using 1GB chunks backed by huge pages.
Packit 345191
     */
Packit 345191
    HBW_PAGESIZE_1GB_STRICT    = 3,
Packit 345191
Packit 345191
    /*
Packit 345191
     * This option is deprecated.
Packit 345191
     * Allocate high bandwidth memory using 1GB chunks backed by huge pages.
Packit 345191
     */
Packit 345191
    HBW_PAGESIZE_1GB           = 4,
Packit 345191
Packit 345191
    /*
Packit 345191
     * Helper representing value of the last enum element incremented by 1.
Packit 345191
     * Shall not be treated as a valid value for functions taking hbw_pagesize_t
Packit 345191
     * as parameter.
Packit 345191
     */
Packit 345191
    HBW_PAGESIZE_MAX_VALUE
Packit 345191
} hbw_pagesize_t;
Packit 345191
Packit 345191
/*
Packit 345191
 * Flags for hbw_verify_ptr function
Packit 345191
 */
Packit 345191
enum {
Packit 345191
Packit 345191
    /*
Packit 345191
     * This option touches first byte of all pages in address range starting from "addr" to "addr" + "size"
Packit 345191
     * by read and write (so the content will be overwritten by the same data as it was read).
Packit 345191
     */
Packit 345191
    HBW_TOUCH_PAGES      = (1 << 0)
Packit 345191
};
Packit 345191
Packit 345191
/*
Packit 345191
 * Returns the current fallback policy when insufficient high bandwidth memory
Packit 345191
 * is available.
Packit 345191
 */
Packit 345191
hbw_policy_t hbw_get_policy(void);
Packit 345191
Packit 345191
/*
Packit 345191
 * Sets the current fallback policy. The policy can be modified only once in
Packit 345191
 * the lifetime of an application and before calling hbw_*alloc() or
Packit 345191
 * hbw_posix_memalign*() function.
Packit 345191
 * Note: If the policy is not set, than HBW_POLICY_PREFERRED will be used by
Packit 345191
 * default.
Packit 345191
 *
Packit 345191
 * Returns:
Packit 345191
 *   0: on success
Packit 345191
 *   EPERM: if hbw_set_policy () was called more than once
Packit 345191
 *   EINVAL: if mode argument was neither HBW_POLICY_PREFERRED, HBW_POLICY_BIND, HBW_POLICY_BIND_ALL nor HBW_POLICY_INTERLEAVE
Packit 345191
 */
Packit 345191
int hbw_set_policy(hbw_policy_t mode);
Packit 345191
Packit 345191
/*
Packit 345191
 * Verifies high bandwidth memory availability.
Packit 345191
 * Returns:
Packit 345191
 *   0: if high bandwidth memory is available
Packit 345191
 *   ENODEV: if high-bandwidth memory is unavailable.
Packit 345191
 */
Packit 345191
int hbw_check_available(void);
Packit 345191
Packit 345191
/*
Packit 345191
 * Verifies if allocated memory fully fall into high bandwidth memory.
Packit 345191
 * Returns:
Packit 345191
 *   0: if memory in address range from "addr" to "addr" + "size" is allocated in high bandwidth memory
Packit 345191
 *   -1: if any region of memory was not allocated in high bandwidth memory
Packit 345191
 *   EINVAL: if addr is NULL, size equals 0 or flags contained unsupported bit set
Packit 345191
 *   EFAULT: could not verify memory
Packit 345191
 */
Packit 345191
int hbw_verify_memory_region(void *addr, size_t size, int flags);
Packit 345191
Packit 345191
/*
Packit 345191
 * Allocates size bytes of uninitialized high bandwidth memory.
Packit 345191
 * The allocated space is  suitably  aligned (after  possible  pointer
Packit 345191
 * coercion) for storage of any type of object. If size is zero then
Packit 345191
 * hbw_malloc() returns NULL.
Packit 345191
 */
Packit 345191
void *hbw_malloc(size_t size);
Packit 345191
Packit 345191
/*
Packit 345191
 * Allocates space for num objects in high bandwidth memory, each size bytes
Packit 345191
 * in length.
Packit 345191
 * The result is identical to calling hbw_malloc() with an argument of
Packit 345191
 * num*size, with the exception that the allocated memory is explicitly
Packit 345191
 * initialized to zero bytes.
Packit 345191
 * If num or size is 0, then hbw_calloc() returns NULL.
Packit 345191
 */
Packit 345191
void *hbw_calloc(size_t num, size_t size);
Packit 345191
Packit 345191
/*
Packit 345191
 * Allocates size bytes of high bandwidth memory such that the allocation's
Packit 345191
 * base address is an even multiple of alignment, and returns the allocation
Packit 345191
 * in the value pointed to by memptr.  The requested alignment must be a power
Packit 345191
 * of 2 at least as large as sizeof(void *).
Packit 345191
 * Returns:
Packit 345191
 *   0: on success
Packit 345191
 *   ENOMEM: if there was insufficient memory to satisfy the request
Packit 345191
 *   EINVAL: if the alignment parameter was not a power of two, or was less than sizeof(void *)
Packit 345191
 */
Packit 345191
int hbw_posix_memalign(void **memptr, size_t alignment, size_t size);
Packit 345191
Packit 345191
/*
Packit 345191
 * Allocates size bytes of high bandwidth memory such that the allocation's
Packit 345191
 * base address is an even multiple of alignment, and returns the allocation
Packit 345191
 * in the value pointed to by memptr. The requested alignment must be a power
Packit 345191
 * of 2 at least as large as sizeof(void  *). The memory will be allocated
Packit 345191
 * using pages determined by the pagesize variable.
Packit 345191
 * Returns:
Packit 345191
 *   0: on success
Packit 345191
 *   ENOMEM: if there was insufficient memory to satisfy the request
Packit 345191
 *   EINVAL: if the alignment parameter was not a power of two, or was less than sizeof(void *)
Packit 345191
 */
Packit 345191
int hbw_posix_memalign_psize(void **memptr, size_t alignment, size_t size,
Packit 345191
                             hbw_pagesize_t pagesize);
Packit 345191
Packit 345191
/*
Packit 345191
 * Changes the size of the previously allocated memory referenced by ptr to
Packit 345191
 * size bytes of the specified kind. The contents of the memory are unchanged
Packit 345191
 * up to the lesser of the new and old size.
Packit 345191
 * If the new size is larger, the contents of the newly allocated portion
Packit 345191
 * of the memory are undefined.
Packit 345191
 * Upon success, the memory referenced by ptr is freed and a pointer to the
Packit 345191
 * newly allocated high bandwidth memory is returned.
Packit 345191
 * Note: memkind_realloc() may move the memory allocation, resulting in a
Packit 345191
 * different return value than ptr.
Packit 345191
 * If ptr is NULL, the hbw_realloc() function behaves identically to
Packit 345191
 * hbw_malloc() for the specified size. The address ptr, if not NULL,
Packit 345191
 * was returned by a previous call to hbw_malloc(), hbw_calloc(),
Packit 345191
 * hbw_realloc(), or hbw_posix_memalign(). Otherwise, or if hbw_free(ptr)
Packit 345191
 * was called before, undefined behavior occurs.
Packit 345191
 * Note: hbw_realloc() cannot be used with a pointer returned by
Packit 345191
 * hbw_posix_memalign_psize().
Packit 345191
 */
Packit 345191
void *hbw_realloc(void *ptr, size_t size);
Packit 345191
Packit 345191
/*
Packit 345191
 * Causes the allocated memory referenced by ptr to be made
Packit 345191
 * available for future allocations. If ptr is NULL, no action occurs.
Packit 345191
 * The address ptr, if not NULL, must have been returned by a previous call
Packit 345191
 * to hbw_malloc(), hbw_calloc(), hbw_realloc(), hbw_posix_memalign(), or
Packit 345191
 * hbw_posix_memalign_psize(). Otherwise, if hbw_free(ptr) was called before,
Packit 345191
 * undefined behavior occurs.
Packit 345191
 */
Packit 345191
void hbw_free(void *ptr);
Packit 345191
Packit 345191
/*
Packit 345191
 * Returns the number of usable bytes in the block pointed to by ptr, a pointer
Packit 345191
 * to a block of memory allocated by hbw_malloc(), hbw_calloc(), hbw_realloc(),
Packit 345191
 * hbw_posix_memalign(), or hbw_posix_memalign_psize().
Packit 345191
 */
Packit 345191
size_t hbw_malloc_usable_size(void *ptr);
Packit 345191
Packit 345191
#ifdef __cplusplus
Packit 345191
}
Packit 345191
#endif