File: alloc_benchmark.c

package info (click to toggle)
memkind 1.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,508 kB
  • sloc: ansic: 72,572; cpp: 39,493; sh: 4,594; perl: 4,250; xml: 2,044; python: 1,753; makefile: 1,393; csh: 7
file content (195 lines) | stat: -rw-r--r-- 5,380 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// SPDX-License-Identifier: BSD-2-Clause
/* Copyright (C) 2016 - 2021 Intel Corporation. */

#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#ifdef _OPENMP
#include <omp.h>
#endif
#if defined(HBWMALLOC)
#include <hbwmalloc.h>
#define MALLOC_FN hbw_malloc
#define FREE_FN   hbw_free
#elif defined(TBBMALLOC)
#include "tbbmalloc.h"
void *(*scalable_malloc)(size_t);
void *(*scalable_realloc)(void *, size_t);
void *(*scalable_calloc)(size_t, size_t);
void (*scalable_free)(void *);
#define MALLOC_FN scalable_malloc
#define FREE_FN   scalable_free
#elif defined(PMEMMALLOC)
#include "memkind.h"
#include <sys/stat.h>
#define MALLOC_FN(x) memkind_malloc(pmem_bench_kind, (x))
#define FREE_FN(x)   memkind_free(pmem_bench_kind, (x))

static const size_t PMEM_PART_SIZE = 0;
static const char *PMEM_DIR = "/tmp/";
static memkind_t pmem_bench_kind;
#else
#define MALLOC_FN malloc
#define FREE_FN   free
#endif

double ctimer(void);
void usage(char *name);

int main(int argc, char *argv[])
{
#ifdef _OPENMP
    int nthr = omp_get_max_threads();
#else
    int nthr = 1;
#endif
    long n, size;
    size_t alloc_size;
    unsigned long i;
    double dt, t_start, t_end, t_malloc, t_free, t_first_malloc, t_first_free,
        malloc_time = 0.0, free_time = 0.0, first_malloc_time, first_free_time;
    void *ptr;
#ifdef TBBMALLOC
    int ret;

    ret = load_tbbmalloc_symbols();
    if (ret) {
        printf("Error: TBB symbols not loaded (ret: %d)\n", ret);
        return EXIT_FAILURE;
    }
#endif
#ifdef PMEMMALLOC
    struct stat st;

    /* Handle command line arguments */
    if (argc == 3 || argc == 4) {
        n = atol(argv[1]);
        size = atol(argv[2]);

        if (argc == 4) {
            if (stat(argv[3], &st) != 0 || !S_ISDIR(st.st_mode)) {
                usage(argv[0]);
                return EXIT_FAILURE;
            } else {
                PMEM_DIR = argv[3];
            }
        }
    }
    if ((argc != 3 && argc != 4) || n < 0 || size < 0 ||
        size > (LONG_MAX >> 10)) {
        usage(argv[0]);
        return EXIT_FAILURE;
    }

    int err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &pmem_bench_kind);
    if (err) {
        printf("Error: memkind_create_pmem failed %d\n", err);
        return EXIT_FAILURE;
    }
#else
    /* Handle command line arguments */
    if (argc == 3) {
        n = atol(argv[1]);
        size = atol(argv[2]);
    }
    if (argc != 3 || n < 0 || size < 0 || size > (LONG_MAX >> 10)) {
        usage(argv[0]);
        return EXIT_FAILURE;
    }
#endif
    alloc_size = (size_t)size * 1024;

    /* Get pagesize and compute page_mask */
    const size_t page_size = sysconf(_SC_PAGESIZE);
    const size_t page_mask = ~(page_size - 1);

    /* Warm up */
    t_first_malloc = ctimer();
    ptr = MALLOC_FN(alloc_size);
    first_malloc_time = ctimer() - t_first_malloc;
    if (ptr == NULL) {
        printf("Error: first allocation failed\n");
        return EXIT_FAILURE;
    }
    t_first_free = ctimer();
    FREE_FN(ptr);
    first_free_time = ctimer() - t_first_free;
    ptr = NULL;

    t_start = ctimer();
#pragma omp parallel private(i, t_malloc, t_free, ptr)                         \
    reduction(max                                                              \
              : malloc_time, free_time)
    {
        malloc_time = 0.0;
        free_time = 0.0;
        for (i = 0; i < n; i++) {
            t_malloc = ctimer();
            ptr = (void *)MALLOC_FN(alloc_size);
            malloc_time += ctimer() - t_malloc;
#pragma omp critical
            {
                if (ptr == NULL) {
                    printf("Error: allocation failed\n");
                    exit(EXIT_FAILURE);
                }
            }

            /* Make sure to touch every page */
            char *end = ptr + alloc_size;
            char *aligned_beg = (char *)((uintptr_t)ptr & page_mask);
            while (aligned_beg < end) {
                char *temp_ptr = (char *)aligned_beg;
                char value = temp_ptr[0];
                temp_ptr[0] = value;
                aligned_beg += page_size;
            }

            t_free = ctimer();
            FREE_FN(ptr);
            free_time += ctimer() - t_free;
            ptr = NULL;
        }
    }
    t_end = ctimer();
    dt = t_end - t_start;

    printf("%d %lu %8.6f %8.6f  %8.6f  %8.6f  %8.6f\n", nthr, size, dt / n,
           malloc_time / n, free_time / n, first_malloc_time, first_free_time);
#ifdef PMEMMALLOC
    err = memkind_destroy_kind(pmem_bench_kind);
    if (err) {
        printf("Error: memkind_destroy_kind failed %d\n", err);
        return EXIT_FAILURE;
    }
#endif
    return EXIT_SUCCESS;
}

void usage(char *name)
{
#ifdef PMEMMALLOC
    printf("Usage: %s <N> <SIZE> [DIR], where \n"
           "N is an number of repetitions \n"
           "SIZE is an allocation size in kbytes\n"
           "DIR is a custom path for PMEM kind, (default: \"/tmp/\")\n",
           name);
#else
    printf("Usage: %s <N> <SIZE>, where \n"
           "N is an number of repetitions \n"
           "SIZE is an allocation size in kbytes\n",
           name);
#endif
}

inline double ctimer()
{
    struct timeval tmr;
    gettimeofday(&tmr, NULL);
    /* Return time in ms */
    return (tmr.tv_sec + tmr.tv_usec / 1000000.0) * 1000;
}