Slashdot Mirror


Software SSD Cache Implementation For Linux?

Annirak writes "With the bottom dropping out of the magnetic disk market and SSD prices still over $3/GB, I want to know if there is a way to to get the best of both worlds. Ideally, a caching algorithm would store frequently used sectors, or sectors used during boot or application launches (hot sectors), to the SSD. Adaptec has a firmware implementation of this concept, called MaxIQ, but this is only for use on their RAID controllers and only works with their special, even more expensive, SSD. Silverstone recently released a device which does this for a single disk, but it is limited: it caches the first part of the magnetic disk, up to the size of the SSD, rather than caching frequently used sectors. The FS-Cache implementation in recent Linux kernels seems to be primarily intended for use in NFS and AFS, without much provision for speeding up local filesystems. Is there a way to use an SSD to act as a hot sector cache for a magnetic disk under Linux?"

18 of 297 comments (clear)

  1. isn't 40 GB enough for applications? by owlstead · · Score: 3, Interesting

    Is there really a need for this? Intel 40 GB SSD still has a read speed of 170 MB/s and costs about 100 euro here in NL. Why have some kind of experimental configuration while prices are like that? OK, 35 MB/s write speed is not that high, but with the high IOPS and seek times you still have most of the benefits.

    I can see why you would want something like this, but I doubt the benefits are that large over a normal SSD + HDD configuration.

    1. Re:isn't 40 GB enough for applications? by Unit3 · · Score: 4, Informative

      They are huge for larger applications. Database servers, for instance, can see performance increases in the magnitude of 10-20x the number of transactions per second when using a scheme like this for datasets that are too large to fit in RAM.

      --
      -- sudo.ca
  2. ZFS by Anonymous Coward · · Score: 5, Informative

    ZFS can do this (http://www.solarisinternals.com/wiki/index.php/ZFS_Best_Practices_Guide#Separate_Cache_Devices) but I don't know about zfs-fuse

  3. ZFS L2ARC by jdong · · Score: 5, Informative

    Not Linux per se, but the same idea is implemented nicely on ZFS through its L2ARC: http://blogs.sun.com/brendan/entry/test

  4. bcache by Wesley+Felter · · Score: 5, Informative

    http://lkml.org/lkml/2010/4/5/41

    I'm a little surprised at the lack of response on linux-kernel.

    Solaris and DragonFly have already implemented this feature; I'm surprised that Linux is so far behind.

    1. Re:bcache by Kento · · Score: 5, Informative

      Hey, at least someone noticed :)

      That version was pretty raw. The current one is a lot farther along than that, but it's still got a ways to go - I'm hoping to have it ready for inclusion in a few months, if I can keep working on it full time. Anyone want to fund me? :D

  5. Waste of time by onefriedrice · · Score: 5, Informative

    What a waste of time. Just put /home on a magnetic disk and everything else on the SSD. This way, you can get away with a small (very affordable) SSD for your binaries, libraries, config files, and app data, and use tried and true magnetic for your important files. Your own personal files don't need to be on a super fast disk anyway because they don't get as much access as you would think, but your binaries and config files get accessed a lot (unless you have a lot of RAM to cache that, which I also recommend). I've been doing this for over a year and enjoying 10 second boots, and instant program access coldstarts (including openoffice and firefox).

    I personally fit all my partitions except /home in only 12.7GB (the SSD is 30GB). Seriously, best upgrade ever. I will never put my root partition on a magnetic drive ever again.

    --
    This author takes full ownership and responsibility for the unpopular opinions outlined above.
  6. Re:I don't get it by Anonymous Coward · · Score: 5, Informative

    The idea is to use the SSD as a second-level disk cache. So instead of simply discarding cached data under memory pressure, it's written to the SSD. It's still way slower than RAM, but it's got much better random-access performance characteristics than spinning rust and it's large compared to RAM.

    As for how to do it in Linux, I'm not aware of a way. If you are open to the possibility of using other operating systems, this functionality is part of OpenSolaris (google for "zfs l2arc" for more information).

    Cache Devices
              Devices can be added to a storage pool as "cache devices."
              These devices provide an additional layer of caching between
              main memory and disk. For read-heavy workloads, where the
              working set size is much larger than what can be cached in
              main memory, using cache devices allow much more of this
              working set to be served from low latency media. Using cache
              devices provides the greatest performance improvement for
              random read-workloads of mostly static content.

              To create a pool with cache devices, specify a "cache" vdev
              with any number of devices. For example:

                  # zpool create pool c0d0 c1d0 cache c2d0 c3d0

              The content of the cache devices is considered volatile, as
              is the case with other system caches.

    You can also use it as an intent log, which can dramatically improve write performance:

    Intent Log
              The ZFS Intent Log (ZIL) satisfies POSIX requirements for
              synchronous transactions. For instance, databases often
              require their transactions to be on stable storage devices
              when returning from a system call. NFS and other applica-
              tions can also use fsync() to ensure data stability. By
              default, the intent log is allocated from blocks within the
              main pool. However, it might be possible to get better per-
              formance using separate intent log devices such as NVRAM or
              a dedicated disk. For example:

                  # zpool create pool c0d0 c1d0 log c2d0

              Multiple log devices can also be specified, and they can be
              mirrored. See the EXAMPLES section for an example of mirror-
              ing multiple log devices.

              Log devices can be added, replaced, attached, detached, and
              imported and exported as part of the larger pool. Mirrored
              log devices can be removed by specifying the top-level mir-
              ror for the log.

  7. Re:I don't get it by Colin+Smith · · Score: 4, Insightful

    so

    CPU L1
    CPU L2
    CPU L3
    RAM
    SSD
    DISK
    NETWORK
    Internet

    I estimate SSDs would be closer to Level 5 cache.

     

    --
    Deleted
  8. dm-cache by Gyver_lb · · Score: 3, Informative

    google dm-cache. Not updated since 2.6.29 though.

  9. Re:Counter-Productive by pwnies · · Score: 3, Informative

    Sadly, nowadays this is a myth. Current MLC and SLC SSD's have (on average) 10,000 and 100,000 writes (respectively) before any bitwear will occur. While this number is small, remember that all modern mainstream SSD's have wear leveling algorithms built into the controller. Intel rates their drives' minimum useful life at 5 years [pdf link - page 10], with an estimated life of 20 years. Note that this number is based on 20GB of writes per day, every day. SSD's nowadays will have no problems with acting as a cache for the system.

  10. Re:I don't get it by TheRaven64 · · Score: 4, Informative

    The submitter wants something like ZFS's L2ARC, which uses the flash as an intermediate cache between the RAM cache and the disk. This works very well for a lot of workloads. Since Linux users appear to be allowed to say 'switch to Linux' as an answer to questions about Windows, it only seems fair that 'switch to Solaris of FreeBSD' would be a valid solution to this problem.

    --
    I am TheRaven on Soylent News
  11. Re:I don't get it by owlstead · · Score: 3, Interesting

    A fast SSD is not 1 Gb/s under ideal conditions. A fast SSD is up to 2 Gb/s (about 250 MB/s) under real life conditions (while reading). Anyway, it still makes sense to cache network content to disk if the other side of the connection is slow or not reliable.

  12. FSCache would work except... by Jah-Wren+Ryel · · Score: 4, Interesting

    I have a similar problem and I tried the FSCache approach:

    I've got two raids.
    One is optimized for big ass files read contiguously and has raid6 redundancy.
    The other is a much smaller JBOD that I can reconfigure via mdraid to anything that linux supports in software.

    The problem is that 5% of the big ass files need read-only random access and that kills throughput for anything else going on. It takes me down from ~400MB/s to 15MB/s.

    So, I thought I'd use the FSCache approach and use the JBOD as the cache.
    I did an NFS mount over loopback and pointed the fscache to the JBOD.
    It worked great got practically full throughput for contiguous access, for about 10 hours and then crashed the system.

    Apparently NFS over loopback is well known to be broken in linux and has been since, essentially, forever.
    I was stunned, it had never even occurred to me that NFS over loopback would be broken. Its freaking 2010 - that something I had been using on Sun0S 3 a bazillion years ago didn't work on linux today had not even entered my mind.

    I've also tried replicating the files from the raid6 to the jbod, but that quickly turned into a hassle keeping everything syncronized between the files on disk and the applications that create the files on the raid6 and the apps that use the files on the JBOD. Plus, it doesn't scale out past the size of the JBOD, which I also ran into.

    So now, I'm looking at putting the apps that need random access reads to the data in a VM and NFS mounting it with cache to the VM hoping to avoid the NFS-broken-over-loopback problem. I haven't had time to implement it yet, and personally and leery of doing so since I have to wonder what new "known-broken" problems will bite me in the ass.

    So, if there is a better way, I am dying to hear it, unfortunately solaris/freebsd is not an option...

    --
    When information is power, privacy is freedom.
  13. Re:Buffers? by m.dillon · · Score: 3, Informative

    The single largest problem addressed by e.g. DragonFly's swapcache is meta-data caching to make scans and other operations on large filesystems with potentially millions or tens of millions of files a fast operation. Secondarily for something like DragonFly's HAMMER filesystem which can store a virtually unlimited number of live-accessable snapshots of the filesystem you can wind up with not just tens of millions of inodes, but hundreds of millions of inodes. Being able to efficiently operate on such large filesystems requires very low latency access to meta-data. Swapcache does a very good job providing the low latency necessary.

    System main memory just isn't big enough to cache all those inodes in a cost-effective manner. 14 million inodes takes around 6G of storage to cache. Well, you can do the math. Do you spend tens of thousands of dollars on a big whopping server with 60G of ram or do you spend a mere $200 on a 80G SSD?

    -Matt

  14. Re:Wrong. Swap often acts as a cache. by m.dillon · · Score: 4, Informative

    The way DragonFly's swapcache works is that VM pages (cached in ram) go from the active queue to the inactive queue to the cache (almost free) queue to the free queue. VM pages sitting in the inactive queue are subject to being written out to the swapcache. VM pages in the active queue (or cache or free queues) are not considered.

    In otherwords, simply accessing cacheable data or meta-data from the hard drive does not itself trigger writing to the SSD swapcache. It's only when the cached VM pages are pushed out of the active queue due to memory pressure and are clearly heading out the door when DragonFly decides to write them to the SSD.

    This prevents SSD write activity from interfering with the operation of the production system and also tends to do a good job selecting what data to write to the SSD when and what data not to. A file which is in constant use by the system just stays in ram, there's no point writing it out to the SSD.

    With respect to deciding what data to cache and what data not to, with meta-data its simple. You cache as much meta-data as you can because every piece of meta-data gives you a multiplicative performance improvement. With file data it is harder since you don't want to try to cycle e.g. a terrabyte of data through a 40G swapcache. The production system's working data set at any given moment needs to either fit in the swapcache or you need to carefully select which directory topologies you want to cache.

    -Matt

  15. Re:Wrong. Swap often acts as a cache. by m.dillon · · Score: 3, Informative

    OS's have traditionally discarded clean cache data when memory pressure forces the pages out. Swap traditionally applied only to dirty anonymous memory (The OS needs to write dirty data somewhere, after all, and if it isn't backed by a file then that is what swap is for).

    However in the last decade traditional paging to swap has fallen by the wayside as memory capacities have increased. Most of the data in ram on systems today is clean data, not dirty data, and most of the dirty data is backed by a file (e.g. write()s to a database or something like that). On most systems today if you look at swap space use you find it near zero.

    But the concept of swap can trivially be expanded to cover more areas of interest. tmpfs (tmpfs, md, mfs, etc) is a good example. For that matter anonymous memory for VMs can be backed by swap. It is very desireable to back the memory for a VM with either a tmpfs-based file or just straight anonymous memory instead of a file in a normal filesystem. That is a good use for swap too.

    It isn't that big a leap to expand swap coverage to also cache clean data. It took about two weeks to implement the basics on DragonFly. Those operating systems which don't have this capability will probably get it as time goes on simply because it is an extremely useful mechanic for interfacing a SSD-based cache into a system. It is also probably the cleanest and simplest way to implement this sort of cache, and it pairs up well with the strengths of the SSD storage mechanic. Since you can reallocate swap space when something is rewritten there are virtually no write amplification effects and the storage on the SSD is cycled very nicely. You get much better wear leveling than you would if you tried to map a normal filesystem (or mirror the blocks associated with a normal filesystem) on top of the SSD.

    -Matt

  16. Re:Wrong. Swap often acts as a cache. by Score+Whore · · Score: 5, Informative

    Solaris certainly doesn't. What developer would ever code this kind of behavior? Non-dirty filesystem data in the cache is already on disk, what would be the rational to write it out to another part of the disk? That's just stupid. Non-dirty pages are thrown away when RAM is in demand. Dirty filesystem data is just written to disk. Then the pages become non-dirty and can be freed at any time. Possibly immediately if there is demand.

    Scenario A:

    1. File is read and data is copied into system memory where is it buffered. Time passes.
    2. Memory usage skyrockets.
    3. Kernel writes data to swap space and frees the memory for use by other processes.
    4. Later an application wants that data. Kernel reads data from swap space.

    Scenario B:

    1. File is read and data is copied into system memory where is it buffered. Time passes.
    2. Memory usage skyrockets.
    3. Kernel locates non-dirty cached data and frees that page for use by other processes.
    4. Later an application wants that data. Kernel reads data from original file on disk.

    Differences between scenario A & B:

    Scenario A has two disk IOs (steps 3&4) during memory pressure. Scenario B has one (step 4).
    Scenario A uses limited swap space to store duplicate data. Scenario B doesn't.

    And no, Solaris doesn't cache slow devices (tape, dvd-rom, etc.) either. If you choose to access those types of devices, that is your choice. The OS isn't going to save your ass. If you want it cached, make your application do the caching.

    Also, I'm not considering special purpose systems such as ZFS's l2arc or other similar/more generalized systems that utilize SSD as a midway point between RAM and HDD. We're talking generic swap space and filesystem caches.