Tuesday, 31 July 2018

Every thing About Kernals

Kernals

The thing to remember about computers is that they are dumb. Incredibly dumb. They only know to do one thing and one thing only, execute instructions. One instruction after another, round and round, ad infinitum. It is only when a set of instructions is created that performs a useful task that computers, including mobile devices, take on a semblance of ability.

From the moment a computer starts it needs instructions, it can’t do anything without them. Even while your Android device is booting and showing some boot logo, the CPU is just following instructions. Once your phone is fully booted you get Android. What we see of Android is a nice launcher, some apps like the Chrome web browser and maybe some games. But there is more going on than meets the eye.Under the user interface there are a bunch of different systems including the Java virtual machine (i.e. Dalvik or ART) and the SurfaceFlinger, which is responsible for compositing all the different things that needs to be drawn into a single buffer that is then displayed on the screen.

Go down deeper, really deep – beyond the permafrost, and you reach the kernel, the Linux kernel to be precise. All multi-tasking operating systems have a kernel of one form or another. Windows has a kernel, OS X has a kernel, iOS has a kernel, Windows Phone has a kernel, and of course Android has a kernel. But of those only Android uses the Linux kernel. Windows and Windows Phone/Mobile have their own kernel often referred to as the NT kernel, while OS X and iOS use a kernel known as Darwin.

There are other kernels out there including Unix-like kernels from the FreeBSD project or the NetBSD project; real-time kernels from projects like FreeRTOS; embedded kernels from projects like Contiki; and even low power kernels like the mbed OS kernel from ARM. What this means is that any computing device from an IoT thing or wearable right up to a supercomputer uses a kernel.

That means that when you start an app it is the kernel which loads the app into memory, creates the processes needed and starts the app running. When app needs memory it is the kernel that allocates it. When the app wants networking it is the kernel which does all the low level processing. The driver for devices like Bluetooth are also in the kernel. When the app wants to perform a task in the background it is the kernel which handles the background threads. When the app closes it is the kernel which cleans up all the memory and other resources that were used by the app.

As you can imagine the kernel is a fairly complex piece of software. The Linux kernel is thought to be over 15 million lines of source code. That includes all the drivers (over 70% of the code) plus the support for the different system architectures (ARM, x86, MIPS, IBM, PowerPC, SPARC, etc). When the kernel is built for a particular device, say a smartphone, not all those 15 million lines of code are used, however even when you strip away what isn’t needed for a particular build there is still a lot of code.

Monolithic vs microkernel

As with all complex systems, there are different approaches that can be used when it comes to designing a kernel. The Linux kernel is what is known as a monolithic kernel. That means that the kernel is one program using one memory space. The main alternative is the microkernel approach. With microkernels the essentials of the kernel are placed in the smallest possible program and it interacts with other kernel level programs which run as separate servers or services.

Back in 1992 when Linux was in its early days, Linus Torvalds and Professor Andrew Tanenbaum (who is famous for his books on operating system design and networking) had an online discussion (some say a flame war) about the different merits of monolithic kernel designs versus microkernels. Tanenbaum preferred microkernels and Linus was writing a monolithic kernel. That is now all history as Linux has remained a monolithic kernel and it is the kernel used in Android. If you are interested in a Unix-like microkernel operating system then you should check out Minix 3.

Since Linux is a monolithic kernel there needs to be a way to enable and disable certain parts of the kernel depending on your needs. This is done at compile time using a system that allows the kernel to be tuned, trimmed and configured as needed. Some of the configurations do more than just activate or deactivate certain functions, they actually change the behavior of the kernel. Since Linux is open source and Android is open source it is possible to build a version of Android with a tweaked kernel, one that uses different settings than the default kernel. This kernel can then be copied on to your Android device instead of the default kernel. To do this you will probably need a phone with an unlocked bootloader and your device needs to be rooted.

Probably the most well known alternative kernel for Android is the Franco Kernel. It is available for the different Nexus devices (including the Galaxy Nexus and the Nexus 4) and there is even a app to make using the kernel as easy as possible. However the Franco Kernel isn’t the only alternative kernel, there are others including the ElementalX Kernel, the Jolla kernel, and many others.

Pros and cons?

First the downsides, to put a new kernel on your Android device you are going to need root access. Some people are very comfortable with having root access on their devices, others aren’t. It also implies a certain level of technical expertise. Putting on a new kernel isn’t within everyone’s skill set.

Also you need to trust the kernel builder. Probably, hopefully, not because there could be nefarious code included in the new kernel build, but more in terms of how reliable the kernel builder is in terms of fixing bugs. There is also the danger of “bricking” your device. When you get an Android smartphone from a reliable OEM then the firmware has had lots of testing before it is released. If you start playing with new kernels you could find yourself in the position where the kernel isn’t working properly and the kernel builder isn’t responding to bugs requests or questions. Other than a bricked phone, the answer will be to install a different kernel.

It is also worth noting that rooting your phone and using a new kernel will invalidate your warranty. Most of the kernels released on forums like XDA carry a warning: “Your warranty is now void. I am not responsible for bricked devices.” Some even go on to say things like this, “YOU are choosing to make these modifications, and if you point the finger at me for messing up your device, I will laugh at you.” So you have been warned.

On the plus side you get a kernel that is highly configurable, you will be able to play with a whole host of different governors and I/O schedulers, plus these custom built kernels tend to have a range of extra features. It also means that you can get access to more recent versions of the Linux kernel.

Anecdotally custom built kernels can improve battery life or improve performance depending on how the kernel builder has tweaked the settings and depending on your usage patterns. But as I have mentioned before, all the major OEMs (including Google) are competing with each (and with Apple and Microsoft to some extent) in an attempt to gain customers by offering the best smartphones at the best prices. If it was possible to get better battery life or better performance just by tweaking a few kernel parameters then you can rest assured that they would! Generally a boost in battery life means lower performance, or a boost in performance means lower battery life, the goal is to strike that happy medium where performance is good, but so is battery life.