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