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/>. */
22 #include "sim-options.h"
23 #include "sim-assert.h"
29 #if !WITH_PROFILE_PC_P
30 static unsigned int _profile_stub
;
31 # define PROFILE_PC_FREQ(p) _profile_stub
32 # define PROFILE_PC_NR_BUCKETS(p) _profile_stub
33 # define PROFILE_PC_SHIFT(p) _profile_stub
34 # define PROFILE_PC_START(p) _profile_stub
35 # define PROFILE_PC_END(p) _profile_stub
36 # define PROFILE_INSN_COUNT(p) &_profile_stub
39 #define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n))
41 static MODULE_INIT_FN profile_init
;
42 static MODULE_UNINSTALL_FN profile_uninstall
;
44 static DECLARE_OPTION_HANDLER (profile_option_handler
);
47 OPTION_PROFILE_INSN
= OPTION_START
,
48 OPTION_PROFILE_MEMORY
,
52 OPTION_PROFILE_CPU_FREQUENCY
,
54 OPTION_PROFILE_PC_RANGE
,
55 OPTION_PROFILE_PC_GRANULARITY
,
57 OPTION_PROFILE_FUNCTION
60 static const OPTION profile_options
[] = {
61 { {"profile", optional_argument
, NULL
, 'p'},
62 'p', "on|off", "Perform profiling",
63 profile_option_handler
, NULL
},
64 { {"profile-insn", optional_argument
, NULL
, OPTION_PROFILE_INSN
},
65 '\0', "on|off", "Perform instruction profiling",
66 profile_option_handler
, NULL
},
67 { {"profile-memory", optional_argument
, NULL
, OPTION_PROFILE_MEMORY
},
68 '\0', "on|off", "Perform memory profiling",
69 profile_option_handler
, NULL
},
70 { {"profile-core", optional_argument
, NULL
, OPTION_PROFILE_CORE
},
71 '\0', "on|off", "Perform CORE profiling",
72 profile_option_handler
, NULL
},
73 { {"profile-model", optional_argument
, NULL
, OPTION_PROFILE_MODEL
},
74 '\0', "on|off", "Perform model profiling",
75 profile_option_handler
, NULL
},
76 { {"profile-cpu-frequency", required_argument
, NULL
,
77 OPTION_PROFILE_CPU_FREQUENCY
},
78 '\0', "CPU FREQUENCY", "Specify the speed of the simulated cpu clock",
79 profile_option_handler
, NULL
},
81 { {"profile-file", required_argument
, NULL
, OPTION_PROFILE_FILE
},
82 '\0', "FILE NAME", "Specify profile output file",
83 profile_option_handler
, NULL
},
85 { {"profile-pc", optional_argument
, NULL
, OPTION_PROFILE_PC
},
86 '\0', "on|off", "Perform PC profiling",
87 profile_option_handler
, NULL
},
88 { {"profile-pc-frequency", required_argument
, NULL
, 'F'},
89 'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency",
90 profile_option_handler
, NULL
},
91 { {"profile-pc-size", required_argument
, NULL
, 'S'},
92 'S', "PC PROFILE SIZE", "Specify PC profiling size",
93 profile_option_handler
, NULL
},
94 { {"profile-pc-granularity", required_argument
, NULL
, OPTION_PROFILE_PC_GRANULARITY
},
95 '\0', "PC PROFILE GRANULARITY", "Specify PC profiling sample coverage",
96 profile_option_handler
, NULL
},
97 { {"profile-pc-range", required_argument
, NULL
, OPTION_PROFILE_PC_RANGE
},
98 '\0', "BASE,BOUND", "Specify PC profiling address range",
99 profile_option_handler
, NULL
},
101 #ifdef SIM_HAVE_ADDR_RANGE
102 { {"profile-range", required_argument
, NULL
, OPTION_PROFILE_RANGE
},
103 '\0', "START,END", "Specify range of addresses for instruction and model profiling",
104 profile_option_handler
, NULL
},
106 { {"profile-function", required_argument
, NULL
, OPTION_PROFILE_FUNCTION
},
107 '\0', "FUNCTION", "Specify function to profile",
108 profile_option_handler
, NULL
},
112 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
115 /* Set/reset the profile options indicated in MASK. */
118 set_profile_option_mask (SIM_DESC sd
, const char *name
, int mask
, const char *arg
)
126 if (strcmp (arg
, "yes") == 0
127 || strcmp (arg
, "on") == 0
128 || strcmp (arg
, "1") == 0)
130 else if (strcmp (arg
, "no") == 0
131 || strcmp (arg
, "off") == 0
132 || strcmp (arg
, "0") == 0)
136 sim_io_eprintf (sd
, "Argument `%s' for `--profile%s' invalid, one of `on', `off', `yes', `no' expected\n", arg
, name
);
141 /* update applicable profile bits */
142 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
144 if ((mask
& (1 << profile_nr
)) == 0)
147 #if 0 /* see sim-trace.c, set flags in STATE here if/when there are any */
148 /* Set non-cpu specific values. */
156 /* Set cpu values. */
157 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
159 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
] = profile_val
;
163 /* Re-compute the cpu profile summary. */
166 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
167 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
171 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
173 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 0;
174 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
176 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
])
178 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
188 /* Set one profile option based on its IDX value.
189 Not static as cgen-scache.c uses it. */
192 sim_profile_set_option (SIM_DESC sd
, const char *name
, int idx
, const char *arg
)
194 return set_profile_option_mask (sd
, name
, 1 << idx
, arg
);
198 parse_frequency (SIM_DESC sd
, const char *arg
, unsigned long *freq
)
201 /* First, parse a decimal number. */
206 for (/**/; *ch
!= '\0'; ++ch
)
210 *freq
= *freq
* 10 + (*ch
- '0');
213 /* Accept KHz, MHz or Hz as a suffix. */
214 if (tolower (*ch
) == 'm')
219 else if (tolower (*ch
) == 'k')
225 if (tolower (*ch
) == 'h')
228 if (tolower (*ch
) == 'z')
235 sim_io_eprintf (sd
, "Invalid argument for --profile-cpu-frequency: %s\n",
245 profile_option_handler (SIM_DESC sd
,
253 /* FIXME: Need to handle `cpu' arg. */
259 sim_io_eprintf (sd
, "Profiling not compiled in, `-p' ignored\n");
261 return set_profile_option_mask (sd
, "profile", PROFILE_USEFUL_MASK
,
265 case OPTION_PROFILE_INSN
:
266 if (WITH_PROFILE_INSN_P
)
267 return sim_profile_set_option (sd
, "-insn", PROFILE_INSN_IDX
, arg
);
269 sim_io_eprintf (sd
, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
272 case OPTION_PROFILE_MEMORY
:
273 if (WITH_PROFILE_MEMORY_P
)
274 return sim_profile_set_option (sd
, "-memory", PROFILE_MEMORY_IDX
, arg
);
276 sim_io_eprintf (sd
, "Memory profiling not compiled in, `--profile-memory' ignored\n");
279 case OPTION_PROFILE_CORE
:
280 if (WITH_PROFILE_CORE_P
)
281 return sim_profile_set_option (sd
, "-core", PROFILE_CORE_IDX
, arg
);
283 sim_io_eprintf (sd
, "CORE profiling not compiled in, `--profile-core' ignored\n");
286 case OPTION_PROFILE_MODEL
:
287 if (WITH_PROFILE_MODEL_P
)
288 return sim_profile_set_option (sd
, "-model", PROFILE_MODEL_IDX
, arg
);
290 sim_io_eprintf (sd
, "Model profiling not compiled in, `--profile-model' ignored\n");
293 case OPTION_PROFILE_CPU_FREQUENCY
:
296 SIM_RC rc
= parse_frequency (sd
, arg
, &val
);
299 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
300 PROFILE_CPU_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
,cpu_nr
))) = val
;
305 case OPTION_PROFILE_FILE
:
306 /* FIXME: Might want this to apply to pc profiling only,
307 or have two profile file options. */
309 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-file' ignored\n");
312 FILE *f
= fopen (arg
, "w");
316 sim_io_eprintf (sd
, "Unable to open profile output file `%s'\n", arg
);
319 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
320 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = f
;
324 case OPTION_PROFILE_PC
:
325 if (WITH_PROFILE_PC_P
)
326 return sim_profile_set_option (sd
, "-pc", PROFILE_PC_IDX
, arg
);
328 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc' ignored\n");
332 if (WITH_PROFILE_PC_P
)
334 /* FIXME: Validate arg. */
335 int val
= atoi (arg
);
336 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
337 PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
338 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
339 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
342 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
346 if (WITH_PROFILE_PC_P
)
348 /* FIXME: Validate arg. */
349 int val
= atoi (arg
);
350 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
351 PROFILE_PC_NR_BUCKETS (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
352 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
353 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
356 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
359 case OPTION_PROFILE_PC_GRANULARITY
:
360 if (WITH_PROFILE_PC_P
)
363 int val
= atoi (arg
);
364 /* check that the granularity is a power of two */
366 while (val
> (1 << shift
))
370 if (val
!= (1 << shift
))
372 sim_io_eprintf (sd
, "PC profiling granularity not a power of two\n");
377 sim_io_eprintf (sd
, "PC profiling granularity too small");
380 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
381 PROFILE_PC_SHIFT (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = shift
;
382 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
383 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
386 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
389 case OPTION_PROFILE_PC_RANGE
:
390 if (WITH_PROFILE_PC_P
)
392 /* FIXME: Validate args */
396 base
= strtoul (chp
, &chp
, 0);
399 sim_io_eprintf (sd
, "--profile-pc-range missing BOUND argument\n");
402 bound
= strtoul (chp
+ 1, NULL
, 0);
403 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
405 PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = base
;
406 PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = bound
;
408 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
409 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
412 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
415 #ifdef SIM_HAVE_ADDR_RANGE
416 case OPTION_PROFILE_RANGE
:
420 unsigned long start
,end
;
421 start
= strtoul (chp
, &chp
, 0);
424 sim_io_eprintf (sd
, "--profile-range missing END argument\n");
427 end
= strtoul (chp
+ 1, NULL
, 0);
428 /* FIXME: Argument validation. */
430 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
433 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
434 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))),
438 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-range' ignored\n");
441 case OPTION_PROFILE_FUNCTION
:
444 /*wip: need to compute function range given name*/
447 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-function' ignored\n");
449 #endif /* SIM_HAVE_ADDR_RANGE */
455 /* Profiling output hooks. */
458 profile_vprintf (SIM_DESC sd
, sim_cpu
*cpu
, const char *fmt
, va_list ap
)
460 FILE *fp
= PROFILE_FILE (CPU_PROFILE_DATA (cpu
));
462 /* If an output file was given, redirect output to that. */
464 vfprintf (fp
, fmt
, ap
);
466 sim_io_evprintf (sd
, fmt
, ap
);
469 __attribute__ ((format (printf
, 3, 4)))
471 profile_printf (SIM_DESC sd
, sim_cpu
*cpu
, const char *fmt
, ...)
476 profile_vprintf (sd
, cpu
, fmt
, ap
);
480 /* PC profiling support */
482 #if WITH_PROFILE_PC_P
485 profile_pc_cleanup (SIM_DESC sd
)
488 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
490 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
491 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
492 if (PROFILE_PC_COUNT (data
) != NULL
)
493 free (PROFILE_PC_COUNT (data
));
494 PROFILE_PC_COUNT (data
) = NULL
;
495 if (PROFILE_PC_EVENT (data
) != NULL
)
496 sim_events_deschedule (sd
, PROFILE_PC_EVENT (data
));
497 PROFILE_PC_EVENT (data
) = NULL
;
503 profile_pc_uninstall (SIM_DESC sd
)
505 profile_pc_cleanup (sd
);
509 profile_pc_event (SIM_DESC sd
,
512 sim_cpu
*cpu
= (sim_cpu
*) data
;
513 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
514 address_word pc
= sim_pc_get (cpu
);
516 i
= (pc
- PROFILE_PC_START (profile
)) >> PROFILE_PC_SHIFT (profile
);
517 if (i
< PROFILE_PC_NR_BUCKETS (profile
))
518 PROFILE_PC_COUNT (profile
) [i
] += 1; /* Overflow? */
520 PROFILE_PC_COUNT (profile
) [PROFILE_PC_NR_BUCKETS (profile
)] += 1;
521 PROFILE_PC_EVENT (profile
) =
522 sim_events_schedule (sd
, PROFILE_PC_FREQ (profile
), profile_pc_event
, cpu
);
526 profile_pc_init (SIM_DESC sd
)
529 profile_pc_cleanup (sd
);
530 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
532 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
533 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
534 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
])
537 /* fill in the frequency if not specified */
538 if (PROFILE_PC_FREQ (data
) == 0)
539 PROFILE_PC_FREQ (data
) = 257;
540 /* fill in the start/end if not specified */
541 if (PROFILE_PC_END (data
) == 0)
543 PROFILE_PC_START (data
) = STATE_TEXT_START (sd
);
544 PROFILE_PC_END (data
) = STATE_TEXT_END (sd
);
546 /* Compute the number of buckets if not specified. */
547 if (PROFILE_PC_NR_BUCKETS (data
) == 0)
549 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
550 PROFILE_PC_NR_BUCKETS (data
) = 16;
553 if (PROFILE_PC_END (data
) == 0)
555 /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
556 PROFILE_PC_NR_BUCKETS (data
) =
557 ((1ULL << sizeof (sim_cia
) * (8 - 1))
558 / (PROFILE_PC_BUCKET_SIZE (data
) / 2));
562 PROFILE_PC_NR_BUCKETS (data
) =
563 ((PROFILE_PC_END (data
)
564 - PROFILE_PC_START (data
)
565 + PROFILE_PC_BUCKET_SIZE (data
) - 1)
566 / PROFILE_PC_BUCKET_SIZE (data
));
570 /* Compute the bucket size if not specified. Ensure that it
571 is rounded up to the next power of two */
572 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
574 if (PROFILE_PC_END (data
) == 0)
575 /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
576 bucket_size
= ((1ULL << ((sizeof (sim_cia
) * 8) - 1))
577 / (PROFILE_PC_NR_BUCKETS (data
) / 2));
579 bucket_size
= ((PROFILE_PC_END (data
)
580 - PROFILE_PC_START (data
)
581 + PROFILE_PC_NR_BUCKETS (data
) - 1)
582 / PROFILE_PC_NR_BUCKETS (data
));
583 PROFILE_PC_SHIFT (data
) = 0;
584 while (bucket_size
> PROFILE_PC_BUCKET_SIZE (data
))
586 PROFILE_PC_SHIFT (data
) += 1;
589 /* Align the end address with bucket size */
590 if (PROFILE_PC_END (data
) != 0)
591 PROFILE_PC_END (data
) = (PROFILE_PC_START (data
)
592 + (PROFILE_PC_BUCKET_SIZE (data
)
593 * PROFILE_PC_NR_BUCKETS (data
)));
594 /* create the relevant buffers */
595 PROFILE_PC_COUNT (data
) =
596 NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data
) + 1);
597 PROFILE_PC_EVENT (data
) =
598 sim_events_schedule (sd
,
599 PROFILE_PC_FREQ (data
),
608 profile_print_pc (sim_cpu
*cpu
, int verbose
)
610 SIM_DESC sd
= CPU_STATE (cpu
);
611 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
617 if (PROFILE_PC_COUNT (profile
) == 0)
620 profile_printf (sd
, cpu
, "Program Counter Statistics:\n\n");
622 /* First pass over data computes various things. */
625 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
627 total
+= PROFILE_PC_COUNT (profile
) [i
];
628 if (PROFILE_PC_COUNT (profile
) [i
] > max_val
)
629 max_val
= PROFILE_PC_COUNT (profile
) [i
];
632 profile_printf (sd
, cpu
, " Total samples: %s\n",
634 profile_printf (sd
, cpu
, " Granularity: %s bytes per bucket\n",
635 COMMAS (PROFILE_PC_BUCKET_SIZE (profile
)));
636 profile_printf (sd
, cpu
, " Size: %s buckets\n",
637 COMMAS (PROFILE_PC_NR_BUCKETS (profile
)));
638 profile_printf (sd
, cpu
, " Frequency: %s cycles per sample\n",
639 COMMAS (PROFILE_PC_FREQ (profile
)));
641 if (PROFILE_PC_END (profile
) != 0)
642 profile_printf (sd
, cpu
, " Range: 0x%lx 0x%lx\n",
643 (long) PROFILE_PC_START (profile
),
644 (long) PROFILE_PC_END (profile
));
646 if (verbose
&& max_val
!= 0)
648 /* Now we can print the histogram. */
649 profile_printf (sd
, cpu
, "\n");
650 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
652 if (PROFILE_PC_COUNT (profile
) [i
] != 0)
654 profile_printf (sd
, cpu
, " ");
655 if (i
== PROFILE_PC_NR_BUCKETS (profile
))
656 profile_printf (sd
, cpu
, "%10s:", "overflow");
658 profile_printf (sd
, cpu
, "0x%08lx:",
659 (long) (PROFILE_PC_START (profile
)
660 + (i
* PROFILE_PC_BUCKET_SIZE (profile
))));
661 profile_printf (sd
, cpu
, " %*s",
662 max_val
< 10000 ? 5 : 10,
663 COMMAS (PROFILE_PC_COUNT (profile
) [i
]));
664 profile_printf (sd
, cpu
, " %4.1f",
665 (PROFILE_PC_COUNT (profile
) [i
] * 100.0) / total
);
666 profile_printf (sd
, cpu
, ": ");
667 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
668 PROFILE_PC_COUNT (profile
) [i
],
670 profile_printf (sd
, cpu
, "\n");
675 /* dump the histogram to the file "gmon.out" using BSD's gprof file
677 /* Since a profile data file is in the native format of the host on
678 which the profile is being, endian issues are not considered in
680 /* FIXME: Is this the best place for this code? */
682 FILE *pf
= fopen ("gmon.out", "wb");
685 sim_io_eprintf (sd
, "Failed to open \"gmon.out\" profile file\n");
689 /* FIXME: what if the target has a 64 bit PC? */
690 unsigned32 header
[3];
692 if (PROFILE_PC_END (profile
) != 0)
694 header
[0] = PROFILE_PC_START (profile
);
695 header
[1] = PROFILE_PC_END (profile
);
702 /* size of sample buffer (+ header) */
703 header
[2] = PROFILE_PC_NR_BUCKETS (profile
) * 2 + sizeof (header
);
705 /* Header must be written out in target byte order. */
710 ok
= fwrite (&header
, sizeof (header
), 1, pf
);
712 ok
&& (loop
< PROFILE_PC_NR_BUCKETS (profile
));
716 if (PROFILE_PC_COUNT (profile
) [loop
] >= 0xffff)
719 sample
= PROFILE_PC_COUNT (profile
) [loop
];
721 ok
= fwrite (&sample
, sizeof (sample
), 1, pf
);
724 sim_io_eprintf (sd
, "Failed to write to \"gmon.out\" profile file\n");
729 profile_printf (sd
, cpu
, "\n");
734 /* Summary printing support. */
736 #if WITH_PROFILE_INSN_P
739 profile_insn_init (SIM_DESC sd
)
743 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
745 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
747 if (CPU_MAX_INSNS (cpu
) > 0)
748 PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu
)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu
));
755 profile_print_insn (sim_cpu
*cpu
, int verbose
)
757 unsigned int i
, n
, total
, max_val
, max_name_len
;
758 SIM_DESC sd
= CPU_STATE (cpu
);
759 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
762 /* If MAX_INSNS not set, insn profiling isn't supported. */
763 if (CPU_MAX_INSNS (cpu
) == 0)
766 profile_printf (sd
, cpu
, "Instruction Statistics");
767 #ifdef SIM_HAVE_ADDR_RANGE
768 if (PROFILE_RANGE (data
)->ranges
)
769 profile_printf (sd
, cpu
, " (for selected address range(s))");
771 profile_printf (sd
, cpu
, "\n\n");
773 /* First pass over data computes various things. */
777 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
779 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
783 total
+= PROFILE_INSN_COUNT (data
) [i
];
784 if (PROFILE_INSN_COUNT (data
) [i
] > max_val
)
785 max_val
= PROFILE_INSN_COUNT (data
) [i
];
787 if (n
> max_name_len
)
790 /* set the total insn count, in case client is being lazy */
791 if (! PROFILE_TOTAL_INSN_COUNT (data
))
792 PROFILE_TOTAL_INSN_COUNT (data
) = total
;
794 profile_printf (sd
, cpu
, " Total: %s insns\n", COMMAS (total
));
796 if (verbose
&& max_val
!= 0)
798 /* Now we can print the histogram. */
799 profile_printf (sd
, cpu
, "\n");
800 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
802 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
806 if (PROFILE_INSN_COUNT (data
) [i
] != 0)
808 profile_printf (sd
, cpu
, " %*s: %*s: ",
810 max_val
< 10000 ? 5 : 10,
811 COMMAS (PROFILE_INSN_COUNT (data
) [i
]));
812 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
813 PROFILE_INSN_COUNT (data
) [i
],
815 profile_printf (sd
, cpu
, "\n");
820 profile_printf (sd
, cpu
, "\n");
825 #if WITH_PROFILE_MEMORY_P
828 profile_print_memory (sim_cpu
*cpu
, int verbose
)
831 unsigned int total_read
, total_write
;
832 unsigned int max_val
, max_name_len
;
833 /* FIXME: Need to add smp support. */
834 SIM_DESC sd
= CPU_STATE (cpu
);
835 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
838 profile_printf (sd
, cpu
, "Memory Access Statistics\n\n");
840 /* First pass over data computes various things. */
841 max_val
= total_read
= total_write
= max_name_len
= 0;
842 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
844 total_read
+= PROFILE_READ_COUNT (data
) [i
];
845 total_write
+= PROFILE_WRITE_COUNT (data
) [i
];
846 if (PROFILE_READ_COUNT (data
) [i
] > max_val
)
847 max_val
= PROFILE_READ_COUNT (data
) [i
];
848 if (PROFILE_WRITE_COUNT (data
) [i
] > max_val
)
849 max_val
= PROFILE_WRITE_COUNT (data
) [i
];
850 n
= strlen (MODE_NAME (i
));
851 if (n
> max_name_len
)
855 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
856 profile_printf (sd
, cpu
, " Total read: %s accesses\n",
857 COMMAS (total_read
));
858 profile_printf (sd
, cpu
, " Total write: %s accesses\n",
859 COMMAS (total_write
));
861 if (verbose
&& max_val
!= 0)
863 /* FIXME: Need to separate instruction fetches from data fetches
864 as the former swamps the latter. */
865 /* Now we can print the histogram. */
866 profile_printf (sd
, cpu
, "\n");
867 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
869 if (PROFILE_READ_COUNT (data
) [i
] != 0)
871 profile_printf (sd
, cpu
, " %*s read: %*s: ",
872 max_name_len
, MODE_NAME (i
),
873 max_val
< 10000 ? 5 : 10,
874 COMMAS (PROFILE_READ_COUNT (data
) [i
]));
875 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
876 PROFILE_READ_COUNT (data
) [i
],
878 profile_printf (sd
, cpu
, "\n");
880 if (PROFILE_WRITE_COUNT (data
) [i
] != 0)
882 profile_printf (sd
, cpu
, " %*s write: %*s: ",
883 max_name_len
, MODE_NAME (i
),
884 max_val
< 10000 ? 5 : 10,
885 COMMAS (PROFILE_WRITE_COUNT (data
) [i
]));
886 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
887 PROFILE_WRITE_COUNT (data
) [i
],
889 profile_printf (sd
, cpu
, "\n");
894 profile_printf (sd
, cpu
, "\n");
899 #if WITH_PROFILE_CORE_P
902 profile_print_core (sim_cpu
*cpu
, int verbose
)
905 unsigned int max_val
;
906 /* FIXME: Need to add smp support. */
907 SIM_DESC sd
= CPU_STATE (cpu
);
908 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
911 profile_printf (sd
, cpu
, "CORE Statistics\n\n");
913 /* First pass over data computes various things. */
918 for (map
= 0; map
< nr_maps
; map
++)
920 total
+= PROFILE_CORE_COUNT (data
) [map
];
921 if (PROFILE_CORE_COUNT (data
) [map
] > max_val
)
922 max_val
= PROFILE_CORE_COUNT (data
) [map
];
926 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
927 profile_printf (sd
, cpu
, " Total: %s accesses\n",
930 if (verbose
&& max_val
!= 0)
933 /* Now we can print the histogram. */
934 profile_printf (sd
, cpu
, "\n");
935 for (map
= 0; map
< nr_maps
; map
++)
937 if (PROFILE_CORE_COUNT (data
) [map
] != 0)
939 profile_printf (sd
, cpu
, "%10s:", map_to_str (map
));
940 profile_printf (sd
, cpu
, "%*s: ",
941 max_val
< 10000 ? 5 : 10,
942 COMMAS (PROFILE_CORE_COUNT (data
) [map
]));
943 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
944 PROFILE_CORE_COUNT (data
) [map
],
946 profile_printf (sd
, cpu
, "\n");
951 profile_printf (sd
, cpu
, "\n");
956 #if WITH_PROFILE_MODEL_P
959 profile_print_model (sim_cpu
*cpu
, int verbose
)
961 SIM_DESC sd
= CPU_STATE (cpu
);
962 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
963 unsigned long cti_stall_cycles
= PROFILE_MODEL_CTI_STALL_CYCLES (data
);
964 unsigned long load_stall_cycles
= PROFILE_MODEL_LOAD_STALL_CYCLES (data
);
965 unsigned long total_cycles
= PROFILE_MODEL_TOTAL_CYCLES (data
);
968 profile_printf (sd
, cpu
, "Model %s Timing Information",
969 MODEL_NAME (CPU_MODEL (cpu
)));
970 #ifdef SIM_HAVE_ADDR_RANGE
971 if (PROFILE_RANGE (data
)->ranges
)
972 profile_printf (sd
, cpu
, " (for selected address range(s))");
974 profile_printf (sd
, cpu
, "\n\n");
975 profile_printf (sd
, cpu
, " %-*s %s\n",
976 PROFILE_LABEL_WIDTH
, "Taken branches:",
977 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data
)));
978 profile_printf (sd
, cpu
, " %-*s %s\n",
979 PROFILE_LABEL_WIDTH
, "Untaken branches:",
980 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data
)));
981 profile_printf (sd
, cpu
, " %-*s %s\n",
982 PROFILE_LABEL_WIDTH
, "Cycles stalled due to branches:",
983 COMMAS (cti_stall_cycles
));
984 profile_printf (sd
, cpu
, " %-*s %s\n",
985 PROFILE_LABEL_WIDTH
, "Cycles stalled due to loads:",
986 COMMAS (load_stall_cycles
));
987 profile_printf (sd
, cpu
, " %-*s %s\n",
988 PROFILE_LABEL_WIDTH
, "Total cycles (*approximate*):",
989 COMMAS (total_cycles
));
990 profile_printf (sd
, cpu
, "\n");
996 sim_profile_print_bar (SIM_DESC sd
, sim_cpu
*cpu
, unsigned int width
,
997 unsigned int val
, unsigned int max_val
)
999 unsigned int i
, count
;
1001 count
= ((double) val
/ (double) max_val
) * (double) width
;
1003 for (i
= 0; i
< count
; ++i
)
1004 profile_printf (sd
, cpu
, "*");
1007 /* Print the simulator's execution speed for CPU. */
1010 profile_print_speed (sim_cpu
*cpu
)
1012 SIM_DESC sd
= CPU_STATE (cpu
);
1013 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1014 unsigned long milliseconds
= sim_events_elapsed_time (sd
);
1015 unsigned long total
= PROFILE_TOTAL_INSN_COUNT (data
);
1020 profile_printf (sd
, cpu
, "Simulator Execution Speed\n\n");
1023 profile_printf (sd
, cpu
, " Total instructions: %s\n", COMMAS (total
));
1025 if (milliseconds
< 1000)
1026 profile_printf (sd
, cpu
, " Total execution time: < 1 second\n\n");
1029 /* The printing of the time rounded to 2 decimal places makes the speed
1030 calculation seem incorrect [even though it is correct]. So round
1031 MILLISECONDS first. This can marginally affect the result, but it's
1032 better that the user not perceive there's a math error. */
1033 secs
= (double) milliseconds
/ 1000;
1034 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1035 profile_printf (sd
, cpu
, " Total execution time : %.2f seconds\n", secs
);
1036 /* Don't confuse things with data that isn't useful.
1037 If we ran for less than 2 seconds, only use the data if we
1038 executed more than 100,000 insns. */
1039 if (secs
>= 2 || total
>= 100000)
1040 profile_printf (sd
, cpu
, " Simulator speed: %s insns/second\n",
1041 COMMAS ((unsigned long) ((double) total
/ secs
)));
1044 /* Print simulated execution time if the cpu frequency has been specified. */
1045 clock
= PROFILE_CPU_FREQ (data
);
1048 if (clock
>= 1000000)
1049 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f MHz\n",
1052 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f Hz\n", clock
);
1054 #if WITH_PROFILE_MODEL_P
1055 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1057 /* The printing of the time rounded to 2 decimal places makes the
1058 speed calculation seem incorrect [even though it is correct].
1059 So round SECS first. This can marginally affect the result,
1060 but it's better that the user not perceive there's a math
1062 secs
= PROFILE_MODEL_TOTAL_CYCLES (data
) / clock
;
1063 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1064 profile_printf (sd
, cpu
, " Simulated execution time: %.2f seconds\n",
1067 #endif /* WITH_PROFILE_MODEL_P */
1071 #ifdef SIM_HAVE_ADDR_RANGE
1072 /* Print selected address ranges. */
1075 profile_print_addr_ranges (sim_cpu
*cpu
)
1077 ADDR_SUBRANGE
*asr
= PROFILE_RANGE (CPU_PROFILE_DATA (cpu
))->ranges
;
1078 SIM_DESC sd
= CPU_STATE (cpu
);
1082 profile_printf (sd
, cpu
, "Selected address ranges\n\n");
1085 profile_printf (sd
, cpu
, " 0x%lx - 0x%lx\n",
1086 (long) asr
->start
, (long) asr
->end
);
1089 profile_printf (sd
, cpu
, "\n");
1094 /* Top level function to print all summary profile information.
1095 It is [currently] intended that all such data is printed by this function.
1096 I'd rather keep it all in one place for now. To that end, MISC_CPU and
1097 MISC are callbacks used to print any miscellaneous data.
1099 One might want to add a user option that allows printing by type or by cpu
1100 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
1101 This may be a case of featuritis so it's currently left out.
1103 Note that results are indented two spaces to distinguish them from
1107 profile_info (SIM_DESC sd
, int verbose
)
1110 int print_title_p
= 0;
1112 /* Only print the title if some data has been collected. */
1113 /* ??? Why don't we just exit if no data collected? */
1114 /* FIXME: If the number of processors can be selected on the command line,
1115 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
1117 for (c
= 0; c
< MAX_NR_PROCESSORS
&& !print_title_p
; ++c
)
1119 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1120 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1122 for (i
= 0; i
< MAX_PROFILE_VALUES
; ++i
)
1123 if (PROFILE_FLAGS (data
) [i
])
1125 profile_printf (sd
, cpu
, "Summary profiling results:\n\n");
1131 /* Loop, cpu by cpu, printing results. */
1133 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
1135 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1136 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1138 if (MAX_NR_PROCESSORS
> 1
1140 #if WITH_PROFILE_INSN_P
1141 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
]
1143 #if WITH_PROFILE_MEMORY_P
1144 || PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
]
1146 #if WITH_PROFILE_CORE_P
1147 || PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
]
1149 #if WITH_PROFILE_MODEL_P
1150 || PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
]
1152 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1153 || PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
]
1155 #if WITH_PROFILE_PC_P
1156 || PROFILE_FLAGS (data
) [PROFILE_PC_IDX
]
1160 profile_printf (sd
, cpu
, "CPU %d\n\n", c
);
1163 #ifdef SIM_HAVE_ADDR_RANGE
1165 && (PROFILE_INSN_P (cpu
)
1166 || PROFILE_MODEL_P (cpu
)))
1167 profile_print_addr_ranges (cpu
);
1170 #if WITH_PROFILE_INSN_P
1171 if (PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1172 profile_print_insn (cpu
, verbose
);
1175 #if WITH_PROFILE_MEMORY_P
1176 if (PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
])
1177 profile_print_memory (cpu
, verbose
);
1180 #if WITH_PROFILE_CORE_P
1181 if (PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
])
1182 profile_print_core (cpu
, verbose
);
1185 #if WITH_PROFILE_MODEL_P
1186 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1187 profile_print_model (cpu
, verbose
);
1190 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1191 if (PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
])
1192 scache_print_profile (cpu
, verbose
);
1195 #if WITH_PROFILE_PC_P
1196 if (PROFILE_FLAGS (data
) [PROFILE_PC_IDX
])
1197 profile_print_pc (cpu
, verbose
);
1200 /* Print cpu-specific data before the execution speed. */
1201 if (PROFILE_INFO_CPU_CALLBACK (data
) != NULL
)
1202 PROFILE_INFO_CPU_CALLBACK (data
) (cpu
, verbose
);
1204 /* Always try to print execution time and speed. */
1206 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1207 profile_print_speed (cpu
);
1210 /* Finally print non-cpu specific miscellaneous data. */
1211 if (STATE_PROFILE_INFO_CALLBACK (sd
))
1212 STATE_PROFILE_INFO_CALLBACK (sd
) (sd
, verbose
);
1216 /* Install profiling support in the simulator. */
1219 profile_install (SIM_DESC sd
)
1223 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1224 sim_add_option_table (sd
, NULL
, profile_options
);
1225 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1226 memset (CPU_PROFILE_DATA (STATE_CPU (sd
, i
)), 0,
1227 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd
, i
))));
1228 #if WITH_PROFILE_INSN_P
1229 sim_module_add_init_fn (sd
, profile_insn_init
);
1231 #if WITH_PROFILE_PC_P
1232 sim_module_add_uninstall_fn (sd
, profile_pc_uninstall
);
1233 sim_module_add_init_fn (sd
, profile_pc_init
);
1235 sim_module_add_init_fn (sd
, profile_init
);
1236 sim_module_add_uninstall_fn (sd
, profile_uninstall
);
1237 sim_module_add_info_fn (sd
, profile_info
);
1242 profile_init (SIM_DESC sd
)
1244 #ifdef SIM_HAVE_ADDR_RANGE
1245 /* Check if a range has been specified without specifying what to
1250 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1252 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1254 if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)))
1255 && ! (PROFILE_INSN_P (cpu
)
1256 || PROFILE_MODEL_P (cpu
)))
1258 sim_io_eprintf_cpu (cpu
, "Profiling address range specified without --profile-insn or --profile-model.\n");
1259 sim_io_eprintf_cpu (cpu
, "Address range ignored.\n");
1260 sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
1261 0, ~ (address_word
) 0);
1271 profile_uninstall (SIM_DESC sd
)
1275 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1277 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1278 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1280 if (PROFILE_FILE (data
) != NULL
)
1282 /* If output from different cpus is going to the same file,
1283 avoid closing the file twice. */
1284 for (j
= 0; j
< i
; ++j
)
1285 if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, j
)))
1286 == PROFILE_FILE (data
))
1289 fclose (PROFILE_FILE (data
));
1292 if (PROFILE_INSN_COUNT (data
) != NULL
)
1293 free (PROFILE_INSN_COUNT (data
));