docs: Add VERSION section giving first version a plugin/filter appeared.
[nbdkit/ericb.git] / filters / cache / nbdkit-cache-filter.pod
blob3c2ad3c4a2215a8d24faafbf06a9dbcc28677ada
1 =head1 NAME
3 nbdkit-cache-filter - nbdkit caching filter
5 =head1 SYNOPSIS
7  nbdkit --filter=cache plugin [cache=writeback|writethrough|unsafe]
8                               [cache-max-size=SIZE]
9                               [cache-high-threshold=N]
10                               [cache-low-threshold=N]
11                               [cache-on-read=true|false]
12                               [plugin-args...]
14 =head1 DESCRIPTION
16 C<nbdkit-cache-filter> is a filter that adds caching on top of a
17 plugin.  This is useful if a plugin is slow or expensive to use,
18 because nbdkit will try to minimize requests to the plugin by caching
19 previous requests.
21 Note that many NBD I<clients> are able to do caching, and because the
22 caching happens on the client side it will usually be more effective
23 than caching inside the server.  This filter can be used if the client
24 does not have effective caching, or (with C<cache=unsafe>) to defeat
25 flush requests from the client (which is unsafe and can cause data
26 loss, as the name suggests).
28 Note that the use of this filter rounds the image size down to a
29 multiple of the caching granularity (the larger of 4096 or the
30 C<f_bsize> field of L<fstatvfs(3)>), to ease the implementation. If
31 you need to round the image size up instead to access the last few
32 bytes, combine this filter with L<nbdkit-truncate-filter(1)>.
34 This filter only caches image contents.  To cache image metadata, use
35 L<nbdkit-cacheextents-filter(1)> between this filter and the plugin.
36 To accelerate sequential reads, use L<nbdkit-readahead-filter(1)>
37 instead.
39 =head1 PARAMETERS
41 =over 4
43 =item B<cache=writeback>
45 Store writes in the cache.  They are not written to the plugin unless
46 an explicit flush is done by the client.
48 This is the default caching mode, and is safe if your client issues
49 flush requests correctly (which is true for modern Linux and other
50 well-written NBD clients).
52 =item B<cache=writethrough>
54 Always force writes through to the plugin.
56 This makes the cache less effective, but is necessary if your client
57 does not issue correct flush requests.
59 =item B<cache=unsafe>
61 Ignore flush requests.  Never write to the plugin unless the cache
62 grows too large.
64 This is dangerous and can cause data loss, but this may be acceptable
65 if you only use it for testing or with data that you don't care about
66 or can cheaply reconstruct.
68 =item B<cache-max-size=>SIZE
70 =item B<cache-high-threshold=>N
72 =item B<cache-low-threshold=>N
74 Limit the size of the cache to C<SIZE>.  See L</CACHE MAXIMUM SIZE> below.
76 =item B<cache-on-read=true>
78 Cache read requests as well as write and cache requests.  Any time a
79 block is read from the plugin, it is saved in the cache (if there is
80 sufficient space) so the same data can be served more quickly later.
82 Note that if the underlying data served by the plugin can be modified
83 by some other means (eg. something else can write to a file which is
84 being served by L<nbdkit-file-plugin(1)>), this option will cause
85 nbdkit to serve stale data because reads won't always go through to
86 the plugin.
88 =item B<cache-on-read=false>
90 Do not cache read requests (this is the default).
92 =back
94 =head1 CACHE MAXIMUM SIZE
96 By default the cache can grow to any size (although not larger than
97 the virtual size of the underlying plugin) and you have to ensure
98 there is sufficient space in C<$TMPDIR> for it.
100 Using the parameters C<cache-max-size>, C<cache-high-threshold> and
101 C<cache-low-threshold> you can limit the maximum size of the cache.
103 This requires kernel and filesystem support (for L<fallocate(2)>
104 C<FALLOC_FL_PUNCH_HOLE>), so it may not work on all platforms.
106 Some examples:
108 =over 4
110 =item C<cache-max-size=1G>
112 The cache is limited to around 1 gigabyte.
114 =item C<cache-max-size=1G cache-high-threshold=95 cache-low-threshold=80>
116 Once the cache size reaches 972M (95% of 1G), blocks are reclaimed
117 from the cache until the size is reduced to 819M (80% of 1G).
119 =back
121 The way this works is once the size of the cache exceeds
122 S<C<SIZE> ✕ the high threshold>, the filter works to reduce the size
123 of the cache until it is less than S<C<SIZE> ✕ the low threshold>.
124 Once the size is below the low threshold, no more reclaim work is done
125 until the size exceeds the high threshold again.
127 The default thresholds are high 95% and low 80%.  You must set
128 S<0 E<lt> low E<lt> high>.  The thresholds are expressed as integer
129 percentages of C<cache-max-size>.
131 Least recently used blocks are discarded first.
133 =head1 ENVIRONMENT VARIABLES
135 =over 4
137 =item C<TMPDIR>
139 The cache is stored in a temporary file located in F</var/tmp> by
140 default.  You can override this location by setting the C<TMPDIR>
141 environment variable before starting nbdkit.
143 =back
145 =head1 FILES
147 =over 4
149 =item F<$filterdir/nbdkit-cache-filter.so>
151 The filter.
153 Use C<nbdkit --dump-config> to find the location of C<$filterdir>.
155 =back
157 =head1 VERSION
159 C<nbdkit-cache-filter> first appeared in nbdkit 1.2.
161 =head1 SEE ALSO
163 L<nbdkit(1)>,
164 L<nbdkit-file-plugin(1)>,
165 L<nbdkit-cacheextents-filter(1)>,
166 L<nbdkit-readahead-filter(1)>,
167 L<nbdkit-truncate-filter(1)>,
168 L<nbdkit-filter(3)>,
169 L<qemu-img(1)>.
171 =head1 AUTHORS
173 Eric Blake
175 Richard W.M. Jones
177 =head1 COPYRIGHT
179 Copyright (C) 2018-2019 Red Hat Inc.