-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allocate disk space with posix_fallocate before mmapping. #2325
Allocate disk space with posix_fallocate before mmapping. #2325
Conversation
Thanks for contributing! |
Thanks for the quick review and merge :) |
FYI, This commit breaks my build on OSX 10.13.3 with the following error:
I'm temporarily working around it by adding this patch into
based on this KDE discussion: https://git.reviewboard.kde.org/r/129267/diff/2 |
Thanks for letting us know. @SergioRAgostinho what's your opinion as a Mac user? Should we include this, or rather |
Without pre-allocation it would at the very least be necessary to make sure the file size is large enough for the Either way, it makes sense to handle the file resizing / disk space allocation in a single function since it is used 4 times. Might as well call it Alternatively it can be moved to a wrapper called |
I like the wrapper function approach. We already have And getting rid of the offset argument also sounds good to me. |
I can probably find the time soon to do this, but someone else should also feel free if they can beat me to it. |
Thanks, would be great. |
Initial PR in #2341. |
If using
mmap
to write to a file without allocating disk space ahead of time, the kernel will allocate disk space as new pages are mapped. This can fail, for example when the hard disk is full. If it does fail, this causes the program to be killed by a SIGBUS signal.See also #637.
The method currently used by pcl is to seek to where the end of the file should be and write a byte. This does update the file size, but it creates a sparse file on most filesystems. There is no disk space allocated for all the seeked-over bytes, so writing to the memory mapped file still causes disk space allocation, which can fail.
This PR adds calls to
posix_fallocate
[1] before memory mapping the file to ensure that the required disk space is allocated, or the function fails cleanly.There should be no performance penalty from this change, since the disk allocation has to be done either way. In fact, allocating it all in one go may well be faster.
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html