concurrent-write-bench.c 13.9 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6 7 8 9 10 11 12
#define  _GNU_SOURCE

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
13 14
#include <float.h>
#include <errno.h>
Philip Carns's avatar
Philip Carns committed
15

16
#include "abt-io-config.h"
17 18 19
#include <abt.h>
#include <abt-io.h>

20 21 22 23
#ifndef HAVE_ODIRECT
#define O_DIRECT 0
#endif

Philip Carns's avatar
Philip Carns committed
24
#define OFLAGS (O_WRONLY|O_CREAT|O_DIRECT|O_SYNC)
25

Philip Carns's avatar
Philip Carns committed
26 27 28 29 30 31 32 33 34 35 36 37 38
/* This is a simple benchmark that measures the
 * streaming, concurrent, sequentially-issued write throughput for a 
 * specified number of concurrent operations.  It includes an abt-io version and
 * a pthread version.
 *
 * Both tests use O_DIRECT and O_SYNC if available.
 */

/* abt data types and fn prototypes */
struct write_abt_arg
{
    double start_time;
    size_t size;
39
    ABT_mutex *mutex;
Philip Carns's avatar
Philip Carns committed
40 41 42
    off_t *next_offset;
    int fd;
    double duration;
43
    abt_io_instance_id aid;
44
    void* buffer;
Philip Carns's avatar
Philip Carns committed
45 46 47
};

static void write_abt_bench(void *_arg);
48 49 50
static void abt_bench(int buffer_per_thread, unsigned int concurrency, size_t size, 
    double duration, const char* filename, unsigned int* ops_done, double *seconds);
static void abt_bench_nb(int buffer_per_thread, unsigned int concurrency, size_t size, 
Philip Carns's avatar
Philip Carns committed
51 52 53 54 55 56 57 58 59 60 61
    double duration, const char* filename, unsigned int* ops_done, double *seconds);

/* pthread data types and fn prototypes */
struct write_pthread_arg
{
    double start_time;
    size_t size;
    pthread_mutex_t *mutex;
    off_t *next_offset;
    int fd;
    double duration;
62
    void* buffer;
Philip Carns's avatar
Philip Carns committed
63 64 65
};

static void* write_pthread_bench(void *_arg);
66
static void pthread_bench( int buffer_per_thread, unsigned int concurrency, size_t size, 
Philip Carns's avatar
Philip Carns committed
67 68 69 70 71 72 73 74
    double duration, const char* filename, unsigned int* ops_done, double *seconds);


static double wtime(void);

int main(int argc, char **argv) 
{
    int ret;
75 76
    unsigned abt_ops_done, abt_nb_ops_done, pthread_ops_done;
    double abt_seconds, abt_nb_seconds, pthread_seconds;
Philip Carns's avatar
Philip Carns committed
77 78 79
    size_t size;
    unsigned int concurrency;
    double duration;
80
    int buffer_per_thread = 0;
Philip Carns's avatar
Philip Carns committed
81 82
    ABT_sched self_sched;
    ABT_xstream self_xstream;
Philip Carns's avatar
Philip Carns committed
83

84 85
    ABT_init(argc, argv);

Philip Carns's avatar
Philip Carns committed
86 87 88 89 90 91 92 93
    /* set caller (self) ES to sleep when idle by using SCHED_BASIC_WAIT */
    ret = ABT_sched_create_basic(ABT_SCHED_BASIC_WAIT, 0, NULL,
        ABT_SCHED_CONFIG_NULL, &self_sched);
    assert(ret == ABT_SUCCESS);
    ret = ABT_xstream_self(&self_xstream);
    assert(ret == ABT_SUCCESS);
    ret = ABT_xstream_set_main_sched(self_xstream, self_sched);
    assert(ret == ABT_SUCCESS);
94

95
    if(argc != 6)
Philip Carns's avatar
Philip Carns committed
96
    {
97
        fprintf(stderr, "Usage: concurrent-write-bench <write_size> <concurrency> <duration> <file> <buffer_per_thread (0|1)>\n");
Philip Carns's avatar
Philip Carns committed
98 99 100 101 102 103
        return(-1);
    }

    ret = sscanf(argv[1], "%zu", &size);
    if(ret != 1)
    {
104
        fprintf(stderr, "Usage: concurrent-write-bench <write_size> <concurrency> <duration> <file> <buffer_per_thread (0|1)>\n");
Philip Carns's avatar
Philip Carns committed
105 106 107 108 109 110
        return(-1);
    }

    ret = sscanf(argv[2], "%u", &concurrency);
    if(ret != 1)
    {
111
        fprintf(stderr, "Usage: concurrent-write-bench <write_size> <concurrency> <duration> <file> <buffer_per_thread (0|1)>\n");
Philip Carns's avatar
Philip Carns committed
112 113 114 115 116 117
        return(-1);
    }

    ret = sscanf(argv[3], "%lf", &duration);
    if(ret != 1)
    {
118 119 120 121 122 123 124 125
        fprintf(stderr, "Usage: concurrent-write-bench <write_size> <concurrency> <duration> <file> <buffer_per_thread (0|1)>\n");
        return(-1);
    }

    ret = sscanf(argv[5], "%d", &buffer_per_thread);
    if(ret != 1)
    {
        fprintf(stderr, "Usage: concurrent-write-bench <write_size> <concurrency> <duration> <file> <buffer_per_thread (0|1)>\n");
Philip Carns's avatar
Philip Carns committed
126 127
        return(-1);
    }
128
    buffer_per_thread = !!buffer_per_thread;
Philip Carns's avatar
Philip Carns committed
129 130 131

    /* run benchmarks */
    printf("# Running ABT benchmark...\n");
132 133 134 135 136 137 138 139
    abt_bench(buffer_per_thread, concurrency, size, duration, argv[4], &abt_ops_done, &abt_seconds);
    printf("# ...ABT benchmark done.\n");

    printf("# Running ABT (nonblocking) benchmark...\n");
    abt_bench_nb(buffer_per_thread, concurrency, size, duration, argv[4], &abt_nb_ops_done, &abt_nb_seconds);
    printf("# ...ABT (nonblocking) benchmark done.\n");

    ABT_finalize();
Philip Carns's avatar
Philip Carns committed
140 141 142 143

    sleep(1);

    printf("# Running pthread benchmark...\n");
144
    pthread_bench(buffer_per_thread, concurrency, size, duration, argv[4], &pthread_ops_done, &pthread_seconds);
Philip Carns's avatar
Philip Carns committed
145 146 147 148 149 150 151 152
    printf("# ...pthread benchmark done.\n");


    /* output */
    printf("#<type>\t<concurrency>\t<write_size>\t<ops>\t<seconds>\t<MiB/s>\n");
    printf("abt\t%u\t%zu\t%u\t%f\t%f\n",
        concurrency, size, abt_ops_done, abt_seconds, 
        ((((double)size*(double)abt_ops_done))/abt_seconds)/(1024.0*1024.0));
153 154 155
    printf("abt_nb\t%u\t%zu\t%u\t%f\t%f\n",
        concurrency, size, abt_nb_ops_done, abt_nb_seconds, 
        ((((double)size*(double)abt_nb_ops_done))/abt_nb_seconds)/(1024.0*1024.0));
Philip Carns's avatar
Philip Carns committed
156 157 158 159 160 161 162
    printf("pthread\t%u\t%zu\t%u\t%f\t%f\n",
        concurrency, size, pthread_ops_done, pthread_seconds, 
        ((((double)size*(double)pthread_ops_done))/pthread_seconds)/(1024.0*1024.0));

    return(0);
}

163
static void abt_bench(int buffer_per_thread, unsigned int concurrency, size_t size, double duration,
Philip Carns's avatar
Philip Carns committed
164 165
    const char *filename, unsigned int* ops_done, double *seconds)
{
166 167
    ABT_thread *tid_array = NULL;
    ABT_mutex mutex;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
168
    struct write_abt_arg* args;
Philip Carns's avatar
Philip Carns committed
169 170 171
    off_t next_offset = 0;
    int ret;
    double end;
172
    unsigned int i;
Philip Carns's avatar
Philip Carns committed
173 174 175
    ABT_xstream xstream;
    ABT_pool pool;
    abt_io_instance_id aid;
176 177
    int *fd_array;
    char tmp_filename[256] = {0};
Jonathan Jenkins's avatar
Jonathan Jenkins committed
178 179
    void *buffer;
    double start;
Philip Carns's avatar
Philip Carns committed
180

181 182
    tid_array = malloc(concurrency * sizeof(*tid_array));
    assert(tid_array);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    fd_array = malloc(concurrency * sizeof(*fd_array));
    assert(fd_array);

    for(i=0; i<concurrency; i++)
    {
        sprintf(tmp_filename, "%s.%d", filename, i);
        fd_array[i] = open(tmp_filename, OFLAGS, S_IWUSR|S_IRUSR);
        if(fd_array[i] < 0)
        {
            perror("open");
            assert(0);
        }
        ret = fallocate(fd_array[i], 0, 0, 10737418240UL);
        assert(ret == 0);
        ret = posix_fadvise(fd_array[i], 0, 10737418240UL, POSIX_FADV_DONTNEED);
        assert(ret == 0);
        ret = posix_fadvise(fd_array[i], 0, 10737418240UL, POSIX_FADV_SEQUENTIAL);
        assert(ret == 0);
    }
Philip Carns's avatar
Philip Carns committed
202

Philip Carns's avatar
Philip Carns committed
203 204 205 206 207
    /* retrieve current pool to use for ULT concurrency */
    ret = ABT_xstream_self(&xstream);
    assert(ret == 0);
    ret = ABT_xstream_get_main_pools(xstream, 1, &pool);
    assert(ret == 0);
208

Philip Carns's avatar
Philip Carns committed
209
    /* initialize abt_io */
210
    aid = abt_io_init(0);
Philip Carns's avatar
Philip Carns committed
211
    assert(aid != NULL);
Philip Carns's avatar
Philip Carns committed
212

213
    ABT_mutex_create(&mutex);
Philip Carns's avatar
Philip Carns committed
214

Jonathan Jenkins's avatar
Jonathan Jenkins committed
215 216 217
    if (buffer_per_thread)
        buffer = NULL;
    else
218
    {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
219
        ret = posix_memalign(&buffer, 4096, size);
220
        assert(ret == 0);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
221 222 223 224 225 226 227 228 229 230 231 232 233
        memset(buffer, 0, size);
    }

    args = malloc(concurrency*sizeof(*args));
    assert(args != NULL);

    for (i = 0; i < concurrency; i++)
    {
        args[i].mutex = &mutex;
        args[i].size = size;
        args[i].next_offset = &next_offset;
        args[i].duration = duration;
        args[i].aid = aid;
234
        args[i].fd = fd_array[i];
Jonathan Jenkins's avatar
Jonathan Jenkins committed
235 236 237 238 239 240 241 242
        if (buffer == NULL)
        {
            ret = posix_memalign(&args[i].buffer, 4096, size);
            assert(ret == 0);
            memset(args[i].buffer, 0, size);
        }
        else
            args[i].buffer = buffer;
243 244
    }

245

Philip Carns's avatar
Philip Carns committed
246 247
    for(i=0; i<concurrency; i++)
    {
248
        /* create ULTs */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
249
        ret = ABT_thread_create(pool, write_abt_bench, &args[i], ABT_THREAD_ATTR_NULL, &tid_array[i]);
Philip Carns's avatar
Philip Carns committed
250
        assert(ret == 0);
Philip Carns's avatar
Philip Carns committed
251 252 253
    }

    for(i=0; i<concurrency; i++)
Philip Carns's avatar
Philip Carns committed
254
        ABT_thread_join(tid_array[i]);
Philip Carns's avatar
Philip Carns committed
255 256

    end = wtime();
Jonathan Jenkins's avatar
Jonathan Jenkins committed
257

258
    for(i=0; i<concurrency; i++)
Philip Carns's avatar
Philip Carns committed
259
        ABT_thread_free(&tid_array[i]);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
260 261 262 263 264 265 266

    /* compute min start time */
    start = DBL_MAX;
    for (i = 0; i < concurrency; i++)
        start = args[i].start_time < start ? args[i].start_time : start;

    *seconds = end-start;
Philip Carns's avatar
Philip Carns committed
267 268
    *ops_done = next_offset/size;

Philip Carns's avatar
Philip Carns committed
269 270
    abt_io_finalize(aid);

271 272
    ABT_mutex_free(&mutex);
    free(tid_array);
Philip Carns's avatar
Philip Carns committed
273

Jonathan Jenkins's avatar
Jonathan Jenkins committed
274 275 276 277 278 279 280
    if(buffer_per_thread)
    {
        for (i = 0; i < concurrency; i++)
            free(args[i].buffer);
    }
    else
        free(buffer);
281

Jonathan Jenkins's avatar
Jonathan Jenkins committed
282 283
    free(args);

284 285 286 287 288 289 290 291
    for(i=0; i<concurrency; i++)
        close(fd_array[i]);
    for(i=0; i<concurrency; i++)
    {
        sprintf(tmp_filename, "%s.%d", filename, i);
        unlink(tmp_filename);

    }
Philip Carns's avatar
Philip Carns committed
292 293 294 295

    return;
}

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
static void abt_bench_nb(int buffer_per_thread, unsigned int concurrency, size_t size, double duration,
    const char *filename, unsigned int* ops_done, double *seconds)
{
    int fd;
    off_t next_offset = 0;
    int ret;
    double end;
    unsigned int i;
    abt_io_instance_id aid;
    void **buffers = NULL;
    unsigned int num_buffers = 0;
    double start_time;
    abt_io_op_t **ops;
    ssize_t *wrets;

311
    fd = open(filename, OFLAGS, S_IWUSR|S_IRUSR);
312 313 314 315 316
    if(!fd)
    {
        perror("open");
        assert(0);
    }
317 318
    ret = fallocate(fd, 0, 0, 10737418240UL);
    assert(ret == 0);
Philip Carns's avatar
Philip Carns committed
319 320 321 322
    ret = posix_fadvise(fd, 0, 10737418240UL, POSIX_FADV_DONTNEED);
    assert(ret == 0);
    ret = posix_fadvise(fd, 0, 10737418240UL, POSIX_FADV_SEQUENTIAL);
    assert(ret == 0);
323

324
    /* initialize abt_io */
325
    aid = abt_io_init(0);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    assert(aid != NULL);

    /* set up buffers */
    num_buffers = buffer_per_thread ? concurrency : 1;
    buffers = malloc(num_buffers*sizeof(*buffers));
    assert(buffers);
    for (i = 0; i < num_buffers; i++) {
        ret = posix_memalign(&buffers[i], 4096, size);
        assert(ret == 0);
        memset(buffers[i], 0, size);
    }

    /* set up async contexts */
    ops = calloc(concurrency, sizeof(*ops));
    assert(ops);
    wrets = malloc(concurrency * sizeof(*wrets));
    assert(wrets);

    /* start the benchmark */
    start_time = wtime();

    /* in the absence of a waitany, just going through one-by-one */
    for(i = 0; ; i = (i+1) % concurrency)
    {
        if (ops[i] != NULL)
        {
            ret = abt_io_op_wait(ops[i]);
            assert(ret == 0 && wrets[i] > 0 && (size_t)wrets[i] == size);
            abt_io_op_free(ops[i]);
        }

        if (wtime() - start_time < duration)
        {
            ops[i] = abt_io_pwrite_nb(aid, fd, buffers[i*buffer_per_thread],
                    size, next_offset, wrets+i);
            assert(ops[i]);
            next_offset += size;
        }
        else if (ops[i] == NULL)
            break;
        else
            ops[i] = NULL;
    }

    end = wtime();

    *seconds = end-start_time;
    *ops_done = next_offset/size;

    abt_io_finalize(aid);

    for (i = 0; i < num_buffers; i++)
        free(buffers[i]);
    free(buffers);

    free(wrets);

    close(fd);
    unlink(filename);

    return;
}

389
static void pthread_bench(int buffer_per_thread, unsigned int concurrency, size_t size, double duration,
Philip Carns's avatar
Philip Carns committed
390 391 392 393
    const char *filename, unsigned int* ops_done, double *seconds)
{
    pthread_t *id_array = NULL;
    pthread_mutex_t mutex;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
394
    struct write_pthread_arg* args;
Philip Carns's avatar
Philip Carns committed
395 396 397
    off_t next_offset = 0;
    int ret;
    double end;
398
    unsigned int i;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
399 400 401
    int fd;
    void *buffer;
    double start;
Philip Carns's avatar
Philip Carns committed
402

403
    fd = open(filename, OFLAGS, S_IWUSR|S_IRUSR);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
404
    if(!fd)
Philip Carns's avatar
Philip Carns committed
405 406 407 408
    {
        perror("open");
        assert(0);
    }
409 410
    ret = fallocate(fd, 0, 0, 10737418240UL);
    assert(ret == 0);
Philip Carns's avatar
Philip Carns committed
411 412 413 414
    ret = posix_fadvise(fd, 0, 10737418240UL, POSIX_FADV_DONTNEED);
    assert(ret == 0);
    ret = posix_fadvise(fd, 0, 10737418240UL, POSIX_FADV_SEQUENTIAL);
    assert(ret == 0);
Philip Carns's avatar
Philip Carns committed
415 416 417 418 419 420

    id_array = malloc(concurrency * sizeof(*id_array));
    assert(id_array);

    pthread_mutex_init(&mutex, NULL);

Jonathan Jenkins's avatar
Jonathan Jenkins committed
421 422 423
    if (buffer_per_thread)
        buffer = NULL;
    else
424
    {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
425
        ret = posix_memalign(&buffer, 4096, size);
426
        assert(ret == 0);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
427
        memset(buffer, 0, size);
428 429
    }

Jonathan Jenkins's avatar
Jonathan Jenkins committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    args = malloc(concurrency * sizeof(*args));
    assert(args != NULL);

    for (i = 0; i < concurrency; i++) {
        args[i].mutex = &mutex;
        args[i].size = size;
        args[i].next_offset = &next_offset;
        args[i].duration = duration;
        args[i].fd = fd;
        if(buffer == NULL)
        {
            ret = posix_memalign(&args[i].buffer, 4096, size);
            assert(ret == 0);
            memset(args[i].buffer, 0, size);
        }
        else
            args[i].buffer = buffer;
    }
Philip Carns's avatar
Philip Carns committed
448 449 450

    for(i=0; i<concurrency; i++)
    {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
451
        ret = pthread_create(&id_array[i], NULL, write_pthread_bench, &args[i]);
Philip Carns's avatar
Philip Carns committed
452 453 454 455 456 457 458 459 460 461
        assert(ret == 0);
    }

    for(i=0; i<concurrency; i++)
    {
        ret = pthread_join(id_array[i], NULL);
        assert(ret == 0);
    }

    end = wtime();
Jonathan Jenkins's avatar
Jonathan Jenkins committed
462 463 464 465 466 467

    start = DBL_MAX;
    for (i = 0; i < concurrency; i++)
        start = args[i].start_time < start ? args[i].start_time : start;

    *seconds = end-start;
Philip Carns's avatar
Philip Carns committed
468 469 470 471 472
    *ops_done = next_offset/size;

    pthread_mutex_destroy(&mutex);
    free(id_array);

Jonathan Jenkins's avatar
Jonathan Jenkins committed
473 474 475 476 477 478 479 480 481
    if(buffer_per_thread)
    {
        for (i = 0; i < concurrency; i++)
            free(args[i].buffer);
    }
    else
        free(buffer);

    free(args);
482

Jonathan Jenkins's avatar
Jonathan Jenkins committed
483
    close(fd);
Philip Carns's avatar
Philip Carns committed
484 485 486 487 488 489 490
    unlink(filename);

    return;
}

static void write_abt_bench(void *_arg)
{
491
    struct write_abt_arg* arg = _arg;
Philip Carns's avatar
Philip Carns committed
492 493 494
    off_t my_offset;
    size_t ret;

Jonathan Jenkins's avatar
Jonathan Jenkins committed
495 496
    arg->start_time = wtime();
    while((wtime()-arg->start_time) < arg->duration) 
Philip Carns's avatar
Philip Carns committed
497
    {
498
        ABT_mutex_lock(*arg->mutex);
Philip Carns's avatar
Philip Carns committed
499 500
        my_offset = *arg->next_offset;
        (*arg->next_offset) += arg->size;
501
        ABT_mutex_unlock(*arg->mutex);
Philip Carns's avatar
Philip Carns committed
502

503
        ret = abt_io_pwrite(arg->aid, arg->fd, arg->buffer, arg->size, my_offset);
Philip Carns's avatar
Philip Carns committed
504 505
        assert(ret == arg->size);
    }
Philip Carns's avatar
Philip Carns committed
506

Philip Carns's avatar
Philip Carns committed
507 508 509 510 511 512 513 514 515
    return;
}

static void *write_pthread_bench(void *_arg)
{
    struct write_pthread_arg* arg = _arg;
    off_t my_offset;
    size_t ret;

Jonathan Jenkins's avatar
Jonathan Jenkins committed
516 517
    arg->start_time = wtime();
    while((wtime()-arg->start_time) < arg->duration) 
Philip Carns's avatar
Philip Carns committed
518 519 520 521 522 523
    {
        pthread_mutex_lock(arg->mutex);
        my_offset = *arg->next_offset;
        (*arg->next_offset) += arg->size;
        pthread_mutex_unlock(arg->mutex);

524
        ret = pwrite(arg->fd, arg->buffer, arg->size, my_offset);
Philip Carns's avatar
Philip Carns committed
525 526 527 528 529 530 531 532 533 534 535 536
        assert(ret == arg->size);
    }

    return(NULL);
}

static double wtime(void)
{
    struct timeval t;
    gettimeofday(&t, NULL);
    return((double)t.tv_sec + (double)t.tv_usec / 1000000.0);
}