1*4882a593Smuzhiyun============================ 2*4882a593SmuzhiyunA block layer cache (bcache) 3*4882a593Smuzhiyun============================ 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunSay you've got a big slow raid 6, and an ssd or three. Wouldn't it be 6*4882a593Smuzhiyunnice if you could use them as cache... Hence bcache. 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunThe bcache wiki can be found at: 9*4882a593Smuzhiyun https://bcache.evilpiepirate.org 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunThis is the git repository of bcache-tools: 12*4882a593Smuzhiyun https://git.kernel.org/pub/scm/linux/kernel/git/colyli/bcache-tools.git/ 13*4882a593Smuzhiyun 14*4882a593SmuzhiyunThe latest bcache kernel code can be found from mainline Linux kernel: 15*4882a593Smuzhiyun https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/ 16*4882a593Smuzhiyun 17*4882a593SmuzhiyunIt's designed around the performance characteristics of SSDs - it only allocates 18*4882a593Smuzhiyunin erase block sized buckets, and it uses a hybrid btree/log to track cached 19*4882a593Smuzhiyunextents (which can be anywhere from a single sector to the bucket size). It's 20*4882a593Smuzhiyundesigned to avoid random writes at all costs; it fills up an erase block 21*4882a593Smuzhiyunsequentially, then issues a discard before reusing it. 22*4882a593Smuzhiyun 23*4882a593SmuzhiyunBoth writethrough and writeback caching are supported. Writeback defaults to 24*4882a593Smuzhiyunoff, but can be switched on and off arbitrarily at runtime. Bcache goes to 25*4882a593Smuzhiyungreat lengths to protect your data - it reliably handles unclean shutdown. (It 26*4882a593Smuzhiyundoesn't even have a notion of a clean shutdown; bcache simply doesn't return 27*4882a593Smuzhiyunwrites as completed until they're on stable storage). 28*4882a593Smuzhiyun 29*4882a593SmuzhiyunWriteback caching can use most of the cache for buffering writes - writing 30*4882a593Smuzhiyundirty data to the backing device is always done sequentially, scanning from the 31*4882a593Smuzhiyunstart to the end of the index. 32*4882a593Smuzhiyun 33*4882a593SmuzhiyunSince random IO is what SSDs excel at, there generally won't be much benefit 34*4882a593Smuzhiyunto caching large sequential IO. Bcache detects sequential IO and skips it; 35*4882a593Smuzhiyunit also keeps a rolling average of the IO sizes per task, and as long as the 36*4882a593Smuzhiyunaverage is above the cutoff it will skip all IO from that task - instead of 37*4882a593Smuzhiyuncaching the first 512k after every seek. Backups and large file copies should 38*4882a593Smuzhiyunthus entirely bypass the cache. 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunIn the event of a data IO error on the flash it will try to recover by reading 41*4882a593Smuzhiyunfrom disk or invalidating cache entries. For unrecoverable errors (meta data 42*4882a593Smuzhiyunor dirty data), caching is automatically disabled; if dirty data was present 43*4882a593Smuzhiyunin the cache it first disables writeback caching and waits for all dirty data 44*4882a593Smuzhiyunto be flushed. 45*4882a593Smuzhiyun 46*4882a593SmuzhiyunGetting started: 47*4882a593SmuzhiyunYou'll need bcache util from the bcache-tools repository. Both the cache device 48*4882a593Smuzhiyunand backing device must be formatted before use:: 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun bcache make -B /dev/sdb 51*4882a593Smuzhiyun bcache make -C /dev/sdc 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun`bcache make` has the ability to format multiple devices at the same time - if 54*4882a593Smuzhiyunyou format your backing devices and cache device at the same time, you won't 55*4882a593Smuzhiyunhave to manually attach:: 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun bcache make -B /dev/sda /dev/sdb -C /dev/sdc 58*4882a593Smuzhiyun 59*4882a593SmuzhiyunIf your bcache-tools is not updated to latest version and does not have the 60*4882a593Smuzhiyununified `bcache` utility, you may use the legacy `make-bcache` utility to format 61*4882a593Smuzhiyunbcache device with same -B and -C parameters. 62*4882a593Smuzhiyun 63*4882a593Smuzhiyunbcache-tools now ships udev rules, and bcache devices are known to the kernel 64*4882a593Smuzhiyunimmediately. Without udev, you can manually register devices like this:: 65*4882a593Smuzhiyun 66*4882a593Smuzhiyun echo /dev/sdb > /sys/fs/bcache/register 67*4882a593Smuzhiyun echo /dev/sdc > /sys/fs/bcache/register 68*4882a593Smuzhiyun 69*4882a593SmuzhiyunRegistering the backing device makes the bcache device show up in /dev; you can 70*4882a593Smuzhiyunnow format it and use it as normal. But the first time using a new bcache 71*4882a593Smuzhiyundevice, it'll be running in passthrough mode until you attach it to a cache. 72*4882a593SmuzhiyunIf you are thinking about using bcache later, it is recommended to setup all your 73*4882a593Smuzhiyunslow devices as bcache backing devices without a cache, and you can choose to add 74*4882a593Smuzhiyuna caching device later. 75*4882a593SmuzhiyunSee 'ATTACHING' section below. 76*4882a593Smuzhiyun 77*4882a593SmuzhiyunThe devices show up as:: 78*4882a593Smuzhiyun 79*4882a593Smuzhiyun /dev/bcache<N> 80*4882a593Smuzhiyun 81*4882a593SmuzhiyunAs well as (with udev):: 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun /dev/bcache/by-uuid/<uuid> 84*4882a593Smuzhiyun /dev/bcache/by-label/<label> 85*4882a593Smuzhiyun 86*4882a593SmuzhiyunTo get started:: 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun mkfs.ext4 /dev/bcache0 89*4882a593Smuzhiyun mount /dev/bcache0 /mnt 90*4882a593Smuzhiyun 91*4882a593SmuzhiyunYou can control bcache devices through sysfs at /sys/block/bcache<N>/bcache . 92*4882a593SmuzhiyunYou can also control them through /sys/fs//bcache/<cset-uuid>/ . 93*4882a593Smuzhiyun 94*4882a593SmuzhiyunCache devices are managed as sets; multiple caches per set isn't supported yet 95*4882a593Smuzhiyunbut will allow for mirroring of metadata and dirty data in the future. Your new 96*4882a593Smuzhiyuncache set shows up as /sys/fs/bcache/<UUID> 97*4882a593Smuzhiyun 98*4882a593SmuzhiyunAttaching 99*4882a593Smuzhiyun--------- 100*4882a593Smuzhiyun 101*4882a593SmuzhiyunAfter your cache device and backing device are registered, the backing device 102*4882a593Smuzhiyunmust be attached to your cache set to enable caching. Attaching a backing 103*4882a593Smuzhiyundevice to a cache set is done thusly, with the UUID of the cache set in 104*4882a593Smuzhiyun/sys/fs/bcache:: 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun echo <CSET-UUID> > /sys/block/bcache0/bcache/attach 107*4882a593Smuzhiyun 108*4882a593SmuzhiyunThis only has to be done once. The next time you reboot, just reregister all 109*4882a593Smuzhiyunyour bcache devices. If a backing device has data in a cache somewhere, the 110*4882a593Smuzhiyun/dev/bcache<N> device won't be created until the cache shows up - particularly 111*4882a593Smuzhiyunimportant if you have writeback caching turned on. 112*4882a593Smuzhiyun 113*4882a593SmuzhiyunIf you're booting up and your cache device is gone and never coming back, you 114*4882a593Smuzhiyuncan force run the backing device:: 115*4882a593Smuzhiyun 116*4882a593Smuzhiyun echo 1 > /sys/block/sdb/bcache/running 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun(You need to use /sys/block/sdb (or whatever your backing device is called), not 119*4882a593Smuzhiyun/sys/block/bcache0, because bcache0 doesn't exist yet. If you're using a 120*4882a593Smuzhiyunpartition, the bcache directory would be at /sys/block/sdb/sdb2/bcache) 121*4882a593Smuzhiyun 122*4882a593SmuzhiyunThe backing device will still use that cache set if it shows up in the future, 123*4882a593Smuzhiyunbut all the cached data will be invalidated. If there was dirty data in the 124*4882a593Smuzhiyuncache, don't expect the filesystem to be recoverable - you will have massive 125*4882a593Smuzhiyunfilesystem corruption, though ext4's fsck does work miracles. 126*4882a593Smuzhiyun 127*4882a593SmuzhiyunError Handling 128*4882a593Smuzhiyun-------------- 129*4882a593Smuzhiyun 130*4882a593SmuzhiyunBcache tries to transparently handle IO errors to/from the cache device without 131*4882a593Smuzhiyunaffecting normal operation; if it sees too many errors (the threshold is 132*4882a593Smuzhiyunconfigurable, and defaults to 0) it shuts down the cache device and switches all 133*4882a593Smuzhiyunthe backing devices to passthrough mode. 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun - For reads from the cache, if they error we just retry the read from the 136*4882a593Smuzhiyun backing device. 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun - For writethrough writes, if the write to the cache errors we just switch to 139*4882a593Smuzhiyun invalidating the data at that lba in the cache (i.e. the same thing we do for 140*4882a593Smuzhiyun a write that bypasses the cache) 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun - For writeback writes, we currently pass that error back up to the 143*4882a593Smuzhiyun filesystem/userspace. This could be improved - we could retry it as a write 144*4882a593Smuzhiyun that skips the cache so we don't have to error the write. 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun - When we detach, we first try to flush any dirty data (if we were running in 147*4882a593Smuzhiyun writeback mode). It currently doesn't do anything intelligent if it fails to 148*4882a593Smuzhiyun read some of the dirty data, though. 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun 151*4882a593SmuzhiyunHowto/cookbook 152*4882a593Smuzhiyun-------------- 153*4882a593Smuzhiyun 154*4882a593SmuzhiyunA) Starting a bcache with a missing caching device 155*4882a593Smuzhiyun 156*4882a593SmuzhiyunIf registering the backing device doesn't help, it's already there, you just need 157*4882a593Smuzhiyunto force it to run without the cache:: 158*4882a593Smuzhiyun 159*4882a593Smuzhiyun host:~# echo /dev/sdb1 > /sys/fs/bcache/register 160*4882a593Smuzhiyun [ 119.844831] bcache: register_bcache() error opening /dev/sdb1: device already registered 161*4882a593Smuzhiyun 162*4882a593SmuzhiyunNext, you try to register your caching device if it's present. However 163*4882a593Smuzhiyunif it's absent, or registration fails for some reason, you can still 164*4882a593Smuzhiyunstart your bcache without its cache, like so:: 165*4882a593Smuzhiyun 166*4882a593Smuzhiyun host:/sys/block/sdb/sdb1/bcache# echo 1 > running 167*4882a593Smuzhiyun 168*4882a593SmuzhiyunNote that this may cause data loss if you were running in writeback mode. 169*4882a593Smuzhiyun 170*4882a593Smuzhiyun 171*4882a593SmuzhiyunB) Bcache does not find its cache:: 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun host:/sys/block/md5/bcache# echo 0226553a-37cf-41d5-b3ce-8b1e944543a8 > attach 174*4882a593Smuzhiyun [ 1933.455082] bcache: bch_cached_dev_attach() Couldn't find uuid for md5 in set 175*4882a593Smuzhiyun [ 1933.478179] bcache: __cached_dev_store() Can't attach 0226553a-37cf-41d5-b3ce-8b1e944543a8 176*4882a593Smuzhiyun [ 1933.478179] : cache set not found 177*4882a593Smuzhiyun 178*4882a593SmuzhiyunIn this case, the caching device was simply not registered at boot 179*4882a593Smuzhiyunor disappeared and came back, and needs to be (re-)registered:: 180*4882a593Smuzhiyun 181*4882a593Smuzhiyun host:/sys/block/md5/bcache# echo /dev/sdh2 > /sys/fs/bcache/register 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun 184*4882a593SmuzhiyunC) Corrupt bcache crashes the kernel at device registration time: 185*4882a593Smuzhiyun 186*4882a593SmuzhiyunThis should never happen. If it does happen, then you have found a bug! 187*4882a593SmuzhiyunPlease report it to the bcache development list: linux-bcache@vger.kernel.org 188*4882a593Smuzhiyun 189*4882a593SmuzhiyunBe sure to provide as much information that you can including kernel dmesg 190*4882a593Smuzhiyunoutput if available so that we may assist. 191*4882a593Smuzhiyun 192*4882a593Smuzhiyun 193*4882a593SmuzhiyunD) Recovering data without bcache: 194*4882a593Smuzhiyun 195*4882a593SmuzhiyunIf bcache is not available in the kernel, a filesystem on the backing 196*4882a593Smuzhiyundevice is still available at an 8KiB offset. So either via a loopdev 197*4882a593Smuzhiyunof the backing device created with --offset 8K, or any value defined by 198*4882a593Smuzhiyun--data-offset when you originally formatted bcache with `bcache make`. 199*4882a593Smuzhiyun 200*4882a593SmuzhiyunFor example:: 201*4882a593Smuzhiyun 202*4882a593Smuzhiyun losetup -o 8192 /dev/loop0 /dev/your_bcache_backing_dev 203*4882a593Smuzhiyun 204*4882a593SmuzhiyunThis should present your unmodified backing device data in /dev/loop0 205*4882a593Smuzhiyun 206*4882a593SmuzhiyunIf your cache is in writethrough mode, then you can safely discard the 207*4882a593Smuzhiyuncache device without loosing data. 208*4882a593Smuzhiyun 209*4882a593Smuzhiyun 210*4882a593SmuzhiyunE) Wiping a cache device 211*4882a593Smuzhiyun 212*4882a593Smuzhiyun:: 213*4882a593Smuzhiyun 214*4882a593Smuzhiyun host:~# wipefs -a /dev/sdh2 215*4882a593Smuzhiyun 16 bytes were erased at offset 0x1018 (bcache) 216*4882a593Smuzhiyun they were: c6 85 73 f6 4e 1a 45 ca 82 65 f5 7f 48 ba 6d 81 217*4882a593Smuzhiyun 218*4882a593SmuzhiyunAfter you boot back with bcache enabled, you recreate the cache and attach it:: 219*4882a593Smuzhiyun 220*4882a593Smuzhiyun host:~# bcache make -C /dev/sdh2 221*4882a593Smuzhiyun UUID: 7be7e175-8f4c-4f99-94b2-9c904d227045 222*4882a593Smuzhiyun Set UUID: 5bc072a8-ab17-446d-9744-e247949913c1 223*4882a593Smuzhiyun version: 0 224*4882a593Smuzhiyun nbuckets: 106874 225*4882a593Smuzhiyun block_size: 1 226*4882a593Smuzhiyun bucket_size: 1024 227*4882a593Smuzhiyun nr_in_set: 1 228*4882a593Smuzhiyun nr_this_dev: 0 229*4882a593Smuzhiyun first_bucket: 1 230*4882a593Smuzhiyun [ 650.511912] bcache: run_cache_set() invalidating existing data 231*4882a593Smuzhiyun [ 650.549228] bcache: register_cache() registered cache device sdh2 232*4882a593Smuzhiyun 233*4882a593Smuzhiyunstart backing device with missing cache:: 234*4882a593Smuzhiyun 235*4882a593Smuzhiyun host:/sys/block/md5/bcache# echo 1 > running 236*4882a593Smuzhiyun 237*4882a593Smuzhiyunattach new cache:: 238*4882a593Smuzhiyun 239*4882a593Smuzhiyun host:/sys/block/md5/bcache# echo 5bc072a8-ab17-446d-9744-e247949913c1 > attach 240*4882a593Smuzhiyun [ 865.276616] bcache: bch_cached_dev_attach() Caching md5 as bcache0 on set 5bc072a8-ab17-446d-9744-e247949913c1 241*4882a593Smuzhiyun 242*4882a593Smuzhiyun 243*4882a593SmuzhiyunF) Remove or replace a caching device:: 244*4882a593Smuzhiyun 245*4882a593Smuzhiyun host:/sys/block/sda/sda7/bcache# echo 1 > detach 246*4882a593Smuzhiyun [ 695.872542] bcache: cached_dev_detach_finish() Caching disabled for sda7 247*4882a593Smuzhiyun 248*4882a593Smuzhiyun host:~# wipefs -a /dev/nvme0n1p4 249*4882a593Smuzhiyun wipefs: error: /dev/nvme0n1p4: probing initialization failed: Device or resource busy 250*4882a593Smuzhiyun Ooops, it's disabled, but not unregistered, so it's still protected 251*4882a593Smuzhiyun 252*4882a593SmuzhiyunWe need to go and unregister it:: 253*4882a593Smuzhiyun 254*4882a593Smuzhiyun host:/sys/fs/bcache/b7ba27a1-2398-4649-8ae3-0959f57ba128# ls -l cache0 255*4882a593Smuzhiyun lrwxrwxrwx 1 root root 0 Feb 25 18:33 cache0 -> ../../../devices/pci0000:00/0000:00:1d.0/0000:70:00.0/nvme/nvme0/nvme0n1/nvme0n1p4/bcache/ 256*4882a593Smuzhiyun host:/sys/fs/bcache/b7ba27a1-2398-4649-8ae3-0959f57ba128# echo 1 > stop 257*4882a593Smuzhiyun kernel: [ 917.041908] bcache: cache_set_free() Cache set b7ba27a1-2398-4649-8ae3-0959f57ba128 unregistered 258*4882a593Smuzhiyun 259*4882a593SmuzhiyunNow we can wipe it:: 260*4882a593Smuzhiyun 261*4882a593Smuzhiyun host:~# wipefs -a /dev/nvme0n1p4 262*4882a593Smuzhiyun /dev/nvme0n1p4: 16 bytes were erased at offset 0x00001018 (bcache): c6 85 73 f6 4e 1a 45 ca 82 65 f5 7f 48 ba 6d 81 263*4882a593Smuzhiyun 264*4882a593Smuzhiyun 265*4882a593SmuzhiyunG) dm-crypt and bcache 266*4882a593Smuzhiyun 267*4882a593SmuzhiyunFirst setup bcache unencrypted and then install dmcrypt on top of 268*4882a593Smuzhiyun/dev/bcache<N> This will work faster than if you dmcrypt both the backing 269*4882a593Smuzhiyunand caching devices and then install bcache on top. [benchmarks?] 270*4882a593Smuzhiyun 271*4882a593Smuzhiyun 272*4882a593SmuzhiyunH) Stop/free a registered bcache to wipe and/or recreate it 273*4882a593Smuzhiyun 274*4882a593SmuzhiyunSuppose that you need to free up all bcache references so that you can 275*4882a593Smuzhiyunfdisk run and re-register a changed partition table, which won't work 276*4882a593Smuzhiyunif there are any active backing or caching devices left on it: 277*4882a593Smuzhiyun 278*4882a593Smuzhiyun1) Is it present in /dev/bcache* ? (there are times where it won't be) 279*4882a593Smuzhiyun 280*4882a593Smuzhiyun If so, it's easy:: 281*4882a593Smuzhiyun 282*4882a593Smuzhiyun host:/sys/block/bcache0/bcache# echo 1 > stop 283*4882a593Smuzhiyun 284*4882a593Smuzhiyun2) But if your backing device is gone, this won't work:: 285*4882a593Smuzhiyun 286*4882a593Smuzhiyun host:/sys/block/bcache0# cd bcache 287*4882a593Smuzhiyun bash: cd: bcache: No such file or directory 288*4882a593Smuzhiyun 289*4882a593Smuzhiyun In this case, you may have to unregister the dmcrypt block device that 290*4882a593Smuzhiyun references this bcache to free it up:: 291*4882a593Smuzhiyun 292*4882a593Smuzhiyun host:~# dmsetup remove oldds1 293*4882a593Smuzhiyun bcache: bcache_device_free() bcache0 stopped 294*4882a593Smuzhiyun bcache: cache_set_free() Cache set 5bc072a8-ab17-446d-9744-e247949913c1 unregistered 295*4882a593Smuzhiyun 296*4882a593Smuzhiyun This causes the backing bcache to be removed from /sys/fs/bcache and 297*4882a593Smuzhiyun then it can be reused. This would be true of any block device stacking 298*4882a593Smuzhiyun where bcache is a lower device. 299*4882a593Smuzhiyun 300*4882a593Smuzhiyun3) In other cases, you can also look in /sys/fs/bcache/:: 301*4882a593Smuzhiyun 302*4882a593Smuzhiyun host:/sys/fs/bcache# ls -l */{cache?,bdev?} 303*4882a593Smuzhiyun lrwxrwxrwx 1 root root 0 Mar 5 09:39 0226553a-37cf-41d5-b3ce-8b1e944543a8/bdev1 -> ../../../devices/virtual/block/dm-1/bcache/ 304*4882a593Smuzhiyun lrwxrwxrwx 1 root root 0 Mar 5 09:39 0226553a-37cf-41d5-b3ce-8b1e944543a8/cache0 -> ../../../devices/virtual/block/dm-4/bcache/ 305*4882a593Smuzhiyun lrwxrwxrwx 1 root root 0 Mar 5 09:39 5bc072a8-ab17-446d-9744-e247949913c1/cache0 -> ../../../devices/pci0000:00/0000:00:01.0/0000:01:00.0/ata10/host9/target9:0:0/9:0:0:0/block/sdl/sdl2/bcache/ 306*4882a593Smuzhiyun 307*4882a593Smuzhiyun The device names will show which UUID is relevant, cd in that directory 308*4882a593Smuzhiyun and stop the cache:: 309*4882a593Smuzhiyun 310*4882a593Smuzhiyun host:/sys/fs/bcache/5bc072a8-ab17-446d-9744-e247949913c1# echo 1 > stop 311*4882a593Smuzhiyun 312*4882a593Smuzhiyun This will free up bcache references and let you reuse the partition for 313*4882a593Smuzhiyun other purposes. 314*4882a593Smuzhiyun 315*4882a593Smuzhiyun 316*4882a593Smuzhiyun 317*4882a593SmuzhiyunTroubleshooting performance 318*4882a593Smuzhiyun--------------------------- 319*4882a593Smuzhiyun 320*4882a593SmuzhiyunBcache has a bunch of config options and tunables. The defaults are intended to 321*4882a593Smuzhiyunbe reasonable for typical desktop and server workloads, but they're not what you 322*4882a593Smuzhiyunwant for getting the best possible numbers when benchmarking. 323*4882a593Smuzhiyun 324*4882a593Smuzhiyun - Backing device alignment 325*4882a593Smuzhiyun 326*4882a593Smuzhiyun The default metadata size in bcache is 8k. If your backing device is 327*4882a593Smuzhiyun RAID based, then be sure to align this by a multiple of your stride 328*4882a593Smuzhiyun width using `bcache make --data-offset`. If you intend to expand your 329*4882a593Smuzhiyun disk array in the future, then multiply a series of primes by your 330*4882a593Smuzhiyun raid stripe size to get the disk multiples that you would like. 331*4882a593Smuzhiyun 332*4882a593Smuzhiyun For example: If you have a 64k stripe size, then the following offset 333*4882a593Smuzhiyun would provide alignment for many common RAID5 data spindle counts:: 334*4882a593Smuzhiyun 335*4882a593Smuzhiyun 64k * 2*2*2*3*3*5*7 bytes = 161280k 336*4882a593Smuzhiyun 337*4882a593Smuzhiyun That space is wasted, but for only 157.5MB you can grow your RAID 5 338*4882a593Smuzhiyun volume to the following data-spindle counts without re-aligning:: 339*4882a593Smuzhiyun 340*4882a593Smuzhiyun 3,4,5,6,7,8,9,10,12,14,15,18,20,21 ... 341*4882a593Smuzhiyun 342*4882a593Smuzhiyun - Bad write performance 343*4882a593Smuzhiyun 344*4882a593Smuzhiyun If write performance is not what you expected, you probably wanted to be 345*4882a593Smuzhiyun running in writeback mode, which isn't the default (not due to a lack of 346*4882a593Smuzhiyun maturity, but simply because in writeback mode you'll lose data if something 347*4882a593Smuzhiyun happens to your SSD):: 348*4882a593Smuzhiyun 349*4882a593Smuzhiyun # echo writeback > /sys/block/bcache0/bcache/cache_mode 350*4882a593Smuzhiyun 351*4882a593Smuzhiyun - Bad performance, or traffic not going to the SSD that you'd expect 352*4882a593Smuzhiyun 353*4882a593Smuzhiyun By default, bcache doesn't cache everything. It tries to skip sequential IO - 354*4882a593Smuzhiyun because you really want to be caching the random IO, and if you copy a 10 355*4882a593Smuzhiyun gigabyte file you probably don't want that pushing 10 gigabytes of randomly 356*4882a593Smuzhiyun accessed data out of your cache. 357*4882a593Smuzhiyun 358*4882a593Smuzhiyun But if you want to benchmark reads from cache, and you start out with fio 359*4882a593Smuzhiyun writing an 8 gigabyte test file - so you want to disable that:: 360*4882a593Smuzhiyun 361*4882a593Smuzhiyun # echo 0 > /sys/block/bcache0/bcache/sequential_cutoff 362*4882a593Smuzhiyun 363*4882a593Smuzhiyun To set it back to the default (4 mb), do:: 364*4882a593Smuzhiyun 365*4882a593Smuzhiyun # echo 4M > /sys/block/bcache0/bcache/sequential_cutoff 366*4882a593Smuzhiyun 367*4882a593Smuzhiyun - Traffic's still going to the spindle/still getting cache misses 368*4882a593Smuzhiyun 369*4882a593Smuzhiyun In the real world, SSDs don't always keep up with disks - particularly with 370*4882a593Smuzhiyun slower SSDs, many disks being cached by one SSD, or mostly sequential IO. So 371*4882a593Smuzhiyun you want to avoid being bottlenecked by the SSD and having it slow everything 372*4882a593Smuzhiyun down. 373*4882a593Smuzhiyun 374*4882a593Smuzhiyun To avoid that bcache tracks latency to the cache device, and gradually 375*4882a593Smuzhiyun throttles traffic if the latency exceeds a threshold (it does this by 376*4882a593Smuzhiyun cranking down the sequential bypass). 377*4882a593Smuzhiyun 378*4882a593Smuzhiyun You can disable this if you need to by setting the thresholds to 0:: 379*4882a593Smuzhiyun 380*4882a593Smuzhiyun # echo 0 > /sys/fs/bcache/<cache set>/congested_read_threshold_us 381*4882a593Smuzhiyun # echo 0 > /sys/fs/bcache/<cache set>/congested_write_threshold_us 382*4882a593Smuzhiyun 383*4882a593Smuzhiyun The default is 2000 us (2 milliseconds) for reads, and 20000 for writes. 384*4882a593Smuzhiyun 385*4882a593Smuzhiyun - Still getting cache misses, of the same data 386*4882a593Smuzhiyun 387*4882a593Smuzhiyun One last issue that sometimes trips people up is actually an old bug, due to 388*4882a593Smuzhiyun the way cache coherency is handled for cache misses. If a btree node is full, 389*4882a593Smuzhiyun a cache miss won't be able to insert a key for the new data and the data 390*4882a593Smuzhiyun won't be written to the cache. 391*4882a593Smuzhiyun 392*4882a593Smuzhiyun In practice this isn't an issue because as soon as a write comes along it'll 393*4882a593Smuzhiyun cause the btree node to be split, and you need almost no write traffic for 394*4882a593Smuzhiyun this to not show up enough to be noticeable (especially since bcache's btree 395*4882a593Smuzhiyun nodes are huge and index large regions of the device). But when you're 396*4882a593Smuzhiyun benchmarking, if you're trying to warm the cache by reading a bunch of data 397*4882a593Smuzhiyun and there's no other traffic - that can be a problem. 398*4882a593Smuzhiyun 399*4882a593Smuzhiyun Solution: warm the cache by doing writes, or use the testing branch (there's 400*4882a593Smuzhiyun a fix for the issue there). 401*4882a593Smuzhiyun 402*4882a593Smuzhiyun 403*4882a593SmuzhiyunSysfs - backing device 404*4882a593Smuzhiyun---------------------- 405*4882a593Smuzhiyun 406*4882a593SmuzhiyunAvailable at /sys/block/<bdev>/bcache, /sys/block/bcache*/bcache and 407*4882a593Smuzhiyun(if attached) /sys/fs/bcache/<cset-uuid>/bdev* 408*4882a593Smuzhiyun 409*4882a593Smuzhiyunattach 410*4882a593Smuzhiyun Echo the UUID of a cache set to this file to enable caching. 411*4882a593Smuzhiyun 412*4882a593Smuzhiyuncache_mode 413*4882a593Smuzhiyun Can be one of either writethrough, writeback, writearound or none. 414*4882a593Smuzhiyun 415*4882a593Smuzhiyunclear_stats 416*4882a593Smuzhiyun Writing to this file resets the running total stats (not the day/hour/5 minute 417*4882a593Smuzhiyun decaying versions). 418*4882a593Smuzhiyun 419*4882a593Smuzhiyundetach 420*4882a593Smuzhiyun Write to this file to detach from a cache set. If there is dirty data in the 421*4882a593Smuzhiyun cache, it will be flushed first. 422*4882a593Smuzhiyun 423*4882a593Smuzhiyundirty_data 424*4882a593Smuzhiyun Amount of dirty data for this backing device in the cache. Continuously 425*4882a593Smuzhiyun updated unlike the cache set's version, but may be slightly off. 426*4882a593Smuzhiyun 427*4882a593Smuzhiyunlabel 428*4882a593Smuzhiyun Name of underlying device. 429*4882a593Smuzhiyun 430*4882a593Smuzhiyunreadahead 431*4882a593Smuzhiyun Size of readahead that should be performed. Defaults to 0. If set to e.g. 432*4882a593Smuzhiyun 1M, it will round cache miss reads up to that size, but without overlapping 433*4882a593Smuzhiyun existing cache entries. 434*4882a593Smuzhiyun 435*4882a593Smuzhiyunrunning 436*4882a593Smuzhiyun 1 if bcache is running (i.e. whether the /dev/bcache device exists, whether 437*4882a593Smuzhiyun it's in passthrough mode or caching). 438*4882a593Smuzhiyun 439*4882a593Smuzhiyunsequential_cutoff 440*4882a593Smuzhiyun A sequential IO will bypass the cache once it passes this threshold; the 441*4882a593Smuzhiyun most recent 128 IOs are tracked so sequential IO can be detected even when 442*4882a593Smuzhiyun it isn't all done at once. 443*4882a593Smuzhiyun 444*4882a593Smuzhiyunsequential_merge 445*4882a593Smuzhiyun If non zero, bcache keeps a list of the last 128 requests submitted to compare 446*4882a593Smuzhiyun against all new requests to determine which new requests are sequential 447*4882a593Smuzhiyun continuations of previous requests for the purpose of determining sequential 448*4882a593Smuzhiyun cutoff. This is necessary if the sequential cutoff value is greater than the 449*4882a593Smuzhiyun maximum acceptable sequential size for any single request. 450*4882a593Smuzhiyun 451*4882a593Smuzhiyunstate 452*4882a593Smuzhiyun The backing device can be in one of four different states: 453*4882a593Smuzhiyun 454*4882a593Smuzhiyun no cache: Has never been attached to a cache set. 455*4882a593Smuzhiyun 456*4882a593Smuzhiyun clean: Part of a cache set, and there is no cached dirty data. 457*4882a593Smuzhiyun 458*4882a593Smuzhiyun dirty: Part of a cache set, and there is cached dirty data. 459*4882a593Smuzhiyun 460*4882a593Smuzhiyun inconsistent: The backing device was forcibly run by the user when there was 461*4882a593Smuzhiyun dirty data cached but the cache set was unavailable; whatever data was on the 462*4882a593Smuzhiyun backing device has likely been corrupted. 463*4882a593Smuzhiyun 464*4882a593Smuzhiyunstop 465*4882a593Smuzhiyun Write to this file to shut down the bcache device and close the backing 466*4882a593Smuzhiyun device. 467*4882a593Smuzhiyun 468*4882a593Smuzhiyunwriteback_delay 469*4882a593Smuzhiyun When dirty data is written to the cache and it previously did not contain 470*4882a593Smuzhiyun any, waits some number of seconds before initiating writeback. Defaults to 471*4882a593Smuzhiyun 30. 472*4882a593Smuzhiyun 473*4882a593Smuzhiyunwriteback_percent 474*4882a593Smuzhiyun If nonzero, bcache tries to keep around this percentage of the cache dirty by 475*4882a593Smuzhiyun throttling background writeback and using a PD controller to smoothly adjust 476*4882a593Smuzhiyun the rate. 477*4882a593Smuzhiyun 478*4882a593Smuzhiyunwriteback_rate 479*4882a593Smuzhiyun Rate in sectors per second - if writeback_percent is nonzero, background 480*4882a593Smuzhiyun writeback is throttled to this rate. Continuously adjusted by bcache but may 481*4882a593Smuzhiyun also be set by the user. 482*4882a593Smuzhiyun 483*4882a593Smuzhiyunwriteback_running 484*4882a593Smuzhiyun If off, writeback of dirty data will not take place at all. Dirty data will 485*4882a593Smuzhiyun still be added to the cache until it is mostly full; only meant for 486*4882a593Smuzhiyun benchmarking. Defaults to on. 487*4882a593Smuzhiyun 488*4882a593SmuzhiyunSysfs - backing device stats 489*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 490*4882a593Smuzhiyun 491*4882a593SmuzhiyunThere are directories with these numbers for a running total, as well as 492*4882a593Smuzhiyunversions that decay over the past day, hour and 5 minutes; they're also 493*4882a593Smuzhiyunaggregated in the cache set directory as well. 494*4882a593Smuzhiyun 495*4882a593Smuzhiyunbypassed 496*4882a593Smuzhiyun Amount of IO (both reads and writes) that has bypassed the cache 497*4882a593Smuzhiyun 498*4882a593Smuzhiyuncache_hits, cache_misses, cache_hit_ratio 499*4882a593Smuzhiyun Hits and misses are counted per individual IO as bcache sees them; a 500*4882a593Smuzhiyun partial hit is counted as a miss. 501*4882a593Smuzhiyun 502*4882a593Smuzhiyuncache_bypass_hits, cache_bypass_misses 503*4882a593Smuzhiyun Hits and misses for IO that is intended to skip the cache are still counted, 504*4882a593Smuzhiyun but broken out here. 505*4882a593Smuzhiyun 506*4882a593Smuzhiyuncache_miss_collisions 507*4882a593Smuzhiyun Counts instances where data was going to be inserted into the cache from a 508*4882a593Smuzhiyun cache miss, but raced with a write and data was already present (usually 0 509*4882a593Smuzhiyun since the synchronization for cache misses was rewritten) 510*4882a593Smuzhiyun 511*4882a593Smuzhiyuncache_readaheads 512*4882a593Smuzhiyun Count of times readahead occurred. 513*4882a593Smuzhiyun 514*4882a593SmuzhiyunSysfs - cache set 515*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~ 516*4882a593Smuzhiyun 517*4882a593SmuzhiyunAvailable at /sys/fs/bcache/<cset-uuid> 518*4882a593Smuzhiyun 519*4882a593Smuzhiyunaverage_key_size 520*4882a593Smuzhiyun Average data per key in the btree. 521*4882a593Smuzhiyun 522*4882a593Smuzhiyunbdev<0..n> 523*4882a593Smuzhiyun Symlink to each of the attached backing devices. 524*4882a593Smuzhiyun 525*4882a593Smuzhiyunblock_size 526*4882a593Smuzhiyun Block size of the cache devices. 527*4882a593Smuzhiyun 528*4882a593Smuzhiyunbtree_cache_size 529*4882a593Smuzhiyun Amount of memory currently used by the btree cache 530*4882a593Smuzhiyun 531*4882a593Smuzhiyunbucket_size 532*4882a593Smuzhiyun Size of buckets 533*4882a593Smuzhiyun 534*4882a593Smuzhiyuncache<0..n> 535*4882a593Smuzhiyun Symlink to each of the cache devices comprising this cache set. 536*4882a593Smuzhiyun 537*4882a593Smuzhiyuncache_available_percent 538*4882a593Smuzhiyun Percentage of cache device which doesn't contain dirty data, and could 539*4882a593Smuzhiyun potentially be used for writeback. This doesn't mean this space isn't used 540*4882a593Smuzhiyun for clean cached data; the unused statistic (in priority_stats) is typically 541*4882a593Smuzhiyun much lower. 542*4882a593Smuzhiyun 543*4882a593Smuzhiyunclear_stats 544*4882a593Smuzhiyun Clears the statistics associated with this cache 545*4882a593Smuzhiyun 546*4882a593Smuzhiyundirty_data 547*4882a593Smuzhiyun Amount of dirty data is in the cache (updated when garbage collection runs). 548*4882a593Smuzhiyun 549*4882a593Smuzhiyunflash_vol_create 550*4882a593Smuzhiyun Echoing a size to this file (in human readable units, k/M/G) creates a thinly 551*4882a593Smuzhiyun provisioned volume backed by the cache set. 552*4882a593Smuzhiyun 553*4882a593Smuzhiyunio_error_halflife, io_error_limit 554*4882a593Smuzhiyun These determines how many errors we accept before disabling the cache. 555*4882a593Smuzhiyun Each error is decayed by the half life (in # ios). If the decaying count 556*4882a593Smuzhiyun reaches io_error_limit dirty data is written out and the cache is disabled. 557*4882a593Smuzhiyun 558*4882a593Smuzhiyunjournal_delay_ms 559*4882a593Smuzhiyun Journal writes will delay for up to this many milliseconds, unless a cache 560*4882a593Smuzhiyun flush happens sooner. Defaults to 100. 561*4882a593Smuzhiyun 562*4882a593Smuzhiyunroot_usage_percent 563*4882a593Smuzhiyun Percentage of the root btree node in use. If this gets too high the node 564*4882a593Smuzhiyun will split, increasing the tree depth. 565*4882a593Smuzhiyun 566*4882a593Smuzhiyunstop 567*4882a593Smuzhiyun Write to this file to shut down the cache set - waits until all attached 568*4882a593Smuzhiyun backing devices have been shut down. 569*4882a593Smuzhiyun 570*4882a593Smuzhiyuntree_depth 571*4882a593Smuzhiyun Depth of the btree (A single node btree has depth 0). 572*4882a593Smuzhiyun 573*4882a593Smuzhiyununregister 574*4882a593Smuzhiyun Detaches all backing devices and closes the cache devices; if dirty data is 575*4882a593Smuzhiyun present it will disable writeback caching and wait for it to be flushed. 576*4882a593Smuzhiyun 577*4882a593SmuzhiyunSysfs - cache set internal 578*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~ 579*4882a593Smuzhiyun 580*4882a593SmuzhiyunThis directory also exposes timings for a number of internal operations, with 581*4882a593Smuzhiyunseparate files for average duration, average frequency, last occurrence and max 582*4882a593Smuzhiyunduration: garbage collection, btree read, btree node sorts and btree splits. 583*4882a593Smuzhiyun 584*4882a593Smuzhiyunactive_journal_entries 585*4882a593Smuzhiyun Number of journal entries that are newer than the index. 586*4882a593Smuzhiyun 587*4882a593Smuzhiyunbtree_nodes 588*4882a593Smuzhiyun Total nodes in the btree. 589*4882a593Smuzhiyun 590*4882a593Smuzhiyunbtree_used_percent 591*4882a593Smuzhiyun Average fraction of btree in use. 592*4882a593Smuzhiyun 593*4882a593Smuzhiyunbset_tree_stats 594*4882a593Smuzhiyun Statistics about the auxiliary search trees 595*4882a593Smuzhiyun 596*4882a593Smuzhiyunbtree_cache_max_chain 597*4882a593Smuzhiyun Longest chain in the btree node cache's hash table 598*4882a593Smuzhiyun 599*4882a593Smuzhiyuncache_read_races 600*4882a593Smuzhiyun Counts instances where while data was being read from the cache, the bucket 601*4882a593Smuzhiyun was reused and invalidated - i.e. where the pointer was stale after the read 602*4882a593Smuzhiyun completed. When this occurs the data is reread from the backing device. 603*4882a593Smuzhiyun 604*4882a593Smuzhiyuntrigger_gc 605*4882a593Smuzhiyun Writing to this file forces garbage collection to run. 606*4882a593Smuzhiyun 607*4882a593SmuzhiyunSysfs - Cache device 608*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~ 609*4882a593Smuzhiyun 610*4882a593SmuzhiyunAvailable at /sys/block/<cdev>/bcache 611*4882a593Smuzhiyun 612*4882a593Smuzhiyunblock_size 613*4882a593Smuzhiyun Minimum granularity of writes - should match hardware sector size. 614*4882a593Smuzhiyun 615*4882a593Smuzhiyunbtree_written 616*4882a593Smuzhiyun Sum of all btree writes, in (kilo/mega/giga) bytes 617*4882a593Smuzhiyun 618*4882a593Smuzhiyunbucket_size 619*4882a593Smuzhiyun Size of buckets 620*4882a593Smuzhiyun 621*4882a593Smuzhiyuncache_replacement_policy 622*4882a593Smuzhiyun One of either lru, fifo or random. 623*4882a593Smuzhiyun 624*4882a593Smuzhiyundiscard 625*4882a593Smuzhiyun Boolean; if on a discard/TRIM will be issued to each bucket before it is 626*4882a593Smuzhiyun reused. Defaults to off, since SATA TRIM is an unqueued command (and thus 627*4882a593Smuzhiyun slow). 628*4882a593Smuzhiyun 629*4882a593Smuzhiyunfreelist_percent 630*4882a593Smuzhiyun Size of the freelist as a percentage of nbuckets. Can be written to to 631*4882a593Smuzhiyun increase the number of buckets kept on the freelist, which lets you 632*4882a593Smuzhiyun artificially reduce the size of the cache at runtime. Mostly for testing 633*4882a593Smuzhiyun purposes (i.e. testing how different size caches affect your hit rate), but 634*4882a593Smuzhiyun since buckets are discarded when they move on to the freelist will also make 635*4882a593Smuzhiyun the SSD's garbage collection easier by effectively giving it more reserved 636*4882a593Smuzhiyun space. 637*4882a593Smuzhiyun 638*4882a593Smuzhiyunio_errors 639*4882a593Smuzhiyun Number of errors that have occurred, decayed by io_error_halflife. 640*4882a593Smuzhiyun 641*4882a593Smuzhiyunmetadata_written 642*4882a593Smuzhiyun Sum of all non data writes (btree writes and all other metadata). 643*4882a593Smuzhiyun 644*4882a593Smuzhiyunnbuckets 645*4882a593Smuzhiyun Total buckets in this cache 646*4882a593Smuzhiyun 647*4882a593Smuzhiyunpriority_stats 648*4882a593Smuzhiyun Statistics about how recently data in the cache has been accessed. 649*4882a593Smuzhiyun This can reveal your working set size. Unused is the percentage of 650*4882a593Smuzhiyun the cache that doesn't contain any data. Metadata is bcache's 651*4882a593Smuzhiyun metadata overhead. Average is the average priority of cache buckets. 652*4882a593Smuzhiyun Next is a list of quantiles with the priority threshold of each. 653*4882a593Smuzhiyun 654*4882a593Smuzhiyunwritten 655*4882a593Smuzhiyun Sum of all data that has been written to the cache; comparison with 656*4882a593Smuzhiyun btree_written gives the amount of write inflation in bcache. 657