Struct glommio::io::DmaFile[][src]

pub struct DmaFile { /* fields omitted */ }
Expand description

An asynchronously accessed Direct Memory Access (DMA) file.

All access uses Direct I/O, and all operations including open and close are asynchronous (with some exceptions noted). Reads from and writes to this struct must come and go through the DmaBuffer type, which will buffer them in memory; on calling DmaFile::write_at and DmaFile::read_at, the buffers will be passed to the OS to asynchronously write directly to the file on disk, bypassing page caches.

See the module-level documentation for more details and examples.

Implementations

align a value up to the minimum alignment needed to access this file

align a value down to the minimum alignment needed to access this file

Returns true if the DmaFiles represent the same file on the underlying device.

Files are considered to be the same if they live in the same file system and have the same Linux inode. Note that based on this rule a symlink is not considered to be the same file.

Files will be considered to be the same if:

  • A file is opened multiple times (different file descriptors, but same file!)
  • they are hard links.

Examples

use glommio::{io::DmaFile, LocalExecutor};
use std::os::unix::io::AsRawFd;

let ex = LocalExecutor::default();
ex.run(async {
    let mut wfile = DmaFile::create("myfile.txt").await.unwrap();
    let mut rfile = DmaFile::open("myfile.txt").await.unwrap();
    // Different objects (OS file descriptors), so they will be different...
    assert_ne!(wfile.as_raw_fd(), rfile.as_raw_fd());
    // However they represent the same object.
    assert!(wfile.is_same(&rfile));
    wfile.close().await;
    rfile.close().await;
});

Allocates a buffer that is suitable for using to write to this file.

Similar to create() in the standard library, but returns a DMA file

Similar to open() in the standard library, but returns a DMA file

Write the buffer in buf to a specific position in the file.

It is expected that the buffer and the position be properly aligned for Direct I/O. In most platforms that means 4096 bytes. There is no write_at_aligned, since a non aligned write would require a read-modify-write.

Buffers should be allocated through alloc_dma_buffer, which guarantees proper alignment, but alignment on position is still up to the user.

This method acquires ownership of the buffer so the buffer can be kept alive while the kernel has it.

Note that it is legal to return fewer bytes than the buffer size. That is the situation, for example, when the device runs out of space (See the man page for write(2) for details)

Examples

use glommio::{io::DmaFile, LocalExecutor};

let ex = LocalExecutor::default();
ex.run(async {
    let file = DmaFile::create("test.txt").await.unwrap();

    let mut buf = file.alloc_dma_buffer(4096);
    let res = file.write_at(buf, 0).await.unwrap();
    assert!(res <= 4096);
    file.close().await.unwrap();
});

Reads from a specific position in the file and returns the buffer.

The position must be aligned to for Direct I/O. In most platforms that means 512 bytes.

Reads into buffer in buf from a specific position in the file.

It is not necessary to respect the O_DIRECT alignment of the file, and this API will internally convert the positions and sizes to match, at a cost.

If you can guarantee proper alignment, prefer Self::read_at_aligned instead

Submit many reads and process the results in a stream-like fashion via a ReadManyResult.

This API will optimistically coalesce and deduplicate IO requests such that two overlapping or adjacent reads will result in a single IO request. This is transparent for the consumer, you will still receive individual ReadResults corresponding to what you asked for.

The first argument is an iterator of IoVec. The last two arguments control how aggressive the IO coalescing should be:

  • max_merged_buffer_size controls how large a merged IO request can be. A value of 0 disables merging completely.
  • max_read_amp is optional and defines the maximum read amplification you are comfortable with. If two read requests are separated by a distance less than this value, they will be merged. A value None disables all read amplification limitation.

It is not necessary to respect the O_DIRECT alignment of the file, and this API will internally convert the positions and sizes to match.

Issues fdatasync for the underlying file, instructing the OS to flush all writes to the device, providing durability even if the system crashes or is rebooted.

As this is a DMA file, the OS will not be caching this file; however, there may be caches on the drive itself.

pre-allocates space in the filesystem to hold a file at least as big as the size argument.

Hint to the OS the size of increase of this file, to allow more efficient allocation of blocks.

Allocating blocks at the filesystem level turns asynchronous writes into threaded synchronous writes, as we need to first find the blocks to host the file.

If the extent is larger, that means many blocks are allocated at a time. For instance, if the extent size is 1MB, that means that only 1 out of 4 256kB writes will be turned synchronous. Combined with diligent use of fallocate we can greatly minimize context switches.

It is important not to set the extent size too big. Writes can fail otherwise if the extent can’t be allocated.

Truncates a file to the specified size.

Warning: synchronous operation, will block the reactor

rename this file.

Warning: synchronous operation, will block the reactor

remove this file.

The file does not have to be closed to be removed. Removing removes the name from the filesystem but the file will still be accessible for as long as it is open.

Warning: synchronous operation, will block the reactor

Returns the size of a file, in bytes

Closes this DMA file.

Returns an Option containing the path associated with this open directory, or None if there isn’t one.

Convenience method that closes a DmaFile wrapped inside an Rc

Trait Implementations

Extracts the raw file descriptor. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more