From patchwork Fri May 15 00:12:50 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ruchi Kandoi X-Patchwork-Id: 6410571 Return-Path: X-Original-To: patchwork-linux-pm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 3D219C0432 for ; Fri, 15 May 2015 00:13:36 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 37832204AD for ; Fri, 15 May 2015 00:13:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0790B20499 for ; Fri, 15 May 2015 00:13:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1423065AbbEOANQ (ORCPT ); Thu, 14 May 2015 20:13:16 -0400 Received: from mail-ie0-f177.google.com ([209.85.223.177]:34519 "EHLO mail-ie0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1423270AbbEOANL (ORCPT ); Thu, 14 May 2015 20:13:11 -0400 Received: by ieczm2 with SMTP id zm2so4357239iec.1 for ; Thu, 14 May 2015 17:13:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:date:message-id:in-reply-to:references; bh=+EHD5brWZyhiFZzs1kqVlN51jZxMpAZTMrXsczQpcqk=; b=PmG0cPoCmvtx2Hpp1gdRvqxCv6JUHlLi5/LqefOKr5luI3sme0g04/ByS+IbYarf7g CHtvIOtk+kZFr2SjVNhmEhhhha79U3MGCI3xKy2FnYZA+4svOK/DL56+lzMnRQ0q1k4Y JaQl6WyIGLgBRAiyUK1G0NHo9IqV0hIob6JVKLnOYi6nU0ceKQqG5HpgxZlZxNuX3FVf aoKR/rMFH5pUUNQB5w+xEDYqQQTmdPONeOzCiXSYPB3CwcTVQPCdZG0C07uK8m78AcS8 Bg5L/4DsngWJrdXtCSeIRcIO7hOyXerPwlicgDGoTDjneOEOvrVWD9wdMK7ImqmGVuzj jNNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=+EHD5brWZyhiFZzs1kqVlN51jZxMpAZTMrXsczQpcqk=; b=MiJSu1Hbsetn/S4SDFHoSDf4GjAtBWE170AmZfX8/MaBVLn7dpWuPINpFaml0L5rA8 CGST4oBhYjIIGPCyaVrgCaytybF109WxxVP2V66pFHV572lwu9JGkdIQ5M4piWKdU/XX OjxwFWvNC8HrYG23g8mGyMpoOG2LG8hkRBSx9cpDc2RBaK35T9FFsXwUUWLZOTx92Qja pletpbWgZjJq3zqu9JNYTT0io53/AVlPw7tKxyNKzdFLZKKftILUTLYtXqC+qLlsHd2J fZrfNUadOmF6o9JRiZHl1EaCX0swf+7wBOrr4jbLSsCzvZqvRmx85vYikGY8Y1NwTw2u RKMQ== X-Gm-Message-State: ALoCoQmykT/y32TxmlQ0ukFl12+pNmcf61PwEBG8o4rL5ladj+WOB+GhwI1YQ0f9T+PDokHXYcqN X-Received: by 10.107.8.87 with SMTP id 84mr5329813ioi.87.1431648790817; Thu, 14 May 2015 17:13:10 -0700 (PDT) Received: from kandoiruchi.mtv.corp.google.com ([172.22.121.97]) by mx.google.com with ESMTPSA id qt1sm211235igb.5.2015.05.14.17.13.09 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 14 May 2015 17:13:10 -0700 (PDT) From: Ruchi Kandoi To: kandoiruchi@google.com, "Rafael J. Wysocki" , Viresh Kumar , Ingo Molnar , Peter Zijlstra , Andrew Morton , Oleg Nesterov , "Kirill A. Shutemov" , Vladimir Davydov , Heinrich Schuchardt , Thomas Gleixner , Kees Cook , Konstantin Khlebnikov , Davidlohr Bueso , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] sched: cpufreq: Adds a field cpu_power in the task_struct Date: Thu, 14 May 2015 17:12:50 -0700 Message-Id: <1431648770-7404-3-git-send-email-kandoiruchi@google.com> X-Mailer: git-send-email 2.2.0.rc0.207.ga3a616c In-Reply-To: <1431648770-7404-1-git-send-email-kandoiruchi@google.com> References: <1431648770-7404-1-git-send-email-kandoiruchi@google.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,RCVD_IN_DNSWL_HI,T_DKIM_INVALID,T_RP_MATCHES_RCVD, 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 cpu_power has been added to keep track of amount of power each task is consuming. cpu_power is updated whenever stime and utime are updated for a task. power is computed by taking into account the frequency at which the current core was running and the current for cpu actively running at hat frequency. Signed-off-by: Ruchi Kandoi --- drivers/cpufreq/cpufreq_stats.c | 28 ++++++++++++++++++++++++++++ include/linux/cpufreq.h | 8 ++++++++ include/linux/sched.h | 2 ++ kernel/fork.c | 1 + kernel/sched/cputime.c | 7 +++++++ 5 files changed, 46 insertions(+) diff --git a/drivers/cpufreq/cpufreq_stats.c b/drivers/cpufreq/cpufreq_stats.c index 6f0b562..682ed898 100644 --- a/drivers/cpufreq/cpufreq_stats.c +++ b/drivers/cpufreq/cpufreq_stats.c @@ -14,6 +14,7 @@ #include #include #include +#include static spinlock_t cpufreq_stats_lock; @@ -83,6 +84,33 @@ static void store_current_value(struct cpufreq_power_stats *powerstats, } } +void acct_update_power(struct task_struct *task, cputime_t cputime) +{ + struct cpufreq_power_stats *powerstats; + struct cpufreq_stats *stats; + struct cpufreq_policy *policy; + unsigned int cpu_num, curr; + + if (!task) + return; + cpu_num = task_cpu(task); + powerstats = per_cpu(cpufreq_power_stats, cpu_num); + policy = cpufreq_cpu_get(cpu_num); + if (!policy) + return; + + if (!powerstats || !(policy->stats)) { + cpufreq_cpu_put(policy); + return; + } + + stats = policy->stats; + curr = powerstats->curr[stats->last_index]; + task->cpu_power += curr * cputime_to_usecs(cputime); + cpufreq_cpu_put(policy); +} +EXPORT_SYMBOL_GPL(acct_update_power); + static ssize_t store_current_in_state(struct cpufreq_policy *policy, const char *buf, size_t len) { diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index 2ee4888..86826c8 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -18,6 +18,7 @@ #include #include #include +#include /********************************************************************* * CPUFREQ INTERFACE * @@ -601,4 +602,11 @@ unsigned int cpufreq_generic_get(unsigned int cpu); int cpufreq_generic_init(struct cpufreq_policy *policy, struct cpufreq_frequency_table *table, unsigned int transition_latency); + +/********************************************************************* + * CPUFREQ STATS * + *********************************************************************/ + +void acct_update_power(struct task_struct *p, cputime_t cputime); + #endif /* _LINUX_CPUFREQ_H */ diff --git a/include/linux/sched.h b/include/linux/sched.h index 26a2e61..1f2400a 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1429,6 +1429,7 @@ struct task_struct { int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */ cputime_t utime, stime, utimescaled, stimescaled; + unsigned long long cpu_power; cputime_t gtime; #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE struct cputime prev_cputime; @@ -1441,6 +1442,7 @@ struct task_struct { VTIME_USER, VTIME_SYS, } vtime_snap_whence; + #endif unsigned long nvcsw, nivcsw; /* context switch counts */ u64 start_time; /* monotonic time in nsec */ diff --git a/kernel/fork.c b/kernel/fork.c index 03c1eaa..2ca0e9e 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1341,6 +1341,7 @@ static struct task_struct *copy_process(unsigned long clone_flags, p->utime = p->stime = p->gtime = 0; p->utimescaled = p->stimescaled = 0; + p->cpu_power = 0; #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE p->prev_cputime.utime = p->prev_cputime.stime = 0; #endif diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index 8394b1e..53a79d5 100644 --- a/kernel/sched/cputime.c +++ b/kernel/sched/cputime.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "sched.h" @@ -149,6 +150,9 @@ void account_user_time(struct task_struct *p, cputime_t cputime, /* Account for user time used */ acct_account_cputime(p); + + /* Account power usage for user time */ + acct_update_power(p, cputime); } /* @@ -199,6 +203,9 @@ void __account_system_time(struct task_struct *p, cputime_t cputime, /* Account for system time used */ acct_account_cputime(p); + + /* Account power usage for system time */ + acct_update_power(p, cputime); } /*