KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches

On-target gprof profiling support More...

Macros

#define GMON_OUT_PREFIX   "GMON_OUT_PREFIX"
 Environment variable that redirects the gmon.out output path.
 

Functions

void monstartup (uintptr_t lowpc, uintptr_t highpc)
 Start gprof profiling for a specified address range.
 
void moncontrol (bool enable)
 Restart or stop gprof profiling.
 
void _mcleanup (void)
 Flush gmon.out and stop profiling.
 

Detailed Description

On-target gprof profiling support

This file provides utilities for profiling applications with gprof. Gprof lets you analyze where your program spent its time and which functions called which other functions during execution, helping you find performance bottlenecks.

Profiling Steps:

  1. Build with profiling flags. -pg must be on both the compile and the link (as on a normal gprof system). KOS applies CFLAGS automatically when compiling, but the link command is hand-written, so add -pg to it directly:

    CFLAGS += -pg -fno-omit-frame-pointer -fno-inline
    $(TARGET): $(OBJS)
    kos-cc -pg -o $(TARGET) $(OBJS)

    -fno-omit-frame-pointer is REQUIRED because GCC rejects -pg together with -fomit-frame-pointer (which KOS sets by default); -fno-inline keeps each function a distinct symbol in the report but is not required.

  2. Running your program to create gmon.out:

    Execute your program normally to completion. This writes the profiling data to /pc/gmon.out by default, or to $GMON_OUT_PREFIX.$pid if the GMON_OUT_PREFIX environment variable is set (see GMON_OUT_PREFIX).

  3. Running gprof:

    $(KOS_GPROF) $(TARGET) gmon.out > gprof_output.txt

    Replace with your .elf compiled program. This produces a human-readable report.

  4. Optional: visualize with gprof2dot + Graphviz:

    gprof2dot gprof_output.txt | dot -Tsvg -o graph.svg
Author
Andy Barajas

Macro Definition Documentation

◆ GMON_OUT_PREFIX

#define GMON_OUT_PREFIX   "GMON_OUT_PREFIX"

Environment variable that redirects the gmon.out output path.

If set, the profiling data is written to <GMON_OUT_PREFIX>.<pid> instead of the default location(/pc/gmon.out), letting you choose where it lands.

Function Documentation

◆ _mcleanup()

void _mcleanup ( void )

Flush gmon.out and stop profiling.

Writes the collected profile to its output file (see GMON_OUT_PREFIX) and tears profiling down – stopping the sampler and freeing its buffers. This runs automatically at program exit via atexit(), so most programs never call it.

Call it explicitly if your program never returns from main() (e.g. a game loop) and you still want a gmon.out. It is idempotent, so a manual call plus the automatic exit-time one is harmless. Profiling stays off afterward until monstartup() is called again.

◆ moncontrol()

void moncontrol ( bool enable)

Restart or stop gprof profiling.

This function restarts or stops gprof profiling. It does NOT perform the initial setup (that is done by monstartup() before main()). Use it to pause profiling and then resume it later once you reach the section of code you want to profile.

Parameters
enabletrue to (re)start profiling, false to stop it.

◆ monstartup()

void monstartup ( uintptr_t lowpc,
uintptr_t highpc )

Start gprof profiling for a specified address range.

Allocates the profiling buffers, readies the per-architecture instrumentation, starts the histogram sampler, and begins profiling immediately. With -pg this is called automatically before main() (via the weak gprof_init() hook), so most programs never call it directly.

Call it by hand only in a program not built with -pg, for a flat histogram-only profile – without the -pg instrumentation there is no call graph.

Parameters
lowpcThe lower bound of the address range to profile.
highpcThe upper bound of the address range to profile.