Adding features to your kernel

Adding features to your kernel

The kernel is probably one of the most important parts of the Android system, the whole system relies on the kernel, the performance, stability, smoothness, etc.
Considering the kernel as the most important part, you want it to be optimized, making your system run smooth and performing above standards, also the battery life may not suffer when gaining these improvements. In this tutorial I will explain how to add CPU Governors,  how to add I/O Schedulers and a global sight on how to add Overclocking support for your kernel.

 

Chapters:
1. Github
2. Governors
3. I/O Schedulers
4. Overclocking

 

Chapter 1. Github
I will not explain how to use Github in general, also I’m not trying to advertise for Github. I just want to say that you really “need” to use Github in order to get a clear overview of the changes you made.
Believe me, without a commit system and thousands of changes in your source, you will definitely forget what you actually did to the source.
Github helps you keeping track of the changes you made. Easily done by commits added by the developer.

If you want to build a (custom) kernel form source and you want to customize it, you must use Github! Not just for the overview of changes but also to make sure you don’t  violate the GPL law!
Since the android kernels are open source and protected by the GPL law, you need to share your source with the world. (same thing counts for manufacturers!)

Enough about Github, let’s start with customizing.

 

Chapter 2. Governors
What is a governor, why is it important to have custom governors and how to add governors to your kernel? These 3 questions are very important to know.

What is a governor:  A governor is actually nothing more then a behavior profile for your CPU, the governor will tell the CPU exactly what to do in what situation. The term ‘governor’ has nothing to do with it’s function, imagine it was called CPU Presidents! sounds strange but it’s as normal as using the word Governor.

Why is it important to have custom governors: The default governors added to kernels are “ondemand, powersave, performance, conservative” and optionally “userspace”, all these governors are pretty basic and not optimized for usage/battery whatsoever. a list of default governors with a small description:

– ondemand, the overall default CPU governor worldwide. Basically just ramps up your CPU speed to max freq when just pushing the screen once, then lowering it back to the minimal cpu freq (somewhat smart, but not optimized at all).
– performance just stays at the max CPU freq all the time (horrible for your battery).
– powersave stays at the min CPU freq all the time (resulting over the top lag).
– conservative is somewhat adjusted to use less power, but that’s also very notable in performance.
– userspace is a governor that needs to be completely configured manually, the default setting is the same as powersave, it will stay at your min CPU freq when not configured. (this governor is rarely used)

Because the stock CPU Governors are just plain boring and do not improve your device whatsoever, you want to add custom CPU Governors, making your device smoother, more stable and less battery consuming.

How to add a CPU Governor:
The CPU governor itself is just 1 C file, which is located in  kernel_source/drivers/cpufreq/,  for example: cpufreq_smartass2.c. You are responsible yourself for find the governor (look in an existing kernel repo for your device)
But in order to successfully call and compile this file into your kernel you will have to make the following changes:

1. Copy your governor file (cpufreq_govname.c) and browse to  kernel_source/drivers/cpufreq, now paste it.
2.  and open Kconfig (this is the interface of the config menu layout) when adding a kernel, you want it to show up in your config. You can do that by adding the choice of governor.

for example, for smartassV2.

 

next to adding the choice, you also must declare the possibility that the governor gets chosen as default governor.

for example, for smartassV2.

– can’t find the right place to put it? Just search for  “CPU_FREQ_GOV_CONSERVATIVE”,  and place the code beneath, same thing counts for “CPU_FREQ_DEFAULT_GOV_CONSERVATIVE”

Now that Kconfig is finished you can save and close the file.
3. While still in the /drivers/cpufreq folder, open Makefile.
In Makefile, add the line corresponding to your CPU Governor. for example:

Be ware that you do not call the native C file, but the O file! which is the compiled C file. Save the file.
4. Move to:  kernel_source/includes/linux.  now open cpufreq.h
Scroll down until you see something like:

(other cpu governors are also listed there)

Now add your entry with the selected CPU Governor, example:

Save the file and close it.
The initial CPU Governor setup is now complete. when you’ve done all steps successfully, you should be able to choose your governor from the menu (menuconfig, xconfig, gconfig, nconfig). Once checked in the menu it will be included to the kernel.

Commit that is nearly the same as above instructions:  “add smartassV2 and lulzactive governor commit

 

 

Chapter 3.  I/O Schedulers
Next to adding custom CPU governors, you can also enhance your kernel by adding new I/O Schedulers if needed. Globally Governors and Schedulers are the same, they both provide a way how the system should work. but on Schedulers it’s all about the Input/Output datastream except the CPU settings. the I/O Schedulers decide how the upcoming I/O activity is being scheduled.
The standard schedulers such as “noop” or “cfq” are pretty decent actually. They perform very reasonable.

Anyways, you can add more I/O Schedulers if you like to, think of “deadline/sio/row/vr”  for example
I will explain how to add a I/O Scheduler, it’s just a bit easier then adding a CPU Governor.

I/O Schedulers can be found in kernel_source/block.

1. Copy your I/O Scheduler file (example sio-iosched.c)  and browse to kernel_source/block, now paste it.
2. Now open Kconfig.iosched, and add the choice to the Kconfig, example for SIO

3. and the default choice option:

Save the file, and move on to the Makefile of kernel_source/block/
4. Simply add this line, for SIO:

Save the file and you’re done! The I/O Schedulers should now popup at the menu config.

Commit that is nearly the same as above instructions:  “add simple I/O scheduler commit
Chapter 4. Adding Overclocking
Unfortunately there is no solution ALL-IN-ONE solution available regarding overclocking, every device has a different OC, making it very important to use the right one!

The target file depends on your chipset type and chipset revision.
for example: on the Galaxy S Plus (which has a QualComm MSM Chipset, (msm7x30) the target file would be:

kernel_source/arch/arm/mach-msm/acpuclock-7×30.c

Once you got your acpuclock file, it’s time to take a look at how overclocking actually works.
Basically overclocking is nothing more then extending an array of frequencies and matching voltages.

Example show’n below is only an example, please find existing kernel with OC for your phone on Github, and study the changes that are made to OC the kernel!

Stock Settings:

Along with:

 

OC Settings:

along with:

Handle the CPU with care! Keep in mention that you’re playing with the voltages of your phone. Only raise the voltages by 25 max per try. When your phone doesn’t get enough power when requested it will turn off.
No big deal luckily, but a WARNING to you that you shouldn’t raise the voltage anymore (in the matter of fact you need to lower it by 25 when your device goes off)