How can I limit the cache used by copying so there is still memory available for other cache?

前端 未结 7 1274
春和景丽
春和景丽 2020-12-29 22:51

Basic situation:

I am copying some NTFS disks in openSuSE. Each one is 2TB. When I do this, the system runs slow.

My guesses:

相关标签:
7条回答
  • 2020-12-29 22:58

    The nocache command is the general answer to this problem! See https://github.com/Feh/nocache or find it in Debian and Ubuntu 13.10 (saucy).

    Thanks, Peter, for alerting us to the --drop-cache" option in rsync. But that was rejected upstream (Bug 9560 – drop-cache option), in favor of a more general solution for this: the new "nocache" command based on the rsync work with fadvise.

    You just prepend "nocache" to any command you want. It also has nice utilities for describing and modifying the cache status of files. E.g. here are the effects with and without nocache:

    $ ./cachestats ~/file.mp3
    pages in cache: 154/1945 (7.9%)  [filesize=7776.2K, pagesize=4K]
    $ ./nocache cp ~/file.mp3 /tmp
    $ ./cachestats ~/file.mp3
    pages in cache: 154/1945 (7.9%)  [filesize=7776.2K, pagesize=4K]\
    $ cp ~/file.mp3 /tmp
    $ ./cachestats ~/file.mp3
    pages in cache: 1945/1945 (100.0%)  [filesize=7776.2K, pagesize=4K]
    

    So hopefully that will work for other backup programs (rsnapshot, duplicity, rdiff-backup, amanda, s3sync, s3ql, tar, etc) and other commands that you don't want trashing your cache.

    0 讨论(0)
  • 2020-12-29 23:00

    try using dd instead of cp .

    Or mount the filesystem with the sync flag.

    I'm not completely sure if these methods bypass the swap, but it may be worth giving a try.

    Just my 2c.

    0 讨论(0)
  • 2020-12-29 23:07

    Kristof Provost was very close, but in my situation, I didn't want to use dd or write my own software, so the solution was to use the "--drop-cache" option in rsync.

    I have used this many times since creating this question, and it seems to fix the problem completely. One exception was when I am using rsync to copy from a FreeBSD machine, which doesn't support "--drop-cache". So I wrote a wrapper to replace the /usr/local/bin/rsync command, and remove that option, and now it works copying from there too.

    It still uses huge amount of memory for buffers, and seems to keep almost no cache, but works smoothly anyway.

    $ free
                 total       used       free     shared    buffers     cached
    Mem:      24731544   24531576     199968          0   15349680     850624
    -/+ buffers/cache:    8331272   16400272
    Swap:      4194300     602648    3591652
    
    0 讨论(0)
  • 2020-12-29 23:08

    It's not possible if you're using plain old cp, but if you're willing to re-implement or patch it yourself setting posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE) on both input and output file will probably help.

    posix_fadvise() tells the kernel about your intended access pattern. In this case, you'd only use the data once so there's no point in caching it. The Linux kernel honours these flags, so shouldn't be caching the data any more.

    0 讨论(0)
  • 2020-12-29 23:14

    The kernel can not know, that you won't use the cached data from copying agains. This is your information advantage.

    But you could set the swapiness to 0: sudo sysctl vm.swappiness=0. This will cause linux to drop the cache before libraries etc. are written to the swap.

    Works nice for me too, especially very performant in combination with hugh ram (16-32 GB).

    0 讨论(0)
  • 2020-12-29 23:14

    I am copying some NTFS disks [...] the system runs slow. [...] Since it is USB [...]

    The slowdown is a known memory management issue.

    Use a newer Linux Kernel. The older ones have a problem with USB data and "Transparent Huge Pages". See this LWN article. Very recently this issue was addressed, see "Memory Management" in LinuxChanges.

    0 讨论(0)
提交回复
热议问题