Embedded Design Handbook

ID 683689
Date 8/28/2023
Public
Document Table of Contents

4.4.3.3.1. Profiler Mechanics

You enable the GNU profiler by turning on the hal.enable_gprof switch in the scripts to generate the BSP. Turning on this switch automatically turns on the -pg compiler switch and then links the profiling library code in the altera_nios2 software component with the BSP. This code counts the number of calls to each profiled function.

The -pg compiler option forces the compiler to insert a call to the mcount() function (located in the file altera_nios2/HAL/src/alt_mcount.S) at the beginning of every function call. The calls to mcount() track every dynamic parent and child function call relationship to enable the construction of a call graph. The option also installs nios2_pcsample()function (located in the file altera_nios2/HAL/src/ alt_gmon.c) that samples the foreground program counter at every system clock interrupt. When the program executes, the GNU profiler collects data on the host of the gmon.out. The nios2-elf-gprof utility can read this file and display profiling information about the program.

The profiling code operates on the target by performing the following steps:

  1. The Compiler implements function prologues with a call to mcount() to enable the Compiler to determine the function call graph. The GNU profiler documentation refers to this data as the function call arc data.
  2. The timer interrupt handler registers an alarm to capture information about the foreground function (histogram data) that executes when the alarm triggers.
  3. The heap allocates a target memory to store the profiling data.
  4. When your code exits with a BREAK 2 instruction, the nios2-download utility copies the profiling data from the target to the host.

The nios2-elf-gprof utility requires the function call arc data and the histogram data to work correctly.

Note: For more information about the GNU profiler, refer to the Nios® II GNU profiler documentation, included with the GCC documentation, available on the Nios® II Embedded Design Suite Support page of the Intel website.