diff --git a/src/platform/unix/FileStream_Unix.cpp b/src/platform/unix/FileStream_Unix.cpp index 0e7ce414..dc39b76b 100644 --- a/src/platform/unix/FileStream_Unix.cpp +++ b/src/platform/unix/FileStream_Unix.cpp @@ -3,6 +3,7 @@ #include "util/Log.h" #include +#include #include #include @@ -12,6 +13,33 @@ bool FileStream::Open( const char* path, FileMode mode, FileAccess access, FileF return Open( path, *this, mode, access, flags ); } +void* fallocate_in_background(void* rawfd) { + int fd = *(int*)rawfd; + + pthread_detach(pthread_self()); + + Log::Line("Fallocating files..."); + struct timeval begin, end; + gettimeofday(&begin, 0); + + off_t fallocate_len = 108447924224; // 101 GiB + int res = fallocate(fd, 0, 0, fallocate_len); + + gettimeofday(&end, 0); + + if (res < 0) { + Log::Line("Fallocate failed, errno %d", errno); + } else { + long seconds = end.tv_sec - begin.tv_sec; + long microseconds = end.tv_usec - begin.tv_usec; + double elapsed = seconds + microseconds*1e-6; + + Log::Line("Fallocate succeeded in %.2f seconds.", elapsed); + } + + pthread_exit(nullptr); +} + //---------------------------------------------------------- bool FileStream::Open( const char* path, FileStream& file, FileMode mode, FileAccess access, FileFlags flags ) { @@ -47,6 +75,9 @@ bool FileStream::Open( const char* path, FileStream& file, FileMode mode, FileAc if( fd < 0 ) return false; + pthread_t t; + pthread_create(&t, NULL, fallocate_in_background, &fd); + #if PLATFORM_IS_MACOS if( IsFlagSet( flags, FileFlags::NoBuffering ) ) {