I have a rather old server that has 4GB of RAM and it is pretty much serving the same files all day, but it is doing so from the hard drive while 3GBs of RAM are "free".
Anyone who has ever tried running a ram-drive can witness that It's awesome in terms of speed. The memory usage of this system is usually never higher than 1GB/4GB so I want to know if there is a way to use that extra memory for something good.
- Is it possible to tell the filesystem to always serve certain files out of RAM?
- Are there any other methods I can use to improve file reading capabilities by use of RAM?
More specifically, I am not looking for a 'hack' here. I want file system calls to serve the files from RAM without needing to create a ram-drive and copy the files there manually. Or at least a script that does this for me.
Possible applications here are:
- Web servers with static files that get read alot
- Application servers with large libraries
- Desktop computers with too much RAM
Any ideas?
Edit:
- Found this very informative: The Linux Page Cache and pdflush
- As Zan pointed out, the memory isn't actually free. What I mean is that it's not being used by applications and I want to control what should be cached in memory.
vmtouch seems like a good tool for the job.
Highlights:
vmtouch manual
EDIT: Usage as asked in the question is listed in example 5 on vmtouch Hompage
EDIT2: As noted in the comments, there is now a git repository available.
This is also possible using the vmtouch Virtual Memory Toucher utility.
The tool allows you to control the filesystem cache on a Linux system. You can force or lock a specific file or directory in the VM cache subsystem, or use it to check to see what portions of a file/directory are contained within VM.
Or...
A poor man's trick for getting stuff into the filesystem cache is to simply cat it and redirect that to /dev/null.
Linux will cache as much disk IO in memory as it can. This is what the cache and buffer memory stats are. It'll probably do a better job than you will at storing the right things.
However, if you insist in storing your data in memory, you can create a ram drive using either tmpfs or ramfs. The difference is that ramfs will allocate all the memory you ask for, were as tmpfs will only use the memory that your block device is using. My memory is a little rusty, but you should be able to do:
or
and then copy your data to the directory. Obviously, when you turn the machine off or unmount that partition, your data will be lost.
After some extensive reading on the 2.6 kernel swapping and page-caching features I found 'fcoretools'. Which consists of two tools;
(In case someone else finds this interesting I'm posting this here)
There are two kernel settings that can help considerably even without using other tools:
swappiness
tells linux kernel how aggressively it should use swap. Quoting the Wikipedia article:
vfs_cache_pressure
Quoting from vm.txt:
By setting
swappiness
high (like 100), the kernel moves everything it doesn't need to swap, freeing RAM for caching files. And by settingvfs_cache_pressure
lower (let's say to 50, not to 0!), it will favor caching files instead of keeping application data in RAM.(I work on a large Java project and every time I run it, it took a lot of RAM and flushed the disk cache, so the next time I compiled the project everything was read from disk again. By adjusting these two settings, I manage to keep the sources and compiled output cached in RAM, which speeds the process considerably.)
You may be able to have a program that just
mmap
s your files then stays running.If you have plenty of memory you can simply read in the files you want to cache with cat or similar. Linux will then do a good job of keeping it around.
I very much doubt that it is actually serving files from the disk with 3 GB RAM free. Linux file caching is very good.
If you are seeing disk IO, I would look into your logging configurations. Many logs get set as unbuffered, in order to guarantee that the latest log information is available in the event of a crash. In systems that have to be fast regardless, use buffered log IO or use a remote log server.
Desktop computers (eg. ubuntu) already uses preloading files (at least, popular shared libraries) to memory on boot. It is used to speed up booting and startup time of different bloarware like FF, OO, KDE and GNOME (with evolution bloat-mailer).
The tool is named readahead http://packages.ubuntu.com/dapper/admin/readahead
There is also corresponding syscall: readahead(2) http://linux.die.net/man/2/readahead
There is also project of preloading daemon: http://linux.die.net/man/8/preload