[−][src]Module glommio::io
glommio::io provides data structures targeted towards File I/O.
File I/O in Glommio comes in two kinds: Buffered and Direct I/O.
Ideally an application would pick one of them according to its needs and not mix both. However if you do want to mix both, it is recommended that you do not do so in the same device: Kernel settings like I/O schedulers and merge settings that are beneficial to one of them can be detrimental to the others.
If you absolutely must use both in the same device, avoid issuing both Direct and Buffered I/O in the same file: at this point you are just trying to drive Linux crazy.
Buffered I/O
Buffered I/O will use the operating system page cache. It is ideal for simpler applications that don't want to deal with caching policies and have I/O performance as a maybe important, but definitely not crucial part of their performance story.
Disadvantages of Buffered I/O:
- Hard to know when resources are really use, which make controlled processes almost impossible (the time of write to device is detached from the file write time)
- More copies than necessary, as the data has to be copied from the device to the page
cache, from the page cache to the internal file buffers, and in abstract linear
implementations like
AsyncWriteExt
andAsyncReadExt
from user-provided buffers to the file internal buffers. - Advanced features for io_uring like Non-interrupt mode, registered files, registered buffers, will not work with Buffered I/O
- Read amplification for small random reads, as the OS is bounded by the page size (usually 4kB), even though modern NVMe devices are perfectly capable of issuing 512-byte I/O.
The main structure to deal with Buffered I/O is
BufferedFile
is targeted at random Direct I/O. Reads from and writes to it
expect a position.
Direct I/O
Direct I/O will not use the Operating System page cache and will always touch the device directly. That will always work very well for stream-based workloads (scanning a file much larger than memory, writing a buffer that will not be read from in the near future, etc) but will require a user-provided cache for good random performance.
There are advantages to using a user-provided cache: Files usually contain serialized objects and every read have to deserialize them. A user-provided cache can cache the parsed objects, among others. Still, not all applications can or want to deal with that complexity.
Disadvantages of Direct I/O:
- I/O needs to be aligned. Both the buffers and the file positions need specific alignments.
The
DmaBuffer
should hide most of that complexity, but you may still end up with heavy read amplification if you are not careful. - Without a user-provided cache, random performance can be bad.
There are two main structs that deal with File Direct I/O:
DmaFile
is targeted at random Direct I/O. Reads from and writes to it
expect a position.
DmaStreamWriter
and DmaStreamReader
perform sequential I/O and their
interface is a lot closer to other mainstream rust interfaces in std::fs.
However, despite being sequential, I/O for the two Stream structs are parallel:
DmaStreamWriter
exposes a setting for write-behind, meaning that it will keep
accepting writes to its internal buffers even with older writes are still in-flight.
In turn, DmaStreamReader
exposes a setting for read-ahead meaning it will initiate
I/O for positions you will read into the future sooner.
Structs
BufferedFile | Constructs a file that is backed by the operating system page cache |
Directory | A directory representation where asynchronous operations can be issued |
DmaFile | Constructs a file that can issue DMA operations. All access uses Direct I/O, and all operations including open and close are asynchronous. |
DmaStreamReader | Provides linear access to a |
DmaStreamReaderBuilder | Builds a DmaStreamReader, allowing linear access to a Direct I/O |
DmaStreamWriter | Provides linear access to a |
DmaStreamWriterBuilder | Builds a DmaStreamWriter, allowing linear access to a Direct I/O |
ReadResult | ReadResult encapsulates a buffer, returned by read operations like |
StreamReader | Provides linear read access to a |
StreamReaderBuilder | Builds a |
StreamWriter | Provides linear write access to a |
StreamWriterBuilder | Builds a |
Functions
remove | remove an existing file given its name |
rename | rename an existing file. |
stdin | Allows asynchronous read access to the standard input |
Type Definitions
DmaBuffer | A buffer that can be used with DmaFile. |