readahead-collect.c revision 5430f7f2bc7330f3088b894166bf3524a067e3d8
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <inttypes.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/fanotify.h>
#include <sys/signalfd.h>
#include <getopt.h>
#include <systemd/sd-daemon.h>
#include "missing.h"
#include "util.h"
#include "set.h"
#include "ioprio.h"
#include "readahead-common.h"
#include "virt.h"
/* fixme:
*
* - read ahead directories
* - gzip?
* - remount rw?
* - handle files where nothing is in mincore
* - does ioprio_set work with fadvise()?
*/
/* Avoid collisions with the NULL pointer */
static int btrfs_defrag(int fd) {
struct btrfs_ioctl_vol_args data;
}
void *start = MAP_FAILED;
uint32_t b, c;
bool mapped;
int r = 0, fd = -1, k;
return 0;
return 0;
r = -errno;
goto finish;
}
r = k;
goto finish;
}
if (on_btrfs)
r = -errno;
goto finish;
}
r = -errno;
goto finish;
}
mapped = false;
for (c = 0; c < pages; c++) {
if (!mapped && new_mapped)
b = c;
else if (mapped && !new_mapped) {
}
mapped = new_mapped;
}
/* We don't write any range data if we should read the entire file */
if (mapped && b > 0) {
}
/* End marker */
b = 0;
if (start != MAP_FAILED)
if (fd >= 0)
return r;
}
static unsigned long fd_first_block(int fd) {
struct {
struct fiemap_extent extent;
} data;
return 0;
return 0;
return 0;
}
struct item {
const char *path;
unsigned long block;
};
static int qsort_compare(const void *a, const void *b) {
const struct item *i, *j;
i = a;
j = b;
return -1;
return 1;
}
enum {
FD_FANOTIFY, /* Get the actual fs events */
FD_INOTIFY, /* We get notifications to quit early via this fd */
};
Iterator i;
char *p, *q;
log_warning("Failed to set IDLE IO priority class: %m");
log_error("signalfd(): %m");
r = -errno;
goto finish;
}
log_error("Failed to allocate set.");
r = -ENOMEM;
goto finish;
}
if ((fanotify_fd = fanotify_init(FAN_CLOEXEC|FAN_NONBLOCK, O_RDONLY|O_LARGEFILE|O_CLOEXEC|O_NOATIME)) < 0) {
log_error("Failed to create fanotify object: %m");
r = -errno;
goto finish;
}
r = -errno;
goto finish;
}
if ((inotify_fd = open_inotify()) < 0) {
r = inotify_fd;
goto finish;
}
sd_notify(0,
"READY=1\n"
"STATUS=Collecting readahead data");
log_debug("Collecting...");
log_debug("Collection canceled");
r = -ECANCELED;
goto finish;
}
log_debug("Got termination request");
goto done;
}
for (;;) {
union {
struct fanotify_event_metadata metadata;
char buffer[4096];
} data;
ssize_t n;
struct fanotify_event_metadata *m;
usec_t t;
int h;
log_debug("Reached maximum number of read ahead files, ending collection.");
break;
}
t = now(CLOCK_MONOTONIC);
if (t >= not_after) {
log_debug("Reached maximum collection time, ending collection.");
break;
}
continue;
log_error("poll(): %m");
r = -errno;
goto finish;
}
if (h == 0) {
log_debug("Reached maximum collection time, ending collection.");
break;
}
log_debug("Got signal.");
break;
}
struct inotify_event *e;
continue;
log_error("Failed to read inotify event: %m");
r = -errno;
goto finish;
}
e = (struct inotify_event*) inotify_buffer;
while (n > 0) {
log_debug("Collection canceled");
r = -ECANCELED;
goto finish;
}
log_debug("Got termination request");
goto done;
}
n -= step;
}
}
continue;
/* fanotify sometimes returns EACCES on read()
* where it shouldn't. For now let's just
* ignore it here (which is safe), but
* eventually this should be
* dropped when the kernel is fixed.
*
continue;
log_error("Failed to read event: %m");
r = -errno;
goto finish;
}
int k;
if (m->fd < 0)
goto next_iteration;
goto next_iteration;
goto next_iteration;
if ((k = readlink_malloc(fn, &p)) >= 0) {
if (startswith(p, "/tmp") ||
endswith(p, " (deleted)") ||
hashmap_get(files, p))
/* Not interesting, or
* already read */
free(p);
else {
unsigned long ul;
free(p);
}
}
} else
if (m->fd)
close_nointr_nofail(m->fd);
}
}
done:
if (fanotify_fd >= 0) {
fanotify_fd = -1;
}
log_debug("Writing Pack File...");
if (!pack_fn || !pack_fn_new) {
log_error("Out of memory");
r = -ENOMEM;
goto finish;
}
log_error("Failed to open pack file: %m");
r = -errno;
goto finish;
}
/* On SSD or on btrfs, just write things out in the
* order the files were accessed. */
HASHMAP_FOREACH_KEY(q, p, files, i)
} else {
unsigned k, n;
/* On rotating media, order things by the block
* numbers */
log_debug("Ordering...");
n = hashmap_size(files);
log_error("Out of memory");
r = -ENOMEM;
goto finish;
}
j = ordered;
HASHMAP_FOREACH_KEY(q, p, files, i) {
j->path = p;
j->block = PTR_TO_SECTOR(q);
j++;
}
for (k = 0; k < n; k++)
}
log_debug("Finalizing...");
log_error("Failed to write pack file.");
r = -EIO;
goto finish;
}
log_error("Failed to rename readahead file: %m");
r = -errno;
goto finish;
}
log_debug("Done.");
if (fanotify_fd >= 0)
if (signal_fd >= 0)
if (inotify_fd >= 0)
if (pack) {
}
while ((p = hashmap_steal_first_key(files)))
free(p);
return r;
}
static int help(void) {
printf("%s [OPTIONS...] [DIRECTORY]\n\n"
"Collect read-ahead data on early boot.\n\n"
" -h --help Show this help\n"
" --max-files=INT Maximum number of files to read ahead\n"
" --max-file-size=BYTES Maximum size of files to read ahead\n"
" --timeout=USEC Maximum time to spend collecting data\n",
return 0;
}
enum {
ARG_FILES_MAX = 0x100,
};
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_FILES_MAX:
return -EINVAL;
}
break;
case ARG_FILE_SIZE_MAX: {
unsigned long long ull;
return -EINVAL;
}
break;
}
case ARG_TIMEOUT:
return -EINVAL;
}
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
help();
return -EINVAL;
}
return 1;
}
int r;
const char *root;
log_open();
umask(0022);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
if (fs_on_read_only(root) > 0) {
log_info("Disabling readahead collector due to read-only media.");
return 0;
}
if (!enough_ram()) {
log_info("Disabling readahead collector due to low memory.");
return 0;
}
if (detect_virtualization(NULL) > 0) {
log_info("Disabling readahead collector due to execution in virtualized environment.");
return 0;
}
if (!(shared = shared_get()))
return 1;
return 1;
return 0;
}