1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <linux/limits.h>
30 #include <sys/select.h>
32 #include <sys/types.h>
35 #include <linux/fanotify.h>
36 #include <sys/signalfd.h>
40 #include <linux/fiemap.h>
41 #include <sys/ioctl.h>
47 #include "sd-daemon.h"
49 #include "readahead-common.h"
51 #define MINCORE_VEC_SIZE (READAHEAD_FILE_SIZE_MAX/PAGE_SIZE)
55 * - detect ssd/lvm/... on btrfs
56 * - read ahead directories
57 * - sd_readahead_cancel
60 static int btrfs_defrag(int fd) {
61 struct btrfs_ioctl_vol_args data;
66 return ioctl(fd, BTRFS_IOC_DEFRAG, &data);
69 static int pack_file(FILE *pack, const char *fn, bool on_btrfs) {
71 void *start = MAP_FAILED;
72 uint8_t vec[MINCORE_VEC_SIZE];
76 int r = 0, fd = -1, k;
81 if ((fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW)) < 0) {
82 log_warning("open(%s) failed: %m", fn);
87 if ((k = file_verify(fd, fn, &st)) <= 0) {
95 l = PAGE_ALIGN(st.st_size);
96 if ((start = mmap(NULL, l, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
97 log_warning("mmap(%s) failed: %m", fn);
102 if (mincore(start, l, vec) < 0) {
103 log_warning("mincore(%s) failed: %m", fn);
111 pages = l / PAGE_SIZE;
113 for (c = 0; c < pages; c++) {
114 bool new_mapped = (vec[c] & 1);
116 if (!mapped && new_mapped)
118 else if (mapped && !new_mapped) {
119 fwrite(&b, sizeof(b), 1, pack);
120 fwrite(&c, sizeof(c), 1, pack);
122 log_debug("%s: page %u to %u", fn, b, c);
128 /* We don't write any range data if we should read the entire file */
129 if (mapped && b > 0) {
130 fwrite(&b, sizeof(b), 1, pack);
131 fwrite(&c, sizeof(c), 1, pack);
133 log_debug("%s: page %u to %u", fn, b, c);
138 fwrite(&b, sizeof(b), 1, pack);
139 fwrite(&b, sizeof(b), 1, pack);
142 if (start != MAP_FAILED)
146 close_nointr_nofail(fd);
151 static unsigned long fd_first_block(int fd) {
153 struct fiemap fiemap;
154 struct fiemap_extent extent;
158 data.fiemap.fm_length = ~0ULL;
159 data.fiemap.fm_extent_count = 1;
161 if (ioctl(fd, FS_IOC_FIEMAP, &data) < 0)
164 if (data.fiemap.fm_mapped_extents <= 0)
167 if (data.fiemap.fm_extents[0].fe_flags & FIEMAP_EXTENT_UNKNOWN)
170 return (unsigned long) data.fiemap.fm_extents[0].fe_physical;
178 static int qsort_compare(const void *a, const void *b) {
179 const struct item *i, *j;
184 if (i->block < j->block)
186 if (i->block > j->block)
189 return strcmp(i->path, j->path);
192 static int collect(const char *root) {
198 struct pollfd pollfd[_FD_MAX];
199 int fanotify_fd = -1, signal_fd = -1, r = 0;
201 Hashmap *files = NULL;
206 char *pack_fn_new = NULL, *pack_fn = NULL;
207 bool on_ssd, on_btrfs;
212 if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)) < 0)
213 log_warning("Failed to set IDLE IO priority class: %m");
215 assert_se(sigemptyset(&mask) == 0);
216 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
217 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
219 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
220 log_error("signalfd(): %m");
225 if (!(files = hashmap_new(string_hash_func, string_compare_func))) {
226 log_error("Failed to allocate set.");
231 if ((fanotify_fd = fanotify_init(FAN_CLOEXEC, O_RDONLY|O_LARGEFILE|O_CLOEXEC|O_NOATIME)) < 0) {
232 log_error("Failed to create fanotify object: %m");
237 if (fanotify_mark(fanotify_fd, FAN_MARK_ADD|FAN_MARK_MOUNT, FAN_OPEN, AT_FDCWD, root) < 0) {
238 log_error("Failed to mark %s: %m", root);
246 pollfd[FD_FANOTIFY].fd = fanotify_fd;
247 pollfd[FD_FANOTIFY].events = POLLIN;
248 pollfd[FD_SIGNAL].fd = signal_fd;
249 pollfd[FD_SIGNAL].events = POLLIN;
253 "STATUS=Collecting readahead data");
255 log_debug("Collecting...");
259 struct fanotify_event_metadata metadata;
263 struct fanotify_event_metadata *m;
265 if (hashmap_size(files) > READAHEAD_FILES_MAX)
268 if (poll(pollfd, _FD_MAX, -1) < 0) {
273 log_error("poll(): %m");
278 if (pollfd[FD_SIGNAL].revents != 0)
281 if ((n = read(fanotify_fd, &data, sizeof(data))) < 0) {
283 if (errno == EINTR || errno == EAGAIN)
286 log_error("Failed to read event: %m");
292 while (FAN_EVENT_OK(m, n)) {
294 if (m->pid != my_pid && m->fd >= 0) {
298 snprintf(fn, sizeof(fn), "/proc/self/fd/%i", m->fd);
301 if ((k = readlink_malloc(fn, &p)) >= 0) {
303 if (hashmap_get(files, p))
309 ul = fd_first_block(m->fd);
311 if ((k = hashmap_put(files, p, ULONG_TO_PTR(ul))) < 0) {
312 log_warning("set_put() failed: %s", strerror(-k));
318 log_warning("readlink(%s) failed: %s", fn, strerror(-k));
322 close_nointr_nofail(m->fd);
324 m = FAN_EVENT_NEXT(m, n);
328 if (fanotify_fd >= 0) {
329 close_nointr_nofail(fanotify_fd);
333 log_debug("Writing Pack File...");
335 on_ssd = fs_on_ssd(root);
336 log_debug("On SSD: %s", yes_no(on_ssd));
338 on_btrfs = statfs(root, &sfs) >= 0 && sfs.f_type == BTRFS_SUPER_MAGIC;
339 log_debug("On btrfs: %s", yes_no(on_btrfs));
341 asprintf(&pack_fn, "%s/.readahead", root);
342 asprintf(&pack_fn_new, "%s/.readahead.new", root);
344 if (!pack_fn || !pack_fn_new) {
345 log_error("Out of memory");
350 if (!(pack = fopen(pack_fn_new, "we"))) {
351 log_error("Failed to open pack file: %m");
356 fputs(CANONICAL_HOST "\n", pack);
357 putc(on_ssd ? 'S' : 'R', pack);
359 if (on_ssd || on_btrfs) {
361 /* On SSD or on btrfs, just write things out in the
362 * order the files were accessed. */
364 HASHMAP_FOREACH_KEY(q, p, files, i)
365 pack_file(pack, p, on_btrfs);
367 struct item *ordered, *j;
370 /* On rotating media, order things by the block
373 log_debug("Ordering...");
375 n = hashmap_size(files);
376 if (!(ordered = new(struct item, n))) {
377 log_error("Out of memory");
383 HASHMAP_FOREACH_KEY(q, p, files, i) {
385 j->block = PTR_TO_ULONG(q);
389 assert(ordered + n == j);
391 qsort(ordered, n, sizeof(struct item), qsort_compare);
393 for (k = 0; k < n; k++)
394 pack_file(pack, ordered[k].path, on_btrfs);
399 log_debug("Finalizing...");
404 log_error("Failed to write pack file.");
409 if (rename(pack_fn_new, pack_fn) < 0) {
410 log_error("Failed to rename readahead file: %m");
421 if (fanotify_fd >= 0)
422 close_nointr_nofail(fanotify_fd);
425 close_nointr_nofail(signal_fd);
435 while ((p = hashmap_steal_first_key(files)))
443 int main(int argc, char *argv[]) {
445 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
446 log_parse_environment();
450 log_info("Disabling readahead collector due to low memory.");
454 if (collect(argc >= 2 ? argv[1] : "/") < 0)