* Makefile.in (sim-options_h): Define.
[binutils-gdb.git] / sim / common / sim-profile.c
1 /* Default profiling support.
2 Copyright (C) 1996, 1997 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 2, or (at your option)
10 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 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. */
20
21 #include "sim-main.h"
22 #include "sim-io.h"
23 #include "sim-options.h"
24
25 static MODULE_UNINSTALL_FN profile_uninstall;
26
27 static void print_bar (SIM_DESC, unsigned int, unsigned int, unsigned int);
28
29 static DECLARE_OPTION_HANDLER (profile_option_handler);
30
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)
36
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 },
59 #if 0 /*FIXME:wip*/
60 { {"profile-range", required_argument, NULL, OPTION_PROFILE_RANGE},
61 0, NULL, "Specify range of addresses to profile",
62 profile_option_handler },
63 #endif
64 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
65 };
66
67 static SIM_RC
68 profile_option_handler (SIM_DESC sd, int opt, char *arg)
69 {
70 int i,n;
71
72 switch (opt)
73 {
74 case 'p' :
75 if (! WITH_PROFILE)
76 sim_io_eprintf (sd, "Profiling not compiled in, -p option ignored\n");
77 else
78 {
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;
82 }
83 break;
84
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;
89 #else
90 sim_io_eprintf (sd, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
91 #endif
92 break;
93
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;
98 #else
99 sim_io_eprintf (sd, "Memory profiling not compiled in, `--profile-memory' ignored\n");
100 #endif
101 break;
102
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;
107 #else
108 sim_io_eprintf (sd, "Model profiling not compiled in, `--profile-model' ignored\n");
109 #endif
110 break;
111
112 case OPTION_PROFILE_FILE :
113 /* FIXME: Might want this to apply to pc profiling only,
114 or have two profile file options. */
115 if (! WITH_PROFILE)
116 sim_io_eprintf (sd, "Profiling not compiled in, `--profile-file' ignored\n");
117 else
118 {
119 FILE *f = fopen (arg, "w");
120
121 if (f == NULL)
122 {
123 sim_io_eprintf (sd, "Unable to open profile output file `%s'\n", arg);
124 return SIM_RC_FAIL;
125 }
126 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
127 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd, n))) = f;
128 }
129 break;
130
131 case 'F' :
132 #if WITH_PROFILE_PC_P
133 /* FIXME: Validate arg. */
134 i = atoi (arg);
135 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
136 PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd, n))) = i;
137 #else
138 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
139 #endif
140 break;
141
142 case 'S' :
143 #if WITH_PROFILE_PC_P
144 /* FIXME: Validate arg. */
145 i = atoi (arg);
146 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
147 PROFILE_PC_SIZE (CPU_PROFILE_DATA (STATE_CPU (sd, n))) = i;
148 #else
149 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
150 #endif
151 break;
152
153 #if 0 /* FIXME:wip */
154 case OPTION_PROFILE_RANGE :
155 break;
156 #endif
157 }
158
159 return SIM_RC_OK;
160 }
161
162 SIM_RC
163 profile_install (SIM_DESC sd)
164 {
165 int i;
166
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);
172 return SIM_RC_OK;
173 }
174
175 static void
176 profile_uninstall (SIM_DESC sd)
177 {
178 int i;
179
180 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
181 {
182 PROFILE_DATA *data = CPU_PROFILE_DATA (STATE_CPU (sd, i));
183 if (PROFILE_FILE (data) != NULL)
184 fclose (PROFILE_FILE (data));
185 }
186 }
187
188 #if WITH_PROFILE_INSN_P
189
190 static void
191 profile_print_insn (sim_cpu *cpu, int verbose)
192 {
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);
196
197 sim_io_printf (sd, "Instruction Statistics\n\n");
198
199 /* First pass over data computes various things. */
200 max_val = total = max_name_len = 0;
201 for (i = 1; i < MAX_INSNS; ++i)
202 {
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)
208 max_name_len = n;
209 }
210
211 sim_io_printf (sd, " Total: %d insns\n", total);
212
213 if (verbose && max_val != 0)
214 {
215 /* Now we can print the histogram. */
216 sim_io_printf (sd, "\n");
217 for (i = 1; i < MAX_INSNS; ++i)
218 {
219 if (PROFILE_INSN_COUNT (data) [i] != 0)
220 {
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],
227 max_val);
228 sim_io_printf (sd, "\n");
229 }
230 }
231 }
232
233 sim_io_printf (sd, "\n");
234 }
235
236 #endif
237
238 #if WITH_PROFILE_MEMORY_P
239
240 static void
241 profile_print_memory (sim_cpu *cpu, int verbose)
242 {
243 unsigned int i, n;
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);
249
250 sim_io_printf (sd, "Memory Access Statistics\n\n");
251
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)
255 {
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)
264 max_name_len = n;
265 }
266
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);
270
271 if (verbose && max_val != 0)
272 {
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)
278 {
279 if (PROFILE_READ_COUNT (data) [i] != 0)
280 {
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],
287 max_val);
288 sim_io_printf (sd, "\n");
289 }
290 if (PROFILE_WRITE_COUNT (data) [i] != 0)
291 {
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],
298 max_val);
299 sim_io_printf (sd, "\n");
300 }
301 }
302 }
303
304 sim_io_printf (sd, "\n");
305 }
306
307 #endif
308
309 #if WITH_PROFILE_MODEL_P
310
311 static void
312 profile_print_model (sim_cpu *cpu, int verbose)
313 {
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;
320
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:",
331 cti_stalls);
332 sim_io_printf (sd, " %-*s %ld\n",
333 PROFILE_LABEL_WIDTH, "Cycles stalled due to loads:",
334 load_stalls);
335 sim_io_printf (sd, " %-*s %ld\n",
336 PROFILE_LABEL_WIDTH, "Total cycles (*approximate*):",
337 total);
338 sim_io_printf (sd, "\n");
339 }
340
341 #endif
342
343 static void
344 print_bar (SIM_DESC sd, unsigned int width,
345 unsigned int val, unsigned int max_val)
346 {
347 unsigned int i, count;
348
349 count = ((double) val / (double) max_val) * (double) width;
350
351 for (i = 0; i < count; ++i)
352 sim_io_printf (sd, "*");
353 }
354
355 /* Print the simulator's execution speed for CPU. */
356
357 static void
358 profile_print_speed (sim_cpu *cpu)
359 {
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);
364
365 sim_io_printf (sd, "Simulator Execution Speed\n\n");
366
367 if (total != 0)
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");
371 else
372 {
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));
381 }
382 }
383
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.
388
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.
392
393 Note that results are indented two spaces to distinguish them from
394 section titles. */
395
396 void
397 profile_print (SIM_DESC sd, int verbose,
398 PROFILE_CALLBACK *misc, PROFILE_CPU_CALLBACK *misc_cpu)
399 {
400 int i,c;
401 int print_title_p = 0;
402
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'. */
406
407 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
408 {
409 sim_cpu *cpu = STATE_CPU (sd, c);
410 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
411
412 for (i = 0; i < MAX_PROFILE_VALUES; ++i)
413 if (PROFILE_FLAGS (data) [i])
414 print_title_p = 1;
415 /* One could break out early if print_title_p is set. */
416 }
417 if (print_title_p)
418 sim_io_printf (sd, "Summary profiling results:\n\n");
419
420 /* Loop, cpu by cpu, printing results. */
421
422 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
423 {
424 sim_cpu *cpu = STATE_CPU (sd, c);
425 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
426
427 if (MAX_NR_PROCESSORS > 1)
428 sim_io_printf (sd, "CPU %d\n\n", c);
429
430 #if WITH_PROFILE_INSN_P
431 if (PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
432 profile_print_insn (cpu, verbose);
433 #endif
434
435 #if WITH_PROFILE_MEMORY_P
436 if (PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX])
437 profile_print_memory (cpu, verbose);
438 #endif
439
440 #if WITH_PROFILE_MODEL_P
441 if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX])
442 profile_print_model (cpu, verbose);
443 #endif
444
445 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
446 if (PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX])
447 scache_print_profile (cpu, verbose);
448 #endif
449
450 /* Print cpu-specific data before the execution speed. */
451 if (misc_cpu != NULL)
452 (*misc_cpu) (cpu, verbose);
453
454 /* Always try to print execution time and speed. */
455 if (verbose
456 || PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
457 profile_print_speed (cpu);
458 }
459
460 /* Finally print non-cpu specific miscellaneous data. */
461
462 if (misc != NULL)
463 (*misc) (sd, verbose);
464 }