Does the Linux filesystem cache files efficiently?

前端 未结 5 2030
悲&欢浪女
悲&欢浪女 2020-12-25 13:08

I\'m creating a web application running on a Linux server. The application is constantly accessing a 250K file - it loads it in memory, reads it and sends back some info to

相关标签:
5条回答
  • 2020-12-25 13:19

    Yes, if you do not modify the file each time you open it.

    Linux will hold the file's information in copy-on-write pages in memory, and "loading" the file into memory should be very fast (page table swap at worst).

    Edit: Though, as cdhowie points out, there is no 'linux filesystem'. However, I believe the relevant code is in linux's memory management, and is therefore independent of the filesystem in question. If you're curious, you can read in the linux source about handling vm_area_struct objects in linux/mm/mmap.c, mainly.

    0 讨论(0)
  • 2020-12-25 13:20

    The file should be cached, but make sure the noatime option is set on the mount, otherwise the access time will attempt to be saved to the file, invalidating the cache.

    0 讨论(0)
  • 2020-12-25 13:30

    I guess putting that file into ramdisk (tmpfs) may make enough advantage without big modifications. Unless you are really serious about response time in microseconds unit.

    0 讨论(0)
  • 2020-12-25 13:36

    As people have mentioned, mmap is a good solution here.

    But, one 250k file is very small. You might want to read it in and put it in some sort of memory structure that matches what you want to send back to the user on startup. Ie, if it is a text file an array of lines might be a good choice, etc.

    0 讨论(0)
  • 2020-12-25 13:37

    Yes, definitely. It will keep accessed files in memory indefinitely, unless something else needs the memory.

    You can control this behaviour (to some extent) with the fadvise system call. See its "man" page for more details.

    A read/write system call will still normally need to copy the data, so if you see a real bottleneck doing this, consider using mmap() which can avoid the copy, by mapping the cache pages directly into the process.

    0 讨论(0)
提交回复
热议问题