As memory access is typically orders of magnitude faster than
disk access, the idea of using a part of RAM as an in-memory
storage device has been one of the earliest performance
optimizations realized by the computer science community.
Even though today this type of optimization takes place
transparently inside modern operating systems (via mechanisms like
the disk buffer cache), there are still circumstances where
manually creating a RAM disk might
still be quite useful.
A Solaris customer using the ramdiskadm(1m)
utility to create a Ram disk device for a real-time application
posed the following question: "Are RAM disks swappable?". From a
real-time perspective, a RAM disk not only yields significantly
better seek performance but also provides for more deterministic
behavior compared to the traditional rotating disk platter.
The customer's concern here is that, under dire circumstances, is
it possible for operating system swap out the RAM disk
memory? Needless to say, if a swap out occurred it would put
a big crimp in the real-time application's predictability.
To get an idea of what's going on when a RAM disk is created, let's use the Solaris kstat(1m) kernel statitics utility to see how memory is being allocated. First let's see what memory looks like before creating a RAM disk:
# pagesize
4096
# kstat -n system_pages | grep pagesfree
pagesfree 96334
# kstat -n system_pages | grep pageslocke pageslocked 26170
So, on this particular system, where a page is 4096 bytes, there are currently 96334 pages free, and 26170 pages that are locked. Now let's create a 50MB RAM disk:
# ramdiskadm -a rd 50m
/dev/ramdisk/rd
# ramdiskadm
Block Device Size
Removable
/dev/ramdisk/rd 52428800 Yes
# kstat -n system_pages | grep pagesfree
pagesfree 83507
# kstat -n system_pages | grep pageslocked
pageslocked 38988
Let's subtract the original number of pageslocked from the latest value and multiply by the pagesize:
# pagesize
4096
# bc
(38988-26170)\*4096
52502528
\^D
The increase in locked pages can be attributed to the creation of the RAM disk (50m + a small amount of overhead). So yes, these pages are locked into memory. But it would be nice to get a definitive statement on what pageslocked actually means. According to McDougal, Mauro and Gregg's Performance and Tools: DTrace and MDB Techniques for Solaris 10 and OpenSolaris, pageslocked is "Total number of pages locked into memory by the kernel and user processes". Furthermore, the man page for plock(3C), a related library routine which enables programmers to lock memory segments, states that "Locked segments are immune to all routine swapping". What's routine swapping?
Hmm. Anybody care to shed some light on this?