During the two days of Asterisk performance testing, we often encounter the following error: "Too program open files ".
We know that in Linux, the file descriptor is a simple integer. Traditionally, the file descriptor of the standard input is 0, and the standard output is 1, the standard error is 2. POSIX defines stdin_fileno, stdout_fileno, and stderr_fileno to replace 0, 1, and 2. The definitions of these three symbolic constants are located in the header file unistd. h.
The valid range of the file descriptor is 0 to open_max. How many file descriptors can a process open at most? The following describes the asterisk process as an example.
# Ps-Ef | grep Asterisk
Obtain the asterisk PID from the second column as 19488.
# Cat/proc/19488/limits
A red line indicates that the asterisk process can open up to 1024 file descriptors (excluding its sub-processes or created threads ). The subtasks under the/proc/19488/task/directory are listed in detail. Each subfolder also contains a limits file, which limits the subtasks.
How many file descriptors does a process open?
# Ll/proc/19488/FD/
Each file descriptor opened by a process is listed in detail in the subdirectory FD. Similarly, file descriptors opened by a sub-task may exist in/proc/19488/task/xxxx/FD. You need to know a few, execute
# Ll/proc/19488/FD/| WC-l
How do I know which files are associated with a process and its sub-processes? Lsof can do this. Note that the associated file and open file descriptor are two different concepts. The number of associated files may be much larger than the number of opened file descriptors.
# Lsof | grep asterisk | WC-l
You can also use the PID of the parent process to filter data.
# Lsof | grep 19488 | WC-l
The value I get here is 9525.
How can I modify the limits of file descriptors? Temporary modification. You can use ulimit.
# Ulimit-shn2048
However, this only affects the current Session. When the terminal reconnects or the current user exits, the configuration becomes invalid. To modify the/etc/security/limits. conf file permanently, add the following two lines:
* Hard nofile 2048
* Soft nofile 2048
The kernel parameters also have restrictions on file descriptors. If the value is set to be greater than the kernel limit, it is not possible:
Find the kernel parameters of file-MAX:
# Sysctl-A | grep file-max
Change the kernel parameters of file-MAX:
# Sysctl-W file-max= 65535
Sysctl is temporary. To take effect permanently, you can change the sysctl file, edit the/etc/sysctl. conf file, and add or modify the following line:
FS. File-max = 65535
Note that the restrictions on file descriptors are not limited to the ones described here, but may also be related to the startup parameters of the process and the user's environment settings. Of course, if the file descriptor is not closed in time due to a process bug, this increase limit is only a cure, and the bug must be fixed.
In addition, lsof lists the resources occupied by the system, but these resources do not necessarily occupy open file descriptors (such as shared memory, semaphores, message queues, and memory ing. and so on.
The/proc/sys/fs/file-max value is smaller than lsof | WC-L. Asterisk itself provides a STARTUP script named safe_asterisk, which configures the file descriptor.