Wednesday, February 23, 2011

Experimental Mods

UPDATE: v4.2e is now deprecated - please download the latest instead.

I've been trying out some crazy minfree settings and I have a set that seems to work fantastic for me, so here they are:
For those who are comfortable modifying init files directly, you just need to change minfree values in 99imoseyon to 15000,20000,20000,20000,20000,25000, and reboot.  For others, just flash the zip over the previous version.

Give it a try for a day or two and let me know what you guys think.  Comment here on any changes you notice in speed, battery life, FCs, etc.

Monday, February 21, 2011

Soft Reboot (quick reboot)

I talked a bit about dropping caches to free up memory and jumpstart the kernel caching system a few times in my previous posts.  I think occasionally some of us would like to clean things up even more to keep the phone running smoother (ie. maybe you feel that the phone is running really sluggish).  One way to possibly accomplish this is to do a soft reboot.

Soft reboot would essentially kill all running processes and restart the essential services.  Anything at the OS layer or below, namely bootloader, kernel and init would not be restarted.  So you're rebooting your phone without touching the OS/bootloader, and all within just a matter of seconds.

Here's what you do to cause a soft reboot:
pkill zygote
That's it.  Zygote is an android service that initializes Dalvik and rest of the system.  By killing it, you basically kill all apps registered with zygote and restarts essential services including zygote.  (At least that's how I understand it haha).

If you've been reading my blog, you probably know all about crond on android.  You could potentially use crond to schedule a soft reboot once a week.  Your crontab file may look something like this:

# drop kernel caches at 3AM every morning
0 3 * * * sync; echo 3 > /proc/sys/vm/drop_caches
# soft reboot system at 2AM monday morning
0 2 * * 1 pkill zygote

There's one slight problem with this method. This soft reboot is more or less based on my theory and I have no idea what the long term effect would be if you continue to use this method.  Use it at your own risk! :)

Friday, February 11, 2011

I Got a ZIP (imoseyon mods)

- v5.0 universal zip (this post is now deprecated - please go to the lastest post)

Due to popular requests, I decided to make a zip with most of my tweaks.  Applying the zip below will:
  1. Installs busybox 1.18.0 (in an alternate location).
  2. Enables Cron.
  3. Tweaks kernel stuff (vm, minfree, etc.).
  4. Tweaks build.prop (on some ROMs).
  5. Flushes system caches once a day (3AM PT).
  6. Disables logger
  7. Enables new governer.
  8. and more (in the change logs below).
This zip should be all you need to get your phone running in tip top shape.  More specifically, it will hopefully make your phone run faster, longer, and more stable.  I think a good majority of you will notice the difference, and some will not.  I have different versions of the zip for various custom ROMS (rubix, liberty, darkslide, etc. and maybe even stock ROM) - I hope to make a single unified zip that works for all ROMS soon.   It should also work for both Droid X and Droid 2.

Here's how you apply the zip (this is standard stuff):
  1. Back up your ROM! (Some of the stuff in the zip are experimental, and I don't test every release for every ROM, so make sure you have a nandroid backup to go back to!)
  2. Boot into Bootstrapper/clockworkmod.
  3. Install zip.
  4. Reboot.
To verify that the zip was applied successfully, you can run this command from Terminal Emulator:
pgrep -f crond
If you get a number, then you're all good. (The number just means that the process is running in the background, nothing more.)
    Warning: Flashing this zip will override some of the kernel tweaks you may have made with built-in tool provided by your ROM (ie. liberty toolbox).
    • v3.1: Fixed busybox/crond issue. Separated DX and D2 files - duh. 
    • v3.2: Fixed wget. 
    • v3.21 (D2 ONLY): Fixed crond start bug. 
    • v3.3 (DX/D2), V2.0 (Apex): busybox 1.18.0, made minfree more aggressive for secondary apps (may reduce occasional sluggishness), prettier "ls", got rid of scheduler tweaks which did nothing.
    • v4.0:
      • Timezone semi-auto detection for cron (US only).
      • Additional minfree tweaks to make memory management a bit more aggressive to prevent some reboots and freezes.
      • Added bash.
      • Added additional cron job examples (backup files including sms datase, killapps - all disabled  by default).
      • Liberty version has call delay and proximity delay slightly tweaked per Jreguit. 
      • Purge logs - view /tmp/imosey to see what kind of effects nightly purging has.
    • v4.1:
      • Changed the purge time from 1AM to 3AM.
      • Fixed a bug in timezone detection (doh!)
      • Disabled logcat at boot for extra performance, battery, etc - hopefully. (comment in 99imoseyon to disable)
      • Added zipalign for system apps at boot. (comment in 99imoseyon to disable)
      • Separated crond startup to a separate script for future proofing.
      • Added another cron job example in /data/cron/root for soft reboot, and added explanation for each cron job.
      • TODO: test the others version for APEX.
    • v4.2:
      • Disabled zipalign at boot - should fix the no service problem.  Sorry about that! 
    • v4.3:
      • Fixed a major bug in the v4.2 of "others", where if you were installing the zip for the first time the mods weren't being applied at all! (sorry)
      • Incorporated the minfree tweak from my experimental version.
      • Interactive governer for speed! (thx to drod via jrummy via milestone, etc.)

      Wednesday, February 9, 2011

      sysctl (and minfree) tweaks revisited

      (This is a follow up to my first post).

      First of all, I believe tweaking minfree is just as important, if not more, than sysctl.  Android tends to behave badly when there's very little free memory.  When you're running your device with say less than 20-30MB of free RAM, a misbehaving app or two could invoke kernel's OOM killer and cause the device to become unstable, then eventually reboot or freeze.

      For that reason, I don't like the default value of 25MB minfree.  (minfree, btw, is android's memory management system).  From my experience, I think at least 80MB minfree is required for your device to run fast and stable.  In general I recommend Rubix's minfree setting (20480 blocks x 4 will give you 80MB): 

      echo "2048,3072,6144,15360,17920,20480"  > /sys/module/lowmemorykiller/parameters/minfree

      I personally run my Droid X with the following: 

      echo "256,512,1024,5120,13000,20000" > /sys/module/lowmemorykiller/parameters/minfree

      Both should be fine.  Be sure that the last two numbers are at least 13000 and 20000.

      Now back to sysctl.  Personally, I haven't had to manually drop caches at all.  In fact, my phone will probably run fine indefinitely without having to purge (longest i've run it without rebooting was 5 days, but it's really hard not to reboot your phone when you tweak it as much as i do - the kernel will automatically drop caches when it think it needs more memory). But I'm not a heavy user (i don't play games),  your mileage will vary of course, and you may need to purge every once in while.   To purge page, dentry and inode caches, use this command:
      sync; echo 3 > /proc/sys/vm/drop_caches
      My personal recommendation for most people (no manual flushing should be required):
      vm.dirty_ratio = 90
      vm.dirty_background_ratio = 55
      vm.vfs_cache_pressure = 20
      If you are willing to play around with manual or timed flushing I think you can squeeze a little more performance and battery life with:
      vm.dirty_ratio = 90
      vm.dirty_background_ratio = 70
      vm.vfs_cache_pressure = 1
      Btw, you can schedule cache purge using Tasker/sl4a, or you can use cron.

      Tuesday, February 8, 2011

      cron on android is awesome

      Cron is a popular time based job scheduler on Unix/Linux, and is super useful as you can virtually run any command or script periodically at certain times or dates.

      Even though Android is based on linux, it does not run crond (cron daemon) by default, well at least not on my Droid X.  Fortunately, you can run crond with busybox with a few hacks.  It's not the prettiest hack but it works.  Here's how you do it:
      1. Make sure that your busybox supports crond.  Try typing "crond" in Terminal and see what it says.  I run busybox v1.17.1.
      2. Run your favorite command line tool telnet, Terminal, adb, etc. to run the commands below.
      3. Remount / and /system rw.
      4. Create a /etc/passwd. (crond calls getpwnam to search user db)
        echo "root:x:0:0::/data/cron:/system/bin/bash" > /etc/passwd
      5. Create a symlink for /system/bin in /bin.
        ln -s /system/bin/ /bin
      6. Create your crontab file, and call it "root" (I created /data/cron/root).  Check the example section in this article to see what a crontab file should look like:
      7. Set your timezone.  I live in California so mine looks like this (apparently the new Olson style timezones do not work):
        export TZ
      8. Run crond:
        crond -c /data/cron
      9. To verify that crond is running succesfully run "pgrep crond".  If you get a number (PID) then you know it's running.
      Obviously you don't want to do the above steps every time your phone boots, so you want to create an init.d script with the above commands to run at boot, or add the commands to the end of an existing init.d script.  I've added them at the end of my init.d script called /etc/init.d/99imoseyon:
      # enable crond
      # crond calls getpwnam (user database search)
      mount -o remount,rw -t yaffs2 `grep /system /proc/mounts | cut -d' ' -f1` /system
      echo "root:x:0:0::/data/cron:/system/bin/bash" > /etc/passwd
      mount -o remount,ro -t yaffs2 `grep /system /proc/mounts | cut -d' ' -f1` /system
      # crond has "/bin/sh" hardcoded
      mount -o remount,rw rootfs /
      ln -s /system/bin/ /bin
      mount -o remount,ro rootfs /

      # set timezone
      export TZ
      # use /data/cron, call the crontab file "root"
      crond -c /data/cron
      So what can we use it for?  Lots of things.  I'm going to start using it to periodically drop page/filesystem caches, back up SMS database nightly, kill memory resident apps hourly, etc.  There are literally hundreds of use cases for android phones. Here's a sample crontab file (backups my init.d script at 8pm every night) at /data/cron/root:

      0 20 * * * cp /system/etc/init.d/99imoseyon /sdcard/data/init.d
      UPDATE: Added timezone step to the instructions.  Also added a command to verify that crond is running.

      Saturday, February 5, 2011

      Differences between rubix and liberty

      If you're a ROM whore like me, you're constantly switching between ROMs and not loyal to a single ROM. However, my two favorites in the recent past has been rubix and liberty. I like the features of liberty and the smoothness of rubix. I believe you can have both if you're handy with vi and command line. Here are the differences at the OS level. (I could explain what each does but that would take too long).
      • build.prop ("-" is rubix, and "+" is liberty) :
      -wifi.supplicant_scan_interval = 180
      +wifi.supplicant_scan_interval = 90
      • cpufreq ondemand:
      liberty: 86, rubix: 95
      liberty: 46875, rubix: 37500
      • minfree (/sys/module/lowmemorykiller/parameters/minfree):
      liberty: 1536,2048,4096,5120,5632,6144
      rubix: 2048,3072,6144,15360,17920,20480
      • kernel scheduler (/sys/kernel/debug/sched_feature):

      • And finally, sysctl.conf:
      Liberty (yikes):
      vm.dirty_background_ratio = 5
      vm.dirty_ratio = 20
      vm.dirty_background_ratio = 60
      vm.dirty_ratio =95
      Overall, Rubix tends to tune out of the box whereas Liberty settings are more or less stock and you'll have to tune them via the toolbox.  Or if you're like me, you tune them manually. :)  (Oh fyi - liberty will run quicker after applying some or all of the changes from rubix above.)

      Below will make things easier if you want to try it out yourself.  Make sure you have a backup!