2011年10月20日 星期四

Android 記憶體調教

XDA 神人 Juwe11 的 Ram 調教語法 (需求 : Root, Kernel which supports init.d)
http://forum.xda-developers.com/showthread.php?t=1111145
/system/etc/init.d/ if there is file named S97ramscript
其中有這一段

if [ -e /sys/module/lowmemorykiller/parameters/minfree ]; then
 echo "2560,4096,5632,10240,11776,14848" > /sys/module/lowmemorykiller/parameters/minfree
fi

有6個數字 , 分別代表的意義是

1 : 2560  -> foreground ; 前景程式 , 目前正在執行 (不該動)
2 : 4096  -> visible ; 可見程式 , 目前未執行但尚未結束(不該動)
3 : 5632  -> Secondary server ; 作業系統需要的服務(不該動)
4 : 10240 -> hidden ; 隱藏程式 , 目前所不需要的服務
5 : 11776 -> content provider ; App 的內容提供者 (APP 有執行時優先權會調高)
6 : 14848 -> empty ; 空程式 , 並沒有執行 , 僅保留再記憶體 (優先要釋放的)

註 : 數字表達的是分頁 , 必須乘以 4K Byte (再除以1024會得到 MB)
     舉例 前景程式 2560 , 2560 * 4 / 1024 = 10 MB
     意思是說 當記憶體低於 10 MB 時 , 前景程式會被強迫關閉停止

一般是修改 4 5 6 這三個部分
4: 10240 = 40MB
5:11776 = 46MB
6:14848 = 58MB

如果希望都有超過 100MB 的記憶體的話就都改成 25600 囉
90mb = 23040 , 80mb = 20480 , 70mb = 17920


===============================
ANDROID 定制ROM教學區
===============================

十、系統啟動腳本
在system/etc/init.d 這個目錄,是刷機後系統第一次開機時的設定在這我們可以透過二進制指令來對內核進行控制與優化~
1.tweaks-optimize 建立一個文件輸入以下指令
#!/system/bin/sh          
#
#Optimisation script...

# Attempt to pin home app to memory.

$ECHO 'ro.HOME_APP_ADJ=1' > /data/local.prop        預設值改成1可優化執行完程式後回到桌面的LAG
$ECHO 'ro.HOME_APP_MEM 3072' >> /data/local.prop  優化內存釋放

$ECHO 'connection.concurrent=90' >>> /data/local.prop 可優化WIFI減少斷連



# IO Blocks Specifying      IO塊指定    

PART=`mount | cut -d " " -f3`;
BLK0=`ls -d /sys/block/mmcblk0*`;
BLOCK0=`ls -d /sys/block/mtdblock0*`;
BLOCK1=`ls -d /sys/block/mtdblock1*`;
BLOCK2=`ls -d /sys/block/mtdblock2*`;
BLOCK3=`ls -d /sys/block/mtdblock3*`;
BLOCK4=`ls -d /sys/block/mtdblock4*`;
BLOCK5=`ls -d /sys/block/mtdblock5*`;

# Tweak Memory and CPU        調整內存和CPU 
echo "::         RAM Tweaks.....";

echo "1024,2048,3072,4096,5120,6144" > /sys/module/lowmemorykiller/parameters/minfree
# echo "ondemand" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_ondemand;
# echo "ondemand" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_performance;
echo "............done";


# Tweak kernel VM management   調整內核虛擬機管理
echo "::         Kernel Virtual Memory Tweaks.....";

echo "3" > /proc/sys/vm/drop_caches;
echo "8192" > /proc/sys/vm/min_free_kbytes;
echo "600" > /proc/sys/vm/dirty_expire_centisecs;
echo "1" > /proc/sys/vm/oom_kill_allocating_task;
echo "0" > /proc/sys/vm/laptop_mode;
echo "10" > /proc/sys/vm/vfs_cache_pressure;
echo "............done";




# Various Kernel Tweaks         內核調整
echo "::         Kernel Tweaks.....";

# echo "0" > /proc/sys/kernel/hung_task_timeout_secs;
# echo "1" > /proc/sys/kernel/panic_on_oops;
# echo "0" > /proc/sys/kernel/tainted;
echo "18000000" > /proc/sys/kernel/sched_latency_ns;
echo "1500000" > /proc/sys/kernel/sched_min_granularity_ns;
echo "3000000" > /proc/sys/kernel/sched_wakeup_granularity_ns;
# echo "256000" > /proc/sys/kernel/sched_shares_ratelimit;
# echo "-1" > /proc/sys/kernel/sched_rt_runtime_us;
# echo "100000" > /proc/sys/kernel/sched_rt_period_us;
# echo "95000" > /proc/sys/kernel/sched_rt_runtime_us;
echo "............done";


# internet speed tweaks              上網速度的調整
echo "::         Networking Tweaks.....";

echo "30" > /proc/sys/net/ipv4/tcp_fin_timeout;
echo "1200" > /proc/sys/net/ipv4/tcp_keepalive_time;
echo "1" > /proc/sys/net/ipv4/tcp_window_scaling;
echo "1" > /proc/sys/net/ipv4/tcp_sack;
echo "30" > /proc/sys/net/ipv4/tcp_fin_timeout;
echo "30" > /proc/sys/net/ipv4/tcp_keepalive_intvl;
echo "5" > /proc/sys/net/ipv4/tcp_keepalive_probes;
echo "0" > /proc/sys/net/ipv4/tcp_timestamps;
echo "1" > /proc/sys/net/ipv4/tcp_tw_reuse;
echo "524288" > /proc/sys/net/core/wmem_max;
echo "524288" > /proc/sys/net/core/rmem_max;
echo "256960" > /proc/sys/net/core/rmem_default;
echo "256960" > /proc/sys/net/core/wmem_default;
echo "6144,87380,524288" > /proc/sys/net/ipv4/tcp_wmem;
echo "6144,87380,524288" > /proc/sys/net/ipv4/tcp_rmem;
echo "............done";

# SD Speed Increase             SD卡的存取速度調整
echo "::         SD Cache Tweaks.....";

if [ -e /sys/devices/virtual/bdi/179:0/read_ahead_kb ]
  then
    /system/xbin/echo "3072" > /sys/devices/virtual/bdi/179:0/read_ahead_kb;
fi;

if [ -e /sys/devices/virtual/bdi/179:8/read_ahead_kb ]
  then
    /system/xbin/echo "2048" > /sys/devices/virtual/bdi/179:8/read_ahead_kb;
fi;

if [ -e /sys/devices/virtual/bdi/default/read_ahead_kb ]
  then
    /system/xbin/echo "2048" > /sys/devices/virtual/bdi/default/read_ahead_kb;
fi;

echo "............done";


# I/O Scheduling              I / O調度
echo "::         noop as Default IO";

echo "noop" > /sys/block/mmcblk0/queue/scheduler;
echo "noop" > /sys/block/mmcblk0p2/queue/scheduler;
echo "noop" > /sys/block/mtdblock0/queue/scheduler;
echo "noop" > /sys/block/mtdblock1/queue/scheduler;
echo "noop" > /sys/block/mtdblock2/queue/scheduler;
echo "noop" > /sys/block/mtdblock3/queue/scheduler;
echo "noop" > /sys/block/mtdblock4/queue/scheduler;
echo "noop" > /sys/block/mtdblock5/queue/scheduler;
echo "............done";
#SetInstallLocation 1 系統設置的調整

if [ `busybox id -u` != "0" ];
then
    su -c "sh $0"
    exit
fi
sqlite3 /data/data/com.android.providers.settings/databases/settings.db 'update system set value="1" where name="set_install_location";'

 echo " Default install location set to internal (1)";


# 100_swap  虛擬內存的調整
mount -o remount,rw /data
swapoff /dev/block/loop7
dd if=/dev/zero of=/data/swap.img bs=2048 count=204800
losetup /dev/block/loop7 /data/swap.img
mkswap /dev/block/loop7
echo 40 > /proc/sys/vm/swappiness
swapon /dev/block/loop7



# IO Tweaks          IO調整
echo "::         Tweaking IO Features.....";
for i in $BLK0 $BLOCK0 $BLOCK1 $BLOCK2 $BLOCK3 $BLOCK4 $BLOCK5
do
echo "noop" > $i/queue/scheduler;
echo "0" > $i/queue/rotational;
echo "1" > $i/queue/iosched/back_seek_penalty;
echo "0" > $i/queue/iosched/slice_idle;
echo "16" > $i/queue/iosched/quantum; # default: 4 (recomended: 16)
echo "128" > $i/queue/read_ahead_kb;# default: 128; (recomended: 128)
echo "2048" > $i/queue/nr_requests;# default:128 (recomended: 2048)
echo "16384" > $i/queue/iosched/back_seek_max; # default: 16384; (recomended: 16384)
echo "1" > $i/queue/iosched/fifo_batch;

echo "............done";
# misc kernel tweaks
echo "8" > /proc/sys/vm/page-cluster;
echo "10" > /proc/sys/fs/lease-break-time;

# Flags blocks as non-rotational and increases cache size
LOOP=`ls -d /sys/block/loop*`;
RAM=`ls -d /sys/block/ram*`;
MMC=`ls -d /sys/block/mmc*`;
for j in $LOOP $RAM
do
echo "0" > $j/queue/rotational;
echo "2048" > $j/queue/read_ahead_kb;
done

# Defrags database files
for i in \
`find /data -iname "*.db"`
do \
sqlite3 $i 'VACUUM;';
done

# Remove logger
#rm /dev/log/main

# Disable normalized sleeper
echo "::         Disabling Sleeper.....";

mount -t debugfs none /sys/kernel/debug
echo NO_NORMALIZED_SLEEPER > /sys/kernel/debug/sched_features
echo ".............done";
# Disable normalized sleeper
echo "::         Disabling Sleeper.....";

mount -t debugfs none /sys/kernel/debug
echo NO_NORMALIZED_SLEEPER > /sys/kernel/debug/sched_features
echo "::         Some more RAM tweaks.....";

if [ -e /sys/module/lowmemorykiller/parameters/adj ]; then
    echo "0,1,2,4,6,15" > /sys/module/lowmemorykiller/parameters/adj
fi

if [ -e /sys/module/lowmemorykiller/parameters/minfree ]; then
    echo "2560,4096,6144,12288,14336,18432" > /sys/module/lowmemorykiller/parameters/minfree
fi

if [ -e /proc/sys/vm/dirty_expire_centisecs ]; then
    echo "1000" > /proc/sys/vm/dirty_expire_centisecs
fi

if [ -e /proc/sys/vm/dirty_writeback_centisecs ]; then
    echo "2000" > /proc/sys/vm/dirty_writeback_centisecs
fi

if [ -e /proc/sys/vm/dirty_ratio ]; then
    echo "95" > /proc/sys/vm/dirty_ratio
fi

if [ -e /proc/sys/vm/dirty_background_ratio ]; then
    echo "60" > /proc/sys/vm/dirty_background_ratio
fi

mount -o remount,noauto_da_alloc /data /data
mount -o remount,noauto_da_alloc /cache /cache

echo ".............done";

done;

 2. 對用戶自行加入的程式於開機後自動進入zipalign優化
#!/system/bin/sh
# ========================================
# init.d script
# ========================================
# Automatic ZipAlign by Wes Garner
# ZipAlign files in /data that have not been previously ZipAligned (using -c 4)

LOG_FILE=/data/zipalign.log
    if [ -e $LOG_FILE ]; then
        rm $LOG_FILE;
    fi;

echo "Starting Automatic ZipAlign $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;
    for apk in /data/app/*.apk ; do
    zipalign -c 4 $apk;
    ZIPCHECK=$?;
    if [ $ZIPCHECK -eq 1 ]; then
        echo ZipAligning $(basename $apk)  | tee -a $LOG_FILE;
        zipalign -f 4 $apk /cache/$(basename $apk);
            if [ -e /cache/$(basename $apk) ]; then
                cp -f -p /cache/$(basename $apk) $apk  | tee -a $LOG_FILE;
                rm /cache/$(basename $apk);
            else
                echo ZipAligning $(basename $apk) Failed  | tee -a $LOG_FILE;
            fi;
    else
        echo ZipAlign already completed on $apk  | tee -a $LOG_FILE;
    fi;
       done;
echo "Automatic ZipAlign finished at $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;

3.內核電壓(在這以LeeDrOiD 304-kernel示範)
 透過RE管理器進入sys/devices/system/cpu/cpu0/cpufreq
可以看見目前你所使用的內核頻率,在這需注意頻率1G以下的電壓值不可改而超過1.3G以上的電壓也不可改太低,會照成系統重開機
#!/system/bin/sh
# ========================================
# init.d script
# ========================================

sysctl -p

sync;
setprop cm.filesystem.ready 1;
#sleep 30;
echo 'c24576 24576 900' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels; 標示的是最高頻率
echo 'c61440 61440 900' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels; 標示的是最低頻率
echo 'c122880 122880 900' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;標示的是電壓值
echo 'c184320 184320 900' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c192000 192000 900' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c245000 245000 900' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c422400 422400 925' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c499200 499200 950' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c576000 576000 975' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c652800 652800 1000' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c729600 729600 1000' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c806400 806400 1025' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c883200 883200 1050' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c960000 960000 1050' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c1036800 1036800 1050' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c1113600 1113600 1075' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c1190400 1190400 1100' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c1267200 1267200 1125' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c1344000 1344000 1150' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c1420800 1420800 1175' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c1497600 1497600 1250' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c1574400 1574400 1275' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c1651200 1651200 1350' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c1728000 1728000 1375' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;
echo 'c1804800 1804800 1400' > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels;

4.校正電池的最大值,來達到更加省電
#!/system/bin/sh
# ========================================
# init.d script
# made by seo
# quick and dirty way to calibrate battery
# after fresh ROM install
# ========================================

SLEEP=120

if [ -e /data/.battery-calibrated ] ; then
        exit 0
fi

(
while : ; do
        LEVEL=$(cat /sys/class/power_supply/battery/capacity)
        CUR=$(cat /sys/class/power_supply/battery/batt_current)
        if [ "$LEVEL" == "100" ] && [ "$CUR" == "0" ] ; then
                log -p i -t battery-calibration "*** LEVEL: $LEVEL CUR: $CUR***: calibrating..."
                rm -f /data/system/batterystats.bin
                touch /data/.battery-calibrated
                exit 0
        fi
        # log -p i -t battery-calibration "*** LEVEL: $LEVEL CUR: $CUR ***: sleeping for $SLEEP s..."
        sleep $SLEEP
done
) &


沒有留言:

張貼留言