Copy without Cache五, 17/04/2020 - 14:30 的修訂版本

修訂版本可以讓你追蹤文章的多個版本的不同之處。

 

 


 

[方法1] dd 的 "direct" flag

# iflag=direct oflag=direct

dd if=vda.qcow2 of=tmp.qcow2 bs=8M iflag=direct oflag=direct

 

[方法2] 建立 cgroup 去限制 memory usage

apt-get install cgroup-tools

env ppid=$$ sh -c '
    cgcreate -g memory:backup ;
    cgcreate -g blkio:backup ;
    echo 256M > /sys/fs/cgroup/memory/backup/memory.limit_in_bytes ;
    echo 100 > /sys/fs/cgroup/blkio/backup/blkio.weight ;
    echo $ppid > /sys/fs/cgroup/memory/backup/tasks ;
    echo $ppid > /sys/fs/cgroup/blkio/backup/tasks ;
'

 cp -a vda.qcow2 tmp.qcow2

 


nocache utility

 

nocache utility ( 獲得工具: nocache, cachestats, cachedel )

HomePage: https://github.com/Feh/nocache

原理

This is done by intercepting the `open` and `close` system calls and

calling `posix_fadvise` with the `POSIX_FADV_DONTNEED` parameter

Limitations

The pre-loaded library tries really hard to catch all system calls that open or close a file.

This happens by "hijacking" the libc functions that wrap the actual system calls.

In some cases, this may fail, for example because the application does some clever wrapping.

(That is the reason why __openat_2 is defined: GNU tar uses this instead of a regular openat.)

Install

apt-get install nocache

nocache 測試

# 當 copy 後會 cleanup cache

nocache cp -a vda.qcow2 tmp.qcow2

cachestats

cachestats vda.qcow2                                        # cachestats tmp.qcow2 都有相同 result

pages in cache: 271168/3555104 (7.6%)  [filesize=14220416.0K, pagesize=4K]

cachedel

# cachedel calls posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) on the file argument.

# Thus, if the file is not accessed by any other application, the pages will be eradicated from the fs cache.

i.e. clean file cache

cachedel vda.qcow2