From 825cbbaee78eb17f1381a6be2f36b8023ac433da Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Wed, 19 May 2010 22:06:46 -0400 Subject: [PATCH] liblttdvfs: put fadvise hint after sync writeback On Wed, 19 May 2010, Mathieu Desnoyers wrote: > > A faced a small counter-intuitive fadvise behavior though. > > posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED); > > only seems to affect the parts of a file that already exist. POSIX_FADV_DONTNEED does not have _any_ long-term behavior. So when you do a posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED); it only affects the pages that are there right now, it has no effect on any future actions. > So after each splice() that appends to the file, I have to call fadvise > again. I would have expected the "0" len parameter to tell the kernel to > apply the hint to the whole file, even parts that will be added in the > future. It's not a hint about future at all. It's a "throw current pages away". I would also suggest against doing that kind of thing in a streaming write situation. The behavior for dirty page writeback is _not_ welldefined, and if you do POSIX_FADV_DONTNEED, I would suggest you do it as part of that writeback logic, ie you do it only on ranges that you have just waited on. IOW, in my example, you'd couple the sync_file_range(fd, (index-1)*BUFSIZE, BUFSIZE, +SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER); with a posix_fadvise(fd, (index-1)*BUFSIZE, BUFSIZE, POSIX_FADV_DONTNEED); afterwards to throw out the pages that you just waited for. Linus Signed-off-by: Mathieu Desnoyers --- liblttd/liblttdvfs.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/liblttd/liblttdvfs.c b/liblttd/liblttdvfs.c index 6e28c9b..74a5766 100644 --- a/liblttd/liblttdvfs.c +++ b/liblttd/liblttdvfs.c @@ -185,21 +185,6 @@ int liblttdvfs_on_read_subbuffer(struct liblttd_callbacks *data, struct fd_pair goto write_end; } len -= ret; - /* - * Give hints to the kernel about how we access the file: - * POSIX_FADV_DONTNEED : we won't re-access data in a near - * future after we write it. - * We need to call fadvise again after the file grows because - * the kernel does not seem to apply fadvise to non-existing - * parts of the file. - */ - ret = posix_fadvise(outfd, 0, 0, POSIX_FADV_DONTNEED); - if (ret != 0) { - perror("fadvise"); - /* Just a hint, not critical. Continue. */ - ret = 0; - } - /* This won't block, but will start writeout asynchronously */ sync_file_range(outfd, pair->offset, ret, SYNC_FILE_RANGE_WRITE); @@ -209,14 +194,31 @@ write_end: /* * This does a blocking write-and-wait on any page that belongs to the * subbuffer prior to the one we just wrote. + * Don't care about error values, as these are just hints and ways to + * limit the amount of page cache used. */ - if (orig_offset >= pair->max_sb_size) + if (orig_offset >= pair->max_sb_size) { sync_file_range(outfd, orig_offset - pair->max_sb_size, pair->max_sb_size, SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER); - + /* + * Give hints to the kernel about how we access the file: + * POSIX_FADV_DONTNEED : we won't re-access data in a near + * future after we write it. + * We need to call fadvise again after the file grows because + * the kernel does not seem to apply fadvise to non-existing + * parts of the file. + * Call fadvise _after_ having waited for the page writeback to + * complete because the dirty page writeback semantic is not + * well defined. So it can be expected to lead to lower + * throughput in streaming. + */ + posix_fadvise(outfd, orig_offset - pair->max_sb_size, + pair->max_sb_size, POSIX_FADV_DONTNEED); + } + return ret; } -- 2.34.1