Implement daemon mode for readahead.
function old new delta
readahead_main 1272455 +2328
.rodata 157133 157709+576
qsort_cmp - 54 +54
packed_usage 30271 30315 +44
tree_add_item - 40 +40
move_item_to_array - 38 +38
tree_cmp - 11 +11
--
(add/remove: 4/0 grow/shrink: 3/0 up/down: 3091/0) Total: 3091 bytes
Signed-off-by: Bartosz Golaszewski
---
docs/readahead.txt| 36 +++
miscutils/Config.src | 4 +-
miscutils/readahead.c | 649 --
3 files changed, 669 insertions(+), 20 deletions(-)
create mode 100644 docs/readahead.txt
diff --git a/docs/readahead.txt b/docs/readahead.txt
new file mode 100644
index 000..535b621
--- /dev/null
+++ b/docs/readahead.txt
@@ -0,0 +1,36 @@
+Readahead applet works in two modes. If at least one file is given via the
+command-line, it just calls readahead() for all the files. Otherwise it works
+as a daemon.
+
+In daemon mode it reads config options from /etc/readahead/readahead.conf,
+readahead()s all the files that have already been saved in
+/etc/readahead/readahead.lst and potentially starts collecting data about
+files being accessed for a configured number of seconds. It then adds all
+new entires to readahead.lst and increases the number in readahead.stamp
+which is then used to determine the number of data acquisition passes
+already done.
+
+If the value in readahead.stamp is equal or greater than the value of
+COLLECT_PASSES config option readahead stops collecting data and only
+readahead()s the files as soon as possible.
+
+Config file:
+
+/etc/readahead/readahead.conf contains simple key = value configuration
+options.
+
+Available options:
+
+COLLECT_TIME - desired time of data collection in seconds
+
+RAM_MAX - max memory usage for readahead in bytes (half of
+ available RAM by default)
+
+COLLECT_PASSES - number of times readahead should collect data before
+ switching to passive mode
+
+It is possible to run readahead either as a regular process during system
+boot, or as init in which case it will spawn a second readahead process to
+start the file preload as fast as possible and then re-exec as the real
+init. The kernel command-line argument readahead_init can be used to specify
+the init executable different than /sbin/init.
diff --git a/miscutils/Config.src b/miscutils/Config.src
index d69abf1..feccb0b 100644
--- a/miscutils/Config.src
+++ b/miscutils/Config.src
@@ -456,8 +456,10 @@ config READAHEAD
default y
depends on LFS
select PLATFORM_LINUX
+ select BUNZIP2
help
- Preload the files listed on the command line into RAM cache so that
+ Preload files passed to the applet via the command-line or stored
+ in /etc/ by previous passes in daemon mode into RAM cache so that
subsequent reads on these files will not block on disk I/O.
This applet just calls the readahead(2) system call on each file.
diff --git a/miscutils/readahead.c b/miscutils/readahead.c
index e22aaa4..4a314bf 100644
--- a/miscutils/readahead.c
+++ b/miscutils/readahead.c
@@ -5,42 +5,653 @@
* Preloads the given files in RAM, to reduce access time.
* Does this by calling the readahead(2) system call.
*
- * Copyright (C) 2006 Michael Opdenacker
+ * Copyright (C) 2006 Michael Opdenacker
+ * Copyright (C) 2015 Bartosz Golaszewski
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
//usage:#define readahead_trivial_usage
-//usage: "[FILE]..."
+//usage: "[-f] [FILE]"
//usage:#define readahead_full_usage "\n\n"
-//usage: "Preload FILEs to RAM"
+//usage: "Preload FILEs to RAM (as a command-line tool or as a daemon)"
+//usage: "\n -f don't fork in daemon mode\n\n"
+//usage: "For detailed configuration see readahead.txt."
#include "libbb.h"
+#include "missing_syscalls.h"
-int readahead_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int readahead_main(int argc UNUSED_PARAM, char **argv)
+#include
+#include
+#include
+#include
+#ifndef __UCLIBC__
+#include
+#endif
+
+#define OPT_f (1 << 0)
+#define FAN_POLL_INTERVAL 1000
+#define PARSER_FLAGS (PARSE_NORMAL & ~(PARSE_TRIM | PARSE_COLLAPSE))
+
+#define RA_CONFIG "/etc/readahead/readahead.conf"
+#define RA_FILE_LIST "/etc/readahead/readahead.lst.bz2"
+#define RA_STAMP "/etc/readahead/readahead.stamp"
+
+#define BZIP2_CMD