summaryrefslogtreecommitdiffstats
path: root/extras/benchmarking/glfs-bm.c
blob: 1da2054118620880927ca03373f1281bf63d4979 (plain)
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
196
197
198
199
200
201
202
203
204
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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
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
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
389
390
/*
  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#define _GNU_SOURCE
#define __USE_FILE_OFFSET64
#define _FILE_OFFSET_BITS 64

#include <stdio.h>
#include <argp.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/xattr.h>
#include <string.h>
#include <libgen.h>
#include <errno.h>
#include <sys/time.h>

struct state {
        char need_op_write:1;
        char need_op_read:1;

        char need_iface_fileio:1;
        char need_iface_xattr:1;

        char need_mode_posix:1;

        char prefix[512];
        long int count;

        size_t block_size;

        char *specfile;

        long int io_size;
};


#define MEASURE(func, arg) measure (func, #func, arg)


void
tv_difference (struct timeval *tv_stop,
               struct timeval *tv_start,
               struct timeval *tv_diff)
{
        if (tv_stop->tv_usec < tv_start->tv_usec) {
                tv_diff->tv_usec = (tv_stop->tv_usec + 1000000) - tv_start->tv_usec;
                tv_diff->tv_sec = (tv_stop->tv_sec - 1 - tv_start->tv_sec);
        } else {
                tv_diff->tv_usec = tv_stop->tv_usec - tv_start->tv_usec;
                tv_diff->tv_sec = tv_stop->tv_sec - tv_start->tv_sec;
        }
}


void
measure (int (*func)(struct state *state),
         char *func_name, struct state *state)
{
        struct timeval tv_start, tv_stop, tv_diff;
        state->io_size = 0;
        long int count;

        gettimeofday (&tv_start, NULL);
        count = func (state);
        gettimeofday (&tv_stop, NULL);

        tv_difference (&tv_stop, &tv_start, &tv_diff);

        fprintf (stdout, "%s: count=%ld, size=%ld, time=%ld:%ld\n",
                 func_name, count, state->io_size,
                 tv_diff.tv_sec, tv_diff.tv_usec);
}


static error_t
parse_opts (int key, char *arg,
            struct argp_state *_state)
{
        struct state *state = _state->input;

        switch (key)
        {
        case 'o':
                if (strcasecmp (arg, "read") == 0) {
                        state->need_op_write = 0;
                        state->need_op_read = 1;
                } else if (strcasecmp (arg, "write") == 0) {
                        state->need_op_write = 1;
                        state->need_op_read = 0;
                } else if (strcasecmp (arg, "both") == 0) {
                        state->need_op_write = 1;
                        state->need_op_read = 1;
                } else {
                        fprintf (stderr, "unknown op: %s\n", arg);
                        return -1;
                }
                break;
        case 'i':
                if (strcasecmp (arg, "fileio") == 0) {
                        state->need_iface_fileio = 1;
                        state->need_iface_xattr = 0;
                } else if (strcasecmp (arg, "xattr") == 0) {
                        state->need_iface_fileio = 0;
                        state->need_iface_xattr = 1;
                } else if (strcasecmp (arg, "both") == 0) {
                        state->need_iface_fileio = 1;
                        state->need_iface_xattr = 1;
                } else {
                        fprintf (stderr, "unknown interface: %s\n", arg);
                        return -1;
                }
                break;
        case 'b':
        {
                size_t block_size = atoi (arg);
                if (!block_size) {
                        fprintf (stderr, "incorrect size: %s\n", arg);
                        return -1;
                }
                state->block_size = block_size;
        }
        break;
        case 's':
                state->specfile = strdup (arg);
                break;
        case 'p':
                fprintf (stderr, "using prefix: %s\n", arg);
                strncpy (state->prefix, arg, 512);
                break;
        case 'c':
        {
                long count = atol (arg);
                if (!count) {
                        fprintf (stderr, "incorrect count: %s\n", arg);
                        return -1;
                }
                state->count = count;
        }
        break;
        case ARGP_KEY_NO_ARGS:
                break;
        case ARGP_KEY_ARG:
                break;
        }

        return 0;
}

int
do_mode_posix_iface_fileio_write (struct state *state)
{
        long int i;
        int ret = -1;
        char block[state->block_size];

        for (i=0; i<state->count; i++) {
                int fd = -1;
                char filename[512];

                sprintf (filename, "%s.%06ld", state->prefix, i);

                fd = open (filename, O_CREAT|O_WRONLY, 00600);
                if (fd == -1) {
                        fprintf (stderr, "open(%s) => %s\n", filename, strerror (errno));
                        break;
                }
                ret = write (fd, block, state->block_size);
                if (ret != state->block_size) {
                        fprintf (stderr, "write (%s) => %d/%s\n", filename, ret,
                                 strerror (errno));
                        close (fd);
                        break;
                }
                close (fd);
                state->io_size += ret;
        }

        return i;
}


int
do_mode_posix_iface_fileio_read (struct state *state)
{
        long int i;
        int ret = -1;
        char block[state->block_size];

        for (i=0; i<state->count; i++) {
                int fd = -1;
                char filename[512];

                sprintf (filename, "%s.%06ld", state->prefix, i);

                fd = open (filename, O_RDONLY);
                if (fd == -1) {
                        fprintf (stderr, "open(%s) => %s\n", filename, strerror (errno));
                        break;
                }
                ret = read (fd, block, state->block_size);
                if (ret == -1) {
                        fprintf (stderr, "read(%s) => %d/%s\n", filename, ret, strerror (errno));
                        close (fd);
                        break;
                }
                close (fd);
                state->io_size += ret;
        }

        return i;
}


int
do_mode_posix_iface_fileio (struct state *state)
{
        if (state->need_op_write)
                MEASURE (do_mode_posix_iface_fileio_write, state);

        if (state->need_op_read)
                MEASURE (do_mode_posix_iface_fileio_read, state);

        return 0;
}


int
do_mode_posix_iface_xattr_write (struct state *state)
{
        long int i;
        int ret = -1;
        char block[state->block_size];
        char *dname = NULL, *dirc = NULL;
        char *bname = NULL, *basec = NULL;

        dirc = strdup (state->prefix);
        basec = strdup (state->prefix);
        dname = dirname (dirc);
        bname = basename (basec);

        for (i=0; i<state->count; i++) {
                char key[512];

                sprintf (key, "glusterfs.file.%s.%06ld", bname, i);

                ret = lsetxattr (dname, key, block, state->block_size, 0);

                if (ret != 0) {
                        fprintf (stderr, "lsetxattr (%s, %s, %p) => %s\n",
                                 dname, key, block, strerror (errno));
                        break;
                }
                state->io_size += state->block_size;
        }

        free (dirc);
        free (basec);

        return i;
}


int
do_mode_posix_iface_xattr_read (struct state *state)
{
        long int i;
        int ret = -1;
        char block[state->block_size];
        char *dname = NULL, *dirc = NULL;
        char *bname = NULL, *basec = NULL;

        dirc = strdup (state->prefix);
        basec = strdup (state->prefix);
        dname = dirname (dirc);
        bname = basename (basec);

        for (i=0; i<state->count; i++) {
                char key[512];

                sprintf (key, "glusterfs.file.%s.%06ld", bname, i);

                ret = lgetxattr (dname, key, block, state->block_size);

                if (ret < 0) {
                        fprintf (stderr, "lgetxattr (%s, %s, %p) => %s\n",
                                 dname, key, block, strerror (errno));
                        break;
                }
                state->io_size += ret;
        }

        return i;
}


int
do_mode_posix_iface_xattr (struct state *state)
{
        if (state->need_op_write)
                MEASURE (do_mode_posix_iface_xattr_write, state);

        if (state->need_op_read)
                MEASURE (do_mode_posix_iface_xattr_read, state);

        return 0;
}

int
do_mode_posix (struct state *state)
{
        if (state->need_iface_fileio)
                do_mode_posix_iface_fileio (state);

        if (state->need_iface_xattr)
                do_mode_posix_iface_xattr (state);

        return 0;
}


int
do_actions (struct state *state)
{
        if (state->need_mode_posix)
                do_mode_posix (state);

        return 0;
}

static struct argp_option options[] = {
        {"op", 'o', "OPERATIONS", 0,
         "WRITE|READ|BOTH - defaults to BOTH"},
        {"iface", 'i', "INTERFACE", 0,
         "FILEIO|XATTR|BOTH - defaults to FILEIO"},
        {"block", 'b', "BLOCKSIZE", 0,
         "<NUM> - defaults to 4096"},
        {"specfile", 's', "SPECFILE", 0,
         "absolute path to specfile"},
        {"prefix", 'p', "PREFIX", 0,
         "filename prefix"},
        {"count", 'c', "COUNT", 0,
         "number of files"},
        {0, 0, 0, 0, 0}
};

static struct argp argp = {
        options,
        parse_opts,
        "tool",
        "tool to benchmark small file performance"
};

int
main (int argc, char *argv[])
{
        struct state state = {0, };

        state.need_op_write = 1;
        state.need_op_read = 1;

        state.need_iface_fileio = 1;
        state.need_iface_xattr = 0;

        state.need_mode_posix = 1;

        state.block_size = 4096;

        strcpy (state.prefix, "tmpfile");
        state.count = 1048576;

        if (argp_parse (&argp, argc, argv, 0, 0, &state) != 0) {
                fprintf (stderr, "argp_parse() failed\n");
                return 1;
        }

        do_actions (&state);

        return 0;
}