Based on the documentation and the xnu source I read, I understand that Mac OS X caches file I / O using Unified Buffer Cache (UBC). UBC is growing as much as possible based on available RAM, but UBC pages are among the first to be sacrificed when memory becomes more tense.
In my driver, I deal with various metadata on the disk. I would like to be able to use UBC or a similar mechanism to store MRU caches of this data in order to speed things up, but at the same time the kernel will be able to return this memory whenever necessary. However, metadata does not represent file data and therefore does not directly go to the UBC domain. Is there a lower level mechanism that I can use, or can I somehow use only the part of UBC that deals with buffers on my own?
I am now hunting for the HFS + source code to try to figure out how and how it caches file system metadata, albeit without much success.
The main alternative, of course, is to reserve a specific memory area for caches and delete my own LRU. I can choose a fixed cache size or use some kind of heuristic, but it will always use too little memory when RAM is full, and too much if it is not.
Update:
After searching a few more, I found that instances IOBufferMemoryDescriptorcan be created using the option kIOMemoryPurgeable. This allows you to call IOMemoryDescriptor::setPurgeable(), to mark the memory of "fair play" for discarding. I will try and update the question with the results.
source
share