init.d performance startup script

daniel.weck

Senior Member
Nov 2, 2010
574
95
0
daniel.weck.free.fr
I've adapted the original performance tweaks by 'hardcore' @ XDA for my overclocked/undervolted Galaxy Tab. The tweaked values are pretty much the same as the ones recommended by the original poster, but my simple init.d/ script allows me to log default system values when pushing new values, which is useful for debugging. The script also contains comments to describe the role of each setting. Note that if you don't have init.d/ support in your kernel / initramfs, you can still run the script manually at each system start (use an app like GScript).

Code:
#!/system/bin/sh
# 
# Original tweaks by 'hardcore' @ XDA
# http://forum.xda-developers.com/showthread.php?t=813309

# This is a startup script designed for /system/etc/init.d/.
# Note that "run-parts" support (for init.d/ scripts) is normally provided by custom a initramfs,
# which should bundle busybox in /sbin/. The /sbin/run-parts.sh script should take care of
# running init scripts (by calling /sbin/runparts), and it should subsequently trigger
# the device startup (using "setprop filesystem.ready 1", or similar).
# Note that the recovery mode typically doesn't run /system/etc/init.d/ startup scripts.

# Ensure /sbin/busybox takes precedence.
# Normally this is redundant, because the /init.rc startup script already sets the correct path.
export PATH=/sbin:$PATH

# Logging of old/new sysfs values, useful for double-checking.
logFile=/data/local/tmp/S_perf_tweaks.log

if [ -f $logFile ]
then
rm $logFile
fi

touch $logFile

# This function logs the old value and writes the new value.
echo_()
{
	echo '' >> $logFile
	echo -n "${2}${3} (${1}): " >> $logFile
	#head -1 ${2}${3} >> $logFile
	#read $firstLine < ${2}${3}
	#echo -n $firstLine >> $logFile
	contents=`echo -n $(cat ${2}${3})`
	echo -n $contents >> $logFile
	echo -n " ---> " >> $logFile
	echo $1 > ${2}${3}
	contents=`echo -n $(cat ${2}${3})`
	echo -n $contents >> $logFile
}

# Note that the settings pushed by VoltageControl.apk
# could also be managed here (this only applies to kernels with clock/frequency tables and undervolt sysfs support):
#echo_ "50 50 50 25 25 25 25 " "/sys/devices/system/cpu/cpu0/cpufreq" "/UV_mV_table"
#echo_ 1400000 "/sys/devices/system/cpu/cpu0/cpufreq" "/scaling_max_freq"

echo "---------" >> $logFile

# Remount all partitions that use relatime with noatime and nodiratime instead.
# Note: atime generates a write-after-every-read, relatime is an optimized version of atime.
for k in $(mount | grep relatime | cut -d " " -f3)
do
	echo "mount -o remount,noatime,nodiratime $k" >> $logFile
	sync
	mount -o remount,noatime $k
done

# Here is a sample test to measure read/write performance on rfs partitions:
### test for write: dd if=/dev/zero of=/data/test count=30000
### test for read:  dd if=/data/test of=/dev/zero

echo "---------" >> $logFile

# Log the mount table
mount >> $logFile

echo "---------" >> $logFile

# Optimize the cfq/bfq I/O scheduler for flash memory (defaults are designed for spinning harddisks).
# Lower the idle wait, re-enable the low latency mode, remove the penalty for back-seeks,
# and explicitly tell the kernel that the storage is not a spinning disk.

for i in $(ls -1 /sys/block/stl*) $(ls -1 /sys/block/mmc*) $(ls -1 /sys/block/bml*) $(ls -1 -d /sys/block/tfsr*)
#for i in `ls /sys/block/stl* /sys/block/mmc* /sys/block/bml* /sys/block/tfsr*`;
do
	# DEF noop anticipatory deadline cfq [bfq]
	echo_ "bfq" $i "/queue/scheduler"
	
	# DEF 1 ?
	echo_ "0" $i "/queue/rotational"
	
	# DEF 1 ?
	echo_ "1" $i "/queue/iosched/low_latency"
	
	# DEF 2 ?
	echo_ "1" $i "/queue/iosched/back_seek_penalty"
	
	# DEF 16384 ?
	echo_ "1000000000" $i "/queue/iosched/back_seek_max"
	
	# DEF 6 ?
	echo_ "3" $i "/queue/iosched/slice_idle"
	
	sync
done

# Set tendency of kernel to swap to minimum, since swap isn't used anyway.
# (swap = move portions of RAM data to disk partition or file, to free-up RAM)
# (a value of 0 means "do not swap unless out of free RAM", a value of 100 means "swap whenever possible")
# (the default is 60 which is okay for normal Linux installations)

# DEF 60
echo_ "0" "/proc/sys/vm" "/swappiness"

# Lower the amount of unwritten write cache to reduce lags when a huge write is required.

# DEF 20
echo_ "10" "/proc/sys/vm" "/dirty_ratio"

# Increase minimum free memory, in theory this should make the kernel less likely to suddenly run out of memory.

# DEF 3102
echo_ "4096" "/proc/sys/vm" "/min_free_kbytes"

# Increase tendency of kernel to keep block-cache to help with slower RFS filesystem.

# DEF 100
echo_ "1000" "/proc/sys/vm" "/vfs_cache_pressure"

# Increase the write flush timeouts to save some battery life.

# DEF 250
echo_ "2000" "/proc/sys/vm" "/dirty_writeback_centisecs"

# DEF 200
echo_ "1000" "/proc/sys/vm" "/dirty_expire_centisecs"

# Make the task scheduler more 'fair' when multiple tasks are running,
# which improves user-interface and application responsiveness.

# DEF 10000000
echo_ "20000000" "/proc/sys/kernel" "/sched_latency_ns"

# DEF 2000000
echo_ "2000000" "/proc/sys/kernel" "/sched_wakeup_granularity_ns"

# DEF 1000000
echo_ "1000000" "/proc/sys/kernel" "/sched_min_granularity_ns"

sync

# Miscellaneous tweaks
setprop dalvik.vm.startheapsize 8m
#setprop wifi.supplicant_scan_interval 90

echo '' >> $logFile
echo "---------" >> $logFile



#This apply a tweaked deadline scheduler to all RFS (and ext2/3/4, if existent) partitions.
#for i in /sys/block/*
#do
	# DEF noop anticipatory deadline cfq [bfq]
	#echo deadline > $i/queue/scheduler
	
	#echo 4 > $i/queue/iosched/writes_starved
	#echo 1 > $i/queue/iosched/fifo_batch
	#echo 256 > $i/queue/nr_requests
#done
 
Last edited:

jeebspawnshop

Senior Member
Feb 6, 2008
1,378
1,125
0
Thanks for this Daniel - since I know absolutely nothing about anything Android, all I can say is that I have 2 questions ;)

1 - Would this script be any benefit to someone running a custom ROM (Overcome 1.1.3 in my case) and a custom kernel (Richard Trip's 1.4gHz EXT4)? I ask because I don't know if these guys have already including these tweaks or not... but maybe I should ask them instead...

2 - Is there any way you could put that script - in its most efficient, non-debugging form - in to a script file that us speed-freaks can just toss in to init.d and reboot?

Please do forgive me for any newbishness I have displayed here.
 

fastcx

Senior Member
Jun 9, 2010
1,007
191
0
Puchong
So should I put this script in /system/etc/init.d/ ?

UPDATE: Ok, I've done the script(without extension at the back of the file name) and put it in ~/init.d/, it runs well. I comment out the remount and logging function tho :) Tested it on some games that required load time like gangstar which lags alot previously, now just dnt have any lag time in it! Thanks!

Anyway, im running Overcome rom with richard's kernel, other than voltage script, i saw 2 more script that does the following:

10fixsh does:
"#!/system/bin/sh

busybox mount -o remount,rw /
find /sbin -maxdepth 1 -type l -exec rm {} \;
busybox mount -o remount,ro /
"

99done does:
"#!/system/bin/sh

sync;
setprop mcr.filesystem.ready 1;
"

So I just add your script as userinit since they do not have conflicting calls :)
 
Last edited:

fastcx

Senior Member
Jun 9, 2010
1,007
191
0
Puchong
Personally thinks that similar tweaks should be included in custom kernels so that those who wish to do more extreme settings can go with it while those who just wants increased performance as it is can still have the boost provided by such init script.
 

kay_kiat88

Senior Member
Dec 19, 2010
889
246
0
Personally thinks that similar tweaks should be included in custom kernels so that those who wish to do more extreme settings can go with it while those who just wants increased performance as it is can still have the boost provided by such init script.
so basically just copy and paste the whole "code" in the first post, and create it as userinit.sh and put the userinit.sh file in /system/etc/init.d ?

am i right?
 

fastcx

Senior Member
Jun 9, 2010
1,007
191
0
Puchong
so basically just copy and paste the whole "code" in the first post, and create it as userinit.sh and put the userinit.sh file in /system/etc/init.d ?

am i right?
still need more time to test, after some verification, none of the "/proc/sys/kernel" setting works with any of the kernel i'm using. thats y i need more time to rectify..

used richard's kernel and overcome kernel, both dont do anything now..perhaps wrong command in the script? Cause run-parts.sh does specify where to run script, and init.d does have 2 script in there that runs. So i'll need more time to make sure it works now..

Dont need to have .sh as extension, just a name for your script like the other 2 script that i posted in previous reply. And I did not copy the whole script to run, as the script seems questionable in some part..
 
Last edited:
  • Like
Reactions: kay_kiat88

kay_kiat88

Senior Member
Dec 19, 2010
889
246
0
still need more time to test, after some verification, none of the "/proc/sys/kernel" setting works with any of the kernel i'm using. thats y i need more time to rectify..

used richard's kernel and overcome kernel, both dont do anything now..perhaps wrong command in the script? Cause run-parts.sh does specify where to run script, and init.d does have 2 script in there that runs. So i'll need more time to make sure it works now..

Dont need to have .sh as extension, just a name for your script like the other 2 script that i posted in previous reply. And I did not copy the whole script to run, as the script seems questionable in some part..
okay.. so basically just copy everything under the "code" and create it as userinit?

edit: hmmm okay thanks for you help. i don't think it's of any use for me now as my tab is quite fast and i don't do any intensive stuff on it. thanks anyway!
 
Last edited:

fastcx

Senior Member
Jun 9, 2010
1,007
191
0
Puchong
okay.. so basically just copy everything under the "code" and create it as userinit?

edit: hmmm okay thanks for you help. i don't think it's of any use for me now as my tab is quite fast and i don't do any intensive stuff on it. thanks anyway!
Now i'm editing init.rc file instead, but it's not the safest thing to do
 

daniel.weck

Senior Member
Nov 2, 2010
574
95
0
daniel.weck.free.fr
So should I put this script in /system/etc/init.d/ ?

UPDATE: Ok, I've done the script(without extension at the back of the file name) and put it in ~/init.d/, it runs well.
Yes, the filename of the startup script must start with "S_" and must not end with ".sh". This is standard Linux stuff. ;)

find /sbin -maxdepth 1 -type l -exec rm {}
Removing the symlinks from "/sbin/" ... why ?
Normally Busybox should be installed properly in "/sbin/" by your kernel provider, you shouldn't have to touch this folder. :)

99done does:
"#!/system/bin/sh

sync;
setprop mcr.filesystem.ready 1;
"
The "mcr.filesystem.ready" is only relevant if the "init.rc" file in the kernel zImage's initramfs ramdisc responds to a change to the "mcr.filesystem.ready" property (for example, when its value changes from 0 to 1). This is typically used to start the device normally, after *all* of the "/system/etc/init.d/" startup scripts have been executed.

The line of code "setprop mcr.filesystem.ready 1" is therefore typically included in "/sbin/runparts.sh", not in one of the "S_startup_scripts" (as this may trigger the device normal startup prematurely).

It really depends on your kernel, so it should be documented by your kernel provider. :D
 

fastcx

Senior Member
Jun 9, 2010
1,007
191
0
Puchong
Yes, the filename of the startup script must start with "S_" and must not end with ".sh". This is standard Linux stuff. ;)

It really depends on your kernel, so it should be documented by your kernel provider. :D
Great! :) thanks for clarifying! Sadly none of the kernel provider states any info on such matter. Will try your script again, as previously running thru gscript not all command works..
 

fastcx

Senior Member
Jun 9, 2010
1,007
191
0
Puchong
Copied your script 100%, put it in init.d with chmod 755, named it S_userinit, doesnt run..i double checked by cat value that I stated to change in the script.

Help? It obviously runs the UV script in init.d tho.

EDIT: Got it to work by editing the UV script, now settings are in! Great! Time to test ;) anyway, nodiratime is not needed, noatime already has it
 
Last edited:

andytof46

Member
Sep 25, 2008
20
0
0
So how can I use this script - I want to

Hi, Just connected to my galaxy tab running froyo, and went looking in my filesystem for the init.d folder and yeah there isn't one

do i create one? where? and what permissions?

I try to keep this brief and not waste too much of your time.

many thanks for the script

Ttime & effort ++
 

fastcx

Senior Member
Jun 9, 2010
1,007
191
0
Puchong
Hi, Just connected to my galaxy tab running froyo, and went looking in my filesystem for the init.d folder and yeah there isn't one

do i create one? where? and what permissions?

I try to keep this brief and not waste too much of your time.

many thanks for the script

Ttime & effort ++
you will need a kernel that support init script, after that, /etc/init.d(or /system/etc/init.d) will be created
 

jeebspawnshop

Senior Member
Feb 6, 2008
1,378
1,125
0
Copied your script 100%, put it in init.d with chmod 755, named it S_userinit, doesnt run..i double checked by cat value that I stated to change in the script.

Help? It obviously runs the UV script in init.d tho.

EDIT: Got it to work by editing the UV script, now settings are in! Great! Time to test ;) anyway, nodiratime is not needed, noatime already has it
Don't suppose you could post a copy of your trimmed & tweaked version of Daniel's script for us to use? ;)

Cuz I, for example, have no idea what's useful in the original script, and what's not useful. But I at least know how to edit the UV script, heh.
 

fastcx

Senior Member
Jun 9, 2010
1,007
191
0
Puchong
Don't suppose you could post a copy of your trimmed & tweaked version of Daniel's script for us to use? ;)

Cuz I, for example, have no idea what's useful in the original script, and what's not useful. But I at least know how to edit the UV script, heh.
LOL here it goes

Code:
#!/system/bin/sh
#set UV
echo "0 0 0 0 0 0 0 0 " > /sys/devices/system/cpu/cpu0/cpufreq/UV_mV_table
echo 1000000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
#select enabled states
echo "0 0 0 0 0 0 0 0 " > /sys/devices/system/cpu/cpu0/cpufreq/states_enabled_table
#set scheduler for stl, bml and mmc
for i in `ls /sys/block/stl*` /sys/block/bml* /sys/block/mmcblk*
do
echo "bfq" > $i/queue/scheduler
echo "0" > $i/queue/rotational
echo "1" > $i/queue/iosched/low_latency
echo "1" > $i/queue/iosched/back_seek_penalty
echo "1000000000" > $i/queue/iosched/back_seek_max
echo "3" > $i/queue/iosched/slice_idle
done

# Remount all partitions with noatime
for k in $(busybox mount | grep relatime | cut -d " " -f3)
do
sync
busybox mount -o remount,noatime $k
done

# Tweak kernel VM management
echo "0" > /proc/sys/vm/swappiness
echo "10" > /proc/sys/vm/dirty_ratio
echo "4096" > /proc/sys/vm/min_free_kbytes

# Tweak kernel scheduler, less aggressive settings
echo "18000000" > /proc/sys/kernel/sched_latency_ns
echo "3000000" > /proc/sys/kernel/sched_wakeup_granularity_ns
echo "1500000" > /proc/sys/kernel/sched_min_granularity_ns

# Misc tweaks for battery life
echo "2000" > /proc/sys/vm/dirty_writeback_centisecs
echo "1000" > /proc/sys/vm/dirty_expire_centisecs

# Miscellaneous tweaks
setprop dalvik.vm.startheapsize 8m
done
NOTE: If you change any settings in uv app, u'll revert S_volt_scheduler to ONLY UV settings, so It's recommended to edit your UV setting on this file instead of using uv app from now on if you wants to keep your other settings
 
Last edited:

fastcx

Senior Member
Jun 9, 2010
1,007
191
0
Puchong
Thanks dude!

I hit your Thanks Button too.
Thanks :) U should thanks the thread starter too :) U should just take it as reference, modify any that you feels ok, and perhaps post here as feedback on which makes things better, especially disc scheduler and task scheduler settings.

UPDATE: Made some changes to suggested value by daniel for "fairness", these setting really deals with multitasking better
echo "20000000" > /proc/sys/kernel/sched_latency_ns
echo "2000000" > /proc/sys/kernel/sched_wakeup_granularity_ns
echo "1000000" > /proc/sys/kernel/sched_min_granularity_ns
 
Last edited:

kay_kiat88

Senior Member
Dec 19, 2010
889
246
0
hey fastcx, i copied and pasted your script in the UV scheduler file in init.d but it seems that it's not working as the values don't apply. any ideas why?