Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp3355160pxb; Sat, 9 Oct 2021 08:16:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJznMlxpCconCPWaolZ92IGi0ii8JA9cSmaH+Ka1cmdMreo1HrNYdxwJOnHzu7NcjUu5ctbn X-Received: by 2002:a17:906:22c9:: with SMTP id q9mr12822900eja.216.1633792592522; Sat, 09 Oct 2021 08:16:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633792592; cv=none; d=google.com; s=arc-20160816; b=eEhtxwCYFhBHG8rdhC753NLXmK9ohyWcQj6WXt2FWHNPuMO63Xb6TDgES8g42tFAfd LRAb/IG6HFQLRTnQqm5/cI4hzRmbgUrCdyhRQWQhD33icDn8rPqbRFLAOd9n3RCxJY0J HWy+j4xPIkEa3HWpUv/Kesucz2iOWXL/q4WzWu8UO8R+FS52d1RmA8rCf0CMv0A3RiC1 d/eqb0UiGyX+S+J4zd4XeqvHOCh3JUww/cflPF3ef+ecgJETX6xuYvobqCWFXgO+/c/Z kvyqo23dUn0i3irtTkp93EnNQOpt0G9RkhaTpOs1naUlLSMdmmvRNfz+wmUHgs+TxOd8 PENw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=OsjRPBuVAZJUiH/1Tx72owDpcys2APioOZ0ZDm+Jjuw=; b=lhLC/ehQQ6BZMqqOyyUN/J9Fei8wz02VifxrbiqLoJ9aq+WDOFrkQkMpkbvBSUGfXh GEt1oDmXr2No0SlYtsH+doaw3oRIhgI49SMXATQE19cFp1MWpdhJ5pXNFjAbdd6Yq+aV ETus1jeFv9Rk8VSBR9+xVDrW838uYAEbXCnZ+Oh+lFwVTRQ1v43E0CmtT2Egz16OC77X 6gOHAmqDO8k8dixQbv5UTUlqOWQiV341tW0WWN5OljYHL7gt8WM4bxIulIAh/yHsg8MW 8x6AK93ZikkYq3nEFhNbGEo5K6qneg/lvlTFYstfG7Q3Fban5551GJWhKJIpVNpIiobs 7ubA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=I83NbEN3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n26si3834493edr.145.2021.10.09.08.16.07; Sat, 09 Oct 2021 08:16:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=I83NbEN3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234848AbhJIPPc (ORCPT + 99 others); Sat, 9 Oct 2021 11:15:32 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:38030 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S234652AbhJIPPZ (ORCPT ); Sat, 9 Oct 2021 11:15:25 -0400 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 199BuD1B000392; Sat, 9 Oct 2021 11:13:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=OsjRPBuVAZJUiH/1Tx72owDpcys2APioOZ0ZDm+Jjuw=; b=I83NbEN3nMcRnBJCS8ba6wlY7yuDfgQBwW4Al5U8+uXQXosXBSh7xS0LpD3EQU4NKvTm 7ybvsGnlKwUmntopz6lmhCEebpA7Xija9E3eUCf87fBHRNIpGdLzW46eOULCHQLlXfnq UB2fr/SP5vvD0aK/LtWUkolnK+SBxm6Qrg+HRqrWmKQaEMOQlURH+yLYsu3b9l6qMEMi KFASBCXwuJ59QlEePc6OQkQXxWe7kVYrbA+aEm2G/EeQfc9qOtBmOTXHoI0X4LUmcQk1 sJcDApLmnBfrfkYsPttMr3tmzxd5Wlaj3Nru+2CFxpQMVonRQKhGelz3lO4vNeb7wGp9 4g== Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com with ESMTP id 3bk8p23njq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 09 Oct 2021 11:13:22 -0400 Received: from m0098416.ppops.net (m0098416.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 199FCf9L021297; Sat, 9 Oct 2021 11:13:22 -0400 Received: from ppma05fra.de.ibm.com (6c.4a.5195.ip4.static.sl-reverse.com [149.81.74.108]) by mx0b-001b2d01.pphosted.com with ESMTP id 3bk8p23njc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 09 Oct 2021 11:13:21 -0400 Received: from pps.filterd (ppma05fra.de.ibm.com [127.0.0.1]) by ppma05fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 199FCtOS011912; Sat, 9 Oct 2021 15:13:20 GMT Received: from b06avi18626390.portsmouth.uk.ibm.com (b06avi18626390.portsmouth.uk.ibm.com [9.149.26.192]) by ppma05fra.de.ibm.com with ESMTP id 3bk2q91y7t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 09 Oct 2021 15:13:19 +0000 Received: from d06av24.portsmouth.uk.ibm.com (mk.ibm.com [9.149.105.60]) by b06avi18626390.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 199F7oZg45875672 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 9 Oct 2021 15:07:51 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 108B442041; Sat, 9 Oct 2021 15:13:17 +0000 (GMT) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3309A4203F; Sat, 9 Oct 2021 15:13:12 +0000 (GMT) Received: from pratiks-thinkpad.ibm.com (unknown [9.43.17.147]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTP; Sat, 9 Oct 2021 15:13:11 +0000 (GMT) From: "Pratik R. Sampat" To: bristot@redhat.com, christian@brauner.io, ebiederm@xmission.com, lizefan.x@bytedance.com, tj@kernel.org, hannes@cmpxchg.org, mingo@kernel.org, juri.lelli@redhat.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org, containers@lists.linux.dev, containers@lists.linux-foundation.org, psampat@linux.ibm.com, pratik.r.sampat@gmail.com Subject: [RFC 5/5] proc/cpuns: Make procfs load stats CPU namespace aware Date: Sat, 9 Oct 2021 20:42:43 +0530 Message-Id: <20211009151243.8825-6-psampat@linux.ibm.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211009151243.8825-1-psampat@linux.ibm.com> References: <20211009151243.8825-1-psampat@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: X_x5yr7sENtw0e_STqG8RDaFy-Rk1EG- X-Proofpoint-GUID: HSvmPRK-eWOmoMD9w29veN912rT3gHlL X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.182.1,Aquarius:18.0.790,Hydra:6.0.391,FMLib:17.0.607.475 definitions=2021-10-09_04,2021-10-07_02,2020-04-07_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 malwarescore=0 adultscore=0 spamscore=0 suspectscore=0 bulkscore=0 mlxlogscore=999 priorityscore=1501 impostorscore=0 clxscore=1015 phishscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2109230001 definitions=main-2110090109 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This commit adds support provide a virtualized view to the /proc/stat load statistics. The load, idle, irq and the rest of the information of a physical CPU is now displayed for its corresponding virtual CPU counterpart. The procfs file only populates the virtualized view for the CPUs based on the restrictions from cgroupfs set upon it. Signed-off-by: Pratik R. Sampat --- fs/proc/stat.c | 50 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/fs/proc/stat.c b/fs/proc/stat.c index 6561a06ef905..3ff39e7362bb 100644 --- a/fs/proc/stat.c +++ b/fs/proc/stat.c @@ -14,6 +14,7 @@ #include #include #include +#include #ifndef arch_irq_stat_cpu #define arch_irq_stat_cpu(cpu) 0 @@ -107,13 +108,14 @@ static void show_all_irqs(struct seq_file *p) static int show_stat(struct seq_file *p, void *v) { - int i, j; + int i, j, pcpu; u64 user, nice, system, idle, iowait, irq, softirq, steal; u64 guest, guest_nice; u64 sum = 0; u64 sum_softirq = 0; unsigned int per_softirq_sums[NR_SOFTIRQS] = {0}; struct timespec64 boottime; + cpumask_var_t cpu_mask; user = nice = system = idle = iowait = irq = softirq = steal = 0; @@ -122,27 +124,39 @@ static int show_stat(struct seq_file *p, void *v) /* shift boot timestamp according to the timens offset */ timens_sub_boottime(&boottime); - for_each_possible_cpu(i) { +#ifdef CONFIG_CPU_NS + if (current->nsproxy->cpu_ns == &init_cpu_ns) { + cpumask_copy(cpu_mask, cpu_possible_mask); + } else { + cpumask_copy(cpu_mask, + ¤t->nsproxy->cpu_ns->v_cpuset_cpus); + } +#else + cpumask_copy(cpu_mask, cpu_possible_mask); +#endif + + for_each_cpu(i, cpu_mask) { struct kernel_cpustat kcpustat; u64 *cpustat = kcpustat.cpustat; - kcpustat_cpu_fetch(&kcpustat, i); + pcpu = get_pcpu_cpuns(current->nsproxy->cpu_ns, i); + kcpustat_cpu_fetch(&kcpustat, pcpu); user += cpustat[CPUTIME_USER]; nice += cpustat[CPUTIME_NICE]; system += cpustat[CPUTIME_SYSTEM]; - idle += get_idle_time(&kcpustat, i); - iowait += get_iowait_time(&kcpustat, i); + idle += get_idle_time(&kcpustat, pcpu); + iowait += get_iowait_time(&kcpustat, pcpu); irq += cpustat[CPUTIME_IRQ]; softirq += cpustat[CPUTIME_SOFTIRQ]; steal += cpustat[CPUTIME_STEAL]; guest += cpustat[CPUTIME_GUEST]; guest_nice += cpustat[CPUTIME_GUEST_NICE]; - sum += kstat_cpu_irqs_sum(i); - sum += arch_irq_stat_cpu(i); + sum += kstat_cpu_irqs_sum(pcpu); + sum += arch_irq_stat_cpu(pcpu); for (j = 0; j < NR_SOFTIRQS; j++) { - unsigned int softirq_stat = kstat_softirqs_cpu(j, i); + unsigned int softirq_stat = kstat_softirqs_cpu(j, pcpu); per_softirq_sums[j] += softirq_stat; sum_softirq += softirq_stat; @@ -162,18 +176,30 @@ static int show_stat(struct seq_file *p, void *v) seq_put_decimal_ull(p, " ", nsec_to_clock_t(guest_nice)); seq_putc(p, '\n'); - for_each_online_cpu(i) { +#ifdef CONFIG_CPU_NS + if (current->nsproxy->cpu_ns == &init_cpu_ns) { + cpumask_copy(cpu_mask, cpu_online_mask); + } else { + cpumask_copy(cpu_mask, + ¤t->nsproxy->cpu_ns->v_cpuset_cpus); + } +#else + cpumask_copy(cpu_mask, cpu_online_mask); +#endif + for_each_cpu(i, cpu_mask) { struct kernel_cpustat kcpustat; u64 *cpustat = kcpustat.cpustat; - kcpustat_cpu_fetch(&kcpustat, i); + pcpu = get_pcpu_cpuns(current->nsproxy->cpu_ns, i); + + kcpustat_cpu_fetch(&kcpustat, pcpu); /* Copy values here to work around gcc-2.95.3, gcc-2.96 */ user = cpustat[CPUTIME_USER]; nice = cpustat[CPUTIME_NICE]; system = cpustat[CPUTIME_SYSTEM]; - idle = get_idle_time(&kcpustat, i); - iowait = get_iowait_time(&kcpustat, i); + idle = get_idle_time(&kcpustat, pcpu); + iowait = get_iowait_time(&kcpustat, pcpu); irq = cpustat[CPUTIME_IRQ]; softirq = cpustat[CPUTIME_SOFTIRQ]; steal = cpustat[CPUTIME_STEAL]; -- 2.31.1