A deep analysis of the performance of a smartphone or tablet is impossible without understanding what exactly is happening in the operating system kernel at each specific moment in time. System tracing (System Tracing) is a powerful built-in mechanism into the Linux kernel on which Android is based, which allows you to record scheduler events, the operation of drivers and system calls. The average user does not need this data, but for application developers and enthusiasts involved in device optimization, this tool becomes indispensable for identifying bottlenecks and lags.

The process of setting up and collecting logs may seem complicated due to the abundance of technical terms and the need to use a computer, but modern Google tools have greatly simplified this task. If previously the tool dominated, now the main emphasis is shifting to the platform, which offers more flexible recording and visualization options. To get started, you will need not only the device itself, but also a properly configured environment on the PC with it installed. Systrace, then now the main emphasis is shifting to the platform Perfetto, which offers more flexible recording and visualization options. To get started, you will need not only the device itself, but also a properly configured environment on a PC with Android Debug Bridge.

It is important to understand that enabling tracing creates additional load on the processor and takes up space in the internal memory. Therefore, using these functions in the background on an ongoing basis is not advisable. Logging should be activated only for the period when you are reproducing a specific problem or testing the operation of new software. Next, we will look in detail at how to prepare the device, what commands to enter and how to interpret the results.

Preparing the device and enabling developer mode

The first and mandatory step is to activate the hidden menu, which gives access to advanced system settings. Without this step, any attempts to connect debugging tools will be unsuccessful, since the system by default blocks external interference. You need to go to the section Settings โ†’ About phone and find the item Build number. By clicking on it seven times in a row, you will receive a notification that the developer mode is activated.

After the new item appears in the main settings menu, go to the section For developers. Here you are interested in the option USB debugging. Switch the slider to the active position. When you connect to a computer for the first time, a request will appear on the smartphone screen to allow debugging from this computer - be sure to confirm it by checking the โ€œAlways allow from this computerโ€ checkbox.

โš ๏ธ Attention: Do not leave USB debugging enabled all the time, especially if you connect the phone to public chargers stations. This may create a vulnerability for unauthorized access to your device data.

Additionally, in the developer menu, you should pay attention to the item Select the application for tracking. Here you can select the specific package that the system will monitor most closely, although this is not strictly necessary for overall system tracing. Also check the Android version, since support for some tracing functions depends on the OS release.

  • ๐Ÿ“ฑ Go to Settings and find the section About phone.
  • ๐Ÿ”ข Press 7 times on the item Build number until the activation message appears.
  • ๐Ÿ”Œ In the menu For developers turn on the toggle switch USB debugging.
  • โœ… Confirm permission for debugging when connecting the cable to the PC.
๐Ÿ“Š What is your main purpose for setting up tracing?
Application development
Optimization of the old smartphone
Studying how the kernel works
Just curious
Other

Installing and configuring Android SDK Platform Tools

To interact with a smartphone at a low level, you need a set of utilities known as Platform Tools. This package contains a critical component adbthrough which commands are passed to start and stop the trace. It should be downloaded exclusively from the official website of Android developers in order to avoid malicious modifications that may be found on third-party resources.

After downloading the archive, unpack it into a convenient directory on your computer. For ease of use, it is recommended to add the path to this folder to the system environment variables (PATH on Windows or export PATH on Linux/macOS). This will allow you to run console commands from any directory without specifying the full path to the executable file each time.

Check the connection is correct by running the command adb devices in the terminal or command line. If everything is configured correctly, you will see your device's serial number and status device. If unauthorizedis displayed instead, check your smartphone screen and confirm the debugging request. If the device is not listed, try replacing the USB cable, as some cords only support charging without data transfer.

adb kill-server

adb start-server

adb devices

Modern tracing tools, such as Perfettomay require installing additional components through the SDK manager. However, for basic setup and launch of the classic one, the minimum set is sufficient. Make sure that the latest drivers for your smartphone are installed on your PC, especially if you are using a device from less popular brands. systrace the minimum set is enough Platform Tools. Make sure you have the latest drivers for your smartphone installed on your PC, especially if you are using a device from less popular brands.

Running a trace via the ADB command line

The most universal way to start recording system events is to use the command adb shell. This method works on the vast majority of Android devices, starting with relatively older versions. The command initiates writing to a special ring-type buffer, which stores data until it stops or overflows.

To start recording, use the utility atrace or systrace, built into the Android shell. You can specify specific tags for tracking so as not to overload the log with unnecessary information. For example, the tag gfx is responsible for graphics, input for data entry, and view for rendering the interface. A combination of tags allows you to focus on a specific subsystem.

adb shell atrace --async_start gfx input view sched freq

After executing the command, background recording will begin. Perform the actions on the device that you want to analyze (launching a game, scrolling the feed, opening a heavy application). When the test script is completed, stop recording with the command atrace --async_stop. The data will be saved to a file that needs to be extracted to the computer for further analysis.

โ˜‘๏ธ Check before starting tracing

Done: 0 / 5

โš ๏ธ Attention: The command line interface and available tags may differ depending on the version of Android and the manufacturer's custom firmware. If the command returns a "command not found" error, check for superuser rights or update Platform Tools.

This phenomenon is called overhead (overhead). The system wastes CPU resources writing logs, which can distort the results of real-world performance testing. Try to make entries as short as possible, covering only the problematic time interval.

Using the Perfetto UI web interface

The modern de facto standard for trace analysis is the platform Perfetto. Unlike the legacy one, it allows you to record data directly through the browser using a secure connection and provides an incredibly detailed visual interface. To get started, go to the official website systrace, it allows you to record data directly through your browser using a secure connection and provides an incredibly detailed visual interface. To get started, go to the official website ui.perfetto.dev.

On the main page, click the button Record new trace. The browser will ask for permission to connect to the device via the WebUSB protocol. Select your device from the list. The interface will prompt you to select a recording configuration: you can use preset profiles (for example, "Android system tracing") or configure your own event categories.

Event category Description Impact on performance
Scheduling Context switching between processes Low
Graphics GPU and SurfaceFlinger frame rendering Medium
App Sobs_within_applications (Java/Kotlin) Depends on code
Power Changing the CPU frequency and battery status Low

After selecting the settings, click Start recording. Perform the necessary actions on your smartphone. When finished, click Stop recording. The trace file will automatically open in the same browser window, where you can zoom the timeline, search for specific processes, and analyze latency. This the most visual way see which thread is blocking the main event loop.

Why is Perfetto better than the old Systrace?

Perfetto is faster, consumes less battery resources, supports buffering in kernel memory (which prevents loss of data during lags) and has a much more convenient web interface for analysis, which does not require installation of Python scripts.

Analysis of the received data and search for bottlenecks

Having received a trace file, you are faced with a huge amount of data. The main task is to find moments when the system was idle or performed unnecessary work. In the GUI, pay attention to the line CPU. If you see gaps in activity or frequent switching between cores without payload, this may indicate scheduler problems.

Pay special attention to the process surfaceflinger and your target application. If there are large time intervals between frames (more than 16.6 ms for 60 Hz), then there is frame drop (frame skipping). By hovering the cursor over such a period, you can see what event occupied the processor: it could be garbage collection (GC), waiting for input/output (I/O), or blocking by a mutex.

The concept of โ€œframe lengthโ€ is often used for analysis. In an ideal scenario, all calculations should be completed within the allotted time. If you see a thread RenderThread consistently loaded at 100% for an extended period, this is a signal that the graphics pipeline cannot cope with the load. In this case, it is worth checking textures, vertex complexity, or optimizing the rendering code.

๐Ÿ’ก

Use the search function (Ctrl+F) in the Perfetto interface to find the specific names of functions or tags that you added to the application code for debugging. This will save you hours of manually studying the graph.

Remember that interpreting the data requires an understanding of the Android architecture. For example, high process activity kswapd0 indicates a lack of RAM and active paging, which is a serious cause of slowdowns. At the same time, bursts of network driver activity can be the norm for online games.

Frequent errors and ways to fix them

When setting up tracing, beginners often encounter a number of common problems. The most common is the lack of data in the log. This usually occurs because the trace buffer is too small for the selected number of tags, and old data is overwritten by new data before recording stops. The solution is to increase the buffer size through the launch parameters.

Another problem is time desynchronization between logs of different processor cores. This can make it difficult to analyze multi-threaded applications. Make sure that network time synchronization is enabled on the device or use the command adb shell date to check that the system time is up to date before starting recording.

โš ๏ธ Attention: On some devices with custom firmware (for example, older versions of MIUI or EMUI), access to system traces may be limited by the manufacturer, even if you have root access. In such cases, the only option is to install stock firmware or use an emulator.

If you receive an access error when trying to write certain tags (for example, those related to security or kernel), it means that the current user does not have sufficient privileges. Full access to kernel tracing often requires root access, which can be obtained by unlocking the bootloader and installing Magisk, but this will void the warranty and may break banking applications.

๐Ÿ’ก

If the log is empty or truncated, the first step is to reduce the number of active tags and increase the ring buffer size in the launch command settings.

FAQ: Questions and answers about tracing

Is it safe to enable tracing on your main smartphone?

Yes, it is safe from a data integrity point of view, but it can temporarily reduce device performance and increase battery consumption. It is not recommended to start long-term recording during important calls or games where reaction speed is critical.

Are root access needed for basic tracing?

For most custom tags (applications, graphics, input), root access is not needed. However, superuser privileges may be required to access low-level kernel events and some system processes.

Where are trace files stored after recording?

When using ADB, the files are usually saved on the computer where you exported them. When recording inside the device, they can be located in a temporary directory /data/local/tmp, access to which is limited without root access.

Is it possible to analyze a trace without a computer?

There are mobile analyzer applications, but their functionality is extremely limited. To fully analyze complex system interactions, you need a large screen and powerful visualization tools, available only on a PC (Perfetto UI, Android Studio Profiler).

What to do if the atrace command is not found?

The utility atrace has been built into Android since version 4.3. If it is missing, the manufacturer may have cut it out of the firmware. Try using an alternative via simpleperf or install a terminal with root access, where you can install the necessary binaries.