5

Is there any command to for a file to be read in and loaded into the linux disk cache? This is on an up-to-date debian system.

I know in the general case, it's better to let the linux kernel figure this out. But I have an edge case. I have a laptop that has an NFS director mounted, and i want to play a long video file, but I don't want to have a network problem interrupt the playnig. I know that (largeish) file will be read in it's entirety later on. I know that nothing else (really) will be running while playing this video. There is enough free memory to store this file.

(I know I could just copy the file into a new tmpfs filesystem, but I'm curious if there's an even shorter way to do it)

2 Answers 2

7

A small program that mmap()ed the file with the MAP_LOCKED flag set would do the trick. You'll need to run this program as root, or a user with an RLIMIT_MEMLOCK value greater than the size of the file you want to map.

As SaveTheRbtz has mentioned, vmtouch has this capability built-in. The manpage suggests that vmtouch -l /some/file will do the trick.

4
  • 2
    And don't forget to munlock() that file afterwards again! :)
    – Michuelnik
    Jul 10, 2012 at 12:51
  • 1
    Not necessary; as per mlock(2), "Memory locks are [...] automatically removed (unlocked) [...] when the process terminates.".
    – womble
    Jul 10, 2012 at 15:57
  • 2
    There is a tool for that: hoytech.com/vmtouch Jul 12, 2012 at 8:38
  • @SaveTheRbtz: Thanks for that -- I expected someone would have written it already.
    – womble
    Jul 12, 2012 at 9:22
5

If you really control the local environment that strictly, a simple cat $file >/dev/null should be sufficient. It's quite non-deterministic, when cache eviction removes that file - but if you have plenty of RAM and noone consuming it, it should be kept.

If there is a sticky and deterministic way to keep a file in cache - dunno...

2
  • Is that guaranteed to put the file into the cache though? Jul 10, 2012 at 16:12
  • Yes, thats the way it works. It's read through the cache.
    – Michuelnik
    Jul 11, 2012 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.