Blame src/memkind-auto-dax-kmem-nodes.c

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_dax_kmem.h>
Packit 345191
#include <memkind.h>
Packit 345191
#include <numa.h>
Packit 345191
#include <stdio.h>
Packit 345191
Packit 345191
#define MAX_ARG_LEN 8
Packit 345191
Packit 345191
static const char *const help_message =
Packit 345191
    "\n"
Packit 345191
    "NAME\n"
Packit 345191
    "    memkind-auto-dax-kmem-nodes - prints comma-separated list of automatically detected persistent memory NUMA nodes.\n"
Packit 345191
    "\n"
Packit 345191
    "SYNOPSIS\n"
Packit 345191
    "    memkind-auto-dax-kmem-nodes -h | --help\n"
Packit 345191
    "        Prints this help message.\n"
Packit 345191
    "\n"
Packit 345191
    "DESCRIPTION\n"
Packit 345191
    "    Prints a comma-separated list of automatically detected persistent memory NUMA nodes\n"
Packit 345191
    "    that can be used with the numactl --membind option.\n"
Packit 345191
    "\n"
Packit 345191
    "EXIT STATUS\n"
Packit 345191
    "    Return code is :\n"
Packit 345191
    "        0 on success\n"
Packit 345191
    "        1 if automatic detection is disabled (memkind wasn't build with suitable libdaxctl-devel version)\n"
Packit 345191
    "        2 on invalid argument\n"
Packit 345191
    "        3 on other failure\n"
Packit 345191
    "\n"
Packit 345191
    "COPYRIGHT\n"
Packit 345191
    "    Copyright 2019 Intel Corporation All Rights Reserved.\n"
Packit 345191
    "\n"
Packit 345191
    "AUTHORS\n"
Packit 345191
    "    Michal Biesek\n"
Packit 345191
    "\n"
Packit 345191
    "SEE ALSO\n"
Packit 345191
    "    memkind(3)\n"
Packit 345191
    "\n";
Packit 345191
Packit 345191
Packit 345191
static int print_dax_kmem_nodes()
Packit 345191
{
Packit 345191
    unsigned i, j = 0;
Packit 345191
Packit 345191
    nodemask_t nodemask;
Packit 345191
    struct bitmask nodemask_dax_kmem = {NUMA_NUM_NODES, nodemask.n};
Packit 345191
Packit 345191
    // ensuring functions in numa library are defined
Packit 345191
    if (numa_available() == -1) {
Packit 345191
        return 3;
Packit 345191
    }
Packit 345191
    numa_bitmask_clearall(&nodemask_dax_kmem);
Packit 345191
Packit 345191
    //WARNING: code below is usage of memkind experimental API which may be changed in future
Packit 345191
    int status = memkind_dax_kmem_all_get_mbind_nodemask(NULL, nodemask.n,
Packit 345191
                                                         NUMA_NUM_NODES);
Packit 345191
Packit 345191
    if (status == MEMKIND_ERROR_OPERATION_FAILED ) {
Packit 345191
        return 1;
Packit 345191
    } else if (status != MEMKIND_SUCCESS ) {
Packit 345191
        return 3;
Packit 345191
    }
Packit 345191
Packit 345191
    for(i = 0; i
Packit 345191
        if(numa_bitmask_isbitset(&nodemask_dax_kmem, i)) {
Packit 345191
            printf("%u%s", i, (++j == numa_bitmask_weight(&nodemask_dax_kmem)) ? "" : ",");
Packit 345191
        }
Packit 345191
    }
Packit 345191
    printf("\n");
Packit 345191
    return 0;
Packit 345191
}
Packit 345191
Packit 345191
int main(int argc, char *argv[])
Packit 345191
{
Packit 345191
    if(argc == 1) {
Packit 345191
        return print_dax_kmem_nodes();
Packit 345191
    } else if ((argc == 2) && (strncmp(argv[1], "-h", MAX_ARG_LEN) == 0 ||
Packit 345191
                               strncmp(argv[1], "--help", MAX_ARG_LEN) == 0)) {
Packit 345191
        printf("%s", help_message);
Packit 345191
        return 0;
Packit 345191
    }
Packit 345191
Packit 345191
    printf("ERROR: Unknown option %s. More info with \"%s --help\".\n", argv[1],
Packit 345191
           argv[0]);
Packit 345191
    return 2;
Packit 345191
}