MADVISE(2)

πŸ’– MADVISE(2) β€” the system calls manual, but make it a group chat πŸ’–

okay bestie, so this is madvise from the System Calls Manual, and it's literally the "hey system, lemme tell you a secret about my memory" page. yes, you're gonna gaslight the kernel. affectionately. 🫢


✨ NAME ✨

madvise, posix_madvise β€” give advice about use of memory

like… you're not telling the system what to do, you're advising it. it's giving consultant energy. πŸ’…

πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)

πŸŽ€ SYNOPSIS πŸŽ€

#include <sys/mman.h>

int
madvise(void *addr, size_t len, int behav);

int
posix_madvise(void *addr, size_t len, int behav);

πŸ¦‹ DESCRIPTION πŸ¦‹

So. The madvise() system call lets a process that already knows how it's gonna use its memory just… tell the system about it. no cap. The posix_madvise() interface is identical, except β€” and this is the important part, bestie β€” it returns an error number on error and does not modify errno. it's there for standards conformance, iykyk.

Here are the known behaviors, aka the vibes you can request:

  • MADV_NORMAL β€” tells the system to revert to the default paging behavior. we're going back to factory settings, babe. ✨
  • MADV_RANDOM β€” it's a hint that pages are gonna be accessed randomly, so prefetching is probably not advantageous. not me reading memory pages out of order πŸ™ˆ
  • MADV_SEQUENTIAL β€” causes the VM system to depress the priority of pages immediately preceding a given page when it's faulted in. bye to the past pages, we're moving forward only. πŸ‘‘
  • MADV_WILLNEED β€” causes pages in a given virtual address range to temporarily have higher priority, and if they're already in memory, it decreases the likelihood of them being freed. additionally, the pages already in memory will be immediately mapped into the process, thereby eliminating unnecessary overhead of going through the entire process of faulting the pages in. > ⚠️ this WILL NOT fault pages in from backing store β€” it just quickly maps the pages that are already in memory into the calling process. so like, it's a shortcut, not a miracle. 😭
  • MADV_DONTNEED β€” allows the VM system to decrease the in-memory priority of pages in the specified address range. consequently, future references to this address range are more likely to incur a page fault. girl, you're on your own now. πŸ’€
  • MADV_FREE β€” gives the VM system the freedom to free pages, and tells the system that the information in the specified page range is no longer important. this is an efficient way of allowing malloc(3) to free pages anywhere in the address space while keeping the address space valid. the next time that page is referenced, the page might be demand zeroed, or it might contain the data that was there before the MADV_FREE call. references made to that address space range will not make the VM system page the information back in from backing store until the page is modified again. it's giving "i might remember, i might not, we'll see" 🌸
  • MADV_NOSYNC β€” request that the system not flush the data associated with this map to physical backing store unless it needs to. typically this prevents the file system update daemon from gratuitously writing pages dirtied by the VM system to physical disk. note that VM/file system coherency is always maintained β€” this feature simply ensures that the mapped data is only flushed when it needs to be, usually by the system pager.

this feature is typically used when you want to use a file-backed shared memory area to communicate between processes (IPC) and you don't particularly need the data being stored in that area to be physically written to disk. with this feature you get the equivalent performance with mmap that you'd expect to get with SysV shared memory calls, but in a more controllable and less restrictive manner. however, note that this feature is not portable across UNIX platforms (though some may do the right thing by default). for more information see the MAP_NOSYNC section of mmap(2). 🧚

  • MADV_AUTOSYNC β€” undoes the effects of MADV_NOSYNC for any future pages dirtied within the address range. the effect on pages already dirtied is indeterminate β€” they may or may not be reverted. you can guarantee reversion by using the msync(2) or fsync(2) system calls. so if you need certainty, bestie, go get it yourself. πŸ’…
  • MADV_NOCORE β€” region is not included in a core file.
  • MADV_CORE β€” include region in a core file.
  • MADV_PROTECT β€” informs the VM system that this process should not be killed when swap space is exhausted. the process must have superuser privileges. this should be used judiciously in processes that must remain running for the system to properly function. like, main character energy, but you better actually be the main character. πŸ‘‘

Portable programs that call the posix_madvise() interface should use the aliases POSIX_MADV_NORMAL, POSIX_MADV_SEQUENTIAL, POSIX_MADV_RANDOM, POSIX_MADV_WILLNEED, and POSIX_MADV_DONTNEED rather than the flags described above. no cap, that's the portable way. 🌈


🫢 RETURN VALUES 🫢

The madvise() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

(zero = slay. minus one = not slay, check errno.) ✨

😭 ERRORS 😭

The madvise() system call will fail if:

  • [EINVAL] β€” the behav argument is not valid. you made up a vibe. the system is not amused. πŸ’€
  • [ENOMEM] β€” the virtual address range specified by the addr and len arguments is not valid. that memory is not real, babe. πŸ™ˆ
  • [EPERM] β€” MADV_PROTECT was specified and the process does not have superuser privileges. bestie you are not that powerful. 😭

πŸ‘€ SEE ALSO πŸ‘€

mincore(2), mprotect(2), msync(2), munmap(2), posix_fadvise(2)

go read those too, they're all part of the memory-management friend group. πŸŽ€

⭐ STANDARDS ⭐

The posix_madvise() interface conforms to IEEE Std 1003.1-2001 ("POSIX.1").

🌸 HISTORY 🌸

The madvise() system call first appeared in 4.4BSD. old money, honestly. πŸ‘‘


and that's the whole page, bestie!! this was the Debian build of the July 12, 2015 MADVISE(2) man page β€” yes, 2015, it's vintage, it's iconic, we respect it. mwah πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…

πŸ“¦ where this page comes from