From patchwork Wed Apr 29 01:07:32 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yannick Brosseau X-Patchwork-Id: 6293101 Return-Path: X-Original-To: patchwork-linux-fsdevel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 1963B9F46B for ; Wed, 29 Apr 2015 01:07:46 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 22118202FE for ; Wed, 29 Apr 2015 01:07:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id F24F920320 for ; Wed, 29 Apr 2015 01:07:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1031217AbbD2BHl (ORCPT ); Tue, 28 Apr 2015 21:07:41 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:46761 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1031166AbbD2BHk (ORCPT ); Tue, 28 Apr 2015 21:07:40 -0400 Received: from pps.filterd (m0004348 [127.0.0.1]) by m0004348.ppops.net (8.14.5/8.14.5) with SMTP id t3T13hhA025335; Tue, 28 Apr 2015 18:07:39 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-type; s=facebook; bh=nIsijs0Ts6lNJsxxeVtoIUOtCPJwYtpWG/3vclyIaOE=; b=Vf027GABQ01TheFfnmPMSjM9AB8x99LC/HI65Ml0uwPkb5iP8evzuP3l/NuiTUG61G26 BBLLOgjzfJqCJAG0QqvZyDLPENotIlCAzy1UNkrvU0iaCvfjSLtGzIXzkAKbKSYbLgDO uFIQzFXBhOIqASime884Igy80OTLPDM6WVE= Received: from mail.thefacebook.com ([199.201.64.23]) by m0004348.ppops.net with ESMTP id 1u2j2g8b70-1 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Tue, 28 Apr 2015 18:07:39 -0700 Received: from scientist-laptop.DHCP.TheFacebook.com (192.168.52.13) by mail.TheFacebook.com (192.168.16.17) with Microsoft SMTP Server (TLS) id 14.3.195.1; Tue, 28 Apr 2015 18:07:36 -0700 From: Yannick Brosseau To: CC: , , , Yannick Brosseau Subject: [PATCH] proc: Add number of allocated FD in PID/status file Date: Tue, 28 Apr 2015 18:07:32 -0700 Message-ID: <1430269652-17705-1-git-send-email-scientist@fb.com> X-Mailer: git-send-email 2.1.4 MIME-Version: 1.0 X-Originating-IP: [192.168.52.13] X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.13.68, 1.0.33, 0.0.0000 definitions=2015-04-29_01:2015-04-29, 2015-04-28, 1970-01-01 signatures=0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID,T_RP_MATCHES_RCVD,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Iterating over the fd directory to count the number of allocated FD in a process is expensive if you have lots of them. This expose the count of open FDs from the fdtable open_fds bitmap. Signed-off-by: Yannick Brosseau --- Documentation/filesystems/proc.txt | 3 ++- fs/file.c | 12 ++++++++++++ fs/proc/array.c | 11 +++++++---- include/linux/fdtable.h | 2 +- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Documentation/filesystems/proc.txt b/Documentation/filesystems/proc.txt index c3b6b30..d2ceb81 100644 --- a/Documentation/filesystems/proc.txt +++ b/Documentation/filesystems/proc.txt @@ -205,7 +205,7 @@ asynchronous manner and the value may not be very precise. To see a precise snapshot of a moment, you can see /proc//smaps file and scan page table. It's slow but very precise. -Table 1-2: Contents of the status files (as of 3.20.0) +Table 1-2: Contents of the status files (as of 4.1.0) .............................................................................. Field Content Name filename of the executable @@ -220,6 +220,7 @@ Table 1-2: Contents of the status files (as of 3.20.0) Uid Real, effective, saved set, and file system UIDs Gid Real, effective, saved set, and file system GIDs FDSize number of file descriptor slots currently allocated + FDAlloc number of file descriptor allocated Groups supplementary group list NStgid descendant namespace thread group ID hierarchy NSpid descendant namespace process ID hierarchy diff --git a/fs/file.c b/fs/file.c index 93c5f89..caa973b 100644 --- a/fs/file.c +++ b/fs/file.c @@ -236,6 +236,18 @@ static int count_open_files(struct fdtable *fdt) return i; } +int count_open_files_exact(struct fdtable *fdt) +{ + int size = fdt->max_fds; + int i, count = 0; + + for (i = 0; i < size / BITS_PER_LONG; i++) + if (fdt->open_fds[i]) + count += hweight_long(fdt->open_fds[i]); + + return count; +} + /* * Allocate a new files structure and copy contents from the * passed in files structure. diff --git a/fs/proc/array.c b/fs/proc/array.c index fd02a9e..421ea674 100644 --- a/fs/proc/array.c +++ b/fs/proc/array.c @@ -140,7 +140,7 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns, struct task_struct *tracer; const struct cred *cred; pid_t ppid, tpid = 0, tgid, ngid; - unsigned int max_fds = 0; + unsigned int max_fds = 0, count_fds = 0; rcu_read_lock(); ppid = pid_alive(p) ? @@ -155,8 +155,10 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns, cred = get_task_cred(p); task_lock(p); - if (p->files) + if (p->files) { max_fds = files_fdtable(p->files)->max_fds; + count_fds = count_open_files_exact(files_fdtable(p->files)); + } task_unlock(p); rcu_read_unlock(); @@ -169,7 +171,7 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns, "TracerPid:\t%d\n" "Uid:\t%d\t%d\t%d\t%d\n" "Gid:\t%d\t%d\t%d\t%d\n" - "FDSize:\t%d\nGroups:\t", + "FDSize:\t%d\nFDAlloc:\t%d\nGroups:\t", get_task_state(p), tgid, ngid, pid_nr_ns(pid, ns), ppid, tpid, from_kuid_munged(user_ns, cred->uid), @@ -180,7 +182,8 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns, from_kgid_munged(user_ns, cred->egid), from_kgid_munged(user_ns, cred->sgid), from_kgid_munged(user_ns, cred->fsgid), - max_fds); + max_fds, + count_fds); group_info = cred->group_info; for (g = 0; g < group_info->ngroups; g++) diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h index 230f87b..5ca987e 100644 --- a/include/linux/fdtable.h +++ b/include/linux/fdtable.h @@ -112,7 +112,7 @@ extern void __fd_install(struct files_struct *files, unsigned int fd, struct file *file); extern int __close_fd(struct files_struct *files, unsigned int fd); - +extern int count_open_files_exact(struct fdtable *fdt); extern struct kmem_cache *files_cachep; #endif /* __LINUX_FDTABLE_H */