1 /* Default profiling support.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
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 2, or (at your option)
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.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "sim-options.h"
25 static MODULE_UNINSTALL_FN profile_uninstall
;
27 static void print_bar (SIM_DESC
, unsigned int, unsigned int, unsigned int);
29 static DECLARE_OPTION_HANDLER (profile_option_handler
);
31 #define OPTION_PROFILE_INSN (OPTION_START + 0)
32 #define OPTION_PROFILE_MEMORY (OPTION_START + 1)
33 #define OPTION_PROFILE_MODEL (OPTION_START + 2)
34 #define OPTION_PROFILE_FILE (OPTION_START + 3)
35 #define OPTION_PROFILE_RANGE (OPTION_START + 4)
37 static const OPTION profile_options
[] = {
38 { {"profile", no_argument
, NULL
, 'p'},
39 'p', NULL
, "Perform profiling",
40 profile_option_handler
},
41 { {"profile-insn", no_argument
, NULL
, OPTION_PROFILE_INSN
},
42 '\0', NULL
, "Perform instruction profiling",
43 profile_option_handler
},
44 { {"profile-memory", no_argument
, NULL
, OPTION_PROFILE_MEMORY
},
45 '\0', NULL
, "Perform memory profiling",
46 profile_option_handler
},
47 { {"profile-model", no_argument
, NULL
, OPTION_PROFILE_MODEL
},
48 '\0', NULL
, "Perform model profiling",
49 profile_option_handler
},
50 { {"profile-file", required_argument
, NULL
, OPTION_PROFILE_FILE
},
51 '\0', "FILE NAME", "Specify profile output file",
52 profile_option_handler
},
53 { {"profile-pc-frequency", required_argument
, NULL
, 'F'},
54 'F', "PC PROFILE FREQUENCY", "Turn on PC profiling at specified frequency",
55 profile_option_handler
},
56 { {"profile-pc-size", required_argument
, NULL
, 'S'},
57 'S', "PC PROFILE SIZE", "Specify PC profiling size",
58 profile_option_handler
},
60 { {"profile-range", required_argument
, NULL
, OPTION_PROFILE_RANGE
},
61 0, NULL
, "Specify range of addresses to profile",
62 profile_option_handler
},
64 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
68 profile_option_handler (SIM_DESC sd
, int opt
, char *arg
)
76 sim_io_eprintf (sd
, "Profiling not compiled in, -p option ignored\n");
79 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
80 for (i
= 0; i
< MAX_PROFILE_VALUES
; ++i
)
81 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[i
] = 1;
85 case OPTION_PROFILE_INSN
:
86 #if WITH_PROFILE_INSN_P
87 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
88 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_INSN_IDX
] = 1;
90 sim_io_eprintf (sd
, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
94 case OPTION_PROFILE_MEMORY
:
95 #if WITH_PROFILE_MEMORY_P
96 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
97 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_MEMORY_IDX
] = 1;
99 sim_io_eprintf (sd
, "Memory profiling not compiled in, `--profile-memory' ignored\n");
103 case OPTION_PROFILE_MODEL
:
104 #if WITH_PROFILE_MODEL_P
105 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
106 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_MODEL_IDX
] = 1;
108 sim_io_eprintf (sd
, "Model profiling not compiled in, `--profile-model' ignored\n");
112 case OPTION_PROFILE_FILE
:
113 /* FIXME: Might want this to apply to pc profiling only,
114 or have two profile file options. */
116 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-file' ignored\n");
119 FILE *f
= fopen (arg
, "w");
123 sim_io_eprintf (sd
, "Unable to open profile output file `%s'\n", arg
);
126 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
127 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, n
))) = f
;
132 #if WITH_PROFILE_PC_P
133 /* FIXME: Validate arg. */
135 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
136 PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
, n
))) = i
;
138 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
143 #if WITH_PROFILE_PC_P
144 /* FIXME: Validate arg. */
146 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
147 PROFILE_PC_SIZE (CPU_PROFILE_DATA (STATE_CPU (sd
, n
))) = i
;
149 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
153 #if 0 /* FIXME:wip */
154 case OPTION_PROFILE_RANGE
:
163 profile_install (SIM_DESC sd
)
167 sim_add_option_table (sd
, profile_options
);
168 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
169 memset (CPU_PROFILE_DATA (STATE_CPU (sd
, i
)), 0,
170 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd
, i
))));
171 sim_module_add_uninstall_fn (sd
, profile_uninstall
);
176 profile_uninstall (SIM_DESC sd
)
180 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
182 PROFILE_DATA
*data
= CPU_PROFILE_DATA (STATE_CPU (sd
, i
));
183 if (PROFILE_FILE (data
) != NULL
)
184 fclose (PROFILE_FILE (data
));
188 #if WITH_PROFILE_INSN_P
191 profile_print_insn (sim_cpu
*cpu
, int verbose
)
193 unsigned int i
, n
, total
, max_val
, max_name_len
;
194 SIM_DESC sd
= CPU_STATE (cpu
);
195 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
197 sim_io_printf (sd
, "Instruction Statistics\n\n");
199 /* First pass over data computes various things. */
200 max_val
= total
= max_name_len
= 0;
201 for (i
= 1; i
< MAX_INSNS
; ++i
)
203 total
+= PROFILE_INSN_COUNT (data
) [i
];
204 if (PROFILE_INSN_COUNT (data
) [i
] > max_val
)
205 max_val
= PROFILE_INSN_COUNT (data
) [i
];
206 n
= strlen (INSN_NAME (i
));
207 if (n
> max_name_len
)
211 sim_io_printf (sd
, " Total: %d insns\n", total
);
213 if (verbose
&& max_val
!= 0)
215 /* Now we can print the histogram. */
216 sim_io_printf (sd
, "\n");
217 for (i
= 1; i
< MAX_INSNS
; ++i
)
219 if (PROFILE_INSN_COUNT (data
) [i
] != 0)
221 sim_io_printf (sd
, " %*s: %*d: ",
222 max_name_len
, INSN_NAME (i
),
223 max_val
< 10000 ? 4 : 8,
224 PROFILE_INSN_COUNT (data
) [i
]);
225 print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
226 PROFILE_INSN_COUNT (data
) [i
],
228 sim_io_printf (sd
, "\n");
233 sim_io_printf (sd
, "\n");
238 #if WITH_PROFILE_MEMORY_P
241 profile_print_memory (sim_cpu
*cpu
, int verbose
)
244 unsigned int total_read
, total_write
;
245 unsigned int max_val
, max_name_len
;
246 /* FIXME: Need to add smp support. */
247 SIM_DESC sd
= CPU_STATE (cpu
);
248 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
250 sim_io_printf (sd
, "Memory Access Statistics\n\n");
252 /* First pass over data computes various things. */
253 max_val
= total_read
= total_write
= max_name_len
= 0;
254 for (i
= 0; i
< MAX_MODES
; ++i
)
256 total_read
+= PROFILE_READ_COUNT (data
) [i
];
257 total_write
+= PROFILE_WRITE_COUNT (data
) [i
];
258 if (PROFILE_READ_COUNT (data
) [i
] > max_val
)
259 max_val
= PROFILE_READ_COUNT (data
) [i
];
260 if (PROFILE_WRITE_COUNT (data
) [i
] > max_val
)
261 max_val
= PROFILE_WRITE_COUNT (data
) [i
];
262 n
= strlen (MODE_NAME (i
));
263 if (n
> max_name_len
)
267 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
268 sim_io_printf (sd
, " Total read: %d accesses\n", total_read
);
269 sim_io_printf (sd
, " Total write: %d accesses\n", total_write
);
271 if (verbose
&& max_val
!= 0)
273 /* FIXME: Need to separate instruction fetches from data fetches
274 as the former swamps the latter. */
275 /* Now we can print the histogram. */
276 sim_io_printf (sd
, "\n");
277 for (i
= 0; i
< MAX_MODES
; ++i
)
279 if (PROFILE_READ_COUNT (data
) [i
] != 0)
281 sim_io_printf (sd
, " %*s read: %*d: ",
282 max_name_len
, MODE_NAME (i
),
283 max_val
< 10000 ? 4 : 8,
284 PROFILE_READ_COUNT (data
) [i
]);
285 print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
286 PROFILE_READ_COUNT (data
) [i
],
288 sim_io_printf (sd
, "\n");
290 if (PROFILE_WRITE_COUNT (data
) [i
] != 0)
292 sim_io_printf (sd
, " %*s write: %*d: ",
293 max_name_len
, MODE_NAME (i
),
294 max_val
< 10000 ? 4 : 8,
295 PROFILE_WRITE_COUNT (data
) [i
]);
296 print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
297 PROFILE_WRITE_COUNT (data
) [i
],
299 sim_io_printf (sd
, "\n");
304 sim_io_printf (sd
, "\n");
309 #if WITH_PROFILE_MODEL_P
312 profile_print_model (sim_cpu
*cpu
, int verbose
)
314 SIM_DESC sd
= CPU_STATE (cpu
);
315 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
316 unsigned long cti_stalls
= PROFILE_MODEL_CTI_STALL_COUNT (data
);
317 unsigned long load_stalls
= PROFILE_MODEL_LOAD_STALL_COUNT (data
);
318 unsigned long total
= PROFILE_MODEL_CYCLE_COUNT (data
)
319 + cti_stalls
+ load_stalls
;
321 sim_io_printf (sd
, "Model %s Timing Information\n\n",
322 MODEL_NAME (STATE_MODEL (sd
)));
323 sim_io_printf (sd
, " %-*s %ld\n",
324 PROFILE_LABEL_WIDTH
, "Taken branches:",
325 PROFILE_MODEL_TAKEN_COUNT (data
));
326 sim_io_printf (sd
, " %-*s %ld\n",
327 PROFILE_LABEL_WIDTH
, "Untaken branches:",
328 PROFILE_MODEL_UNTAKEN_COUNT (data
));
329 sim_io_printf (sd
, " %-*s %ld\n",
330 PROFILE_LABEL_WIDTH
, "Cycles stalled due to branches:",
332 sim_io_printf (sd
, " %-*s %ld\n",
333 PROFILE_LABEL_WIDTH
, "Cycles stalled due to loads:",
335 sim_io_printf (sd
, " %-*s %ld\n",
336 PROFILE_LABEL_WIDTH
, "Total cycles (*approximate*):",
338 sim_io_printf (sd
, "\n");
344 print_bar (SIM_DESC sd
, unsigned int width
,
345 unsigned int val
, unsigned int max_val
)
347 unsigned int i
, count
;
349 count
= ((double) val
/ (double) max_val
) * (double) width
;
351 for (i
= 0; i
< count
; ++i
)
352 sim_io_printf (sd
, "*");
355 /* Print the simulator's execution speed for CPU. */
358 profile_print_speed (sim_cpu
*cpu
)
360 SIM_DESC sd
= CPU_STATE (cpu
);
361 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
362 unsigned long milliseconds
= PROFILE_EXEC_TIME (data
);
363 unsigned long total
= PROFILE_TOTAL_INSN_COUNT (data
);
365 sim_io_printf (sd
, "Simulator Execution Speed\n\n");
368 sim_io_printf (sd
, " Total instructions: %ld\n", total
);
369 if (milliseconds
< 1000)
370 sim_io_printf (sd
, " Total Execution Time: < 1 second\n\n");
373 sim_io_printf (sd
, " Total Execution Time: %.2f seconds\n",
374 (double) milliseconds
/ 1000);
375 /* Don't confuse things with data that isn't useful.
376 If we ran for less than two seconds, only use the data if we
377 executed more than 100,000 insns. */
378 if (milliseconds
>= 2000 || total
>= 100000)
379 sim_io_printf (sd
, " Simulator Speed: %.0f insns/second\n\n",
380 (double) total
/ ((double) milliseconds
/ 1000));
384 /* Top level function to print all summary profile information.
385 It is [currently] intended that all such data is printed by this function.
386 I'd rather keep it all in one place for now. To that end, MISC_CPU and
387 MISC are callbacks used to print any miscellaneous data.
389 One might want to add a user option that allows printing by type or by cpu
390 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
391 This may be a case of featuritis so it's currently left out.
393 Note that results are indented two spaces to distinguish them from
397 profile_print (SIM_DESC sd
, int verbose
,
398 PROFILE_CALLBACK
*misc
, PROFILE_CPU_CALLBACK
*misc_cpu
)
401 int print_title_p
= 0;
403 /* Only print the title if some data has been collected. */
404 /* FIXME: If the number of processors can be selected on the command line,
405 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
407 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
409 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
410 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
412 for (i
= 0; i
< MAX_PROFILE_VALUES
; ++i
)
413 if (PROFILE_FLAGS (data
) [i
])
415 /* One could break out early if print_title_p is set. */
418 sim_io_printf (sd
, "Summary profiling results:\n\n");
420 /* Loop, cpu by cpu, printing results. */
422 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
424 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
425 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
427 if (MAX_NR_PROCESSORS
> 1)
428 sim_io_printf (sd
, "CPU %d\n\n", c
);
430 #if WITH_PROFILE_INSN_P
431 if (PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
432 profile_print_insn (cpu
, verbose
);
435 #if WITH_PROFILE_MEMORY_P
436 if (PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
])
437 profile_print_memory (cpu
, verbose
);
440 #if WITH_PROFILE_MODEL_P
441 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
442 profile_print_model (cpu
, verbose
);
445 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
446 if (PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
])
447 scache_print_profile (cpu
, verbose
);
450 /* Print cpu-specific data before the execution speed. */
451 if (misc_cpu
!= NULL
)
452 (*misc_cpu
) (cpu
, verbose
);
454 /* Always try to print execution time and speed. */
456 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
457 profile_print_speed (cpu
);
460 /* Finally print non-cpu specific miscellaneous data. */
463 (*misc
) (sd
, verbose
);