How to Use OProfile

OProfile is a system-wide profiler for Linux systems, capable of profiling all running code at low overhead.

Operf vs Opcontrol

Legacy OProfile consists of the opcontrol shell script for configuring, starting, and stopping a profiling session. To this purpose, a kernel driver is used for collecting samples, which are recorded into sample files. A disadvantage of this mode is the necessity of elevated user privileges to run opcontrol. opcontrol was discontinued in version 1.0.0.

Operf was designed to be used in place of opcontrol for profiling. It uses the Linux Performance Events Subsystem, and therefore, does not require the use of the opcontrol daemon or any elevated privileges. The use of operf and opcontrol are mutually exclusive. operf was introduced in version 0.9.8.

operf requires hardware support for profiling. In the case of an ARM processor, the Performance Monitoring Unit (PMU) and arm-pmu driver must be present. On devices without hardware support, oprofile version 0.9.9 and opcontrol must be used. Without hardware support, operf will report

Your kernel's Performance Events Subsystem does not support your processor type

Prerequisites

  • Kernel built with the following options:
    • CONFIG_PROFILING=y
    • CONFIG_OPROFILE=y
  • Target RFS containing:
    • oprofile
    • Native toolchain required if profiling the kernel
    • Uncompressed kernel image within the RFS if profiling the kernel.
      • You can include this in your Factory build by running make menuconfig and selecting Target Software > Kernel > Include uncompressed kernel image within the RFS
    • bash
    • A number of utilities, which are typically provided by busybox or the following packages
      • coreutils
      • which
      • mktemp
      • procps
  • In order to profile the libraries, unstripped versions need to be included in root filesystem.
    Run make menuconfig and unselect: Target Configuration > Build RFS > Strip all libraries and binaries in the RFS
    And then run:
    • make rfs-distclean
    • make

If you are using Factory on your desktop, then you can run "make checktool-oprofile" to check your platform for compatibility with OProfile.

Procedure: opcontrol-based oprofile

Initialization

If you want to profile the kernel you must copy the uncompressed vmlinux file from the root of your kernel source tree to your RFS. If your distribution comes from the Timesys Web Factory then you will have to rebuild your kernel from source, as this file is not included in the binary installer. You must have a native toolchain installed on the target in order to profile the kernel. Then you can initialize OProfile by entering the following on the target:

opcontrol --vmlinux=/path/to/vmlinux/file

If you don't want to profile the kernel, you can initialize by entering the following on the target:

opcontrol --no-vmlinux

Profiling

To begin profiling, enter the following on the target:

opcontrol --start

Then perform any action and run any software you wish to profile. When finished enter the following on the target:

opcontrol --shutdown

Reporting

To obtain a summary of the profiling information enter the following on the target:

opreport

For a more detailed summary enter the following on the target:

opreport -l

Examples

Here is an example session profiling the kernel on an OMAP 3530 board:

# opcontrol --vmlinux=/boot/vmlinux
# opcontrol --start
Using 2.6+ OProfile kernel interface.
Reading module info.
Using log file /var/lib/oprofile/samples/oprofiled.log
Daemon started.
Profiler running.
# cat vmlinux > /dev/null
# opcontrol --shutdown
Detected stale lock file. Removing.
# opreport
CPU: CPU with timer interrupt, speed 0 MHz (estimated)
Profiling through timer interrupt
          TIMER:0|
  samples|      %|
------------------
     5717 99.8603 vmlinux
        4  0.0699 libc-2.7.so
        2  0.0349 busybox
        2  0.0349 ld-2.7.so

Procedure: operf-based oprofile

Profiling

To profile your application:

operf application [ application_args ]

Reporting

To obtain a summary of the profiling information enter the following on the target:

opreport

For a more detailed summary enter the following on the target:

opreport -l

Example

Here is an example session profiling the kernel on an i.MX6 board:

# cp /bin/busybox .
# operf --vmlinux=/boot/vmlinux ./busybox --help
BusyBox v1.20.2 (2014-09-25 16:48:10 EDT) multi-call binary.
Copyright (C) 1998-2011 Erik Andersen, Rob Landley, Denys Vlasenko
and others. Licensed under GPLv2.
See source distribution for full notice.
...
        whois, xargs, xz, xzcat, yes, zcat, zcip

operf: Profiler started

Profiling done.
#
# opreport -l ./busybox
Using //oprofile_data/samples/ for samples directory.
CPU: ARM Cortex-A9, speed 996000 MHz (estimated)
Counted CPU_CYCLES events (CPU cycle) with a unit mask of 0x00 (No unit mask) count 100000
samples  %        image name               symbol name
34       51.5152  vmlinux                  _raw_spin_unlock_irqrestore
3         4.5455  vmlinux                  do_page_fault
2         3.0303  ld-2.17.so               _dl_relocate_object
2         3.0303  vmlinux                  fget_light
2         3.0303  vmlinux                  vector_swi
1         1.5152  busybox                  /busybox^M
1         1.5152  ld-2.17.so               _dl_check_map_versions
1         1.5152  ld-2.17.so               _dl_init_paths
1         1.5152  libc-2.17.so             _dl_addr
1         1.5152  libc-2.17.so             mallopt
1         1.5152  vmlinux                  __remove_shared_vm_struct
1         1.5152  vmlinux                  __slab_free.isra.44
1         1.5152  vmlinux                  __srcu_read_lock
1         1.5152  vmlinux                  _set_bit
1         1.5152  vmlinux                  copy_page
1         1.5152  vmlinux                  do_get_write_access
1         1.5152  vmlinux                  do_sync_read
1         1.5152  vmlinux                  find_get_page
1         1.5152  vmlinux                  find_vma
1         1.5152  vmlinux                  getname_flags
1         1.5152  vmlinux                  mutex_lock
1         1.5152  vmlinux                  n_tty_write
1         1.5152  vmlinux                  release_pages
1         1.5152  vmlinux                  tty_write_unlock
1         1.5152  vmlinux                  uart_start
1         1.5152  vmlinux                  unmap_single_vma
1         1.5152  vmlinux                  v7_flush_kern_dcache_area
1         1.5152  vmlinux                  vfs_write

Additional Resources