Make self.fd Optional. (#6855)

* Make self.fd Optional.

* Fix io_uring when missing fd.

* Compress io_uring fast path code.
This commit is contained in:
vladov 2024-10-07 22:25:34 -07:00 committed by GitHub
parent a78c96273a
commit 20a9683403
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 3 deletions

View File

@ -125,7 +125,8 @@ class BufferCopy(Runner):
else: name = f"{type(self).__name__[6:].lower()} {total_sz:8d}, {dest_device[:7]:>7s} <- {src_device[:7]:7s}"
super().__init__(colored(name, "yellow"), dest_device, 0, total_sz)
def copy(self, dest, src):
disk_supports_fast_copyout = src.device.startswith("DISK") and hasattr(src.allocator.device, 'io_uring') and hasattr(src.allocator.device, 'fd')
disk_supports_fast_copyout = src.device.startswith("DISK") and hasattr(src.allocator.device, 'io_uring') and \
getattr(src.allocator.device, 'fd', None) is not None
if src.device.startswith("DISK") and hasattr(dest.allocator, 'copy_from_disk') and disk_supports_fast_copyout and src.nbytes >= 4096:
dest.allocator.copy_from_disk(dest._buf, src._buf, src.nbytes)
elif src.device.startswith("DISK") and hasattr(dest.allocator, 'as_buffer'):

View File

@ -23,7 +23,7 @@ class DiskAllocator(Allocator):
def as_buffer(self, src:DiskBuffer): return src._buf()
def copyin(self, dest:DiskBuffer, src:memoryview): dest._buf()[:] = src
def copyout(self, dest:memoryview, src:DiskBuffer):
if OSX and hasattr(self.device, 'fd'):
if OSX and self.device.fd is not None:
# OSX doesn't seem great at mmap, this is faster
with io.FileIO(self.device.fd, "a+b", closefd=False) as fo:
fo.seek(src.offset)
@ -72,6 +72,7 @@ class DiskDevice(Compiled):
if not DiskDevice._tried_io_uring_init: self._iouring_setup()
self.size: Optional[int] = None
self.fd: Optional[int] = None
self.count = 0
super().__init__(device, DiskAllocator(self), None, None, None)
def _might_open(self, size):
@ -95,7 +96,7 @@ class DiskDevice(Compiled):
def _might_close(self):
self.count -= 1
if self.count == 0:
if hasattr(self, 'fd'): os.close(self.fd)
if self.fd is not None: os.close(self.fd)
self.size = None
def _iouring_setup(self):
DiskDevice._tried_io_uring_init = True