<commit_before>/**
 * Copyright (c) UT-Battelle, LLC. 2014-2015. ALL RIGHTS RESERVED.
 * Copyright (C) Mellanox Technologies Ltd. 2001-2015.  ALL RIGHTS RESERVED.
 * See file LICENSE for terms.
 */

#include "mm_pd.h"
#include "mm_iface.h"

#include <ucs/debug/memtrack.h>
#include <ucs/debug/log.h>
#include <sys/mman.h>
#include <ucs/sys/sys.h>

#define UCT_MM_POSIX_SHM_OPEN_MODE  (0666)
#define UCT_MM_POSIX_MMAP_PROT      (PROT_READ | PROT_WRITE)
#define UCT_MM_POSIX_HUGETLB        UCS_BIT(0)

typedef struct uct_posix_pd_config {
    uct_mm_pd_config_t      super;
    char                    *path;
} uct_posix_pd_config_t;

static ucs_config_field_t uct_posix_pd_config_table[] = {
  {"MM_", "", NULL,
   ucs_offsetof(uct_posix_pd_config_t, super), UCS_CONFIG_TYPE_TABLE(uct_mm_pd_config_table)},

  {"DIR", "/dev/shm", "The path to the backing file",
   ucs_offsetof(uct_posix_pd_config_t, path), UCS_CONFIG_TYPE_STRING},

  {NULL}
};

static ucs_status_t uct_posix_test_mem(size_t length, int shm_fd)
{
    int *buf;
    int chunk_size = 256 * 1024;
    ucs_status_t status = UCS_OK;
    size_t size_to_write, remaining, single_write, total_write = 0;

    buf = ucs_malloc(chunk_size, "write buffer");
    if (buf == NULL) {
        ucs_error("Failed to allocate memory for testing space for backing file.");
        status = UCS_ERR_NO_MEMORY;
        goto out;
    }

    memset(buf, 0, chunk_size);
    if (lseek(shm_fd, 0, SEEK_SET) < 0) {
        status = UCS_ERR_IO_ERROR;
        goto out_free_buf;
    }

    remaining = length;
    while (remaining > 0) {
        size_to_write = ucs_min(remaining, chunk_size);
        single_write = write(shm_fd, buf, size_to_write);
        if (single_write < size_to_write) {
            ucs_error("Failed to write %zu bytes. %m", size_to_write);
            goto out_check;
        } else {
            total_write += single_write;
        }
        remaining -= size_to_write;
    }

out_check:
    if (length != total_write) {
        ucs_error("Not enough memory to write %zu bytes. "
                  "Please check that /dev/shm or the directory you specified has "
                  "more available memory.", length);
        status = UCS_ERR_NO_MEMORY;
    }

out_free_buf:
    ucs_free(buf);

out:
    return status;
}

static ucs_status_t
uct_posix_alloc(uct_pd_h pd, size_t *length_p, ucs_ternary_value_t hugetlb,
                void **address_p, uct_mm_id_t *mmid_p UCS_MEMTRACK_ARG)
{
    ucs_status_t status;
    int shm_fd;
    uint64_t uuid;
    char *file_name;

    if (0 == *length_p) {
        ucs_error("Unexpected length %zu", *length_p);
        status = UCS_ERR_INVALID_PARAM;
        goto err;
    }

    file_name = ucs_malloc(NAME_MAX, "shared mr posix");
    if (file_name == NULL) {
        status = UCS_ERR_NO_MEMORY;
        ucs_error("Failed to allocate memory for the shm_open file name. %m");
        goto err;
    }

    uuid = ucs_generate_uuid(0);
    /* use 63 bits of the uuid for creating the file_name.
     * 1 bit is for indicating whether or not hugepages were used */
    sprintf(file_name, "/ucx_shared_mr_uuid_%zu", uuid >> 1);

    /* Create shared memory object and set its size */
    shm_fd = shm_open(file_name, O_CREAT | O_RDWR | O_EXCL,
                      UCT_MM_POSIX_SHM_OPEN_MODE);
    if (shm_fd == -1) {
        ucs_error("Error returned from shm_open %m. File name is: %s",
                  file_name);
        status = UCS_ERR_SHMEM_SEGMENT;
        goto err_free_file;
    }

    if (ftruncate(shm_fd, *length_p) == -1) {
        ucs_error("Error returned from ftruncate %m");
        status = UCS_ERR_SHMEM_SEGMENT;
        goto err_shm_unlink;
    }

    status = uct_posix_test_mem(*length_p, shm_fd);
    if (status != UCS_OK) {<commit_msg>MM/POSIX: apply more CR comments.<commit_after>/**
 * Copyright (c) UT-Battelle, LLC. 2014-2015. ALL RIGHTS RESERVED.
 * Copyright (C) Mellanox Technologies Ltd. 2001-2015.  ALL RIGHTS RESERVED.
 * See file LICENSE for terms.
 */

#include "mm_pd.h"
#include "mm_iface.h"

#include <ucs/debug/memtrack.h>
#include <ucs/debug/log.h>
#include <sys/mman.h>
#include <ucs/sys/sys.h>

#define UCT_MM_POSIX_SHM_OPEN_MODE  (0666)
#define UCT_MM_POSIX_MMAP_PROT      (PROT_READ | PROT_WRITE)
#define UCT_MM_POSIX_HUGETLB        UCS_BIT(0)

typedef struct uct_posix_pd_config {
    uct_mm_pd_config_t      super;
    char                    *path;
} uct_posix_pd_config_t;

static ucs_config_field_t uct_posix_pd_config_table[] = {
  {"MM_", "", NULL,
   ucs_offsetof(uct_posix_pd_config_t, super), UCS_CONFIG_TYPE_TABLE(uct_mm_pd_config_table)},

  {"DIR", "/dev/shm", "The path to the backing file",
   ucs_offsetof(uct_posix_pd_config_t, path), UCS_CONFIG_TYPE_STRING},

  {NULL}
};

static ucs_status_t uct_posix_test_mem(size_t length, int shm_fd)
{
    int *buf;
    int chunk_size = 256 * 1024;
    ucs_status_t status = UCS_OK;
    size_t size_to_write, remaining;
    ssize_t single_write;

    buf = ucs_malloc(chunk_size, "write buffer");
    if (buf == NULL) {
        ucs_error("Failed to allocate memory for testing space for backing file.");
        status = UCS_ERR_NO_MEMORY;
        goto out;
    }

    memset(buf, 0, chunk_size);
    if (lseek(shm_fd, 0, SEEK_SET) < 0) {
        ucs_error("lseek failed. %m");
        status = UCS_ERR_IO_ERROR;
        goto out_free_buf;
    }

    remaining = length;
    while (remaining > 0) {
        size_to_write = ucs_min(remaining, chunk_size);
        single_write = write(shm_fd, buf, size_to_write);
        if (single_write < 0) {
            ucs_error("Failed to write %zu bytes. %m", size_to_write);
            status = UCS_ERR_IO_ERROR;
            goto out_free_buf;
        }
        if (single_write != size_to_write) {
            ucs_error("Not enough memory to write total of %zu bytes. "
                      "Please check that /dev/shm or the directory you specified has "
                      "more available memory.", length);
            status = UCS_ERR_NO_MEMORY;
            goto out_free_buf;
        }
        remaining -= size_to_write;
    }

out_free_buf:
    ucs_free(buf);

out:
    return status;
}

static ucs_status_t
uct_posix_alloc(uct_pd_h pd, size_t *length_p, ucs_ternary_value_t hugetlb,
                void **address_p, uct_mm_id_t *mmid_p UCS_MEMTRACK_ARG)
{
    ucs_status_t status;
    int shm_fd;
    uint64_t uuid;
    char *file_name;

    if (0 == *length_p) {
        ucs_error("Unexpected length %zu", *length_p);
        status = UCS_ERR_INVALID_PARAM;
        goto err;
    }

    file_name = ucs_malloc(NAME_MAX, "shared mr posix");
    if (file_name == NULL) {
        status = UCS_ERR_NO_MEMORY;
        ucs_error("Failed to allocate memory for the shm_open file name. %m");
        goto err;
    }

    uuid = ucs_generate_uuid(0);
    /* use 63 bits of the uuid for creating the file_name.
     * 1 bit is for indicating whether or not hugepages were used */
    sprintf(file_name, "/ucx_shared_mr_uuid_%zu", uuid >> 1);

    /* Create shared memory object and set its size */
    shm_fd = shm_open(file_name, O_CREAT | O_RDWR | O_EXCL,
                      UCT_MM_POSIX_SHM_OPEN_MODE);
    if (shm_fd == -1) {
        ucs_error("Error returned from shm_open %m. File name is: %s",
                  file_name);
        status = UCS_ERR_SHMEM_SEGMENT;
        goto err_free_file;
    }

    if (ftruncate(shm_fd, *length_p) == -1) {
        ucs_error("Error returned from ftruncate %m");
        status = UCS_ERR_SHMEM_SEGMENT;
        goto err_shm_unlink;
    }

    /* check is the location of the backing file has enough memory for the needed size
     * by trying to write there before calling mmap */
    status = uct_posix_test_mem(*length_p, shm_fd);
    if (status != UCS_OK) {
{'fraction_non_alphanumeric': 0.09989701338825953, 'fraction_numerical': 0.011071060762100926, 'mean_word_length': 2.7190043082814745, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5596985', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2850, 'n_words': 669}