1 /* Default profiling support.
2 Copyright (C) 1996-2024 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. */
32 #include "sim-options.h"
33 #include "sim-assert.h"
35 #if !WITH_PROFILE_PC_P
36 static unsigned int _profile_stub
;
37 # define PROFILE_PC_FREQ(p) _profile_stub
38 # define PROFILE_PC_NR_BUCKETS(p) _profile_stub
39 # define PROFILE_PC_SHIFT(p) _profile_stub
40 # define PROFILE_PC_START(p) _profile_stub
41 # define PROFILE_PC_END(p) _profile_stub
42 # define PROFILE_INSN_COUNT(p) &_profile_stub
45 #define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n))
47 static MODULE_INIT_FN profile_init
;
48 static MODULE_UNINSTALL_FN profile_uninstall
;
50 static DECLARE_OPTION_HANDLER (profile_option_handler
);
53 OPTION_PROFILE_INSN
= OPTION_START
,
54 OPTION_PROFILE_MEMORY
,
58 OPTION_PROFILE_CPU_FREQUENCY
,
60 OPTION_PROFILE_PC_RANGE
,
61 OPTION_PROFILE_PC_GRANULARITY
,
63 OPTION_PROFILE_FUNCTION
66 static const OPTION profile_options
[] = {
67 { {"profile", optional_argument
, NULL
, 'p'},
68 'p', "on|off", "Perform profiling",
69 profile_option_handler
, NULL
},
70 { {"profile-insn", optional_argument
, NULL
, OPTION_PROFILE_INSN
},
71 '\0', "on|off", "Perform instruction profiling",
72 profile_option_handler
, NULL
},
73 { {"profile-memory", optional_argument
, NULL
, OPTION_PROFILE_MEMORY
},
74 '\0', "on|off", "Perform memory profiling",
75 profile_option_handler
, NULL
},
76 { {"profile-core", optional_argument
, NULL
, OPTION_PROFILE_CORE
},
77 '\0', "on|off", "Perform CORE profiling",
78 profile_option_handler
, NULL
},
79 { {"profile-model", optional_argument
, NULL
, OPTION_PROFILE_MODEL
},
80 '\0', "on|off", "Perform model profiling",
81 profile_option_handler
, NULL
},
82 { {"profile-cpu-frequency", required_argument
, NULL
,
83 OPTION_PROFILE_CPU_FREQUENCY
},
84 '\0', "CPU FREQUENCY", "Specify the speed of the simulated cpu clock",
85 profile_option_handler
, NULL
},
87 { {"profile-file", required_argument
, NULL
, OPTION_PROFILE_FILE
},
88 '\0', "FILE NAME", "Specify profile output file",
89 profile_option_handler
, NULL
},
91 { {"profile-pc", optional_argument
, NULL
, OPTION_PROFILE_PC
},
92 '\0', "on|off", "Perform PC profiling",
93 profile_option_handler
, NULL
},
94 { {"profile-pc-frequency", required_argument
, NULL
, 'F'},
95 'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency",
96 profile_option_handler
, NULL
},
97 { {"profile-pc-size", required_argument
, NULL
, 'S'},
98 'S', "PC PROFILE SIZE", "Specify PC profiling size",
99 profile_option_handler
, NULL
},
100 { {"profile-pc-granularity", required_argument
, NULL
, OPTION_PROFILE_PC_GRANULARITY
},
101 '\0', "PC PROFILE GRANULARITY", "Specify PC profiling sample coverage",
102 profile_option_handler
, NULL
},
103 { {"profile-pc-range", required_argument
, NULL
, OPTION_PROFILE_PC_RANGE
},
104 '\0', "BASE,BOUND", "Specify PC profiling address range",
105 profile_option_handler
, NULL
},
107 #ifdef SIM_HAVE_ADDR_RANGE
108 { {"profile-range", required_argument
, NULL
, OPTION_PROFILE_RANGE
},
109 '\0', "START,END", "Specify range of addresses for instruction and model profiling",
110 profile_option_handler
, NULL
},
112 { {"profile-function", required_argument
, NULL
, OPTION_PROFILE_FUNCTION
},
113 '\0', "FUNCTION", "Specify function to profile",
114 profile_option_handler
, NULL
},
118 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
121 /* Set/reset the profile options indicated in MASK. */
124 set_profile_option_mask (SIM_DESC sd
, const char *name
, int mask
, const char *arg
)
132 if (strcmp (arg
, "yes") == 0
133 || strcmp (arg
, "on") == 0
134 || strcmp (arg
, "1") == 0)
136 else if (strcmp (arg
, "no") == 0
137 || strcmp (arg
, "off") == 0
138 || strcmp (arg
, "0") == 0)
142 sim_io_eprintf (sd
, "Argument `%s' for `--profile%s' invalid, one of `on', `off', `yes', `no' expected\n", arg
, name
);
147 /* update applicable profile bits */
148 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
150 if ((mask
& (1 << profile_nr
)) == 0)
153 #if 0 /* see sim-trace.c, set flags in STATE here if/when there are any */
154 /* Set non-cpu specific values. */
162 /* Set cpu values. */
163 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
165 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
] = profile_val
;
169 /* Re-compute the cpu profile summary. */
172 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
173 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
177 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
179 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 0;
180 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
182 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
])
184 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
194 /* Set one profile option based on its IDX value.
195 Not static as cgen-scache.c uses it. */
198 sim_profile_set_option (SIM_DESC sd
, const char *name
, int idx
, const char *arg
)
200 return set_profile_option_mask (sd
, name
, 1 << idx
, arg
);
204 parse_frequency (SIM_DESC sd
, const char *arg
, unsigned long *freq
)
207 /* First, parse a decimal number. */
212 for (/**/; *ch
!= '\0'; ++ch
)
216 *freq
= *freq
* 10 + (*ch
- '0');
219 /* Accept KHz, MHz or Hz as a suffix. */
220 if (tolower (*ch
) == 'm')
225 else if (tolower (*ch
) == 'k')
231 if (tolower (*ch
) == 'h')
234 if (tolower (*ch
) == 'z')
241 sim_io_eprintf (sd
, "Invalid argument for --profile-cpu-frequency: %s\n",
251 profile_option_handler (SIM_DESC sd
,
259 /* FIXME: Need to handle `cpu' arg. */
265 sim_io_eprintf (sd
, "Profiling not compiled in, `-p' ignored\n");
267 return set_profile_option_mask (sd
, "profile", PROFILE_USEFUL_MASK
,
271 case OPTION_PROFILE_INSN
:
272 if (WITH_PROFILE_INSN_P
)
273 return sim_profile_set_option (sd
, "-insn", PROFILE_INSN_IDX
, arg
);
275 sim_io_eprintf (sd
, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
278 case OPTION_PROFILE_MEMORY
:
279 if (WITH_PROFILE_MEMORY_P
)
280 return sim_profile_set_option (sd
, "-memory", PROFILE_MEMORY_IDX
, arg
);
282 sim_io_eprintf (sd
, "Memory profiling not compiled in, `--profile-memory' ignored\n");
285 case OPTION_PROFILE_CORE
:
286 if (WITH_PROFILE_CORE_P
)
287 return sim_profile_set_option (sd
, "-core", PROFILE_CORE_IDX
, arg
);
289 sim_io_eprintf (sd
, "CORE profiling not compiled in, `--profile-core' ignored\n");
292 case OPTION_PROFILE_MODEL
:
293 if (WITH_PROFILE_MODEL_P
)
294 return sim_profile_set_option (sd
, "-model", PROFILE_MODEL_IDX
, arg
);
296 sim_io_eprintf (sd
, "Model profiling not compiled in, `--profile-model' ignored\n");
299 case OPTION_PROFILE_CPU_FREQUENCY
:
302 SIM_RC rc
= parse_frequency (sd
, arg
, &val
);
305 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
306 PROFILE_CPU_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
,cpu_nr
))) = val
;
311 case OPTION_PROFILE_FILE
:
312 /* FIXME: Might want this to apply to pc profiling only,
313 or have two profile file options. */
315 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-file' ignored\n");
318 FILE *f
= fopen (arg
, "w");
322 sim_io_eprintf (sd
, "Unable to open profile output file `%s'\n", arg
);
325 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
326 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = f
;
330 case OPTION_PROFILE_PC
:
331 if (WITH_PROFILE_PC_P
)
332 return sim_profile_set_option (sd
, "-pc", PROFILE_PC_IDX
, arg
);
334 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc' ignored\n");
338 if (WITH_PROFILE_PC_P
)
340 /* FIXME: Validate arg. */
341 int val
= atoi (arg
);
342 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
343 PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
344 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
345 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
348 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
352 if (WITH_PROFILE_PC_P
)
354 /* FIXME: Validate arg. */
355 int val
= atoi (arg
);
356 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
357 PROFILE_PC_NR_BUCKETS (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
358 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
359 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
362 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
365 case OPTION_PROFILE_PC_GRANULARITY
:
366 if (WITH_PROFILE_PC_P
)
369 int val
= atoi (arg
);
370 /* check that the granularity is a power of two */
372 while (val
> (1 << shift
))
376 if (val
!= (1 << shift
))
378 sim_io_eprintf (sd
, "PC profiling granularity not a power of two\n");
383 sim_io_eprintf (sd
, "PC profiling granularity too small");
386 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
387 PROFILE_PC_SHIFT (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = shift
;
388 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
389 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
392 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
395 case OPTION_PROFILE_PC_RANGE
:
396 if (WITH_PROFILE_PC_P
)
398 /* FIXME: Validate args */
402 base
= strtoul (chp
, &chp
, 0);
405 sim_io_eprintf (sd
, "--profile-pc-range missing BOUND argument\n");
408 bound
= strtoul (chp
+ 1, NULL
, 0);
409 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
411 PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = base
;
412 PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = bound
;
414 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
415 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
418 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
421 #ifdef SIM_HAVE_ADDR_RANGE
422 case OPTION_PROFILE_RANGE
:
426 unsigned long start
,end
;
427 start
= strtoul (chp
, &chp
, 0);
430 sim_io_eprintf (sd
, "--profile-range missing END argument\n");
433 end
= strtoul (chp
+ 1, NULL
, 0);
434 /* FIXME: Argument validation. */
436 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
439 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
440 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))),
444 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-range' ignored\n");
447 case OPTION_PROFILE_FUNCTION
:
450 /*wip: need to compute function range given name*/
453 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-function' ignored\n");
455 #endif /* SIM_HAVE_ADDR_RANGE */
461 /* Profiling output hooks. */
463 static void ATTRIBUTE_PRINTF (3, 0)
464 profile_vprintf (SIM_DESC sd
, sim_cpu
*cpu
, const char *fmt
, va_list ap
)
466 FILE *fp
= PROFILE_FILE (CPU_PROFILE_DATA (cpu
));
468 /* If an output file was given, redirect output to that. */
470 vfprintf (fp
, fmt
, ap
);
472 sim_io_evprintf (sd
, fmt
, ap
);
475 ATTRIBUTE_PRINTF (3, 4)
477 profile_printf (SIM_DESC sd
, sim_cpu
*cpu
, const char *fmt
, ...)
482 profile_vprintf (sd
, cpu
, fmt
, ap
);
486 /* PC profiling support */
488 #if WITH_PROFILE_PC_P
491 profile_pc_cleanup (SIM_DESC sd
)
494 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
496 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
497 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
498 if (PROFILE_PC_COUNT (data
) != NULL
)
499 free (PROFILE_PC_COUNT (data
));
500 PROFILE_PC_COUNT (data
) = NULL
;
501 if (PROFILE_PC_EVENT (data
) != NULL
)
502 sim_events_deschedule (sd
, PROFILE_PC_EVENT (data
));
503 PROFILE_PC_EVENT (data
) = NULL
;
509 profile_pc_uninstall (SIM_DESC sd
)
511 profile_pc_cleanup (sd
);
515 profile_pc_event (SIM_DESC sd
,
518 sim_cpu
*cpu
= (sim_cpu
*) data
;
519 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
520 address_word pc
= sim_pc_get (cpu
);
522 i
= (pc
- PROFILE_PC_START (profile
)) >> PROFILE_PC_SHIFT (profile
);
523 if (i
< PROFILE_PC_NR_BUCKETS (profile
))
524 PROFILE_PC_COUNT (profile
) [i
] += 1; /* Overflow? */
526 PROFILE_PC_COUNT (profile
) [PROFILE_PC_NR_BUCKETS (profile
)] += 1;
527 PROFILE_PC_EVENT (profile
) =
528 sim_events_schedule (sd
, PROFILE_PC_FREQ (profile
), profile_pc_event
, cpu
);
532 profile_pc_init (SIM_DESC sd
)
535 profile_pc_cleanup (sd
);
536 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
538 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
539 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
540 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
])
543 /* fill in the frequency if not specified */
544 if (PROFILE_PC_FREQ (data
) == 0)
545 PROFILE_PC_FREQ (data
) = 257;
546 /* fill in the start/end if not specified */
547 if (PROFILE_PC_END (data
) == 0)
549 PROFILE_PC_START (data
) = STATE_TEXT_START (sd
);
550 PROFILE_PC_END (data
) = STATE_TEXT_END (sd
);
552 /* Compute the number of buckets if not specified. */
553 if (PROFILE_PC_NR_BUCKETS (data
) == 0)
555 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
556 PROFILE_PC_NR_BUCKETS (data
) = 16;
559 if (PROFILE_PC_END (data
) == 0)
561 /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
562 PROFILE_PC_NR_BUCKETS (data
) =
563 ((1ULL << sizeof (sim_cia
) * (8 - 1))
564 / (PROFILE_PC_BUCKET_SIZE (data
) / 2));
568 PROFILE_PC_NR_BUCKETS (data
) =
569 ((PROFILE_PC_END (data
)
570 - PROFILE_PC_START (data
)
571 + PROFILE_PC_BUCKET_SIZE (data
) - 1)
572 / PROFILE_PC_BUCKET_SIZE (data
));
576 /* Compute the bucket size if not specified. Ensure that it
577 is rounded up to the next power of two */
578 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
580 if (PROFILE_PC_END (data
) == 0)
581 /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
582 bucket_size
= ((1ULL << ((sizeof (sim_cia
) * 8) - 1))
583 / (PROFILE_PC_NR_BUCKETS (data
) / 2));
585 bucket_size
= ((PROFILE_PC_END (data
)
586 - PROFILE_PC_START (data
)
587 + PROFILE_PC_NR_BUCKETS (data
) - 1)
588 / PROFILE_PC_NR_BUCKETS (data
));
589 PROFILE_PC_SHIFT (data
) = 0;
590 while (bucket_size
> PROFILE_PC_BUCKET_SIZE (data
))
592 PROFILE_PC_SHIFT (data
) += 1;
595 /* Align the end address with bucket size */
596 if (PROFILE_PC_END (data
) != 0)
597 PROFILE_PC_END (data
) = (PROFILE_PC_START (data
)
598 + (PROFILE_PC_BUCKET_SIZE (data
)
599 * PROFILE_PC_NR_BUCKETS (data
)));
600 /* create the relevant buffers */
601 PROFILE_PC_COUNT (data
) =
602 NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data
) + 1);
603 PROFILE_PC_EVENT (data
) =
604 sim_events_schedule (sd
,
605 PROFILE_PC_FREQ (data
),
614 profile_print_pc (sim_cpu
*cpu
, bool verbose
)
616 SIM_DESC sd
= CPU_STATE (cpu
);
617 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
623 if (PROFILE_PC_COUNT (profile
) == 0)
626 profile_printf (sd
, cpu
, "Program Counter Statistics:\n\n");
628 /* First pass over data computes various things. */
631 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
633 total
+= PROFILE_PC_COUNT (profile
) [i
];
634 if (PROFILE_PC_COUNT (profile
) [i
] > max_val
)
635 max_val
= PROFILE_PC_COUNT (profile
) [i
];
638 profile_printf (sd
, cpu
, " Total samples: %s\n",
640 profile_printf (sd
, cpu
, " Granularity: %s bytes per bucket\n",
641 COMMAS (PROFILE_PC_BUCKET_SIZE (profile
)));
642 profile_printf (sd
, cpu
, " Size: %s buckets\n",
643 COMMAS (PROFILE_PC_NR_BUCKETS (profile
)));
644 profile_printf (sd
, cpu
, " Frequency: %s cycles per sample\n",
645 COMMAS (PROFILE_PC_FREQ (profile
)));
647 if (PROFILE_PC_END (profile
) != 0)
648 profile_printf (sd
, cpu
, " Range: 0x%lx 0x%lx\n",
649 (long) PROFILE_PC_START (profile
),
650 (long) PROFILE_PC_END (profile
));
652 if (verbose
&& max_val
!= 0)
654 /* Now we can print the histogram. */
655 profile_printf (sd
, cpu
, "\n");
656 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
658 if (PROFILE_PC_COUNT (profile
) [i
] != 0)
660 profile_printf (sd
, cpu
, " ");
661 if (i
== PROFILE_PC_NR_BUCKETS (profile
))
662 profile_printf (sd
, cpu
, "%10s:", "overflow");
664 profile_printf (sd
, cpu
, "0x%08lx:",
665 (long) (PROFILE_PC_START (profile
)
666 + (i
* PROFILE_PC_BUCKET_SIZE (profile
))));
667 profile_printf (sd
, cpu
, " %*s",
668 max_val
< 10000 ? 5 : 10,
669 COMMAS (PROFILE_PC_COUNT (profile
) [i
]));
670 profile_printf (sd
, cpu
, " %4.1f",
671 (PROFILE_PC_COUNT (profile
) [i
] * 100.0) / total
);
672 profile_printf (sd
, cpu
, ": ");
673 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
674 PROFILE_PC_COUNT (profile
) [i
],
676 profile_printf (sd
, cpu
, "\n");
681 /* dump the histogram to the file "gmon.out" using BSD's gprof file
683 /* Since a profile data file is in the native format of the host on
684 which the profile is being, endian issues are not considered in
686 /* FIXME: Is this the best place for this code? */
688 FILE *pf
= fopen ("gmon.out", "wb");
691 sim_io_eprintf (sd
, "Failed to open \"gmon.out\" profile file\n");
695 /* FIXME: what if the target has a 64 bit PC? */
698 if (PROFILE_PC_END (profile
) != 0)
700 header
[0] = PROFILE_PC_START (profile
);
701 header
[1] = PROFILE_PC_END (profile
);
708 /* size of sample buffer (+ header) */
709 header
[2] = PROFILE_PC_NR_BUCKETS (profile
) * 2 + sizeof (header
);
711 /* Header must be written out in target byte order. */
716 ok
= fwrite (&header
, sizeof (header
), 1, pf
);
718 ok
&& (loop
< PROFILE_PC_NR_BUCKETS (profile
));
722 if (PROFILE_PC_COUNT (profile
) [loop
] >= 0xffff)
725 sample
= PROFILE_PC_COUNT (profile
) [loop
];
727 ok
= fwrite (&sample
, sizeof (sample
), 1, pf
);
730 sim_io_eprintf (sd
, "Failed to write to \"gmon.out\" profile file\n");
735 profile_printf (sd
, cpu
, "\n");
740 /* Summary printing support. */
742 #if WITH_PROFILE_INSN_P
745 profile_insn_init (SIM_DESC sd
)
749 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
751 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
753 if (CPU_MAX_INSNS (cpu
) > 0)
754 PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu
)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu
));
761 profile_print_insn (sim_cpu
*cpu
, bool verbose
)
763 unsigned int i
, n
, total
, max_val
, max_name_len
;
764 SIM_DESC sd
= CPU_STATE (cpu
);
765 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
768 /* If MAX_INSNS not set, insn profiling isn't supported. */
769 if (CPU_MAX_INSNS (cpu
) == 0)
772 profile_printf (sd
, cpu
, "Instruction Statistics");
773 #ifdef SIM_HAVE_ADDR_RANGE
774 if (PROFILE_RANGE (data
)->ranges
)
775 profile_printf (sd
, cpu
, " (for selected address range(s))");
777 profile_printf (sd
, cpu
, "\n\n");
779 /* First pass over data computes various things. */
783 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
785 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
789 total
+= PROFILE_INSN_COUNT (data
) [i
];
790 if (PROFILE_INSN_COUNT (data
) [i
] > max_val
)
791 max_val
= PROFILE_INSN_COUNT (data
) [i
];
793 if (n
> max_name_len
)
796 /* set the total insn count, in case client is being lazy */
797 if (! PROFILE_TOTAL_INSN_COUNT (data
))
798 PROFILE_TOTAL_INSN_COUNT (data
) = total
;
800 profile_printf (sd
, cpu
, " Total: %s insns\n", COMMAS (total
));
802 if (verbose
&& max_val
!= 0)
804 /* Now we can print the histogram. */
805 profile_printf (sd
, cpu
, "\n");
806 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
808 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
812 if (PROFILE_INSN_COUNT (data
) [i
] != 0)
814 profile_printf (sd
, cpu
, " %*s: %*s: ",
816 max_val
< 10000 ? 5 : 10,
817 COMMAS (PROFILE_INSN_COUNT (data
) [i
]));
818 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
819 PROFILE_INSN_COUNT (data
) [i
],
821 profile_printf (sd
, cpu
, "\n");
826 profile_printf (sd
, cpu
, "\n");
831 #if WITH_PROFILE_MEMORY_P
834 profile_print_memory (sim_cpu
*cpu
, bool verbose
)
837 unsigned int total_read
, total_write
;
838 unsigned int max_val
, max_name_len
;
839 /* FIXME: Need to add smp support. */
840 SIM_DESC sd
= CPU_STATE (cpu
);
841 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
844 profile_printf (sd
, cpu
, "Memory Access Statistics\n\n");
846 /* First pass over data computes various things. */
847 max_val
= total_read
= total_write
= max_name_len
= 0;
848 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
850 total_read
+= PROFILE_READ_COUNT (data
) [i
];
851 total_write
+= PROFILE_WRITE_COUNT (data
) [i
];
852 if (PROFILE_READ_COUNT (data
) [i
] > max_val
)
853 max_val
= PROFILE_READ_COUNT (data
) [i
];
854 if (PROFILE_WRITE_COUNT (data
) [i
] > max_val
)
855 max_val
= PROFILE_WRITE_COUNT (data
) [i
];
856 n
= strlen (MODE_NAME (i
));
857 if (n
> max_name_len
)
861 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
862 profile_printf (sd
, cpu
, " Total read: %s accesses\n",
863 COMMAS (total_read
));
864 profile_printf (sd
, cpu
, " Total write: %s accesses\n",
865 COMMAS (total_write
));
867 if (verbose
&& max_val
!= 0)
869 /* FIXME: Need to separate instruction fetches from data fetches
870 as the former swamps the latter. */
871 /* Now we can print the histogram. */
872 profile_printf (sd
, cpu
, "\n");
873 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
875 if (PROFILE_READ_COUNT (data
) [i
] != 0)
877 profile_printf (sd
, cpu
, " %*s read: %*s: ",
878 max_name_len
, MODE_NAME (i
),
879 max_val
< 10000 ? 5 : 10,
880 COMMAS (PROFILE_READ_COUNT (data
) [i
]));
881 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
882 PROFILE_READ_COUNT (data
) [i
],
884 profile_printf (sd
, cpu
, "\n");
886 if (PROFILE_WRITE_COUNT (data
) [i
] != 0)
888 profile_printf (sd
, cpu
, " %*s write: %*s: ",
889 max_name_len
, MODE_NAME (i
),
890 max_val
< 10000 ? 5 : 10,
891 COMMAS (PROFILE_WRITE_COUNT (data
) [i
]));
892 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
893 PROFILE_WRITE_COUNT (data
) [i
],
895 profile_printf (sd
, cpu
, "\n");
900 profile_printf (sd
, cpu
, "\n");
905 #if WITH_PROFILE_CORE_P
908 profile_print_core (sim_cpu
*cpu
, bool verbose
)
911 unsigned int max_val
;
912 /* FIXME: Need to add smp support. */
913 SIM_DESC sd
= CPU_STATE (cpu
);
914 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
917 profile_printf (sd
, cpu
, "CORE Statistics\n\n");
919 /* First pass over data computes various things. */
924 for (map
= 0; map
< nr_maps
; map
++)
926 total
+= PROFILE_CORE_COUNT (data
) [map
];
927 if (PROFILE_CORE_COUNT (data
) [map
] > max_val
)
928 max_val
= PROFILE_CORE_COUNT (data
) [map
];
932 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
933 profile_printf (sd
, cpu
, " Total: %s accesses\n",
936 if (verbose
&& max_val
!= 0)
939 /* Now we can print the histogram. */
940 profile_printf (sd
, cpu
, "\n");
941 for (map
= 0; map
< nr_maps
; map
++)
943 if (PROFILE_CORE_COUNT (data
) [map
] != 0)
945 profile_printf (sd
, cpu
, "%10s:", map_to_str (map
));
946 profile_printf (sd
, cpu
, "%*s: ",
947 max_val
< 10000 ? 5 : 10,
948 COMMAS (PROFILE_CORE_COUNT (data
) [map
]));
949 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
950 PROFILE_CORE_COUNT (data
) [map
],
952 profile_printf (sd
, cpu
, "\n");
957 profile_printf (sd
, cpu
, "\n");
962 #if WITH_PROFILE_MODEL_P
965 profile_print_model (sim_cpu
*cpu
, bool verbose
)
967 SIM_DESC sd
= CPU_STATE (cpu
);
968 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
969 unsigned long cti_stall_cycles
= PROFILE_MODEL_CTI_STALL_CYCLES (data
);
970 unsigned long load_stall_cycles
= PROFILE_MODEL_LOAD_STALL_CYCLES (data
);
971 unsigned long total_cycles
= PROFILE_MODEL_TOTAL_CYCLES (data
);
974 profile_printf (sd
, cpu
, "Model %s Timing Information",
975 MODEL_NAME (CPU_MODEL (cpu
)));
976 #ifdef SIM_HAVE_ADDR_RANGE
977 if (PROFILE_RANGE (data
)->ranges
)
978 profile_printf (sd
, cpu
, " (for selected address range(s))");
980 profile_printf (sd
, cpu
, "\n\n");
981 profile_printf (sd
, cpu
, " %-*s %s\n",
982 PROFILE_LABEL_WIDTH
, "Taken branches:",
983 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data
)));
984 profile_printf (sd
, cpu
, " %-*s %s\n",
985 PROFILE_LABEL_WIDTH
, "Untaken branches:",
986 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data
)));
987 profile_printf (sd
, cpu
, " %-*s %s\n",
988 PROFILE_LABEL_WIDTH
, "Cycles stalled due to branches:",
989 COMMAS (cti_stall_cycles
));
990 profile_printf (sd
, cpu
, " %-*s %s\n",
991 PROFILE_LABEL_WIDTH
, "Cycles stalled due to loads:",
992 COMMAS (load_stall_cycles
));
993 profile_printf (sd
, cpu
, " %-*s %s\n",
994 PROFILE_LABEL_WIDTH
, "Total cycles (*approximate*):",
995 COMMAS (total_cycles
));
996 profile_printf (sd
, cpu
, "\n");
1002 sim_profile_print_bar (SIM_DESC sd
, sim_cpu
*cpu
, unsigned int width
,
1003 unsigned int val
, unsigned int max_val
)
1005 unsigned int i
, count
;
1007 count
= ((double) val
/ (double) max_val
) * (double) width
;
1009 for (i
= 0; i
< count
; ++i
)
1010 profile_printf (sd
, cpu
, "*");
1013 /* Print the simulator's execution speed for CPU. */
1016 profile_print_speed (sim_cpu
*cpu
)
1018 SIM_DESC sd
= CPU_STATE (cpu
);
1019 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1020 unsigned long milliseconds
= sim_events_elapsed_time (sd
);
1021 unsigned long total
= PROFILE_TOTAL_INSN_COUNT (data
);
1026 profile_printf (sd
, cpu
, "Simulator Execution Speed\n\n");
1029 profile_printf (sd
, cpu
, " Total instructions: %s\n", COMMAS (total
));
1031 if (milliseconds
< 1000)
1032 profile_printf (sd
, cpu
, " Total execution time: < 1 second\n\n");
1035 /* The printing of the time rounded to 2 decimal places makes the speed
1036 calculation seem incorrect [even though it is correct]. So round
1037 MILLISECONDS first. This can marginally affect the result, but it's
1038 better that the user not perceive there's a math error. */
1039 secs
= (double) milliseconds
/ 1000;
1040 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1041 profile_printf (sd
, cpu
, " Total execution time : %.2f seconds\n", secs
);
1042 /* Don't confuse things with data that isn't useful.
1043 If we ran for less than 2 seconds, only use the data if we
1044 executed more than 100,000 insns. */
1045 if (secs
>= 2 || total
>= 100000)
1046 profile_printf (sd
, cpu
, " Simulator speed: %s insns/second\n",
1047 COMMAS ((unsigned long) ((double) total
/ secs
)));
1050 /* Print simulated execution time if the cpu frequency has been specified. */
1051 clock
= PROFILE_CPU_FREQ (data
);
1054 if (clock
>= 1000000)
1055 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f MHz\n",
1058 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f Hz\n", clock
);
1060 #if WITH_PROFILE_MODEL_P
1061 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1063 /* The printing of the time rounded to 2 decimal places makes the
1064 speed calculation seem incorrect [even though it is correct].
1065 So round SECS first. This can marginally affect the result,
1066 but it's better that the user not perceive there's a math
1068 secs
= PROFILE_MODEL_TOTAL_CYCLES (data
) / clock
;
1069 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1070 profile_printf (sd
, cpu
, " Simulated execution time: %.2f seconds\n",
1073 #endif /* WITH_PROFILE_MODEL_P */
1077 #ifdef SIM_HAVE_ADDR_RANGE
1078 /* Print selected address ranges. */
1081 profile_print_addr_ranges (sim_cpu
*cpu
)
1083 ADDR_SUBRANGE
*asr
= PROFILE_RANGE (CPU_PROFILE_DATA (cpu
))->ranges
;
1084 SIM_DESC sd
= CPU_STATE (cpu
);
1088 profile_printf (sd
, cpu
, "Selected address ranges\n\n");
1091 profile_printf (sd
, cpu
, " 0x%lx - 0x%lx\n",
1092 (long) asr
->start
, (long) asr
->end
);
1095 profile_printf (sd
, cpu
, "\n");
1100 /* Top level function to print all summary profile information.
1101 It is [currently] intended that all such data is printed by this function.
1102 I'd rather keep it all in one place for now. To that end, MISC_CPU and
1103 MISC are callbacks used to print any miscellaneous data.
1105 One might want to add a user option that allows printing by type or by cpu
1106 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
1107 This may be a case of featuritis so it's currently left out.
1109 Note that results are indented two spaces to distinguish them from
1113 profile_info (SIM_DESC sd
, bool verbose
)
1116 int print_title_p
= 0;
1118 /* Only print the title if some data has been collected. */
1119 /* ??? Why don't we just exit if no data collected? */
1120 /* FIXME: If the number of processors can be selected on the command line,
1121 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
1123 for (c
= 0; c
< MAX_NR_PROCESSORS
&& !print_title_p
; ++c
)
1125 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1126 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1128 for (i
= 0; i
< MAX_PROFILE_VALUES
; ++i
)
1129 if (PROFILE_FLAGS (data
) [i
])
1131 profile_printf (sd
, cpu
, "Summary profiling results:\n\n");
1137 /* Loop, cpu by cpu, printing results. */
1139 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
1141 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1142 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1144 if (MAX_NR_PROCESSORS
> 1
1146 #if WITH_PROFILE_INSN_P
1147 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
]
1149 #if WITH_PROFILE_MEMORY_P
1150 || PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
]
1152 #if WITH_PROFILE_CORE_P
1153 || PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
]
1155 #if WITH_PROFILE_MODEL_P
1156 || PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
]
1158 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE && defined(CGEN_ARCH)
1159 || PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
]
1161 #if WITH_PROFILE_PC_P
1162 || PROFILE_FLAGS (data
) [PROFILE_PC_IDX
]
1166 profile_printf (sd
, cpu
, "CPU %d\n\n", c
);
1169 #ifdef SIM_HAVE_ADDR_RANGE
1171 && (PROFILE_INSN_P (cpu
)
1172 || PROFILE_MODEL_P (cpu
)))
1173 profile_print_addr_ranges (cpu
);
1176 #if WITH_PROFILE_INSN_P
1177 if (PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1178 profile_print_insn (cpu
, verbose
);
1181 #if WITH_PROFILE_MEMORY_P
1182 if (PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
])
1183 profile_print_memory (cpu
, verbose
);
1186 #if WITH_PROFILE_CORE_P
1187 if (PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
])
1188 profile_print_core (cpu
, verbose
);
1191 #if WITH_PROFILE_MODEL_P
1192 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1193 profile_print_model (cpu
, verbose
);
1196 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE && defined(CGEN_ARCH)
1197 if (PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
])
1198 scache_print_profile (cpu
, verbose
);
1201 #if WITH_PROFILE_PC_P
1202 if (PROFILE_FLAGS (data
) [PROFILE_PC_IDX
])
1203 profile_print_pc (cpu
, verbose
);
1206 /* Print cpu-specific data before the execution speed. */
1207 if (PROFILE_INFO_CPU_CALLBACK (data
) != NULL
)
1208 PROFILE_INFO_CPU_CALLBACK (data
) (cpu
, verbose
);
1210 /* Always try to print execution time and speed. */
1212 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1213 profile_print_speed (cpu
);
1216 /* Finally print non-cpu specific miscellaneous data. */
1217 if (STATE_PROFILE_INFO_CALLBACK (sd
))
1218 STATE_PROFILE_INFO_CALLBACK (sd
) (sd
, verbose
);
1222 /* Provide a prototype to silence -Wmissing-prototypes. */
1223 SIM_RC
sim_install_profile (SIM_DESC sd
);
1225 /* Install profiling support in the simulator. */
1227 sim_install_profile (SIM_DESC sd
)
1231 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1232 sim_add_option_table (sd
, NULL
, profile_options
);
1233 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1234 memset (CPU_PROFILE_DATA (STATE_CPU (sd
, i
)), 0,
1235 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd
, i
))));
1236 #if WITH_PROFILE_INSN_P
1237 sim_module_add_init_fn (sd
, profile_insn_init
);
1239 #if WITH_PROFILE_PC_P
1240 sim_module_add_uninstall_fn (sd
, profile_pc_uninstall
);
1241 sim_module_add_init_fn (sd
, profile_pc_init
);
1243 sim_module_add_init_fn (sd
, profile_init
);
1244 sim_module_add_uninstall_fn (sd
, profile_uninstall
);
1245 sim_module_add_info_fn (sd
, profile_info
);
1250 profile_init (SIM_DESC sd
)
1252 #ifdef SIM_HAVE_ADDR_RANGE
1253 /* Check if a range has been specified without specifying what to
1258 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1260 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1262 if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)))
1263 && ! (PROFILE_INSN_P (cpu
)
1264 || PROFILE_MODEL_P (cpu
)))
1266 sim_io_eprintf_cpu (cpu
, "Profiling address range specified without --profile-insn or --profile-model.\n");
1267 sim_io_eprintf_cpu (cpu
, "Address range ignored.\n");
1268 sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
1269 0, ~ (address_word
) 0);
1279 profile_uninstall (SIM_DESC sd
)
1283 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1285 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1286 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1288 if (PROFILE_FILE (data
) != NULL
)
1290 /* If output from different cpus is going to the same file,
1291 avoid closing the file twice. */
1292 for (j
= 0; j
< i
; ++j
)
1293 if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, j
)))
1294 == PROFILE_FILE (data
))
1297 fclose (PROFILE_FILE (data
));
1300 if (PROFILE_INSN_COUNT (data
) != NULL
)
1301 free (PROFILE_INSN_COUNT (data
));