in util/FileWriter.cpp [140:196]
bool FileWriter::syncFileRange(int64_t written, bool forced) {
#ifdef HAS_SYNC_FILE_RANGE
const WdtOptions &options = threadCtx_.getOptions();
if (options.disk_sync_interval_mb < 0) {
return true;
}
const int64_t syncIntervalBytes = options.disk_sync_interval_mb * 1024 * 1024;
writtenSinceLastSync_ += written;
if (writtenSinceLastSync_ == 0) {
// no need to sync
WVLOG(1) << "skipping syncFileRange for " << blockDetails_->fileName
<< ". Data written " << written
<< " sync forced = " << std::boolalpha << forced;
return true;
}
if (forced || writtenSinceLastSync_ > syncIntervalBytes) {
// sync_file_range with flag SYNC_FILE_RANGE_WRITE is an asynchronous
// operation. So, this is not that costly. Source :
// http://yoshinorimatsunobu.blogspot.com/2014/03/how-syncfilerange-really-works.html
int status;
{
PerfStatCollector statCollector(threadCtx_,
PerfStatReport::SYNC_FILE_RANGE);
status = sync_file_range(fd_, nextSyncOffset_, writtenSinceLastSync_,
SYNC_FILE_RANGE_WRITE);
}
if (status != 0) {
WPLOG(ERROR) << "sync_file_range() failed for " << blockDetails_->fileName
<< "fd " << fd_;
return false;
}
#ifdef HAS_POSIX_FADVISE
int ret;
if (!options.skip_fadvise) {
{
PerfStatCollector statCollector(threadCtx_, PerfStatReport::FADVISE);
ret = posix_fadvise(fd_, nextSyncOffset_, writtenSinceLastSync_,
POSIX_FADV_DONTNEED);
}
if (ret != 0) {
WPLOG(ERROR) << "posix_fadvise failed for "
<< blockDetails_->fileName
<< " " << nextSyncOffset_ << " "
<< writtenSinceLastSync_;
return false;
}
}
#endif
WVLOG(1) << "file range [" << nextSyncOffset_ << " "
<< writtenSinceLastSync_ << "] synced for file "
<< blockDetails_->fileName;
nextSyncOffset_ += writtenSinceLastSync_;
writtenSinceLastSync_ = 0;
}
#endif
return true;
}