Linux文件描述符表和vmalloc

时间:2013-10-30 18:01:30

标签: linux linux-kernel

我看到Linux内核使用vmallocfdtable分配大于特定阈值的内存。我想知道何时发生这种情况并获得更明确的信息。

static void *alloc_fdmem(size_t size)
{
       /*
       * Very large allocations can stress page reclaim, so fall back to
       * vmalloc() if the allocation size will be considered "large" by the VM.
       */
       if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
              void *data = kmalloc(size, GFP_KERNEL|__GFP_NOWARN);
              if (data != NULL)
                     return data;
       }
       return vmalloc(size);
}
alloc_fdmem调用

alloc_fdtable,从expand_fdtable调用最后一个函数

我写了这段代码来打印尺寸。

#include <stdio.h>

#define PAGE_ALLOC_COSTLY_ORDER 3
#define PAGE_SIZE 4096


int main(){
      printf("\t%d\n", PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER);
}

输出

 ./printo
  32768

那么,内核切换到使用vmalloc分配fdtable需要多少文件?

1 个答案:

答案 0 :(得分:2)

所以PAGE_SIZE&lt;&lt; PAGE_ALLOC_COSTLY_ORDER是32768

这称为:

  data = alloc_fdmem(nr * sizeof(struct file *));

即。它用于存储struct文件指针。

如果您的指针是4个字节,则当您的32768/4 = 8192个打开文件时会发生这种情况,如果指针是8个字节,则会发生4096个打开文件。

相关问题