readahead.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  11. */
  12. //config:config READAHEAD
  13. //config: bool "readahead (1.5 kb)"
  14. //config: default y
  15. //config: depends on LFS
  16. //config: select PLATFORM_LINUX
  17. //config: help
  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.
  20. //config:
  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.
  26. //config:
  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.
  29. //applet:IF_READAHEAD(APPLET(readahead, BB_DIR_USR_SBIN, BB_SUID_DROP))
  30. //kbuild:lib-$(CONFIG_READAHEAD) += readahead.o
  31. //usage:#define readahead_trivial_usage
  32. //usage: "[FILE]..."
  33. //usage:#define readahead_full_usage "\n\n"
  34. //usage: "Preload FILEs to RAM"
  35. #include "libbb.h"
  36. int readahead_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  37. int readahead_main(int argc UNUSED_PARAM, char **argv)
  38. {
  39. int retval = EXIT_SUCCESS;
  40. if (!argv[1]) {
  41. bb_show_usage();
  42. }
  43. while (*++argv) {
  44. int fd = open_or_warn(*argv, O_RDONLY);
  45. if (fd >= 0) {
  46. off_t len;
  47. int r;
  48. /* fdlength was reported to be unreliable - use seek */
  49. len = xlseek(fd, 0, SEEK_END);
  50. xlseek(fd, 0, SEEK_SET);
  51. r = readahead(fd, 0, len);
  52. close(fd);
  53. if (r >= 0)
  54. continue;
  55. }
  56. retval = EXIT_FAILURE;
  57. }
  58. return retval;
  59. }