getting MADV_SEQUENTIAL in freebsd to read ahead

47 Views Asked by At

I have a program that needs to read through many files at the same time that are filemapped. In an attempt to get the system to fetch the data before it is needed, I am calling madvise with MADV_SEQUENTIAL on each file mapping. However when it runs, this seems to have little effect. Instead the program runs at 100% cpu for a bit without any disk activity whatsoever, and then stops as it fetches the next chunks from the disk. The number of mapped files can exceed 1000 which I suspect is part of the problem. Am I doing something wrong, or is a better way of telling the system to start fetching the next chunk of each file before it is actually needed, preferably in chunks of a few MB or so.

1

There are 1 best solutions below

0
camelccc On

It seems that the man page of madvise on this platform states:

     MADV_SEQUENTIAL  Causes the VM system to depress the priority of pages
                  immediately preceding a given page when it is faulted
                  in.

The solution is therefore another thread that reads from the pages about to be accessed and storing the results in a volatile variable, to force the compiler to perform the read. This way the entire problem goes away. MADV_SEQUENTIAL does not seem to work reliably for persuading the OS to perform readahead.