Skip to content

Commit

Permalink
test: fix direct read/write start page calculation
Browse files Browse the repository at this point in the history
Signed-off-by: davidko <dko@suse.com>
  • Loading branch information
innobead authored and David Ko committed Jan 2, 2024
1 parent 563531d commit ae2aeda
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions integration/common/frontend.py
Expand Up @@ -13,33 +13,36 @@

def readat_direct(dev, offset, length):
pg = offset // PAGE_SIZE
pg_offset = pg * PAGE_SIZE
in_page_offset = offset % PAGE_SIZE
# either read less than a page, or whole pages

# either read less than a page, or the whole page
if in_page_offset != 0:
assert pg == (offset + length) / PAGE_SIZE
assert pg == (offset + length) // PAGE_SIZE
to_read = PAGE_SIZE
else:
assert length % PAGE_SIZE == 0
to_read = length
pg_offset = pg * PAGE_SIZE

f = os.open(dev, os.O_DIRECT | os.O_RDONLY)
try:
os.lseek(f, pg_offset, os.SEEK_SET)
ret = directio.read(f, to_read)
finally:
os.close(f)

return ret[in_page_offset : in_page_offset + length]


def writeat_direct(dev, offset, data):
pg = offset // PAGE_SIZE
pg_offset = pg * PAGE_SIZE

# don't support across page write
if len(data) == PAGE_SIZE:
assert pg == (offset + len(data) - 1) // PAGE_SIZE
else:
assert pg == (offset + len(data)) // PAGE_SIZE
pg_offset = pg * PAGE_SIZE

f = os.open(dev, os.O_DIRECT | os.O_RDWR)
m = mmap.mmap(-1, PAGE_SIZE)
Expand All @@ -53,6 +56,7 @@ def writeat_direct(dev, offset, data):
finally:
m.close()
os.close(f)

return ret


Expand Down

0 comments on commit ae2aeda

Please sign in to comment.