本文介绍了分配“临时"资源内存(在Linux中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到允许进程分配临时"内存的任何系统功能,即该进程认为可丢弃的内存,并且在需要内存时可以被系统占用,但是允许过程尽可能地利用可用内存.换句话说,该进程告诉系统可以在不使用内存块时牺牲该内存块.释放该块也比替换掉它更可取(替换掉它,而不是重新构造其内容更昂贵,或者更昂贵).

I'm trying to find any system functionality that would allow a process to allocate "temporary" memory - i.e. memory that is considered discardable by the process, and can be take away by the system when memory is needed, but allowing the process to benefit from available memory when possible. In other words, the process tells the system it's OK to sacrifice the block of memory when the process is not using it. Freeing the block is also preferable to swapping it out (it's more expensive, or as expensive, to swap it out rather then re-constitute its contents).

系统(例如Linux)在内核中具有这些内容,例如F/S内存缓存.我正在寻找类似的东西,但是可以在用户空间使用.

Systems (e.g. Linux), have those things in the kernel, like F/S memory cache. I am looking for something like this, but available to the user space.

我知道可以从程序中执行此操作,但是处理此问题实际上更多地是内核工作.在某种程度上,我要问内核:

I understand there are ways to do this from the program, but it's really more of a kernel job to deal with this. To some extent, I'm asking the kernel:

  • 如果您需要减少我或其他流程的驻留时间,请先删除这些临时页面
  • 如果您要删除这些临时页面,请不要换掉它们,只需取消映射它们

特别是,我对可以在Linux上运行的解决方案感兴趣,但是希望了解其他操作系统是否存在任何解决方案.

Specifically, I'm interested on a solution that would work on Linux, but would be interested to learn if any exist for any other O/S.

更新

我希望它如何工作的示例:

An example on how I expect this to work:

  • 映射页面(通过交换).与现在的可用资源没有区别.
  • 告诉内核该页面是临时的"(由于缺少更好的名称),这意味着如果该页面消失了,我不希望它被分页.
  • 告诉内核我需要返回"临时页面.如果自从我将页面标记为临时"以来未对其进行映射,则会得知发生了这种情况.如果还没有,那么它将开始表现为常规页面.

这是在现有的MM上完成的问题:

Here are the problems to have that done over existing MM:

要使页面不被分页,我必须不分配任何内容.但是,他们可以随时被调出页面,而无需另行通知.使用mincore()进行测试并不能保证在mincore()完成时该页面仍然存在.使用mlock()需要提升的特权.

To make pages not being paged in, I have to allocate them over nothing. But then, they can get paged out at any time, without notice. Testing with mincore() doesn't guarantee that the page will still be there by the time mincore() finishes. Using mlock() requires elevated privileges.

因此,最接近我的方法是使用mlock()和匿名页面.遵循我之前概述的期望,它将是:

So, the closest I can get to this is by using mlock(), and anonymous pages. Following the expectations I outlined earlier, it would be:

  • 映射一个匿名的锁定页面. (MAP_ANON | MAP_LOCKED | MAP_NORESERVE).用魔术标记页面.
  • 要将页面设为临时",请解锁页面
  • 需要该页面时,请再次将其锁定.如果存在魔力,那是我的数据,否则就丢失了,我需要重新构造它.

但是,当我使用页面时,我实际上并不需要将页面锁定在RAM中.另外,如果内存过量使用,则MAP_NORESERVE会出现问题.

However, I don't really need for pages to be locked in RAM when I'm using them. Also, MAP_NORESERVE is problematic if memory is overcommitted.

推荐答案

我不确定您是否完全了解您的需求.请记住,过程虚拟内存(其地址空间是虚拟的),则内核正在处理虚拟地址到物理地址的转换(使用 MMU )并使用分页.因此,页面错误可以随时发生.内核将选择在任意时刻进行页面调入或页面调出-并选择要交换的页面(仅内核关心RAM,并且它可以随意调出任何物理RAM页面).也许您希望内核告诉您何时真正丢弃了页面.内核如何在不通知您的进程的情况下从您的进程中删除临时内存?内核可能会带走,然后再给回一些RAM....(所以您想知道给定的回显内存何时是新鲜的)

I'm not sure to understand exactly your needs. Remember that processes run in virtual memory (their address space is virtual), that the kernel is dealing with virtual to physical address translation (using the MMU) and with paging. So page fault can happen at any time. The kernel will choose to page-in or page-out at arbitrary moments - and will choose which page to swap (only the kernel care about RAM, and it can page-out any physical RAM page at will). Perhaps you want the kernel to tell you when a page is genuinely discarded. How would the kernel take away temporary memory from your process without your process being notified ? The kernel could take away and later give back some RAM.... (so you want to know when the given back memory is fresh)

您可以将 mmap(2)与首先,然后再次使用MAP_FIXED|MAP_PRIVATE(在相同的内存范围内).另请参见 mincore(2) mlock(2)

You might use mmap(2) with MAP_NORESERVE first, then again (on the same memory range) with MAP_FIXED|MAP_PRIVATE. See also mincore(2) and mlock(2)

您还可以稍后将 madvise(2)MADV_WONTNEEDMADV_WILLNEED等.

You can also later use madvise(2) with MADV_WONTNEED or MADV_WILLNEED etc..

也许您想mmap一些设备,例如/dev/null/dev/full/dev/zero或(更可能是)编写自己的内核模块以提供类似的设备.

Perhaps you want to mmap some device like /dev/null, /dev/full, /dev/zero or (more likely) write your own kernel module providing a similar device.

GNU赫德具有外部传呼机机制 ...在Linux上,您还不能完全做到这一点. (也许考虑在某些 FUSE 挂载的文件上使用mmap).

GNU Hurd has an external pager mechanism... You cannot yet get exactly that on Linux. (Perhaps consider mmap on some FUSE mounted file).

这篇关于分配“临时"资源内存(在Linux中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 12:08