Blame test/alloc_benchmark.c

Packit 345191
/*
Packit 345191
 * Copyright (C) 2016 - 2020 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 <stdio.h>
Packit 345191
#include <stdlib.h>
Packit 345191
#include <time.h>
Packit 345191
#include <sys/time.h>
Packit 345191
#include <unistd.h>
Packit 345191
#include <stdint.h>
Packit 345191
#include <limits.h>
Packit 345191
#ifdef _OPENMP
Packit 345191
#include <omp.h>
Packit 345191
#endif
Packit 345191
#if defined(HBWMALLOC)
Packit 345191
#include <hbwmalloc.h>
Packit 345191
#define MALLOC_FN hbw_malloc
Packit 345191
#define FREE_FN hbw_free
Packit 345191
#elif defined (TBBMALLOC)
Packit 345191
#include "tbbmalloc.h"
Packit 345191
void *(*scalable_malloc)(size_t);
Packit 345191
void *(*scalable_realloc)(void *, size_t);
Packit 345191
void *(*scalable_calloc)(size_t, size_t);
Packit 345191
void  (*scalable_free)(void *);
Packit 345191
#define MALLOC_FN scalable_malloc
Packit 345191
#define FREE_FN scalable_free
Packit 345191
#elif defined (PMEMMALLOC)
Packit 345191
#include <sys/stat.h>
Packit 345191
#include "memkind.h"
Packit 345191
#define MALLOC_FN(x) memkind_malloc(pmem_bench_kind, (x))
Packit 345191
#define FREE_FN(x) memkind_free(pmem_bench_kind, (x))
Packit 345191
Packit 345191
static const size_t PMEM_PART_SIZE = 0;
Packit 345191
static const char *PMEM_DIR = "/tmp/";
Packit 345191
static memkind_t pmem_bench_kind;
Packit 345191
#else
Packit 345191
#define MALLOC_FN malloc
Packit 345191
#define FREE_FN free
Packit 345191
#endif
Packit 345191
Packit 345191
double ctimer(void);
Packit 345191
void usage(char *name);
Packit 345191
Packit 345191
int main(int argc, char *argv[])
Packit 345191
{
Packit 345191
#ifdef _OPENMP
Packit 345191
    int nthr = omp_get_max_threads();
Packit 345191
#else
Packit 345191
    int nthr = 1;
Packit 345191
#endif
Packit 345191
    long n, size;
Packit 345191
    size_t alloc_size;
Packit 345191
    unsigned long i;
Packit 345191
    double dt, t_start, t_end, t_malloc, t_free, t_first_malloc, t_first_free,
Packit 345191
           malloc_time = 0.0, free_time = 0.0, first_malloc_time, first_free_time;
Packit 345191
    void *ptr;
Packit 345191
#ifdef TBBMALLOC
Packit 345191
    int ret;
Packit 345191
Packit 345191
    ret = load_tbbmalloc_symbols();
Packit 345191
    if (ret) {
Packit 345191
        printf("Error: TBB symbols not loaded (ret: %d)\n", ret);
Packit 345191
        return EXIT_FAILURE;
Packit 345191
    }
Packit 345191
#endif
Packit 345191
#ifdef PMEMMALLOC
Packit 345191
    struct stat st;
Packit 345191
Packit 345191
    /* Handle command line arguments */
Packit 345191
    if (argc == 3 || argc == 4) {
Packit 345191
        n = atol(argv[1]);
Packit 345191
        size = atol(argv[2]);
Packit 345191
Packit 345191
        if (argc == 4) {
Packit 345191
            if (stat(argv[3], &st) != 0 || !S_ISDIR(st.st_mode)) {
Packit 345191
                usage(argv[0]);
Packit 345191
                return EXIT_FAILURE;
Packit 345191
            } else {
Packit 345191
                PMEM_DIR = argv[3];
Packit 345191
            }
Packit 345191
        }
Packit 345191
    }
Packit 345191
    if ((argc != 3 && argc != 4) || n < 0 || size < 0 || size > (LONG_MAX >> 10)) {
Packit 345191
        usage(argv[0]);
Packit 345191
        return EXIT_FAILURE;
Packit 345191
    }
Packit 345191
Packit 345191
    int err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &pmem_bench_kind);
Packit 345191
    if (err) {
Packit 345191
        printf("Error: memkind_create_pmem failed %d\n", err);
Packit 345191
        return EXIT_FAILURE;
Packit 345191
    }
Packit 345191
#else
Packit 345191
    /* Handle command line arguments */
Packit 345191
    if (argc == 3) {
Packit 345191
        n = atol(argv[1]);
Packit 345191
        size = atol(argv[2]);
Packit 345191
    }
Packit 345191
    if (argc != 3 || n < 0 || size < 0 || size > (LONG_MAX >> 10)) {
Packit 345191
        usage(argv[0]);
Packit 345191
        return EXIT_FAILURE;
Packit 345191
    }
Packit 345191
#endif
Packit 345191
    alloc_size = (size_t) size * 1024;
Packit 345191
Packit 345191
    /* Get pagesize and compute page_mask */
Packit 345191
    const size_t page_size = sysconf(_SC_PAGESIZE);
Packit 345191
    const size_t page_mask = ~(page_size-1);
Packit 345191
Packit 345191
    /* Warm up */
Packit 345191
    t_first_malloc = ctimer();
Packit 345191
    ptr = MALLOC_FN(alloc_size);
Packit 345191
    first_malloc_time = ctimer() - t_first_malloc;
Packit 345191
    if (ptr == NULL) {
Packit 345191
        printf("Error: first allocation failed\n");
Packit 345191
        return EXIT_FAILURE;
Packit 345191
    }
Packit 345191
    t_first_free = ctimer();
Packit 345191
    FREE_FN(ptr);
Packit 345191
    first_free_time = ctimer() - t_first_free;
Packit 345191
    ptr = NULL;
Packit 345191
Packit 345191
    t_start = ctimer();
Packit 345191
    #pragma omp parallel private(i,t_malloc,t_free,ptr) reduction(max:malloc_time,free_time)
Packit 345191
    {
Packit 345191
        malloc_time = 0.0;
Packit 345191
        free_time = 0.0;
Packit 345191
        for (i=0; i
Packit 345191
            t_malloc = ctimer();
Packit 345191
            ptr = (void *) MALLOC_FN(alloc_size);
Packit 345191
            malloc_time += ctimer() - t_malloc;
Packit 345191
            #pragma omp critical
Packit 345191
            {
Packit 345191
                if (ptr == NULL) {
Packit 345191
                    printf("Error: allocation failed\n");
Packit 345191
                    exit(EXIT_FAILURE);
Packit 345191
                }
Packit 345191
            }
Packit 345191
Packit 345191
            /* Make sure to touch every page */
Packit 345191
            char *end = ptr + alloc_size;
Packit 345191
            char *aligned_beg = (char *)((uintptr_t)ptr & page_mask);
Packit 345191
            while(aligned_beg < end) {
Packit 345191
                char *temp_ptr = (char *) aligned_beg;
Packit 345191
                char value = temp_ptr[0];
Packit 345191
                temp_ptr[0] = value;
Packit 345191
                aligned_beg += page_size;
Packit 345191
            }
Packit 345191
Packit 345191
            t_free = ctimer();
Packit 345191
            FREE_FN(ptr);
Packit 345191
            free_time += ctimer() - t_free;
Packit 345191
            ptr = NULL;
Packit 345191
        }
Packit 345191
    }
Packit 345191
    t_end = ctimer();
Packit 345191
    dt = t_end - t_start;
Packit 345191
Packit 345191
    printf("%d %lu %8.6f %8.6f  %8.6f  %8.6f  %8.6f\n",
Packit 345191
           nthr, size, dt/n, malloc_time/n, free_time/n, first_malloc_time,
Packit 345191
           first_free_time);
Packit 345191
#ifdef PMEMMALLOC
Packit 345191
    err = memkind_destroy_kind(pmem_bench_kind);
Packit 345191
    if (err) {
Packit 345191
        printf("Error: memkind_destroy_kind failed %d\n", err);
Packit 345191
        return EXIT_FAILURE;
Packit 345191
    }
Packit 345191
#endif
Packit 345191
    return EXIT_SUCCESS;
Packit 345191
}
Packit 345191
Packit 345191
void usage(char *name)
Packit 345191
{
Packit 345191
#ifdef PMEMMALLOC
Packit 345191
    printf("Usage: %s <N> <SIZE> [DIR], where \n"
Packit 345191
           "N is an number of repetitions \n"
Packit 345191
           "SIZE is an allocation size in kbytes\n"
Packit 345191
           "DIR is a custom path for PMEM kind, (default: \"/tmp/\")\n",
Packit 345191
           name);
Packit 345191
#else
Packit 345191
    printf("Usage: %s <N> <SIZE>, where \n"
Packit 345191
           "N is an number of repetitions \n"
Packit 345191
           "SIZE is an allocation size in kbytes\n", name);
Packit 345191
#endif
Packit 345191
}
Packit 345191
Packit 345191
inline double ctimer()
Packit 345191
{
Packit 345191
    struct timeval tmr;
Packit 345191
    gettimeofday(&tmr, NULL);
Packit 345191
    /* Return time in ms */
Packit 345191
    return (tmr.tv_sec + tmr.tv_usec/1000000.0)*1000;
Packit 345191
}