concurrent-write-bench.c 8.71 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1
2
3
4
5
6
7
8
9
10
11
12
13
#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>

14
15
#include <abt.h>
#include <abt-io.h>
16
#include <abt-snoozer.h>
17

Philip Carns's avatar
Philip Carns committed
18
19
20
21
22
23
24
25
26
27
28
29
30
/* 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;
31
    ABT_mutex *mutex;
Philip Carns's avatar
Philip Carns committed
32
33
34
    off_t *next_offset;
    int fd;
    double duration;
35
    abt_io_instance_id aid;
Philip Carns's avatar
Philip Carns committed
36
37
38
};

static void write_abt_bench(void *_arg);
39
static void abt_bench(int argc, char **argv, unsigned int concurrency, size_t size, 
Philip Carns's avatar
Philip Carns committed
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
    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;
};

static void* write_pthread_bench(void *_arg);
static void pthread_bench(unsigned int concurrency, size_t size, 
    double duration, const char* filename, unsigned int* ops_done, double *seconds);


static double wtime(void);

int main(int argc, char **argv) 
{
    int ret;
    unsigned abt_ops_done, pthread_ops_done;
    double abt_seconds, pthread_seconds;
    size_t size;
    unsigned int concurrency;
    double duration;

    if(argc != 5)
    {
        fprintf(stderr, "Usage: concurrent-write-bench <write_size> <concurrency> <duration> <file>\n");
        return(-1);
    }

    ret = sscanf(argv[1], "%zu", &size);
    if(ret != 1)
    {
        fprintf(stderr, "Usage: concurrent-write-bench <write_size> <concurrency> <duration> <file>\n");
        return(-1);
    }

    ret = sscanf(argv[2], "%u", &concurrency);
    if(ret != 1)
    {
        fprintf(stderr, "Usage: concurrent-write-bench <write_size> <concurrency> <duration> <file>\n");
        return(-1);
    }

    ret = sscanf(argv[3], "%lf", &duration);
    if(ret != 1)
    {
        fprintf(stderr, "Usage: concurrent-write-bench <write_size> <concurrency> <duration> <file>\n");
        return(-1);
    }

    /* run benchmarks */
    printf("# Running ABT benchmark...\n");
98
    abt_bench(argc, argv, concurrency, size, duration, argv[4], &abt_ops_done, &abt_seconds);
Philip Carns's avatar
Philip Carns committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    printf("# ...abt benchmark done.\n");

    sleep(1);

    printf("# Running pthread benchmark...\n");
    pthread_bench(concurrency, size, duration, argv[4], &pthread_ops_done, &pthread_seconds);
    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));
    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);
}

120
static void abt_bench(int argc, char **argv, unsigned int concurrency, size_t size, double duration,
Philip Carns's avatar
Philip Carns committed
121
122
    const char *filename, unsigned int* ops_done, double *seconds)
{
123
124
125
    ABT_thread *tid_array = NULL;
    ABT_mutex mutex;
    struct write_abt_arg arg;
Philip Carns's avatar
Philip Carns committed
126
127
128
129
    off_t next_offset = 0;
    int ret;
    double end;
    int i;
Philip Carns's avatar
Philip Carns committed
130
    ABT_xstream *progress_xstreams;
131
    ABT_pool progress_pool;
Philip Carns's avatar
Philip Carns committed
132
133
134
    ABT_xstream xstream;
    ABT_pool pool;
    abt_io_instance_id aid;
Philip Carns's avatar
Philip Carns committed
135
136
137
138
139
140
141
142

    arg.fd = open(filename, O_WRONLY|O_CREAT|O_DIRECT|O_SYNC, S_IWUSR|S_IRUSR);
    if(!arg.fd)
    {
        perror("open");
        assert(0);
    }

143
144
    tid_array = malloc(concurrency * sizeof(*tid_array));
    assert(tid_array);
Philip Carns's avatar
Philip Carns committed
145

Philip Carns's avatar
Philip Carns committed
146
147
148
    progress_xstreams = malloc(concurrency * sizeof(*progress_xstreams));
    assert(progress_xstreams);

149
    printf("WARNING: this benchmark may have an issue with one core busy spinning, see comments.\n");
150
151
152
153
154
155
156
157
158
    /* set up argobots */
    ret = ABT_init(argc, argv);
    assert(ret == 0);

    /* set primary ES to idle without polling */
    ret = ABT_snoozer_xstream_self_set();
    assert(ret == 0);

    /* create a dedicated ES drive Mercury progress */
Philip Carns's avatar
Philip Carns committed
159
160
161
162
163
    /* NOTE: for now we are going to use the same number of execution streams
     * in the io pool as the desired level of issue concurrency, but this
     * doesn't need to be the case in general.
     */
    ret = ABT_snoozer_xstream_create(concurrency, &progress_pool, progress_xstreams);
164
165
    assert(ret == 0);

Philip Carns's avatar
Philip Carns committed
166
167
168
169
170
    /* 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);
171

Philip Carns's avatar
Philip Carns committed
172
173
174
    /* initialize abt_io */
    aid = abt_io_init(progress_pool);
    assert(aid != NULL);
Philip Carns's avatar
Philip Carns committed
175

176
    ABT_mutex_create(&mutex);
Philip Carns's avatar
Philip Carns committed
177
178
179
180
181

    arg.mutex = &mutex;
    arg.size = size;
    arg.next_offset = &next_offset;
    arg.duration = duration;
Philip Carns's avatar
Philip Carns committed
182
    arg.aid = aid;
Philip Carns's avatar
Philip Carns committed
183

184
185
    arg.start_time = wtime();

Philip Carns's avatar
Philip Carns committed
186
187
    for(i=0; i<concurrency; i++)
    {
188
        /* create ULTs */
Philip Carns's avatar
Philip Carns committed
189
190
        ret = ABT_thread_create(pool, write_abt_bench, &arg, ABT_THREAD_ATTR_NULL, &tid_array[i]);
        assert(ret == 0);
Philip Carns's avatar
Philip Carns committed
191
192
193
194
    }

    arg.start_time = wtime();

195
196
197
198
    /* TODO: this is likely to be causing the main thread to busy spin
     * waiting for the ULTs to complete.  Need to confirm.  Can be addressed
     * in Argobots possibly or by working around with an eventual wait here.
     */
Philip Carns's avatar
Philip Carns committed
199
    for(i=0; i<concurrency; i++)
Philip Carns's avatar
Philip Carns committed
200
        ABT_thread_join(tid_array[i]);
Philip Carns's avatar
Philip Carns committed
201
202

    end = wtime();
203
204
 
    for(i=0; i<concurrency; i++)
Philip Carns's avatar
Philip Carns committed
205
        ABT_thread_free(&tid_array[i]);
206
   
Philip Carns's avatar
Philip Carns committed
207
208
209
    *seconds = end-arg.start_time;
    *ops_done = next_offset/size;

Philip Carns's avatar
Philip Carns committed
210
211
212
213
214
215
216
217
218
219
220
    abt_io_finalize(aid);

    /* wait on the ESs to complete */
    for(i=0; i<4; i++)
    {
        ABT_xstream_join(progress_xstreams[i]);
        ABT_xstream_free(&progress_xstreams[i]);
    }

    ABT_finalize();

221
222
    ABT_mutex_free(&mutex);
    free(tid_array);
Philip Carns's avatar
Philip Carns committed
223
    free(progress_xstreams);
Philip Carns's avatar
Philip Carns committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
277
278
279
280
281
282
283
284
285
286
287
288

    close(arg.fd);
    unlink(filename);

    return;
}

static void pthread_bench(unsigned int concurrency, size_t size, double duration,
    const char *filename, unsigned int* ops_done, double *seconds)
{
    pthread_t *id_array = NULL;
    pthread_mutex_t mutex;
    struct write_pthread_arg arg;
    off_t next_offset = 0;
    int ret;
    double end;
    int i;

    arg.fd = open(filename, O_WRONLY|O_CREAT|O_DIRECT|O_SYNC, S_IWUSR|S_IRUSR);
    if(!arg.fd)
    {
        perror("open");
        assert(0);
    }

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

    pthread_mutex_init(&mutex, NULL);

    arg.mutex = &mutex;
    arg.size = size;
    arg.next_offset = &next_offset;
    arg.duration = duration;

    arg.start_time = wtime();

    for(i=0; i<concurrency; i++)
    {
        ret = pthread_create(&id_array[i], NULL, write_pthread_bench, &arg);
        assert(ret == 0);
    }

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

    end = wtime();
    
    *seconds = end-arg.start_time;
    *ops_done = next_offset/size;

    pthread_mutex_destroy(&mutex);
    free(id_array);

    close(arg.fd);
    unlink(filename);

    return;
}

static void write_abt_bench(void *_arg)
{
289
    struct write_abt_arg* arg = _arg;
Philip Carns's avatar
Philip Carns committed
290
291
292
293
294
295
296
297
298
299
300
    off_t my_offset;
    void *buffer;
    size_t ret;

    ret = posix_memalign(&buffer, 4096, arg->size);
    assert(ret == 0);
    memset(buffer, 0, arg->size);

    double now = wtime();
    while((now-arg->start_time) < arg->duration) 
    {
301
        ABT_mutex_lock(*arg->mutex);
Philip Carns's avatar
Philip Carns committed
302
303
        my_offset = *arg->next_offset;
        (*arg->next_offset) += arg->size;
304
        ABT_mutex_unlock(*arg->mutex);
Philip Carns's avatar
Philip Carns committed
305

306
        ret = abt_io_pwrite(arg->aid, arg->fd, buffer, arg->size, my_offset);
Philip Carns's avatar
Philip Carns committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
        assert(ret == arg->size);

        now = wtime();
    }
    return;
}

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

    ret = posix_memalign(&buffer, 4096, arg->size);
    assert(ret == 0);
    memset(buffer, 0, arg->size);

    double now = wtime();
    while((now-arg->start_time) < arg->duration) 
    {
        pthread_mutex_lock(arg->mutex);
        my_offset = *arg->next_offset;
        (*arg->next_offset) += arg->size;
        pthread_mutex_unlock(arg->mutex);

        ret = pwrite(arg->fd, buffer, arg->size, my_offset);
        assert(ret == arg->size);

        now = wtime();
    }

    return(NULL);
}

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