Introducing XDA:DevCon – A Conference For Developers By Developers
XDA Developers Android and Mobile Development Forum
Forgot your password?
 
Post Reply+
Tip us?
 
Ezekeel
Old
(Last edited by Ezekeel; 8th November 2011 at 08:17 PM.)
#1  
Ezekeel's Avatar
Recognized Developer - OP
Thanks Meter 1656
Posts: 715
Join Date: Jun 2011
Default [MOD][KERNEL] Deep Idle

I recently had a discussion with someone regarding the benefits of screen-off profiles/daemons that put a limit on the CPU frequency while the screen is switched off for the sake of reducing the power consumption and thus prolonging the battery runtime. My argument was that putting a hard limit on the CPU frequency would rather have a detrimental effect of the battery runtime since this would hinder the CPU from racing-to-idle. I also argued that due to this race-to-idle effect, in general restricting the CPU frequency is not beneficial to the battery runtime and in contrast to intuition overclocking should reduce the average battery drain (as long as no disproportionate increase in voltage is necessary).

Since this is just theory and Morfic noted that his experience seems to indicate otherwise and that limiting the CPU frequency does actually decrease the battery drain, I put this theory to the test and logged the battery charge over 10h of FLAC playback in Airplane mode with the screen switched off.

As one can see, limiting the frequency to a maximum of 880MHz does indeed reduce the power consumption.

Thus it seems that race-to-idle does not properly work on this device. But why? The answer is that, while the technical manual showed that the hardware supports six different power states: NORMAL, IDLE, DEEP-IDLE, STOP, DEEP-STOP and SLEEP, a quick look into the source code revealed that (besides SLEEP) only the most rudimentary power saving state IDLE is actually implemented in the kernel. And for this IDLE state no parts of the hardware are powered down; the only thing that essentially happens is that the CPU clock is set to zero.

For the real power savings to kick in, one would have to power down parts of the hardware (at least the CPU) and that is exactly what the DEEP-IDLE state is supposed to do. Unfortunately this was not implemented. So, following the technical manual I tried to implement this DEEP-IDLE state on my own... and failed miserably. Well, not that miserably... at least I had an idea where the problem was. However I had no idea how to fix it since I did not know enough about the hardware details, had no experience working that close to the CPU and also the manual was far from instructive.

So I googled to maybe find someone smarter than me who had figured out a solution and found out that "some guys" already tried to implement this DEEP-IDLE state for the Galaxy S, however that did not work for some reason. I located the source code in some older version of sixstringsg's Glitch kernel, made some modification, put it into my reference kernel and everything is running fine for more than two days now.

I have repeated the above test runs on a kernel including the deep-idle state.

The battery drain is reduced to 17-18% compared to the 41%/46% (880MHz/1320MHz) obtained without deep-idle support. Thus the battery consumption is reduced by more than 55%. Also we see that with the kernel including deep-idle both 880MHz and 1320MHz yield almost the same average battery drain, showing that race-to-idle actually does work as long as the power saving features are properly supported. So, once these modifications are implemented in your favorite kernel, there should be no need for limiting the frequency anymore and you should be able to blast away without wasting a thought about the battery drain. Also, there is no reason anymore not to overclock your device, except of course the thermal limit of the device auto-incinerating in your hand.


Changes to the source code: http://www.pastie.org/2679956


BUGFIX:

1. While the screen is on, only the normal IDLE state is used.
2. Removed DEEP IDLE state with TOP=ON.
3. For the DEEP IDLE state with TOP=OFF, all external interrupt
wakeup sources except XEINT[30] (key input) are disabled and
all internal wakeup sources are disabled.

Bugfix: http://www.pastie.org/2686141


BUGFIX #2:

1. The reserved values of S5P_WAKEUP_MASK are not touched.
2. Only XEINT[22] = GPH2[6] = GPIO_nPOWER = GPIO_N_POWER and
and XEINT[29] = GPH3[5] = GPIO_OK_KEY are enabled as external
wakeup sources.
3. Only RTC TICK and I2S are enabled as wakeup sources.

Bugfix: http://www.pastie.org/2692199


BUGFIX #3:

For DEEP IDLE the same wakeup interrupt sources are used as for the
sleep state plus at least XEINT[22] = GPH2[6] = GPIO_nPOWER =
GPIO_N_POWER, XEINT[29] = GPH3[5] = GPIO_OK_KEY, RTC TICK and I2S.

Bugfix: http://www.pastie.org/2697909


BUGFIX #4:

Implemented a sysfs interface (/sys/class/misc/deepidle) for enabling/disabling the use of the DEEP IDLE state.

Bugfix: http://www.pastie.org/2709212


BUGFIX #5:

Fixed the BT problem and made sure the DEEP IDLE state is not used when the device is going to/waking from suspend (SLEEP).

Bugfix: http://www.pastie.org/2723702


BUGFIX #6:

1. Mimicking the procedure for the SLEEP state, when entering/returning
from the DEEP IDLE state, a notification is send to kernel modules
using 'pm_notifier_call_chain'. This will cause CPUfreq to statically
set the frequency to SLEEP_FREQ and prevent further frequency changes
until the system returns from DEEP IDLE.
2. The APLL lock is not modified.

Bugfix: http://www.pastie.org/2744527


BUGFIX #7:

Fixed GPS problems.

Bugfix: http://www.pastie.org/2775884


BUGFIX #8:

The DEEP IDLE with TOP=ON is used while GPS or BT are running.

Bugfix: http://www.pastie.org/2778855


BUGFIX #9:
Notifications using 'pm_notifier_call_chain' when entering/leaving DEEP IDLE have been removed.

Bugfix: http://www.pastie.org/2796786

Test kernel: http://www.multiupload.com/K8ZXS5HTLS
For each of the different idle states, the number of idle calls and the total amount of time spend in each state are saved. These stats can be shown using 'idle_stats' in the sysfs interface and resetted with 'reset_stats'.

Bugfix: http://www.pastie.org/2779930


BUGFIX #10:

1. Changed stats variables to 'unsigned long long' to defer overflow and keep the idle times in us for increased accuracy.
2. In case of an overflow of one of the stats variables, all stats are resetted.
3. Put in a mutex to prevent simultanous access to the stats variables.

Bugfix: http://www.pastie.org/2788087


BUGFIX #11:

Notifications using 'pm_notifier_call_chain' when entering/leaving DEEP IDLE have been removed.

Bugfix: http://www.pastie.org/2796786


BUGFIX #12:

Removed RTC TICK as wakeup source and added RTC ALARM.

Bugfix: http://www.pastie.org/2805119


BUGFIX #13:

Added ST as wakeup source.

Bugfix: http://www.pastie.org/2815223


BUGFIX #14:

Added KEY as wakeup source.

Bugfix: http://www.pastie.org/2815884


No further patches will be published here. I have set up a git repo for all my tweaks. Each mod has its own branch to keep the tweaks cleanly separated and one can simply pull the latest patches from the corresponding branch.

https://github.com/Ezekeel/GLaDOS-nexus-s/tree/deepidle



As I said above, this code is almost entirely written by "some other guys". I just made some improvements and extensions to it. Unfortunately I have no idea who these great people are, so I cannot give any credit (yet). However I asked sixstringsg and he told me he would ask around.


CAM bug

There is a bug in the Google Talk app with video-chat which causes the CAM submodule to be activated on boot. As a consequence the DEEP IDLE state will not be used. Opening the camera app once and then closing it, shuts down the CAM module so after that the DEEP IDLE state will be used properly. The Talk version without video-chat does not have this issue. Unfortunately both version are shown as Version 1.3 in the application info, however the app size differs (740KB with video-chat, 500KB without video-chat) as well as the permissions (with video-chat additionally has the permission 'Hardware controls').

without video-chat:



with video-chat:



A big thank you to FloHimself for tracking down the problem!
The Following 129 Users Say Thank You to Ezekeel For This Useful Post: [ Click to Expand ]
 
simms22
Old
#2  
simms22's Avatar
Recognized Contributor
Thanks Meter 10007
Posts: 15,578
Join Date: Jun 2009
Location: BROOKLYN!
exciting
 
superfloup
Old
#3  
superfloup's Avatar
Senior Member
Thanks Meter 12
Posts: 133
Join Date: Oct 2008
Location: Massy

 
DONATE TO ME
Woow !! Sounds good !!



Bootloader: MAKOZ10o | Recovery: TWRP 2.4.4.0 | Radio: 2.0.1700.48
ROM: Paranoid Android 3.15 20130327 | Kernel: ziddey OTG Kernel


Bootloader: 4.18 | Recovery: TWRP 2.4.3.0
Kernel: Stock Paranoid Android | ROM: Paranoid Android 3.15 20130328

Accessories
Smartwatch 0.1.B.1.0
Ar.Drone 2.0 + DualShock 3

 
alxjag
Old
#4  
Junior Member
Thanks Meter 0
Posts: 13
Join Date: Nov 2008
Awesome! Can't wait... J
 
treUse
Old
#5  
Senior Member
Thanks Meter 84
Posts: 560
Join Date: Jan 2011
Wow, really looking forward to this

Sent from my Nexus S using Tapatalk
 
qubz
Old
#6  
qubz's Avatar
Recognized Developer
Thanks Meter 68
Posts: 576
Join Date: Jun 2007
Location: A parallel dimension

 
DONATE TO ME
Holy crap. Awesome.
Nexus 4 | Transformer Prime
R.I.P - SGS2 , Nexus S, Xoom WiFi, HTC Desire, HTC Trinity, Dopod
 
Serious_Beans
Old
#7  
Serious_Beans's Avatar
Senior Member
Thanks Meter 273
Posts: 2,237
Join Date: Feb 2011
Location: New York

 
DONATE TO ME
Quote:
Originally Posted by qubit76 View Post
Holy crap. Awesome.
This with morfics TEUV will give me days of battery life, can't wait! Great stuff!
 
arianR10
Old
#8  
arianR10's Avatar
Senior Member
Thanks Meter 4
Posts: 146
Join Date: Sep 2010
Location: Toronto
!!!!!!!!!!!!!!
 
franciscofranco
Old
#9  
franciscofranco's Avatar
Recognized Developer
Thanks Meter 44450
Posts: 10,332
Join Date: Dec 2010
Location: Mountain View, CA

 
DONATE TO ME
**** that looks ****ing awesome. Can't wait to see the code to check what you did.
franco.Kernel - Galaxy Nexus
franco.Kernel - Nexus 4
franco.Kernel - Nexus 7
franco.Kernel - Nexus 10


Google+ | Twitter | Portfolio

CruzerLite franco.Kernel case for the Nexus 4
CruzerLite franco.Kernel case for the Galaxy Nexus

You don't like my reply? Read this

However, if you put any trust in Quadrant scores you could use them to prove that dancing naked for 5 minutes in your garden affects device performance. By Chainfire
The Following User Says Thank You to franciscofranco For This Useful Post: [ Click to Expand ]
 
robin_77
Old
#10  
Member
Thanks Meter 5
Posts: 71
Join Date: Aug 2011
Ezekeel - are you a google employee, how the heck do you come out with these brilliant ideas and code implementations week to week? I think we're all for longer battery life. For some myself included have weighed the decision of that 3000mah whale of a battery (opt cover) or tweak the heck out of phones to extract as much as we can from it.

I'm all for the tweaks.

I'm waiting for the next [MOD] [KERNEL] - walk my dog lol

 
Post Reply+
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

report this ad
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Go to top of page...