KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
gmon.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 gprof/gmon.h
4 Copyright (C) 2025 Andy Barajas
5
6*/
7
8#ifndef __GPROF_GMON_H
9#define __GPROF_GMON_H
10
11#include <sys/cdefs.h>
12__BEGIN_DECLS
13
14#include <stdint.h>
15#include <stdbool.h>
16
17/** \defgroup debugging_gprof GPROF
18 \brief On-target gprof profiling support
19 \ingroup debugging
20
21 This file provides utilities for profiling applications with gprof. Gprof
22 lets you analyze where your program spent its time and which functions
23 called which other functions during execution, helping you find
24 performance bottlenecks.
25
26 Profiling Steps:
27
28 1. Build with profiling flags. \c -pg must be on both the compile and the
29 link (as on a normal gprof system). KOS applies \c CFLAGS automatically
30 when compiling, but the link command is hand-written, so add \c -pg to it
31 directly:
32
33 ```make
34 CFLAGS += -pg -fno-omit-frame-pointer -fno-inline
35
36 $(TARGET): $(OBJS)
37 kos-cc -pg -o $(TARGET) $(OBJS)
38 ```
39 \c -fno-omit-frame-pointer is REQUIRED because GCC rejects \c -pg together
40 with \c -fomit-frame-pointer (which KOS sets by default); \c -fno-inline
41 keeps each function a distinct symbol in the report but is not required.
42
43 2. Running your program to create gmon.out:
44
45 Execute your program normally to completion. This writes the profiling
46 data to \c /pc/gmon.out by default, or to \c $GMON_OUT_PREFIX.$pid if
47 the \c GMON_OUT_PREFIX environment variable is set (see #GMON_OUT_PREFIX).
48
49 3. Running gprof:
50
51 ```sh
52 $(KOS_GPROF) $(TARGET) gmon.out > gprof_output.txt
53 ```
54 Replace \c $(TARGET) with your .elf compiled program. This produces a
55 human-readable report.
56
57 4. Optional: visualize with gprof2dot + Graphviz:
58
59 ```sh
60 gprof2dot gprof_output.txt | dot -Tsvg -o graph.svg
61 ```
62
63 \author Andy Barajas
64
65 @{
66*/
67
68/** \brief Environment variable that redirects the gmon.out output path.
69
70 If set, the profiling data is written to `<GMON_OUT_PREFIX>.<pid>` instead of
71 the default location(`/pc/gmon.out`), letting you choose where it lands.
72*/
73#define GMON_OUT_PREFIX "GMON_OUT_PREFIX"
74
75/** \brief Start gprof profiling for a specified address range.
76
77 Allocates the profiling buffers, readies the per-architecture instrumentation,
78 starts the histogram sampler, and begins profiling immediately. With \c -pg
79 this is called automatically before \c main() (via the weak \c gprof_init()
80 hook), so most programs never call it directly.
81
82 Call it by hand only in a program not built with \c -pg, for a flat
83 histogram-only profile -- without the \c -pg instrumentation there is no call
84 graph.
85
86 \param lowpc The lower bound of the address range to profile.
87 \param highpc The upper bound of the address range to profile.
88*/
89void monstartup(uintptr_t lowpc, uintptr_t highpc);
90
91/** \brief Restart or stop gprof profiling.
92
93 This function restarts or stops gprof profiling. It does NOT perform the
94 initial setup (that is done by monstartup() before main()). Use it to pause
95 profiling and then resume it later once you reach the section of code you
96 want to profile.
97
98 \param enable true to (re)start profiling, false to stop it.
99*/
100void moncontrol(bool enable);
101
102/** \brief Flush gmon.out and stop profiling.
103
104 Writes the collected profile to its output file (see #GMON_OUT_PREFIX) and
105 tears profiling down -- stopping the sampler and freeing its buffers. This
106 runs automatically at program exit via atexit(), so most programs never call
107 it.
108
109 Call it explicitly if your program never returns from \c main() (e.g. a game
110 loop) and you still want a gmon.out. It is idempotent, so a manual call plus
111 the automatic exit-time one is harmless. Profiling stays off afterward until
112 monstartup() is called again.
113*/
114void _mcleanup(void);
115
116/** @} */
117
118__END_DECLS
119
120#endif /* __GPROF_GMON_H */
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.