Force block device (NBD) to bypass the buffer cache

Posted on

Force block device (NBD) to bypass the buffer cache – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about linux, cache, block-device, , .

I’d like to know if there is a way to force a block device to bypass the linux buffer cache (ie direct IO) and pass the requests directly to the underlying layer. I know one can open a file using O_DIRECT flag to achieve this, but my program is not the user of the block device, a file system is. And so far, I have not had any luck telling various FSes to use direct IO.

In summary, I want the raw device functionality without using raw devices (since they’re
deprecated in my distro, Fedora).

If this is of any importance, my block device is a Network Block Device.
Any help would be appreciated.

Solution :

You can achieve this by mounting with the sync-option.

From the mount(8)-man page:

   sync   All I/O to the filesystem should be done synchronously. In  case
          of  media  with  limited number of write cycles (e.g. some flash
          drives) "sync" may cause life-cycle shortening.

I don’t think it’s possible to have direct I/O without using either O_DIRECT or raw devices.
Using sync option on mount won’t bypass the buffer cache either.

Because you can’t set it from the filesystem, you’d have to modify your program to either use
O_DIRECT or mmap() and madvise() to get similar behaviour.

“I know one can open a file using O_DIRECT flag to achieve this, but my program is not the user of the block device, a file system is.”

Don’t worry about the your program is not the user, O_DIRECT works for open() which is file
based anyway.

Leave a Reply

Your email address will not be published.