How to Boost Performance on Android by Tweaking KTS Part A

How to Boost Performance on Android by Tweaking KTS Part A

How to Boost Performance on Android by Tweaking KTS Part A

First of all the Android users know clearly that Adroid Operating System abounds in much custom configuration. This is only possible by the usage of Linux Kernel at its top. This tutorial "How to Boost Performance on Android by Tweaking KTS Part A".

we are going to analyze the configuration of one of the lowest-level parts of Android’s kernel: Obviously it’s Task Scheduler. With the help of  right Task Scheduler configuration, you can boost performance on Android. Read the full tutorial to find out how to boost performance on android by tweaking KTS.  What Exactly is a Task Scheduler? We kkep on getting updates from various Android News regarding flagships.

An Operating system’s Task Scheduler is thoroughly responsible for distributing CPUs among running tasks. It does so by providing each task some run time on the CPU and swapping it out with another one on specific time intervals.

In other way, actually there are two types of task scheduling: preemptive scheduling and cooperative scheduling. In preemptive scheduling, the kernel decides when to preempt (=swap) a running task. It does that at almost any time, based on complex scheduling algorithms. In contrast, in cooperative scheduling, each task is responsible to notify the kernel when it does not need the CPU anymore,

And thus it can be swapped out. In this case, the kernel is only responsible for identifying the best candidate for scheduling and putting it on the CPU.

How to Boost Performance on Android by Tweaking KTS Part A

Task Scheduling on Android

As fo as the system is concerned this runs on preemptive kernel. Task scheduler triggers at a hard-coded, at some regular interval or scheduling interval. This is usually happens every 10 milliseconds on Android devices.

When the scheduler runs, it decides whether the running task should be preempted or not, and which task from those waiting to run should take its place on the CPU.

Since version 2.6.23, Linux kernel uses a highly sophisticated task scheduler, the Completely Fair Scheduler (CFS). CFS tries to give every task a fair amount of run time on the CPU based on several factors, including task weight and priority.

This scheduler’s algorithm is quite complex to understand. That is why there is a relatively small number of kernel developers that contribute to its development.

How to Boost Performance on Android by Tweaking KTS Part A

How to Tweak the CFS Scheduler

Prerequisites

  • Root Access
  • A Terminal Emulator App

Despite of too much complexity, CFS scheduler obviously offers many configuration options. Tweaking the options could highly affect system performance. consequently, the sysctl command line tool is used for examining and changing kernel parameters at runtime, including scheduler parameters.

We are going to use this tool in our tutorial since it comes pre-installed on most Androids out there. But first of all, we need to do a small introduction to it:

Really How to Use Sysctl

  • Listing the Parameters and it’s current values
    To list all available kernel parameters with their assigned values, give the following command inside a Terminal App:
  • The parameters list is quite big. We are going to only discuss some of the parameters given below, that are the most important ones.

    How to Boost Performance on Android by Tweaking KTS Part A
    How to Boost Performance on Android by Tweaking KTS Part A
  • Changing a Parameter
    We can change a parameter using a command in the form sysctl -w [parameter]=[value] where parameter is the kernel parameter name and value is the new value. For example:

sysctl -w kernel.sched_scaling_enable=1

Read the given below for more sysctl usage examples. Keep in mind that changes do not survive a reboot. You can use an init.d script to apply any needed changes at boot time.

How to Boost Performance on Android by Tweaking KTS Part A

Basic Task Scheduler Parameters

·         kernel.sched_min_granularity_ns

Processor-bound tasks are guaranteed to run for this minimum time before they are preempted. A task is considered as processor-bound when the time it takes to complete depends only on the processor speed. For example, a task waiting for a package to arrive through the net is not a processor-bound task. Generally, increasing this value will increase a system’s throughput. On the other hand, values too high might reduce device responsiveness. This parameter takes values in nanoseconds. For example, to set it to 4000000 nanoseconds (4 milliseconds), we can give the following command:

sysctl -w kernel.sched_min_granularity_ns=4000000

As per official Linux kernel documentation, this is the central CFS scheduler tunable.

·         kernel.sched_latency_ns

All of the tasks on the CFS’s run queue are guaranteed to be scheduled at least once within this period. However, as the number of tasks increases, the scheduler cannot assure that both this and sched_min_granularity_ns are satisfied. So, when the number of running tasks exceeds sched_latency_ns/sched_min_granularity_ns , this period equals to [ (number of runnable tasks) * sched_min_granularity_ns ]. Values are expressed in nanoseconds.

·         kernel.sched_tunable_scaling

This controls whether the scheduler can automatically adjust sched_latency_ns, based on number of online CPUs. Possible values are 0 (do not adjust) , 1 (logarithmic adjustment), and 2 (linear adjustment).

·         kernel.sched_child_runs_first

This parameter defines whether a freshly forked child runs before the parent continues execution
(for example a messaging App that starts a background message sync service). This might be beneficial in processes that the child needs to execute some code as soon as it runs. Whether changing this parameter will improve a device’s performance or not, depends on usage scenario.  To enable this feature, enter the following command in the Terminal App:

sysctl -w kernel.sched_child_runs_first=1

·         kernel.sched_wakeup_granularity_ns

A sleeping task that wakes up after an event will run for at least the amount of time defined by this parameter. An example is a process waiting for data to arrive from a sensor. That process will usually sleep until the sensor sends an interrupt. Then, it will wake up and try to execute some code as soon as possible. This parameter must hold a value higher than half of sched_latency_ns. This ensures that waking tasks can compete with CPU hogs (tasks that make big use of the device’s processor).

Kernel Scheduler Configuration Examples

Following examples are provided as a reference for more experimentation. They follow some simple rules depending to the workload they are designed for, but they are not guaranteed to have the same results on all Android devices.

 

Gaming

Games are demanding applications that make heavy utilization of the CPU. Giving them higher run time on the CPU might help reduce hangs and sound artifacts. Most games typically run several code execution threats in the background. Letting these threats run as soon as possible might be beneficial. Tunable scaling is set to off, to make sure kernel.scheduler_latency_ns remains constant.

  1. sched_min_granularity_ns=6000000
  2. sched_latency_ns=20000000
  3. sched_wakeup_granularity_ns=10000000
  4. sched_child_runs_first=1
  5. sched_tunable_scaling=0

Device Responsiveness

Most multicore Android devices use a CPU hotplug daemon which turns CPUs on or off depending on the system load. The kernel scheduler does a great job adjusting its latency depending on the number of available CPUs. As mentioned above, this feature is provided through the kernel.sched_tunable_scaling parameter. Furthermore, high device responsiveness generally needs low scheduling latencies. We can help keep latencies low by reducing the tasks minimum run time.

  1. sched_min_granularity_ns=1000000
  2. sched_wakeup_granularity_ns=2000000
  3. sched_child_runs_first=1
  4. sched_tunable_scaling=1 or kernel.sched_tunable_scaling=2 (results differ between devices and workloads)

Low Battery Consumption

we helped you "How to Boost Performance on Android by Tweaking KTS Part A". Task Scheduler greatly affects the battery consumption. A high rate of task swapping on the CPU will introduce some scheduler-related workload on the system. This might increase battery consumption. However, a very low rate of task swapping (very high minimum granularity and/or latency values) will severely decrease responsiveness. The magic recipe lies somewhere in the middle. To minimize scheduler-related workload, tunable scaling is disabled.

  1. sched_min_granularity_ns=2000000
  2. sched_latency_ns=15000000
  3. sched_wakeup_granularity_ns=10000000
  4. sched_child_runs_first=0
  5. sched_tunable_scaling=0

We have discussed the most important configuration parameters of the Linux Task Scheduler. Wait  for the second part of this guide, in which we woulh discuss how to enable or disable some special Task Scheduler features. If you decide to tweak your scheduler’s parameters and get some good results, share your results with us in the comments section .