[Galaxy S I9000 Android Development] lpy:
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

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.

adb shell
top -n 1

At the top of the list was:

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:

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.

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

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

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

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):

#!/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.

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:

C:\>adb root
restarting adbd as root

C:\>adb remount
remount succeeded

[SIZE=2][SIZE=2]C:\>adb shell rm /system/etc/init.d/[/SIZE][/SIZE][SIZE=2]S00loopyst
[/SIZE][SIZE=2]

[/SIZE][SIZE=2]C:\>
[/SIZE]

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

[SIZE=2][SIZE=2]C:\>adb shell rm /system/etc/init.d/S_loopy_smoothness_tweak[/SIZE][/SIZE][SIZE=2]
[/SIZE][SIZE=2]

[/SIZE][SIZE=2]C:\>
[/SIZE]

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)