开发者

Purgeable Memory Regions on Linux

On Mac OS X, vm_allocate() allows creation of a purgeable memory region. This region may be reclaimed at any time (other than when it is locked) by the kernel when memory pressure is detected. It is useful for caching data, etc.

I'm wondering if a similar mechanism exists on Linux (kernel 2.6). Google sear开发者_JS百科ches lead me to some info on Android discussing allocation of shared memory that may be made purgeable, but I have not seen any talk of API for actually doing this on vanilla Linux. A review of the man pages for mmap, shmat, etc, show no flags for setting a purgeable region. Is this functionality available on Linux?

The notes about this feature on Android are here:

MemoryFile Reference

MemoryFile Sample

Thanks!


There is such similar system in 2.6.39-rc1. It is called "Transcendent memory":

  • Transcendent memory, 2009
  • Transcendent memory in a nutshell, 2011
  • Slides from 2011: Transcendent Memory and Friends, TmemNotVirt-Linuxcon2011-Final.pdf (It also lists some similar terms)
  • Api specification from oss.oracle: Transcendent Memory Interface Specification Version 0.0.1 - 081202

Update: There is also short into at wikipedia: https://en.wikipedia.org/wiki/Transcendent_memory

In computing, transcendent memory (aka "tmem") is a concept explored by Dan Magenheimer. Transcendent memory is a class of memory that is of unknown and dynamically variable size, is addressable only indirectly by the kernel, can be configured either as persistent or as "ephemeral" (meaning it will be around for a while, but might disappear without warning), and is still fast enough to be synchronously accessible

One can think of transcendent memory as a sort of RAM disk with some interesting characteristics: nobody knows how big it is, writes to the disk may not succeed, and, potentially, data written to the disk may vanish before being read back again. At a first blush, it may seem like a relatively useless sort of device, but it is hoped that transcendent memory will be able to improve performance in a few situations.

Introduced in Linux kernel 2.6.39.[1][2] Implementation by Dan Magenheimer of Oracle Corporation. Xen 4.0 supports tmem in the hypervisor.


I have never heard of such a feature for Linux.

In Linux, memory can be swapped out at any time. If it is a file-backed memory map then it can be written to disk and released instead of swapping.

Probably the closest that you will be able to get is to either periodically check the current memory usage and release the cache yourself, or use a Linux kernel patched with a low-memory notifier which will send either a signal or a filehandle event when memory pressure is high.

I say a patched kernel because I don't think that any of the proposals for a resource notifier have been accepted into the mainline kernel. Here was one attempt.

With a newer kernel with trace events you could probably hook a memory pressure trace and do it that way. It'd not be particularly portable to other Linux kernel versions though.


Thanks for links! I dig code up to ashmem_pin_region function. It is a ioctl (ioctl(fd, ASHMEM_PIN, &pin);) on device http://www.google.com/codesearch/p?hl=en#2wSbThBwwIw/libcutils/ashmem-dev.c&q=ashmem_pin_region&exact_package=git://android.git.kernel.org/platform/system/core.git&sa=N&cd=1&ct=rc&l=76

and no-op on host: http://www.google.com/codesearch/p?hl=en#2wSbThBwwIw/libcutils/ashmem-host.c&q=ashmem_pin_region&exact_package=git://android.git.kernel.org/platform/system/core.git&sa=N&cd=2&ct=rc&l=86

And here is a kernel part /kernel/msm.git›mm›ashmem.c: http://www.google.com/codesearch/p?hl=en#KMCRKdMbI4g/mm/ashmem.c&q=ASHMEM_PIN&exact_package=git://android.git.kernel.org/kernel/msm.git&sa=N&cd=1&ct=rc&l=463

ASHMEM seems to be an google extension: http://cs736-android.pbworks.com/w/page/5834465/ASHMEM http://elinux.org/Android_Kernel_Features It is not in mainline.

There was a (failed) try to push it (at first - logger, and explaining letter from Greg "KH " at Sat, Nov 28, 2009 at 9:58 PM, subject "Re: [PATCH 0/2] staging/android fixes"). Also there was a google summer of code project proposal: http://elinux.org/Mainline_Android_kernel_functionality (this page have a useful links and a good presentation from Greg KH about google's android patches and linux.)


Seems (as fas as I can get), no similar interface can be used for user-space in Linux.

Purgeable memory is integrated into Mach microkernel by Apple. And they protect this technique by patent http://www.faqs.org/patents/app/20090024809 (United States Patent 7831784).

There is a some similar, used by kernel itself - disk cache (page cache). It caches pages, backed by FS. They (if not dirty) can be deleted if more memory is needed. More about this is at http://www.westnet.com/~gsmith/content/linux-pdflush.htm

Also, there is madvise() with MADV_DONTNEED flag (http://linux.die.net/man/2/madvise) but this will free (purge) given memory region immediately.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜