Tuesday, January 25, 2011

Enabling sysctl

First of all, I'd like to thank all the ROM developers for making my phone so much fun to play with.  Without them it will be like having an iphone. :)

Secondly, I strongly suggest enabling telnetd.  It makes typing (and copy/pasting) so much easier, and you can also mess with the phone while your son is playing Angry Birds on it!

Enabling sysctl on custom ROMs is mostly pretty easy.  Roughly the steps are:
  1. Verify you can run custom scripts at boot.
  2. Check to see if sysctl is already enabled.
  3. Create/modify /etc/sysctl.conf
  4. Verify that the new values stick at reboot.
BTW, if your ROM already allows you to enable/modify sysctl via settings, you can skip 1-2.
    Let's start with #1.  First open up Terminal and/or telnet into your phone.  Then verify that you got an init.d directory on the phone (I'm currently running Rubix):
    # ls -l /etc/init.d
    -r-xr-xr-x root     root          759 2008-08-01 05:00 01cpu
    -r-xr-xr-x root     root          230 2008-08-01 05:00 02vm
    -r-xr-xr-x root     root          228 2008-08-01 05:00 03pfm
    -r-xr-xr-x root     root          100 2008-08-01 05:00 04screen
    -r-xr-xr-x root     root           72 2008-08-01 05:00 05zipalign
    -r-xr-xr-x root     root          344 2008-08-01 05:00 98firstboot
    -r-xr-xr-x root     root          233 2008-08-01 05:00 99complete
    Oh look, it looks like I can run some custom scripts at boot time.  If /etc/init.d doesn't exist, stop.  This gets messy and honestly I wouldn't recommend going further because you could potentially bootloop your phone if you try to modify anything other than what's in this post.

    #2. At first glance I don't see an init script named sysctl like the Liberty Rom, but it's possible that one of the scripts takes care of it:
    # grep sysctl /etc/init.d/*
    /etc/init.d/02vm:# Load /sys/etc/sysctl.conf
    /etc/init.d/02vm:sysctl -p
     Yup, 02vm file enables sysctl.  "sysctl -p" takes what's already in /etc/sysctl.conf and applies it to the kernel.  If you don't see any init script running sysctl -p, then you'll want to create one.  Call it something like 99sysctl, and then simply add "sysctl -p" in the file.

    #3.  Modify /etc/sysctl.conf.  Here's what my latest looks like:
    # cat /etc/sysctl.conf
    # shouldn't matter - no swap is used
    vm.swappiness = 0

    # try to keep at least 4MB in memory
    vm.min_free_kbytes = 4096

    # favor block cache
    vm.dirty_ratio = 90
    vm.dirty_background_ratio = 55

    # extremely favor file cache
    vm.vfs_cache_pressure = 1

    # reboot when OOM happens
    vm.panic_on_oom = 2

    # wait 5 sec before rebooting when OOM
    kernel.panic = 5
     #4. Verify that the sysctl init script is working.  Reboot the phone and run this command:
    # sysctl -a | grep vm  
     "sysctl -a" will display all the kernel settings, and what you're interested in is the section that you modified, hence "grep vm".  The output should look something like this:
    # sysctl -a | grep vm     
    vm.overcommit_memory = 1
    vm.panic_on_oom = 2
    vm.oom_kill_allocating_task = 0
    vm.oom_dump_tasks = 0
    vm.overcommit_ratio = 50
    vm.page-cluster = 3
    vm.dirty_background_ratio = 55
    vm.dirty_background_bytes = 0
    vm.dirty_ratio = 90
    vm.dirty_bytes = 0
    vm.dirty_writeback_centisecs = 500
    vm.dirty_expire_centisecs = 200
    vm.nr_pdflush_threads = 0
    vm.swappiness = 0
    vm.lowmem_reserve_ratio = 32
    vm.drop_caches = 0
    vm.min_free_kbytes = 4096
    vm.min_free_order_shift = 4
    vm.percpu_pagelist_fraction = 0
    vm.max_map_count = 65530
    vm.laptop_mode = 0
    vm.block_dump = 0
    vm.vfs_cache_pressure = 1
    vm.mmap_min_addr = 4096
    vm.scan_unevictable_pages = 0
     There you can see that the values in your customized /etc/sysctl.conf have taken effect.  Great success!


    1. I've implemented sysctl by adding sysctl -p to the init.post_boot.sh script that is called by my ROMs init.rc. All values that I set using sysctl.conf take effect, except for vm.dirty_ratio. No matter what I do, after rebooting, dirty_ratio = 0.

      I'm on a Thunderbold running Redemption ROM. I've done a grep through all the files in the ROM and only see dirty_ratio referenced in the /system/bin/vold binary - appears to be just error messages.

      Any ideas on what might be resetting dirty_ratio to 0?