1 /* Default profiling support.
2 Copyright (C) 1996, 1997, 1998 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"
24 #include "sim-assert.h"
38 #define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n))
40 static MODULE_INIT_FN profile_init
;
41 static MODULE_UNINSTALL_FN profile_uninstall
;
43 static DECLARE_OPTION_HANDLER (profile_option_handler
);
46 OPTION_PROFILE_INSN
= OPTION_START
,
47 OPTION_PROFILE_MEMORY
,
52 OPTION_PROFILE_PC_RANGE
,
53 OPTION_PROFILE_PC_GRANULARITY
,
55 OPTION_PROFILE_FUNCTION
58 static const OPTION profile_options
[] = {
59 { {"profile", no_argument
, NULL
, 'p'},
60 'p', NULL
, "Perform profiling",
61 profile_option_handler
},
62 { {"profile-insn", no_argument
, NULL
, OPTION_PROFILE_INSN
},
63 '\0', NULL
, "Perform instruction profiling",
64 profile_option_handler
},
65 { {"profile-memory", no_argument
, NULL
, OPTION_PROFILE_MEMORY
},
66 '\0', NULL
, "Perform memory profiling",
67 profile_option_handler
},
68 { {"profile-core", no_argument
, NULL
, OPTION_PROFILE_CORE
},
69 '\0', NULL
, "Perform CORE profiling",
70 profile_option_handler
},
71 { {"profile-model", no_argument
, NULL
, OPTION_PROFILE_MODEL
},
72 '\0', NULL
, "Perform model profiling",
73 profile_option_handler
},
75 { {"profile-file", required_argument
, NULL
, OPTION_PROFILE_FILE
},
76 '\0', "FILE NAME", "Specify profile output file",
77 profile_option_handler
},
79 { {"profile-pc", no_argument
, NULL
, OPTION_PROFILE_PC
},
80 '\0', NULL
, "Perform PC profiling",
81 profile_option_handler
},
82 { {"profile-pc-frequency", required_argument
, NULL
, 'F'},
83 'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency",
84 profile_option_handler
},
85 { {"profile-pc-size", required_argument
, NULL
, 'S'},
86 'S', "PC PROFILE SIZE", "Specify PC profiling size",
87 profile_option_handler
},
88 { {"profile-pc-granularity", required_argument
, NULL
, OPTION_PROFILE_PC_GRANULARITY
},
89 '\0', "PC PROFILE GRANULARITY", "Specify PC profiling sample coverage",
90 profile_option_handler
},
91 { {"profile-pc-range", required_argument
, NULL
, OPTION_PROFILE_PC_RANGE
},
92 '\0', "BASE,BOUND", "Specify PC profiling address range",
93 profile_option_handler
},
95 #ifdef SIM_HAVE_ADDR_RANGE
96 { {"profile-range", required_argument
, NULL
, OPTION_PROFILE_RANGE
},
97 '\0', "START,END", "Specify range of addresses for instruction and model profiling",
98 profile_option_handler
},
100 { {"profile-function", required_argument
, NULL
, OPTION_PROFILE_FUNCTION
},
101 '\0', "FUNCTION", "Specify function to profile",
102 profile_option_handler
},
106 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
110 profile_option_handler (SIM_DESC sd
,
118 /* FIXME: Need to handle `cpu' arg. */
124 sim_io_eprintf (sd
, "Profiling not compiled in, -p option ignored\n");
127 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
128 for (prof_nr
= 0; prof_nr
< MAX_PROFILE_VALUES
; ++prof_nr
)
129 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[prof_nr
] = 1;
133 case OPTION_PROFILE_INSN
:
134 #if WITH_PROFILE_INSN_P
135 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
136 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_INSN_IDX
] = 1;
138 sim_io_eprintf (sd
, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
142 case OPTION_PROFILE_MEMORY
:
143 #if WITH_PROFILE_MEMORY_P
144 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
145 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_MEMORY_IDX
] = 1;
147 sim_io_eprintf (sd
, "Memory profiling not compiled in, `--profile-memory' ignored\n");
151 case OPTION_PROFILE_CORE
:
152 #if WITH_PROFILE_CORE_P
153 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
154 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_CORE_IDX
] = 1;
156 sim_io_eprintf (sd
, "CORE profiling not compiled in, `--profile-core' ignored\n");
160 case OPTION_PROFILE_MODEL
:
161 #if WITH_PROFILE_MODEL_P
162 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
163 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_MODEL_IDX
] = 1;
165 sim_io_eprintf (sd
, "Model profiling not compiled in, `--profile-model' ignored\n");
169 case OPTION_PROFILE_FILE
:
170 /* FIXME: Might want this to apply to pc profiling only,
171 or have two profile file options. */
173 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-file' ignored\n");
176 FILE *f
= fopen (arg
, "w");
180 sim_io_eprintf (sd
, "Unable to open profile output file `%s'\n", arg
);
183 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
184 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = f
;
188 case OPTION_PROFILE_PC
:
189 if (WITH_PROFILE_PC_P
)
191 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
192 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
195 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc' ignored\n");
199 if (WITH_PROFILE_PC_P
)
201 /* FIXME: Validate arg. */
202 int val
= atoi (arg
);
203 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
204 PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
205 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
206 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
209 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
213 if (WITH_PROFILE_PC_P
)
215 /* FIXME: Validate arg. */
216 int val
= atoi (arg
);
217 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
218 PROFILE_PC_NR_BUCKETS (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
219 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
220 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
223 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
226 case OPTION_PROFILE_PC_GRANULARITY
:
227 if (WITH_PROFILE_PC_P
)
230 int val
= atoi (arg
);
231 /* check that the granularity is a power of two */
233 while (val
> (1 << shift
))
237 if (val
!= (1 << shift
))
239 sim_io_eprintf (sd
, "PC profiling granularity not a power of two\n");
244 sim_io_eprintf (sd
, "PC profiling granularity too small");
247 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
248 PROFILE_PC_SHIFT (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = shift
;
249 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
250 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
253 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
256 case OPTION_PROFILE_PC_RANGE
:
257 if (WITH_PROFILE_PC_P
)
259 /* FIXME: Validate args */
263 base
= strtoul (chp
, &chp
, 0);
266 sim_io_eprintf (sd
, "--profile-pc-range missing BOUND argument\n");
269 bound
= strtoul (chp
+ 1, NULL
, 0);
270 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
272 PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = base
;
273 PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = bound
;
275 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
276 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
279 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
281 #ifdef SIM_HAVE_ADDR_RANGE
282 case OPTION_PROFILE_RANGE
:
286 unsigned long start
,end
;
287 start
= strtoul (chp
, &chp
, 0);
290 sim_io_eprintf (sd
, "--profile-range missing END argument\n");
293 end
= strtoul (chp
+ 1, NULL
, 0);
294 /* FIXME: Argument validation. */
296 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
299 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
300 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))),
304 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-range' ignored\n");
307 case OPTION_PROFILE_FUNCTION
:
310 /*wip: need to compute function range given name*/
313 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-function' ignored\n");
315 #endif /* SIM_HAVE_ADDR_RANGE */
318 /* Re-compute the cpu profile summary. */
319 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
321 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 0;
322 for (prof_nr
= 0; prof_nr
< MAX_PROFILE_VALUES
; ++prof_nr
)
324 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[prof_nr
])
326 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
335 /* PC profiling support */
337 #if WITH_PROFILE_PC_P
340 profile_pc_cleanup (SIM_DESC sd
)
343 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
345 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
346 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
347 if (PROFILE_PC_COUNT (data
) != NULL
)
348 zfree (PROFILE_PC_COUNT (data
));
349 PROFILE_PC_COUNT (data
) = NULL
;
350 if (PROFILE_PC_EVENT (data
) != NULL
)
351 sim_events_deschedule (sd
, PROFILE_PC_EVENT (data
));
352 PROFILE_PC_EVENT (data
) = NULL
;
358 profile_pc_uninstall (SIM_DESC sd
)
360 profile_pc_cleanup (sd
);
364 profile_pc_event (SIM_DESC sd
,
367 sim_cpu
*cpu
= (sim_cpu
*) data
;
368 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
371 switch (STATE_WATCHPOINTS (sd
)->sizeof_pc
)
373 case 2: pc
= *(unsigned_2
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
374 case 4: pc
= *(unsigned_4
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
375 case 8: pc
= *(unsigned_8
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
378 i
= (pc
- PROFILE_PC_START (profile
)) >> PROFILE_PC_SHIFT (profile
);
379 if (i
< PROFILE_PC_NR_BUCKETS (profile
))
380 PROFILE_PC_COUNT (profile
) [i
] += 1; /* Overflow? */
382 PROFILE_PC_COUNT (profile
) [PROFILE_PC_NR_BUCKETS (profile
)] += 1;
383 PROFILE_PC_EVENT (profile
) =
384 sim_events_schedule (sd
, PROFILE_PC_FREQ (profile
), profile_pc_event
, cpu
);
388 profile_pc_init (SIM_DESC sd
)
391 profile_pc_cleanup (sd
);
392 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
394 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
395 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
396 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
]
397 && STATE_WATCHPOINTS (sd
)->pc
!= NULL
)
400 /* fill in the frequency if not specified */
401 if (PROFILE_PC_FREQ (data
) == 0)
402 PROFILE_PC_FREQ (data
) = 256;
403 /* fill in the start/end if not specified */
404 if (PROFILE_PC_END (data
) == 0)
406 PROFILE_PC_START (data
) = STATE_TEXT_START (sd
);
407 PROFILE_PC_END (data
) = STATE_TEXT_END (sd
);
409 /* Compute the number of buckets if not specified. */
410 if (PROFILE_PC_NR_BUCKETS (data
) == 0)
412 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
413 PROFILE_PC_NR_BUCKETS (data
) = 16;
416 if (PROFILE_PC_END (data
) == 0)
418 /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
419 PROFILE_PC_NR_BUCKETS (data
) =
420 ((1 << (STATE_WATCHPOINTS (sd
)->sizeof_pc
) * (8 - 1))
421 / (PROFILE_PC_BUCKET_SIZE (data
) / 2));
425 PROFILE_PC_NR_BUCKETS (data
) =
426 ((PROFILE_PC_END (data
)
427 - PROFILE_PC_START (data
)
428 + PROFILE_PC_BUCKET_SIZE (data
) - 1)
429 / PROFILE_PC_BUCKET_SIZE (data
));
433 /* Compute the bucket size if not specified. Ensure that it
434 is rounded up to the next power of two */
435 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
437 if (PROFILE_PC_END (data
) == 0)
438 /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
439 bucket_size
= ((1 << ((STATE_WATCHPOINTS (sd
)->sizeof_pc
* 8) - 1))
440 / (PROFILE_PC_NR_BUCKETS (data
) / 2));
442 bucket_size
= ((PROFILE_PC_END (data
)
443 - PROFILE_PC_START (data
)
444 + PROFILE_PC_NR_BUCKETS (data
) - 1)
445 / PROFILE_PC_NR_BUCKETS (data
));
446 PROFILE_PC_SHIFT (data
) = 0;
447 while (bucket_size
< PROFILE_PC_BUCKET_SIZE (data
))
449 PROFILE_PC_SHIFT (data
) += 1;
452 /* Align the end address with bucket size */
453 if (PROFILE_PC_END (data
) != 0)
454 PROFILE_PC_END (data
) = (PROFILE_PC_START (data
)
455 + (PROFILE_PC_BUCKET_SIZE (data
)
456 * PROFILE_PC_NR_BUCKETS (data
)));
457 /* create the relevant buffers */
458 PROFILE_PC_COUNT (data
) =
459 NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data
) + 1);
460 PROFILE_PC_EVENT (data
) =
461 sim_events_schedule (sd
,
462 PROFILE_PC_FREQ (data
),
471 profile_print_pc (sim_cpu
*cpu
, int verbose
)
473 SIM_DESC sd
= CPU_STATE (cpu
);
474 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
480 if (PROFILE_PC_COUNT (profile
) == 0)
483 sim_io_printf (sd
, "Program Counter Statistics:\n\n");
485 /* First pass over data computes various things. */
488 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
490 total
+= PROFILE_PC_COUNT (profile
) [i
];
491 if (PROFILE_PC_COUNT (profile
) [i
] > max_val
)
492 max_val
= PROFILE_PC_COUNT (profile
) [i
];
495 sim_io_printf (sd
, " Total samples: %s\n",
497 sim_io_printf (sd
, " Granularity: %s bytes per bucket\n",
498 COMMAS (PROFILE_PC_BUCKET_SIZE (profile
)));
499 sim_io_printf (sd
, " Size: %s buckets\n",
500 COMMAS (PROFILE_PC_NR_BUCKETS (profile
)));
501 sim_io_printf (sd
, " Frequency: %s cycles per sample\n",
502 COMMAS (PROFILE_PC_FREQ (profile
)));
504 if (PROFILE_PC_END (profile
) != 0)
505 sim_io_printf (sd
, " Range: 0x%lx 0x%lx\n",
506 (long) PROFILE_PC_START (profile
),
507 (long) PROFILE_PC_END (profile
));
509 if (verbose
&& max_val
!= 0)
511 /* Now we can print the histogram. */
512 sim_io_printf (sd
, "\n");
513 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
515 if (PROFILE_PC_COUNT (profile
) [i
] != 0)
517 sim_io_printf (sd
, " ");
518 if (i
== PROFILE_PC_NR_BUCKETS (profile
))
519 sim_io_printf (sd
, "%10s:", "overflow");
521 sim_io_printf (sd
, "0x%08lx:",
522 (long) (PROFILE_PC_START (profile
)
523 + (i
* PROFILE_PC_BUCKET_SIZE (profile
))));
524 sim_io_printf (sd
, " %*s",
525 max_val
< 10000 ? 5 : 10,
526 COMMAS (PROFILE_PC_COUNT (profile
) [i
]));
527 sim_io_printf (sd
, " %4.1f",
528 (PROFILE_PC_COUNT (profile
) [i
] * 100.0) / total
);
529 sim_io_printf (sd
, ": ");
530 sim_profile_print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
531 PROFILE_PC_COUNT (profile
) [i
],
533 sim_io_printf (sd
, "\n");
538 /* dump the histogram to the file "gmon.out" using BSD's gprof file
540 /* Since a profile data file is in the native format of the host on
541 which the profile is being, endian issues are not considered in
543 /* FIXME: Is this the best place for this code? */
545 FILE *pf
= fopen ("gmon.out", "wb");
548 sim_io_eprintf (sd
, "Failed to open \"gmon.out\" profile file\n");
552 /* FIXME: what if the target has a 64 bit PC? */
553 unsigned32 header
[3];
555 if (PROFILE_PC_END (profile
) != 0)
557 header
[0] = PROFILE_PC_START (profile
);
558 header
[1] = PROFILE_PC_END (profile
);
565 /* size of sample buffer (+ header) */
566 header
[2] = PROFILE_PC_NR_BUCKETS (profile
) * 2 + sizeof (header
);
567 ok
= fwrite (&header
, sizeof (header
), 1, pf
);
569 ok
&& (loop
< PROFILE_PC_NR_BUCKETS (profile
));
573 if (PROFILE_PC_COUNT (profile
) [loop
] >= 0xffff)
576 sample
= PROFILE_PC_COUNT (profile
) [loop
];
577 ok
= fwrite (&sample
, sizeof (sample
), 1, pf
);
580 sim_io_eprintf (sd
, "Failed to write to \"gmon.out\" profile file\n");
585 sim_io_printf (sd
, "\n");
590 /* Summary printing support. */
592 #if WITH_PROFILE_INSN_P
595 profile_insn_init (SIM_DESC sd
)
599 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
601 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
603 if (CPU_MAX_INSNS (cpu
) > 0)
604 PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu
)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu
));
611 profile_print_insn (sim_cpu
*cpu
, int verbose
)
613 unsigned int i
, n
, total
, max_val
, max_name_len
;
614 SIM_DESC sd
= CPU_STATE (cpu
);
615 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
618 /* If MAX_INSNS not set, insn profiling isn't supported. */
619 if (CPU_MAX_INSNS (cpu
) == 0)
622 sim_io_printf (sd
, "Instruction Statistics");
623 #ifdef SIM_HAVE_ADDR_RANGE
624 if (PROFILE_RANGE (data
)->ranges
)
625 sim_io_printf (sd
, " (for selected address range(s))");
627 sim_io_printf (sd
, "\n\n");
629 /* First pass over data computes various things. */
633 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
635 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
639 total
+= PROFILE_INSN_COUNT (data
) [i
];
640 if (PROFILE_INSN_COUNT (data
) [i
] > max_val
)
641 max_val
= PROFILE_INSN_COUNT (data
) [i
];
643 if (n
> max_name_len
)
646 /* set the total insn count, in case client is being lazy */
647 if (! PROFILE_TOTAL_INSN_COUNT (data
))
648 PROFILE_TOTAL_INSN_COUNT (data
) = total
;
650 sim_io_printf (sd
, " Total: %s insns\n", COMMAS (total
));
652 if (verbose
&& max_val
!= 0)
654 /* Now we can print the histogram. */
655 sim_io_printf (sd
, "\n");
656 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
658 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
662 if (PROFILE_INSN_COUNT (data
) [i
] != 0)
664 sim_io_printf (sd
, " %*s: %*s: ",
666 max_val
< 10000 ? 5 : 10,
667 COMMAS (PROFILE_INSN_COUNT (data
) [i
]));
668 sim_profile_print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
669 PROFILE_INSN_COUNT (data
) [i
],
671 sim_io_printf (sd
, "\n");
676 sim_io_printf (sd
, "\n");
681 #if WITH_PROFILE_MEMORY_P
684 profile_print_memory (sim_cpu
*cpu
, int verbose
)
687 unsigned int total_read
, total_write
;
688 unsigned int max_val
, max_name_len
;
689 /* FIXME: Need to add smp support. */
690 SIM_DESC sd
= CPU_STATE (cpu
);
691 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
694 sim_io_printf (sd
, "Memory Access Statistics\n\n");
696 /* First pass over data computes various things. */
697 max_val
= total_read
= total_write
= max_name_len
= 0;
698 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
700 total_read
+= PROFILE_READ_COUNT (data
) [i
];
701 total_write
+= PROFILE_WRITE_COUNT (data
) [i
];
702 if (PROFILE_READ_COUNT (data
) [i
] > max_val
)
703 max_val
= PROFILE_READ_COUNT (data
) [i
];
704 if (PROFILE_WRITE_COUNT (data
) [i
] > max_val
)
705 max_val
= PROFILE_WRITE_COUNT (data
) [i
];
706 n
= strlen (MODE_NAME (i
));
707 if (n
> max_name_len
)
711 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
712 sim_io_printf (sd
, " Total read: %s accesses\n",
713 COMMAS (total_read
));
714 sim_io_printf (sd
, " Total write: %s accesses\n",
715 COMMAS (total_write
));
717 if (verbose
&& max_val
!= 0)
719 /* FIXME: Need to separate instruction fetches from data fetches
720 as the former swamps the latter. */
721 /* Now we can print the histogram. */
722 sim_io_printf (sd
, "\n");
723 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
725 if (PROFILE_READ_COUNT (data
) [i
] != 0)
727 sim_io_printf (sd
, " %*s read: %*s: ",
728 max_name_len
, MODE_NAME (i
),
729 max_val
< 10000 ? 5 : 10,
730 COMMAS (PROFILE_READ_COUNT (data
) [i
]));
731 sim_profile_print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
732 PROFILE_READ_COUNT (data
) [i
],
734 sim_io_printf (sd
, "\n");
736 if (PROFILE_WRITE_COUNT (data
) [i
] != 0)
738 sim_io_printf (sd
, " %*s write: %*s: ",
739 max_name_len
, MODE_NAME (i
),
740 max_val
< 10000 ? 5 : 10,
741 COMMAS (PROFILE_WRITE_COUNT (data
) [i
]));
742 sim_profile_print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
743 PROFILE_WRITE_COUNT (data
) [i
],
745 sim_io_printf (sd
, "\n");
750 sim_io_printf (sd
, "\n");
755 #if WITH_PROFILE_CORE_P
758 profile_print_core (sim_cpu
*cpu
, int verbose
)
761 unsigned int max_val
;
762 /* FIXME: Need to add smp support. */
763 SIM_DESC sd
= CPU_STATE (cpu
);
764 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
767 sim_io_printf (sd
, "CORE Statistics\n\n");
769 /* First pass over data computes various things. */
774 for (map
= 0; map
< nr_maps
; map
++)
776 total
+= PROFILE_CORE_COUNT (data
) [map
];
777 if (PROFILE_CORE_COUNT (data
) [map
] > max_val
)
778 max_val
= PROFILE_CORE_COUNT (data
) [map
];
782 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
783 sim_io_printf (sd
, " Total: %s accesses\n",
786 if (verbose
&& max_val
!= 0)
789 /* Now we can print the histogram. */
790 sim_io_printf (sd
, "\n");
791 for (map
= 0; map
< nr_maps
; map
++)
793 if (PROFILE_CORE_COUNT (data
) [map
] != 0)
795 sim_io_printf (sd
, "%10s:", map_to_str (map
));
796 sim_io_printf (sd
, "%*s: ",
797 max_val
< 10000 ? 5 : 10,
798 COMMAS (PROFILE_CORE_COUNT (data
) [map
]));
799 sim_profile_print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
800 PROFILE_CORE_COUNT (data
) [map
],
802 sim_io_printf (sd
, "\n");
807 sim_io_printf (sd
, "\n");
812 #if WITH_PROFILE_MODEL_P
815 profile_print_model (sim_cpu
*cpu
, int verbose
)
817 SIM_DESC sd
= CPU_STATE (cpu
);
818 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
819 unsigned long cti_stall_cycles
= PROFILE_MODEL_CTI_STALL_CYCLES (data
);
820 unsigned long load_stall_cycles
= PROFILE_MODEL_LOAD_STALL_CYCLES (data
);
821 unsigned long total_cycles
= PROFILE_MODEL_TOTAL_CYCLES (data
);
824 sim_io_printf (sd
, "Model %s Timing Information",
825 MODEL_NAME (CPU_MODEL (cpu
)));
826 #ifdef SIM_HAVE_ADDR_RANGE
827 if (PROFILE_RANGE (data
)->ranges
)
828 sim_io_printf (sd
, " (for selected address range(s))");
830 sim_io_printf (sd
, "\n\n");
831 sim_io_printf (sd
, " %-*s %s\n",
832 PROFILE_LABEL_WIDTH
, "Taken branches:",
833 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data
)));
834 sim_io_printf (sd
, " %-*s %s\n",
835 PROFILE_LABEL_WIDTH
, "Untaken branches:",
836 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data
)));
837 sim_io_printf (sd
, " %-*s %s\n",
838 PROFILE_LABEL_WIDTH
, "Cycles stalled due to branches:",
839 COMMAS (cti_stall_cycles
));
840 sim_io_printf (sd
, " %-*s %s\n",
841 PROFILE_LABEL_WIDTH
, "Cycles stalled due to loads:",
842 COMMAS (load_stall_cycles
));
843 sim_io_printf (sd
, " %-*s %s\n",
844 PROFILE_LABEL_WIDTH
, "Total cycles (*approximate*):",
845 COMMAS (total_cycles
));
846 sim_io_printf (sd
, "\n");
852 sim_profile_print_bar (SIM_DESC sd
, unsigned int width
,
853 unsigned int val
, unsigned int max_val
)
855 unsigned int i
, count
;
857 count
= ((double) val
/ (double) max_val
) * (double) width
;
859 for (i
= 0; i
< count
; ++i
)
860 sim_io_printf (sd
, "*");
863 /* Print the simulator's execution speed for CPU. */
866 profile_print_speed (sim_cpu
*cpu
)
868 SIM_DESC sd
= CPU_STATE (cpu
);
869 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
870 unsigned long milliseconds
= sim_events_elapsed_time (sd
);
871 unsigned long total
= PROFILE_TOTAL_INSN_COUNT (data
);
874 sim_io_printf (sd
, "Simulator Execution Speed\n\n");
877 sim_io_printf (sd
, " Total instructions: %s\n", COMMAS (total
));
879 if (milliseconds
< 1000)
880 sim_io_printf (sd
, " Total execution time: < 1 second\n\n");
883 /* The printing of the time rounded to 2 decimal places makes the speed
884 calculation seem incorrect [even though it is correct]. So round
885 MILLISECONDS first. This can marginally affect the result, but it's
886 better that the user not perceive there's a math error. */
887 double secs
= (double) milliseconds
/ 1000;
888 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
889 sim_io_printf (sd
, " Total execution time: %.2f seconds\n", secs
);
890 /* Don't confuse things with data that isn't useful.
891 If we ran for less than 2 seconds, only use the data if we
892 executed more than 100,000 insns. */
893 if (secs
>= 2 || total
>= 100000)
894 sim_io_printf (sd
, " Simulator speed: %s insns/second\n\n",
895 COMMAS ((unsigned long) ((double) total
/ secs
)));
899 /* Print selected address ranges. */
902 profile_print_addr_ranges (sim_cpu
*cpu
)
904 ADDR_SUBRANGE
*asr
= PROFILE_RANGE (CPU_PROFILE_DATA (cpu
))->ranges
;
905 SIM_DESC sd
= CPU_STATE (cpu
);
909 sim_io_printf (sd
, "Selected address ranges:\n\n");
912 sim_io_printf (sd
, " 0x%lx - 0x%lx\n",
913 (long) asr
->start
, (long) asr
->end
);
916 sim_io_printf (sd
, "\n");
920 /* Top level function to print all summary profile information.
921 It is [currently] intended that all such data is printed by this function.
922 I'd rather keep it all in one place for now. To that end, MISC_CPU and
923 MISC are callbacks used to print any miscellaneous data.
925 One might want to add a user option that allows printing by type or by cpu
926 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
927 This may be a case of featuritis so it's currently left out.
929 Note that results are indented two spaces to distinguish them from
933 profile_info (SIM_DESC sd
, int verbose
)
936 int print_title_p
= 0;
938 /* Only print the title if some data has been collected. */
939 /* ??? Why don't we just exit if no data collected? */
940 /* FIXME: If the number of processors can be selected on the command line,
941 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
943 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
945 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
946 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
948 for (i
= 0; i
< MAX_PROFILE_VALUES
; ++i
)
949 if (PROFILE_FLAGS (data
) [i
])
951 /* One could break out early if print_title_p is set. */
954 sim_io_printf (sd
, "Summary profiling results:\n\n");
956 /* Loop, cpu by cpu, printing results. */
958 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
960 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
961 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
963 if (MAX_NR_PROCESSORS
> 1
965 #if WITH_PROFILE_INSN_P
966 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
]
968 #if WITH_PROFILE_MEMORY_P
969 || PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
]
971 #if WITH_PROFILE_CORE_P
972 || PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
]
974 #if WITH_PROFILE_MODEL_P
975 || PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
]
977 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
978 || PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
]
980 #if WITH_PROFILE_PC_P
981 || PROFILE_FLAGS (data
) [PROFILE_PC_IDX
]
985 sim_io_printf (sd
, "CPU %d\n\n", c
);
988 #ifdef SIM_HAVE_ADDR_RANGE
990 && (PROFILE_INSN_P (cpu
)
991 || PROFILE_MODEL_P (cpu
)))
992 profile_print_addr_ranges (cpu
);
995 #if WITH_PROFILE_INSN_P
996 if (PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
997 profile_print_insn (cpu
, verbose
);
1000 #if WITH_PROFILE_MEMORY_P
1001 if (PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
])
1002 profile_print_memory (cpu
, verbose
);
1005 #if WITH_PROFILE_CORE_P
1006 if (PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
])
1007 profile_print_core (cpu
, verbose
);
1010 #if WITH_PROFILE_MODEL_P
1011 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1012 profile_print_model (cpu
, verbose
);
1015 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1016 if (PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
])
1017 scache_print_profile (cpu
, verbose
);
1020 #if WITH_PROFILE_PC_P
1021 if (PROFILE_FLAGS (data
) [PROFILE_PC_IDX
])
1022 profile_print_pc (cpu
, verbose
);
1025 /* Print cpu-specific data before the execution speed. */
1026 if (PROFILE_INFO_CPU_CALLBACK (data
) != NULL
)
1027 PROFILE_INFO_CPU_CALLBACK (data
) (cpu
, verbose
);
1029 /* Always try to print execution time and speed. */
1031 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1032 profile_print_speed (cpu
);
1035 /* Finally print non-cpu specific miscellaneous data. */
1036 if (STATE_PROFILE_INFO_CALLBACK (sd
))
1037 STATE_PROFILE_INFO_CALLBACK (sd
) (sd
, verbose
);
1041 /* Install profiling support in the simulator. */
1044 profile_install (SIM_DESC sd
)
1048 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1049 sim_add_option_table (sd
, NULL
, profile_options
);
1050 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1051 memset (CPU_PROFILE_DATA (STATE_CPU (sd
, i
)), 0,
1052 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd
, i
))));
1053 #if WITH_PROFILE_INSN_P
1054 sim_module_add_init_fn (sd
, profile_insn_init
);
1056 #if WITH_PROFILE_PC_P
1057 sim_module_add_uninstall_fn (sd
, profile_pc_uninstall
);
1058 sim_module_add_init_fn (sd
, profile_pc_init
);
1060 sim_module_add_init_fn (sd
, profile_init
);
1061 sim_module_add_uninstall_fn (sd
, profile_uninstall
);
1062 sim_module_add_info_fn (sd
, profile_info
);
1067 profile_init (SIM_DESC sd
)
1069 #ifdef SIM_HAVE_ADDR_RANGE
1070 /* Check if a range has been specified without specifying what to
1075 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1077 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1079 if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)))
1080 && ! (PROFILE_INSN_P (cpu
)
1081 || PROFILE_MODEL_P (cpu
)))
1083 sim_io_eprintf_cpu (cpu
, "Profiling address range specified without --profile-insn or --profile-model.\n");
1084 sim_io_eprintf_cpu (cpu
, "Address range ignored.\n");
1085 sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
1086 0, ~ (address_word
) 0);
1096 profile_uninstall (SIM_DESC sd
)
1100 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1102 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1103 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1105 if (PROFILE_FILE (data
) != NULL
)
1107 /* If output from different cpus is going to the same file,
1108 avoid closing the file twice. */
1109 for (j
= 0; j
< i
; ++j
)
1110 if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, j
)))
1111 == PROFILE_FILE (data
))
1114 fclose (PROFILE_FILE (data
));
1117 if (PROFILE_INSN_COUNT (data
) != NULL
)
1118 zfree (PROFILE_INSN_COUNT (data
));