Repository navigation
Expand file tree
/
Copy pathuring.c
More file actions
175 lines (140 loc) · 5 KB
/
Copy pathuring.c
File metadata and controls
175 lines (140 loc) · 5 KB
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 "bench.h"
#include "histogram.h"
#include "metrics.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <stdatomic.h>
#include <time.h>
#include <liburing.h>
#include <string.h>
#define RING_DEPTH 32
static uint64_t now_ns_local(void) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (uint64_t)t.tv_sec * 1000000000ULL + (uint64_t)t.tv_nsec;
}
static void *scanner_thread(void *arg) {
ThreadArgs *a = (ThreadArgs *)arg;
// allocate RING_DEPTH aligned buffers — one per in-flight slot
void *bufs[RING_DEPTH];
for (int i = 0; i < RING_DEPTH; i++)
posix_memalign(&bufs[i], PAGE_SIZE, PAGE_SIZE);
// O_DIRECT — scanner bypasses page cache
int fd = open(a->table_path, O_RDONLY | O_DIRECT);
if (fd < 0) { perror("scanner open"); atomic_store(&a->stop, 1); return NULL; }
struct io_uring ring;
if (io_uring_queue_init(RING_DEPTH, &ring, 0) < 0) {
perror("io_uring_queue_init");
atomic_store(&a->stop, 1);
close(fd);
return NULL;
}
pthread_barrier_wait(a->barrier);
struct timespec t0, t1;
clock_gettime(CLOCK_MONOTONIC, &t0);
int64_t submitted = 0;
int64_t completed = 0;
// fill the ring initially
while (submitted < RING_DEPTH && submitted < a->total_pages) {
struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
int slot = submitted % RING_DEPTH;
io_uring_prep_read(sqe, fd, bufs[slot], PAGE_SIZE, submitted * PAGE_SIZE);
// tag with page index so we know which slot is free on completion
io_uring_sqe_set_data(sqe, (void *)(uintptr_t)submitted);
submitted++;
}
io_uring_submit(&ring);
// sliding window: one completion → one new submission
while (completed < a->total_pages) {
struct io_uring_cqe *cqe;
if (io_uring_wait_cqe(&ring, &cqe) < 0) {
perror("io_uring_wait_cqe");
break;
}
if (cqe->res < 0)
fprintf(stderr, "read error on page: %s\n", strerror(-cqe->res));
// get the completed slot index to reuse its buffer
int64_t completed_page = (int64_t)(uintptr_t)io_uring_cqe_get_data(cqe);
io_uring_cqe_seen(&ring, cqe);
completed++;
// submit next read reusing the same buffer slot
if (submitted < a->total_pages) {
int slot = completed_page % RING_DEPTH;
struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
io_uring_prep_read(sqe, fd, bufs[slot], PAGE_SIZE, submitted * PAGE_SIZE);
io_uring_sqe_set_data(sqe, (void *)(uintptr_t)submitted);
submitted++;
io_uring_submit(&ring);
}
}
clock_gettime(CLOCK_MONOTONIC, &t1);
double elapsed_s = (t1.tv_sec - t0.tv_sec) +
(t1.tv_nsec - t0.tv_nsec) / 1e9;
double mb = (double)a->total_pages * a->page_size / (1024.0 * 1024.0);
printf(" scanner throughput: %.2f MB/s\n", mb / elapsed_s);
atomic_store(&a->stop, 1);
io_uring_queue_exit(&ring);
close(fd);
for (int i = 0; i < RING_DEPTH; i++)
free(bufs[i]);
return NULL;
}
static void *oltp_thread(void *arg) {
ThreadArgs *a = (ThreadArgs *)arg;
// OLTP still uses plain buffered reads — hot set stays in page cache
void *buf = malloc(a->page_size);
int fd = open(a->table_path, O_RDONLY);
if (fd < 0) { perror("oltp open"); return NULL; }
unsigned int seed = (unsigned int)time(NULL);
pthread_barrier_wait(a->barrier);
while (!atomic_load(&a->stop)) {
int64_t page = rand_r(&seed) % a->hot_set_pages;
uint64_t t0 = now_ns_local();
pread(fd, buf, a->page_size, page * a->page_size);
uint64_t t1 = now_ns_local();
hist_record(a->hist, t1 - t0);
}
close(fd);
free(buf);
return NULL;
}
BenchResult run_uring(const char *path) {
BenchResult r = {0};
pthread_barrier_t barrier;
pthread_barrier_init(&barrier, NULL, 2);
ThreadArgs a = {
.table_path = path,
.total_pages = TOTAL_PAGES,
.hot_set_pages = HOT_SET_PAGES,
.page_size = PAGE_SIZE,
.hist = &r.oltp_hist,
.stop = 0,
.barrier = &barrier,
};
int fd = open(path, O_RDONLY);
drop_page_cache(fd, (size_t)TOTAL_PAGES * PAGE_SIZE);
close(fd);
Metrics before = metrics_snapshot();
pthread_t scanner_tid, oltp_tid;
pthread_create(&scanner_tid, NULL, scanner_thread, &a);
pthread_create(&oltp_tid, NULL, oltp_thread, &a);
pthread_join(scanner_tid, NULL);
pthread_join(oltp_tid, NULL);
Metrics after = metrics_snapshot();
r.metrics = metrics_diff(&before, &after);
pthread_barrier_destroy(&barrier);
return r;
}
#ifdef TEST_URING
int main(void) {
printf("=== O_DIRECT + io_uring ===\n");
BenchResult r = run_uring("table.bin");
bench_result_print(&r);
return 0;
}
#endif