Rob Landley | 0e4690d | 2006-08-20 22:12:18 +0000 | [diff] [blame] | 1 | /* 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 Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Rob Landley | 0e4690d | 2006-08-20 22:12:18 +0000 | [diff] [blame] | 11 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 12 | //config:config READAHEAD |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 13 | //config: bool "readahead (1.5 kb)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 14 | //config: default y |
| 15 | //config: depends on LFS |
| 16 | //config: select PLATFORM_LINUX |
| 17 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 18 | //config: Preload the files listed on the command line into RAM cache so that |
| 19 | //config: subsequent reads on these files will not block on disk I/O. |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 20 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 21 | //config: This applet just calls the readahead(2) system call on each file. |
| 22 | //config: It is mainly useful in system startup scripts to preload files |
| 23 | //config: or executables before they are used. When used at the right time |
| 24 | //config: (in particular when a CPU bound process is running) it can |
| 25 | //config: significantly speed up system startup. |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 26 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 27 | //config: As readahead(2) blocks until each file has been read, it is best to |
| 28 | //config: run this applet as a background job. |
Rob Landley | 0e4690d | 2006-08-20 22:12:18 +0000 | [diff] [blame] | 29 | |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 30 | //applet:IF_READAHEAD(APPLET(readahead, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 31 | |
| 32 | //kbuild:lib-$(CONFIG_READAHEAD) += readahead.o |
| 33 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 34 | //usage:#define readahead_trivial_usage |
| 35 | //usage: "[FILE]..." |
| 36 | //usage:#define readahead_full_usage "\n\n" |
| 37 | //usage: "Preload FILEs to RAM" |
| 38 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 39 | #include "libbb.h" |
Rob Landley | 0e4690d | 2006-08-20 22:12:18 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 41 | int readahead_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 42 | int readahead_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 0e4690d | 2006-08-20 22:12:18 +0000 | [diff] [blame] | 43 | { |
Rob Landley | 0e4690d | 2006-08-20 22:12:18 +0000 | [diff] [blame] | 44 | int retval = EXIT_SUCCESS; |
| 45 | |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 46 | if (!argv[1]) { |
| 47 | bb_show_usage(); |
| 48 | } |
Rob Landley | 0e4690d | 2006-08-20 22:12:18 +0000 | [diff] [blame] | 49 | |
| 50 | while (*++argv) { |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 51 | int fd = open_or_warn(*argv, O_RDONLY); |
| 52 | if (fd >= 0) { |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 53 | off_t len; |
| 54 | int r; |
| 55 | |
| 56 | /* fdlength was reported to be unreliable - use seek */ |
| 57 | len = xlseek(fd, 0, SEEK_END); |
| 58 | xlseek(fd, 0, SEEK_SET); |
| 59 | r = readahead(fd, 0, len); |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 60 | close(fd); |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 61 | if (r >= 0) |
| 62 | continue; |
Rob Landley | 0e4690d | 2006-08-20 22:12:18 +0000 | [diff] [blame] | 63 | } |
| 64 | retval = EXIT_FAILURE; |
| 65 | } |
| 66 | |
| 67 | return retval; |
| 68 | } |