From db5582f7d1c08cafa44c0f11369cc5714e8e54bc Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Tue, 10 Jan 2023 14:39:28 +0000 Subject: [PATCH] block_util: Preserve ordering of sync file backend completions For the synchronous backends efficiently preserve the order for completion requests through the use of VecDequeue. Preserving the order is not required but is beneficial as it matches the existing optimisation that looks to match completions and requests. Signed-off-by: Rob Bradford --- block_util/src/lib.rs | 13 +++++++------ block_util/src/qcow_sync.rs | 7 ++++--- block_util/src/raw_sync.rs | 13 +++++++------ block_util/src/vhdx_sync.rs | 7 ++++--- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/block_util/src/lib.rs b/block_util/src/lib.rs index 3d13003c9..b809243d0 100644 --- a/block_util/src/lib.rs +++ b/block_util/src/lib.rs @@ -25,6 +25,7 @@ use io_uring::{opcode, IoUring, Probe}; use smallvec::SmallVec; use std::alloc::{alloc_zeroed, dealloc, Layout}; use std::cmp; +use std::collections::VecDeque; use std::convert::TryInto; use std::fs::File; use std::io::{self, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write}; @@ -595,7 +596,7 @@ where iovecs: &[libc::iovec], user_data: u64, eventfd: &EventFd, - completion_list: &mut Vec<(u64, i32)>, + completion_list: &mut VecDeque<(u64, i32)>, ) -> AsyncIoResult<()> { // Convert libc::iovec into IoSliceMut let mut slices = Vec::new(); @@ -616,7 +617,7 @@ where .map_err(AsyncIoError::ReadVectored)? }; - completion_list.push((user_data, result as i32)); + completion_list.push_back((user_data, result as i32)); eventfd.write(1).unwrap(); Ok(()) @@ -628,7 +629,7 @@ where iovecs: &[libc::iovec], user_data: u64, eventfd: &EventFd, - completion_list: &mut Vec<(u64, i32)>, + completion_list: &mut VecDeque<(u64, i32)>, ) -> AsyncIoResult<()> { // Convert libc::iovec into IoSlice let mut slices = Vec::new(); @@ -649,7 +650,7 @@ where .map_err(AsyncIoError::WriteVectored)? }; - completion_list.push((user_data, result as i32)); + completion_list.push_back((user_data, result as i32)); eventfd.write(1).unwrap(); Ok(()) @@ -659,7 +660,7 @@ where &mut self, user_data: Option, eventfd: &EventFd, - completion_list: &mut Vec<(u64, i32)>, + completion_list: &mut VecDeque<(u64, i32)>, ) -> AsyncIoResult<()> { let result: i32 = { let mut file = self.file(); @@ -671,7 +672,7 @@ where }; if let Some(user_data) = user_data { - completion_list.push((user_data, result)); + completion_list.push_back((user_data, result)); eventfd.write(1).unwrap(); } diff --git a/block_util/src/qcow_sync.rs b/block_util/src/qcow_sync.rs index 1202fba0b..5d9c042a7 100644 --- a/block_util/src/qcow_sync.rs +++ b/block_util/src/qcow_sync.rs @@ -5,6 +5,7 @@ use crate::async_io::{AsyncIo, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult}; use crate::AsyncAdaptor; use qcow::{QcowFile, RawFile, Result as QcowResult}; +use std::collections::VecDeque; use std::fs::File; use std::io::{Seek, SeekFrom}; use std::sync::{Arc, Mutex, MutexGuard}; @@ -37,7 +38,7 @@ impl DiskFile for QcowDiskSync { pub struct QcowSync { qcow_file: Arc>, eventfd: EventFd, - completion_list: Vec<(u64, i32)>, + completion_list: VecDeque<(u64, i32)>, } impl QcowSync { @@ -46,7 +47,7 @@ impl QcowSync { qcow_file, eventfd: EventFd::new(libc::EFD_NONBLOCK) .expect("Failed creating EventFd for QcowSync"), - completion_list: Vec::new(), + completion_list: VecDeque::new(), } } } @@ -98,6 +99,6 @@ impl AsyncIo for QcowSync { } fn next_completed_request(&mut self) -> Option<(u64, i32)> { - self.completion_list.pop() + self.completion_list.pop_front() } } diff --git a/block_util/src/raw_sync.rs b/block_util/src/raw_sync.rs index b9b87e0a6..b85bc14f5 100644 --- a/block_util/src/raw_sync.rs +++ b/block_util/src/raw_sync.rs @@ -5,6 +5,7 @@ use crate::async_io::{ AsyncIo, AsyncIoError, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult, DiskTopology, }; +use std::collections::VecDeque; use std::fs::File; use std::io::{Seek, SeekFrom}; use std::os::unix::io::{AsRawFd, RawFd}; @@ -44,7 +45,7 @@ impl DiskFile for RawFileDiskSync { pub struct RawFileSync { fd: RawFd, eventfd: EventFd, - completion_list: Vec<(u64, i32)>, + completion_list: VecDeque<(u64, i32)>, } impl RawFileSync { @@ -52,7 +53,7 @@ impl RawFileSync { RawFileSync { fd, eventfd: EventFd::new(libc::EFD_NONBLOCK).expect("Failed creating EventFd for RawFile"), - completion_list: Vec::new(), + completion_list: VecDeque::new(), } } } @@ -81,7 +82,7 @@ impl AsyncIo for RawFileSync { return Err(AsyncIoError::ReadVectored(std::io::Error::last_os_error())); } - self.completion_list.push((user_data, result as i32)); + self.completion_list.push_back((user_data, result as i32)); self.eventfd.write(1).unwrap(); Ok(()) @@ -106,7 +107,7 @@ impl AsyncIo for RawFileSync { return Err(AsyncIoError::WriteVectored(std::io::Error::last_os_error())); } - self.completion_list.push((user_data, result as i32)); + self.completion_list.push_back((user_data, result as i32)); self.eventfd.write(1).unwrap(); Ok(()) @@ -120,7 +121,7 @@ impl AsyncIo for RawFileSync { } if let Some(user_data) = user_data { - self.completion_list.push((user_data, result)); + self.completion_list.push_back((user_data, result)); self.eventfd.write(1).unwrap(); } @@ -128,6 +129,6 @@ impl AsyncIo for RawFileSync { } fn next_completed_request(&mut self) -> Option<(u64, i32)> { - self.completion_list.pop() + self.completion_list.pop_front() } } diff --git a/block_util/src/vhdx_sync.rs b/block_util/src/vhdx_sync.rs index 678aedeba..a199e06e0 100644 --- a/block_util/src/vhdx_sync.rs +++ b/block_util/src/vhdx_sync.rs @@ -4,6 +4,7 @@ use crate::async_io::{AsyncIo, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult}; use crate::AsyncAdaptor; +use std::collections::VecDeque; use std::fs::File; use std::sync::{Arc, Mutex, MutexGuard}; use vhdx::vhdx::{Result as VhdxResult, Vhdx}; @@ -37,7 +38,7 @@ impl DiskFile for VhdxDiskSync { pub struct VhdxSync { vhdx_file: Arc>, eventfd: EventFd, - completion_list: Vec<(u64, i32)>, + completion_list: VecDeque<(u64, i32)>, } impl VhdxSync { @@ -45,7 +46,7 @@ impl VhdxSync { Ok(VhdxSync { vhdx_file, eventfd: EventFd::new(libc::EFD_NONBLOCK)?, - completion_list: Vec::new(), + completion_list: VecDeque::new(), }) } } @@ -97,6 +98,6 @@ impl AsyncIo for VhdxSync { } fn next_completed_request(&mut self) -> Option<(u64, i32)> { - self.completion_list.pop() + self.completion_list.pop_front() } }