How calculate, aligned, offset, and count, for, linux's, direct, storage io, through `O_DIRECT`

94 views Asked by At

I need to perform direct-io on a storage device.

This question is also valid for a buffer-pool programmer.

The alignment needed is 4096 bytes, Im doing

// Given: 7,000(count), 14,000(offset)
u64
  aligned_offset = (offset >> 12/* from 4096's width */), // value: 12288
  aligned_offset_remainder = (offset - aligned_offset), // value: 1712
  aligned_count = ((count + 4096) >> 12); // value: 8192

The problem is, aligned_count should be 12288, to get (aligned_count + aligned_offset) as 24576; instead of (aligned_count/*8192*/ + aligned_offset/*12288*/), which gives 20480, which is less than 21,000, hence the required bytes are not read full

thanks

1

There are 1 answers

1
Black Coder On

If you want your alligned_count to be 12288 and alligned_count+alligned_offset to 24576 then you should adjust calculation like instead of

alligned_count=((count+4096)>>12)

you can try this

alligned_count=((count+8192)>>12) 

I see some miss calculation in your code