blob: d2bed20923832f0f508a0dca31b783e5bccf3557 [file] [log] [blame]
Rob Landley0e4690d2006-08-20 22:12:18 +00001/* vi: set sw=4 ts=4: */
2/*
3 * readahead implementation for busybox
4 *
5 * Preloads the given files in RAM, to reduce access time.
6 * Does this by calling the readahead(2) system call.
7 *
8 * Copyright (C) 2006 Michael Opdenacker <michael@free-electrons.com>
9 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Rob Landley0e4690d2006-08-20 22:12:18 +000011 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +010012//config:config READAHEAD
Denys Vlasenkob097a842018-12-28 03:20:17 +010013//config: bool "readahead (1.5 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010014//config: default y
15//config: depends on LFS
Denys Vlasenkofb4da162016-11-22 23:14:24 +010016//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: Preload the files listed on the command line into RAM cache so that
18//config: subsequent reads on these files will not block on disk I/O.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010019//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020020//config: This applet just calls the readahead(2) system call on each file.
21//config: It is mainly useful in system startup scripts to preload files
22//config: or executables before they are used. When used at the right time
23//config: (in particular when a CPU bound process is running) it can
24//config: significantly speed up system startup.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010025//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020026//config: As readahead(2) blocks until each file has been read, it is best to
27//config: run this applet as a background job.
Rob Landley0e4690d2006-08-20 22:12:18 +000028
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010029//applet:IF_READAHEAD(APPLET(readahead, BB_DIR_USR_SBIN, BB_SUID_DROP))
30
31//kbuild:lib-$(CONFIG_READAHEAD) += readahead.o
32
Pere Orga5bc8c002011-04-11 03:29:49 +020033//usage:#define readahead_trivial_usage
34//usage: "[FILE]..."
35//usage:#define readahead_full_usage "\n\n"
36//usage: "Preload FILEs to RAM"
37
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000038#include "libbb.h"
Rob Landley0e4690d2006-08-20 22:12:18 +000039
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000040int readahead_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010041int readahead_main(int argc UNUSED_PARAM, char **argv)
Rob Landley0e4690d2006-08-20 22:12:18 +000042{
Rob Landley0e4690d2006-08-20 22:12:18 +000043 int retval = EXIT_SUCCESS;
44
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010045 if (!argv[1]) {
46 bb_show_usage();
47 }
Rob Landley0e4690d2006-08-20 22:12:18 +000048
49 while (*++argv) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000050 int fd = open_or_warn(*argv, O_RDONLY);
51 if (fd >= 0) {
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +000052 off_t len;
53 int r;
54
55 /* fdlength was reported to be unreliable - use seek */
56 len = xlseek(fd, 0, SEEK_END);
57 xlseek(fd, 0, SEEK_SET);
58 r = readahead(fd, 0, len);
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000059 close(fd);
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +000060 if (r >= 0)
61 continue;
Rob Landley0e4690d2006-08-20 22:12:18 +000062 }
63 retval = EXIT_FAILURE;
64 }
65
66 return retval;
67}