Simpleperf analysis of Android system

Android system of Simpleperf analysis

Hate jade novel network https://www.1589.info

Translator's note:

Simpleperf is a CPU performance analysis tool for Native, which is mainly used to analyze code execution time. This article is a part of the main document, system.

See aosp warehouse: Android for the original text_ platform_ profiling. md

See aosp warehouse: Simpleperf for the main document of Simpleperf

Introduction to the official website: https://developer.android.com/ndk/guides/simpleperf

A report of your own: https://files.cnblogs.com/files/houser0323/report.7z

A good expansion introduction blog post: introduction to Simpleperf

catalogue

  • Simpleperf analysis of Android system
    • catalogue
    • General skills
    • In system_ Grab simpleperf on the server process
    • Hardware PMU counter limit

General skills

Here are some tips for Android system developers with root privileges:

  1. After running adb root, simpleperf can be used to analyze any process within the system.
  2. If you are not working on the main branch, it is recommended to use the latest simpleperf in AOSP main. The script location is system/extras/simpleperf/scripts, and the binary program is system/extras/simpleperf/scripts/bin/android
  3. Recommended app_profiler.py grab trace, and then use report_html.py generates HTML reports. Here is an example.
# Record surfaceflinger process for 10 seconds with dwarf based call graph. More examples are in
# scripts reference in the doc.
$ python app_profiler.py -np surfaceflinger -r "-g --duration 10"

# Generate html report.
$ python report_html.py
  1. Starting from Android > = O, the system library has a symbolic table by default. We don't need to use $Android_ PRODUCT_ There is no striped binary file in out / symbols. However, they are required when adding source code and disassembly (with line numbers) to the report. Here is an example.
# Doing recording with app_profiler.py or simpleperf on device, and generates perf.data on host.
$ python app_profiler.py -np surfaceflinger -r "--call-graph fp --duration 10"

# Collect unstripped binaries from $ANDROID_PRODUCT_OUT/symbols to binary_cache/.
$ python binary_cache_builder.py -lib $ANDROID_PRODUCT_OUT/symbols

# Report source code and disassembly. Disassembling all binaries is slow, so it's better to add
# --binary_filter option to only disassemble selected binaries.
$ python report_html.py --add_source_code --source_dirs $ANDROID_BUILD_TOP --add_disassembly \n  --binary_filter surfaceflinger.so

In system_ Grab simpleperf on the server process

Sometimes we want to catch the system process in case of special circumstances. In this case, we can add SimplEperf code at the point where the situation is detected.

  1. Turn off selinux ADB shell setenforce0. Because selinux only allows simpleperf to be used in shell or debuggable/profileable applications.
  2. Add the following code where special conditions are detected.
try {
  // for capability check
  Os.prctl(OsConstants.PR_CAP_AMBIENT, OsConstants.PR_CAP_AMBIENT_RAISE,
           OsConstants.CAP_SYS_PTRACE, 0, 0);
  // Write to /data instead of /data/local/tmp. Because /data can be written by system user.
  Runtime.getRuntime().exec("/system/bin/simpleperf record -g -p " + String.valueOf(Process.myPid())
            + " -o /data/perf.data --duration 30 --log-to-android-buffer --log verbose");
} catch (Exception e) {
  Slog.e(TAG, "error while running simpleperf");
  e.printStackTrace();
}

Hardware PMU counter limit

When monitoring instruction and cache related performance events (hw/cache/raw/pmu category listed in the list command), these events are mapped to PMU counters on each cpu core. However, there are only a limited number of PMU counters per core. If the number of events > the number of PMU counters, and then the counters are multiplexed between events, this may not be what we want.

On Pixel devices, the number of PMU counters on each core is usually 7, of which 4 are used by the kernel to monitor memory latency. So only 3 counters are available. Up to 3 PMU events can be monitored simultaneously. To monitor more than three events, you can use the -- use devfreq counters option to borrow counters used by the kernel.

Added by travelkind on Sat, 22 Jan 2022 16:49:23 +0200