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 sys/gmon.h
4 Copyright (C) 2025 Andy Barajas
5
6*/
7
8#ifndef __SYS_GMON_H
9#define __SYS_GMON_H
10
11#include <sys/cdefs.h>
12__BEGIN_DECLS
13
14#include <stdint.h>
15#include <stdbool.h>
16
17/** \defgroup gprof GPROF
18 \brief Lightweight GPROF profiling runtime for KOS
19 \ingroup debugging
20
21
22 This file provides utilities for profiling applications using gprof. Gprof
23 is a profiling tool that allows you to analyze where your program spent
24 its time and which functions called which other functions during execution.
25 This can help you identify performance bottlenecks and optimize your code.
26
27 Profiling Steps:
28
29 1. Compile your program with profiling flags:
30
31 Use the following flags:
32 ```sh
33 CFLAGS = -pg -fno-omit-frame-pointer -fno-inline
34 ```
35 These flags enable profiling and prevent function inlining to ensure
36 accurate profiling data. When you use the -pg flag, the GCC compiler
37 inserts trapa #33 instructions at the beginning of each function into
38 your build.
39
40 2. Running your program to create gmon.out:
41
42 Execute your program normally to completion using dcload-ip or
43 dcload-serial. This will generate a file named `gmon.out` in the current
44 directory, which contains the profiling data.
45
46 3. Running gprof:
47
48 Use the following command to analyze the profiling data:
49 ```sh
50 $(KOS_GPROF) $(TARGET) gmon.out > gprof_output.txt
51 ```
52 Replace `$(TARGET)` with the name of your compiled program. This
53 command will generate a human-readable report in `gprof_output.txt`.
54
55 4. Visualizing profiling data with gprof2dot:
56
57 To create a graphical representation of the profiling data, use the
58 `gprof2dot` tool:
59 ```sh
60 gprof2dot gprof_output.txt > graph.dot
61 ```
62 This converts the `gprof` output into a DOT format file, which can be
63 used to generate various types of graphs.
64
65 5. Generating an image from the DOT file:
66
67 Use the `dot` command from Graphviz to create a SVG image from the DOT
68 file:
69 ```sh
70 dot -Tsvg graph.dot -o graph.svg
71 ```
72 This command will generate a SVG image (`graph.svg`) that visually
73 represents the profiling data.
74
75 By following these steps, you can effectively profile your program and
76 identify performance bottlenecks for optimization.
77
78 \author Andy Barajas
79
80 @{
81*/
82
83/** \brief GPROF Trapa Code
84
85 GCC generates this pattern before each profiled function when you compile
86 your project with the -pg flag:
87
88 trapa #33 --- This is a 2-byte instruction
89 nop --- This is a 2-byte no-op placeholder
90 label --- This is a 4-byte LABELNO
91*/
92#define GPROF_TRAPA_CODE 33
93
94/** \brief Start gprof profiling for a specified address range.
95
96 This function is intended for programs not linked with the `-pg' linker
97 switch. If `-pg' was used during linking, monstartup is automatically
98 called by the startup code with arguments covering the entire range of
99 executable addresses, from the program's entry point to the highest code
100 segment address. Using this method to initialize gprof will only generate
101 histogram profiling data, without producing a call graph. Profiling starts
102 immediately after calling this function.
103
104 \param lowpc The lower bound of the address range to profile.
105 \param highpc The upper bound of the address range to profile.
106*/
107void monstartup(uintptr_t lowpc, uintptr_t highpc);
108
109/** \brief Restart or stop gprof profiling.
110
111 This function restarts or stops gprof profiling. It does NOT start gprof
112 profiling initially, as gprof profiling starts before the program enters
113 the main function. You can use this function to stop profiling and then
114 restart it later when you reach the section of code you want to profile.
115
116 \param enable A boolean value to restart (true) or stop (false)
117 gprof profiling.
118*/
119void moncontrol(bool enable);
120
121/** @} */
122
123__END_DECLS
124
125#endif /* __SYS_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.