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>
46 #include "sd-daemon.h"
48 #include "readahead-common.h"
56 #define MINCORE_VEC_SIZE (READAHEAD_FILE_SIZE_MAX/PAGE_SIZE)
58 static int pack_file(FILE *pack, const char *fn) {
60 void *start = MAP_FAILED;
61 uint8_t vec[MINCORE_VEC_SIZE];
65 int r = 0, fd = -1, k;
70 if ((fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW)) < 0) {
71 log_warning("open(%s) failed: %m", fn);
76 if ((k = file_verify(fd, fn, &st)) <= 0) {
81 l = PAGE_ALIGN(st.st_size);
82 if ((start = mmap(NULL, l, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
83 log_warning("mmap(%s) failed: %m", fn);
88 if (mincore(start, l, vec) < 0) {
89 log_warning("mincore(%s) failed: %m", fn);
97 pages = l / PAGE_SIZE;
99 for (c = 0; c < pages; c++) {
100 bool new_mapped = (vec[c] & 1);
102 if (!mapped && new_mapped)
104 else if (mapped && !new_mapped) {
105 fwrite(&b, sizeof(b), 1, pack);
106 fwrite(&c, sizeof(c), 1, pack);
108 log_debug("%s: page %u to %u", fn, b, c);
114 /* We don't write any range data if we should read the entire file */
115 if (mapped && b > 0) {
116 fwrite(&b, sizeof(b), 1, pack);
117 fwrite(&c, sizeof(c), 1, pack);
119 log_debug("%s: page %u to %u", fn, b, c);
124 fwrite(&b, sizeof(b), 1, pack);
125 fwrite(&b, sizeof(b), 1, pack);
128 if (start != MAP_FAILED)
132 close_nointr_nofail(fd);
137 static unsigned long fd_first_block(int fd) {
139 struct fiemap fiemap;
140 struct fiemap_extent extent;
144 data.fiemap.fm_length = ~0ULL;
145 data.fiemap.fm_extent_count = 1;
147 if (ioctl(fd, FS_IOC_FIEMAP, &data) < 0)
150 if (data.fiemap.fm_mapped_extents <= 0)
153 if (data.fiemap.fm_extents[0].fe_flags & FIEMAP_EXTENT_UNKNOWN)
156 return (unsigned long) data.fiemap.fm_extents[0].fe_physical;
164 static int qsort_compare(const void *a, const void *b) {
165 const struct item *i, *j;
170 if (i->block < j->block)
172 if (i->block > j->block)
175 return strcmp(i->path, j->path);
178 static int collect(const char *root) {
184 struct pollfd pollfd[_FD_MAX];
185 int fanotify_fd = -1, signal_fd = -1, r = 0;
187 Hashmap *files = NULL;
192 char *pack_fn_new = NULL, *pack_fn = NULL;
197 if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)) < 0)
198 log_warning("Failed to set IDLE IO priority class: %m");
200 assert_se(sigemptyset(&mask) == 0);
201 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
202 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
204 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
205 log_error("signalfd(): %m");
210 if (!(files = hashmap_new(string_hash_func, string_compare_func))) {
211 log_error("Failed to allocate set.");
216 if ((fanotify_fd = fanotify_init(FAN_CLOEXEC, O_RDONLY|O_LARGEFILE|O_CLOEXEC|O_NOATIME)) < 0) {
217 log_error("Failed to create fanotify object: %m");
222 if (fanotify_mark(fanotify_fd, FAN_MARK_ADD|FAN_MARK_MOUNT, FAN_OPEN, AT_FDCWD, root) < 0) {
223 log_error("Failed to mark %s: %m", root);
231 pollfd[FD_FANOTIFY].fd = fanotify_fd;
232 pollfd[FD_FANOTIFY].events = POLLIN;
233 pollfd[FD_SIGNAL].fd = signal_fd;
234 pollfd[FD_SIGNAL].events = POLLIN;
238 "STATUS=Collecting readahead data");
240 log_debug("Collecting...");
244 struct fanotify_event_metadata metadata;
248 struct fanotify_event_metadata *m;
250 if (poll(pollfd, _FD_MAX, -1) < 0) {
255 log_error("poll(): %m");
260 if (pollfd[FD_SIGNAL].revents != 0)
263 if ((n = read(fanotify_fd, &data, sizeof(data))) < 0) {
265 if (errno == EINTR || errno == EAGAIN)
268 log_error("Failed to read event: %m");
274 while (FAN_EVENT_OK(m, n)) {
276 if (m->pid != my_pid && m->fd >= 0) {
280 snprintf(fn, sizeof(fn), "/proc/self/fd/%i", m->fd);
283 if ((k = readlink_malloc(fn, &p)) >= 0) {
285 if (hashmap_get(files, p))
291 ul = fd_first_block(m->fd);
293 if ((k = hashmap_put(files, p, ULONG_TO_PTR(ul))) < 0) {
296 log_warning("set_put() failed: %s", strerror(-k));
303 log_warning("readlink(%s) failed: %s", fn, strerror(-k));
307 close_nointr_nofail(m->fd);
309 m = FAN_EVENT_NEXT(m, n);
314 if (fanotify_fd >= 0) {
315 close_nointr_nofail(fanotify_fd);
319 log_debug("Writing Pack File...");
321 on_ssd = fs_on_ssd(root);
322 log_debug("On SSD: %s", yes_no(on_ssd));
324 asprintf(&pack_fn, "%s/.readahead", root);
325 asprintf(&pack_fn_new, "%s/.readahead.new", root);
327 if (!pack_fn || !pack_fn_new) {
328 log_error("Out of memory");
333 if (!(pack = fopen(pack_fn_new, "we"))) {
334 log_error("Failed to open pack file: %m");
339 fputs(CANONICAL_HOST "\n", pack);
340 putc(on_ssd ? 'S' : 'R', pack);
344 /* On SSD, just write things out in the order the
345 * files where accessed */
347 HASHMAP_FOREACH_KEY(q, p, files, i)
350 struct item *ordered, *j;
353 /* On rotating media, order things by the block
356 log_debug("Ordering...");
358 n = hashmap_size(files);
359 if (!(ordered = new(struct item, n))) {
360 log_error("Out of memory");
366 HASHMAP_FOREACH_KEY(q, p, files, i) {
368 j->block = PTR_TO_ULONG(q);
372 assert(ordered + n == j);
374 qsort(ordered, n, sizeof(struct item), qsort_compare);
376 for (k = 0; k < n; k++)
377 pack_file(pack, ordered[k].path);
382 log_debug("Finalizing...");
387 log_error("Failed to write pack file.");
392 if (rename(pack_fn_new, pack_fn) < 0) {
393 log_error("Failed to rename readahead file: %m");
404 if (fanotify_fd >= 0)
405 close_nointr_nofail(fanotify_fd);
408 close_nointr_nofail(signal_fd);
418 while ((p = hashmap_steal_first_key(files)))
426 int main(int argc, char *argv[]) {
427 /* log_set_target(LOG_TARGET_SYSLOG_OR_KMSG); */
428 log_parse_environment();
431 log_set_max_level(LOG_DEBUG);
433 if (collect("/") < 0)