2

I would like to implement an in memory cache (of some type) in my user space linux application.

what i'm after is essentially the same behavior as linux's filesystem buffer cache : whatever memory isn't used by anything else, is used by my processes that are caching.

if i were to write this with a hard coded limit on how large my cache should be, I must essentially limit my cache to some predefined value, instead of it growing if the rest of the processes aren't using as much memory, or it shrinking, if memory usage balloons.

what i would like, is linux to call some kind of callback that notifies me to drop some entries from my cache, when it actually needs memory.

that way, whatever memory is needed by the processes can be taken away from my cache, and whatever is left of memory is dedicated to my cache.

is there a way to do this in linux?

is the best method of doing this just monitoring /proc/meminfo and increasing my cache size such that the used physical memory is barely less than total physical memory?

Michael Xu
  • 557
  • 1
  • 5
  • 14
  • It depends on what you want to do. If you are doing something with files, it might be better to let linux cache those. – Prof. Falken Feb 22 '12 at 08:02
  • This question reminds me of the Varnish HTTP cache/accelerator which attempts to do the same. – jørgensen Feb 22 '12 at 08:50
  • >>>barely less than total memory - Utilizing close to 100% [or even 90%] is not actually very good idea as serving time approaches infinity in an exponential fashion. See any queuing theory document or http://www.johndcook.com/blog/2009/01/30/server-utilization-joel-on-queuing/ – Fakrudeen Feb 22 '12 at 09:49
  • fakrudeen, your comment doesn't make any sense. a bigger cache will always be better than a smaller cache. – Michael Xu Feb 23 '12 at 08:59

1 Answers1

1

This sounds very much like you are trying to code around the virtual memory manager (VMM). This is not likely to either work the way you would like, or even if it does: be very efficient.

This issue is discussed in the Varnish architecture notes written by Poul-Henning Kamp. I urge you to read this document, as he talks about how Varnish was designed to interact efficiently with the VMM, and may give you some ideas for alternative design ideas for your cache.

Any user process that is attempting to adjust itself to system memory pressure, say by looking at the ratio: resident set size (rss) / virtual size (vsize). Is going to suffer from being in a feedback loop with the VMM, and that feedback loop may have detrimental effects on the process, the system, or both.

Look at the documentation for /proc as to where to get rss, and vsize for a process.

Good luck.

Marc Butler
  • 1,346
  • 7
  • 13