readahead-collect.c revision 22be093ffb403a1c474037939ca9b88b1ee39f77
/*-*- 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 General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU 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 "missing.h"
#include "util.h"
#include "set.h"
#include "sd-daemon.h"
#include "ioprio.h"
#include "readahead-common.h"
/*
fixme:
- BTRFS_IOC_DEFRAG
*/
void *start = MAP_FAILED;
uint32_t b, c;
bool mapped;
int r = 0, fd = -1, k;
r = -errno;
goto finish;
}
r = k;
goto finish;
}
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 {
};
Iterator i;
char *p, *q;
bool on_ssd;
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;
}
log_error("Failed to create fanotify object: %m");
r = -errno;
goto finish;
}
r = -errno;
goto finish;
}
sd_notify(0,
"READY=1\n"
"STATUS=Collecting readahead data");
log_debug("Collecting...");
for (;;) {
union {
struct fanotify_event_metadata metadata;
char buffer[4096];
} data;
ssize_t n;
struct fanotify_event_metadata *m;
continue;
log_error("poll(): %m");
r = -errno;
goto finish;
}
break;
continue;
log_error("Failed to read event: %m");
r = -errno;
goto finish;
}
while (FAN_EVENT_OK(m, n)) {
int k;
if ((k = readlink_malloc(fn, &p)) >= 0) {
if (hashmap_get(files, p))
/* Already read */
free(p);
else {
unsigned long ul;
if (k != -EEXIST)
free(p);
}
}
} else
}
if (m->fd)
close_nointr_nofail(m->fd);
m = FAN_EVENT_NEXT(m, n);
}
}
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;
}
if (on_ssd) {
/* On SSD, just write things out in the order the
* files where 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_ULONG(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 (pack) {
}
while ((p = hashmap_steal_first_key(files)))
free(q);
return r;
}
/* log_set_target(LOG_TARGET_SYSLOG_OR_KMSG); */
log_open();
if (collect("/") < 0)
return 1;
return 0;
}