Sunday, 21 October 2018

Solaris : How to find number of open files by a process By Devan

Solaris : How to find number of open files by a process

Sometimes, the error message “too many open files” is displayed in /var/adm/messages. In these cases, it is often useful to know how many files are currently open by a process. The number of files currently opened by a certain process can be examined by:
1. The pfiles command
2. Using the /proc file system

Using pfiles command

The pfiles command displays the current limit of the number of open files for the process and more information about all currently open files of that process. See the following example. Run the sleep 120 seconds command in background and then run pfiles command on the PID of the sleep command.
# sleep 120&
[1] 29803
# pfiles 29803
29803:  sleep 120
  Current rlimit: 256 file descriptors
   0: S_IFCHR mode:0620 dev:270,0 ino:12582920 uid:84883 gid:7 rdev:24,2
      O_RDWR|O_NOCTTY|O_LARGEFILE
      /devices/pseudo/pts@0:2
   1: S_IFCHR mode:0620 dev:270,0 ino:12582920 uid:84883 gid:7 rdev:24,2
      O_RDWR|O_NOCTTY|O_LARGEFILE
      /devices/pseudo/pts@0:2
   2: S_IFCHR mode:0620 dev:270,0 ino:12582920 uid:84883 gid:7 rdev:24,2
      O_RDWR|O_NOCTTY|O_LARGEFILE
      /devices/pseudo/pts@0:2
As you can see from the output, 3 files are opened by the commands sleep. For just checking the number of open files of a process, the following command sequence can be used:
# pfiles 29803 | nawk '/[0-9]: /{a++}END{print a}'
3
However, according to pfiles man pages, pfiles “stop[s] their target processes while inspecting them and reporting the results”. This can have a negative impact on application performance in case a process is very active at the time the pfiles command is run on it.

Using the /proc file system

The number of files opened by a process can also be checked by examining the contents of directory /proc/[PID]/fd (in this example, /proc/29803/fd):
# ls -al /proc/29803/fd/
total 19
dr-x------   2 root     root        8208 Oct 24 15:46 .
dr-x--x--x   5 root     root         864 Oct 24 15:46 ..
c---------   1 soluser1 tty       24,  2 Oct 24 15:47 0
c---------   1 soluser1 tty       24,  2 Oct 24 15:47 1
c---------   1 soluser1 tty       24,  2 Oct 24 15:47 2
There is one character special file for each open file of that process. By counting the number of files in the fd subdirectory of /proc/[PID], the number of open files of the process can be examined quickly.
# ls /proc/29803/fd | nawk 'END{print NR}'
3

No comments:

Post a Comment