Advanced OS Customizations & Bootloaders

From Source to System: Tracing Android’s Cgroup v2 Initialization & Configuration Flow

Google AdSense Native Placement - Horizontal Top-Post banner

Introduction

Android’s intricate resource management relies heavily on Linux control groups (cgroups) to ensure system stability, responsiveness, and efficient multitasking. With the adoption of Cgroup v2, Android devices gain a unified, hierarchical system for allocating and limiting resources like CPU, memory, and I/O. This deep dive traces the journey of Cgroup v2 initialization and configuration, from the Linux kernel’s boot process through Android’s init system and the dynamic management by system_server.

Cgroup v2 consolidates various resource controllers into a single, unified hierarchy, simplifying management compared to Cgroup v1’s multiple independent hierarchies. This shift empowers Android to precisely control how system services, foreground applications, and background processes consume resources, directly impacting user experience by preventing resource starvation and improving battery life.

Kernel’s Role in Cgroup v2 Initialization

Boot Arguments and Kernel Configuration

The foundation of Cgroup v2 on an Android device is laid by the Linux kernel. For Cgroup v2 to be active and the unified hierarchy to be used exclusively, specific kernel configurations and boot parameters are essential. The kernel must be compiled with CONFIG_CGROUP_UNIFIED_HIERARCHY=y.

During boot, the kernel command line often includes arguments like cgroup_no_v1=all. While Android’s init system typically handles the unified hierarchy mount explicitly, this parameter ensures no Cgroup v1 hierarchies are created, preventing potential conflicts and resource duplication. You can inspect your device’s kernel command line:

adb shell cat /proc/cmdline

Upon successful initialization, the kernel mounts the unified cgroup hierarchy at /sys/fs/cgroup. This directory becomes the root of all Cgroup v2 operations, containing the necessary control files. You can verify its presence:

adb shell ls -l /sys/fs/cgroup

Within this root, you’ll find files like cgroup.controllers, which lists all available resource controllers (e.g., cpu, memory, io, pids), and cgroup.subtree_control, which is used to enable or disable these controllers for child cgroups.

Initial Hierarchy Setup

The kernel establishes the root cgroup, which encompasses all processes on the system. It then populates cgroup.controllers with the available resource controllers. Subsequently, Android’s init process takes over to create the specific cgroup hierarchy tailored for Android’s needs, enabling and configuring these controllers within the various sub-cgroups.

Android ‘init’ Process: Early Resource Orchestration

Parsing init.rc and Service Definitions

As the first user-space process, init (from system/core/init) is responsible for reading configuration files like init.rc and any associated .conf files (e.g., init.device.rc). These scripts define system services, their properties, and crucially, their initial Cgroup v2 affiliations.

The cgroup_path property in a service definition tells init where to place the service’s primary process within the unified cgroup hierarchy. When init starts a service, it creates the specified cgroup directory (if it doesn’t exist) under /sys/fs/cgroup and moves the service’s PID into that cgroup’s cgroup.procs file. Additionally, init can configure initial resource limits by writing to controller-specific files within these cgroup directories.

Here’s a simplified example of an init.rc service definition leveraging cgroup_path:

service mediaserver /system/bin/mediaserver
class main
user media
group media system audio drmrpc
capabilities SYS_NICE
seclabel u:r:mediaserver:s0
cgroup_path /system-background
# Further cgroup configurations can be applied via write commands
# E.g., write /sys/fs/cgroup/system-background/cpu.max

Android Mobile Specs & Compare Directory

Are you researching mobile hardware properties, processor SoCs, GPU chipsets, or RAM configurations? Access our complete specs catalog to compare up to 5 devices side-by-side!

Compare Devices Specs →
Google AdSense Inline Placement - Content Footer banner