Skip to content
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

Fsync #6793

Merged
merged 6 commits into from
Oct 3, 2018
Merged

Fsync #6793

merged 6 commits into from
Oct 3, 2018

Conversation

carlhoerberg
Copy link
Contributor

Implementing fsync (and fdatasync) (fflush in windows) in the File class.

The spec goes through even with a simple File#flush, suggestions on how to test it better are welcome.

References:
https://linux.die.net/man/2/fsync
https://linux.die.net/man/2/fdatasync
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fflush?view=vs-2017
https://ruby-doc.org/core-2.5.1/IO.html#method-i-fsync

src/lib_c/x86_64-windows-msvc/c/io.cr Outdated Show resolved Hide resolved
Copy link
Contributor

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I knew fsync but had to lookup the man page to understand fdatasync. I wonder if we should have both syscalls as distinct methods, or if a single method, for example fsync(flush_metadata = true) wouldn't be more explicit?

@carlhoerberg
Copy link
Contributor Author

carlhoerberg commented Sep 26, 2018 via email

@RX14
Copy link
Contributor

RX14 commented Sep 26, 2018

fdatasync isn't a thing in windows, so fdatasync would just call fsync. I'd be fine with having it as a parameter of fsync.

@carlhoerberg
Copy link
Contributor Author

carlhoerberg commented Sep 26, 2018 via email

@ysbaddaden
Copy link
Contributor

I also like being close to POSIX, but Crystal aims to be a little more intuitive and simplify such details —while still having them accessible when possible.

A real thing is that we don't want aliases. Since fdatasync has no equivalent in Windows, fdatasync and fsync would end up being exactly the same. Hence my proposal to have a single fsync method with a named parameter that will be used on POSIX systems, and overlooked on Windows with a documentation to explain that.

@RX14
Copy link
Contributor

RX14 commented Sep 26, 2018

anyone searching for fdatasync will quickly fall back to searching for fsync, and then find the parameter documented.

@ysbaddaden
Copy link
Contributor

Note that POSIX AIO has a single aio_fsync function that can be passed O_SYNC or O_DSYNC meaning respectively fsync or fdatasync.

@carlhoerberg
Copy link
Contributor Author

Updated with docs and a single method with a flush_metadata parameter

Copy link
Contributor

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

src/file.cr Outdated Show resolved Hide resolved
src/file.cr Outdated Show resolved Hide resolved
@RX14
Copy link
Contributor

RX14 commented Oct 2, 2018

Perhaps flush_metadata should even be false by default...

@carlhoerberg
Copy link
Contributor Author

Don't you think it would be a bit confusing for a method called fsync to actually do a fdatasync by default?

On another note, it's only when doing small rewrites of an existing file that fdatasync has an advantage over fsync (about 30% faster when doing 1KB rewrites). Doing append only writes or large rewrites (~1MB) makes fsync and fdatasync equally fast.

@RX14 RX14 merged commit a2f52f0 into crystal-lang:master Oct 3, 2018
@RX14 RX14 added this to the 0.27.0 milestone Oct 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants