From patchwork Wed Jun 24 13:52: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: 6667851 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 844449F46B for ; Wed, 24 Jun 2015 13:53:01 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9EAAC203B8 for ; Wed, 24 Jun 2015 13:53:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A570D204E7 for ; Wed, 24 Jun 2015 13:52:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753653AbbFXNwq (ORCPT ); Wed, 24 Jun 2015 09:52:46 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:8215 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753593AbbFXNwn (ORCPT ); Wed, 24 Jun 2015 09:52:43 -0400 Received: from pps.filterd (m0004060 [127.0.0.1]) by mx0b-00082601.pphosted.com (8.14.5/8.14.5) with SMTP id t5ODoWt8002987; Wed, 24 Jun 2015 06:52:42 -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=V7tOruF0sqOjR4ASBUwZ+BGSPIY/fNi5aKwyioeysnMJ93igUf33zUaM/atudkp7/bbe jg97Bwt7lPApc1EDpa+xGtLi/UhXrNennDm9BmYFlbwBDcPUqCndBclFpwNSLiHilgak o+4j1psuV1Ci2mw18lZNFoK2OM69LjlbfuE= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0b-00082601.pphosted.com with ESMTP id 1v7tf00psk-1 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Wed, 24 Jun 2015 06:52:42 -0700 Received: from scientist-laptop.thefacebook.com (192.168.52.123) by mail.thefacebook.com (192.168.16.13) with Microsoft SMTP Server (TLS) id 14.3.195.1; Wed, 24 Jun 2015 06:52:40 -0700 From: Yannick Brosseau To: CC: , , Yannick Brosseau Subject: [RESEND PATCH] proc: Add number of allocated FD in PID/status file Date: Wed, 24 Jun 2015 06:52:32 -0700 Message-ID: <1435153952-5087-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.123] X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.14.151, 1.0.33, 0.0.0000 definitions=2015-06-24_05:2015-06-23, 2015-06-24, 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=-8.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable 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 */