Debian Codegraph @

README

raw
1 # Stop and read before you blindly try this tool
2
3 What is this tool good for:
4
5 * Learn about how the Linux page cache works, and what syscalls exist to
6 interfere with its normal operation.
7 * Learn what hacks are necessary to intercept syscalls via their libc wrappers.
8
9 What this tool is **not** good for:
10
11 * Controlling how your page cache is used
12 * Why do you think some random tool you found on GitHub can do better than
13 the Linux Kernel?
14 * Defending against cache thrashing
15 * Use cgroups to bound the amount of memory a process has. See below or
16 search the internet, this is widely known, works reliably, and does not
17 introduce performance penalties or potentially dangerous behavior like this
18 tool does.
19 * Making a binary run faster
20 * `nocache` intercepts a bunch of syscalls and does lots of speculative work;
21 it will slow down your binary.
22
23 So then why does this tool exist?
24
25 * It was written in 2012, when cgroups, containerization etc. were all new
26 things. A decade+ on, they aren’t any more.
27
28 ## How to run a process and its children in a memory-bounded cgroup
29
30 Do this if you e.g. want to run a backup but don’t want your system to slow
31 down due to page cache thrashing.
32
33 ### If you use systemd
34
35 If your distro uses `systemd`, this is very easy. Systemd allows to run a
36 process (and its subprocesses) in a “scope”, which is a cgroup, and you can
37 specify parameters that get translated to cgroup limits.
38
39 When I run my backups, I do:
40
41 ```sh
42 $ systemd-run --scope --property=MemoryLimit=500M -- backup command
43 ```
44
45 The effect is that cache space stays bounded by an additional max 500MiB:
46
47 Before:
48
49 ```
50 $ free -h
51 total used free shared buff/cache available
52 Mem: 7.5G 2.4G 1.3G 1.0G 3.7G 3.7G
53 Swap: 9.7G 23M 9.7G
54
55 ```
56
57 During (notice how buff/cache only goes up by ~300MiB):
58
59 ```
60 free -h
61 total used free shared buff/cache available
62 Mem: 7.5G 2.5G 1.0G 1.1G 4.0G 3.6G
63 Swap: 9.7G 23M 9.7G
64 ```
65
66 #### How does this work?
67
68 Use `systemd-cgls` to list the cgroups systemd creates. On my system, the above
69 command creates a group called `run-u467.scope` in the `system.slice` parent
70 group; you can inspect its memory settings like this:
71
72 ```
73 $ mount | grep cgroup | grep memory
74 cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec latime,memory)
75
76 $ cat /sys/fs/cgroup/memory/system.slice/run-u467.scope/memory.limit_in_bytes
77 524288000
78 ```
79
80 ### The hard way
81
82 Install `cgroup-tools` and be prepared to enter your root password to initially
83 create cgroups.
84
85 ```
86 sudo env ppid=$$ sh -c '
87 cgcreate -g memory:backup ;
88 echo 500M > /sys/fs/cgroup/memory/backup/memory.limit_in_bytes ;
89 echo $ppid > /sys/fs/cgroup/memory/backup/tasks ;
90 '
91 ```
92
93 After entering this, your shell is a member of that cgroup, and any new process
94 spawned will belong to that cgroup, too, and inherit the memory limit. The
95 cgroups created like this won’t be cleaned up automatically.
96
97 More info: https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
98
99 # nocache - minimize filesystem caching effects
100
101 The `nocache` tool tries to minimize the effect an application has on
102 the Linux file system cache. This is done by intercepting the `open`
103 and `close` system calls and calling `posix_fadvise` with the
104 `POSIX_FADV_DONTNEED` parameter. Because the library remembers which
105 pages (ie., 4K-blocks of the file) were already in file system cache
106 when the file was opened, these will not be marked as "don't need",
107 because other applications might need that, although they are not
108 actively used (think: hot standby).
109
110 ## Installation and Usage
111
112 Just type `make`. Then, prepend `./nocache` to your command:
113
114 ```sh
115 ./nocache cp -a ~/ /mnt/backup/home-$(hostname)
116 ```
117
118 The command `make install` will install the shared library, man
119 pages and the `nocache`, `cachestats` and `cachedel` commands
120 under `/usr/local`. You can specify an alternate prefix by using
121 `make install PREFIX=/usr`.
122
123 Debian packages are available, see https://packages.qa.debian.org/n/nocache.html.
124
125 Please note that `nocache` will only build on a system that has
126 support for the `posix_fadvise` syscall and exposes it, too. This
127 should be the case on most modern Unices, but kfreebsd notably has no
128 support for this as of now.
129
130 ## Testing
131
132 For testing purposes, I included two small tools:
133
134 * `cachedel` calls `posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED)` on
135 the file argument. Thus, if the file is not accessed by any other
136 application, the pages will be eradicated from the fs cache.
137 Specifying -n <number> will repeat the syscall the given number of
138 times which can be useful in some circumstances (see below).
139 * `cachestats` has three modes: In quiet mode (`-q`), the exit status
140 is 0 (success) if the file is fully cached. In normal mode,
141 the number of cached vs. not-cached pages is printed. In verbose
142 mode (`-v`), an actual map is printed out, where each page that is
143 present in the cache is marked with `x`.
144
145 It looks like this:
146
147 ```
148 $ cachestats -v ~/somefile.mp3
149 pages in cache: 85/114 (74.6%) [filesize=453.5K, pagesize=4K]
150
151 cache map:
152 0: |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|
153 32: |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|
154 64: |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| | | | | | | | | | | | |
155 96: | | | | | | | | | | | | | | | | | |x|
156 ```
157
158 Also, you can use `vmstat 1` to view cache statistics.
159
160 For debugging purposes, you can specify a filename that `nocache` should log
161 debugging messages to via the `-D` command line switch, e.g. use `nocache
162 -D /tmp/nocache.log …`. Note that for simple testing the file `/dev/stderr`
163 might be a good choice.
164
165 ## Example run
166
167 Without `nocache`, the file will be fully cached when you copy it
168 somewhere:
169
170 $ ./cachestats ~/file.mp3
171 pages in cache: 154/1945 (7.9%) [filesize=7776.2K, pagesize=4K]
172 $ cp ~/file.mp3 /tmp
173 $ ./cachestats ~/file.mp3
174 pages in cache: 1945/1945 (100.0%) [filesize=7776.2K, pagesize=4K]
175
176 With `nocache`, the original caching state will be preserved.
177
178 $ ./cachestats ~/file.mp3
179 pages in cache: 154/1945 (7.9%) [filesize=7776.2K, pagesize=4K]
180 $ ./nocache cp ~/file.mp3 /tmp
181 $ ./cachestats ~/file.mp3
182 pages in cache: 154/1945 (7.9%) [filesize=7776.2K, pagesize=4K]
183
184
185 ## Limitations
186
187 The pre-loaded library tries really hard to catch all system calls
188 that open or close a file. This happens by "hijacking" the libc
189 functions that wrap the actual system calls. In some cases, this may
190 fail, for example because the application does some clever wrapping.
191 (That is the reason why `__openat_2` is defined: GNU `tar` uses this
192 instead of a regular `openat`.)
193
194 However, since the actual `fadvise` calls are performed right before
195 the file descriptor is closed, this may not happen if they are left
196 open when the application exits, although the destructor tries to do
197 that.
198
199 There are timing issues to consider, as well. If you consider `nocache
200 cat <file>`, in most (all?) cases the cache will not be restored. For
201 discussion and possible solutions see <http://lwn.net/Articles/480930/>.
202 My experience showed that in many cases you could "fix" this by doing
203 the `posix_fadvise` call *twice*. For both tools `nocache` and
204 `cachedel` you can specify the number using `-n`, like so:
205
206 $ nocache -n 2 cat ~/file.mp3
207
208 This actually only sets the environment variable `NOCACHE_NR_FADVISE`
209 to the specified value, and the shared library reads out this value.
210 If test number 3 in `t/basic.t` fails, then try increasing this number
211 until it works, e.g.:
212
213 $ env NOCACHE_NR_FADVISE=2 make test
214
215
216 One could also consider that the fact pages are kept mean the kernel
217 considers they are hot, and decide the overhead of allocating one byte
218 per page for mincore and the actual mincore calls are not worth it when
219 the kernel actually does keep some pages when it wants to.
220
221 In this case you can either run `nocache` with `-f` or set the
222 `NOCACHE_FLUSHALL` environment variable to 1, e.g.:
223
224 $ nocache -f cat ~/file.mp3
225 $ env NOCACHE_FLUSHALL=1 make test
226
227 By default `nocache` will only keep track of file descriptors less than `2^20`
228 that are opened by your application, in order to bound its memory
229 consumption. If you want to change this threshold, you can supply the
230 environment variable `NOCACHE_MAX_FDS` and set it to a higher (or lower) value.
231 It should specify a value one greater than the maximum file descriptor that
232 will be handled by `nocache`.
233
234 ## Acknowledgements
235
236 Most of the application logic is from Tobias Oetiker's patch for
237 `rsync` <http://insights.oetiker.ch/linux/fadvise.html>. Note however,
238 that `rsync` uses sockets, so if you try a `nocache rsync`, only
239 the local process will be intercepted.