- /* this should flush all written pages to disk. */
- posix_fadvise(m_fd_dest, dest_pos, w, POSIX_FADV_DONTNEED);
+ written_since_last_sync += w;
+
+ if (written_since_last_sync >= 512*1024)
+ {
+ int toflush = written_since_last_sync > 2*1024*1024 ?
+ 2*1024*1024 : written_since_last_sync &~ 4095; // write max 2MB at once
+ dest_pos = lseek(m_fd_dest, 0, SEEK_CUR);
+ dest_pos -= toflush;
+ posix_fadvise(m_fd_dest, dest_pos, toflush, POSIX_FADV_DONTNEED);
+ written_since_last_sync -= toflush;
+ }