Linux: Find Out How Many File Descriptors Are Being Used
Generally, a file descriptor is an index for an entry in a kernel-resident array data structure containing the details of open files by a process. On Linux, the set of file descriptors open in a process can be accessed under the path /proc/PID/fd/, where PID is the process identifier.
In Unix-like systems, file descriptors can refer to any Unix file type named in a file system. As well as regular files, this includes directories, block and character devices (also called "special files"), Unix domain sockets, and named pipes. File descriptors can also refer to other objects that do not normally exist in the file system, such as anonymous pipes and network sockets.
while administrating a box, you may wanted to find out what a processes is doing and find out how many file descriptors (fd) are being used. You will surprise to find out that process does open all sort of files:
=> Actual log file
=> Actual log file
=> /dev files
=> UNIX Sockets
=> Network sockets
=> Library files /lib /lib64
=> Executables and other programs etc
In this quick post, I will explain how to to count how many file descriptors are currently in use on your Linux server system.
Step # 1 Find Out PID
Step # 1 Find Out PID
To find out PID for mysqld process, enter:
# ps aux | grep mysqld
OR
# pidof mysqld
Output:
# ps aux | grep mysqld
OR
# pidof mysqld
Output:
28290
Step # 2 List File Opened By a PID # 28290
Use the lsof command or /proc/$PID/ file system to display open fds (file descriptors), run:
# lsof -p 28290
# lsof -a -p 28290
OR
# cd /proc/28290/fd
# ls -l | less
You can count open file, enter:
# ls -l | wc -l
# lsof -p 28290
# lsof -a -p 28290
OR
# cd /proc/28290/fd
# ls -l | less
You can count open file, enter:
# ls -l | wc -l
Tip: Count All Open File Handles
To count the number of open file handles of any sort, type the following command:
# lsof | wc -l
Sample outputs:
# lsof | wc -l
Sample outputs:
5436
List File Descriptors in Kernel Memory
Type the following command:
# sysctl fs.file-nr
Sample outputs:
# sysctl fs.file-nr
Sample outputs:
fs.file-nr = 1020 0 70000
Where,
1. 1020 The number of allocated file handles.
2. 0 The number of unused-but-allocated file handles.
3. 70000 The system-wide maximum number of file handles.
You can use the following to find out or set the system-wide maximum number of file handles:
# sysctl fs.file-max
Sample outputs:
# sysctl fs.file-max
Sample outputs:
fs.file-max = 70000
0 comments:
Post a Comment