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

fallocate plot files in background upon creation #46

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/platform/unix/FileStream_Unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "util/Log.h"

#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>

Expand All @@ -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 )
{
Expand Down Expand Up @@ -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 ) )
{
Expand Down