Fork me on GitHub


[Android] oprofile 使用步驟

This post records the basic flow to use oprofile tool on Android

OProfile consists of a Kernel driver and a daemon for collecting data. It makes use of the hardware performance counters provided on Intel, AMD, and other processors. OProfile is capable of profiling all code including the Kernel, Kernel modules, Kernel interrupt handlers, system shared libraries, and other applications.

Modern processors support profiling through the hardware by performance counters. Depending on the processor, there can be many counters and each of these can be programmed with an event to count. Each counter has a value which determines how often a sample is taken. The lower the value, the more often it is used.

During the post-processing step, all information is collected and instruction addresses are mapped to a function name.

Here we go ~

  1. Add config, re-build kernel then flash into device

  2. Build oprofile tool

    $ source setbuildenv
    $ cd {root}/external/oprofile
    $ mm
    The output will be at "out/host/linux-x86/bin/"
  3. Copy needed file

    $ cp {root}/external/oprofile/events/arm/* /usr/local/share/oprofile/arm
  4. Copy vmlinux into device (Optional, if you want to profile kernel)

    $ adb push ${OUT}/obj/KERNEL/arch/arm/boot/compressed/vmlinux data/
  5. Setup oprofile

    opcontrol --setup --event=CPU_CYCLES:1500000
    opcontrol --setup --vmlinux=/data/vmlinux --kernel-range=0xc00xxxxx,0xc0xxxxxx --event=CPU_CYCLES:1500000 (profile kernel)
    The option --kernel-range can be obtained from /proc/kallsyms.
    $ (adb shell cat /proc/kallsyms) | grep " _text" 
    $ (adb shell cat /proc/kallsyms) | grep " _etext" 
    or you could query file
    $ grep " _text"
  6. Check status

    opcontrol --status
  7. Start profiling

    opcontrol --start
  8. Stop profiling

    opcontrol --stop
  9. Get profiling result

    $ cd {root}/external/oprofile
    $ ./opimport_pull <result path>
  10. Get a simple report

    $ cd {root}/out/host/linux-x86/bin/
    $ ./opreport --session-dir <result-path> -m all
    $ ./opreport -g -l -p <path-to-symbols> --session-dir <result-path>
  11. Get annotated report

    $ ./opannotate -p <path-to-symbols> -s -d <path-to-source-files-e.g.-mydroid> --session-dir <result-path>
  12. Profiling based on thread

    opcontrol --setup --event=CPU_CYCLES:1500000 --separate=thread // profiling based on thread
    $ ./opimport_pull <result-path>
    $ ./opreport --session-dir <result-path> -l tgid:[pid you want]


  1. OProfile manual
  2. Profiling the Android kernel and native applications using OProfile
  3. Profiling Tools - ftrace, perf, and oprofile
  4. how-to to oprofile 0xdroid
  5. oprofile 使用步驟 測試程序中各函數運行時間
  6. OProfile—System-Wide Profiler


No comments:

Post a Comment