2016-07-31 62 views
1

内核文档中的过度提交文章只是提到过载模式0基于启发式提交处理。它没有概述涉及的启发式。Linux over commit heuristic

有人可以阐明真正的启发式是什么?任何与内核源码相关的链接都可以工作!

在此先感谢

+0

而你的问题是? – Li357

+0

什么是启发式? – KodeWarrior

回答

3

其实,过量使用会计的内核文档有一些细节:https://www.kernel.org/doc/Documentation/vm/overcommit-accounting

Linux内核支持以下过量使用处理模式

0 - 启发式过量使用处理。

明显的地址空间过量使用被拒绝。用于典型系统。它确保了严重的分配失败,同时允许过量使用 以减少交换使用。允许root在这种模式下分配稍多的内存。这是默认设置。

另外Documentation/sysctl/vm.txt

overcommit_memory: 此值包含一个标志,使内存过度。
当该标志为0时,内核将尝试估计 免费的记忆中留下当用户空间的请求更多的内存量...

见文档/ VM /过量使用,会计和 毫米/ mmap.c :: __ vm_enough_memory () 了解更多信息。

此外,man 5 proc

/proc/sys/vm/overcommit_memory此文件包含内核虚拟内存会计模式。 价值观是:

   0: heuristic overcommit (this is the default) 
       1: always overcommit, never check 
       2: always check, never overcommit 

在模式0中,mmap(2)MAP_NORESERVE呼叫不检查,默认的检查非常薄弱,导致得到一个过程“OOM杀死”的风险。

因此,通过启发式方法禁用非常大的分配,但有时应用程序可能会分配比系统中物理内存大小更多的虚拟内存,如果它没有使用全部内存。用MAP_NORESERVE可存储内存量可能会更高。

设置为“overcommit策略通过sysctl`vm设置。overcommit_memory””,这样我们就可以发现它是如何在源代码中实现: http://lxr.free-electrons.com/ident?v=4.4;i=sysctl_overcommit_memory,在line 112 of mm/mmap.c

112 int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS; /* heuristic overcommit */ 

,不断OVERCOMMIT_GUESS(在linux/mman.h定义)定义只有在line 170 of mm/mmap.cused实际上,这是实施启发式:

138 /* 
139 * Check that a process has enough memory to allocate a new virtual 
140 * mapping. 0 means there is enough memory for the allocation to 
141 * succeed and -ENOMEM implies there is not. 
142 * 
143 * We currently support three overcommit policies, which are set via the 
144 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting 
145 * 
146 * Strict overcommit modes added 2002 Feb 26 by Alan Cox. 
147 * Additional code 2002 Jul 20 by Robert Love. 
148 * 
149 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise. 
150 * 
151 * Note this is a helper function intended to be used by LSMs which 
152 * wish to use this logic. 
153 */ 
154 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin) 
... 
170   if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { 
171     free = global_page_state(NR_FREE_PAGES); 
172     free += global_page_state(NR_FILE_PAGES); 
173 
174     /* 
175     * shmem pages shouldn't be counted as free in this 
176     * case, they can't be purged, only swapped out, and 
177     * that won't affect the overall amount of available 
178     * memory in the system. 
179     */ 
180     free -= global_page_state(NR_SHMEM); 
181 
182     free += get_nr_swap_pages(); 
183 
184     /* 
185     * Any slabs which are created with the 
186     * SLAB_RECLAIM_ACCOUNT flag claim to have contents 
187     * which are reclaimable, under pressure. The dentry 
188     * cache and most inode caches should fall into this 
189     */ 
190     free += global_page_state(NR_SLAB_RECLAIMABLE); 
191 
192     /* 
193     * Leave reserved pages. The pages are not for anonymous pages. 
194     */ 
195     if (free <= totalreserve_pages) 
196       goto error; 
197     else 
198       free -= totalreserve_pages; 
199 
200     /* 
201     * Reserve some for root 
202     */ 
203     if (!cap_sys_admin) 
204       free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10); 
205 
206     if (free > pages) 
207       return 0; 
208 
209     goto error; 
210   } 

因此,启发式是估计物理内存页面有多少现在使用(free)的方式,当请求更多的内存为p漫游(申请要求pages页)。

随着始终启用过量使用(“1”),此函数总是返回0(“有足够的内存用于这个请求”)

164   /* 
165   * Sometimes we want to use more memory than we have 
166   */ 
167   if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS) 
168     return 0; 

没有这个默认启发式,在模式“2”,内核将尽量考虑到所请求的页面pages获得新Committed_AS(从/proc/meminfo):

162   vm_acct_memory(pages); 
... 

这个is actuallyvm_committed_as只是增量 - __percpu_counter_add(&vm_committed_as, pages, vm_committed_as_batch);

212   allowed = vm_commit_limit(); 

一些神奇is here:

401 /* 
402 * Committed memory limit enforced when OVERCOMMIT_NEVER policy is used 
403 */ 
404 unsigned long vm_commit_limit(void) 
405 { 
406   unsigned long allowed; 
407 
408   if (sysctl_overcommit_kbytes) 
409     allowed = sysctl_overcommit_kbytes >> (PAGE_SHIFT - 10); 
410   else 
411     allowed = ((totalram_pages - hugetlb_total_pages()) 
412       * sysctl_overcommit_ratio/100); 
413   allowed += total_swap_pages; 
414 
415   return allowed; 
416 } 
417 

所以,allowed设置了其中一个为vm.overcommit_kbytes的sysctl或vm.overcommit_ratio物理RAM的百分比,以及交换分区的大小千字节。

213   /* 
214   * Reserve some for root 
215   */ 
216   if (!cap_sys_admin) 
217     allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10); 

仅允许用于根 一些量存储器(PAGE_SHIFT是12健康的人,PAGE_SHIFT-10是从千字节只是转换到页计数)。

218 
219   /* 
220   * Don't let a single process grow so big a user can't recover 
221   */ 
222   if (mm) { 
223     reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10); 
224     allowed -= min_t(long, mm->total_vm/32, reserve); 
225   } 
226 
227   if (percpu_counter_read_positive(&vm_committed_as) < allowed) 
228     return 0; 

如果在对请求进行计数之后,所有用户空间仍然具有提交的内存量小于允许的数量,则分配它。在其他情况下,拒绝该请求(并且不要求该请求)。

229 error: 
230   vm_unacct_memory(pages); 
231 
232   return -ENOMEM; 

换句话说,作为总结由安德里斯·布劳威尔,9内存,9.6过量使用和OOM,2003-02-01 - https://www.win.tue.nl/~aeb/linux/lk/lk-9.html “Linux内核Linux内核的一些讨论。”:

在正确的方向前进

由于30年2月5日的值是:

  • 0(默认):如前:猜测多少过量使用是合理的,
  • 1:从不拒绝任何的malloc(),
  • 2:得到准确的过量使用 - 从来没有承诺的虚拟地址空间比交换空间,加上身体的一小部分overcommit_ratio较大记忆。

因此,“2”是的请求之后使用的存储器量精确的计算,和“0”是启发式估计。