Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp2673837ybc; Wed, 20 Nov 2019 18:46:59 -0800 (PST) X-Google-Smtp-Source: APXvYqwgFiCmt7a/VFk53IQVeTg1U57tlnXovuMnQIUKWpdo2akI6YFcGEDvgVpmSo6J/Zi4Gm/a X-Received: by 2002:a17:906:5e52:: with SMTP id b18mr9981960eju.318.1574304419378; Wed, 20 Nov 2019 18:46:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574304419; cv=none; d=google.com; s=arc-20160816; b=C28N/d06wVAWJQ0y2SlWFQWErfWY/y4FnhoIerHA3wvvhhRHbXJ2QpXyvlK26o+Khj utkNAzZBxIhE+H8s6aHQ3wrQM5ifYvkaLj7H6+EvYeo7u5GgTqsa7USZp/pcJ5v6GaG4 fHHRTqdTQ5B8u02WqYkfbvNQF/jNINc6A20P8N1bUeuwQKNpugsT8f68w19Cu0yNcI02 AOZZ2YNzXVGGzV0uARboAuX9kMd9k1vXMAQ9BhbQTlc0UIzJEj9kJvlFYF+t0Tufr7AC kyiRJuDf79FMmV9jmpDyMDbLksiauyE2hsiuw3QoJ9UYUX+PAcIMxu9ZpNmq935GYzat xSmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PRBzDqIhrx8vWDiCs8LNVNOftVHg+D2x7QuC5Bl+2nU=; b=Vu4PeLjq7lQWe6EPPwjd7sycteKjFSxAM5rphlfFGatSIG97RqYtvmLagwWuQwmQV5 2bthZfSKUx56C6kIYJjUW9culbjQMpqgdeG1hlDMuH0v4nqD2SeslP4eb8XDyNnXbyXi mndSD+uPlYU02n9RkYJYbGp0NmCXcxWQQFj+ctIhKXVu9P1Yn7BBXm5TtpHMTyX4bxpC WXradt+/4MlvkgDu6l3fGf7DekFgOyDvcPFoN8J3k35xi13XcH2n/2m7Xgx3Aek+uMZN 7cSL5ZucAMeqzyQfbUwpjss8SC1SKwQswt2jit79piXxPE6La1FuD2j0MeSQ2nTaS8Yk /SWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=LeTQpluY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y14si1042160edu.250.2019.11.20.18.46.36; Wed, 20 Nov 2019 18:46:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=LeTQpluY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726623AbfKUCol (ORCPT + 99 others); Wed, 20 Nov 2019 21:44:41 -0500 Received: from mail.kernel.org ([198.145.29.99]:54206 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725904AbfKUCok (ORCPT ); Wed, 20 Nov 2019 21:44:40 -0500 Received: from lenoir.home (lfbn-ncy-1-150-155.w83-194.abo.wanadoo.fr [83.194.232.155]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C760720878; Thu, 21 Nov 2019 02:44:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574304279; bh=wAA4khObfinmYR2XRJKV1R0nOvJgJ3iSw0Lt15fbVTQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LeTQpluY1PZqn1mWGTI9iholhQB3uTlZmvQyY1kqfHaChL6E95k2z3h3VfS8EroH0 YTTVAm6MZRKEKv/D8Xf1ivt5vCkgIM1wi30lpx85rLob/ZC/Dtcu5FMcLEA/0frG9L rpvy3+Z/pq8L7T1VEpi52YMWn8uYNCqfxqfkKD0o= From: Frederic Weisbecker To: Peter Zijlstra , Ingo Molnar Cc: LKML , Frederic Weisbecker , Jacek Anaszewski , Wanpeng Li , "Rafael J . Wysocki" , Benjamin Herrenschmidt , Rik van Riel , Thomas Gleixner , Yauheni Kaliuta , Viresh Kumar , Pavel Machek Subject: [PATCH 1/6] sched/cputime: Support other fields on kcpustat_field() Date: Thu, 21 Nov 2019 03:44:25 +0100 Message-Id: <20191121024430.19938-2-frederic@kernel.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191121024430.19938-1-frederic@kernel.org> References: <20191121024430.19938-1-frederic@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Provide support for user, nice, guest and guest_nice fields through kcpustat_field(). Whether we account the delta to a nice or not nice field is decided on top of the nice value snapshot taken at the time we call kcpustat_field(). If the nice value of the task has been changed since the last vtime update, we may have inacurrate distribution of the nice VS unnice cputime. However this is considered as a minor issue compared to the proper fix that would involve interrupting the target on nice updates, which is undesired on nohz_full CPUs. Signed-off-by: Frederic Weisbecker Cc: Yauheni Kaliuta Cc: Thomas Gleixner Cc: Rik van Riel Cc: Peter Zijlstra Cc: Wanpeng Li Cc: Ingo Molnar --- kernel/sched/cputime.c | 54 +++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index e0cd20693ef5..27b5406222fc 100644 --- a/kernel/sched/cputime.c +++ b/kernel/sched/cputime.c @@ -912,11 +912,21 @@ void task_cputime(struct task_struct *t, u64 *utime, u64 *stime) } while (read_seqcount_retry(&vtime->seqcount, seq)); } +static u64 kcpustat_user_vtime(struct vtime *vtime) +{ + if (vtime->state == VTIME_USER) + return vtime->utime + vtime_delta(vtime); + else if (vtime->state == VTIME_GUEST) + return vtime->gtime + vtime_delta(vtime); + return 0; +} + static int kcpustat_field_vtime(u64 *cpustat, - struct vtime *vtime, + struct task_struct *tsk, enum cpu_usage_stat usage, int cpu, u64 *val) { + struct vtime *vtime = &tsk->vtime; unsigned int seq; int err; @@ -946,9 +956,37 @@ static int kcpustat_field_vtime(u64 *cpustat, *val = cpustat[usage]; - if (vtime->state == VTIME_SYS) - *val += vtime->stime + vtime_delta(vtime); - + /* + * Nice VS unnice cputime accounting may be inaccurate if + * the nice value has changed since the last vtime update. + * But proper fix would involve interrupting target on nice + * updates which is a no go on nohz_full (although the scheduler + * may still interrupt the target if rescheduling is needed...) + */ + switch (usage) { + case CPUTIME_SYSTEM: + if (vtime->state == VTIME_SYS) + *val += vtime->stime + vtime_delta(vtime); + break; + case CPUTIME_USER: + if (task_nice(tsk) <= 0) + *val += kcpustat_user_vtime(vtime); + break; + case CPUTIME_NICE: + if (task_nice(tsk) > 0) + *val += kcpustat_user_vtime(vtime); + break; + case CPUTIME_GUEST: + if (vtime->state == VTIME_GUEST && task_nice(tsk) <= 0) + *val += vtime->gtime + vtime_delta(vtime); + break; + case CPUTIME_GUEST_NICE: + if (vtime->state == VTIME_GUEST && task_nice(tsk) > 0) + *val += vtime->gtime + vtime_delta(vtime); + break; + default: + break; + } } while (read_seqcount_retry(&vtime->seqcount, seq)); return 0; @@ -965,15 +1003,10 @@ u64 kcpustat_field(struct kernel_cpustat *kcpustat, if (!vtime_accounting_enabled_cpu(cpu)) return cpustat[usage]; - /* Only support sys vtime for now */ - if (usage != CPUTIME_SYSTEM) - return cpustat[usage]; - rq = cpu_rq(cpu); for (;;) { struct task_struct *curr; - struct vtime *vtime; rcu_read_lock(); curr = rcu_dereference(rq->curr); @@ -982,8 +1015,7 @@ u64 kcpustat_field(struct kernel_cpustat *kcpustat, return cpustat[usage]; } - vtime = &curr->vtime; - err = kcpustat_field_vtime(cpustat, vtime, usage, cpu, &val); + err = kcpustat_field_vtime(cpustat, curr, usage, cpu, &val); rcu_read_unlock(); if (!err) -- 2.23.0