From patchwork Mon Mar 18 15:23:23 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 15401 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 68AFD23E3E for ; Mon, 18 Mar 2013 15:24:12 +0000 (UTC) Received: from mail-ve0-f175.google.com (mail-ve0-f175.google.com [209.85.128.175]) by fiordland.canonical.com (Postfix) with ESMTP id 111ABA187B7 for ; Mon, 18 Mar 2013 15:24:11 +0000 (UTC) Received: by mail-ve0-f175.google.com with SMTP id cy12so4478894veb.34 for ; Mon, 18 Mar 2013 08:24:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:x-forwarded-to:x-forwarded-for:delivered-to:x-received :received-spf:x-received:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references:in-reply-to:references:x-gm-message-state; bh=Tjcf2Fb2EJPgTwgAosQ5mavowyDHZMGZw25pRyAT+28=; b=ivHTvh3YITCXTc4TrxxPjus0UWkIiwJf/h0SVaH2vFpSzjJNJ99Snvl+KqfaGmNqux 4ISKEntin5IT5LIGnNtJJWvQgaygC2rzhcoJokEDBwUOf4F2Sb3XeFx9jNypaQR5/6cH GfrcrJzizddjF7M6UXS6jDMt9dZKeaaA3IfVS34LzwDHQ9heO8wwdVcTRP26OhOW1QHG mI4BMye/8EyaMYHyJ5musH3lOCWgObWqZXCqeUZb1UzyV5Cav6keIJzYlXmTD9IAiYrx 4JWqjZ+EfnY7loWQ605V0N54SgQpArooqqHbrLdNEn/GfkxMnt4kfUu5iifd/ueRTVb/ IuZw== X-Received: by 10.221.11.135 with SMTP id pe7mr20176914vcb.41.1363620251481; Mon, 18 Mar 2013 08:24:11 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.58.127.98 with SMTP id nf2csp28973veb; Mon, 18 Mar 2013 08:24:10 -0700 (PDT) X-Received: by 10.68.217.2 with SMTP id ou2mr34231363pbc.6.1363620249805; Mon, 18 Mar 2013 08:24:09 -0700 (PDT) Received: from mail-da0-x233.google.com (mail-da0-x233.google.com [2607:f8b0:400e:c00::233]) by mx.google.com with ESMTPS id xm3si21493111pbc.136.2013.03.18.08.24.09 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 18 Mar 2013 08:24:09 -0700 (PDT) Received-SPF: neutral (google.com: 2607:f8b0:400e:c00::233 is neither permitted nor denied by best guess record for domain of viresh.kumar@linaro.org) client-ip=2607:f8b0:400e:c00::233; Authentication-Results: mx.google.com; spf=neutral (google.com: 2607:f8b0:400e:c00::233 is neither permitted nor denied by best guess record for domain of viresh.kumar@linaro.org) smtp.mail=viresh.kumar@linaro.org Received: by mail-da0-f51.google.com with SMTP id g27so561198dan.24 for ; Mon, 18 Mar 2013 08:24:09 -0700 (PDT) X-Received: by 10.68.225.40 with SMTP id rh8mr34120147pbc.137.1363620249200; Mon, 18 Mar 2013 08:24:09 -0700 (PDT) Received: from localhost ([122.167.69.232]) by mx.google.com with ESMTPS id ip8sm20515334pbc.39.2013.03.18.08.24.02 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Mon, 18 Mar 2013 08:24:08 -0700 (PDT) From: Viresh Kumar To: pjt@google.com, paul.mckenney@linaro.org, tglx@linutronix.de, tj@kernel.org, suresh.b.siddha@intel.com, venki@google.com, mingo@redhat.com, peterz@infradead.org, rostedt@goodmis.org Cc: linaro-kernel@lists.linaro.org, robin.randhawa@arm.com, Steve.Bannister@arm.com, Liviu.Dudau@arm.com, charles.garcia-tobin@arm.com, Arvind.Chauhan@arm.com, linux-rt-users@vger.kernel.org, linux-kernel@vger.kernel.org, Viresh Kumar Subject: [PATCH V3 1/7] sched: Create sched_select_cpu() to give preferred CPU for power saving Date: Mon, 18 Mar 2013 20:53:23 +0530 Message-Id: <9f0fd16f792b9f9b399a4c91c98de37c56067d47.1363617402.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 1.7.12.rc2.18.g61b472e In-Reply-To: References: In-Reply-To: References: X-Gm-Message-State: ALoCoQnSIKcHhL1H/IbycBFYBI5AFeAXAvjziddeSpsbwxV5uE4M7u1FEPyC5mAuiJmhNp2ix+2L In order to save power, it would be useful to schedule light weight work on cpus that aren't IDLE instead of waking up an IDLE one. By idle cpu (from scheduler's perspective) we mean: - Current task is idle task - nr_running == 0 - wake_list is empty This is already implemented for timers as get_nohz_timer_target(). We can figure out few more users of this feature, like workqueues. This patch converts get_nohz_timer_target() into a generic API sched_select_cpu() so that other frameworks (like workqueue) can also use it. This routine returns the cpu which is non-idle. It accepts a bitwise OR of SD_* flags present in linux/sched.h. If the local CPU isn't idle OR all cpus are idle, local cpu is returned back. If local cpu is idle, then we must look for another CPU which have all the flags passed as argument as set and isn't idle. This patch reuses the code from get_nohz_timer_target() routine, which had similar implementation. Signed-off-by: Viresh Kumar --- include/linux/sched.h | 21 +++++++++++++++-- kernel/sched/core.c | 63 +++++++++++++++++++++++++++++---------------------- 2 files changed, 55 insertions(+), 29 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index e20580d..216fa0d 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -230,14 +230,31 @@ extern void init_idle_bootup_task(struct task_struct *idle); extern int runqueue_is_locked(int cpu); -#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ) +#ifdef CONFIG_SMP +extern int sched_select_cpu(unsigned int sd_flags); + +#ifdef CONFIG_NO_HZ extern void nohz_balance_enter_idle(int cpu); extern void set_cpu_sd_state_idle(void); -extern int get_nohz_timer_target(void); +/* + * In the semi idle case, use the nearest busy cpu for migrating timers + * from an idle cpu. This is good for power-savings. + * + * We don't do similar optimization for completely idle system, as + * selecting an idle cpu will add more delays to the timers than intended + * (as that cpu's timer base may not be uptodate wrt jiffies etc). + */ +#define get_nohz_timer_target() sched_select_cpu(0) #else static inline void nohz_balance_enter_idle(int cpu) { } static inline void set_cpu_sd_state_idle(void) { } + +static inline int sched_select_cpu(unsigned int sd_flags) +{ + return raw_smp_processor_id(); +} #endif +#endif /* CONFIG_SMP */ /* * Only dump TASK_* tasks. (0 for all tasks) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index b36635e..ccd76d7 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -551,33 +551,6 @@ void resched_cpu(int cpu) #ifdef CONFIG_NO_HZ /* - * In the semi idle case, use the nearest busy cpu for migrating timers - * from an idle cpu. This is good for power-savings. - * - * We don't do similar optimization for completely idle system, as - * selecting an idle cpu will add more delays to the timers than intended - * (as that cpu's timer base may not be uptodate wrt jiffies etc). - */ -int get_nohz_timer_target(void) -{ - int cpu = smp_processor_id(); - int i; - struct sched_domain *sd; - - rcu_read_lock(); - for_each_domain(cpu, sd) { - for_each_cpu(i, sched_domain_span(sd)) { - if (!idle_cpu(i)) { - cpu = i; - goto unlock; - } - } - } -unlock: - rcu_read_unlock(); - return cpu; -} -/* * When add_timer_on() enqueues a timer into the timer wheel of an * idle CPU then this timer might expire before the next timer event * which is scheduled to wake up that CPU. In case of a completely @@ -648,6 +621,42 @@ void sched_avg_update(struct rq *rq) } } +/* + * This routine returns the nearest non-idle cpu. It accepts a bitwise OR of + * SD_* flags present in linux/sched.h. If the local CPU isn't idle, it is + * returned back. If it is idle, then we must look for another CPU which have + * all the flags passed as argument as set. + */ +int sched_select_cpu(unsigned int sd_flags) +{ + struct sched_domain *sd; + int cpu = smp_processor_id(); + int i; + + /* If Current cpu isn't idle, don't migrate anything */ + if (!idle_cpu(cpu)) + return cpu; + + rcu_read_lock(); + for_each_domain(cpu, sd) { + /* If sd doesnt' have sd_flags set skip sd. */ + if ((sd->flags & sd_flags) != sd_flags) + continue; + + for_each_cpu(i, sched_domain_span(sd)) { + if (i == cpu) + continue; + if (!idle_cpu(i)) { + cpu = i; + goto unlock; + } + } + } +unlock: + rcu_read_unlock(); + return cpu; +} + #else /* !CONFIG_SMP */ void resched_task(struct task_struct *p) {