Received: by 2002:a05:6a10:8a4d:0:0:0:0 with SMTP id dn13csp185391pxb; Thu, 12 Aug 2021 13:55:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxq4wkqH7Hs5gYuoEicR64I6HjfwZBUdYuo3aFc4ivczua5T7c4tY5JDc6fKsOQ9bZGltSw X-Received: by 2002:a17:906:fb91:: with SMTP id lr17mr5539319ejb.110.1628801708578; Thu, 12 Aug 2021 13:55:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628801708; cv=none; d=google.com; s=arc-20160816; b=lYdHaEDuL3Vpp6sjz11/CC1VNowKwdDIAJwt0tfDvVcfekFMOq3BOFtAUzpu2/2P13 G/Ifwbgi1zB4GpbIXGz8cWucuejoOXnp9TfnvqACCj25nhG9pK+chDolhrjX+tCC6x+N z61e5g1LOrNW294WQa7teCe3mK+YELHFWpl1t6QDFT3aBU8vrITh5fhGjl6esLIDrHnX 7GtcTT4pzx3elHxWmMHyZMJ5HV1iBSMMlA3VHsw8IzNjabeR2vHV4MbKuz7WmW3NfdZz TMtNlfaE3eGrMGG/AkMHCdShMOSxC5Z3JGv2sVPLKOso+GBmYjo14cNYYrIYJDfqy7Nt NAUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=SocJctGaI1z5Y2V/7vECrCscsCsQ4+Z36X400HrEw90=; b=kvR/NwziI8bVk8/K8YUg960blHS2itKvLP4k3StKHGmDqjCGhSbuA1/51mW6Ejg7A9 f7UtUIMlPrzfSPXLj+aocdgG/96kCWWoH26hxqRAEHcwzz63fW0ezkC9G+65CTKm035M qSAUpnLljK5sJFNMEh6k/5TKA88PpQ4+z9UxNYCT7bd2NbGN6ykmi2PCbO8Q8rRtUocb KP9zDFLwnNppILP+MtW3zg/325UL9Fw1TVu4HJx1outHqUbxIO5KjsOJiXWENYZ9Fc6/ L5SlUwM709Sx5nksrL5SkQyNK/3SNYxa5ax87tODG05neMysHnPw2U9G1DObr/sSBNQt 3naA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=MCNby1Xi; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d17si3755165eds.604.2021.08.12.13.54.45; Thu, 12 Aug 2021 13:55:08 -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=@google.com header.s=20161025 header.b=MCNby1Xi; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237930AbhHLUnI (ORCPT + 99 others); Thu, 12 Aug 2021 16:43:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238133AbhHLUnB (ORCPT ); Thu, 12 Aug 2021 16:43:01 -0400 Received: from mail-yb1-xb34.google.com (mail-yb1-xb34.google.com [IPv6:2607:f8b0:4864:20::b34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01D1FC06124A for ; Thu, 12 Aug 2021 13:42:36 -0700 (PDT) Received: by mail-yb1-xb34.google.com with SMTP id c5so1300582ybn.5 for ; Thu, 12 Aug 2021 13:42:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=SocJctGaI1z5Y2V/7vECrCscsCsQ4+Z36X400HrEw90=; b=MCNby1XiS34HtYanu4HEMdBBHb3yopMV+ncDfXZPodXtOcrBLr22b4Ls/BfzbM0orx TqWQKlFF8n3hldLltBeAiIuRiSueXGuR/x6wcKtQeIgY/OG+ScEbKhwPxWoI0qGWrurb kiGMVfasnwjaeCnwtMBVGUFibsTbivcP5DJBWTQD6rwAe5AGcxzA6Mnkl/CvWSac4XRb YiwMTW85eHqLcg+YmYX0CvbbhkOF6naVhEq8CtV16QYgDMxcjI4jZ08tesEszudmUeNC 1yFv4FlBVwzeYuw+XFWEQjr8VdpUhfofcBPc7LX8p0I64vx950IJ6NgchwjIyMzAg1fE 3Hxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=SocJctGaI1z5Y2V/7vECrCscsCsQ4+Z36X400HrEw90=; b=AUN6kzIz8XJeGbbCciGrim8tCkIEwT3lLqE4Sx3Ho/Lki+u2ckgHy5NaLcA1AsweiW 49WB3tq2dntMIjwo+ars+L/yvYi+jxRoGnGc3CwhyUXcWiIDNqNcG9BJKCEWTS6IfUnJ fyP9TM/h5kh7am5WiGiQwWoB18vDLAO+auPLrNEV0qpSW5DqguCPe5r6gg6XPAZ53r3j 13DbKb1z33kXy8PkkmWJuHEdRDah1pc6q8ih0/0rPFPH32uExPSXJNP18c53b4uM/ZyY xVc8NsQGIOtfjt9bXF/yLFM7+oHoOLdtgUpPTOstGxPb5HicTu8yGvFBuNl2ETilHpbW 5/iA== X-Gm-Message-State: AOAM533lOyrtFxO2z29Qxyrrn2wm9N7hORfYJc7hpamdrytx5SEYYg2J QUNbeFbRuQRKUHELAOj44K86wPGg5g1bFETTZzhn1Q== X-Received: by 2002:a25:bec2:: with SMTP id k2mr7239053ybm.234.1628800954923; Thu, 12 Aug 2021 13:42:34 -0700 (PDT) MIME-Version: 1.0 References: <20210812203137.2880834-1-joshdon@google.com> In-Reply-To: <20210812203137.2880834-1-joshdon@google.com> From: Eric Dumazet Date: Thu, 12 Aug 2021 22:42:23 +0200 Message-ID: Subject: Re: [PATCH] fs/proc/uptime.c: fix idle time reporting in /proc/uptime To: Josh Don Cc: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , LKML , linux-fsdevel , Luigi Rizzo Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 12, 2021 at 10:31 PM Josh Don wrote: > > /proc/uptime reports idle time by reading the CPUTIME_IDLE field from > the per-cpu kcpustats. However, on NO_HZ systems, idle time is not > continually updated on idle cpus, leading this value to appear > incorrectly small. > > /proc/stat performs an accounting update when reading idle time; we can > use the same approach for uptime. > > With this patch, /proc/stat and /proc/uptime now agree on idle time. > Additionally, the following shows idle time tick up consistently on an > idle machine: > (while true; do cat /proc/uptime; sleep 1; done) | awk '{print $2-prev; prev=$2}' > > Reported-by: Luigi Rizzo > Signed-off-by: Josh Don > --- > fs/proc/stat.c | 26 -------------------------- > fs/proc/uptime.c | 13 ++++++++----- > include/linux/kernel_stat.h | 1 + > kernel/sched/cputime.c | 28 ++++++++++++++++++++++++++++ > 4 files changed, 37 insertions(+), 31 deletions(-) > > diff --git a/fs/proc/stat.c b/fs/proc/stat.c > index 6561a06ef905..99796a8a5223 100644 > --- a/fs/proc/stat.c > +++ b/fs/proc/stat.c > @@ -24,16 +24,6 @@ > > #ifdef arch_idle_time > > -static u64 get_idle_time(struct kernel_cpustat *kcs, int cpu) > -{ > - u64 idle; > - > - idle = kcs->cpustat[CPUTIME_IDLE]; > - if (cpu_online(cpu) && !nr_iowait_cpu(cpu)) > - idle += arch_idle_time(cpu); > - return idle; > -} > - > static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu) > { > u64 iowait; > @@ -46,22 +36,6 @@ static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu) > > #else > > -static u64 get_idle_time(struct kernel_cpustat *kcs, int cpu) > -{ > - u64 idle, idle_usecs = -1ULL; > - > - if (cpu_online(cpu)) > - idle_usecs = get_cpu_idle_time_us(cpu, NULL); > - > - if (idle_usecs == -1ULL) > - /* !NO_HZ or cpu offline so we can rely on cpustat.idle */ > - idle = kcs->cpustat[CPUTIME_IDLE]; > - else > - idle = idle_usecs * NSEC_PER_USEC; > - > - return idle; > -} > - > static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu) > { > u64 iowait, iowait_usecs = -1ULL; ... > diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c > index 872e481d5098..9d7629e21164 100644 > --- a/kernel/sched/cputime.c > +++ b/kernel/sched/cputime.c > @@ -227,6 +227,34 @@ void account_idle_time(u64 cputime) > cpustat[CPUTIME_IDLE] += cputime; > } > > +/* > + * Returns the total idle time for the given cpu. > + * @kcs: The kernel_cpustat for the desired cpu. > + * @cpu: The desired cpu. > + */ > +u64 get_idle_time(const struct kernel_cpustat *kcs, int cpu) > +{ > + u64 idle; > + u64 __maybe_unused idle_usecs = -1ULL; > + > +#ifdef arch_idle_time > + idle = kcs->cpustat[CPUTIME_IDLE]; > + if (cpu_online(cpu) && !nr_iowait_cpu(cpu)) > + idle += arch_idle_time(cpu); > +#else > + if (cpu_online(cpu)) > + idle_usecs = get_cpu_idle_time_us(cpu, NULL); > + > + if (idle_usecs == -1ULL) > + /* !NO_HZ or cpu offline so we can rely on cpustat.idle */ > + idle = kcs->cpustat[CPUTIME_IDLE]; > + else > + idle = idle_usecs * NSEC_PER_USEC; > +#endif > + > + return idle; > +} > + > Not sure why you moved get_idle_time() in kernel/sched/cputime.c For builds where CONFIG_PROC_FS is not set, this function is not used/needed.