linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH v3 1/2] power/cpuidle: enhance the precision of state select
@ 2016-06-27  7:42 Zhaoyang Huang
  2016-06-27  7:42 ` [RFC PATCH v3 2/2] power/idle: enhance the precision of sleep_length Zhaoyang Huang
  0 siblings, 1 reply; 2+ messages in thread
From: Zhaoyang Huang @ 2016-06-27  7:42 UTC (permalink / raw)
  To: linux-kernel, linux-pm, mingo, peterz, zhaoyang.huang,
	private-pmwg, tglx

In previous version, cpu_pm_enter is invoked after the governor
select the state, which cause the executing time of cpu_pm_enter
is included in the idle time. Moving it before the state selection.

Please refer to bellowing chart for detailed information

current approach:
static void cpu_idle_loop(void)
{
	while (1) {
		tick_nohz_idle_enter();
		--->__tick_nohz_idle_enter
		--->tick_nohz_stop_sched_tick
		    {
		    ...
		    	ts->sleep_length = ktime_sub(dev->next_event, now);          /*1*/
		    ...
		    }

		while (!need_resched()) {
			...
			local_irq_disable();
			arch_cpu_idle_enter();
			--->idle_notifier_call_chain(IDLE_START);                    /*2*/

			cpuidle_idle_call();
			--->next_state = cpuidle_select(drv, dev);                   /*3*/
			--->static int arm_enter_idle_state(...)
			{
			...
				ret = cpu_pm_enter();                                /*4*/
			}
}

---------|----------------------|----------------------------|------------------------------------|--------------------------------------------|--------------------
       1.now              2.IDLE_START             3.select idle state                     4.CPU_PM_ENTER                                 next_event
   (sleep_length)

modified approach:
static void cpu_idle_loop(void)
{
	while (1) {
		tick_nohz_idle_enter();
		--->__tick_nohz_idle_enter
		--->tick_nohz_stop_sched_tick
		    {
		    ...

		    ...
		    }

		while (!need_resched()) {
			...
			local_irq_disable();
			arch_cpu_idle_enter();
			--->idle_notifier_call_chain(IDLE_START);                            /*1*/

			cpuidle_idle_call();
			--->ret = cpu_pm_enter();                                            /*2*/
			--->tick_nohz_get_sleep_length
			{
				ts->sleep_length = ktime_sub(dev->next_event, now);          /*3*/
			}
			--->next_state = cpuidle_select(drv, dev);                           /*4*/
			--->static int arm_enter_idle_state(...)
}

----------------|---------------------------------------|---------------------|------------------|--------------------------------------------|--------------------
          1.IDLE_START                           2.CPU_PM_ENTER            3.now          4.select idle state                            next_event
                                                                        (sleep_length)
Signed-off-by: Zhaoyang Huang <zhaoyang.huang@spreadtrum.com>
---
 kernel/sched/idle.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c
index bd12c6c..929da2e 100644
--- a/kernel/sched/idle.c
+++ b/kernel/sched/idle.c
@@ -5,6 +5,7 @@
 #include <linux/cpu.h>
 #include <linux/cpuidle.h>
 #include <linux/cpuhotplug.h>
+#include <linux/cpu_pm.h>
 #include <linux/tick.h>
 #include <linux/mm.h>
 #include <linux/stackprotector.h>
@@ -130,6 +131,7 @@ static void cpuidle_idle_call(void)
 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
 	int next_state, entered_state;
+	int ret;
 
 	/*
 	 * Check if the idle task must be rescheduled. If it is the
@@ -174,12 +176,16 @@ static void cpuidle_idle_call(void)
 		/*
 		 * Ask the cpuidle framework to choose a convenient idle state.
 		 */
-		next_state = cpuidle_select(drv, dev);
-		entered_state = call_cpuidle(drv, dev, next_state);
-		/*
-		 * Give the governor an opportunity to reflect on the outcome
-		 */
-		cpuidle_reflect(dev, entered_state);
+		ret = cpu_pm_enter();
+		if (!ret) {
+			next_state = cpuidle_select(drv, dev);
+			entered_state = call_cpuidle(drv, dev, next_state);
+			cpu_pm_exit();
+			/*
+			 * Give the governor an opportunity to reflect on the outcome
+			 */
+			cpuidle_reflect(dev, entered_state);
+		}
 	}
 
 exit_idle:
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [RFC PATCH v3 2/2] power/idle: enhance the precision of sleep_length
  2016-06-27  7:42 [RFC PATCH v3 1/2] power/cpuidle: enhance the precision of state select Zhaoyang Huang
@ 2016-06-27  7:42 ` Zhaoyang Huang
  0 siblings, 0 replies; 2+ messages in thread
From: Zhaoyang Huang @ 2016-06-27  7:42 UTC (permalink / raw)
  To: linux-kernel, linux-pm, mingo, peterz, zhaoyang.huang,
	private-pmwg, tglx

As shown in bellowing chart, there should be a gap between
tick_nohz_idle_enter(step '1') and tick_nohz_get_sleep_length
(step '3') when idle, which comprise of the execution time of
the code behind step '1' and a series of notify call(step2,4).
Yes, for most of the scenarios, it would NOT be a problem.
However,all time consumed by the steps above will cause the
sleep_length is not very precised in the scenario where the
idle state is shallow(imagin an idle state last less than 500us
 under a 200Mhz's P-state). Change it in this series of patch.

Please refer to bellowing chart for detailed information

    current approach:
    static void cpu_idle_loop(void)
    {
        while (1) {
                tick_nohz_idle_enter();
                --->__tick_nohz_idle_enter
                --->tick_nohz_stop_sched_tick
                    {
                    ...
                        ts->sleep_length = ktime_sub(dev->next_event, now);          /*1*/
                    ...
                    }

                while (!need_resched()) {
                        ...
                        local_irq_disable();
                        arch_cpu_idle_enter();
                        --->idle_notifier_call_chain(IDLE_START);                    /*2*/

	                cpuidle_idle_call();
        	        --->next_state = cpuidle_select(drv, dev);                   /*3*/
                	--->static int arm_enter_idle_state(...)
                        {
                        ...
                                ret = cpu_pm_enter();                                /*4*/
                        }
    }

    ---------|----------------------|----------------------------|------------------------------------|--------------------------------------------|--------------------
           1.now              2.IDLE_START             3.select idle state                     4.CPU_PM_ENTER                                 next_event
       (sleep_length)

    modified approach:
    static void cpu_idle_loop(void)
    {
        while (1) {
                tick_nohz_idle_enter();
                --->__tick_nohz_idle_enter
                --->tick_nohz_stop_sched_tick
                    {
                    ...

                    ...
                    }

                while (!need_resched()) {
                        ...
                        local_irq_disable();
                        arch_cpu_idle_enter();
                        --->idle_notifier_call_chain(IDLE_START);                            /*1*/

	                cpuidle_idle_call();
        	        --->ret = cpu_pm_enter();                                            /*2*/
                	--->tick_nohz_get_sleep_length
                        {
                                ts->sleep_length = ktime_sub(dev->next_event, now);          /*3*/
                        }
	                --->next_state = cpuidle_select(drv, dev);                           /*4*/
        	        --->static int arm_enter_idle_state(...)
    }

    ----------------|---------------------------------------|---------------------|------------------|--------------------------------------------|--------------------
              1.IDLE_START                           2.CPU_PM_ENTER            3.now          4.select idle state                            next_event
                                                                            (sleep_length)
Signed-off-by: Zhaoyang Huang <zhaoyang.huang@spreadtrum.com>
---
 kernel/time/tick-sched.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index 536ada8..ee3be3d 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -975,6 +975,11 @@ void tick_nohz_irq_exit(void)
 ktime_t tick_nohz_get_sleep_length(void)
 {
 	struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
+	struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
+	ktime_t now;
+
+	now = ktime_get();
+	ts->sleep_length = ktime_sub(dev->next_event, now);
 
 	return ts->sleep_length;
 }
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-06-27  7:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-27  7:42 [RFC PATCH v3 1/2] power/cpuidle: enhance the precision of state select Zhaoyang Huang
2016-06-27  7:42 ` [RFC PATCH v3 2/2] power/idle: enhance the precision of sleep_length Zhaoyang Huang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).