#!/system/bin/sh
# Kick Ass Kernelizer created by zeppelinrox.
mount -o remount,rw /system 2>/dev/null
for m in /dev/block/mtdblock*
do
mount -o remount,rw $m /system 2>/dev/null
done
clear
echo " Kick Ass Kernelizer by zeppelinrox"
echo ""
sleep 2
echo " Current I/O Scheduler is in [square brackets]!"
echo ""
sleep 2
cat /sys/block/mmcblk0/queue/scheduler
if [ ! -d "/system/etc/init.d" ]; then
	mkdir /system/etc/init.d
	echo " Created /system/etc/init.d folder..."
	echo ""
	sleep 2
fi
cat > /system/etc/init.d/98kickasskernel <<EOF
#!/system/bin/sh
# Put together and refined by zeppelinrox. See included links for resources.
mount -o remount,rw /system 2>/dev/null;
for m in /dev/block/mtdblock*;
do
mount -o remount,rw \$m /system 2>/dev/null;
done;
busybox sysctl -p
#
# Disable normalized sleeper
#
mount -t debugfs none /sys/kernel/debug;
echo NO_NORMALIZED_SLEEPER > /sys/kernel/debug/sched_features;
#
# Memory management
#
# http://www.linuxinsight.com/proc_sys_hierarchy.html
# http://forum.xda-developers.com/showthread.php?t=813309
# http://forum.xda-developers.com/showthread.php?p=12445735
# http://forum.xda-developers.com/showthread.php?p=4806456
# http://intl.feedfury.com/content/47077504-tweak-skript-f-r-android-spica.html
#
busybox sysctl -w vm.oom_kill_allocating_task=0;
busybox sysctl -w vm.panic_on_oom=0;
busybox sysctl -w vm.dirty_background_ratio=60;
busybox sysctl -w vm.dirty_ratio=95;
busybox sysctl -w vm.min_free_kbytes=8192;
busybox sysctl -w vm.vfs_cache_pressure=10;
busybox sysctl -w vm.overcommit_memory=1;
busybox sysctl -w vm.min_free_order_shift=4;
busybox sysctl -w kernel.panic=0;
busybox sysctl -w kernel.panic_on_oops=1;
busybox sysctl -w kernel.msgmni=2048;
busybox sysctl -w kernel.msgmax=64000;
busybox sysctl -w kernel.shmmax=268435456;
busybox sysctl -w kernel.sem='500 512000 64 2048';
busybox sysctl -w kernel.sched_features=24189;
busybox sysctl -w kernel.hung_task_timeout_secs=30; # Set to 0 to disable but can cause black screen on incoming calls
busybox sysctl -w kernel.sched_latency_ns=18000000;
busybox sysctl -w kernel.sched_min_granularity_ns=1500000;
busybox sysctl -w kernel.sched_wakeup_granularity_ns=3000000;
busybox sysctl -w kernel.sched_compat_yield=1;
busybox sysctl -w kernel.sched_shares_ratelimit=256000;
busybox sysctl -w kernel.sched_child_runs_first=0;
busybox sysctl -w kernel.threads-max=5000;
busybox sysctl -w net.core.wmem_max=524288;
busybox sysctl -w net.core.rmem_max=524288;
busybox sysctl -w net.ipv4.tcp_tw_recycle=1;
busybox sysctl -w net.ipv4.tcp_rmem='6144 87380 524288';
busybox sysctl -w net.ipv4.tcp_wmem='6144 87380 524288';
#
# Misc tweaks for battery life
#
busybox sysctl -w vm.dirty_writeback_centisecs=2000;
busybox sysctl -w vm.dirty_expire_centisecs=1000;
#
# Remount with noatime and nodiratime
#
for k in \$(busybox mount | cut -d " " -f3);
do
sync;
busybox mount -o remount,noatime,nodiratime \$k;
done;
#
# Tweak cfq io scheduler
#
#for i in /sys/block/*/queue/scheduler;
#do
#if [ -f "\$i" ]; then
#	sync;
#	echo "cfq" > \$i;
#fi;
#done;
#for i in /sys/block/*;
#do
#if [ -f "\$i/queue/rotational" ]; then echo "0" > \$i/queue/rotational; fi;
#if [ -f "\$i/queue/iosched/low_latency" ]; then echo "1" > \$i/queue/iosched/low_latency; fi;
#if [ -f "\$i/queue/iosched/back_seek_penalty" ]; then echo "1" > \$i/queue/iosched/back_seek_penalty; fi;
#if [ -f "\$i/queue/iosched/back_seek_max" ]; then echo "1000000000" > \$i/queue/iosched/back_seek_max; fi;
#if [ -f "\$i/queue/iosched/slice_idle" ]; then echo "0" > \$i/queue/iosched/slice_idle; fi;
#if [ -f "\$i/queue/nr_requests" ]; then echo "512" > \$i/queue/nr_requests; fi;
#if [ -f "\$i/queue/iosched/fifo_batch" ]; then echo "1" > \$i/queue/iosched/fifo_batch; fi;
#if [ -f "\$i/queue/iosched/quantum" ]; then echo "16" > \$i/queue/iosched/quantum; fi;
#done;
#for i in /sys/block/*/queue/scheduler;
#do
#if [ -f "\$i" ]; then
#	sync;
#	echo "noop" > \$i;
#	sync;
#	echo "deadline" > \$i;
#fi;
#done;
mount -o remount,ro /system 2>/dev/null;
for m in /dev/block/mtdblock*;
do
mount -o remount,ro \$m /system 2>/dev/null;
done;
EOF
chown 0.0 /system/etc/init.d/98kickasskernel
chmod 777 /system/etc/init.d/98kickasskernel
#chown -R 0.2000 /system/etc/init.d
echo " Successfully created..."
echo ""
sleep 2
echo "          .../system/etc/init.d/98kickasskernel"
echo ""
sleep 2
echo "             Reboot is recommended!"
echo ""
sleep 2
echo " Stock roms: Use Script Manager to run..."
echo ""
sleep 2
echo "         ...98kickasskernel at boot and as root"
echo ""
sleep 2
echo " To uninstall, delete 98kickasskernel..."
echo ""
sleep 2
echo " Now launching 98kickasskernel..."
echo ""
sleep 2
sh /system/etc/init.d/98kickasskernel
echo ""
sleep 2
echo " Please ignore Unknown Key errors..."
echo ""
sleep 2
echo "     ...your kernel doesn't support those keys!"
echo ""
sleep 2
#echo " Current I/O Scheduler is in [square brackets]!"
#echo ""
#sleep 2
#cat /sys/block/mmcblk0/queue/scheduler
#echo " It should now be either [deadline] or [noop]!"
#echo ""
#sleep 2
echo " All done, buh bye..."