3

I am counting current open files by a process pid by :- cmd1:-

ls /proc/$pid/fd/* | wc -l 

and then I am calculating percentage by fetching Max open Files limits. cmd2:-

$ cat /proc/$pid/limits
Limit                     Soft Limit           Hard Limit           Units

Max resident set          unlimited            unlimited            bytes     
Max processes             30425                30425                processes
Max open files            4096                 4096                 files
Max locked memory         65536                65536                bytes

Only pasting part of output.

I am calculating percentage :-

cmd1/cmd2*100

i.e if ls /proc/$pid/fd/* | wc -l gives 1000. then percentage will be :- 1000/4096*100= approx 25%

sometimes I am getting this percentage as 220.

scenario is reproducible now,it happened in one server and I found that in /proc/pid/fd/29: there are 5000 log files opened by a process.

It means process pid is crossing the Max open file limits(4096 ) .

1 Answer 1

1

Note that the limit is on the value of newly created file descriptors (as in open()/socket()/pipe() and so on will never return a number greater than n-1 if the limit was set to n, and dup2(1, n or n+1...) will fail), not on the number of currently open files or file descriptors.

In effect, from the instant the limit is set to n, that will prevent that process from opening more than n files, but if file descriptors above n had already been created before the limit was lowered, then the process can possibly have more than n files open.

Example:

$ limit descriptors 10000 # ulimit -n 10000 in bash
$ perl -MBSD::Resource -MPOSIX -e '
    dup2(1,$_) for (2000..4000);
    setrlimit(RLIMIT_NOFILE, 1024, 1024);
    exec zsh'
zsh$ ls /proc/$$/fd | wc -w
2009
zsh$ readlink /proc/$$/fd/3000
/dev/pts/1
zsh$ repeat 100 exec {a}>&1
zsh$ ls /proc/$$/fd | wc -w
2109
zsh$ grep files /proc/$$/limits
Max open files            1024                 1024                 files

At the time zsh was executed, the process had fds 2000 to 4000 open to /dev/pts/1. Yet the fd limit was 1024.

It already had 2009 fds, but was still able to create 100 more.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.