[systemd-commits] 6 commits - src/readahead-collect.c src/readahead-common.c src/readahead-common.h src/readahead-replay.c
Lennart Poettering
lennart at kemper.freedesktop.org
Sun Sep 26 05:52:45 PDT 2010
src/readahead-collect.c | 167 ++++++++++++++++++++++++++++++++++++++++++------
src/readahead-common.c | 15 +++-
src/readahead-common.h | 5 -
src/readahead-replay.c | 101 +++++++++++++++++++++++++++--
4 files changed, 258 insertions(+), 30 deletions(-)
New commits:
commit 8260358d5a04912223e8a0062b70a621e2241e96
Author: Lennart Poettering <lennart at poettering.net>
Date: Sat Sep 25 15:39:38 2010 +0200
readahead: parse command line arguments
diff --git a/src/readahead-collect.c b/src/readahead-collect.c
index c8490b5..0637c91 100644
--- a/src/readahead-collect.c
+++ b/src/readahead-collect.c
@@ -40,6 +40,7 @@
#include <linux/fiemap.h>
#include <sys/ioctl.h>
#include <sys/vfs.h>
+#include <getopt.h>
#include "missing.h"
#include "util.h"
@@ -48,20 +49,20 @@
#include "ioprio.h"
#include "readahead-common.h"
-#define MINCORE_VEC_SIZE (READAHEAD_FILE_SIZE_MAX/PAGE_SIZE)
-#define TIMEOUT_USEC (2*USEC_PER_MINUTE)
-
/* fixme:
*
* - detect ssd on btrfs/lvm...
* - read ahead directories
* - sd_readahead_cancel
* - gzip?
- * - remount rw
- * - are filenames from anotify normalized regards /../ and // and /./?
+ * - remount rw?
* - does ioprio_set work with fadvise()?
*/
+static unsigned arg_files_max = 16*1024;
+static off_t arg_file_size_max = READAHEAD_FILE_SIZE_MAX;
+static usec_t arg_timeout = 2*USEC_PER_MINUTE;
+
static int btrfs_defrag(int fd) {
struct btrfs_ioctl_vol_args data;
@@ -74,7 +75,7 @@ static int btrfs_defrag(int fd) {
static int pack_file(FILE *pack, const char *fn, bool on_btrfs) {
struct stat st;
void *start = MAP_FAILED;
- uint8_t vec[MINCORE_VEC_SIZE];
+ uint8_t *vec;
uint32_t b, c;
size_t l, pages;
bool mapped;
@@ -89,7 +90,7 @@ static int pack_file(FILE *pack, const char *fn, bool on_btrfs) {
goto finish;
}
- if ((k = file_verify(fd, fn, &st)) <= 0) {
+ if ((k = file_verify(fd, fn, arg_file_size_max, &st)) <= 0) {
r = k;
goto finish;
}
@@ -104,6 +105,9 @@ static int pack_file(FILE *pack, const char *fn, bool on_btrfs) {
goto finish;
}
+ pages = l / PAGE_SIZE;
+
+ vec = alloca(pages);
if (mincore(start, l, vec) < 0) {
log_warning("mincore(%s) failed: %m", fn);
r = -errno;
@@ -113,7 +117,6 @@ static int pack_file(FILE *pack, const char *fn, bool on_btrfs) {
fputs(fn, pack);
fputc('\n', pack);
- pages = l / PAGE_SIZE;
mapped = false;
for (c = 0; c < pages; c++) {
bool new_mapped = !!(vec[c] & 1);
@@ -248,7 +251,7 @@ static int collect(const char *root) {
goto finish;
}
- not_after = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
+ not_after = now(CLOCK_MONOTONIC) + arg_timeout;
my_pid = getpid();
@@ -274,7 +277,7 @@ static int collect(const char *root) {
usec_t t;
int h;
- if (hashmap_size(files) > READAHEAD_FILES_MAX) {
+ if (hashmap_size(files) > arg_files_max) {
log_debug("Reached maximum number of read ahead files, ending collection.");
break;
}
@@ -464,18 +467,109 @@ finish:
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",
+ program_invocation_short_name);
+
+ return 0;
+}
+
+static int parse_argv(int argc, char *argv[]) {
+
+ enum {
+ ARG_FILES_MAX = 0x100,
+ ARG_FILE_SIZE_MAX,
+ ARG_TIMEOUT
+ };
+
+ static const struct option options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "files-max", required_argument, NULL, ARG_FILES_MAX },
+ { "file-size-max", required_argument, NULL, ARG_FILE_SIZE_MAX },
+ { "timeout", required_argument, NULL, ARG_TIMEOUT },
+ { NULL, 0, NULL, 0 }
+ };
+
+ int c;
+
+ assert(argc >= 0);
+ assert(argv);
+
+ while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
+
+ switch (c) {
+
+ case 'h':
+ help();
+ return 0;
+
+ case ARG_FILES_MAX:
+ if (safe_atou(optarg, &arg_files_max) < 0 || arg_files_max <= 0) {
+ log_error("Failed to parse maximum number of files %s.", optarg);
+ return -EINVAL;
+ }
+ break;
+
+ case ARG_FILE_SIZE_MAX: {
+ unsigned long long ull;
+
+ if (safe_atollu(optarg, &ull) < 0 || ull <= 0) {
+ log_error("Failed to parse maximum file size %s.", optarg);
+ return -EINVAL;
+ }
+
+ arg_file_size_max = (off_t) ull;
+ break;
+ }
+
+ case ARG_TIMEOUT:
+ if (parse_usec(optarg, &arg_timeout) < 0 || arg_timeout <= 0) {
+ log_error("Failed to parse timeout %s.", optarg);
+ return -EINVAL;
+ }
+
+ break;
+
+ case '?':
+ return -EINVAL;
+
+ default:
+ log_error("Unknown option code %c", c);
+ return -EINVAL;
+ }
+ }
+
+ if (optind != argc &&
+ optind != argc-1) {
+ help();
+ return -EINVAL;
+ }
+
+ return 1;
+}
+
int main(int argc, char *argv[]) {
+ int r;
log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
log_parse_environment();
log_open();
+ if ((r = parse_argv(argc, argv)) <= 0)
+ return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+
if (!enough_ram()) {
log_info("Disabling readahead collector due to low memory.");
return 0;
}
- if (collect(argc >= 2 ? argv[1] : "/") < 0)
+ if (collect(optind < argc ? argv[optind] : "/") < 0)
return 1;
return 0;
diff --git a/src/readahead-common.c b/src/readahead-common.c
index 88441db..a1016a3 100644
--- a/src/readahead-common.c
+++ b/src/readahead-common.c
@@ -29,7 +29,7 @@
#include "readahead-common.h"
#include "util.h"
-int file_verify(int fd, const char *fn, struct stat *st) {
+int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st) {
assert(fd >= 0);
assert(fn);
assert(st);
@@ -44,7 +44,7 @@ int file_verify(int fd, const char *fn, struct stat *st) {
return 0;
}
- if (st->st_size <= 0 || st->st_size > READAHEAD_FILE_SIZE_MAX) {
+ if (st->st_size <= 0 || st->st_size > file_size_max) {
log_debug("Not preloading file %s with size out of bounds %zi", fn, st->st_size);
return 0;
}
diff --git a/src/readahead-common.h b/src/readahead-common.h
index d39dd5b..c7fd713 100644
--- a/src/readahead-common.h
+++ b/src/readahead-common.h
@@ -25,9 +25,8 @@
#include <sys/stat.h>
#define READAHEAD_FILE_SIZE_MAX (128*1024*1024)
-#define READAHEAD_FILES_MAX (16*1024)
-int file_verify(int fd, const char *fn, struct stat *st);
+int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st);
int fs_on_ssd(const char *p);
diff --git a/src/readahead-replay.c b/src/readahead-replay.c
index 2fa5c18..d4ddf26 100644
--- a/src/readahead-replay.c
+++ b/src/readahead-replay.c
@@ -32,6 +32,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
+#include <getopt.h>
#include "missing.h"
#include "util.h"
@@ -40,6 +41,8 @@
#include "ioprio.h"
#include "readahead-common.h"
+static off_t arg_file_size_max = READAHEAD_FILE_SIZE_MAX;
+
static int unpack_file(FILE *pack) {
char fn[PATH_MAX];
int r = 0, fd = -1;
@@ -56,7 +59,7 @@ static int unpack_file(FILE *pack) {
if ((fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW)) < 0)
log_warning("open(%s) failed: %m", fn);
- else if (file_verify(fd, fn, &st) <= 0) {
+ else if (file_verify(fd, fn, arg_file_size_max, &st) <= 0) {
close_nointr_nofail(fd);
fd = -1;
}
@@ -210,18 +213,89 @@ finish:
return r;
}
+
+static int help(void) {
+
+ printf("%s [OPTIONS...] [DIRECTORY]\n\n"
+ "Replay collected read-ahead data on early boot.\n\n"
+ " -h --help Show this help\n"
+ " --max-file-size=BYTES Maximum size of files to read ahead\n",
+ program_invocation_short_name);
+
+ return 0;
+}
+
+static int parse_argv(int argc, char *argv[]) {
+
+ enum {
+ ARG_FILE_SIZE_MAX
+ };
+
+ static const struct option options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "file-size-max", required_argument, NULL, ARG_FILE_SIZE_MAX },
+ { NULL, 0, NULL, 0 }
+ };
+
+ int c;
+
+ assert(argc >= 0);
+ assert(argv);
+
+ while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
+
+ switch (c) {
+
+ case 'h':
+ help();
+ return 0;
+
+ case ARG_FILE_SIZE_MAX: {
+ unsigned long long ull;
+
+ if (safe_atollu(optarg, &ull) < 0 || ull <= 0) {
+ log_error("Failed to parse maximum file size %s.", optarg);
+ return -EINVAL;
+ }
+
+ arg_file_size_max = (off_t) ull;
+ break;
+ }
+
+ case '?':
+ return -EINVAL;
+
+ default:
+ log_error("Unknown option code %c", c);
+ return -EINVAL;
+ }
+ }
+
+ if (optind != argc &&
+ optind != argc-1) {
+ help();
+ return -EINVAL;
+ }
+
+ return 1;
+}
+
int main(int argc, char*argv[]) {
+ int r;
log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
log_parse_environment();
log_open();
+ if ((r = parse_argv(argc, argv)) <= 0)
+ return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+
if (!enough_ram()) {
log_info("Disabling readahead replay due to low memory.");
return 0;
}
- if (replay(argc >= 2 ? argv[1] : "/") < 0)
+ if (replay(optind < argc ? argv[optind] : "/") < 0)
return 1;
return 0;
commit 437dca8b2f05a83e5c1df41226992c8d68b80007
Author: Lennart Poettering <lennart at poettering.net>
Date: Sat Sep 25 14:35:53 2010 +0200
readahead: never readahead temporary files since their names tend not to be stable
diff --git a/src/readahead-collect.c b/src/readahead-collect.c
index 47095b1..c8490b5 100644
--- a/src/readahead-collect.c
+++ b/src/readahead-collect.c
@@ -324,8 +324,10 @@ static int collect(const char *root) {
if ((k = readlink_malloc(fn, &p)) >= 0) {
- if (hashmap_get(files, p))
- /* Already read */
+ if (startswith(p, "/tmp") ||
+ hashmap_get(files, p))
+ /* Not interesting, or
+ * already read */
free(p);
else {
unsigned long ul;
commit 75a010e0b790a2cf3c26408f99927bdb37011a32
Author: Lennart Poettering <lennart at poettering.net>
Date: Sat Sep 25 14:35:34 2010 +0200
readahead: make candidate for early OOM kill
diff --git a/src/readahead-collect.c b/src/readahead-collect.c
index 6705615..47095b1 100644
--- a/src/readahead-collect.c
+++ b/src/readahead-collect.c
@@ -57,7 +57,6 @@
* - read ahead directories
* - sd_readahead_cancel
* - gzip?
- * - oom adjust
* - remount rw
* - are filenames from anotify normalized regards /../ and // and /./?
* - does ioprio_set work with fadvise()?
@@ -216,6 +215,8 @@ static int collect(const char *root) {
assert(root);
+ write_one_line_file("/proc/self/oom_score_adj", "1000");
+
if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)) < 0)
log_warning("Failed to set IDLE IO priority class: %m");
diff --git a/src/readahead-replay.c b/src/readahead-replay.c
index c01f73e..2fa5c18 100644
--- a/src/readahead-replay.c
+++ b/src/readahead-replay.c
@@ -119,6 +119,8 @@ static int replay(const char *root) {
assert(root);
+ write_one_line_file("/proc/self/oom_score_adj", "1000");
+
if (asprintf(&pack_fn, "%s/.readahead", root) < 0) {
log_error("Out of memory");
r = -ENOMEM;
commit 408b85df83ad5711eeb959dd657b967447892c56
Author: Lennart Poettering <lennart at poettering.net>
Date: Sat Sep 25 14:27:16 2010 +0200
readahead: exit after a maximum runtime
diff --git a/src/readahead-collect.c b/src/readahead-collect.c
index 6b9fb46..6705615 100644
--- a/src/readahead-collect.c
+++ b/src/readahead-collect.c
@@ -49,12 +49,18 @@
#include "readahead-common.h"
#define MINCORE_VEC_SIZE (READAHEAD_FILE_SIZE_MAX/PAGE_SIZE)
+#define TIMEOUT_USEC (2*USEC_PER_MINUTE)
/* fixme:
*
- * - detect ssd/lvm/... on btrfs
+ * - detect ssd on btrfs/lvm...
* - read ahead directories
* - sd_readahead_cancel
+ * - gzip?
+ * - oom adjust
+ * - remount rw
+ * - are filenames from anotify normalized regards /../ and // and /./?
+ * - does ioprio_set work with fadvise()?
*/
static int btrfs_defrag(int fd) {
@@ -111,7 +117,7 @@ static int pack_file(FILE *pack, const char *fn, bool on_btrfs) {
pages = l / PAGE_SIZE;
mapped = false;
for (c = 0; c < pages; c++) {
- bool new_mapped = (vec[c] & 1);
+ bool new_mapped = !!(vec[c] & 1);
if (!mapped && new_mapped)
b = c;
@@ -206,6 +212,7 @@ static int collect(const char *root) {
char *pack_fn_new = NULL, *pack_fn = NULL;
bool on_ssd, on_btrfs;
struct statfs sfs;
+ usec_t not_after;
assert(root);
@@ -228,7 +235,7 @@ static int collect(const char *root) {
goto finish;
}
- if ((fanotify_fd = fanotify_init(FAN_CLOEXEC, O_RDONLY|O_LARGEFILE|O_CLOEXEC|O_NOATIME)) < 0) {
+ 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;
@@ -240,6 +247,8 @@ static int collect(const char *root) {
goto finish;
}
+ not_after = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
+
my_pid = getpid();
zero(pollfd);
@@ -261,11 +270,21 @@ static int collect(const char *root) {
} data;
ssize_t n;
struct fanotify_event_metadata *m;
+ usec_t t;
+ int h;
- if (hashmap_size(files) > READAHEAD_FILES_MAX)
+ if (hashmap_size(files) > READAHEAD_FILES_MAX) {
+ 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;
+ }
- if (poll(pollfd, _FD_MAX, -1) < 0) {
+ if ((h = poll(pollfd, _FD_MAX, (int) ((not_after - t) / USEC_PER_MSEC))) < 0) {
if (errno == EINTR)
continue;
@@ -278,6 +297,11 @@ static int collect(const char *root) {
if (pollfd[FD_SIGNAL].revents != 0)
break;
+ if (h == 0) {
+ log_debug("Reached maximum collection time, ending collection.");
+ break;
+ }
+
if ((n = read(fanotify_fd, &data, sizeof(data))) < 0) {
if (errno == EINTR || errno == EAGAIN)
@@ -288,8 +312,7 @@ static int collect(const char *root) {
goto finish;
}
- m = &data.metadata;
- while (FAN_EVENT_OK(m, n)) {
+ for (m = &data.metadata; FAN_EVENT_OK(m, n); m = FAN_EVENT_NEXT(m, n)) {
if (m->pid != my_pid && m->fd >= 0) {
char fn[PATH_MAX];
@@ -320,8 +343,6 @@ static int collect(const char *root) {
if (m->fd)
close_nointr_nofail(m->fd);
-
- m = FAN_EVENT_NEXT(m, n);
}
}
commit 902a339c93f44bc7290bede2f91f3a5c7cb16402
Author: Lennart Poettering <lennart at poettering.net>
Date: Sat Sep 25 13:47:31 2010 +0200
readahead-replay: delay ready notification until we queued first read
diff --git a/src/readahead-collect.c b/src/readahead-collect.c
index fe35da7..6b9fb46 100644
--- a/src/readahead-collect.c
+++ b/src/readahead-collect.c
@@ -54,6 +54,7 @@
*
* - detect ssd/lvm/... on btrfs
* - read ahead directories
+ * - sd_readahead_cancel
*/
static int btrfs_defrag(int fd) {
diff --git a/src/readahead-replay.c b/src/readahead-replay.c
index 58d9468..c01f73e 100644
--- a/src/readahead-replay.c
+++ b/src/readahead-replay.c
@@ -114,7 +114,7 @@ static int replay(const char *root) {
char line[LINE_MAX];
int r = 0;
char *pack_fn = NULL, c;
- bool on_ssd;
+ bool on_ssd, ready = false;
int prio;
assert(root);
@@ -168,9 +168,7 @@ static int replay(const char *root) {
if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), prio) < 0)
log_warning("Failed to set IDLE IO priority class: %m");
- sd_notify(0,
- "READY=1\n"
- "STATUS=Replaying readahead data");
+ sd_notify(0, "STATUS=Replaying readahead data");
log_debug("Replaying...");
@@ -181,8 +179,18 @@ static int replay(const char *root) {
r = k;
goto finish;
}
+
+ if (!ready) {
+ /* We delay the ready notification until we
+ * queued at least one read */
+ sd_notify(0, "READY=1");
+ ready = true;
+ }
}
+ if (!ready)
+ sd_notify(0, "READY=1");
+
if (ferror(pack)) {
log_error("Failed to read pack file.");
r = -EIO;
commit 41a598e21a9f6cfe8dad7759c3c9facd55774acf
Author: Lennart Poettering <lennart at poettering.net>
Date: Sat Sep 25 13:32:54 2010 +0200
readahead: disable on low memory machines
diff --git a/src/readahead-collect.c b/src/readahead-collect.c
index 937231c..fe35da7 100644
--- a/src/readahead-collect.c
+++ b/src/readahead-collect.c
@@ -50,6 +50,12 @@
#define MINCORE_VEC_SIZE (READAHEAD_FILE_SIZE_MAX/PAGE_SIZE)
+/* fixme:
+ *
+ * - detect ssd/lvm/... on btrfs
+ * - read ahead directories
+ */
+
static int btrfs_defrag(int fd) {
struct btrfs_ioctl_vol_args data;
@@ -302,10 +308,7 @@ static int collect(const char *root) {
ul = fd_first_block(m->fd);
if ((k = hashmap_put(files, p, ULONG_TO_PTR(ul))) < 0) {
-
- if (k != -EEXIST)
- log_warning("set_put() failed: %s", strerror(-k));
-
+ log_warning("set_put() failed: %s", strerror(-k));
free(p);
}
}
@@ -355,7 +358,7 @@ static int collect(const char *root) {
if (on_ssd || on_btrfs) {
/* On SSD or on btrfs, just write things out in the
- * order the files where accessed. */
+ * order the files were accessed. */
HASHMAP_FOREACH_KEY(q, p, files, i)
pack_file(pack, p, on_btrfs);
@@ -442,6 +445,11 @@ int main(int argc, char *argv[]) {
log_parse_environment();
log_open();
+ if (!enough_ram()) {
+ log_info("Disabling readahead collector due to low memory.");
+ return 0;
+ }
+
if (collect(argc >= 2 ? argv[1] : "/") < 0)
return 1;
diff --git a/src/readahead-common.c b/src/readahead-common.c
index 8533717..88441db 100644
--- a/src/readahead-common.c
+++ b/src/readahead-common.c
@@ -23,6 +23,7 @@
#include <libudev.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/sysinfo.h>
#include "log.h"
#include "readahead-common.h"
@@ -105,3 +106,13 @@ finish:
return b;
}
+
+bool enough_ram(void) {
+ struct sysinfo si;
+
+ assert_se(sysinfo(&si) >= 0);
+
+ return si.totalram > 127 * 1024*1024; /* Enable readahead only
+ * with at least 128MB
+ * memory */
+}
diff --git a/src/readahead-common.h b/src/readahead-common.h
index 5d8f1a3..d39dd5b 100644
--- a/src/readahead-common.h
+++ b/src/readahead-common.h
@@ -31,4 +31,6 @@ int file_verify(int fd, const char *fn, struct stat *st);
int fs_on_ssd(const char *p);
+bool enough_ram(void);
+
#endif
diff --git a/src/readahead-replay.c b/src/readahead-replay.c
index 1f1ec52..58d9468 100644
--- a/src/readahead-replay.c
+++ b/src/readahead-replay.c
@@ -206,6 +206,11 @@ int main(int argc, char*argv[]) {
log_parse_environment();
log_open();
+ if (!enough_ram()) {
+ log_info("Disabling readahead replay due to low memory.");
+ return 0;
+ }
+
if (replay(argc >= 2 ? argv[1] : "/") < 0)
return 1;
More information about the systemd-commits
mailing list