1 /* Default profiling support.
2 Copyright (C) 1996-2018 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"
38 #if !WITH_PROFILE_PC_P
39 static unsigned int _profile_stub
;
40 # define PROFILE_PC_FREQ(p) _profile_stub
41 # define PROFILE_PC_NR_BUCKETS(p) _profile_stub
42 # define PROFILE_PC_SHIFT(p) _profile_stub
43 # define PROFILE_PC_START(p) _profile_stub
44 # define PROFILE_PC_END(p) _profile_stub
45 # define PROFILE_INSN_COUNT(p) &_profile_stub
48 #define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n))
50 static MODULE_INIT_FN profile_init
;
51 static MODULE_UNINSTALL_FN profile_uninstall
;
53 static DECLARE_OPTION_HANDLER (profile_option_handler
);
56 OPTION_PROFILE_INSN
= OPTION_START
,
57 OPTION_PROFILE_MEMORY
,
61 OPTION_PROFILE_CPU_FREQUENCY
,
63 OPTION_PROFILE_PC_RANGE
,
64 OPTION_PROFILE_PC_GRANULARITY
,
66 OPTION_PROFILE_FUNCTION
69 static const OPTION profile_options
[] = {
70 { {"profile", optional_argument
, NULL
, 'p'},
71 'p', "on|off", "Perform profiling",
72 profile_option_handler
, NULL
},
73 { {"profile-insn", optional_argument
, NULL
, OPTION_PROFILE_INSN
},
74 '\0', "on|off", "Perform instruction profiling",
75 profile_option_handler
, NULL
},
76 { {"profile-memory", optional_argument
, NULL
, OPTION_PROFILE_MEMORY
},
77 '\0', "on|off", "Perform memory profiling",
78 profile_option_handler
, NULL
},
79 { {"profile-core", optional_argument
, NULL
, OPTION_PROFILE_CORE
},
80 '\0', "on|off", "Perform CORE profiling",
81 profile_option_handler
, NULL
},
82 { {"profile-model", optional_argument
, NULL
, OPTION_PROFILE_MODEL
},
83 '\0', "on|off", "Perform model profiling",
84 profile_option_handler
, NULL
},
85 { {"profile-cpu-frequency", required_argument
, NULL
,
86 OPTION_PROFILE_CPU_FREQUENCY
},
87 '\0', "CPU FREQUENCY", "Specify the speed of the simulated cpu clock",
88 profile_option_handler
, NULL
},
90 { {"profile-file", required_argument
, NULL
, OPTION_PROFILE_FILE
},
91 '\0', "FILE NAME", "Specify profile output file",
92 profile_option_handler
, NULL
},
94 { {"profile-pc", optional_argument
, NULL
, OPTION_PROFILE_PC
},
95 '\0', "on|off", "Perform PC profiling",
96 profile_option_handler
, NULL
},
97 { {"profile-pc-frequency", required_argument
, NULL
, 'F'},
98 'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency",
99 profile_option_handler
, NULL
},
100 { {"profile-pc-size", required_argument
, NULL
, 'S'},
101 'S', "PC PROFILE SIZE", "Specify PC profiling size",
102 profile_option_handler
, NULL
},
103 { {"profile-pc-granularity", required_argument
, NULL
, OPTION_PROFILE_PC_GRANULARITY
},
104 '\0', "PC PROFILE GRANULARITY", "Specify PC profiling sample coverage",
105 profile_option_handler
, NULL
},
106 { {"profile-pc-range", required_argument
, NULL
, OPTION_PROFILE_PC_RANGE
},
107 '\0', "BASE,BOUND", "Specify PC profiling address range",
108 profile_option_handler
, NULL
},
110 #ifdef SIM_HAVE_ADDR_RANGE
111 { {"profile-range", required_argument
, NULL
, OPTION_PROFILE_RANGE
},
112 '\0', "START,END", "Specify range of addresses for instruction and model profiling",
113 profile_option_handler
, NULL
},
115 { {"profile-function", required_argument
, NULL
, OPTION_PROFILE_FUNCTION
},
116 '\0', "FUNCTION", "Specify function to profile",
117 profile_option_handler
, NULL
},
121 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
124 /* Set/reset the profile options indicated in MASK. */
127 set_profile_option_mask (SIM_DESC sd
, const char *name
, int mask
, const char *arg
)
135 if (strcmp (arg
, "yes") == 0
136 || strcmp (arg
, "on") == 0
137 || strcmp (arg
, "1") == 0)
139 else if (strcmp (arg
, "no") == 0
140 || strcmp (arg
, "off") == 0
141 || strcmp (arg
, "0") == 0)
145 sim_io_eprintf (sd
, "Argument `%s' for `--profile%s' invalid, one of `on', `off', `yes', `no' expected\n", arg
, name
);
150 /* update applicable profile bits */
151 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
153 if ((mask
& (1 << profile_nr
)) == 0)
156 #if 0 /* see sim-trace.c, set flags in STATE here if/when there are any */
157 /* Set non-cpu specific values. */
165 /* Set cpu values. */
166 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
168 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
] = profile_val
;
172 /* Re-compute the cpu profile summary. */
175 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
176 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
180 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
182 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 0;
183 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
185 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
])
187 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
197 /* Set one profile option based on its IDX value.
198 Not static as cgen-scache.c uses it. */
201 sim_profile_set_option (SIM_DESC sd
, const char *name
, int idx
, const char *arg
)
203 return set_profile_option_mask (sd
, name
, 1 << idx
, arg
);
207 parse_frequency (SIM_DESC sd
, const char *arg
, unsigned long *freq
)
210 /* First, parse a decimal number. */
215 for (/**/; *ch
!= '\0'; ++ch
)
219 *freq
= *freq
* 10 + (*ch
- '0');
222 /* Accept KHz, MHz or Hz as a suffix. */
223 if (tolower (*ch
) == 'm')
228 else if (tolower (*ch
) == 'k')
234 if (tolower (*ch
) == 'h')
237 if (tolower (*ch
) == 'z')
244 sim_io_eprintf (sd
, "Invalid argument for --profile-cpu-frequency: %s\n",
254 profile_option_handler (SIM_DESC sd
,
262 /* FIXME: Need to handle `cpu' arg. */
268 sim_io_eprintf (sd
, "Profiling not compiled in, `-p' ignored\n");
270 return set_profile_option_mask (sd
, "profile", PROFILE_USEFUL_MASK
,
274 case OPTION_PROFILE_INSN
:
275 if (WITH_PROFILE_INSN_P
)
276 return sim_profile_set_option (sd
, "-insn", PROFILE_INSN_IDX
, arg
);
278 sim_io_eprintf (sd
, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
281 case OPTION_PROFILE_MEMORY
:
282 if (WITH_PROFILE_MEMORY_P
)
283 return sim_profile_set_option (sd
, "-memory", PROFILE_MEMORY_IDX
, arg
);
285 sim_io_eprintf (sd
, "Memory profiling not compiled in, `--profile-memory' ignored\n");
288 case OPTION_PROFILE_CORE
:
289 if (WITH_PROFILE_CORE_P
)
290 return sim_profile_set_option (sd
, "-core", PROFILE_CORE_IDX
, arg
);
292 sim_io_eprintf (sd
, "CORE profiling not compiled in, `--profile-core' ignored\n");
295 case OPTION_PROFILE_MODEL
:
296 if (WITH_PROFILE_MODEL_P
)
297 return sim_profile_set_option (sd
, "-model", PROFILE_MODEL_IDX
, arg
);
299 sim_io_eprintf (sd
, "Model profiling not compiled in, `--profile-model' ignored\n");
302 case OPTION_PROFILE_CPU_FREQUENCY
:
305 SIM_RC rc
= parse_frequency (sd
, arg
, &val
);
308 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
309 PROFILE_CPU_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
,cpu_nr
))) = val
;
314 case OPTION_PROFILE_FILE
:
315 /* FIXME: Might want this to apply to pc profiling only,
316 or have two profile file options. */
318 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-file' ignored\n");
321 FILE *f
= fopen (arg
, "w");
325 sim_io_eprintf (sd
, "Unable to open profile output file `%s'\n", arg
);
328 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
329 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = f
;
333 case OPTION_PROFILE_PC
:
334 if (WITH_PROFILE_PC_P
)
335 return sim_profile_set_option (sd
, "-pc", PROFILE_PC_IDX
, arg
);
337 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc' ignored\n");
341 if (WITH_PROFILE_PC_P
)
343 /* FIXME: Validate arg. */
344 int val
= atoi (arg
);
345 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
346 PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
347 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
348 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
351 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
355 if (WITH_PROFILE_PC_P
)
357 /* FIXME: Validate arg. */
358 int val
= atoi (arg
);
359 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
360 PROFILE_PC_NR_BUCKETS (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
361 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
362 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
365 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
368 case OPTION_PROFILE_PC_GRANULARITY
:
369 if (WITH_PROFILE_PC_P
)
372 int val
= atoi (arg
);
373 /* check that the granularity is a power of two */
375 while (val
> (1 << shift
))
379 if (val
!= (1 << shift
))
381 sim_io_eprintf (sd
, "PC profiling granularity not a power of two\n");
386 sim_io_eprintf (sd
, "PC profiling granularity too small");
389 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
390 PROFILE_PC_SHIFT (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = shift
;
391 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
392 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
395 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
398 case OPTION_PROFILE_PC_RANGE
:
399 if (WITH_PROFILE_PC_P
)
401 /* FIXME: Validate args */
405 base
= strtoul (chp
, &chp
, 0);
408 sim_io_eprintf (sd
, "--profile-pc-range missing BOUND argument\n");
411 bound
= strtoul (chp
+ 1, NULL
, 0);
412 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
414 PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = base
;
415 PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = bound
;
417 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
418 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
421 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
424 #ifdef SIM_HAVE_ADDR_RANGE
425 case OPTION_PROFILE_RANGE
:
429 unsigned long start
,end
;
430 start
= strtoul (chp
, &chp
, 0);
433 sim_io_eprintf (sd
, "--profile-range missing END argument\n");
436 end
= strtoul (chp
+ 1, NULL
, 0);
437 /* FIXME: Argument validation. */
439 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
442 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
443 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))),
447 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-range' ignored\n");
450 case OPTION_PROFILE_FUNCTION
:
453 /*wip: need to compute function range given name*/
456 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-function' ignored\n");
458 #endif /* SIM_HAVE_ADDR_RANGE */
464 /* Profiling output hooks. */
467 profile_vprintf (SIM_DESC sd
, sim_cpu
*cpu
, const char *fmt
, va_list ap
)
469 FILE *fp
= PROFILE_FILE (CPU_PROFILE_DATA (cpu
));
471 /* If an output file was given, redirect output to that. */
473 vfprintf (fp
, fmt
, ap
);
475 sim_io_evprintf (sd
, fmt
, ap
);
478 __attribute__ ((format (printf
, 3, 4)))
480 profile_printf (SIM_DESC sd
, sim_cpu
*cpu
, const char *fmt
, ...)
485 profile_vprintf (sd
, cpu
, fmt
, ap
);
489 /* PC profiling support */
491 #if WITH_PROFILE_PC_P
494 profile_pc_cleanup (SIM_DESC sd
)
497 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
499 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
500 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
501 if (PROFILE_PC_COUNT (data
) != NULL
)
502 free (PROFILE_PC_COUNT (data
));
503 PROFILE_PC_COUNT (data
) = NULL
;
504 if (PROFILE_PC_EVENT (data
) != NULL
)
505 sim_events_deschedule (sd
, PROFILE_PC_EVENT (data
));
506 PROFILE_PC_EVENT (data
) = NULL
;
512 profile_pc_uninstall (SIM_DESC sd
)
514 profile_pc_cleanup (sd
);
518 profile_pc_event (SIM_DESC sd
,
521 sim_cpu
*cpu
= (sim_cpu
*) data
;
522 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
523 address_word pc
= sim_pc_get (cpu
);
525 i
= (pc
- PROFILE_PC_START (profile
)) >> PROFILE_PC_SHIFT (profile
);
526 if (i
< PROFILE_PC_NR_BUCKETS (profile
))
527 PROFILE_PC_COUNT (profile
) [i
] += 1; /* Overflow? */
529 PROFILE_PC_COUNT (profile
) [PROFILE_PC_NR_BUCKETS (profile
)] += 1;
530 PROFILE_PC_EVENT (profile
) =
531 sim_events_schedule (sd
, PROFILE_PC_FREQ (profile
), profile_pc_event
, cpu
);
535 profile_pc_init (SIM_DESC sd
)
538 profile_pc_cleanup (sd
);
539 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
541 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
542 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
543 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
])
546 /* fill in the frequency if not specified */
547 if (PROFILE_PC_FREQ (data
) == 0)
548 PROFILE_PC_FREQ (data
) = 257;
549 /* fill in the start/end if not specified */
550 if (PROFILE_PC_END (data
) == 0)
552 PROFILE_PC_START (data
) = STATE_TEXT_START (sd
);
553 PROFILE_PC_END (data
) = STATE_TEXT_END (sd
);
555 /* Compute the number of buckets if not specified. */
556 if (PROFILE_PC_NR_BUCKETS (data
) == 0)
558 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
559 PROFILE_PC_NR_BUCKETS (data
) = 16;
562 if (PROFILE_PC_END (data
) == 0)
564 /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
565 PROFILE_PC_NR_BUCKETS (data
) =
566 ((1 << sizeof (sim_cia
) * (8 - 1))
567 / (PROFILE_PC_BUCKET_SIZE (data
) / 2));
571 PROFILE_PC_NR_BUCKETS (data
) =
572 ((PROFILE_PC_END (data
)
573 - PROFILE_PC_START (data
)
574 + PROFILE_PC_BUCKET_SIZE (data
) - 1)
575 / PROFILE_PC_BUCKET_SIZE (data
));
579 /* Compute the bucket size if not specified. Ensure that it
580 is rounded up to the next power of two */
581 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
583 if (PROFILE_PC_END (data
) == 0)
584 /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
585 bucket_size
= ((1 << ((sizeof (sim_cia
) * 8) - 1))
586 / (PROFILE_PC_NR_BUCKETS (data
) / 2));
588 bucket_size
= ((PROFILE_PC_END (data
)
589 - PROFILE_PC_START (data
)
590 + PROFILE_PC_NR_BUCKETS (data
) - 1)
591 / PROFILE_PC_NR_BUCKETS (data
));
592 PROFILE_PC_SHIFT (data
) = 0;
593 while (bucket_size
> PROFILE_PC_BUCKET_SIZE (data
))
595 PROFILE_PC_SHIFT (data
) += 1;
598 /* Align the end address with bucket size */
599 if (PROFILE_PC_END (data
) != 0)
600 PROFILE_PC_END (data
) = (PROFILE_PC_START (data
)
601 + (PROFILE_PC_BUCKET_SIZE (data
)
602 * PROFILE_PC_NR_BUCKETS (data
)));
603 /* create the relevant buffers */
604 PROFILE_PC_COUNT (data
) =
605 NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data
) + 1);
606 PROFILE_PC_EVENT (data
) =
607 sim_events_schedule (sd
,
608 PROFILE_PC_FREQ (data
),
617 profile_print_pc (sim_cpu
*cpu
, int verbose
)
619 SIM_DESC sd
= CPU_STATE (cpu
);
620 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
626 if (PROFILE_PC_COUNT (profile
) == 0)
629 profile_printf (sd
, cpu
, "Program Counter Statistics:\n\n");
631 /* First pass over data computes various things. */
634 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
636 total
+= PROFILE_PC_COUNT (profile
) [i
];
637 if (PROFILE_PC_COUNT (profile
) [i
] > max_val
)
638 max_val
= PROFILE_PC_COUNT (profile
) [i
];
641 profile_printf (sd
, cpu
, " Total samples: %s\n",
643 profile_printf (sd
, cpu
, " Granularity: %s bytes per bucket\n",
644 COMMAS (PROFILE_PC_BUCKET_SIZE (profile
)));
645 profile_printf (sd
, cpu
, " Size: %s buckets\n",
646 COMMAS (PROFILE_PC_NR_BUCKETS (profile
)));
647 profile_printf (sd
, cpu
, " Frequency: %s cycles per sample\n",
648 COMMAS (PROFILE_PC_FREQ (profile
)));
650 if (PROFILE_PC_END (profile
) != 0)
651 profile_printf (sd
, cpu
, " Range: 0x%lx 0x%lx\n",
652 (long) PROFILE_PC_START (profile
),
653 (long) PROFILE_PC_END (profile
));
655 if (verbose
&& max_val
!= 0)
657 /* Now we can print the histogram. */
658 profile_printf (sd
, cpu
, "\n");
659 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
661 if (PROFILE_PC_COUNT (profile
) [i
] != 0)
663 profile_printf (sd
, cpu
, " ");
664 if (i
== PROFILE_PC_NR_BUCKETS (profile
))
665 profile_printf (sd
, cpu
, "%10s:", "overflow");
667 profile_printf (sd
, cpu
, "0x%08lx:",
668 (long) (PROFILE_PC_START (profile
)
669 + (i
* PROFILE_PC_BUCKET_SIZE (profile
))));
670 profile_printf (sd
, cpu
, " %*s",
671 max_val
< 10000 ? 5 : 10,
672 COMMAS (PROFILE_PC_COUNT (profile
) [i
]));
673 profile_printf (sd
, cpu
, " %4.1f",
674 (PROFILE_PC_COUNT (profile
) [i
] * 100.0) / total
);
675 profile_printf (sd
, cpu
, ": ");
676 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
677 PROFILE_PC_COUNT (profile
) [i
],
679 profile_printf (sd
, cpu
, "\n");
684 /* dump the histogram to the file "gmon.out" using BSD's gprof file
686 /* Since a profile data file is in the native format of the host on
687 which the profile is being, endian issues are not considered in
689 /* FIXME: Is this the best place for this code? */
691 FILE *pf
= fopen ("gmon.out", "wb");
694 sim_io_eprintf (sd
, "Failed to open \"gmon.out\" profile file\n");
698 /* FIXME: what if the target has a 64 bit PC? */
699 unsigned32 header
[3];
701 if (PROFILE_PC_END (profile
) != 0)
703 header
[0] = PROFILE_PC_START (profile
);
704 header
[1] = PROFILE_PC_END (profile
);
711 /* size of sample buffer (+ header) */
712 header
[2] = PROFILE_PC_NR_BUCKETS (profile
) * 2 + sizeof (header
);
714 /* Header must be written out in target byte order. */
719 ok
= fwrite (&header
, sizeof (header
), 1, pf
);
721 ok
&& (loop
< PROFILE_PC_NR_BUCKETS (profile
));
725 if (PROFILE_PC_COUNT (profile
) [loop
] >= 0xffff)
728 sample
= PROFILE_PC_COUNT (profile
) [loop
];
730 ok
= fwrite (&sample
, sizeof (sample
), 1, pf
);
733 sim_io_eprintf (sd
, "Failed to write to \"gmon.out\" profile file\n");
738 profile_printf (sd
, cpu
, "\n");
743 /* Summary printing support. */
745 #if WITH_PROFILE_INSN_P
748 profile_insn_init (SIM_DESC sd
)
752 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
754 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
756 if (CPU_MAX_INSNS (cpu
) > 0)
757 PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu
)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu
));
764 profile_print_insn (sim_cpu
*cpu
, int verbose
)
766 unsigned int i
, n
, total
, max_val
, max_name_len
;
767 SIM_DESC sd
= CPU_STATE (cpu
);
768 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
771 /* If MAX_INSNS not set, insn profiling isn't supported. */
772 if (CPU_MAX_INSNS (cpu
) == 0)
775 profile_printf (sd
, cpu
, "Instruction Statistics");
776 #ifdef SIM_HAVE_ADDR_RANGE
777 if (PROFILE_RANGE (data
)->ranges
)
778 profile_printf (sd
, cpu
, " (for selected address range(s))");
780 profile_printf (sd
, cpu
, "\n\n");
782 /* First pass over data computes various things. */
786 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
788 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
792 total
+= PROFILE_INSN_COUNT (data
) [i
];
793 if (PROFILE_INSN_COUNT (data
) [i
] > max_val
)
794 max_val
= PROFILE_INSN_COUNT (data
) [i
];
796 if (n
> max_name_len
)
799 /* set the total insn count, in case client is being lazy */
800 if (! PROFILE_TOTAL_INSN_COUNT (data
))
801 PROFILE_TOTAL_INSN_COUNT (data
) = total
;
803 profile_printf (sd
, cpu
, " Total: %s insns\n", COMMAS (total
));
805 if (verbose
&& max_val
!= 0)
807 /* Now we can print the histogram. */
808 profile_printf (sd
, cpu
, "\n");
809 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
811 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
815 if (PROFILE_INSN_COUNT (data
) [i
] != 0)
817 profile_printf (sd
, cpu
, " %*s: %*s: ",
819 max_val
< 10000 ? 5 : 10,
820 COMMAS (PROFILE_INSN_COUNT (data
) [i
]));
821 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
822 PROFILE_INSN_COUNT (data
) [i
],
824 profile_printf (sd
, cpu
, "\n");
829 profile_printf (sd
, cpu
, "\n");
834 #if WITH_PROFILE_MEMORY_P
837 profile_print_memory (sim_cpu
*cpu
, int verbose
)
840 unsigned int total_read
, total_write
;
841 unsigned int max_val
, max_name_len
;
842 /* FIXME: Need to add smp support. */
843 SIM_DESC sd
= CPU_STATE (cpu
);
844 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
847 profile_printf (sd
, cpu
, "Memory Access Statistics\n\n");
849 /* First pass over data computes various things. */
850 max_val
= total_read
= total_write
= max_name_len
= 0;
851 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
853 total_read
+= PROFILE_READ_COUNT (data
) [i
];
854 total_write
+= PROFILE_WRITE_COUNT (data
) [i
];
855 if (PROFILE_READ_COUNT (data
) [i
] > max_val
)
856 max_val
= PROFILE_READ_COUNT (data
) [i
];
857 if (PROFILE_WRITE_COUNT (data
) [i
] > max_val
)
858 max_val
= PROFILE_WRITE_COUNT (data
) [i
];
859 n
= strlen (MODE_NAME (i
));
860 if (n
> max_name_len
)
864 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
865 profile_printf (sd
, cpu
, " Total read: %s accesses\n",
866 COMMAS (total_read
));
867 profile_printf (sd
, cpu
, " Total write: %s accesses\n",
868 COMMAS (total_write
));
870 if (verbose
&& max_val
!= 0)
872 /* FIXME: Need to separate instruction fetches from data fetches
873 as the former swamps the latter. */
874 /* Now we can print the histogram. */
875 profile_printf (sd
, cpu
, "\n");
876 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
878 if (PROFILE_READ_COUNT (data
) [i
] != 0)
880 profile_printf (sd
, cpu
, " %*s read: %*s: ",
881 max_name_len
, MODE_NAME (i
),
882 max_val
< 10000 ? 5 : 10,
883 COMMAS (PROFILE_READ_COUNT (data
) [i
]));
884 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
885 PROFILE_READ_COUNT (data
) [i
],
887 profile_printf (sd
, cpu
, "\n");
889 if (PROFILE_WRITE_COUNT (data
) [i
] != 0)
891 profile_printf (sd
, cpu
, " %*s write: %*s: ",
892 max_name_len
, MODE_NAME (i
),
893 max_val
< 10000 ? 5 : 10,
894 COMMAS (PROFILE_WRITE_COUNT (data
) [i
]));
895 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
896 PROFILE_WRITE_COUNT (data
) [i
],
898 profile_printf (sd
, cpu
, "\n");
903 profile_printf (sd
, cpu
, "\n");
908 #if WITH_PROFILE_CORE_P
911 profile_print_core (sim_cpu
*cpu
, int verbose
)
914 unsigned int max_val
;
915 /* FIXME: Need to add smp support. */
916 SIM_DESC sd
= CPU_STATE (cpu
);
917 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
920 profile_printf (sd
, cpu
, "CORE Statistics\n\n");
922 /* First pass over data computes various things. */
927 for (map
= 0; map
< nr_maps
; map
++)
929 total
+= PROFILE_CORE_COUNT (data
) [map
];
930 if (PROFILE_CORE_COUNT (data
) [map
] > max_val
)
931 max_val
= PROFILE_CORE_COUNT (data
) [map
];
935 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
936 profile_printf (sd
, cpu
, " Total: %s accesses\n",
939 if (verbose
&& max_val
!= 0)
942 /* Now we can print the histogram. */
943 profile_printf (sd
, cpu
, "\n");
944 for (map
= 0; map
< nr_maps
; map
++)
946 if (PROFILE_CORE_COUNT (data
) [map
] != 0)
948 profile_printf (sd
, cpu
, "%10s:", map_to_str (map
));
949 profile_printf (sd
, cpu
, "%*s: ",
950 max_val
< 10000 ? 5 : 10,
951 COMMAS (PROFILE_CORE_COUNT (data
) [map
]));
952 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
953 PROFILE_CORE_COUNT (data
) [map
],
955 profile_printf (sd
, cpu
, "\n");
960 profile_printf (sd
, cpu
, "\n");
965 #if WITH_PROFILE_MODEL_P
968 profile_print_model (sim_cpu
*cpu
, int verbose
)
970 SIM_DESC sd
= CPU_STATE (cpu
);
971 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
972 unsigned long cti_stall_cycles
= PROFILE_MODEL_CTI_STALL_CYCLES (data
);
973 unsigned long load_stall_cycles
= PROFILE_MODEL_LOAD_STALL_CYCLES (data
);
974 unsigned long total_cycles
= PROFILE_MODEL_TOTAL_CYCLES (data
);
977 profile_printf (sd
, cpu
, "Model %s Timing Information",
978 MODEL_NAME (CPU_MODEL (cpu
)));
979 #ifdef SIM_HAVE_ADDR_RANGE
980 if (PROFILE_RANGE (data
)->ranges
)
981 profile_printf (sd
, cpu
, " (for selected address range(s))");
983 profile_printf (sd
, cpu
, "\n\n");
984 profile_printf (sd
, cpu
, " %-*s %s\n",
985 PROFILE_LABEL_WIDTH
, "Taken branches:",
986 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data
)));
987 profile_printf (sd
, cpu
, " %-*s %s\n",
988 PROFILE_LABEL_WIDTH
, "Untaken branches:",
989 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data
)));
990 profile_printf (sd
, cpu
, " %-*s %s\n",
991 PROFILE_LABEL_WIDTH
, "Cycles stalled due to branches:",
992 COMMAS (cti_stall_cycles
));
993 profile_printf (sd
, cpu
, " %-*s %s\n",
994 PROFILE_LABEL_WIDTH
, "Cycles stalled due to loads:",
995 COMMAS (load_stall_cycles
));
996 profile_printf (sd
, cpu
, " %-*s %s\n",
997 PROFILE_LABEL_WIDTH
, "Total cycles (*approximate*):",
998 COMMAS (total_cycles
));
999 profile_printf (sd
, cpu
, "\n");
1005 sim_profile_print_bar (SIM_DESC sd
, sim_cpu
*cpu
, unsigned int width
,
1006 unsigned int val
, unsigned int max_val
)
1008 unsigned int i
, count
;
1010 count
= ((double) val
/ (double) max_val
) * (double) width
;
1012 for (i
= 0; i
< count
; ++i
)
1013 profile_printf (sd
, cpu
, "*");
1016 /* Print the simulator's execution speed for CPU. */
1019 profile_print_speed (sim_cpu
*cpu
)
1021 SIM_DESC sd
= CPU_STATE (cpu
);
1022 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1023 unsigned long milliseconds
= sim_events_elapsed_time (sd
);
1024 unsigned long total
= PROFILE_TOTAL_INSN_COUNT (data
);
1029 profile_printf (sd
, cpu
, "Simulator Execution Speed\n\n");
1032 profile_printf (sd
, cpu
, " Total instructions: %s\n", COMMAS (total
));
1034 if (milliseconds
< 1000)
1035 profile_printf (sd
, cpu
, " Total execution time: < 1 second\n\n");
1038 /* The printing of the time rounded to 2 decimal places makes the speed
1039 calculation seem incorrect [even though it is correct]. So round
1040 MILLISECONDS first. This can marginally affect the result, but it's
1041 better that the user not perceive there's a math error. */
1042 secs
= (double) milliseconds
/ 1000;
1043 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1044 profile_printf (sd
, cpu
, " Total execution time : %.2f seconds\n", secs
);
1045 /* Don't confuse things with data that isn't useful.
1046 If we ran for less than 2 seconds, only use the data if we
1047 executed more than 100,000 insns. */
1048 if (secs
>= 2 || total
>= 100000)
1049 profile_printf (sd
, cpu
, " Simulator speed: %s insns/second\n",
1050 COMMAS ((unsigned long) ((double) total
/ secs
)));
1053 /* Print simulated execution time if the cpu frequency has been specified. */
1054 clock
= PROFILE_CPU_FREQ (data
);
1057 if (clock
>= 1000000)
1058 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f MHz\n",
1061 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f Hz\n", clock
);
1063 #if WITH_PROFILE_MODEL_P
1064 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1066 /* The printing of the time rounded to 2 decimal places makes the
1067 speed calculation seem incorrect [even though it is correct].
1068 So round SECS first. This can marginally affect the result,
1069 but it's better that the user not perceive there's a math
1071 secs
= PROFILE_MODEL_TOTAL_CYCLES (data
) / clock
;
1072 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1073 profile_printf (sd
, cpu
, " Simulated execution time: %.2f seconds\n",
1076 #endif /* WITH_PROFILE_MODEL_P */
1080 #ifdef SIM_HAVE_ADDR_RANGE
1081 /* Print selected address ranges. */
1084 profile_print_addr_ranges (sim_cpu
*cpu
)
1086 ADDR_SUBRANGE
*asr
= PROFILE_RANGE (CPU_PROFILE_DATA (cpu
))->ranges
;
1087 SIM_DESC sd
= CPU_STATE (cpu
);
1091 profile_printf (sd
, cpu
, "Selected address ranges\n\n");
1094 profile_printf (sd
, cpu
, " 0x%lx - 0x%lx\n",
1095 (long) asr
->start
, (long) asr
->end
);
1098 profile_printf (sd
, cpu
, "\n");
1103 /* Top level function to print all summary profile information.
1104 It is [currently] intended that all such data is printed by this function.
1105 I'd rather keep it all in one place for now. To that end, MISC_CPU and
1106 MISC are callbacks used to print any miscellaneous data.
1108 One might want to add a user option that allows printing by type or by cpu
1109 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
1110 This may be a case of featuritis so it's currently left out.
1112 Note that results are indented two spaces to distinguish them from
1116 profile_info (SIM_DESC sd
, int verbose
)
1119 int print_title_p
= 0;
1121 /* Only print the title if some data has been collected. */
1122 /* ??? Why don't we just exit if no data collected? */
1123 /* FIXME: If the number of processors can be selected on the command line,
1124 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
1126 for (c
= 0; c
< MAX_NR_PROCESSORS
&& !print_title_p
; ++c
)
1128 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1129 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1131 for (i
= 0; i
< MAX_PROFILE_VALUES
; ++i
)
1132 if (PROFILE_FLAGS (data
) [i
])
1134 profile_printf (sd
, cpu
, "Summary profiling results:\n\n");
1140 /* Loop, cpu by cpu, printing results. */
1142 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
1144 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1145 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1147 if (MAX_NR_PROCESSORS
> 1
1149 #if WITH_PROFILE_INSN_P
1150 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
]
1152 #if WITH_PROFILE_MEMORY_P
1153 || PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
]
1155 #if WITH_PROFILE_CORE_P
1156 || PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
]
1158 #if WITH_PROFILE_MODEL_P
1159 || PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
]
1161 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1162 || PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
]
1164 #if WITH_PROFILE_PC_P
1165 || PROFILE_FLAGS (data
) [PROFILE_PC_IDX
]
1169 profile_printf (sd
, cpu
, "CPU %d\n\n", c
);
1172 #ifdef SIM_HAVE_ADDR_RANGE
1174 && (PROFILE_INSN_P (cpu
)
1175 || PROFILE_MODEL_P (cpu
)))
1176 profile_print_addr_ranges (cpu
);
1179 #if WITH_PROFILE_INSN_P
1180 if (PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1181 profile_print_insn (cpu
, verbose
);
1184 #if WITH_PROFILE_MEMORY_P
1185 if (PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
])
1186 profile_print_memory (cpu
, verbose
);
1189 #if WITH_PROFILE_CORE_P
1190 if (PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
])
1191 profile_print_core (cpu
, verbose
);
1194 #if WITH_PROFILE_MODEL_P
1195 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1196 profile_print_model (cpu
, verbose
);
1199 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1200 if (PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
])
1201 scache_print_profile (cpu
, verbose
);
1204 #if WITH_PROFILE_PC_P
1205 if (PROFILE_FLAGS (data
) [PROFILE_PC_IDX
])
1206 profile_print_pc (cpu
, verbose
);
1209 /* Print cpu-specific data before the execution speed. */
1210 if (PROFILE_INFO_CPU_CALLBACK (data
) != NULL
)
1211 PROFILE_INFO_CPU_CALLBACK (data
) (cpu
, verbose
);
1213 /* Always try to print execution time and speed. */
1215 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1216 profile_print_speed (cpu
);
1219 /* Finally print non-cpu specific miscellaneous data. */
1220 if (STATE_PROFILE_INFO_CALLBACK (sd
))
1221 STATE_PROFILE_INFO_CALLBACK (sd
) (sd
, verbose
);
1225 /* Install profiling support in the simulator. */
1228 profile_install (SIM_DESC sd
)
1232 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1233 sim_add_option_table (sd
, NULL
, profile_options
);
1234 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1235 memset (CPU_PROFILE_DATA (STATE_CPU (sd
, i
)), 0,
1236 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd
, i
))));
1237 #if WITH_PROFILE_INSN_P
1238 sim_module_add_init_fn (sd
, profile_insn_init
);
1240 #if WITH_PROFILE_PC_P
1241 sim_module_add_uninstall_fn (sd
, profile_pc_uninstall
);
1242 sim_module_add_init_fn (sd
, profile_pc_init
);
1244 sim_module_add_init_fn (sd
, profile_init
);
1245 sim_module_add_uninstall_fn (sd
, profile_uninstall
);
1246 sim_module_add_info_fn (sd
, profile_info
);
1251 profile_init (SIM_DESC sd
)
1253 #ifdef SIM_HAVE_ADDR_RANGE
1254 /* Check if a range has been specified without specifying what to
1259 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1261 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1263 if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)))
1264 && ! (PROFILE_INSN_P (cpu
)
1265 || PROFILE_MODEL_P (cpu
)))
1267 sim_io_eprintf_cpu (cpu
, "Profiling address range specified without --profile-insn or --profile-model.\n");
1268 sim_io_eprintf_cpu (cpu
, "Address range ignored.\n");
1269 sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
1270 0, ~ (address_word
) 0);
1280 profile_uninstall (SIM_DESC sd
)
1284 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1286 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1287 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1289 if (PROFILE_FILE (data
) != NULL
)
1291 /* If output from different cpus is going to the same file,
1292 avoid closing the file twice. */
1293 for (j
= 0; j
< i
; ++j
)
1294 if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, j
)))
1295 == PROFILE_FILE (data
))
1298 fclose (PROFILE_FILE (data
));
1301 if (PROFILE_INSN_COUNT (data
) != NULL
)
1302 free (PROFILE_INSN_COUNT (data
));