Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can mmap and O_DIRECT be used together?

Tags:

file

linux

io

mmap

As I understand it, when you mmap a file you are basically mapping the pages from the page cache for that file directly into your process, and when you use O_DIRECT you are bypassing the page cache. Does it ever make sense to use the two together? If my understanding is right how would it even work? mmap seems to rely on the file being in the page cache and O_DIRECT seems to prevent it from going there (assuming nothing else on the system has the file open). I found this question but the answerer seems to think it's perfectly normal to do.

like image 384
Joseph Garvin Avatar asked Oct 30 '15 23:10

Joseph Garvin


People also ask

What does mmap actually do?

The mmap() function establishes a mapping between a process' address space and a stream file. The address space of the process from the address returned to the caller, for a length of len, is mapped onto a stream file starting at offset off.

What is O_direct?

What does O_DIRECT really mean? It's a hint that you want your I/O to bypass the Linux kernel's caches. What will actually happen depends on things like: Disk configuration. Whether you are opening a block device or a file in a filesystem.

What is O_sync?

O_SYNC guarantees that the call will not return before all data has been transferred to the disk (as far as the OS can tell). This still does not guarantee that the data isn't somewhere in the harddisk write cache, but it is as much as the OS can guarantee.

What does mmap return?

Return Value Upon successful completion, the mmap() function returns the address at which the mapping was placed; otherwise, it returns a value of MAP_FAILED, which has a value of 0, and sets errno to indicate the error.


1 Answers

I think it would not have a lot of sense.

O_DIRECT means that all I/O should be reflected in storage, as soon as possible (without cache).

The mapped pages is a copy of storage (file) in the memory. Reflecting every read from and write to memory would have to do some I/O and this would be huge performance hit.

like image 100
Radek Avatar answered Oct 17 '22 23:10

Radek