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
59 static int btrfs_defrag(int fd) {
60 struct btrfs_ioctl_vol_args data;
65 return ioctl(fd, BTRFS_IOC_DEFRAG, &data);
68 static int pack_file(FILE *pack, const char *fn, bool on_btrfs) {
70 void *start = MAP_FAILED;
71 uint8_t vec[MINCORE_VEC_SIZE];
75 int r = 0, fd = -1, k;
80 if ((fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW)) < 0) {
81 log_warning("open(%s) failed: %m", fn);
86 if ((k = file_verify(fd, fn, &st)) <= 0) {
94 l = PAGE_ALIGN(st.st_size);
95 if ((start = mmap(NULL, l, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
96 log_warning("mmap(%s) failed: %m", fn);
101 if (mincore(start, l, vec) < 0) {
102 log_warning("mincore(%s) failed: %m", fn);
110 pages = l / PAGE_SIZE;
112 for (c = 0; c < pages; c++) {
113 bool new_mapped = (vec[c] & 1);
115 if (!mapped && new_mapped)
117 else if (mapped && !new_mapped) {
118 fwrite(&b, sizeof(b), 1, pack);
119 fwrite(&c, sizeof(c), 1, pack);
121 log_debug("%s: page %u to %u", fn, b, c);
127 /* We don't write any range data if we should read the entire file */
128 if (mapped && b > 0) {
129 fwrite(&b, sizeof(b), 1, pack);
130 fwrite(&c, sizeof(c), 1, pack);
132 log_debug("%s: page %u to %u", fn, b, c);
137 fwrite(&b, sizeof(b), 1, pack);
138 fwrite(&b, sizeof(b), 1, pack);
141 if (start != MAP_FAILED)
145 close_nointr_nofail(fd);
150 static unsigned long fd_first_block(int fd) {
152 struct fiemap fiemap;
153 struct fiemap_extent extent;
157 data.fiemap.fm_length = ~0ULL;
158 data.fiemap.fm_extent_count = 1;
160 if (ioctl(fd, FS_IOC_FIEMAP, &data) < 0)
163 if (data.fiemap.fm_mapped_extents <= 0)
166 if (data.fiemap.fm_extents[0].fe_flags & FIEMAP_EXTENT_UNKNOWN)
169 return (unsigned long) data.fiemap.fm_extents[0].fe_physical;
177 static int qsort_compare(const void *a, const void *b) {
178 const struct item *i, *j;
183 if (i->block < j->block)
185 if (i->block > j->block)
188 return strcmp(i->path, j->path);
191 static int collect(const char *root) {
197 struct pollfd pollfd[_FD_MAX];
198 int fanotify_fd = -1, signal_fd = -1, r = 0;
200 Hashmap *files = NULL;
205 char *pack_fn_new = NULL, *pack_fn = NULL;
206 bool on_ssd, on_btrfs;
211 if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)) < 0)
212 log_warning("Failed to set IDLE IO priority class: %m");
214 assert_se(sigemptyset(&mask) == 0);
215 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
216 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
218 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
219 log_error("signalfd(): %m");
224 if (!(files = hashmap_new(string_hash_func, string_compare_func))) {
225 log_error("Failed to allocate set.");
230 if ((fanotify_fd = fanotify_init(FAN_CLOEXEC, O_RDONLY|O_LARGEFILE|O_CLOEXEC|O_NOATIME)) < 0) {
231 log_error("Failed to create fanotify object: %m");
236 if (fanotify_mark(fanotify_fd, FAN_MARK_ADD|FAN_MARK_MOUNT, FAN_OPEN, AT_FDCWD, root) < 0) {
237 log_error("Failed to mark %s: %m", root);
245 pollfd[FD_FANOTIFY].fd = fanotify_fd;
246 pollfd[FD_FANOTIFY].events = POLLIN;
247 pollfd[FD_SIGNAL].fd = signal_fd;
248 pollfd[FD_SIGNAL].events = POLLIN;
252 "STATUS=Collecting readahead data");
254 log_debug("Collecting...");
258 struct fanotify_event_metadata metadata;
262 struct fanotify_event_metadata *m;
264 if (hashmap_size(files) > READAHEAD_FILES_MAX)
267 if (poll(pollfd, _FD_MAX, -1) < 0) {
272 log_error("poll(): %m");
277 if (pollfd[FD_SIGNAL].revents != 0)
280 if ((n = read(fanotify_fd, &data, sizeof(data))) < 0) {
282 if (errno == EINTR || errno == EAGAIN)
285 log_error("Failed to read event: %m");
291 while (FAN_EVENT_OK(m, n)) {
293 if (m->pid != my_pid && m->fd >= 0) {
297 snprintf(fn, sizeof(fn), "/proc/self/fd/%i", m->fd);
300 if ((k = readlink_malloc(fn, &p)) >= 0) {
302 if (hashmap_get(files, p))
308 ul = fd_first_block(m->fd);
310 if ((k = hashmap_put(files, p, ULONG_TO_PTR(ul))) < 0) {
311 log_warning("set_put() failed: %s", strerror(-k));
317 log_warning("readlink(%s) failed: %s", fn, strerror(-k));
321 close_nointr_nofail(m->fd);
323 m = FAN_EVENT_NEXT(m, n);
327 if (fanotify_fd >= 0) {
328 close_nointr_nofail(fanotify_fd);
332 log_debug("Writing Pack File...");
334 on_ssd = fs_on_ssd(root);
335 log_debug("On SSD: %s", yes_no(on_ssd));
337 on_btrfs = statfs(root, &sfs) >= 0 && sfs.f_type == BTRFS_SUPER_MAGIC;
338 log_debug("On btrfs: %s", yes_no(on_btrfs));
340 asprintf(&pack_fn, "%s/.readahead", root);
341 asprintf(&pack_fn_new, "%s/.readahead.new", root);
343 if (!pack_fn || !pack_fn_new) {
344 log_error("Out of memory");
349 if (!(pack = fopen(pack_fn_new, "we"))) {
350 log_error("Failed to open pack file: %m");
355 fputs(CANONICAL_HOST "\n", pack);
356 putc(on_ssd ? 'S' : 'R', pack);
358 if (on_ssd || on_btrfs) {
360 /* On SSD or on btrfs, just write things out in the
361 * order the files were accessed. */
363 HASHMAP_FOREACH_KEY(q, p, files, i)
364 pack_file(pack, p, on_btrfs);
366 struct item *ordered, *j;
369 /* On rotating media, order things by the block
372 log_debug("Ordering...");
374 n = hashmap_size(files);
375 if (!(ordered = new(struct item, n))) {
376 log_error("Out of memory");
382 HASHMAP_FOREACH_KEY(q, p, files, i) {
384 j->block = PTR_TO_ULONG(q);
388 assert(ordered + n == j);
390 qsort(ordered, n, sizeof(struct item), qsort_compare);
392 for (k = 0; k < n; k++)
393 pack_file(pack, ordered[k].path, on_btrfs);
398 log_debug("Finalizing...");
403 log_error("Failed to write pack file.");
408 if (rename(pack_fn_new, pack_fn) < 0) {
409 log_error("Failed to rename readahead file: %m");
420 if (fanotify_fd >= 0)
421 close_nointr_nofail(fanotify_fd);
424 close_nointr_nofail(signal_fd);
434 while ((p = hashmap_steal_first_key(files)))
442 int main(int argc, char *argv[]) {
444 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
445 log_parse_environment();
449 log_info("Disabling readahead collector due to low memory.");
453 if (collect(argc >= 2 ? argv[1] : "/") < 0)