diff --git a/plugins/in_tail/tail_file.c b/plugins/in_tail/tail_file.c index 06c51d7d68d..9b36bfbdc0c 100644 --- a/plugins/in_tail/tail_file.c +++ b/plugins/in_tail/tail_file.c @@ -1698,15 +1698,6 @@ int flb_tail_file_chunk(struct flb_tail_file *file) file_buffer_capacity = (file->buf_size - file->buf_len) - 1; } - #ifdef __linux__ - if (ctx->file_cache_advise) { - if (posix_fadvise(file->fd, 0, 0, POSIX_FADV_DONTNEED) == -1) { - flb_errno(); - flb_plg_error(ctx->ins, "error during posix_fadvise"); - } - } - #endif - read_size = file_buffer_capacity; if (file->decompression_context != NULL) { @@ -1822,6 +1813,15 @@ int flb_tail_file_chunk(struct flb_tail_file *file) file->buf_len -= processed_bytes; file->buf_data[file->buf_len] = '\0'; +#ifdef __linux__ + /* Evict only the already-processed byte range from the page cache + * so that kernel readahead for upcoming reads is preserved. */ + if (ctx->file_cache_advise && file->stream_offset > 0) { + posix_fadvise(file->fd, 0, file->stream_offset, + POSIX_FADV_DONTNEED); + } +#endif + #ifdef FLB_HAVE_SQLDB if (file->config->db) { flb_tail_db_file_offset(file, file->config);