• Miquel van Smoorenburg's avatar
    [PATCH] mark_page_accessed() for read()s on non-page boundaries · 21adf7ac
    Miquel van Smoorenburg authored
    When reading a (partial) page from disk using read(), the kernel only marks
    the page as "accessed" if the read started at a page boundary.  This means
    that files that are accessed randomly at non-page boundaries (usually
    database style files) will not be cached properly.
    
    The patch below uses the readahead state instead.  If a page is read(), it
    is marked as "accessed" if the previous read() was for a different page,
    whatever the offset in the page.
    
    Testing results:
    
    
    - Boot kernel with mem=128M
    
    - create a testfile of size 8 MB on a partition. Unmount/mount.
    
    - then generate about 10 MB/sec streaming writes
    
    	for i in `seq 1 1000`
    	do
    		dd if=/dev/zero of=junkfile.$i bs=1M count=10
    		sync
    		cat junkfile.$i > /dev/null
    		sleep 1
    	done
    
    - use an application that reads 128 bytes 64000 times from a
      random offset in the 64 MB testfile.
    
    1. Linux 2.6.10-rc3 vanilla, no streaming writes:
    
    # time ~/rr testfile
    Read 128 bytes 64000 times
    ~/rr testfile  0.03s user 0.22s system 5% cpu 4.456 total
    
    2. Linux 2.6.10-rc3 vanilla, streaming writes:
    
    # time ~/rr testfile
    Read 128 bytes 64000 times
    ~/rr testfile  0.03s user 0.16s system 2% cpu 7.667 total
    # time ~/rr testfile
    Read 128 bytes 64000 times
    ~/rr testfile  0.03s user 0.37s system 1% cpu 23.294 total
    # time ~/rr testfile
    Read 128 bytes 64000 times
    ~/rr testfile  0.02s user 0.99s system 1% cpu 1:11.52 total
    # time ~/rr testfile
    Read 128 bytes 64000 times
    ~/rr testfile  0.03s user 0.21s system 2% cpu 10.273 total
    
    3. Linux 2.6.10-rc3 with read-page-access.patch , streaming writes:
    
    # time ~/rr testfile
    Read 128 bytes 64000 times
    ~/rr testfile  0.02s user 0.21s system 3% cpu 7.634 total
    # time ~/rr testfile
    Read 128 bytes 64000 times
    ~/rr testfile  0.04s user 0.22s system 2% cpu 9.588 total
    # time ~/rr testfile
    Read 128 bytes 64000 times
    ~/rr testfile  0.02s user 0.12s system 24% cpu 0.563 total
    # time ~/rr testfile
    Read 128 bytes 64000 times
    ~/rr testfile  0.03s user 0.13s system 98% cpu 0.163 total
    
    As expected, with the read-page-access.patch, the kernel keeps the 8 MB
    testfile cached as expected, while without it, it doesn't.
    
    So this is useful for workloads where one smallish (wrt RAM) file is read
    randomly over and over again (like heavily used database indexes), while
    other I/O is going on.  Plain 2.6 caches those files poorly, if the app
    uses plain read().
    Signed-Off-By: default avatarMiquel van Smoorenburg <miquels@cistron.nl>
    Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
    21adf7ac
filemap.c 55.6 KB