[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Moving /tmp to tmpfs makes it useless



Goswin von Brederlow wrote:
> > Le vendredi 25 mai 2012 à 16:01 +0300, Uoti Urpala a écrit : 
> >> There is one significant difference though. When you read data back to
> >> memory from swap, the kernel does not remember that it already exists on
> >> disk; when the data is evicted from memory again, it is unnecessarily
> >> rewritten to disk rather than just dropped. Thus, if you do multiple
> >> read iterations through a large set of data (which does not fit in
> >> memory) on tmpfs, each iteration does disk read AND write rather than
> >> just read.

> Linux certainly has a notion of cached swap, i.e. pages from swap that
> are also unmodified in memory. As long as you have enough swap the
> kernel should not reap the swapped data and know that it is already on
> disk when it wants to evict the page.

I haven't read the relevant kernel code, but that doesn't match the
behavior I see. Reading a large file from tmpfs and then allocating
memory results in large swap writes every time, even if the newly
allocated memory is not itself immediately swapped out and the file
should already be in swap from before.

The script below can be used to test the behavior. It creates a file,
then loops alternatively reading the file and allocating+freeing memory.
It's noteworthy that sometimes the read performance also drops over
iterations (maybe the swap layout becomes more fragmented?). I used the
given sizes for testing on a machine with 8 GiB memory. This load does
run faster on ext4 than tmpfs.

#!/usr/bin/python3

FILESIZE = 5000000000
MEMSIZE  = 6500000000
FILENAME = '/tmp/alloctest'

from time import time

def main():
    print("creating file")
    t = time()
    with open(FILENAME, 'wb') as f:
        ss = FILESIZE
        while ss:
            s = min(ss, 1000000)
            f.write(b'x' * s)
            ss -= s
    print("file creation time: %.1f" % (time() - t))
    i = 0
    while 1:
        print("iteration %d, reading file" % i)
        i += 1
        t0 = time()
        t = t0
        with open(FILENAME, 'rb') as f:
            while f.read(1000000):
                pass
        print("file read time: %.1f" % (time()-t))
        print("filling memory")
        t = time()
        s = b'x' * MEMSIZE
        print("memory fill time: %.1f" % (time()-t))
        t = time()
        b'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' in s
        del s
        print('memory read time: %.1f' % (time()-t))
        print("total time: %.1f" % (time()-t0))
        print("press return for next iteration")
        input()

main()



Reply to: