readahead-collect.c revision 73f860db9893deab6aebceb53dd7d0deb662e832
/*-*- 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 <math.h>
#ifdef HAVE_LINUX_BTRFS_H
#endif
#ifdef HAVE_FANOTIFY_INIT
#include <sys/fanotify.h>
#endif
#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) {
}
void *start = MAP_FAILED;
uint32_t b, c;
bool mapped;
int r = 0, fd = -1, k;
if (fd < 0) {
return 0;
return 0;
r = -errno;
goto finish;
}
if (k <= 0) {
r = k;
goto finish;
}
if (on_btrfs)
if (start == MAP_FAILED) {
r = -errno;
goto finish;
}
r = -errno;
goto finish;
}
/* Store the inode, so that we notice when the file is deleted */
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)
safe_close(fd);
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;
unsigned long bin;
};
static int qsort_compare(const void *a, const void *b) {
const struct item *i, *j;
i = a;
j = b;
/* sort by bin first */
return -1;
return 1;
/* then sort by sector */
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;
bool previous_block_readahead_set = false;
r = log_oom();
goto finish;
}
/* If there's no pack file yet we lower the kernel readahead
* so that mincore() is accurate. If there is a pack file
* already we assume it is accurate enough so that kernel
* readahead is never triggered. */
log_warning("Failed to set IDLE IO priority class: %m");
log_error("signalfd(): %m");
r = -errno;
goto finish;
}
if (!files) {
log_error("Failed to allocate set.");
r = -ENOMEM;
goto finish;
}
if (fanotify_fd < 0) {
log_error("Failed to create fanotify object: %m");
r = -errno;
goto finish;
}
r = -errno;
goto finish;
}
inotify_fd = open_inotify();
if (inotify_fd < 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;
}
}
if (n < 0) {
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;
k = readlink_malloc(fn, &p);
if (k >= 0) {
if (startswith(p, "/tmp") ||
endswith(p, " (deleted)") ||
hashmap_get(files, p))
/* Not interesting, or
* already read */
free(p);
else {
unsigned long ul;
if (!entry) {
r = log_oom();
goto finish;
}
r = log_oom();
goto finish;
}
if (k < 0) {
free(p);
}
}
} else
safe_close(m->fd);
}
}
done:
log_debug("Writing Pack File...");
r = log_oom();
goto finish;
}
if (!pack) {
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 n;
/* On rotating media, order things by the block
* numbers */
log_debug("Ordering...");
n = hashmap_size(files);
if (n) {
struct item *j;
unsigned k;
if (!ordered) {
r = log_oom();
goto finish;
}
j = ordered;
HASHMAP_FOREACH_KEY(q, p, files, i) {
j++;
}
for (k = 0; k < n; k++)
} else
log_warning("No pack files");
}
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 (pack) {
}
while ((p = hashmap_steal_first_key(files)))
free(p);
if (previous_block_readahead_set) {
/* Restore the original kernel readahead setting if we
* changed it, and nobody has overwritten it since
* yet. */
}
return r;
}
int main_collect(const char *root) {
if (!root)
root = "/";
/* Skip this step on read-only media. Note that we check the
* underlying block device here, not he read-only flag of the
* file system on top, since that one is most likely mounted
* read-only anyway at boot, even if the underlying block
* device is theoretically writable. */
if (fs_on_read_only(root) > 0) {
log_info("Disabling readahead collector due to read-only media.");
return EXIT_SUCCESS;
}
if (!enough_ram()) {
log_info("Disabling readahead collector due to low memory.");
return EXIT_SUCCESS;
}
shared = shared_get();
if (!shared)
return EXIT_FAILURE;
return EXIT_FAILURE;
return EXIT_SUCCESS;
}