VDD, smartass, and cpufreq sysfs interfaces and how to use them.

Since I keep getting asked how to use the sysfs interfaces I have built into my kernel, I decided to do post on just those items. If you would like a more detailed explanation of sysfs, refer to http://en.wikipedia.org/wiki/Sysfs.

I’ve done a write-up on the VDD, smartass, and cpufreq sysfs interfaces. I also included a brief how-to for creating init scripts to make these changes on boot. All of these examples can be done via an adb shell. I’ve been using a ‘Nix of some kind since the early 90’s, so I’m very comfortable in a command line only enviroment. I still recommend trying these commands from the command line first, it’s how I test overclocking and undervolting, that way if something does not work, and the phone reboots, everything goes back to how it was.

Also, if there are any Android Java developers that would like to make an app automate these tasks for the standard users, I’d be more than willing to help with that. I’m no longer a Java developer, and have no intention of touching it again, I learned to hate it in the late 90’s on Sun Sparc’s and have no desire to go back. I’ll always be more comfortable in plain old C or maybe perl, you have to love a language that allows you to do this:
printf $a > $b ? “True” : “False”;

VDD sysfs usage:

The sysfs VDD interface for Static Voltage Scaling, SVS, uses /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels. The sysfs VDD interface for Hybrid Adaptive Voltage Scaling, HAVS, uses /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels_havs. The first was implemented by sqn in his original patch, however the second was implement by myself so that I could have HAVS and a sysfs interface for VDD levels. Both files are similar in usage and function, below are examples of each.

To list the current values:

cat /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels
cat /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels_havs

To change ALL voltages either up (+) or down (-), must be increments of 25mV:

echo "+25" > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels
echo "+25" > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels_havs
echo "-25" > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels
echo "-25" > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels_havs

To change just one frequency, you specify the frequency and the voltage, or for HAVS, the frequency and the voltage range:

echo "998400 1100" > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels
echo "998400 1050 1100" > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels_havs

Smartass sysfs usage:

The original smartass patch that I found did not have a working sysfs interface, there were slight issues with it, so I decided to fix it, since it’s very useful to have. All smartass control files are located at /sys/devices/system/cpu/cpu0/cpufreq/smartass.

The following is a list of each file, and it’s description:

down_rate_us
The minimum amount of time to spend at a frequency before we can ramp down, default is 45ms.

up_min_freq
When ramping up frequency with no idle cycles jump to at least this frequency. Zero disables. Set a very high value to jump to policy max freqeuncy.

sleep_max_freq
When sleep_max_freq>0 the frequency when suspended will be capped by this frequency. Also will wake up at max frequency of policy to minimize wakeup issues.

sleep_wakeup_freq
The frequency to set when waking up from sleep. When sleep_max_freq=0 this will have no effect.

sample_rate_jiffies
Sampling rate, I highly recommend to leave it at 2.

ramp_up_step
Freqeuncy delta when ramping up. Zero disables causes to always jump straight to max frequency.

max_ramp_down
Max freqeuncy delta when ramping down. zero disables.

max_cpu_load
CPU freq will be increased if measured load > max_cpu_load.

min_cpu_load
CPU freq will be decreased if measured load < min_cpu_load.

Current smartass defaults set in my kernels:

down_rate_us=20
max_cpu_load=75
max_ramp_down=38400
min_cpu_load=50
ramp_up_step=38400
sample_rate_jiffies=2
sleep_max=245760
sleep_wakeup_freq=998400
up_min_freq=9999999

To read the current values (max_cpu_load as an example):

cat /sys/devices/system/cpu/cpu0/cpufreq/smartass/max_cpu_load

To set the current values (max_cpu_load as an example):

echo "80" > /sys/devices/system/cpu/cpu0/cpufreq/smartass/max_cpu_load

Controlling the current cpufreq policy, i.e., How to Over/Under clock without SetCPU:

There are also control files associated with the CPU frequency, cpufreq, driver located at /sys/devices/system/cpu/cpu0/cpufreq.

The following is a list of the important files and their descriptions:

scaling_available_frequencies (Read-Only)
Lists all available frequencies that the processor is programmed to handle. This doesn’t mean that all will work, just that it’s possible to tell the processor to attempt to use them.

scaling_available_governors (Read-Only)
Lists all available governors available for usage.

scaling_cur_freq (Read-Only)
Lists the current operating frequency of the processor.

scaling_governor (Read-Write)
Gets/Sets the currently used governor.

scaling_max_freq
Gets/Sets the maximum allowed frequency for the current policy, refer to scaling_available_governors for valid frequencies.

scaling_min_freq
Gets/Sets the minimum allowed frequency for the current policy, refer to scaling_available_governors for valid frequencies.

To read the currently available frequencies:

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors

To set the current governor to smartass:

echo "smartass" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

To change the maximum frequency:

echo "1536000" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq

How to make the settings take effect after a reboot:

The problem with using the sysfs interface for the preding is that it is only current as long as you do not reboot the device. This is a good thing if your attempting to overclock your device, and it locks up, as soon as it reboots, it goes back to the defaults until something tells it differently. In order to make these changes take effect after a reboot, you need to use an init script. If your current ROM has run-parts enabled, which I believe most do anymore, then the following will work. You also need to make sure that the init scripts that you put under /system/etc/init.d are executable, i.e., chmod 755 filename. Also, in order to save these files to /system/etc/init.d, /system needs to be mounted as read-write.

The following example init script is for undervolting the lowest frequency on a ThunderBolt with SVS, others are similar. This file needs to be located in /system/etc/init.d. Example filename would be 99vddlevels.

#!/system/bin/sh
echo "245760 850" > /sys/devices/system/cpu/cpu0/cpufreq/vdd_levels

The following example would set the policies maximum frequency to 1.536GHz. Again, this file needs to be located in /system/etc/init.d, and for this example, is named 99overclock.

#!/system/bin/sh
echo "1536000" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq

Now for some fun stuff. If you would like to setup an init script to allow you to change the policy while the screen is on or off, there are a couple other sysfs files that are going to be of interest you. Those two files are /sys/power/wait_for_fb_wake and /sys/power/wait_for_fb_sleep. These are considered blocking files, in other words, if you attempt to cat the file, display it’s contents, it will not display until a condition is reached. The condition for wait_for_fb_wake, you guessed it, the screen wakes up. When the screen wakes up, cat /sys/power/wait_for_fb_wake will return awake. Same holds true for wait_for_fb_sleep, when the screen turns off, it returns sleeping. Knowing this will now allow us to create a script to deal with different states of the screen.

The following script will change the governor, minimum and maximum frequencies. When this script launches via run-parts, it will remain running until it is either killed or the device reboots. Note, if you decide to change the values after using the script for at least one boot, make sure to reboot for the changes to take effect, either that or kill off the process and restart it.

#!/system/bin/bash

AWAKE_GOVERNOR="performance"
AWAKE_GOVERNOR_FREQENCY_MAX="1536000"
AWAKE_GOVERNOR_FREQENCY_MIN="245760"

SLEEP_GOVERNOR="ondemand"
SLEEP_GOVERNOR_FREQENCY_MAX="368640"
SLEEP_GOVERNOR_FREQENCY_MIN="245760"

(while [ 1 ]
do
AWAKE=`cat /sys/power/wait_for_fb_wake`
if [ $AWAKE = "awake" ]; then
echo $AWAKE_GOVERNOR > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
echo $AWAKE_GOVERNOR_FREQENCY_MAX > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
echo $AWAKE_GOVERNOR_FREQENCY_MIN > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
AWAKE=
fi
SLEEPING=`cat /sys/power/wait_for_fb_sleep`
if [ $SLEEPING = "sleeping" ]; then
echo $SLEEP_GOVERNOR > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
echo $SLEEP_GOVERNOR_FREQENCY_MAX > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
echo $SLEEP_GOVERNOR_FREQENCY_MIN > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
SLEEPING=
fi
done &)

14 Comments

  1. Comment by Thunderbolt_Fan:

    Hey Ziggy, they closed the thread at Xda.com, are you going to start another one? I know you weren’t the OP of that one, but I’d love to see you open that one to keep up the support for your kernels because everyone loved them and not everyone knows about your site. Thanks!

    Btw, AMAZING work on the BFS kernel, LOVE it! Thanks again!

  2. Comment by yardi4life:

    Thanks for this. Really, really appreciate it.

  3. Comment by GPz1100:

    Ziggy, what was the source for the smartass parameter definitions? The text looks awfully familiar.

  4. Comment by Joe V:

    Where can I find the CIFS module for the Thunderbolt kernel. I want to mount shared Windows folders.

  5. Comment by cabagekiller:

    Can you release your source, so others can see how you accomplished this nice kernel and see if they can improve upon it somehow?

  6. Comment by Tim85:

    Hey thanks for this write up. This is definitely very helpful

  7. Comment by ffolkes:

    Thanks for this, I found it very informative, but I have a few questions I’d really appreciate an answer to. With HAVS, how is the actual voltage determined? In a real AVS system, isn’t there a physical component (separate IC, or maybe something on-die in a SOC) that “supervises” the processor like a parent, adjusting the voltage accordingly? Regardless, my main question is how do I know, or at least how can I infer, what voltage the processor is running at within the given range? For example, if I have instability at a given frequency, how do I know if that’s stemming from the bottom limit or the top limit being set too low?

  8. Ping from [Kernel] Ziggy471 MECHA BETA Kernel 22 Apr 11 - PPCGeeks:

    […] like to change the voltages, or use sysfs for what it's intended, I have a nice write-up on my site here. I explain in almost to much detail how to use the sysfs inerface to control the voltage levels, […]

  9. Ping from ★ ☆ ★| ROM | SynergyROM | NIGHTLYS | 2.3.4 | SENSE 3.0 | ★ ☆ ★ - Android Forums:

    […] how… Well, if you want an easy way to do it, I would recommend SetCPU. Theres a writeup here Ziggy471.com | VDD, smartass, and cpufreq sysfs interfaces and how to use them. done by Ziggy. I run Dcofig on equal OC'd to 1.2 on lagfree or smartass and never see lag. If you […]

  10. Ping from RCMix3d Runny v1.2 - Pagina 61 - Forum Android Italiano:

    […] governor e frequenza della cpu,cosa sia e quali vantaggi apporti non lo so comunque leggi qui Ziggy471.com | VDD, smartass, and cpufreq sysfs interfaces and how to use them. __________________ Rom: RCMix runny Kernel: Unity v_7 Radio […]

  11. Ping from forum:

    forum…

    […]Ziggy471.com | VDD, smartass, and cpufreq sysfs interfaces and how to use them.[…]…

  12. Comment by chaos:

    If you lazy, and you don’t wish to do this through terminal, you can use a text editor. When you cat a file you are just echoing it to the screen, and when you echo with > you are just over writing the file with what e er you echo. Also if you over write the available freq’s file with a 0 it will disable all crews outside the range you set.

Leave a Reply