Default value of /proc/sys/fs/file-max

I am aware that /proc/sys/fs/file-max defines the maximum number of open file descriptors and can be set on runtime or during boot.

However: what is its default value? Checking 10 servers in my company gives me 7 different values, which all seem kind of random.

The kernel documentation just keeps mentioning that the value can be changed - but not how the default value is calculated.

Does anyone of you know how the default value is determined?


Solution 1:

The file-max limit that you see under proc fs is one value in struct in "./include/linux/fs.h" the struct is:

/* 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 */
};

Now in ./fs/file_table.c the files_stat_struct is begin used:

struct files_stat_struct files_stat = {
  .max_files = NR_FILE /* This constant is 8192 */
};

Now in the previous file "./fs/file_table.c" will have the function that will make the real job

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);
}

From What I can see in the files_init and looking the macro max_t, If 10% of memory for files is more then 8192, that values will be used, Unless 8192.

files_init is used when kernel is begin executed and you need to see the flag SLAB_PANIC when kmem_cache_create is called for create the general file slab cache.

Now you need to look ./kernel/sysctl.c

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

The file-max is 10% of memory, If your system has different memory size, I think that is normal.