Skip to content

Commit

Permalink
Trimmable buffer + expose some more DmaFile internals (#617)
Browse files Browse the repository at this point in the history
* Expose the inode / dev major / dev minor for the opened file

* Expose DmaBuffer::trim_to_size

This lets us overallocate a larger-than-needed buffer and then trim it
down to size before writing.

---------

Co-authored-by: Glauber Costa <[email protected]>
  • Loading branch information
vlovich and glommer authored Nov 22, 2023
1 parent 7c7c366 commit 9288b52
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
28 changes: 28 additions & 0 deletions glommio/src/io/dma_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,4 +1555,32 @@ pub(crate) mod test {
.await
.expect_err("O_TMPFILE requires opening with write permissions");
});

dma_file_test!(resize_dma_buf, path, _k, {
let file = OpenOptions::new()
.create_new(true)
.read(true)
.write(true)
.tmpfile(true)
.dma_open(path)
.await
.expect("Failed to open file");

let alignment =
(file.alignment()).max(file.stat().await.unwrap().fs_cluster_size.into()) as usize;

let mut buffer = file.alloc_dma_buffer(2 * alignment);
buffer.as_bytes_mut()[0..alignment].fill(1);
buffer.as_bytes_mut()[alignment..].fill(2);
buffer.trim_to_size(alignment);

assert_eq!(alignment, file.write_at(buffer, 0).await.unwrap());

let read = file.read_at_aligned(0, 2 * alignment).await.unwrap();
assert_eq!(read.len(), alignment);
assert!(read.iter().all(|&b| b == 1));

let stat = file.stat().await.unwrap();
assert_eq!(stat.file_size, alignment as u64, "{:?}", stat);
});
}
6 changes: 5 additions & 1 deletion glommio/src/sys/dma_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ impl DmaBuffer {
}
}

pub(crate) fn trim_to_size(&mut self, newsize: usize) {
/// Reduce the length of this buffer to be `newsize`. This value must be <= the current
/// [`len`](#method.len) and is a destructive operation (length cannot be increased).
/// NOTE: When using this with DmaFile, you have to make sure that `newsize` is properly
/// aligned or the write will fail due to O_DIRECT requirements.
pub fn trim_to_size(&mut self, newsize: usize) {
assert!(newsize <= self.size);
self.size = newsize;
}
Expand Down

0 comments on commit 9288b52

Please sign in to comment.