File: disk.c

package info (click to toggle)
raft 0.22.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: ansic: 37,539; makefile: 264; sh: 77; python: 22
file content (240 lines) | stat: -rw-r--r-- 6,419 bytes parent folder | download | duplicates (2)
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
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "disk.h"
#include "disk_options.h"
#include "disk_parse.h"
#include "disk_uring.h"
#include "fs.h"
#include "timer.h"

/* Allocate a buffer of the given size. */
static void allocBuffer(struct iovec *iov, size_t size)
{
    unsigned i;

    iov->iov_len = size;
    iov->iov_base = aligned_alloc(iov->iov_len, iov->iov_len);
    assert(iov->iov_base != NULL);

    /* Populate the buffer with some fixed data. */
    for (i = 0; i < size; i++) {
        *(((uint8_t *)iov->iov_base) + i) = i % 128;
    }
}

/* Prepare the file or device to write to. */
static int openFile(struct diskOptions *opts,
                    struct FsFileInfo *info,
                    int *fd,
                    char **path)
{
    unsigned n = opts->size / (unsigned)opts->buf;
    int rv;

    rv = FsFileInfo(opts->dir, info);
    if (rv != 0) {
        printf("file info '%s': %s\n", opts->dir, strerror(errno));
        return -1;
    }

    if (info->type == FS_TYPE_DEVICE) {
        rv = FsOpenBlockDevice(opts->dir, fd);
    } else {
        rv = FsCreateTempFile(opts->dir, n * opts->buf, path, fd);
        if (rv == 0) {
            rv = FsFileInfo(*path, info);
        }
    }
    if (rv != 0) {
        return -1;
    }

    return 0;
}

static int closeFile(struct FsFileInfo *info, int fd, char *path)
{
    int rv;

    if (info->type != FS_TYPE_DEVICE) {
        rv = FsRemoveTempFile(path, fd);
    } else {
        rv = close(fd);
    }
    if (rv != 0) {
        return -1;
    }

    return 0;
}

static void reportLatency(struct benchmark *benchmark,
                          struct histogram *histogram)
{
    struct metric *m;
    m = BenchmarkGrow(benchmark, METRIC_KIND_LATENCY);
    MetricFillHistogram(m, histogram);
}

static void reportThroughput(struct benchmark *benchmark,
                             unsigned long duration,
                             unsigned size)
{
    struct metric *m;
    unsigned megabytes = size / (1024 * 1024); /* N megabytes written */
    m = BenchmarkGrow(benchmark, METRIC_KIND_THROUGHPUT);
    MetricFillThroughput(m, megabytes, duration);
}

int DiskRun(int argc, char *argv[], struct report *report)
{
    struct diskOptions opts;
    struct Profiler profiler;
    struct FsFileInfo info;
    struct benchmark *benchmark;
    struct timer timer;
    struct histogram histogram;
    struct iovec iov;
    char *name;
    char *path;
    int fd;
    unsigned long duration;
    unsigned i;
    unsigned n;

    int rv;

    DiskParse(argc, argv, &opts);

    rv = openFile(&opts, &info, &fd, &path);
    if (rv != 0) {
        return -1;
    }

    ProfilerInit(&profiler, &info);

    if (opts.perf) {
        rv = ProfilerPerf(&profiler);
        if (rv != 0) {
            return -1;
        }
    }

    for (i = 0; i < opts.n_traces; i++) {
        ProfilerTrace(&profiler, opts.traces[i]);
    }

    allocBuffer(&iov, opts.buf);

    HistogramInit(&histogram, info.buckets, info.resolution);

    TimerStart(&timer);

    n = opts.size / (unsigned)opts.buf;
    rv = DiskWriteUsingUring(fd, &iov, n, &profiler, &histogram);

    duration = TimerStop(&timer);

    free(iov.iov_base);

    if (rv != 0) {
        return -1;
    }

    rv = closeFile(&info, fd, path);
    if (rv != 0) {
        return -1;
    }

    /* 262144 is the maximum buffer size where no context switches happen,
     * presumably because io_uring inlines smaller requests and uses the
     * threadpool for larger ones. */
    if (opts.perf && profiler.switches != 0 &&
        info.driver != FS_DRIVER_GENERIC) {
        printf("Error: unexpected context switches: %u\n", profiler.switches);
        return -1;
    }

    /* Only report disk benchmarks if kernel sub-systems performance measurement
     * is disabled.
     *
     * In CI we run the "raft-benchmark disk" command twice: once with kernel
     * sub-systems performance measurement enabled, to report raw block/nvme
     * metrics, and once with kernel sub-systems performance measurement
     * disabled, to report the actual end-to-end metrics. That's because
     * enabling kernel sub-systems performance measurement has a noticeable
     * (albeit low) overhead.
     */
    if (!opts.perf) {
        rv = asprintf(&name, "disk:%zu", opts.buf);
        assert(rv > 0);
        assert(name != NULL);

        benchmark = ReportGrow(report, name);
        reportLatency(benchmark, &histogram);
        reportThroughput(benchmark, duration, opts.size);
    }

    HistogramClose(&histogram);

    if (opts.perf && info.driver != FS_DRIVER_GENERIC) {
        struct ProfilerDataSource *data;
        const char *system;

        system = "block";
        data = &profiler.block;
        rv = asprintf(&name, "disk:%s:%zu", system, opts.buf);
        assert(rv > 0);
        assert(name != NULL);

        HistogramInit(&histogram, info.buckets, info.resolution);

        if (data->n_commands != n && info.driver != FS_DRIVER_GENERIC) {
            printf("Error: unexpected commands: %u\n", data->n_commands);
            return -1;
        }
        for (i = 0; i < data->n_commands; i++) {
            assert(data->commands[i].duration > 0);
            HistogramCount(&histogram, data->commands[i].duration);
        }

        benchmark = ReportGrow(report, name);
        reportLatency(benchmark, &histogram);
        HistogramClose(&histogram);

        if (info.driver == FS_DRIVER_NVME) {
            system = "nvme";
            data = &profiler.block;
            rv = asprintf(&name, "disk:%s:%zu", system, opts.buf);
            assert(rv > 0);
            assert(name != NULL);

            HistogramInit(&histogram, info.buckets, info.resolution);

            if (data->n_commands != n) {
                printf("Error: unexpected commands: %u\n", data->n_commands);
                return -1;
            }
            for (i = 0; i < data->n_commands; i++) {
                assert(data->commands[i].duration > 0);
                HistogramCount(&histogram, data->commands[i].duration);
            }

            benchmark = ReportGrow(report, name);
            reportLatency(benchmark, &histogram);
            HistogramClose(&histogram);
        }
    }

    ProfilerClose(&profiler);

    return 0;
}