Skip to content

Commit f1f1f25

Browse files
bobrikakpm00
authored andcommitted
proc: report open files as size in stat() for /proc/pid/fd
Many monitoring tools include open file count as a metric. Currently the only way to get this number is to enumerate the files in /proc/pid/fd. The problem with the current approach is that it does many things people generally don't care about when they need one number for a metric. In our tests for cadvisor, which reports open file counts per cgroup, we observed that reading the number of open files is slow. Out of 35.23% of CPU time spent in `proc_readfd_common`, we see 29.43% spent in `proc_fill_cache`, which is responsible for filling dentry info. Some of this extra time is spinlock contention, but it's a contention for the lock we don't want to take to begin with. We considered putting the number of open files in /proc/pid/status. Unfortunately, counting the number of fds involves iterating the open_files bitmap, which has a linear complexity in proportion with the number of open files (bitmap slots really, but it's close). We don't want to make /proc/pid/status any slower, so instead we put this info in /proc/pid/fd as a size member of the stat syscall result. Previously the reported number was zero, so there's very little risk of breaking anything, while still providing a somewhat logical way to count the open files with a fallback if it's zero. RFC for this patch included iterating open fds under RCU. Thanks to Frank Hofmann for the suggestion to use the bitmap instead. Previously: ``` $ sudo stat /proc/1/fd | head -n2 File: /proc/1/fd Size: 0 Blocks: 0 IO Block: 1024 directory ``` With this patch: ``` $ sudo stat /proc/1/fd | head -n2 File: /proc/1/fd Size: 65 Blocks: 0 IO Block: 1024 directory ``` Correctness check: ``` $ sudo ls /proc/1/fd | wc -l 65 ``` I added the docs for /proc/<pid>/fd while I'm at it. [[email protected]: use bitmap_weight() to count the bits] Link: https://lkml.kernel.org/r/[email protected] [[email protected]: include linux/bitmap.h for bitmap_weight()] [[email protected]: return errno from proc_fd_getattr() instead of setting negative size] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ivan Babrou <[email protected]> Cc: Alexey Dobriyan <[email protected]> Cc: Al Viro <[email protected]> Cc: Christoph Anton Mitterer <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: David Laight <[email protected]> Cc: Ivan Babrou <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Kalesh Singh <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Paul Gortmaker <[email protected]> Cc: Theodore Ts'o <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 2122e2a commit f1f1f25

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

Documentation/filesystems/proc.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fixes/update part 1.1 Stefani Seibold <[email protected]> June 9 2009
4747
3.10 /proc/<pid>/timerslack_ns - Task timerslack value
4848
3.11 /proc/<pid>/patch_state - Livepatch patch operation state
4949
3.12 /proc/<pid>/arch_status - Task architecture specific information
50+
3.13 /proc/<pid>/fd - List of symlinks to open files
5051
5152
4 Configuring procfs
5253
4.1 Mount options
@@ -2149,6 +2150,22 @@ AVX512_elapsed_ms
21492150
the task is unlikely an AVX512 user, but depends on the workload and the
21502151
scheduling scenario, it also could be a false negative mentioned above.
21512152

2153+
3.13 /proc/<pid>/fd - List of symlinks to open files
2154+
-------------------------------------------------------
2155+
This directory contains symbolic links which represent open files
2156+
the process is maintaining. Example output::
2157+
2158+
lr-x------ 1 root root 64 Sep 20 17:53 0 -> /dev/null
2159+
l-wx------ 1 root root 64 Sep 20 17:53 1 -> /dev/null
2160+
lrwx------ 1 root root 64 Sep 20 17:53 10 -> 'socket:[12539]'
2161+
lrwx------ 1 root root 64 Sep 20 17:53 11 -> 'socket:[12540]'
2162+
lrwx------ 1 root root 64 Sep 20 17:53 12 -> 'socket:[12542]'
2163+
2164+
The number of open files for the process is stored in 'size' member
2165+
of stat() output for /proc/<pid>/fd for fast access.
2166+
-------------------------------------------------------
2167+
2168+
21522169
Chapter 4: Configuring procfs
21532170
=============================
21542171

fs/proc/fd.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/namei.h>
88
#include <linux/pid.h>
99
#include <linux/ptrace.h>
10+
#include <linux/bitmap.h>
1011
#include <linux/security.h>
1112
#include <linux/file.h>
1213
#include <linux/seq_file.h>
@@ -279,6 +280,30 @@ static int proc_readfd_common(struct file *file, struct dir_context *ctx,
279280
return 0;
280281
}
281282

283+
static int proc_readfd_count(struct inode *inode, loff_t *count)
284+
{
285+
struct task_struct *p = get_proc_task(inode);
286+
struct fdtable *fdt;
287+
288+
if (!p)
289+
return -ENOENT;
290+
291+
task_lock(p);
292+
if (p->files) {
293+
rcu_read_lock();
294+
295+
fdt = files_fdtable(p->files);
296+
*count = bitmap_weight(fdt->open_fds, fdt->max_fds);
297+
298+
rcu_read_unlock();
299+
}
300+
task_unlock(p);
301+
302+
put_task_struct(p);
303+
304+
return 0;
305+
}
306+
282307
static int proc_readfd(struct file *file, struct dir_context *ctx)
283308
{
284309
return proc_readfd_common(file, ctx, proc_fd_instantiate);
@@ -319,9 +344,29 @@ int proc_fd_permission(struct user_namespace *mnt_userns,
319344
return rv;
320345
}
321346

347+
static int proc_fd_getattr(struct user_namespace *mnt_userns,
348+
const struct path *path, struct kstat *stat,
349+
u32 request_mask, unsigned int query_flags)
350+
{
351+
struct inode *inode = d_inode(path->dentry);
352+
int rv = 0;
353+
354+
generic_fillattr(&init_user_ns, inode, stat);
355+
356+
/* If it's a directory, put the number of open fds there */
357+
if (S_ISDIR(inode->i_mode)) {
358+
rv = proc_readfd_count(inode, &stat->size);
359+
if (rv < 0)
360+
return rv;
361+
}
362+
363+
return rv;
364+
}
365+
322366
const struct inode_operations proc_fd_inode_operations = {
323367
.lookup = proc_lookupfd,
324368
.permission = proc_fd_permission,
369+
.getattr = proc_fd_getattr,
325370
.setattr = proc_setattr,
326371
};
327372

0 commit comments

Comments
 (0)