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
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#ifndef __APPLE__
#include <sys/syscall.h>
#endif
enum { count = 512, bytes = 160 };
struct sums {
uint64_t create, write, file_sync, publish, dir_sync, close, cleanup;
};
static void check(int rc) {
if (rc < 0) { perror("publication probe"); exit(1); }
}
static uint64_t now(void) {
#ifdef __APPLE__
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
#else
struct timespec time;
check(clock_gettime(CLOCK_MONOTONIC, &time));
return (uint64_t)time.tv_sec * 1000000000 + time.tv_nsec;
#endif
}
static int compare(const void *a, const void *b) {
uint64_t left = *(const uint64_t *)a, right = *(const uint64_t *)b;
return (left > right) - (left < right);
}
static void payload(unsigned char *out, unsigned n) {
memset(out, 0, bytes);
for (unsigned i = 0; i < 4; ++i) out[i] = (n >> (8 * i)) & 255;
}
static void write_all(int fd, const unsigned char *data) {
size_t offset = 0;
while (offset < bytes) {
ssize_t written = write(fd, data + offset, bytes - offset);
check((int)written);
if (!written) abort();
offset += written;
}
}
static void verify(int fd, unsigned n, off_t offset) {
unsigned char actual[bytes], expected[bytes];
size_t at = 0;
while (at < bytes) {
ssize_t received = pread(fd, actual + at, bytes - at, offset + at);
check((int)received);
if (!received) abort();
at += received;
}
payload(expected, n);
if (memcmp(expected, actual, bytes)) abort();
}
static int publish_exclusive(const char *source, const char *target) {
#ifdef __APPLE__
return renamex_np(source, target, RENAME_EXCL);
#else
return (int)syscall(SYS_renameat2, AT_FDCWD, source, AT_FDCWD, target, 1);
#endif
}
static void run(const char *root, const char *mode) {
char path[2048], file[2048], temp[2048];
int length = snprintf(path, sizeof path, "%s/%s", root, mode);
if (length < 0 || (size_t)length >= sizeof path) abort();
check(mkdir(path, 0700));
int dir = open(path, O_RDONLY);
check(dir);
int object = strncmp(mode, "append", 6) != 0;
int rename = !strncmp(mode, "rename", 6);
int file_sync = strstr(mode, "file") || strstr(mode, "both");
int dir_sync = strstr(mode, "parent") || strstr(mode, "both");
unsigned window = !strcmp(mode, "append-sync64") ? 64 :
!strcmp(mode, "append-sync1") ? 1 : 0;
int append = -1;
if (!object) {
snprintf(file, sizeof file, "%s/wal", path);
append = open(file, O_RDWR | O_CREAT | O_EXCL | O_APPEND, 0600);
check(append);
check(fsync(dir));
}
struct sums sum = {0};
uint64_t samples[count], all_start = now();
for (unsigned n = 0; n < count; ++n) {
unsigned char data[bytes];
payload(data, n);
uint64_t start = now(), before = start;
int fd = append;
if (object) {
snprintf(file, sizeof file, "%s/%08u", path, n);
snprintf(temp, sizeof temp, "%s/%08u.tmp", path, n);
fd = open(temp, O_RDWR | O_CREAT | O_EXCL, 0600);
check(fd);
}
uint64_t after = now(); sum.create += after - before; before = after;
write_all(fd, data);
after = now(); sum.write += after - before; before = after;
if (file_sync || (window && (n + 1) % window == 0)) check(fsync(fd));
after = now(); sum.file_sync += after - before; before = after;
if (object) check(close(fd));
after = now(); sum.close += after - before; before = after;
if (object) check(rename ? publish_exclusive(temp, file) : link(temp, file));
after = now(); sum.publish += after - before; before = after;
if (dir_sync) check(fsync(dir));
after = now(); sum.dir_sync += after - before; before = after;
if (object && !rename) check(unlink(temp));
after = now(); sum.cleanup += after - before;
samples[n] = after - start;
}
uint64_t elapsed = now() - all_start;
if (!object) check(close(append));
qsort(samples, count, sizeof *samples, compare);
printf("%s,%u,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu\n",
mode, count, (unsigned long long)elapsed,
(unsigned long long)samples[255], (unsigned long long)samples[486],
(unsigned long long)sum.create, (unsigned long long)sum.write,
(unsigned long long)sum.file_sync, (unsigned long long)sum.close,
(unsigned long long)sum.publish, (unsigned long long)sum.dir_sync,
(unsigned long long)sum.cleanup);
for (unsigned n = 0; n < count; ++n) {
if (object) snprintf(file, sizeof file, "%s/%08u", path, n);
int fd = open(file, O_RDONLY);
check(fd);
verify(fd, n, object ? 0 : (off_t)n * bytes);
check(close(fd));
if (object) check(unlink(file));
}
if (!object) check(unlink(file));
check(close(dir));
check(rmdir(path));
}
static void check_collision(const char *root) {
char source[2048], target[2048];
snprintf(source, sizeof source, "%s/source", root);
snprintf(target, sizeof target, "%s/target", root);
unsigned char data[bytes];
int a = open(source, O_RDWR | O_CREAT | O_EXCL, 0600);
int b = open(target, O_RDWR | O_CREAT | O_EXCL, 0600);
check(a); check(b);
payload(data, 1); write_all(a, data);
payload(data, 2); write_all(b, data);
if (link(source, target) != -1 || errno != EEXIST) abort();
if (publish_exclusive(source, target) != -1 || errno != EEXIST) abort();
verify(a, 1, 0); verify(b, 2, 0);
check(close(a)); check(close(b));
check(unlink(source)); check(unlink(target));
}
int main(int argc, char **argv) {
if (argc != 2 && argc != 3) return 2;
check(mkdir(argv[1], 0700));
check_collision(argv[1]);
puts("mode,records,elapsed_ns,p50_ns,p95_ns,create_ns,write_ns,file_sync_ns,close_ns,publish_ns,parent_sync_ns,cleanup_ns");
const char *modes[] = {"append-buffered", "append-sync1", "append-sync64",
"object-buffered", "object-file", "object-parent", "object-both",
"rename-buffered", "rename-both"};
size_t total = sizeof modes / sizeof *modes;
size_t offset = argc == 3 ? strtoul(argv[2], NULL, 10) % total : 0;
for (size_t n = 0; n < total; ++n) run(argv[1], modes[(n + offset) % total]);
check(rmdir(argv[1]));
}