From e8955cc64f0f5da0e90ab62fa3dd277a19fe3abf Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Tue, 18 Apr 2023 12:11:33 -0400 Subject: [PATCH] Build fix: add a noop compat wrapper for posix_fadvise MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This allows building on platforms without posix_fadvise. Change-Id: I13fd6404cd02eb8038a97693db27d9619246401d Signed-off-by: Michael Jeanson Signed-off-by: Jérémie Galarneau --- configure.ac | 2 +- src/common/io-hint.cpp | 51 +++++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/configure.ac b/configure.ac index bd6f45d5d..7257cdeff 100644 --- a/configure.ac +++ b/configure.ac @@ -315,7 +315,7 @@ AC_CHECK_FUNCS([ \ mkdir munmap putenv realpath rmdir socket strchr strcspn strdup \ strncasecmp strndup strnlen strpbrk strrchr strstr strtol strtoul \ strtoull dirfd gethostbyname2 getipnodebyname epoll_create1 \ - sched_getcpu sysconf sync_file_range getrandom + sched_getcpu sysconf sync_file_range getrandom posix_fadvise ]) # Check for pthread_setname_np and pthread_getname_np diff --git a/src/common/io-hint.cpp b/src/common/io-hint.cpp index 506f15de6..2bb924674 100644 --- a/src/common/io-hint.cpp +++ b/src/common/io-hint.cpp @@ -54,7 +54,7 @@ int flush_range_async(int fd, off_t offset, off_t nbytes) } } /* namespace */ -#else +#else /* HAVE_SYNC_FILE_RANGE */ namespace { /* @@ -109,7 +109,44 @@ int flush_range_async(int fd, off_t offset, off_t nbytes) return flush_range(fd, offset, nbytes, MS_ASYNC); } } /* namespace */ -#endif +#endif /* !HAVE_SYNC_FILE_RANGE */ + +/* + * Use posix_fadvise when available. + */ +#ifdef HAVE_POSIX_FADVISE +namespace { +int hint_dont_need(int fd, off_t offset, off_t nbytes) +{ + const int ret = posix_fadvise(fd, offset, nbytes, POSIX_FADV_DONTNEED); + if (ret && ret != -ENOSYS) { + PERROR("Failed to mark region as DONTNEED with posix_fadvise: fd=%i, offset=%" PRIu64 + ", nbytes=%" PRIu64, + fd, + static_cast(offset), + static_cast(nbytes)); + errno = ret; + } + + return ret; +} +} /* namespace */ + +#else /* HAVE_POSIX_FADVISE */ + +/* + * Generic noop compat for platforms wihtout posix_fadvise, this is acceptable + * since we are only giving a hint to the kernel. + */ +namespace { +int hint_dont_need(int fd __attribute__((unused)), + off_t offset __attribute__((unused)), + off_t nbytes __attribute__((unused))) +{ + return 0; +} +} /* namespace */ +#endif /* !HAVE_POSIX_FADVISE */ /* * Give a hint to the kernel that we won't need the data at the specified range @@ -135,15 +172,7 @@ void lttng::io::hint_flush_range_dont_need_sync(int fd, off_t offset, off_t nbyt * defined. So it can be expected to lead to lower throughput in * streaming. */ - const int ret = posix_fadvise(fd, offset, nbytes, POSIX_FADV_DONTNEED); - if (ret && ret != -ENOSYS) { - PERROR("Failed to mark region as DONTNEED with posix_fadvise: fd=%i, offset=%" PRIu64 - ", nbytes=%" PRIu64, - fd, - static_cast(offset), - static_cast(nbytes)); - errno = ret; - } + hint_dont_need(fd, offset, nbytes); } /* -- 2.34.1