pmdk-bw.c 8.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2019 UChicago Argonne, LLC
 *
 * See COPYRIGHT in top-level directory.
 */

/* Effective bandwidth test to a single local pmdk pool using pmemobj */

#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>

#include <abt.h>
Philip Carns's avatar
Philip Carns committed
21 22
#include <libpmemobj.h>

23 24 25 26 27 28 29 30 31 32 33 34 35 36

struct options
{
    unsigned long xfer_size;
    unsigned long total_mem_size;
    int concurrency;
    char* pmdk_pool;
    int xstreams;
};

struct bench_worker_arg
{
    ABT_mutex *cur_off_mutex;
    unsigned long *cur_off;
Philip Carns's avatar
Philip Carns committed
37
    PMEMobjpool *pmem_pool;
38 39 40 41 42 43 44 45 46 47 48 49
};

/* defealt to 512 MiB total xfer unless specified otherwise */
#define DEF_BW_TOTAL_MEM_SIZE 524288000UL
/* defealt to 1 MiB xfer sizes unless specified otherwise */
#define DEF_BW_XFER_SIZE 1048576UL

static int parse_args(int argc, char **argv, struct options *opts);
static void usage(void);

static struct options g_opts;
static char *g_buffer = NULL;
Philip Carns's avatar
Philip Carns committed
50
static ABT_pool g_transfer_pool;
51

Philip Carns's avatar
Philip Carns committed
52
static int run_benchmark(struct options *opts, PMEMobjpool *pmem_pool);
53 54 55 56 57
static void bench_worker(void *_arg);

int main(int argc, char **argv) 
{
    int ret;
Philip Carns's avatar
Philip Carns committed
58 59 60 61 62
    ABT_xstream *bw_worker_xstreams = NULL;
    ABT_sched *bw_worker_scheds = NULL;
    ABT_sched self_sched;
    ABT_xstream self_xstream;
    int i;
Philip Carns's avatar
Philip Carns committed
63
    PMEMobjpool *pmem_pool;
64 65 66 67 68 69 70 71

    ret = parse_args(argc, argv, &g_opts);
    if(ret < 0)
    {
        usage();
        exit(EXIT_FAILURE);
    }

Philip Carns's avatar
Philip Carns committed
72 73 74 75 76 77 78 79 80 81 82 83
    ret = ABT_init(argc, argv);
    assert(ret == 0);

    /* set primary ES to idle without polling */
    ret = ABT_sched_create_basic(ABT_SCHED_BASIC_WAIT, 0, NULL,
        ABT_SCHED_CONFIG_NULL, &self_sched);
    assert(ret == 0);
    ret = ABT_xstream_self(&self_xstream);
    assert(ret == 0);
    ret = ABT_xstream_set_main_sched(self_xstream, self_sched);
    assert(ret == 0);

Philip Carns's avatar
Philip Carns committed
84
    pmem_pool = pmemobj_open(g_opts.pmdk_pool, NULL);
Philip Carns's avatar
Philip Carns committed
85 86 87 88 89 90
    if(!pmem_pool)
    {
        fprintf(stderr, "pmemobj_open: %s\n", pmemobj_errormsg());
        return(-1);
    }

91
    /* allocate one big buffer for writes */
92
    g_buffer = calloc(g_opts.xfer_size, 1);
93 94
    if(!g_buffer)
    {
95
        fprintf(stderr, "Error: unable to allocate %lu byte buffer.\n", g_opts.xfer_size);
96 97 98
        return(-1);
    }

Philip Carns's avatar
Philip Carns committed
99
    /* set up abt pool */
Philip Carns's avatar
Philip Carns committed
100
    if(g_opts.xstreams <= 0)
Philip Carns's avatar
Philip Carns committed
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
    {
        ABT_pool pool;
        ABT_xstream xstream;
        
        /* run bulk transfers from primary pool */
        ret = ABT_xstream_self(&xstream);
        assert(ret == 0);

        ret = ABT_xstream_get_main_pools(xstream, 1, &pool);
        assert(ret == 0);

        g_transfer_pool = pool;
    }
    else
    {
        /* run bulk transfers from a dedicated pool */
        bw_worker_xstreams = malloc(
                g_opts.xstreams * sizeof(*bw_worker_xstreams));
        bw_worker_scheds = malloc(
                g_opts.xstreams * sizeof(*bw_worker_scheds));
        assert(bw_worker_xstreams && bw_worker_scheds);

        ret = ABT_pool_create_basic(ABT_POOL_FIFO_WAIT, ABT_POOL_ACCESS_MPMC,
                ABT_TRUE, &g_transfer_pool);
        assert(ret == ABT_SUCCESS);
        for(i = 0; i < g_opts.xstreams; i++)
        {
            ret = ABT_sched_create_basic(ABT_SCHED_BASIC_WAIT, 1, &g_transfer_pool,
                    ABT_SCHED_CONFIG_NULL, &bw_worker_scheds[i]);
            assert(ret == ABT_SUCCESS);
            ret = ABT_xstream_create(bw_worker_scheds[i], &bw_worker_xstreams[i]);
            assert(ret == ABT_SUCCESS);
        }
    }

Philip Carns's avatar
Philip Carns committed
136
    run_benchmark(&g_opts, pmem_pool);
137

Philip Carns's avatar
Philip Carns committed
138 139 140 141 142 143
    for(i=0; i<g_opts.xstreams; i++)
    {
        ABT_xstream_join(bw_worker_xstreams[i]);
        ABT_xstream_free(&bw_worker_xstreams[i]);
    }

Philip Carns's avatar
Philip Carns committed
144 145
    pmemobj_close(pmem_pool);

146 147
    free(g_buffer);

Philip Carns's avatar
Philip Carns committed
148 149
    ABT_finalize();

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    return 0;
}

static int parse_args(int argc, char **argv, struct options *opts)
{
    int opt;
    int ret;

    memset(opts, 0, sizeof(*opts));

    opts->concurrency = 1;
    opts->total_mem_size = DEF_BW_TOTAL_MEM_SIZE;
    opts->xfer_size = DEF_BW_XFER_SIZE;
    opts->xstreams = -1;

165
    while((opt = getopt(argc, argv, "x:c:p:m:T:")) != -1)
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    {
        switch(opt)
        {
            case 'p':
                opts->pmdk_pool = strdup(optarg);
                if(!opts->pmdk_pool)
                {
                    perror("strdup");
                    return(-1);
                }
                break;
            case 'x':
                ret = sscanf(optarg, "%lu", &opts->xfer_size);
                if(ret != 1)
                    return(-1);
                break;
            case 'm':
                ret = sscanf(optarg, "%lu", &opts->total_mem_size);
                if(ret != 1)
                    return(-1);
                break;
187
            case 'T':
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
                ret = sscanf(optarg, "%d", &opts->xstreams);
                if(ret != 1)
                    return(-1);
                break;
            case 'c':
                ret = sscanf(optarg, "%d", &opts->concurrency);
                if(ret != 1)
                    return(-1);
                break;
            default:
                return(-1);
        }
    }

    if(opts->concurrency < 1 || !opts->pmdk_pool)
    {
        return(-1);
    }

    return(0);
}

static void usage(void)
{
    fprintf(stderr,
        "Usage: "
        "bake-p2p-bw -x <xfer_size> -m <total_mem_size> -p <pmdk pool>\n"
        "\t-x <xfer_size> - size of each bulk tranfer in bytes\n"
        "\t-m <total_mem_size> - total amount of data to write from each client process\n"
        "\t-p <pmdk pool> - existing pool created with pmempool create obj\n"
        "\t[-c concurrency] - number of concurrent operations to issue with ULTs\n"
219
        "\t[-T <os threads] - number of dedicated operating system threads to run ULTs on\n"
220 221 222 223 224
        "\t\texample: ./pmdk-bw -x 4096 -p /dev/shm/test.dat\n");
    
    return;
}

Philip Carns's avatar
Philip Carns committed
225
static int run_benchmark(struct options *opts, PMEMobjpool *pmem_pool)
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
{
    int ret;
    int i;
    ABT_thread *tid_array;
    struct bench_worker_arg *arg_array;
    ABT_mutex cur_off_mutex;
    unsigned long cur_off = 0;
    double start_tm, end_tm;

    tid_array = malloc(g_opts.concurrency * sizeof(*tid_array));
    assert(tid_array);
    arg_array = malloc(g_opts.concurrency * sizeof(*arg_array));
    assert(arg_array);

    ABT_mutex_create(&cur_off_mutex);

    start_tm = ABT_get_wtime();
    for(i=0; i<g_opts.concurrency; i++)
    {
        arg_array[i].cur_off_mutex = &cur_off_mutex;
        arg_array[i].cur_off = &cur_off;
Philip Carns's avatar
Philip Carns committed
247
        arg_array[i].pmem_pool = pmem_pool;
Philip Carns's avatar
Philip Carns committed
248
        ret = ABT_thread_create(g_transfer_pool, bench_worker, 
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
            &arg_array[i], ABT_THREAD_ATTR_NULL, &tid_array[i]);
        assert(ret == 0);
    }

    for(i=0; i<g_opts.concurrency; i++)
    {
        ABT_thread_join(tid_array[i]);
        ABT_thread_free(&tid_array[i]);
    }
    end_tm = ABT_get_wtime();

    printf("<op>\t<concurrency>\t<xfer_size>\t<total_bytes>\t<seconds>\t<MiB/s>\n");
    printf("create_write_persist\t%d\t%lu\t%lu\t%f\t%f\n",
        g_opts.concurrency,
        g_opts.xfer_size,
        g_opts.total_mem_size,
        (end_tm-start_tm),
        ((double)g_opts.total_mem_size/(end_tm-start_tm))/(1024.0*1024.0));

    free(tid_array);
    ABT_mutex_free(&cur_off_mutex);

    return(0);
}

static void bench_worker(void *_arg)
{
    struct bench_worker_arg* arg = _arg;
Philip Carns's avatar
Philip Carns committed
277 278 279 280
    PMEMoid oid;
    uint64_t *buffer;
    uint64_t val;
    int ret;
281 282 283 284 285 286 287

    ABT_mutex_spinlock(*arg->cur_off_mutex);
    while(*arg->cur_off < g_opts.total_mem_size)
    {
        (*arg->cur_off) += g_opts.xfer_size;
        ABT_mutex_unlock(*arg->cur_off_mutex);

Philip Carns's avatar
Philip Carns committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
        /* create an object */
        /* NOTE: for now we don't try to keep up with oid */
        ret = pmemobj_alloc(arg->pmem_pool, &oid, g_opts.xfer_size, 0, NULL, NULL);
        if(ret != 0)
        {
            fprintf(stderr, "pmemobj_create: %s\n", pmemobj_errormsg());
            assert(0);
        }

        /* fill in values */
        buffer = pmemobj_direct(oid);
        for(val = 0; val < g_opts.xfer_size/sizeof(val); val++)
            buffer[val] = val;

        /* persist */
Philip Carns's avatar
Philip Carns committed
303
        // pmemobj_persist(arg->pmem_pool, buffer, g_opts.xfer_size);
304 305 306 307 308 309 310 311

        ABT_mutex_spinlock(*arg->cur_off_mutex);
    }

    ABT_mutex_unlock(*arg->cur_off_mutex);

    return;
}