1 /* Default profiling support.
2 Copyright (C) 1996-2021 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 3 of the License, or
10 (at your option) any later version.
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* This must come before any other includes. */
25 #include "sim-options.h"
26 #include "sim-assert.h"
32 #if !WITH_PROFILE_PC_P
33 static unsigned int _profile_stub
;
34 # define PROFILE_PC_FREQ(p) _profile_stub
35 # define PROFILE_PC_NR_BUCKETS(p) _profile_stub
36 # define PROFILE_PC_SHIFT(p) _profile_stub
37 # define PROFILE_PC_START(p) _profile_stub
38 # define PROFILE_PC_END(p) _profile_stub
39 # define PROFILE_INSN_COUNT(p) &_profile_stub
42 #define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n))
44 static MODULE_INIT_FN profile_init
;
45 static MODULE_UNINSTALL_FN profile_uninstall
;
47 static DECLARE_OPTION_HANDLER (profile_option_handler
);
50 OPTION_PROFILE_INSN
= OPTION_START
,
51 OPTION_PROFILE_MEMORY
,
55 OPTION_PROFILE_CPU_FREQUENCY
,
57 OPTION_PROFILE_PC_RANGE
,
58 OPTION_PROFILE_PC_GRANULARITY
,
60 OPTION_PROFILE_FUNCTION
63 static const OPTION profile_options
[] = {
64 { {"profile", optional_argument
, NULL
, 'p'},
65 'p', "on|off", "Perform profiling",
66 profile_option_handler
, NULL
},
67 { {"profile-insn", optional_argument
, NULL
, OPTION_PROFILE_INSN
},
68 '\0', "on|off", "Perform instruction profiling",
69 profile_option_handler
, NULL
},
70 { {"profile-memory", optional_argument
, NULL
, OPTION_PROFILE_MEMORY
},
71 '\0', "on|off", "Perform memory profiling",
72 profile_option_handler
, NULL
},
73 { {"profile-core", optional_argument
, NULL
, OPTION_PROFILE_CORE
},
74 '\0', "on|off", "Perform CORE profiling",
75 profile_option_handler
, NULL
},
76 { {"profile-model", optional_argument
, NULL
, OPTION_PROFILE_MODEL
},
77 '\0', "on|off", "Perform model profiling",
78 profile_option_handler
, NULL
},
79 { {"profile-cpu-frequency", required_argument
, NULL
,
80 OPTION_PROFILE_CPU_FREQUENCY
},
81 '\0', "CPU FREQUENCY", "Specify the speed of the simulated cpu clock",
82 profile_option_handler
, NULL
},
84 { {"profile-file", required_argument
, NULL
, OPTION_PROFILE_FILE
},
85 '\0', "FILE NAME", "Specify profile output file",
86 profile_option_handler
, NULL
},
88 { {"profile-pc", optional_argument
, NULL
, OPTION_PROFILE_PC
},
89 '\0', "on|off", "Perform PC profiling",
90 profile_option_handler
, NULL
},
91 { {"profile-pc-frequency", required_argument
, NULL
, 'F'},
92 'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency",
93 profile_option_handler
, NULL
},
94 { {"profile-pc-size", required_argument
, NULL
, 'S'},
95 'S', "PC PROFILE SIZE", "Specify PC profiling size",
96 profile_option_handler
, NULL
},
97 { {"profile-pc-granularity", required_argument
, NULL
, OPTION_PROFILE_PC_GRANULARITY
},
98 '\0', "PC PROFILE GRANULARITY", "Specify PC profiling sample coverage",
99 profile_option_handler
, NULL
},
100 { {"profile-pc-range", required_argument
, NULL
, OPTION_PROFILE_PC_RANGE
},
101 '\0', "BASE,BOUND", "Specify PC profiling address range",
102 profile_option_handler
, NULL
},
104 #ifdef SIM_HAVE_ADDR_RANGE
105 { {"profile-range", required_argument
, NULL
, OPTION_PROFILE_RANGE
},
106 '\0', "START,END", "Specify range of addresses for instruction and model profiling",
107 profile_option_handler
, NULL
},
109 { {"profile-function", required_argument
, NULL
, OPTION_PROFILE_FUNCTION
},
110 '\0', "FUNCTION", "Specify function to profile",
111 profile_option_handler
, NULL
},
115 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
118 /* Set/reset the profile options indicated in MASK. */
121 set_profile_option_mask (SIM_DESC sd
, const char *name
, int mask
, const char *arg
)
129 if (strcmp (arg
, "yes") == 0
130 || strcmp (arg
, "on") == 0
131 || strcmp (arg
, "1") == 0)
133 else if (strcmp (arg
, "no") == 0
134 || strcmp (arg
, "off") == 0
135 || strcmp (arg
, "0") == 0)
139 sim_io_eprintf (sd
, "Argument `%s' for `--profile%s' invalid, one of `on', `off', `yes', `no' expected\n", arg
, name
);
144 /* update applicable profile bits */
145 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
147 if ((mask
& (1 << profile_nr
)) == 0)
150 #if 0 /* see sim-trace.c, set flags in STATE here if/when there are any */
151 /* Set non-cpu specific values. */
159 /* Set cpu values. */
160 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
162 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
] = profile_val
;
166 /* Re-compute the cpu profile summary. */
169 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
170 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
174 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
176 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 0;
177 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
179 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
])
181 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
191 /* Set one profile option based on its IDX value.
192 Not static as cgen-scache.c uses it. */
195 sim_profile_set_option (SIM_DESC sd
, const char *name
, int idx
, const char *arg
)
197 return set_profile_option_mask (sd
, name
, 1 << idx
, arg
);
201 parse_frequency (SIM_DESC sd
, const char *arg
, unsigned long *freq
)
204 /* First, parse a decimal number. */
209 for (/**/; *ch
!= '\0'; ++ch
)
213 *freq
= *freq
* 10 + (*ch
- '0');
216 /* Accept KHz, MHz or Hz as a suffix. */
217 if (tolower (*ch
) == 'm')
222 else if (tolower (*ch
) == 'k')
228 if (tolower (*ch
) == 'h')
231 if (tolower (*ch
) == 'z')
238 sim_io_eprintf (sd
, "Invalid argument for --profile-cpu-frequency: %s\n",
248 profile_option_handler (SIM_DESC sd
,
256 /* FIXME: Need to handle `cpu' arg. */
262 sim_io_eprintf (sd
, "Profiling not compiled in, `-p' ignored\n");
264 return set_profile_option_mask (sd
, "profile", PROFILE_USEFUL_MASK
,
268 case OPTION_PROFILE_INSN
:
269 if (WITH_PROFILE_INSN_P
)
270 return sim_profile_set_option (sd
, "-insn", PROFILE_INSN_IDX
, arg
);
272 sim_io_eprintf (sd
, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
275 case OPTION_PROFILE_MEMORY
:
276 if (WITH_PROFILE_MEMORY_P
)
277 return sim_profile_set_option (sd
, "-memory", PROFILE_MEMORY_IDX
, arg
);
279 sim_io_eprintf (sd
, "Memory profiling not compiled in, `--profile-memory' ignored\n");
282 case OPTION_PROFILE_CORE
:
283 if (WITH_PROFILE_CORE_P
)
284 return sim_profile_set_option (sd
, "-core", PROFILE_CORE_IDX
, arg
);
286 sim_io_eprintf (sd
, "CORE profiling not compiled in, `--profile-core' ignored\n");
289 case OPTION_PROFILE_MODEL
:
290 if (WITH_PROFILE_MODEL_P
)
291 return sim_profile_set_option (sd
, "-model", PROFILE_MODEL_IDX
, arg
);
293 sim_io_eprintf (sd
, "Model profiling not compiled in, `--profile-model' ignored\n");
296 case OPTION_PROFILE_CPU_FREQUENCY
:
299 SIM_RC rc
= parse_frequency (sd
, arg
, &val
);
302 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
303 PROFILE_CPU_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
,cpu_nr
))) = val
;
308 case OPTION_PROFILE_FILE
:
309 /* FIXME: Might want this to apply to pc profiling only,
310 or have two profile file options. */
312 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-file' ignored\n");
315 FILE *f
= fopen (arg
, "w");
319 sim_io_eprintf (sd
, "Unable to open profile output file `%s'\n", arg
);
322 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
323 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = f
;
327 case OPTION_PROFILE_PC
:
328 if (WITH_PROFILE_PC_P
)
329 return sim_profile_set_option (sd
, "-pc", PROFILE_PC_IDX
, arg
);
331 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc' ignored\n");
335 if (WITH_PROFILE_PC_P
)
337 /* FIXME: Validate arg. */
338 int val
= atoi (arg
);
339 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
340 PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
341 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
342 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
345 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
349 if (WITH_PROFILE_PC_P
)
351 /* FIXME: Validate arg. */
352 int val
= atoi (arg
);
353 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
354 PROFILE_PC_NR_BUCKETS (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
355 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
356 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
359 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
362 case OPTION_PROFILE_PC_GRANULARITY
:
363 if (WITH_PROFILE_PC_P
)
366 int val
= atoi (arg
);
367 /* check that the granularity is a power of two */
369 while (val
> (1 << shift
))
373 if (val
!= (1 << shift
))
375 sim_io_eprintf (sd
, "PC profiling granularity not a power of two\n");
380 sim_io_eprintf (sd
, "PC profiling granularity too small");
383 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
384 PROFILE_PC_SHIFT (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = shift
;
385 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
386 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
389 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
392 case OPTION_PROFILE_PC_RANGE
:
393 if (WITH_PROFILE_PC_P
)
395 /* FIXME: Validate args */
399 base
= strtoul (chp
, &chp
, 0);
402 sim_io_eprintf (sd
, "--profile-pc-range missing BOUND argument\n");
405 bound
= strtoul (chp
+ 1, NULL
, 0);
406 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
408 PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = base
;
409 PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = bound
;
411 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
412 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
415 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
418 #ifdef SIM_HAVE_ADDR_RANGE
419 case OPTION_PROFILE_RANGE
:
423 unsigned long start
,end
;
424 start
= strtoul (chp
, &chp
, 0);
427 sim_io_eprintf (sd
, "--profile-range missing END argument\n");
430 end
= strtoul (chp
+ 1, NULL
, 0);
431 /* FIXME: Argument validation. */
433 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
436 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
437 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))),
441 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-range' ignored\n");
444 case OPTION_PROFILE_FUNCTION
:
447 /*wip: need to compute function range given name*/
450 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-function' ignored\n");
452 #endif /* SIM_HAVE_ADDR_RANGE */
458 /* Profiling output hooks. */
460 static void ATTRIBUTE_PRINTF (3, 0)
461 profile_vprintf (SIM_DESC sd
, sim_cpu
*cpu
, const char *fmt
, va_list ap
)
463 FILE *fp
= PROFILE_FILE (CPU_PROFILE_DATA (cpu
));
465 /* If an output file was given, redirect output to that. */
467 vfprintf (fp
, fmt
, ap
);
469 sim_io_evprintf (sd
, fmt
, ap
);
472 ATTRIBUTE_PRINTF (3, 4)
474 profile_printf (SIM_DESC sd
, sim_cpu
*cpu
, const char *fmt
, ...)
479 profile_vprintf (sd
, cpu
, fmt
, ap
);
483 /* PC profiling support */
485 #if WITH_PROFILE_PC_P
488 profile_pc_cleanup (SIM_DESC sd
)
491 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
493 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
494 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
495 if (PROFILE_PC_COUNT (data
) != NULL
)
496 free (PROFILE_PC_COUNT (data
));
497 PROFILE_PC_COUNT (data
) = NULL
;
498 if (PROFILE_PC_EVENT (data
) != NULL
)
499 sim_events_deschedule (sd
, PROFILE_PC_EVENT (data
));
500 PROFILE_PC_EVENT (data
) = NULL
;
506 profile_pc_uninstall (SIM_DESC sd
)
508 profile_pc_cleanup (sd
);
512 profile_pc_event (SIM_DESC sd
,
515 sim_cpu
*cpu
= (sim_cpu
*) data
;
516 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
517 address_word pc
= sim_pc_get (cpu
);
519 i
= (pc
- PROFILE_PC_START (profile
)) >> PROFILE_PC_SHIFT (profile
);
520 if (i
< PROFILE_PC_NR_BUCKETS (profile
))
521 PROFILE_PC_COUNT (profile
) [i
] += 1; /* Overflow? */
523 PROFILE_PC_COUNT (profile
) [PROFILE_PC_NR_BUCKETS (profile
)] += 1;
524 PROFILE_PC_EVENT (profile
) =
525 sim_events_schedule (sd
, PROFILE_PC_FREQ (profile
), profile_pc_event
, cpu
);
529 profile_pc_init (SIM_DESC sd
)
532 profile_pc_cleanup (sd
);
533 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
535 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
536 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
537 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
])
540 /* fill in the frequency if not specified */
541 if (PROFILE_PC_FREQ (data
) == 0)
542 PROFILE_PC_FREQ (data
) = 257;
543 /* fill in the start/end if not specified */
544 if (PROFILE_PC_END (data
) == 0)
546 PROFILE_PC_START (data
) = STATE_TEXT_START (sd
);
547 PROFILE_PC_END (data
) = STATE_TEXT_END (sd
);
549 /* Compute the number of buckets if not specified. */
550 if (PROFILE_PC_NR_BUCKETS (data
) == 0)
552 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
553 PROFILE_PC_NR_BUCKETS (data
) = 16;
556 if (PROFILE_PC_END (data
) == 0)
558 /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
559 PROFILE_PC_NR_BUCKETS (data
) =
560 ((1ULL << sizeof (sim_cia
) * (8 - 1))
561 / (PROFILE_PC_BUCKET_SIZE (data
) / 2));
565 PROFILE_PC_NR_BUCKETS (data
) =
566 ((PROFILE_PC_END (data
)
567 - PROFILE_PC_START (data
)
568 + PROFILE_PC_BUCKET_SIZE (data
) - 1)
569 / PROFILE_PC_BUCKET_SIZE (data
));
573 /* Compute the bucket size if not specified. Ensure that it
574 is rounded up to the next power of two */
575 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
577 if (PROFILE_PC_END (data
) == 0)
578 /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
579 bucket_size
= ((1ULL << ((sizeof (sim_cia
) * 8) - 1))
580 / (PROFILE_PC_NR_BUCKETS (data
) / 2));
582 bucket_size
= ((PROFILE_PC_END (data
)
583 - PROFILE_PC_START (data
)
584 + PROFILE_PC_NR_BUCKETS (data
) - 1)
585 / PROFILE_PC_NR_BUCKETS (data
));
586 PROFILE_PC_SHIFT (data
) = 0;
587 while (bucket_size
> PROFILE_PC_BUCKET_SIZE (data
))
589 PROFILE_PC_SHIFT (data
) += 1;
592 /* Align the end address with bucket size */
593 if (PROFILE_PC_END (data
) != 0)
594 PROFILE_PC_END (data
) = (PROFILE_PC_START (data
)
595 + (PROFILE_PC_BUCKET_SIZE (data
)
596 * PROFILE_PC_NR_BUCKETS (data
)));
597 /* create the relevant buffers */
598 PROFILE_PC_COUNT (data
) =
599 NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data
) + 1);
600 PROFILE_PC_EVENT (data
) =
601 sim_events_schedule (sd
,
602 PROFILE_PC_FREQ (data
),
611 profile_print_pc (sim_cpu
*cpu
, int verbose
)
613 SIM_DESC sd
= CPU_STATE (cpu
);
614 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
620 if (PROFILE_PC_COUNT (profile
) == 0)
623 profile_printf (sd
, cpu
, "Program Counter Statistics:\n\n");
625 /* First pass over data computes various things. */
628 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
630 total
+= PROFILE_PC_COUNT (profile
) [i
];
631 if (PROFILE_PC_COUNT (profile
) [i
] > max_val
)
632 max_val
= PROFILE_PC_COUNT (profile
) [i
];
635 profile_printf (sd
, cpu
, " Total samples: %s\n",
637 profile_printf (sd
, cpu
, " Granularity: %s bytes per bucket\n",
638 COMMAS (PROFILE_PC_BUCKET_SIZE (profile
)));
639 profile_printf (sd
, cpu
, " Size: %s buckets\n",
640 COMMAS (PROFILE_PC_NR_BUCKETS (profile
)));
641 profile_printf (sd
, cpu
, " Frequency: %s cycles per sample\n",
642 COMMAS (PROFILE_PC_FREQ (profile
)));
644 if (PROFILE_PC_END (profile
) != 0)
645 profile_printf (sd
, cpu
, " Range: 0x%lx 0x%lx\n",
646 (long) PROFILE_PC_START (profile
),
647 (long) PROFILE_PC_END (profile
));
649 if (verbose
&& max_val
!= 0)
651 /* Now we can print the histogram. */
652 profile_printf (sd
, cpu
, "\n");
653 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
655 if (PROFILE_PC_COUNT (profile
) [i
] != 0)
657 profile_printf (sd
, cpu
, " ");
658 if (i
== PROFILE_PC_NR_BUCKETS (profile
))
659 profile_printf (sd
, cpu
, "%10s:", "overflow");
661 profile_printf (sd
, cpu
, "0x%08lx:",
662 (long) (PROFILE_PC_START (profile
)
663 + (i
* PROFILE_PC_BUCKET_SIZE (profile
))));
664 profile_printf (sd
, cpu
, " %*s",
665 max_val
< 10000 ? 5 : 10,
666 COMMAS (PROFILE_PC_COUNT (profile
) [i
]));
667 profile_printf (sd
, cpu
, " %4.1f",
668 (PROFILE_PC_COUNT (profile
) [i
] * 100.0) / total
);
669 profile_printf (sd
, cpu
, ": ");
670 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
671 PROFILE_PC_COUNT (profile
) [i
],
673 profile_printf (sd
, cpu
, "\n");
678 /* dump the histogram to the file "gmon.out" using BSD's gprof file
680 /* Since a profile data file is in the native format of the host on
681 which the profile is being, endian issues are not considered in
683 /* FIXME: Is this the best place for this code? */
685 FILE *pf
= fopen ("gmon.out", "wb");
688 sim_io_eprintf (sd
, "Failed to open \"gmon.out\" profile file\n");
692 /* FIXME: what if the target has a 64 bit PC? */
693 unsigned32 header
[3];
695 if (PROFILE_PC_END (profile
) != 0)
697 header
[0] = PROFILE_PC_START (profile
);
698 header
[1] = PROFILE_PC_END (profile
);
705 /* size of sample buffer (+ header) */
706 header
[2] = PROFILE_PC_NR_BUCKETS (profile
) * 2 + sizeof (header
);
708 /* Header must be written out in target byte order. */
713 ok
= fwrite (&header
, sizeof (header
), 1, pf
);
715 ok
&& (loop
< PROFILE_PC_NR_BUCKETS (profile
));
719 if (PROFILE_PC_COUNT (profile
) [loop
] >= 0xffff)
722 sample
= PROFILE_PC_COUNT (profile
) [loop
];
724 ok
= fwrite (&sample
, sizeof (sample
), 1, pf
);
727 sim_io_eprintf (sd
, "Failed to write to \"gmon.out\" profile file\n");
732 profile_printf (sd
, cpu
, "\n");
737 /* Summary printing support. */
739 #if WITH_PROFILE_INSN_P
742 profile_insn_init (SIM_DESC sd
)
746 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
748 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
750 if (CPU_MAX_INSNS (cpu
) > 0)
751 PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu
)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu
));
758 profile_print_insn (sim_cpu
*cpu
, int verbose
)
760 unsigned int i
, n
, total
, max_val
, max_name_len
;
761 SIM_DESC sd
= CPU_STATE (cpu
);
762 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
765 /* If MAX_INSNS not set, insn profiling isn't supported. */
766 if (CPU_MAX_INSNS (cpu
) == 0)
769 profile_printf (sd
, cpu
, "Instruction Statistics");
770 #ifdef SIM_HAVE_ADDR_RANGE
771 if (PROFILE_RANGE (data
)->ranges
)
772 profile_printf (sd
, cpu
, " (for selected address range(s))");
774 profile_printf (sd
, cpu
, "\n\n");
776 /* First pass over data computes various things. */
780 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
782 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
786 total
+= PROFILE_INSN_COUNT (data
) [i
];
787 if (PROFILE_INSN_COUNT (data
) [i
] > max_val
)
788 max_val
= PROFILE_INSN_COUNT (data
) [i
];
790 if (n
> max_name_len
)
793 /* set the total insn count, in case client is being lazy */
794 if (! PROFILE_TOTAL_INSN_COUNT (data
))
795 PROFILE_TOTAL_INSN_COUNT (data
) = total
;
797 profile_printf (sd
, cpu
, " Total: %s insns\n", COMMAS (total
));
799 if (verbose
&& max_val
!= 0)
801 /* Now we can print the histogram. */
802 profile_printf (sd
, cpu
, "\n");
803 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
805 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
809 if (PROFILE_INSN_COUNT (data
) [i
] != 0)
811 profile_printf (sd
, cpu
, " %*s: %*s: ",
813 max_val
< 10000 ? 5 : 10,
814 COMMAS (PROFILE_INSN_COUNT (data
) [i
]));
815 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
816 PROFILE_INSN_COUNT (data
) [i
],
818 profile_printf (sd
, cpu
, "\n");
823 profile_printf (sd
, cpu
, "\n");
828 #if WITH_PROFILE_MEMORY_P
831 profile_print_memory (sim_cpu
*cpu
, int verbose
)
834 unsigned int total_read
, total_write
;
835 unsigned int max_val
, max_name_len
;
836 /* FIXME: Need to add smp support. */
837 SIM_DESC sd
= CPU_STATE (cpu
);
838 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
841 profile_printf (sd
, cpu
, "Memory Access Statistics\n\n");
843 /* First pass over data computes various things. */
844 max_val
= total_read
= total_write
= max_name_len
= 0;
845 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
847 total_read
+= PROFILE_READ_COUNT (data
) [i
];
848 total_write
+= PROFILE_WRITE_COUNT (data
) [i
];
849 if (PROFILE_READ_COUNT (data
) [i
] > max_val
)
850 max_val
= PROFILE_READ_COUNT (data
) [i
];
851 if (PROFILE_WRITE_COUNT (data
) [i
] > max_val
)
852 max_val
= PROFILE_WRITE_COUNT (data
) [i
];
853 n
= strlen (MODE_NAME (i
));
854 if (n
> max_name_len
)
858 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
859 profile_printf (sd
, cpu
, " Total read: %s accesses\n",
860 COMMAS (total_read
));
861 profile_printf (sd
, cpu
, " Total write: %s accesses\n",
862 COMMAS (total_write
));
864 if (verbose
&& max_val
!= 0)
866 /* FIXME: Need to separate instruction fetches from data fetches
867 as the former swamps the latter. */
868 /* Now we can print the histogram. */
869 profile_printf (sd
, cpu
, "\n");
870 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
872 if (PROFILE_READ_COUNT (data
) [i
] != 0)
874 profile_printf (sd
, cpu
, " %*s read: %*s: ",
875 max_name_len
, MODE_NAME (i
),
876 max_val
< 10000 ? 5 : 10,
877 COMMAS (PROFILE_READ_COUNT (data
) [i
]));
878 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
879 PROFILE_READ_COUNT (data
) [i
],
881 profile_printf (sd
, cpu
, "\n");
883 if (PROFILE_WRITE_COUNT (data
) [i
] != 0)
885 profile_printf (sd
, cpu
, " %*s write: %*s: ",
886 max_name_len
, MODE_NAME (i
),
887 max_val
< 10000 ? 5 : 10,
888 COMMAS (PROFILE_WRITE_COUNT (data
) [i
]));
889 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
890 PROFILE_WRITE_COUNT (data
) [i
],
892 profile_printf (sd
, cpu
, "\n");
897 profile_printf (sd
, cpu
, "\n");
902 #if WITH_PROFILE_CORE_P
905 profile_print_core (sim_cpu
*cpu
, int verbose
)
908 unsigned int max_val
;
909 /* FIXME: Need to add smp support. */
910 SIM_DESC sd
= CPU_STATE (cpu
);
911 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
914 profile_printf (sd
, cpu
, "CORE Statistics\n\n");
916 /* First pass over data computes various things. */
921 for (map
= 0; map
< nr_maps
; map
++)
923 total
+= PROFILE_CORE_COUNT (data
) [map
];
924 if (PROFILE_CORE_COUNT (data
) [map
] > max_val
)
925 max_val
= PROFILE_CORE_COUNT (data
) [map
];
929 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
930 profile_printf (sd
, cpu
, " Total: %s accesses\n",
933 if (verbose
&& max_val
!= 0)
936 /* Now we can print the histogram. */
937 profile_printf (sd
, cpu
, "\n");
938 for (map
= 0; map
< nr_maps
; map
++)
940 if (PROFILE_CORE_COUNT (data
) [map
] != 0)
942 profile_printf (sd
, cpu
, "%10s:", map_to_str (map
));
943 profile_printf (sd
, cpu
, "%*s: ",
944 max_val
< 10000 ? 5 : 10,
945 COMMAS (PROFILE_CORE_COUNT (data
) [map
]));
946 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
947 PROFILE_CORE_COUNT (data
) [map
],
949 profile_printf (sd
, cpu
, "\n");
954 profile_printf (sd
, cpu
, "\n");
959 #if WITH_PROFILE_MODEL_P
962 profile_print_model (sim_cpu
*cpu
, int verbose
)
964 SIM_DESC sd
= CPU_STATE (cpu
);
965 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
966 unsigned long cti_stall_cycles
= PROFILE_MODEL_CTI_STALL_CYCLES (data
);
967 unsigned long load_stall_cycles
= PROFILE_MODEL_LOAD_STALL_CYCLES (data
);
968 unsigned long total_cycles
= PROFILE_MODEL_TOTAL_CYCLES (data
);
971 profile_printf (sd
, cpu
, "Model %s Timing Information",
972 MODEL_NAME (CPU_MODEL (cpu
)));
973 #ifdef SIM_HAVE_ADDR_RANGE
974 if (PROFILE_RANGE (data
)->ranges
)
975 profile_printf (sd
, cpu
, " (for selected address range(s))");
977 profile_printf (sd
, cpu
, "\n\n");
978 profile_printf (sd
, cpu
, " %-*s %s\n",
979 PROFILE_LABEL_WIDTH
, "Taken branches:",
980 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data
)));
981 profile_printf (sd
, cpu
, " %-*s %s\n",
982 PROFILE_LABEL_WIDTH
, "Untaken branches:",
983 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data
)));
984 profile_printf (sd
, cpu
, " %-*s %s\n",
985 PROFILE_LABEL_WIDTH
, "Cycles stalled due to branches:",
986 COMMAS (cti_stall_cycles
));
987 profile_printf (sd
, cpu
, " %-*s %s\n",
988 PROFILE_LABEL_WIDTH
, "Cycles stalled due to loads:",
989 COMMAS (load_stall_cycles
));
990 profile_printf (sd
, cpu
, " %-*s %s\n",
991 PROFILE_LABEL_WIDTH
, "Total cycles (*approximate*):",
992 COMMAS (total_cycles
));
993 profile_printf (sd
, cpu
, "\n");
999 sim_profile_print_bar (SIM_DESC sd
, sim_cpu
*cpu
, unsigned int width
,
1000 unsigned int val
, unsigned int max_val
)
1002 unsigned int i
, count
;
1004 count
= ((double) val
/ (double) max_val
) * (double) width
;
1006 for (i
= 0; i
< count
; ++i
)
1007 profile_printf (sd
, cpu
, "*");
1010 /* Print the simulator's execution speed for CPU. */
1013 profile_print_speed (sim_cpu
*cpu
)
1015 SIM_DESC sd
= CPU_STATE (cpu
);
1016 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1017 unsigned long milliseconds
= sim_events_elapsed_time (sd
);
1018 unsigned long total
= PROFILE_TOTAL_INSN_COUNT (data
);
1023 profile_printf (sd
, cpu
, "Simulator Execution Speed\n\n");
1026 profile_printf (sd
, cpu
, " Total instructions: %s\n", COMMAS (total
));
1028 if (milliseconds
< 1000)
1029 profile_printf (sd
, cpu
, " Total execution time: < 1 second\n\n");
1032 /* The printing of the time rounded to 2 decimal places makes the speed
1033 calculation seem incorrect [even though it is correct]. So round
1034 MILLISECONDS first. This can marginally affect the result, but it's
1035 better that the user not perceive there's a math error. */
1036 secs
= (double) milliseconds
/ 1000;
1037 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1038 profile_printf (sd
, cpu
, " Total execution time : %.2f seconds\n", secs
);
1039 /* Don't confuse things with data that isn't useful.
1040 If we ran for less than 2 seconds, only use the data if we
1041 executed more than 100,000 insns. */
1042 if (secs
>= 2 || total
>= 100000)
1043 profile_printf (sd
, cpu
, " Simulator speed: %s insns/second\n",
1044 COMMAS ((unsigned long) ((double) total
/ secs
)));
1047 /* Print simulated execution time if the cpu frequency has been specified. */
1048 clock
= PROFILE_CPU_FREQ (data
);
1051 if (clock
>= 1000000)
1052 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f MHz\n",
1055 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f Hz\n", clock
);
1057 #if WITH_PROFILE_MODEL_P
1058 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1060 /* The printing of the time rounded to 2 decimal places makes the
1061 speed calculation seem incorrect [even though it is correct].
1062 So round SECS first. This can marginally affect the result,
1063 but it's better that the user not perceive there's a math
1065 secs
= PROFILE_MODEL_TOTAL_CYCLES (data
) / clock
;
1066 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1067 profile_printf (sd
, cpu
, " Simulated execution time: %.2f seconds\n",
1070 #endif /* WITH_PROFILE_MODEL_P */
1074 #ifdef SIM_HAVE_ADDR_RANGE
1075 /* Print selected address ranges. */
1078 profile_print_addr_ranges (sim_cpu
*cpu
)
1080 ADDR_SUBRANGE
*asr
= PROFILE_RANGE (CPU_PROFILE_DATA (cpu
))->ranges
;
1081 SIM_DESC sd
= CPU_STATE (cpu
);
1085 profile_printf (sd
, cpu
, "Selected address ranges\n\n");
1088 profile_printf (sd
, cpu
, " 0x%lx - 0x%lx\n",
1089 (long) asr
->start
, (long) asr
->end
);
1092 profile_printf (sd
, cpu
, "\n");
1097 /* Top level function to print all summary profile information.
1098 It is [currently] intended that all such data is printed by this function.
1099 I'd rather keep it all in one place for now. To that end, MISC_CPU and
1100 MISC are callbacks used to print any miscellaneous data.
1102 One might want to add a user option that allows printing by type or by cpu
1103 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
1104 This may be a case of featuritis so it's currently left out.
1106 Note that results are indented two spaces to distinguish them from
1110 profile_info (SIM_DESC sd
, int verbose
)
1113 int print_title_p
= 0;
1115 /* Only print the title if some data has been collected. */
1116 /* ??? Why don't we just exit if no data collected? */
1117 /* FIXME: If the number of processors can be selected on the command line,
1118 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
1120 for (c
= 0; c
< MAX_NR_PROCESSORS
&& !print_title_p
; ++c
)
1122 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1123 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1125 for (i
= 0; i
< MAX_PROFILE_VALUES
; ++i
)
1126 if (PROFILE_FLAGS (data
) [i
])
1128 profile_printf (sd
, cpu
, "Summary profiling results:\n\n");
1134 /* Loop, cpu by cpu, printing results. */
1136 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
1138 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1139 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1141 if (MAX_NR_PROCESSORS
> 1
1143 #if WITH_PROFILE_INSN_P
1144 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
]
1146 #if WITH_PROFILE_MEMORY_P
1147 || PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
]
1149 #if WITH_PROFILE_CORE_P
1150 || PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
]
1152 #if WITH_PROFILE_MODEL_P
1153 || PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
]
1155 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE && defined(CGEN_ARCH)
1156 || PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
]
1158 #if WITH_PROFILE_PC_P
1159 || PROFILE_FLAGS (data
) [PROFILE_PC_IDX
]
1163 profile_printf (sd
, cpu
, "CPU %d\n\n", c
);
1166 #ifdef SIM_HAVE_ADDR_RANGE
1168 && (PROFILE_INSN_P (cpu
)
1169 || PROFILE_MODEL_P (cpu
)))
1170 profile_print_addr_ranges (cpu
);
1173 #if WITH_PROFILE_INSN_P
1174 if (PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1175 profile_print_insn (cpu
, verbose
);
1178 #if WITH_PROFILE_MEMORY_P
1179 if (PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
])
1180 profile_print_memory (cpu
, verbose
);
1183 #if WITH_PROFILE_CORE_P
1184 if (PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
])
1185 profile_print_core (cpu
, verbose
);
1188 #if WITH_PROFILE_MODEL_P
1189 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1190 profile_print_model (cpu
, verbose
);
1193 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE && defined(CGEN_ARCH)
1194 if (PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
])
1195 scache_print_profile (cpu
, verbose
);
1198 #if WITH_PROFILE_PC_P
1199 if (PROFILE_FLAGS (data
) [PROFILE_PC_IDX
])
1200 profile_print_pc (cpu
, verbose
);
1203 /* Print cpu-specific data before the execution speed. */
1204 if (PROFILE_INFO_CPU_CALLBACK (data
) != NULL
)
1205 PROFILE_INFO_CPU_CALLBACK (data
) (cpu
, verbose
);
1207 /* Always try to print execution time and speed. */
1209 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1210 profile_print_speed (cpu
);
1213 /* Finally print non-cpu specific miscellaneous data. */
1214 if (STATE_PROFILE_INFO_CALLBACK (sd
))
1215 STATE_PROFILE_INFO_CALLBACK (sd
) (sd
, verbose
);
1219 /* Provide a prototype to silence -Wmissing-prototypes. */
1220 SIM_RC
sim_install_profile (SIM_DESC sd
);
1222 /* Install profiling support in the simulator. */
1224 sim_install_profile (SIM_DESC sd
)
1228 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1229 sim_add_option_table (sd
, NULL
, profile_options
);
1230 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1231 memset (CPU_PROFILE_DATA (STATE_CPU (sd
, i
)), 0,
1232 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd
, i
))));
1233 #if WITH_PROFILE_INSN_P
1234 sim_module_add_init_fn (sd
, profile_insn_init
);
1236 #if WITH_PROFILE_PC_P
1237 sim_module_add_uninstall_fn (sd
, profile_pc_uninstall
);
1238 sim_module_add_init_fn (sd
, profile_pc_init
);
1240 sim_module_add_init_fn (sd
, profile_init
);
1241 sim_module_add_uninstall_fn (sd
, profile_uninstall
);
1242 sim_module_add_info_fn (sd
, profile_info
);
1247 profile_init (SIM_DESC sd
)
1249 #ifdef SIM_HAVE_ADDR_RANGE
1250 /* Check if a range has been specified without specifying what to
1255 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1257 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1259 if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)))
1260 && ! (PROFILE_INSN_P (cpu
)
1261 || PROFILE_MODEL_P (cpu
)))
1263 sim_io_eprintf_cpu (cpu
, "Profiling address range specified without --profile-insn or --profile-model.\n");
1264 sim_io_eprintf_cpu (cpu
, "Address range ignored.\n");
1265 sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
1266 0, ~ (address_word
) 0);
1276 profile_uninstall (SIM_DESC sd
)
1280 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1282 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1283 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1285 if (PROFILE_FILE (data
) != NULL
)
1287 /* If output from different cpus is going to the same file,
1288 avoid closing the file twice. */
1289 for (j
= 0; j
< i
; ++j
)
1290 if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, j
)))
1291 == PROFILE_FILE (data
))
1294 fclose (PROFILE_FILE (data
));
1297 if (PROFILE_INSN_COUNT (data
) != NULL
)
1298 free (PROFILE_INSN_COUNT (data
));