Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,772,773 Members 51,800 Now Online
XDA Developers Android and Mobile Development Forum

[SCRIPT/DISCUSSION] Loopy Smoothness Tweak (Android's priorities) [02-02-2013]

Tip us?
 
lpy
Old
(Last edited by lpy; 14th November 2013 at 11:19 AM.)
#1  
lpy's Avatar
Senior Member - OP
Thanks Meter 148
Posts: 231
Join Date: Aug 2010
Post [SCRIPT/DISCUSSION] Loopy Smoothness Tweak (Android's priorities) [02-02-2013]

LATEST UPDATE: 02.02.2013

A question that I'm often asked is:
"Can I have your permission to use your script in my ROM etc...?"

That answer to that is:
"
Yes it is ok to use the code. I don't really believe it's useful anymore, but if you can find a use, and possibly improve the code to make it work better, go for it."

Version 1.0.2

Download available for ClockworkMod: requires Root (Superuser/Busybox), and a kernel that supports "init.d" scripts (script files that are stored in /system/etc/init.d/).


Always have a backup before installing any modification. Even though this script is rather simple, I can't guarantee it will work on every ROM! It appears to freeze custom ROMs such as CM, I don't know why. Maybe someone else knows.

NOTE: This script appears to be less and less useful as newer ROMs and phones become more sophisticated in dealing with any sort of lag. Tested on I9000XWJW9 and it doesn't seem to help. This ROM is quite smooth anyway.

What this script hopes to achieve:

- Entering your PIN with minimal/no delay/lag
- Minimising delay with lock screen pattern/unlock (not the time it takes to wakeup, but the touchscreen response lag)
- Having smoother notifications on the status bar, and smoother pulldown menu response
- Minimising Launcher lag while Media Scanner is running (can scroll through screens without heavy pauses)
- Minimising lag when scrolling through the market while icons are still loading
- Installing/Uninstalling apps while continuing other tasks
- Minimising/eliminating intermittent lag spikes in gaming
- No delay in being able to answer phone calls
... you get the idea


Quote:
#!/system/bin/sh
#
# Loopy Smoothness Tweak v1.0.2 - 02/02/2013 (http://forum.xda-developers.com/show....php?t=1137554).
#
# An attempt at keeping the UI running a little smoother.
# Optimised for Galaxy S1 stock ROM, but can be modded for other Android devices. Backup recommended before running scripts.
# Not recommended for custom ROMs, such as CM. The "sleep" command appears to halt the OS, rendering the script useless.
# Feel free to edit how you wish, but please leave at least the first comment above as is.
# Any editing must be saved in UNIX (LF) format. Google "text editor unix lf" to read more.
#
However, as phones get faster, this script will probably have less of an effect.

Even with 2.3.6 and ext4, RAM scripts, governor tweaks, other tweaks, etc. the occasional lag still appears to be there, especially noticable when installing/unistalling, gaming, or even just scrolling through the launcher screens and menus. I decided to investigate. Being a linux newbie, I had to also source the appropriate commands.

The following has been discussed a little here and there on other Android forums, but surprisingly I couldn't find anything about it in the I9000 section...

Thinking of how processes in Windows can be run with six different priorities (Low, Below Normal, Normal, Above Normal, High, Realtime), I investigated to see what the deal was with Linux/Android. Turns out there are forty settings, ranging from -20 (Highest) to 0 (Default) to 19 (Lowest).

The next step was to see which process was causing the CPU bottleneck as it was happening. The top command is handy for this. I used this in adb shell (these tests were performed quite some time after reboot when there was craploads of stuff in the memory - the impact is not very noticeable after a fresh boot, or with very few apps installed/running.

UPDATE: The reason behind the majority of the kswapd0 issue was due to modified code of a custom kernel I was once using. I do however still find the script of benefit to me, and maybe you will too. You can still read below if you wish, but it may be irreverent.

 

Code:
adb shell
top -n 1
At the top of the list was:
Quote:
18 16% R 1 0K 0K fg root kswapd0
kswapd0 is a kernel thread responsible for managing the memory.

I know Angry Birds can show a little lag from time to time, so I performed a little testing while playing that.

To change the priority of a running process, I used the renice command as such:
Code:
su
renice -20 `pidof kswapd0`
where -20 equals highest priority and `pidof kswapd0` returns the PID of the process, which is required by renice. In this case, 18.

To confirm the change, the ps command will give you what you need to know. Interesting to note "krfcommd" with a value of -10, and "mediaserver", -8.

Code:
ps -p kswapd0
If you enter "ps -p" all processes in memory are shown, along with their "nice" value.


It shouldn't take long before the game becomes barely playable at times (for a split second), as kswapd0 completely takes over the CPU until it performs its task. Setting the priority to
Code:
renice 0 `pidof kswapd0`
returns kswapd0 back to its default, and Angry Birds with the seldom lag. Let's go the other way now with
Code:
renice 19 `pidof kswapd0`
setting kswapd0 to its lowest priority, it should now have barely any impact on the game, if at all. Will this have negative impact on Android, though? Not sure yet.

We can even go one better and give Angry Birds all the CPU it needs via
Code:
renice -20 `pidof com.rovio.angrybirds`
With these settings in place, you could virtually play this all day, and your battery would probably be flat way before any lag becomes noticeable.
One problem though, when you close and reopen the game - assuming it's left the memory - it will have a new PID and the priority is set back to 0 (default).
Now, with my very limited knowledge of Linux/Android/Busybox, I'm not sure if there's a way to tell an app to open with a priority other than 0. Well, there is the nice command, but I couldn't get that working.

A common problem is the phone dialer app taking too long to popup when someone calls. Luckily, processes such as this one enter the memory from boot and seem to remain there. Setting its priority to -20 will basically tell the CPU to hold everything with less priority and open the phone dialer ASAP so you can take that call. And it works. I was on Angry Birds when a call came in, and was able to answer it on the first ring. I tried then calling myself without the -20 phone or the 19 kswapd0 settings, and there was an extra 2-3 second delay before I was able to answer. A few other apps that usually never leave the memory are default launcher (in my case, ADW Launcher EX), SMS/MMS app, Swype, Status Bar, and some others.

Even with apps launching at the default priority, with kswapd0 at 19, it still allows them more CPU, and may just be enough to combat this lag that people have tried to rid of with RAM scripts, Overclocking, and what not.

The script shown below is not the current init.d download script.

And the result (still a work in progress):

Code:
#!/system/bin/sh

###### Loopy Smoothness Tweak for Galaxy S (Experimental) #######

sleep 3
renice 18 `pidof kswapd0` # please give credit and thanks to loopy for this line if it works

##### Set nice levels for smoothness loop BEGIN #####
### Let's loop this in the background for a bit
for i in {1..20}
do
    sleep 5

    ### Phone dialer app ###
    renice -20 `pidof com.android.phone`

    ### Launcher apps ###
    renice -19 `pidof com.sec.android.app.twlauncher` # TouchWiz Launcher
    renice -19 `pidof org.adwfreak.launcher` # ADW Launcher Ex
    renice -19 `pidof org.zeam.core` # Zeam launcher

    ### System apps ###
    renice -19 `pidof com.android.mms` # Text message app
    renice -19 `pidof com.swype.android.inputmethod` # Swype keyboard
    renice -15 `pidof com.sec.android.app.controlpanel` # Task manager
    renice -15 `pidof com.android.systemui` # Status bar
    renice -9 `pidof com.android.settings` # Settings menu
    renice -9 `pidof com.android.browser` # Browser app
    renice -9 `pidof com.android.vending` # Market app
    renice -6 `pidof com.sec.android.app.camera` # Camera app
    renice -6 `pidof com.sec.android.app.fm` # FM Radio app
    renice -6 `pidof com.google.android.youtube` # YouTube app
    renice -6 `pidof com.google.android.apps.maps` # Maps
    renice -3 `pidof android.process.acore`
    renice -3 `pidof android.process.media`

    ### Memory management kernel thread ###
    renice 19 `pidof kswapd0` # please give credit and thanks to loopy for this line if it works
done
##### Set nice levels for smoothness loop END #####



What would be ideal is if programs such as launchers (TouchWiz Launcher etc..) had the ability to set its "nice" level upon launch, and have kswapd0 set real low, assuming it's safe to do so. If Android isn't doing this, why not? This isn't a good idea? Maybe it's only Samsung? If so, then... well, I won't ask why not... :P


NOTE: These commands/parameters vary depending on the su/busybox version installed.

Quote:
Common SGS1 apk's:

com.android.phone # Phone
com.sec.android.app.dialertab # Dialer
com.sec.android.app.twlauncher # TouchWiz Launcher
com.android.mms # Messaging
com.swype.android.inputmethod # Swype keyboard
com.sec.android.app.controlpanel # Task manager
com.android.systemui # Status bar
com.android.settings # Settings menu
com.android.browser # Internet Browser
com.android.vending # Google Play
com.sec.android.app.camera # Camera
com.sec.android.app.fm # FM Radio
com.google.android.youtube # YouTube
com.google.android.apps.maps # Google Maps
com.android.phone # Phone


Common SGS3 apk's:

com.android.phone
com.android.contacts
com.sec.android.app.controlpanel
com.android.browser
com.android.mms
com.android.vending
com.android.settings
mediaserver
com.sec.android.app.camera
com.sec.android.app.videoplayer
com.sec.android.app.myfiles
com.android.browser
com.sec.android.app.camera

Quick FAQ:

I wanna see how it works without it again, how do I remove the script?
To remove, delete the file /system/etc/init.d/S00loopyst. If using Root Explorer, use Mount R/W before you attempt to delete. For adb, use the commands at the DOS prompt:
Code:
C:\>adb root
restarting adbd as root

C:\>adb remount
remount succeeded
Code:
C:\>adb shell rm /system/etc/init.d/S00loopyst


C:\>
For the Test versions (prior to v1.0.0):
 

Code:
C:\>adb shell rm /system/etc/init.d/S_loopy_smoothness_tweak


C:\>
How can I make this script run first/last?
Scripts in the init.d are run in alphabetical/numerical order, following the letter "S". For example, script "S_ramboost" would run AFTER "S00loopyst", because "r" is after "l". Renaming it to "S_1ramboost" will now run that script BEFORE "S00loopyst".

How do I edit the script and add apps I use?

Open the .zip file and extract the file S00loopyst from /system/etc/init.d/. Alternatively, if installed, you can edit the file directly from the phone's init.d folder. File must be saved in UNIX format.

So I can just edit the script and add apps I use?
You can, but there is no guarantee this will actually work. Very few processes remain in memory...there is a lot of chopping and changing going on in the background. You could alter the script's timing variables, but even then results are unpredictable with anything other than what I call the resident system processes. If you're able to improve the script in any way, let us know.

Will this work on a custom ROM such as CM/MIUI?
Probably not. The "sleep" command within the screen appears to run in the foreground as opposed to the background in a stock ROM, hence making the script useless. Not to mention it may prevent anything else loading.

Are the settings permanant?
Usually. Most apps remain in the memory, although some do come and go. If you use the Task Manager to clear apps from the memory, or use the "Clear memory" option, the apps will have lose their priority when removed from the memory.

Changelog:

v1.0.2a:
  • Added option for "low priority" tasks.
  • Early script finish if 2nd loop is complete before "number of checks" is complete.
  • More detailed log file.

v1.0.1:
  • Cleaned up some invalid code.
  • Tested OK on rooted Samsung GT-I9000 2.3.6 ROM.
  • Tested OK on rooted Samsung GT-I9305T 4.1.1 ROM.
  • Tested NOT OK on CM 9.1 GT-I9000 ROM. Phone freezes on boot until loop is complete.

v1.0.0:
  • User friendly - a lot easier to configure.
  • Total number of processes automatically calculated (used to shorten 1st loop if all apps are in memory).
  • Debug file created for troubleshooting (includes "renice" pass or fail test) - can be saved to /data partition or sdcard.
  • Simple "Aggressive" option to increase priority of resident and other apps.
  • In addition to kswapd0 checking before continuing, launcher app is also checked in a loop before continuing (usually no more than ~15 seconds after start-up).
  • oom_adj & oom_score settings for launcher to remain resident (doesn't seem to work too well... settings don't appear to "stick").

T2:
  • "Smarter" script.
  • Checks for kswapd0 every 3 seconds until found, and then 'nice' level is set.
  • 1st loop: checks for apps 60 times, at an interval of 3 seconds. Once an app is found in memory, 'nice' level is set, and it is not checked again during the loop. (Some apps enter and then leave the memory, which ain't helpful.)
  • 2nd loop: checks again 6 times at an interval of 5 seconds and sets 'nice' level.
  • Settings are a little more user-friendly.
  • Debug option (if kernel stores a log file).

T1:
  • Sets kswapd0 'nice' level after 3 seconds.
  • Pauses for 15 seconds, sets 'nice' levels for other apps (many of which don't remain in memory).


Download:
  • Download file and transfer to SD card
  • Enter ClockworkMod Recovery via Vol+Up/Menu/Power button combo
  • Select zip and install (remove any "Test" versions before using Version 1 or above)
GT-I9305T // 4.3
GT-I9000 // PDA XWJW9 2.3.6

The Following 93 Users Say Thank You to lpy For This Useful Post: [ Click to Expand ]
 
petsasj
Old
#2  
petsasj's Avatar
Retired Recognized Developer
Thanks Meter 1566
Posts: 1,597
Join Date: Mar 2010
Location: Athens

 
DONATE TO ME
I'm not a Linux/Android master myself, but this actually is food for thought. I'll try and do some reading, and i would really like to hear more experienced users's opinion about it.

Thanks for the explanatory post.
The Following User Says Thank You to petsasj For This Useful Post: [ Click to Expand ]
 
Overlord Pain
Old
#3  
Overlord Pain's Avatar
Senior Member
Thanks Meter 38
Posts: 209
Join Date: Aug 2010
Location: Ottawa
Very interesting idea

Sent from my GT-I9000 using XDA Premium App
___________________

 
lpy
Old
#4  
lpy's Avatar
Senior Member - OP
Thanks Meter 148
Posts: 231
Join Date: Aug 2010
Thanks for the feedback so far. I too would like to hear what more experienced users have to say, as I've only been using linux for a few days now and have quite a lot to learn.

The other good thing about this is, if it turns out to be a good idea, I'm guessing it can be used on basically any Android device.
GT-I9305T // 4.3
GT-I9000 // PDA XWJW9 2.3.6

The Following User Says Thank You to lpy For This Useful Post: [ Click to Expand ]
 
chebon.
Old
#5  
Member
Thanks Meter 51
Posts: 78
Join Date: Jun 2011

 
DONATE TO ME
this is something to be looked at
interesting
Galaxy S II
 
explodingboy70
Old
#6  
explodingboy70's Avatar
Recognized Developer
Thanks Meter 1570
Posts: 2,965
Join Date: Oct 2010
Location: Southwest Florida

 
DONATE TO ME
I'd personally like to see more discussion on this and input from more experienced people as well so...

BUMP
 
simone201
Old
#7  
simone201's Avatar
Recognized Developer
Thanks Meter 4929
Posts: 4,753
Join Date: Sep 2010
Location: Lecco

 
DONATE TO ME
A good place to start from...
Will take a look more....i am very interested on this and if u need help in scripts i can help u....

Sent from my GT-I9000 using XDA Premium App

Support my Work, Please
 
zacharias.maladroit
Old
(Last edited by zacharias.maladroit; 24th June 2011 at 12:53 AM.)
#8  
zacharias.maladroit's Avatar
Retired Recognized Developer
Thanks Meter 2180
Posts: 2,930
Join Date: Oct 2010
Location: Vienna

 
DONATE TO ME
interesting approach lpy !


FYI:

looping should be fine since it is also done, e.g. on the Galaxy Spica and CM7

but with different parameters



I have some ideas back in the back of my mind that might help with this issue (kernel-patches and other stuff)


Thanks !





edit:


related:

Quote:
On Saturday, May 07, 2011 09:35:48 PM Ali Ahsan wrote:
> Hi All
>
> I have xeon server with 16 Gb Ram and no Swap memory.I am running
> cassandra server on two node in cluster.When there is high load on
> server kswapd0 kicks inn and take 100% cpu and make machine very slow
> and we need to restart out cassandra server.I have latest kernel
> 2.6.18-238.9.1.el5.Please let me know how can i fix this issue .Its
> hurting us badly this our production server any quick help will be
> appreciated.

There is more than one bug that causes this behaviour. A few related memory
managent situations (possibly responsible) may actually be avoided if you add
some swap (even if it's not used). My suggestion would be to add some swap,
set swappiness to 0 and see what happens.

/Peter
_______________
http://www.linux-archive.org/centos/...ap-system.html
development-mode: occasional (as time permits); Status: currently "retired"

Seasons Greetings to everyone and thank you for everything !

Kernels:
TalonDev (for Samsung stock ROMs, SGS/i9000, Captivate)



* for GT-i9000/Telus-Fascinate * for Captivate * for Vibrant


<-- Like my work ? buy me a beer

Please press thanks button if my post was helpful and/or useful to you, get me more dropbox space by signing up - Thanks !
The Following User Says Thank You to zacharias.maladroit For This Useful Post: [ Click to Expand ]
 
Chief_Death
Old
#9  
Senior Member
Thanks Meter 17
Posts: 148
Join Date: Dec 2010
Tried the script with go contacts,go sms, go launcher (edited ofcourse)
it seems to make them open instantly even when they aren't running in the background..

if some1 could make an app which make the app you use as -20 or something and if you close it or move to another one make it back to 0..
 
lttldvl
Old
#10  
lttldvl's Avatar
Senior Member
Thanks Meter 255
Posts: 338
Join Date: Feb 2011
Location: Inverurie

 
DONATE TO ME
It would be really interesting to see this being controlled at Kernel level (if possible.)

I know that it contains the govenors to control the CPU and UV settings (smartass, conservative, etc.), but wouldn't it be awesome if you could determine a priority level for apps installed on your phone so that it removed a load of junk from memory?

I mean I know app developers would all start setting their apps "attention level" to -20, but if there could be an intelligent manager for this, it would make things alot faster I would think.

The Following User Says Thank You to lttldvl For This Useful Post: [ Click to Expand ]
Tags
breakthrough idea, smoothness, tweak
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes