tempest-io 0.0.1

TempestDB I/O Layer
Documentation
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use std::{
    collections::{HashMap, VecDeque},
    io,
    mem::ManuallyDrop,
    os,
    path::Path,
    ptr::{self, NonNull},
};

use io_uring::{IoUring, opcode, types};

use crate::{
    Completions, DirEntry, FstatHandle, Io, IoBuf, IoBufMut, OpHandle, OpenOptions, ReadHandle,
    Statx, WriteHandle,
};

fn sq_full_error() -> io::Error {
    io::Error::new(io::ErrorKind::WouldBlock, "submission queue is full")
}

struct FixedBufPool {
    base: NonNull<u8>,
    buf_size: usize,
    count: u16,
    free: VecDeque<u16>,
}

impl FixedBufPool {
    pub fn new(count: u16, buf_size: usize) -> io::Result<Self> {
        let total = buf_size * count as usize;
        let ptr = unsafe {
            libc::mmap(
                ptr::null_mut(),
                total,
                libc::PROT_READ | libc::PROT_WRITE,
                libc::MAP_ANONYMOUS | libc::MAP_PRIVATE,
                -1,
                0,
            )
        };
        if ptr == libc::MAP_FAILED {
            return Err(io::Error::last_os_error());
        }
        let base = NonNull::new(ptr as *mut u8).expect("mmap succeeded");
        let free = (0..count).collect();

        Ok(Self {
            base,
            buf_size,
            count,
            free,
        })
    }

    fn buf_index(&self, ptr: *const u8) -> Option<u16> {
        let base = self.base.as_ptr() as usize;
        let p = ptr as usize;
        let offset = p.wrapping_sub(base);
        if offset < self.buf_size * self.count as usize && offset % self.buf_size == 0 {
            Some((offset / self.buf_size) as u16)
        } else {
            None
        }
    }

    /// Acquire a fixed buffer from this buffer pool with the buffer size as set in `new()`.
    fn acquire(&mut self) -> Option<FixedBuf> {
        let index = self.free.pop_front()?;
        let offset = self.buf_size * index as usize;
        // SAFETY: we know that base + offset is in bounds of this buffer pool's memory arena
        let ptr = unsafe { self.base.add(offset) };
        Some(FixedBuf {
            ptr,
            buf_index: index,
            capacity: self.buf_size,
            len: 0,
        })
    }

    fn iovecs(&self) -> Vec<libc::iovec> {
        (0..self.count)
            .map(|i| libc::iovec {
                iov_base: unsafe { self.base.as_ptr().add(self.buf_size * i as usize) } as *mut _,
                iov_len: self.buf_size,
            })
            .collect()
    }
}

impl Drop for FixedBufPool {
    fn drop(&mut self) {
        let total = self.buf_size * self.count as usize;
        let ret = unsafe { libc::munmap(self.base.as_ptr() as *mut _, total) };
        if ret != 0 {
            error!(
                "munmap failed for fixed buffer pool: {}",
                io::Error::last_os_error()
            );
        }
    }
}

pub struct FixedBuf {
    ptr: NonNull<u8>,
    buf_index: u16,
    capacity: usize,
    len: usize,
}

unsafe impl IoBuf for FixedBuf {
    fn stable_ptr(&self) -> *const u8 {
        self.ptr.as_ptr()
    }

    fn bytes_init(&self) -> usize {
        self.len
    }

    fn bytes_total(&self) -> usize {
        self.capacity
    }
}

unsafe impl IoBufMut for FixedBuf {
    fn stable_mut_ptr(&mut self) -> *mut u8 {
        self.ptr.as_ptr()
    }

    unsafe fn set_init(&mut self, pos: usize) {
        self.len = pos
    }
}

impl Statx for libc::statx {
    fn stx_size(&self) -> u64 {
        self.stx_size
    }
}

// TODO: retrieve dynamically, possibly **per fd**, to support running on multiple drives?
// => config options, like wal_dir, sst_dir, etc:
// -> override through config, often important for ZFS / RAID configurations
// -> try BLKPBSZGET + BLKSSZGET
// -> fallback (possibly 4096)
// OR: just require the user to set it correctly.
const BLOCK_SIZE: usize = 4096;

pub struct LinuxIoConfig {
    pub entries: u32,
    pub buf_count: u16,
}

impl Default for LinuxIoConfig {
    fn default() -> Self {
        Self {
            entries: 256,
            buf_count: 256,
        }
    }
}

pub struct LinuxIo {
    ring: ManuallyDrop<IoUring>,
    pending_paths: HashMap<OpHandle, Vec<std::ffi::CString>>,
    completions: Completions,
    pool: ManuallyDrop<FixedBufPool>,
    in_flight: usize,
}

impl LinuxIo {
    pub fn new(config: LinuxIoConfig) -> io::Result<Self> {
        let ring = IoUring::builder()
            .setup_sqpoll(2000) // idle ms before kernel SQ-poll-thread sleeps
            .build(config.entries)?;

        let pool = FixedBufPool::new(config.buf_count, BLOCK_SIZE)?;
        let iovecs = pool.iovecs();
        // SAFETY: we ensure that the iovecs are valid by constructing the pool with the correct
        // block size, so that individual fixed buffers aligned with the physical block size, while
        // also ensuring the pool stays allocated until unregistering the buffers.
        unsafe { ring.submitter().register_buffers(&iovecs)? };

        Ok(Self {
            ring: ManuallyDrop::new(ring),
            pending_paths: HashMap::new(),
            completions: Vec::new(),
            pool: ManuallyDrop::new(pool),
            in_flight: 0,
        })
    }
}

impl Drop for LinuxIo {
    fn drop(&mut self) {
        // SAFETY: we manually drop the IoUring here, which is safe inside of Drop::drop
        unsafe { ManuallyDrop::drop(&mut self.ring) };
        // SAFETY: we drop the buffer pool *after* the ring, to ensure IoUring unregisters
        // references first, preventing it from ever holding on to e.g. invalid iovec pointers
        unsafe { ManuallyDrop::drop(&mut self.pool) };
    }
}

impl Io for LinuxIo {
    fn block_size(&self) -> usize {
        BLOCK_SIZE
    }

    fn now(&self) -> std::time::Duration {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("time does not go backwards")
    }

    type Fd = os::unix::io::RawFd;

    unsafe fn into_fd(result: u32) -> Self::Fd {
        result as os::unix::io::RawFd
    }

    type RegisteredBuf = FixedBuf;

    fn acquire_buf(&mut self) -> Option<Self::RegisteredBuf> {
        self.pool.acquire()
    }

    fn release_buf(&mut self, buf: Self::RegisteredBuf) {
        self.pool.free.push_back(buf.buf_index);
    }

    type Statx = libc::statx;

    fn fstat(&mut self, fd: Self::Fd, handle: OpHandle) -> io::Result<FstatHandle<Self::Statx>> {
        // SAFETY: statx gets initialized by io_uring
        let mut statx = Box::new(unsafe { std::mem::zeroed::<libc::statx>() });

        let entry = opcode::Statx::new(
            types::Fd(fd),
            b"\0".as_ptr() as _,
            std::ptr::from_mut(statx.as_mut()) as *mut _,
        )
        .flags(libc::AT_EMPTY_PATH) // we set this since we use the fd as filefd not dirfd above
        .mask(libc::STATX_SIZE) // we just require the size to be set
        .build()
        .user_data(handle.0);

        // SAFETY: StatHandle ensures that statx is kept alive until completion
        unsafe {
            self.ring
                .submission()
                .push(&entry)
                .map_err(|_| sq_full_error())?;
        }
        self.in_flight += 1;

        Ok(FstatHandle::new(statx))
    }

    fn open(&mut self, path: &Path, opts: OpenOptions, handle: OpHandle) -> io::Result<()> {
        let flags = opts.to_libc_flags();
        let mode = 0o644u32;

        let path_c = std::ffi::CString::new(path.as_os_str().as_encoded_bytes()).unwrap();
        let entry = opcode::OpenAt::new(types::Fd(libc::AT_FDCWD), path_c.as_ptr())
            .flags(flags)
            .mode(mode)
            .build()
            .user_data(handle.0);

        // SAFETY: path is kept alive in pending_paths until the CQE arrives
        unsafe {
            self.ring
                .submission()
                .push(&entry)
                .map_err(|_| sq_full_error())?;
        };
        self.in_flight += 1;

        // NB: push path *after* sq.push succeeded
        self.pending_paths.insert(handle, vec![path_c]);

        Ok(())
    }

    fn close(&mut self, fd: Self::Fd, handle: OpHandle) -> io::Result<()> {
        let entry = opcode::Close::new(types::Fd(fd))
            .build()
            .user_data(handle.0);

        // SAFETY: caller guarantees fd is a valid open file descriptor
        unsafe {
            self.ring
                .submission()
                .push(&entry)
                .map_err(|_| sq_full_error())?;
        }
        self.in_flight += 1;

        Ok(())
    }

    fn read_at<B: IoBufMut>(
        &mut self,
        fd: Self::Fd,
        mut buf: B,
        offset: u64,
        handle: OpHandle,
    ) -> Result<ReadHandle<B>, (std::io::Error, B)> {
        let entry = match self.pool.buf_index(buf.stable_mut_ptr()) {
            Some(idx) => opcode::ReadFixed::new(
                types::Fd(fd),
                buf.stable_mut_ptr(),
                buf.bytes_total() as _,
                idx,
            )
            .offset(offset)
            .build(),
            None => opcode::Read::new(types::Fd(fd), buf.stable_mut_ptr(), buf.bytes_total() as _)
                .offset(offset)
                .build(),
        }
        .user_data(handle.0);

        // SAFETY: caller guarantees fd is a valid file descriptor and
        // the read handle keeps the buffer alive, so that the pointer stays valid
        unsafe {
            if self.ring.submission().push(&entry).is_err() {
                return Err((sq_full_error(), buf));
            }
        }
        self.in_flight += 1;

        Ok(ReadHandle::new(buf))
    }

    fn write_at<B: IoBuf>(
        &mut self,
        fd: Self::Fd,
        buf: B,
        offset: u64,
        handle: OpHandle,
    ) -> Result<WriteHandle<B>, (std::io::Error, B)> {
        let entry = match self.pool.buf_index(buf.stable_ptr()) {
            Some(idx) => {
                opcode::WriteFixed::new(types::Fd(fd), buf.stable_ptr(), buf.bytes_init() as _, idx)
                    .offset(offset)
                    .build()
            }
            None => opcode::Write::new(types::Fd(fd), buf.stable_ptr(), buf.bytes_init() as _)
                .offset(offset)
                .build(),
        }
        .user_data(handle.0);

        // SAFETY: caller guarantees fd is a valid file descriptor and
        // the write handle keeps the buffer alive, so that the pointer stays valid
        unsafe {
            if self.ring.submission().push(&entry).is_err() {
                return Err((sq_full_error(), buf));
            }
        }
        self.in_flight += 1;

        Ok(WriteHandle::new(buf))
    }

    fn fsync(&mut self, fd: Self::Fd, handle: OpHandle) -> io::Result<()> {
        let entry = opcode::Fsync::new(types::Fd(fd))
            .build()
            .user_data(handle.0);

        // SAFETY: caller guarantees fd is a valid file descriptor
        unsafe {
            self.ring
                .submission()
                .push(&entry)
                .map_err(|_| sq_full_error())?;
        }
        self.in_flight += 1;

        Ok(())
    }

    fn rename(&mut self, from: &Path, to: &Path, handle: OpHandle) -> io::Result<()> {
        let from_c = std::ffi::CString::new(from.as_os_str().as_encoded_bytes()).unwrap();
        let to_c = std::ffi::CString::new(to.as_os_str().as_encoded_bytes()).unwrap();
        let entry = opcode::RenameAt::new(
            types::Fd(libc::AT_FDCWD),
            from_c.as_ptr(),
            types::Fd(libc::AT_FDCWD),
            to_c.as_ptr(),
        )
        .build()
        .user_data(handle.0);

        unsafe {
            self.ring
                .submission()
                .push(&entry)
                .map_err(|_| sq_full_error())?;
        }
        self.in_flight += 1;

        // NB: push paths *after* sq.push succeeded
        self.pending_paths.insert(handle, vec![from_c, to_c]);

        Ok(())
    }

    fn remove(&mut self, path: &Path, handle: OpHandle) -> io::Result<()> {
        let path_c = std::ffi::CString::new(path.as_os_str().as_encoded_bytes()).unwrap();
        let entry = opcode::UnlinkAt::new(types::Fd(libc::AT_FDCWD), path_c.as_ptr())
            .build()
            .user_data(handle.0);

        unsafe {
            self.ring
                .submission()
                .push(&entry)
                .map_err(|_| sq_full_error())?;
        }
        self.in_flight += 1;

        // NB: push path *after* sq.push succeeded
        self.pending_paths.insert(handle, vec![path_c]);

        Ok(())
    }

    fn poll(&mut self) -> io::Result<()> {
        // synchronize the SQ ring with the latest updates from the kernel
        self.ring.submission().sync();

        // submit any pending SQEs and drain available CQEs without blocking
        // NB: this only wakes up the kernel's SQPOLL thread if required, looking at the flags for
        // us, so we don't have to check the submitter flags here ourselves
        self.ring.submit_and_wait(0)?;

        let mut cq = self.ring.completion();
        cq.sync();

        self.in_flight -= cq.len();

        for cqe in cq {
            let handle = OpHandle(cqe.user_data());
            let result = if cqe.result() < 0 {
                Err(io::Error::from_raw_os_error(-cqe.result()))
            } else {
                Ok(cqe.result() as u32)
            };
            // drop the path from the pending list, since the operation is now completed
            self.pending_paths.remove(&handle);
            self.completions.push((handle, result));
        }

        Ok(())
    }

    fn in_flight(&self) -> usize {
        self.in_flight
    }

    fn park(&mut self) -> io::Result<()> {
        self.ring.submission().sync();
        self.ring.submit_and_wait(1).map(|_| ())
    }

    fn completions(&mut self) -> &mut Completions {
        &mut self.completions
    }

    fn list_dir(&self, path: &Path) -> io::Result<Vec<DirEntry>> {
        let mut results = Vec::new();
        for entry in std::fs::read_dir(path)? {
            let entry = entry?;
            let metadata = entry.metadata()?;
            results.push(DirEntry {
                path: entry.path(),
                is_dir: metadata.is_dir(),
            });
        }

        Ok(results)
    }

    fn create_dir_all(&self, path: &Path) -> io::Result<()> {
        std::fs::create_dir_all(path)
    }
}