xref: /netbsd-src/external/gpl3/gdb/dist/sim/common/sim-profile.h (revision 88241920d21b339bf319c0e979ffda80c49a2936)
1 /* Profile header for simulators using common framework.
2    Copyright (C) 1996-2024 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.
4 
5 This file is part of GDB, the GNU debugger.
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef SIM_PROFILE_H
21 #define SIM_PROFILE_H
22 
23 #ifndef WITH_PROFILE
24 #error "WITH_PROFILE not defined"
25 #endif
26 
27 /* Standard profilable entities.  */
28 
29 enum {
30   /* Profile insn usage.  */
31   PROFILE_INSN_IDX = 1,
32 
33   /* Profile memory usage.  */
34   PROFILE_MEMORY_IDX,
35 
36   /* Profile the cpu model (cycles, etc.).  */
37   PROFILE_MODEL_IDX,
38 
39   /* Profile the simulator's execution cache.  */
40   PROFILE_SCACHE_IDX,
41 
42   /* Profile the PC.  */
43   PROFILE_PC_IDX,
44 
45   /* Profile sim-core.c stuff.  */
46   /* ??? The difference between this and PROFILE_MEMORY_IDX is ... ?  */
47   PROFILE_CORE_IDX,
48 
49   /* Simulator specific profile bits begin here.  */
50   PROFILE_NEXT_IDX
51 };
52 
53 /* Maximum number of profilable entities.  */
54 #ifndef MAX_PROFILE_VALUES
55 #define MAX_PROFILE_VALUES 32
56 #endif
57 
58 /* The -p option only prints useful values.  It's easy to type and shouldn't
59    splat on the screen everything under the sun making nothing easy to
60    find.  */
61 #define PROFILE_USEFUL_MASK \
62 ((1 << PROFILE_INSN_IDX) \
63  | (1 << PROFILE_MEMORY_IDX) \
64  | (1 << PROFILE_MODEL_IDX) \
65  | (1 << PROFILE_CORE_IDX))
66 
67 /* Utility to set profile options.  */
68 SIM_RC set_profile_option_mask (SIM_DESC sd_, const char *name_, int mask_,
69 				const char *arg_);
70 
71 /* Utility to parse a --profile-<foo> option.  */
72 /* ??? On the one hand all calls could be confined to sim-profile.c, but
73    on the other hand keeping a module's profiling option with the module's
74    source is cleaner.   */
75 
76 SIM_RC sim_profile_set_option (SIM_DESC sd_, const char *name_, int idx_,
77 			       const char *arg_);
78 
79 /* Masks so WITH_PROFILE can have symbolic values.
80    The case choice here is on purpose.  The lowercase parts are args to
81    --with-profile.  */
82 #define PROFILE_insn   (1 << PROFILE_INSN_IDX)
83 #define PROFILE_memory (1 << PROFILE_MEMORY_IDX)
84 #define PROFILE_model  (1 << PROFILE_MODEL_IDX)
85 #define PROFILE_scache (1 << PROFILE_SCACHE_IDX)
86 #define PROFILE_pc     (1 << PROFILE_PC_IDX)
87 #define PROFILE_core   (1 << PROFILE_CORE_IDX)
88 
89 /* Preprocessor macros to simplify tests of WITH_PROFILE.  */
90 #define WITH_PROFILE_INSN_P   ((WITH_PROFILE & PROFILE_insn) != 0)
91 #define WITH_PROFILE_MEMORY_P ((WITH_PROFILE & PROFILE_memory) != 0)
92 #define WITH_PROFILE_MODEL_P  ((WITH_PROFILE & PROFILE_model) != 0)
93 #define WITH_PROFILE_SCACHE_P ((WITH_PROFILE & PROFILE_scache) != 0)
94 #define WITH_PROFILE_PC_P     ((WITH_PROFILE & PROFILE_pc) != 0)
95 #define WITH_PROFILE_CORE_P   ((WITH_PROFILE & PROFILE_core) != 0)
96 
97 /* If MAX_TARGET_MODES isn't defined, we can't do memory profiling.
98    ??? It is intended that this is a temporary occurrence.  Normally
99    MAX_TARGET_MODES is defined.  */
100 #ifndef MAX_TARGET_MODES
101 #undef WITH_PROFILE_MEMORY_P
102 #define WITH_PROFILE_MEMORY_P 0
103 #endif
104 
105 /* Output format macros.  */
106 #ifndef PROFILE_HISTOGRAM_WIDTH
107 #define PROFILE_HISTOGRAM_WIDTH 40
108 #endif
109 #ifndef PROFILE_LABEL_WIDTH
110 #define PROFILE_LABEL_WIDTH 32
111 #endif
112 
113 /* Callbacks for internal profile_info.
114    The callbacks may be NULL meaning there isn't one.
115    Note that results are indented two spaces to distinguish them from
116    section titles.
117    If non-NULL, PROFILE_CALLBACK is called to print extra non-cpu related data.
118    If non-NULL, PROFILE_CPU_CALLBACK is called to print extra cpu related data.
119    */
120 
121 typedef void (PROFILE_INFO_CALLBACK_FN) (SIM_DESC, int);
122 struct _sim_cpu; /* forward reference */
123 typedef void (PROFILE_INFO_CPU_CALLBACK_FN) (struct _sim_cpu *cpu, bool verbose);
124 
125 
126 /* Struct containing most profiling data.
127    It doesn't contain all profiling data because for example scache data
128    is kept with the rest of scache support.  */
129 
130 typedef struct {
131   /* Global summary of all the current profiling options.  */
132   char profile_any_p;
133 
134   /* Boolean array of specified profiling flags.  */
135   char profile_flags[MAX_PROFILE_VALUES];
136 #define PROFILE_FLAGS(p) ((p)->profile_flags)
137 
138   /* The total insn count is tracked separately.
139      It is always computed, regardless of insn profiling.  */
140   unsigned long total_insn_count;
141 #define PROFILE_TOTAL_INSN_COUNT(p) ((p)->total_insn_count)
142 
143   /* CPU frequency.  Always accepted, regardless of profiling options.  */
144   unsigned long cpu_freq;
145 #define PROFILE_CPU_FREQ(p) ((p)->cpu_freq)
146 
147 #if WITH_PROFILE_INSN_P
148   unsigned int *insn_count;
149 #define PROFILE_INSN_COUNT(p) ((p)->insn_count)
150 #endif
151 
152 #if WITH_PROFILE_MEMORY_P
153   unsigned int read_count[MAX_TARGET_MODES];
154 #define PROFILE_READ_COUNT(p) ((p)->read_count)
155   unsigned int write_count[MAX_TARGET_MODES];
156 #define PROFILE_WRITE_COUNT(p) ((p)->write_count)
157 #endif
158 
159 #if WITH_PROFILE_CORE_P
160   /* Count read/write/exec accesses separatly. */
161   unsigned int core_count[nr_maps];
162 #define PROFILE_CORE_COUNT(p) ((p)->core_count)
163 #endif
164 
165 #if WITH_PROFILE_MODEL_P
166   /* ??? Quick hack until more elaborate scheme is finished.  */
167   /* Total cycle count, including stalls.  */
168   unsigned long total_cycles;
169 #define PROFILE_MODEL_TOTAL_CYCLES(p) ((p)->total_cycles)
170   /* Stalls due to branches.  */
171   unsigned long cti_stall_cycles;
172 #define PROFILE_MODEL_CTI_STALL_CYCLES(p) ((p)->cti_stall_cycles)
173   unsigned long load_stall_cycles;
174 #define PROFILE_MODEL_LOAD_STALL_CYCLES(p) ((p)->load_stall_cycles)
175   /* Number of cycles the current instruction took.  */
176   unsigned long cur_insn_cycles;
177 #define PROFILE_MODEL_CUR_INSN_CYCLES(p) ((p)->cur_insn_cycles)
178 
179   /* Taken and not-taken branches (and other cti's).  */
180   unsigned long taken_count, untaken_count;
181 #define PROFILE_MODEL_TAKEN_COUNT(p) ((p)->taken_count)
182 #define PROFILE_MODEL_UNTAKEN_COUNT(p) ((p)->untaken_count)
183 #endif
184 
185 #if WITH_PROFILE_PC_P
186   /* PC profiling attempts to determine function usage by sampling the PC
187      every so many instructions.  */
188   unsigned int profile_pc_freq;
189 #define PROFILE_PC_FREQ(p) ((p)->profile_pc_freq)
190   unsigned int profile_pc_nr_buckets;
191 #define PROFILE_PC_NR_BUCKETS(p) ((p)->profile_pc_nr_buckets)
192   address_word profile_pc_start;
193 #define PROFILE_PC_START(p) ((p)->profile_pc_start)
194   address_word profile_pc_end;
195 #define PROFILE_PC_END(p) ((p)->profile_pc_end)
196   unsigned profile_pc_shift;
197 #define PROFILE_PC_SHIFT(p) ((p)->profile_pc_shift)
198 #define PROFILE_PC_BUCKET_SIZE(p) (PROFILE_PC_SHIFT (p) ? (1 << PROFILE_PC_SHIFT (p)) : 0)
199   unsigned *profile_pc_count;
200 #define PROFILE_PC_COUNT(p) ((p)->profile_pc_count)
201   sim_event *profile_pc_event;
202 #define PROFILE_PC_EVENT(p) ((p)->profile_pc_event)
203 #endif
204 
205   /* Profile output goes to this or stderr if NULL.
206      We can't store `stderr' here as stderr goes through a callback.  */
207   FILE *profile_file;
208 #define PROFILE_FILE(p) ((p)->profile_file)
209 
210   /* When reporting a profile summary, hook to include per-processor
211      target specific profile information */
212   PROFILE_INFO_CPU_CALLBACK_FN *info_cpu_callback;
213 #define PROFILE_INFO_CPU_CALLBACK(p) ((p)->info_cpu_callback)
214 
215   /* When reporting a profile summary, hook to include common target
216      specific profile information */
217   PROFILE_INFO_CALLBACK_FN *info_callback;
218 #define STATE_PROFILE_INFO_CALLBACK(sd) \
219 (CPU_PROFILE_DATA (STATE_CPU (sd, 0))->info_callback)
220 
221   /* Profile range.
222      ??? Not all cpu's support this.  */
223   ADDR_RANGE range;
224 #define PROFILE_RANGE(p) (& (p)->range)
225 } PROFILE_DATA;
226 
227 /* Predicates.  */
228 
229 #define CPU_PROFILE_FLAGS(cpu) PROFILE_FLAGS (CPU_PROFILE_DATA (cpu))
230 
231 /* Return non-zero if tracing of IDX is enabled for CPU.  */
232 #define PROFILE_P(cpu,idx) \
233 ((WITH_PROFILE & (1 << (idx))) != 0 \
234  && CPU_PROFILE_FLAGS (cpu)[idx] != 0)
235 
236 /* Non-zero if --profile-<xxxx> was specified for CPU.  */
237 #define PROFILE_ANY_P(cpu)	((WITH_PROFILE) && (CPU_PROFILE_DATA (cpu)->profile_any_p))
238 #define PROFILE_INSN_P(cpu)	PROFILE_P (cpu, PROFILE_INSN_IDX)
239 #define PROFILE_MEMORY_P(cpu)	PROFILE_P (cpu, PROFILE_MEMORY_IDX)
240 #define PROFILE_MODEL_P(cpu)	PROFILE_P (cpu, PROFILE_MODEL_IDX)
241 #define PROFILE_SCACHE_P(cpu)	PROFILE_P (cpu, PROFILE_SCACHE_IDX)
242 #define PROFILE_PC_P(cpu)	PROFILE_P (cpu, PROFILE_PC_IDX)
243 #define PROFILE_CORE_P(cpu)	PROFILE_P (cpu, PROFILE_CORE_IDX)
244 
245 /* Usage macros.  */
246 
247 #if WITH_PROFILE_INSN_P
248 #define PROFILE_COUNT_INSN(cpu, pc, insn_num) \
249 do { \
250   if (PROFILE_INSN_P (cpu)) \
251     ++ PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu)) [insn_num]; \
252 } while (0)
253 #else
254 #define PROFILE_COUNT_INSN(cpu, pc, insn_num)
255 #endif /* ! insn */
256 
257 #if WITH_PROFILE_MEMORY_P
258 #define PROFILE_COUNT_READ(cpu, addr, mode_num) \
259 do { \
260   if (PROFILE_MEMORY_P (cpu)) \
261     ++ PROFILE_READ_COUNT (CPU_PROFILE_DATA (cpu)) [mode_num]; \
262 } while (0)
263 #define PROFILE_COUNT_WRITE(cpu, addr, mode_num) \
264 do { \
265   if (PROFILE_MEMORY_P (cpu)) \
266     ++ PROFILE_WRITE_COUNT (CPU_PROFILE_DATA (cpu)) [mode_num]; \
267 } while (0)
268 #else
269 #define PROFILE_COUNT_READ(cpu, addr, mode_num)
270 #define PROFILE_COUNT_WRITE(cpu, addr, mode_num)
271 #endif /* ! memory */
272 
273 #if WITH_PROFILE_CORE_P
274 #define PROFILE_COUNT_CORE(cpu, addr, size, map) \
275 do { \
276   if (PROFILE_CORE_P (cpu)) \
277     PROFILE_CORE_COUNT (CPU_PROFILE_DATA (cpu)) [map] += 1; \
278 } while (0)
279 #else
280 #define PROFILE_COUNT_CORE(cpu, addr, size, map)
281 #endif /* ! core */
282 
283 #if WITH_PROFILE_MODEL_P
284 #define PROFILE_BRANCH_TAKEN(cpu) \
285 do { \
286   if (PROFILE_MODEL_P (cpu)) \
287     ++ PROFILE_MODEL_TAKEN_COUNT (CPU_PROFILE_DATA (cpu)); \
288 } while (0)
289 #define PROFILE_BRANCH_UNTAKEN(cpu) \
290 do { \
291   if (PROFILE_MODEL_P (cpu)) \
292     ++ PROFILE_MODEL_UNTAKEN_COUNT (CPU_PROFILE_DATA (cpu)); \
293 } while (0)
294 #else
295 #define PROFILE_BRANCH_TAKEN(cpu)
296 #define PROFILE_BRANCH_UNTAKEN(cpu)
297 #endif /* ! model */
298 
299 /* Misc. utilities.  */
300 
301 extern void sim_profile_print_bar (SIM_DESC, sim_cpu *, unsigned int, unsigned int, unsigned int);
302 
303 #endif /* SIM_PROFILE_H */
304