linux bcache

Bcache是Linux平台上具备缓存加速的技术框架,是一种针对慢速设备的性能提升策略。

1、需要安装bcache-tools 具体可以参照https://bcache.evilpiepirate.org/

2、bcache包含cache device 和 blacking device:blacking device一般是大容量的机械硬盘,统属block device

    make-bcache -B /dev/sdx   将生成/dev/bcache0

3、cache device 一般由ssd构成(随机读写性能高)

    make-bcache -C /dev/sdy  

In this example the default block and bucket sizes of 512B and 128kB are used. The block size should match the backing devices sector size which will usually be either 512 or 4k. The bucket size should match the erase block size of the caching device with the intent of reducing write amplification. For example, using a HDD with 4k sectors and an SSD with an erase block size of 2MB this command would look like

     make-bcache --block 4k --bucket 2m -C /dev/sdy

4. Register the cache device against your backing device. To find its cache set UUID, run # bcache-super-show /dev/sdy2 | grep cset.uuid and then add it to the bcache device initially. Udev rules will take care of this on reboot and will only need to be done once.

    echo cset.uuid > /sys/block/bcache0/bcache/attach

    echo /dev/sdy > /sys/fs/bcache/register

5. Change your cache mode (if you want to cache writes as well as reads):

    echo writeback > /sys/block/bcache0/bcache/cache_mode

6. If you want to have this partition available during the initcpio (i.e. you require it at some point in the boot process) you need to add 'bcache' to your modules array in /etc/mkinitcpio.conf as well as adding the 'bcache' hook in your list between block and filesystems. You must then rebuild the initramfs image. This is typically done with

   mkinitcpio -p linux

7. 启动 

      mkfs.ext4 /dev/bcache0

      mount /dev/bcache0 /mnt

1、查看运行状态:

cat /sys/block/bcache0/bcache/state

The output can be:

  • no cache: this means you have not attached a caching device to your backing bcache device
  • clean: this means everything is ok. The cache is clean.
  • dirty: this means everything is setup fine and that you have enabled writeback and that the cache is dirty.
  • inconsistent: you are in trouble because the backing device is not in sync with the caching device

You can have a /dev/bcache0 device associated with a backing device with no caching device attached. This means that all I/O (read/write) are passed directly to the backing device (pass-through mode)

2. See what caching mode is in use

   cat /sys/block/bcache0/bcache/cache_mode 

[writethrough] writeback writearound none

In the above example, the writethrough mode is enabled.

3. Show info about a bcached device:

   bcache-super-show /dev/sdXY

4. Stop the backing device:

   echo 1 > /sys/block/sdX/sdX[Y]/bcache/stop

5. Detach a caching device:

   echo 1 > /sys/block/sdX/sdX[Y]/bcache/detach

6. Safely remove the cache device

   echo <cache-set-uuid> > /sys/block/bcache0/bcache/detach

7. Release attached devices

   echo 1 > /sys/fs/bcache/<cache-set-uuid>/stop

 

Configuring

There are many options that can be configured (such as cache mode, cache flush interval, sequential write heuristic, etc.) This is currently done by writing to files in /sys. See the bcache user documentation.(http://atlas.evilpiepirate.org/git/linux-bcache.git/tree/Documentation/bcache.txt)

Changing the cache mode is done by echoing one of 'writethrough', 'writeback', 'writearound' or 'none' to /sys/block/bcache[0-9]/bcache/cache_mode.

Note that changes to /sys are temporary, and will revert back after a reboot. To set custom configurations at boot create a .conf file in /etc/tmpfile.d. To set, in a persistent fashion, the sequential cutoff for bcache0 to 1 MB and write back you could create a file /etc/tmpfile.d/my-bcache.conf with the contents

  w /sys/block/bcache0/bcache/sequential_cutoff - - - - 1

  w /sys/block/bcache0/bcache/cache_mode - - - - writeback

 

blacking device 在/sys/block/bache<number>/bcache

cache device 在/sys/fs/bcache/<UUID>/

 



posted @ 2016-01-26 19:10  scu_2008_hike  阅读(1045)  评论(0编辑  收藏  举报