Linux Swap File – Why is the Modification Date of My Swapfile Not Changing?

linuxstatswapswap-file

If I check with stat the modification date is not up to date

  File: /home/shares/swap.file
  Size: 1962934272  Blocks: 3833864    IO Block: 4096   regular file
Device: 801h/2049d  Inode: 15          Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-09-03 20:36:39.736199577 +0100
Modify: 2019-09-03 20:36:39.646199258 +0100
Change: 2019-09-03 20:36:39.646199258 +0100
 Birth: -

even while top says I use swap:

top - 05:11:23 up  8:04,  5 users,  load average: 1.01, 0.80, 0.41
Tasks: 159 total,   2 running, 157 sleeping,   0 stopped,   0 zombie
%Cpu(s): 29.5 us,  0.8 sy,  0.0 ni, 68.6 id,  1.0 wa,  0.0 hi,  0.1 si,  0.0 st
MiB Mem :    926.1 total,    170.6 free,    327.1 used,    428.4 buff/cache
MiB Swap:   1872.0 total,   1763.7 free,    108.2 used.    532.8 avail Mem 

I am curious about this because I want to make sure the right swapfile (on external HDD) is used on my Raspberry so my sd card will not break.

In case the external HDD is not mounted into the folder then the swapfile will be created in the mountpoint /home/shares which is then on the card and not on the HDD (/dev/sda).

If the mount happens after the creation of the file then I am not able to check without unmounting – which will not be possible sometimes.

Best Answer

The swapfile is not updated through the filesystem. When you use swapon, the kernel just queries the filesystem about where exactly the file's data areas (extents) are located, then begins directly updating those areas at block device level.

(This is why you cannot have swap files on a multi-device Btrfs volume, cannot have compression or copy-on-write enabled on the file, etc.)

If you have multiple swap areas, the swapon command will tell you whether they're actively used.

Related Question