|
| 1 | +--- |
| 2 | +title: Recap of Filesystems caching and Memory |
| 3 | +weight: 2 |
| 4 | + |
| 5 | +### FIXED, DO NOT MODIFY |
| 6 | +layout: learningpathall |
| 7 | +--- |
| 8 | + |
| 9 | +## Recap of File Systems |
| 10 | + |
| 11 | +A file system is the method an operating system uses to store, organize, and manage data on storage devices like hard drives or SSDs. Linux uses a **Virtual File System (VFS)** layer to provide a uniform interface to different file system types (e.g., `ext4`, `xfs`, `btrfs`), allowing programs to interact with them in a consistent way. |
| 12 | + |
| 13 | +Developers typically interact with the file system through system calls or standard library functions—for example, using `open()`, `read()`, and `write()` in C to access files. For instance, reading a configuration file at `/etc/myapp/config.json` involves navigating the file system hierarchy and accessing the file’s contents through these interfaces. To speed up access to such files, the operating system creates a file system cache, managed by the kernel and residing in main memory (RAM). This cache temporarily stores recently accessed file data to speed up future reads and reduce disk I/O. |
| 14 | + |
| 15 | + |
| 16 | +## Observing Memory Usage |
| 17 | + |
| 18 | +The operating system has to manage the memory usage for the entire system. |
| 19 | + |
| 20 | +The command `free -h` provides a snapshot of memory usage. |
| 21 | + |
| 22 | +```output |
| 23 | + total used free shared buff/cache available |
| 24 | +Mem: 7.6Gi 533Mi 7.0Gi 948Ki 215Mi 7.1Gi |
| 25 | +Swap: 0B 0B 0B |
| 26 | +``` |
| 27 | + |
| 28 | +The `free` command summarises the memory usage into the following columns. |
| 29 | + |
| 30 | +- `total`: Total installed memory |
| 31 | +- `used`: Memory in use (excluding cache/buffers) |
| 32 | +- `free`: Unused memory |
| 33 | +- `shared`: Memory used by tmpfs and shared between processes |
| 34 | +- `buff/cache`: Sum of memory used by kernel buffers and cache |
| 35 | +- `available`: An estimate of memory available to start new apps |
| 36 | + |
| 37 | + |
| 38 | +### What is the posix_fadvise syscall? |
| 39 | + |
| 40 | +`posix_fadvise` is a Linux system call that allows a program to provide the kernel with hints about its expected file access patterns, such as sequential or random reads. These hints help the kernel optimize caching and I/O performance but are purely advisory—the system may choose to ignore them. It’s particularly useful for tuning performance when working with large files or when bypassing unnecessary caching. |
| 41 | + |
| 42 | +### When the posix_fadvise syscall could be of use? |
| 43 | + |
| 44 | +You should consider using `posix_fadvise` when: |
| 45 | + |
| 46 | +- You're reading or writing large files that won't fit entirely in RAM. |
| 47 | +- You want to avoid polluting the cache with data you won't reuse. |
| 48 | +- You know the access pattern ahead of time and can optimize accordingly. |
| 49 | + |
| 50 | +The syscall doesn’t guarantee behavior, but it influences how the kernel allocates caching resources. |
| 51 | + |
| 52 | + |
| 53 | + |
0 commit comments