pmdk-bw.c 9.62 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>
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;
37
    PMEMobjpool *pmem_pool;
38 39 40 41 42 43 44 45 46 47 48
};

/* 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;
49
static ABT_pool g_transfer_pool;
50

51
static int run_benchmark(struct options *opts, PMEMobjpool **pmem_pools, int pmem_pools_count);
52 53 54 55 56
static void bench_worker(void *_arg);

int main(int argc, char **argv) 
{
    int ret;
57 58 59 60 61
    ABT_xstream *bw_worker_xstreams = NULL;
    ABT_sched *bw_worker_scheds = NULL;
    ABT_sched self_sched;
    ABT_xstream self_xstream;
    int i;
62 63 64
    PMEMobjpool **pmem_pools;
    int pmem_pools_count = 0;
    char *tmp_pool_name;
65 66 67 68 69 70 71 72

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

73 74 75 76 77 78 79 80 81 82 83 84
    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);

85 86 87 88 89 90 91 92 93 94 95 96 97 98
    /* count number of commas in pmdk_pool argument to see how many pools we
     * have
     */
    pmem_pools_count = 1;
    for(i=0; i<strlen(g_opts.pmdk_pool); i++)
        if(g_opts.pmdk_pool[i] == ',')
            pmem_pools_count++;

    pmem_pools = malloc(pmem_pools_count * sizeof(*pmem_pools));
    assert(pmem_pools);

    tmp_pool_name = strtok(g_opts.pmdk_pool, ",");
    pmem_pools[0] = pmemobj_open(tmp_pool_name, NULL);
    if(!pmem_pools[0])
99 100 101 102 103
    {
        fprintf(stderr, "pmemobj_open: %s\n", pmemobj_errormsg());
        return(-1);
    }

104 105
    i=1;
    while((tmp_pool_name = strtok(NULL, ",")))
106
    {
107 108 109 110 111 112 113
        pmem_pools[i] = pmemobj_open(tmp_pool_name, NULL);
        if(!pmem_pools[i])
        {
            fprintf(stderr, "pmemobj_open: %s\n", pmemobj_errormsg());
            return(-1);
        }
        i++;
114
    }
115
    assert(i==pmem_pools_count);
116

117
    /* set up abt pool */
Philip Carns's avatar
Philip Carns committed
118
    if(g_opts.xstreams <= 0)
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
    {
        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);
        }
    }

154
    run_benchmark(&g_opts, pmem_pools, pmem_pools_count);
155

156 157 158 159 160 161
    for(i=0; i<g_opts.xstreams; i++)
    {
        ABT_xstream_join(bw_worker_xstreams[i]);
        ABT_xstream_free(&bw_worker_xstreams[i]);
    }

162 163
    for(i=0; i<pmem_pools_count; i++)
        pmemobj_close(pmem_pools[i]);
164

165 166
    ABT_finalize();

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    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;

182
    while((opt = getopt(argc, argv, "x:c:p:m:T:")) != -1)
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    {
        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;
204
            case 'T':
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                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"
232 233
        "\t-x <xfer_size> - size of each write operation in bytes\n"
        "\t-m <total_mem_size> - total amount of data to write to each pool\n"
234
        "\t-p <pmdk pool> - existing pool created with pmempool create obj\n"
235 236
        "\t\tnote: can be comma-separated list to use multiple pools;\n"
        "\t\t      pools will be round-robin distributed across ULTs.\n"
237
        "\t[-c concurrency] - number of concurrent operations to issue with ULTs\n"
238
        "\t[-T <os threads] - number of dedicated operating system threads to run ULTs on\n"
239 240 241 242 243
        "\t\texample: ./pmdk-bw -x 4096 -p /dev/shm/test.dat\n");
    
    return;
}

244 245 246 247 248 249 250
struct pool_offset
{
    ABT_mutex mutex;
    unsigned long off;
};

static int run_benchmark(struct options *opts, PMEMobjpool **pmem_pools, int pmem_pools_count)
251 252 253 254 255 256
{
    int ret;
    int i;
    ABT_thread *tid_array;
    struct bench_worker_arg *arg_array;
    double start_tm, end_tm;
257 258
    int j;
    struct pool_offset* cur_off_array;
259 260 261 262 263

    tid_array = malloc(g_opts.concurrency * sizeof(*tid_array));
    assert(tid_array);
    arg_array = malloc(g_opts.concurrency * sizeof(*arg_array));
    assert(arg_array);
264 265
    cur_off_array = malloc(pmem_pools_count * sizeof(*cur_off_array));
    assert(cur_off_array);
266

267 268 269 270 271 272 273 274
    /* we keep a current offset *per pool* so that any threads sharing that
     * pool will likewise share an offset
     */
    for(i=0; i<pmem_pools_count; i++)
    {
        ABT_mutex_create(&cur_off_array[i].mutex);
        cur_off_array[i].off = 0;
    }
275

276
    j=0;
277 278 279
    start_tm = ABT_get_wtime();
    for(i=0; i<g_opts.concurrency; i++)
    {
280 281 282 283 284 285
        arg_array[i].cur_off_mutex = &cur_off_array[j].mutex;
        arg_array[i].cur_off = &cur_off_array[j].off;
        arg_array[i].pmem_pool = pmem_pools[j];
        j++;
        if(j>=pmem_pools_count)
            j=0;
Philip Carns's avatar
Philip Carns committed
286
        ret = ABT_thread_create(g_transfer_pool, bench_worker, 
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
            &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,
Philip Carns's avatar
Philip Carns committed
302
        g_opts.total_mem_size * pmem_pools_count,
303
        (end_tm-start_tm),
304
        ((double)g_opts.total_mem_size * pmem_pools_count/(end_tm-start_tm))/(1024.0*1024.0));
305 306

    free(tid_array);
307 308 309 310 311
    for(i=0; i<pmem_pools_count; i++)
    {
        ABT_mutex_free(&cur_off_array[i].mutex);
    }
    free(cur_off_array);
312 313 314 315 316 317 318

    return(0);
}

static void bench_worker(void *_arg)
{
    struct bench_worker_arg* arg = _arg;
319 320 321 322
    PMEMoid oid;
    uint64_t *buffer;
    uint64_t val;
    int ret;
323 324 325 326 327 328 329

    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);

330 331 332 333 334 335 336 337 338
        /* 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);
        }

339 340 341 342
        /* TODO: the offset tracking stuff is superfluous if we are just
         * setting values.  Need to think about what we want to measure
         * here.
         */
343 344 345 346 347 348
        /* 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
349
        pmemobj_persist(arg->pmem_pool, buffer, g_opts.xfer_size);
350 351 352 353 354 355 356 357

        ABT_mutex_spinlock(*arg->cur_off_mutex);
    }

    ABT_mutex_unlock(*arg->cur_off_mutex);

    return;
}