KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
perf_monitor.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 arch/dreamcast/include/dc/perf_monitor.h
4 * Copyright (C) 2024 Paul Cercueil
5
6*/
7
8/** \file dc/perf_monitor.h
9 \brief Low-level performance monitor
10 \ingroup perf_monitor
11
12 This file contains an API that can be used to monitor specific
13 performance events in one or several functional blocks.
14
15 \author Paul Cercueil
16*/
17
18#ifndef __KOS_PERF_MONITOR_H
19#define __KOS_PERF_MONITOR_H
20
21#include <sys/cdefs.h>
22__BEGIN_DECLS
23
24#include <dc/perfctr.h>
25#include <stdint.h>
26#include <stdio.h>
27
28/** \defgroup perf_monitor Performance monitor
29 \brief Code performance monitor
30 \ingroup debugging
31
32 The performance monitor API is built on top of the performance counter API,
33 and as such cannot be used at the same time.
34 With this API, programs can set probe points in different functional blocks
35 and later obtain statistics about the execution of said functional blocks.
36
37 @{
38*/
39
40/** \cond */
41struct perf_monitor {
42 const char *fn;
43 unsigned int line;
44 uint64_t calls;
45 uint64_t time_ns, time_start;
46 uint64_t event0, event0_start;
47 uint64_t event1, event1_start;
48};
49
50void __stop_perf_monitor(struct perf_monitor **monitor);
51
52struct perf_monitor *__start_perf_monitor(struct perf_monitor *monitor);
53
54#define __perf_monitor(f, l) \
55 static struct perf_monitor __perf_monitor_##l \
56 __attribute__((section(".monitors"))) = { f, l, }; \
57 struct perf_monitor *___perf_monitor_##l \
58 __attribute__((cleanup(__stop_perf_monitor))) = \
59 __start_perf_monitor(&__perf_monitor_##l)
60
61#define _perf_monitor(f, l) __perf_monitor(f, l)
62
63#define __perf_monitor_if(f, l, tst) ({ \
64 static struct perf_monitor __perf_monitor_##l \
65 __attribute__((section(".monitors"))) = { f, l, }; \
66 __perf_monitor_##l.calls++; \
67 (tst) ? (__perf_monitor_##l.event1++,1) : (__perf_monitor_##l.event0++,0); \
68})
69
70#define _perf_monitor_if(f, l, tst) __perf_monitor_if(f, l, tst)
71/** \endcond */
72
73/** \brief Register a performance monitor in the current functional block
74
75 The performance monitor will run from the moment this macro is used, till
76 the end of the functional block.
77*/
78#define perf_monitor() _perf_monitor(__func__, __LINE__)
79
80/** \brief Register a performance monitor for branch likeliness analysis
81
82 This macro is designed to be used inside an "if" expression, for instance:
83 if (perf_monitor_if(!strcmp("test", str))) { ... }
84
85 The resulting performance monitor will measure the number of calls, and
86 the number of times the branch was taken (in event1) and the number of
87 time it was not (in event0).
88
89 \param tst The boolean expression that is normally used inside
90 the "if" check
91*/
92#define perf_monitor_if(tst) _perf_monitor_if(__func__, __LINE__, tst)
93
94/** \brief Initialize the performance monitor system
95
96 Set up the performance monitor system. Note that using the performance
97 monitor system will conflict with any external usage of the performance
98 counter API.
99
100 \param event1 The first event mode (pef_cntr_event_t).
101 \param event2 The second event mode (pef_cntr_event_t).
102*/
104
105/** \brief De-initialize the performance monitor system
106
107 After this function is called, the performance counter API can be
108 used again.
109*/
111
112/** \brief Print statistics about the probe points to the given file descriptor
113 \param f A valid file descriptor to which the messages will
114 be printed. Use "stdout" for the standard output.
115*/
116void perf_monitor_print(FILE *f);
117
118/** @} */
119
120__END_DECLS
121#endif /* __KOS_PERF_MONITOR_H */
perf_cntr_event_t
Performance Counter Event Modes.
Definition perfctr.h:90
#define perf_monitor()
Register a performance monitor in the current functional block.
Definition perf_monitor.h:78
void perf_monitor_print(FILE *f)
Print statistics about the probe points to the given file descriptor.
void perf_monitor_exit(void)
De-initialize the performance monitor system.
void perf_monitor_init(perf_cntr_event_t event1, perf_cntr_event_t event2)
Initialize the performance monitor system.
Low-level performance counter API.
Basic sys/stdio.h file from newlib.