/proc/sys/fs/file-max 的默认值

tar*_*ion 9 max-file-descriptors linux-kernel

我知道 /proc/sys/fs/file-max 定义了打开文件描述符的最大数量,可以在运行时或启动期间设置。

但是:它的默认值是多少?检查我公司的 10 台服务器给了我 7 个不同的值,这些值似乎都是随机的。

内核文档只是不断提到可以更改该值 - 但没有提到如何计算默认值。

你们中有人知道如何确定默认值吗?

c4f*_*t0r 14

file-max您看到的限制proc fs是结构中 struct 中"./include/linux/fs.h"的一个值是:

/* And dynamically-tunable limits and defaults: */
struct files_stat_struct {
  unsigned long nr_files;   /* read only */
  unsigned long nr_free_files;  /* read only */
  unsigned long max_files;    /* tunable THIS IS OUR VALUE */
};
Run Code Online (Sandbox Code Playgroud)

现在,在./fs/file_table.cfiles_stat_struct方面开始使用:

struct files_stat_struct files_stat = {
  .max_files = NR_FILE /* This constant is 8192 */
};
Run Code Online (Sandbox Code Playgroud)

现在在以前的文件"./fs/file_table.c"中将具有使真正工作的功能

void __init files_init(unsigned long mempages)
{
  unsigned long n;

  filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
      SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);

  /*
   * One file with associated inode and dcache is very roughly 1K.
   * Per default don't use more than 10% of our memory for files. 
   */

  n = (mempages * (PAGE_SIZE / 1024)) / 10;
  files_stat.max_files = max_t(unsigned long, n, NR_FILE);
  files_defer_init();
  lg_lock_init(files_lglock);
  percpu_counter_init(&nr_files, 0);
}
Run Code Online (Sandbox Code Playgroud)

从我files_init在宏中看到的和查看的内容来看max_t,如果文件的 10% 内存大于 8192,则将使用该值,除非 8192。

files_init 在内核开始执行时使用,您需要在调用时查看标志 SLAB_PANICkmem_cache_create以创建通用文件平板缓存。

现在你需要看 ./kernel/sysctl.c

  {
    .procname = "file-max",
    .data   = &files_stat.max_files,
    .maxlen   = sizeof(files_stat.max_files),
    .mode   = 0644,
    .proc_handler = proc_doulongvec_minmax,
  },
Run Code Online (Sandbox Code Playgroud)

file-max 是内存的 10%,如果你的系统有不同的内存大小,我认为这是正常的。