programs: use sequential file access hints

This commit is contained in:
Eric Biggers 2016-08-20 14:12:41 -07:00
parent 3c1077a4b3
commit 94145eb14e
3 changed files with 23 additions and 4 deletions

View File

@ -27,6 +27,8 @@ check_function() {
check_function clock_gettime
check_function futimens
check_function futimes
check_function posix_fadvise
check_function posix_madvise
echo
echo "#endif /* _CONFIG_H */"

View File

@ -39,6 +39,16 @@
# include <sys/time.h>
#endif
#ifndef O_BINARY
# define O_BINARY 0
#endif
#ifndef O_NOFOLLOW
# define O_NOFOLLOW 0
#endif
#ifndef O_SEQUENTIAL
# define O_SEQUENTIAL 0
#endif
/* The invocation name of the program (filename component only) */
const tchar *program_invocation_name;
@ -173,13 +183,17 @@ xopen_for_read(const tchar *path, struct file_stream *strm)
if (strm->name == NULL)
return -1;
strm->fd = topen(path, O_RDONLY | O_BINARY | O_NOFOLLOW);
strm->fd = topen(path, O_RDONLY | O_BINARY | O_NOFOLLOW | O_SEQUENTIAL);
if (strm->fd < 0) {
msg_errno("Can't open %"TS" for reading", strm->name);
free(strm->name);
return -1;
}
#if defined(HAVE_POSIX_FADVISE) && (O_SEQUENTIAL == 0)
posix_fadvise(strm->fd, 0, 0, POSIX_FADV_SEQUENTIAL);
#endif
return 0;
}
@ -267,7 +281,7 @@ map_file_contents(struct file_stream *strm, u64 size)
CloseHandle((HANDLE)strm->mmap_token);
return -1;
}
#else
#else /* _WIN32 */
strm->mmap_mem = mmap(NULL, size, PROT_READ, MAP_SHARED, strm->fd, 0);
if (strm->mmap_mem == MAP_FAILED) {
strm->mmap_mem = NULL;
@ -280,7 +294,12 @@ map_file_contents(struct file_stream *strm, u64 size)
}
return -1;
}
#ifdef HAVE_POSIX_MADVISE
posix_madvise(strm->mmap_mem, size, POSIX_MADV_SEQUENTIAL);
#endif
#endif /* !_WIN32 */
strm->mmap_size = size;
return 0;
}

View File

@ -88,7 +88,6 @@ extern int wmain(int argc, wchar_t **argv);
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
# endif
# define O_NOFOLLOW 0
#else /* _WIN32 */
@ -111,7 +110,6 @@ extern int wmain(int argc, wchar_t **argv);
# define tunlink unlink
# define tutimbuf utimbuf
# define tutime utime
# define O_BINARY 0
#endif /* !_WIN32 */