1 /* Default profiling support.
2 Copyright (C) 1996-1998, 2000-2001, 2007-2012 Free Software
4 Contributed by Cygnus Support.
6 This file is part of GDB, the GNU debugger.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "sim-options.h"
24 #include "sim-assert.h"
39 #if !WITH_PROFILE_PC_P
40 static unsigned int _profile_stub
;
41 # define PROFILE_PC_FREQ(p) _profile_stub
42 # define PROFILE_PC_NR_BUCKETS(p) _profile_stub
43 # define PROFILE_PC_SHIFT(p) _profile_stub
44 # define PROFILE_PC_START(p) _profile_stub
45 # define PROFILE_PC_END(p) _profile_stub
46 # define PROFILE_INSN_COUNT(p) &_profile_stub
49 #define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n))
51 static MODULE_INIT_FN profile_init
;
52 static MODULE_UNINSTALL_FN profile_uninstall
;
54 static DECLARE_OPTION_HANDLER (profile_option_handler
);
57 OPTION_PROFILE_INSN
= OPTION_START
,
58 OPTION_PROFILE_MEMORY
,
62 OPTION_PROFILE_CPU_FREQUENCY
,
64 OPTION_PROFILE_PC_RANGE
,
65 OPTION_PROFILE_PC_GRANULARITY
,
67 OPTION_PROFILE_FUNCTION
70 static const OPTION profile_options
[] = {
71 { {"profile", optional_argument
, NULL
, 'p'},
72 'p', "on|off", "Perform profiling",
73 profile_option_handler
, NULL
},
74 { {"profile-insn", optional_argument
, NULL
, OPTION_PROFILE_INSN
},
75 '\0', "on|off", "Perform instruction profiling",
76 profile_option_handler
, NULL
},
77 { {"profile-memory", optional_argument
, NULL
, OPTION_PROFILE_MEMORY
},
78 '\0', "on|off", "Perform memory profiling",
79 profile_option_handler
, NULL
},
80 { {"profile-core", optional_argument
, NULL
, OPTION_PROFILE_CORE
},
81 '\0', "on|off", "Perform CORE profiling",
82 profile_option_handler
, NULL
},
83 { {"profile-model", optional_argument
, NULL
, OPTION_PROFILE_MODEL
},
84 '\0', "on|off", "Perform model profiling",
85 profile_option_handler
, NULL
},
86 { {"profile-cpu-frequency", required_argument
, NULL
,
87 OPTION_PROFILE_CPU_FREQUENCY
},
88 '\0', "CPU FREQUENCY", "Specify the speed of the simulated cpu clock",
89 profile_option_handler
, NULL
},
91 { {"profile-file", required_argument
, NULL
, OPTION_PROFILE_FILE
},
92 '\0', "FILE NAME", "Specify profile output file",
93 profile_option_handler
, NULL
},
95 { {"profile-pc", optional_argument
, NULL
, OPTION_PROFILE_PC
},
96 '\0', "on|off", "Perform PC profiling",
97 profile_option_handler
, NULL
},
98 { {"profile-pc-frequency", required_argument
, NULL
, 'F'},
99 'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency",
100 profile_option_handler
, NULL
},
101 { {"profile-pc-size", required_argument
, NULL
, 'S'},
102 'S', "PC PROFILE SIZE", "Specify PC profiling size",
103 profile_option_handler
, NULL
},
104 { {"profile-pc-granularity", required_argument
, NULL
, OPTION_PROFILE_PC_GRANULARITY
},
105 '\0', "PC PROFILE GRANULARITY", "Specify PC profiling sample coverage",
106 profile_option_handler
, NULL
},
107 { {"profile-pc-range", required_argument
, NULL
, OPTION_PROFILE_PC_RANGE
},
108 '\0', "BASE,BOUND", "Specify PC profiling address range",
109 profile_option_handler
, NULL
},
111 #ifdef SIM_HAVE_ADDR_RANGE
112 { {"profile-range", required_argument
, NULL
, OPTION_PROFILE_RANGE
},
113 '\0', "START,END", "Specify range of addresses for instruction and model profiling",
114 profile_option_handler
, NULL
},
116 { {"profile-function", required_argument
, NULL
, OPTION_PROFILE_FUNCTION
},
117 '\0', "FUNCTION", "Specify function to profile",
118 profile_option_handler
, NULL
},
122 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
125 /* Set/reset the profile options indicated in MASK. */
128 set_profile_option_mask (SIM_DESC sd
, const char *name
, int mask
, const char *arg
)
136 if (strcmp (arg
, "yes") == 0
137 || strcmp (arg
, "on") == 0
138 || strcmp (arg
, "1") == 0)
140 else if (strcmp (arg
, "no") == 0
141 || strcmp (arg
, "off") == 0
142 || strcmp (arg
, "0") == 0)
146 sim_io_eprintf (sd
, "Argument `%s' for `--profile%s' invalid, one of `on', `off', `yes', `no' expected\n", arg
, name
);
151 /* update applicable profile bits */
152 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
154 if ((mask
& (1 << profile_nr
)) == 0)
157 #if 0 /* see sim-trace.c, set flags in STATE here if/when there are any */
158 /* Set non-cpu specific values. */
166 /* Set cpu values. */
167 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
169 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
] = profile_val
;
173 /* Re-compute the cpu profile summary. */
176 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
177 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
181 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
183 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 0;
184 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
186 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
])
188 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
198 /* Set one profile option based on its IDX value.
199 Not static as cgen-scache.c uses it. */
202 sim_profile_set_option (SIM_DESC sd
, const char *name
, int idx
, const char *arg
)
204 return set_profile_option_mask (sd
, name
, 1 << idx
, arg
);
208 parse_frequency (SIM_DESC sd
, const char *arg
, unsigned long *freq
)
211 /* First, parse a decimal number. */
216 for (/**/; *ch
!= '\0'; ++ch
)
220 *freq
= *freq
* 10 + (*ch
- '0');
223 /* Accept KHz, MHz or Hz as a suffix. */
224 if (tolower (*ch
) == 'm')
229 else if (tolower (*ch
) == 'k')
235 if (tolower (*ch
) == 'h')
238 if (tolower (*ch
) == 'z')
245 sim_io_eprintf (sd
, "Invalid argument for --profile-cpu-frequency: %s\n",
255 profile_option_handler (SIM_DESC sd
,
263 /* FIXME: Need to handle `cpu' arg. */
269 sim_io_eprintf (sd
, "Profiling not compiled in, `-p' ignored\n");
271 return set_profile_option_mask (sd
, "profile", PROFILE_USEFUL_MASK
,
275 case OPTION_PROFILE_INSN
:
276 if (WITH_PROFILE_INSN_P
)
277 return sim_profile_set_option (sd
, "-insn", PROFILE_INSN_IDX
, arg
);
279 sim_io_eprintf (sd
, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
282 case OPTION_PROFILE_MEMORY
:
283 if (WITH_PROFILE_MEMORY_P
)
284 return sim_profile_set_option (sd
, "-memory", PROFILE_MEMORY_IDX
, arg
);
286 sim_io_eprintf (sd
, "Memory profiling not compiled in, `--profile-memory' ignored\n");
289 case OPTION_PROFILE_CORE
:
290 if (WITH_PROFILE_CORE_P
)
291 return sim_profile_set_option (sd
, "-core", PROFILE_CORE_IDX
, arg
);
293 sim_io_eprintf (sd
, "CORE profiling not compiled in, `--profile-core' ignored\n");
296 case OPTION_PROFILE_MODEL
:
297 if (WITH_PROFILE_MODEL_P
)
298 return sim_profile_set_option (sd
, "-model", PROFILE_MODEL_IDX
, arg
);
300 sim_io_eprintf (sd
, "Model profiling not compiled in, `--profile-model' ignored\n");
303 case OPTION_PROFILE_CPU_FREQUENCY
:
306 SIM_RC rc
= parse_frequency (sd
, arg
, &val
);
309 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
310 PROFILE_CPU_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
,cpu_nr
))) = val
;
315 case OPTION_PROFILE_FILE
:
316 /* FIXME: Might want this to apply to pc profiling only,
317 or have two profile file options. */
319 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-file' ignored\n");
322 FILE *f
= fopen (arg
, "w");
326 sim_io_eprintf (sd
, "Unable to open profile output file `%s'\n", arg
);
329 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
330 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = f
;
334 case OPTION_PROFILE_PC
:
335 if (WITH_PROFILE_PC_P
)
336 return sim_profile_set_option (sd
, "-pc", PROFILE_PC_IDX
, arg
);
338 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc' ignored\n");
342 if (WITH_PROFILE_PC_P
)
344 /* FIXME: Validate arg. */
345 int val
= atoi (arg
);
346 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
347 PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
348 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
349 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
352 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
356 if (WITH_PROFILE_PC_P
)
358 /* FIXME: Validate arg. */
359 int val
= atoi (arg
);
360 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
361 PROFILE_PC_NR_BUCKETS (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
362 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
363 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
366 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
369 case OPTION_PROFILE_PC_GRANULARITY
:
370 if (WITH_PROFILE_PC_P
)
373 int val
= atoi (arg
);
374 /* check that the granularity is a power of two */
376 while (val
> (1 << shift
))
380 if (val
!= (1 << shift
))
382 sim_io_eprintf (sd
, "PC profiling granularity not a power of two\n");
387 sim_io_eprintf (sd
, "PC profiling granularity too small");
390 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
391 PROFILE_PC_SHIFT (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = shift
;
392 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
393 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
396 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
399 case OPTION_PROFILE_PC_RANGE
:
400 if (WITH_PROFILE_PC_P
)
402 /* FIXME: Validate args */
406 base
= strtoul (chp
, &chp
, 0);
409 sim_io_eprintf (sd
, "--profile-pc-range missing BOUND argument\n");
412 bound
= strtoul (chp
+ 1, NULL
, 0);
413 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
415 PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = base
;
416 PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = bound
;
418 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
419 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
422 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
425 #ifdef SIM_HAVE_ADDR_RANGE
426 case OPTION_PROFILE_RANGE
:
430 unsigned long start
,end
;
431 start
= strtoul (chp
, &chp
, 0);
434 sim_io_eprintf (sd
, "--profile-range missing END argument\n");
437 end
= strtoul (chp
+ 1, NULL
, 0);
438 /* FIXME: Argument validation. */
440 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
443 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
444 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))),
448 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-range' ignored\n");
451 case OPTION_PROFILE_FUNCTION
:
454 /*wip: need to compute function range given name*/
457 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-function' ignored\n");
459 #endif /* SIM_HAVE_ADDR_RANGE */
465 /* Profiling output hooks. */
468 profile_vprintf (SIM_DESC sd
, sim_cpu
*cpu
, const char *fmt
, va_list ap
)
470 FILE *fp
= PROFILE_FILE (CPU_PROFILE_DATA (cpu
));
472 /* If an output file was given, redirect output to that. */
474 vfprintf (fp
, fmt
, ap
);
476 sim_io_evprintf (sd
, fmt
, ap
);
479 __attribute__ ((format (printf
, 3, 4)))
481 profile_printf (SIM_DESC sd
, sim_cpu
*cpu
, const char *fmt
, ...)
486 profile_vprintf (sd
, cpu
, fmt
, ap
);
490 /* PC profiling support */
492 #if WITH_PROFILE_PC_P
495 profile_pc_cleanup (SIM_DESC sd
)
498 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
500 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
501 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
502 if (PROFILE_PC_COUNT (data
) != NULL
)
503 free (PROFILE_PC_COUNT (data
));
504 PROFILE_PC_COUNT (data
) = NULL
;
505 if (PROFILE_PC_EVENT (data
) != NULL
)
506 sim_events_deschedule (sd
, PROFILE_PC_EVENT (data
));
507 PROFILE_PC_EVENT (data
) = NULL
;
513 profile_pc_uninstall (SIM_DESC sd
)
515 profile_pc_cleanup (sd
);
519 profile_pc_event (SIM_DESC sd
,
522 sim_cpu
*cpu
= (sim_cpu
*) data
;
523 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
526 switch (STATE_WATCHPOINTS (sd
)->sizeof_pc
)
528 case 2: pc
= *(unsigned_2
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
529 case 4: pc
= *(unsigned_4
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
530 case 8: pc
= *(unsigned_8
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
533 i
= (pc
- PROFILE_PC_START (profile
)) >> PROFILE_PC_SHIFT (profile
);
534 if (i
< PROFILE_PC_NR_BUCKETS (profile
))
535 PROFILE_PC_COUNT (profile
) [i
] += 1; /* Overflow? */
537 PROFILE_PC_COUNT (profile
) [PROFILE_PC_NR_BUCKETS (profile
)] += 1;
538 PROFILE_PC_EVENT (profile
) =
539 sim_events_schedule (sd
, PROFILE_PC_FREQ (profile
), profile_pc_event
, cpu
);
543 profile_pc_init (SIM_DESC sd
)
546 profile_pc_cleanup (sd
);
547 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
549 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
550 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
551 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
]
552 && STATE_WATCHPOINTS (sd
)->pc
!= NULL
)
555 /* fill in the frequency if not specified */
556 if (PROFILE_PC_FREQ (data
) == 0)
557 PROFILE_PC_FREQ (data
) = 257;
558 /* fill in the start/end if not specified */
559 if (PROFILE_PC_END (data
) == 0)
561 PROFILE_PC_START (data
) = STATE_TEXT_START (sd
);
562 PROFILE_PC_END (data
) = STATE_TEXT_END (sd
);
564 /* Compute the number of buckets if not specified. */
565 if (PROFILE_PC_NR_BUCKETS (data
) == 0)
567 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
568 PROFILE_PC_NR_BUCKETS (data
) = 16;
571 if (PROFILE_PC_END (data
) == 0)
573 /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
574 PROFILE_PC_NR_BUCKETS (data
) =
575 ((1 << (STATE_WATCHPOINTS (sd
)->sizeof_pc
) * (8 - 1))
576 / (PROFILE_PC_BUCKET_SIZE (data
) / 2));
580 PROFILE_PC_NR_BUCKETS (data
) =
581 ((PROFILE_PC_END (data
)
582 - PROFILE_PC_START (data
)
583 + PROFILE_PC_BUCKET_SIZE (data
) - 1)
584 / PROFILE_PC_BUCKET_SIZE (data
));
588 /* Compute the bucket size if not specified. Ensure that it
589 is rounded up to the next power of two */
590 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
592 if (PROFILE_PC_END (data
) == 0)
593 /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
594 bucket_size
= ((1 << ((STATE_WATCHPOINTS (sd
)->sizeof_pc
* 8) - 1))
595 / (PROFILE_PC_NR_BUCKETS (data
) / 2));
597 bucket_size
= ((PROFILE_PC_END (data
)
598 - PROFILE_PC_START (data
)
599 + PROFILE_PC_NR_BUCKETS (data
) - 1)
600 / PROFILE_PC_NR_BUCKETS (data
));
601 PROFILE_PC_SHIFT (data
) = 0;
602 while (bucket_size
> PROFILE_PC_BUCKET_SIZE (data
))
604 PROFILE_PC_SHIFT (data
) += 1;
607 /* Align the end address with bucket size */
608 if (PROFILE_PC_END (data
) != 0)
609 PROFILE_PC_END (data
) = (PROFILE_PC_START (data
)
610 + (PROFILE_PC_BUCKET_SIZE (data
)
611 * PROFILE_PC_NR_BUCKETS (data
)));
612 /* create the relevant buffers */
613 PROFILE_PC_COUNT (data
) =
614 NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data
) + 1);
615 PROFILE_PC_EVENT (data
) =
616 sim_events_schedule (sd
,
617 PROFILE_PC_FREQ (data
),
626 profile_print_pc (sim_cpu
*cpu
, int verbose
)
628 SIM_DESC sd
= CPU_STATE (cpu
);
629 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
635 if (PROFILE_PC_COUNT (profile
) == 0)
638 profile_printf (sd
, cpu
, "Program Counter Statistics:\n\n");
640 /* First pass over data computes various things. */
643 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
645 total
+= PROFILE_PC_COUNT (profile
) [i
];
646 if (PROFILE_PC_COUNT (profile
) [i
] > max_val
)
647 max_val
= PROFILE_PC_COUNT (profile
) [i
];
650 profile_printf (sd
, cpu
, " Total samples: %s\n",
652 profile_printf (sd
, cpu
, " Granularity: %s bytes per bucket\n",
653 COMMAS (PROFILE_PC_BUCKET_SIZE (profile
)));
654 profile_printf (sd
, cpu
, " Size: %s buckets\n",
655 COMMAS (PROFILE_PC_NR_BUCKETS (profile
)));
656 profile_printf (sd
, cpu
, " Frequency: %s cycles per sample\n",
657 COMMAS (PROFILE_PC_FREQ (profile
)));
659 if (PROFILE_PC_END (profile
) != 0)
660 profile_printf (sd
, cpu
, " Range: 0x%lx 0x%lx\n",
661 (long) PROFILE_PC_START (profile
),
662 (long) PROFILE_PC_END (profile
));
664 if (verbose
&& max_val
!= 0)
666 /* Now we can print the histogram. */
667 profile_printf (sd
, cpu
, "\n");
668 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
670 if (PROFILE_PC_COUNT (profile
) [i
] != 0)
672 profile_printf (sd
, cpu
, " ");
673 if (i
== PROFILE_PC_NR_BUCKETS (profile
))
674 profile_printf (sd
, cpu
, "%10s:", "overflow");
676 profile_printf (sd
, cpu
, "0x%08lx:",
677 (long) (PROFILE_PC_START (profile
)
678 + (i
* PROFILE_PC_BUCKET_SIZE (profile
))));
679 profile_printf (sd
, cpu
, " %*s",
680 max_val
< 10000 ? 5 : 10,
681 COMMAS (PROFILE_PC_COUNT (profile
) [i
]));
682 profile_printf (sd
, cpu
, " %4.1f",
683 (PROFILE_PC_COUNT (profile
) [i
] * 100.0) / total
);
684 profile_printf (sd
, cpu
, ": ");
685 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
686 PROFILE_PC_COUNT (profile
) [i
],
688 profile_printf (sd
, cpu
, "\n");
693 /* dump the histogram to the file "gmon.out" using BSD's gprof file
695 /* Since a profile data file is in the native format of the host on
696 which the profile is being, endian issues are not considered in
698 /* FIXME: Is this the best place for this code? */
700 FILE *pf
= fopen ("gmon.out", "wb");
703 sim_io_eprintf (sd
, "Failed to open \"gmon.out\" profile file\n");
707 /* FIXME: what if the target has a 64 bit PC? */
708 unsigned32 header
[3];
710 if (PROFILE_PC_END (profile
) != 0)
712 header
[0] = PROFILE_PC_START (profile
);
713 header
[1] = PROFILE_PC_END (profile
);
720 /* size of sample buffer (+ header) */
721 header
[2] = PROFILE_PC_NR_BUCKETS (profile
) * 2 + sizeof (header
);
723 /* Header must be written out in target byte order. */
728 ok
= fwrite (&header
, sizeof (header
), 1, pf
);
730 ok
&& (loop
< PROFILE_PC_NR_BUCKETS (profile
));
734 if (PROFILE_PC_COUNT (profile
) [loop
] >= 0xffff)
737 sample
= PROFILE_PC_COUNT (profile
) [loop
];
739 ok
= fwrite (&sample
, sizeof (sample
), 1, pf
);
742 sim_io_eprintf (sd
, "Failed to write to \"gmon.out\" profile file\n");
747 profile_printf (sd
, cpu
, "\n");
752 /* Summary printing support. */
754 #if WITH_PROFILE_INSN_P
757 profile_insn_init (SIM_DESC sd
)
761 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
763 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
765 if (CPU_MAX_INSNS (cpu
) > 0)
766 PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu
)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu
));
773 profile_print_insn (sim_cpu
*cpu
, int verbose
)
775 unsigned int i
, n
, total
, max_val
, max_name_len
;
776 SIM_DESC sd
= CPU_STATE (cpu
);
777 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
780 /* If MAX_INSNS not set, insn profiling isn't supported. */
781 if (CPU_MAX_INSNS (cpu
) == 0)
784 profile_printf (sd
, cpu
, "Instruction Statistics");
785 #ifdef SIM_HAVE_ADDR_RANGE
786 if (PROFILE_RANGE (data
)->ranges
)
787 profile_printf (sd
, cpu
, " (for selected address range(s))");
789 profile_printf (sd
, cpu
, "\n\n");
791 /* First pass over data computes various things. */
795 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
797 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
801 total
+= PROFILE_INSN_COUNT (data
) [i
];
802 if (PROFILE_INSN_COUNT (data
) [i
] > max_val
)
803 max_val
= PROFILE_INSN_COUNT (data
) [i
];
805 if (n
> max_name_len
)
808 /* set the total insn count, in case client is being lazy */
809 if (! PROFILE_TOTAL_INSN_COUNT (data
))
810 PROFILE_TOTAL_INSN_COUNT (data
) = total
;
812 profile_printf (sd
, cpu
, " Total: %s insns\n", COMMAS (total
));
814 if (verbose
&& max_val
!= 0)
816 /* Now we can print the histogram. */
817 profile_printf (sd
, cpu
, "\n");
818 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
820 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
824 if (PROFILE_INSN_COUNT (data
) [i
] != 0)
826 profile_printf (sd
, cpu
, " %*s: %*s: ",
828 max_val
< 10000 ? 5 : 10,
829 COMMAS (PROFILE_INSN_COUNT (data
) [i
]));
830 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
831 PROFILE_INSN_COUNT (data
) [i
],
833 profile_printf (sd
, cpu
, "\n");
838 profile_printf (sd
, cpu
, "\n");
843 #if WITH_PROFILE_MEMORY_P
846 profile_print_memory (sim_cpu
*cpu
, int verbose
)
849 unsigned int total_read
, total_write
;
850 unsigned int max_val
, max_name_len
;
851 /* FIXME: Need to add smp support. */
852 SIM_DESC sd
= CPU_STATE (cpu
);
853 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
856 profile_printf (sd
, cpu
, "Memory Access Statistics\n\n");
858 /* First pass over data computes various things. */
859 max_val
= total_read
= total_write
= max_name_len
= 0;
860 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
862 total_read
+= PROFILE_READ_COUNT (data
) [i
];
863 total_write
+= PROFILE_WRITE_COUNT (data
) [i
];
864 if (PROFILE_READ_COUNT (data
) [i
] > max_val
)
865 max_val
= PROFILE_READ_COUNT (data
) [i
];
866 if (PROFILE_WRITE_COUNT (data
) [i
] > max_val
)
867 max_val
= PROFILE_WRITE_COUNT (data
) [i
];
868 n
= strlen (MODE_NAME (i
));
869 if (n
> max_name_len
)
873 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
874 profile_printf (sd
, cpu
, " Total read: %s accesses\n",
875 COMMAS (total_read
));
876 profile_printf (sd
, cpu
, " Total write: %s accesses\n",
877 COMMAS (total_write
));
879 if (verbose
&& max_val
!= 0)
881 /* FIXME: Need to separate instruction fetches from data fetches
882 as the former swamps the latter. */
883 /* Now we can print the histogram. */
884 profile_printf (sd
, cpu
, "\n");
885 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
887 if (PROFILE_READ_COUNT (data
) [i
] != 0)
889 profile_printf (sd
, cpu
, " %*s read: %*s: ",
890 max_name_len
, MODE_NAME (i
),
891 max_val
< 10000 ? 5 : 10,
892 COMMAS (PROFILE_READ_COUNT (data
) [i
]));
893 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
894 PROFILE_READ_COUNT (data
) [i
],
896 profile_printf (sd
, cpu
, "\n");
898 if (PROFILE_WRITE_COUNT (data
) [i
] != 0)
900 profile_printf (sd
, cpu
, " %*s write: %*s: ",
901 max_name_len
, MODE_NAME (i
),
902 max_val
< 10000 ? 5 : 10,
903 COMMAS (PROFILE_WRITE_COUNT (data
) [i
]));
904 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
905 PROFILE_WRITE_COUNT (data
) [i
],
907 profile_printf (sd
, cpu
, "\n");
912 profile_printf (sd
, cpu
, "\n");
917 #if WITH_PROFILE_CORE_P
920 profile_print_core (sim_cpu
*cpu
, int verbose
)
923 unsigned int max_val
;
924 /* FIXME: Need to add smp support. */
925 SIM_DESC sd
= CPU_STATE (cpu
);
926 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
929 profile_printf (sd
, cpu
, "CORE Statistics\n\n");
931 /* First pass over data computes various things. */
936 for (map
= 0; map
< nr_maps
; map
++)
938 total
+= PROFILE_CORE_COUNT (data
) [map
];
939 if (PROFILE_CORE_COUNT (data
) [map
] > max_val
)
940 max_val
= PROFILE_CORE_COUNT (data
) [map
];
944 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
945 profile_printf (sd
, cpu
, " Total: %s accesses\n",
948 if (verbose
&& max_val
!= 0)
951 /* Now we can print the histogram. */
952 profile_printf (sd
, cpu
, "\n");
953 for (map
= 0; map
< nr_maps
; map
++)
955 if (PROFILE_CORE_COUNT (data
) [map
] != 0)
957 profile_printf (sd
, cpu
, "%10s:", map_to_str (map
));
958 profile_printf (sd
, cpu
, "%*s: ",
959 max_val
< 10000 ? 5 : 10,
960 COMMAS (PROFILE_CORE_COUNT (data
) [map
]));
961 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
962 PROFILE_CORE_COUNT (data
) [map
],
964 profile_printf (sd
, cpu
, "\n");
969 profile_printf (sd
, cpu
, "\n");
974 #if WITH_PROFILE_MODEL_P
977 profile_print_model (sim_cpu
*cpu
, int verbose
)
979 SIM_DESC sd
= CPU_STATE (cpu
);
980 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
981 unsigned long cti_stall_cycles
= PROFILE_MODEL_CTI_STALL_CYCLES (data
);
982 unsigned long load_stall_cycles
= PROFILE_MODEL_LOAD_STALL_CYCLES (data
);
983 unsigned long total_cycles
= PROFILE_MODEL_TOTAL_CYCLES (data
);
986 profile_printf (sd
, cpu
, "Model %s Timing Information",
987 MODEL_NAME (CPU_MODEL (cpu
)));
988 #ifdef SIM_HAVE_ADDR_RANGE
989 if (PROFILE_RANGE (data
)->ranges
)
990 profile_printf (sd
, cpu
, " (for selected address range(s))");
992 profile_printf (sd
, cpu
, "\n\n");
993 profile_printf (sd
, cpu
, " %-*s %s\n",
994 PROFILE_LABEL_WIDTH
, "Taken branches:",
995 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data
)));
996 profile_printf (sd
, cpu
, " %-*s %s\n",
997 PROFILE_LABEL_WIDTH
, "Untaken branches:",
998 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data
)));
999 profile_printf (sd
, cpu
, " %-*s %s\n",
1000 PROFILE_LABEL_WIDTH
, "Cycles stalled due to branches:",
1001 COMMAS (cti_stall_cycles
));
1002 profile_printf (sd
, cpu
, " %-*s %s\n",
1003 PROFILE_LABEL_WIDTH
, "Cycles stalled due to loads:",
1004 COMMAS (load_stall_cycles
));
1005 profile_printf (sd
, cpu
, " %-*s %s\n",
1006 PROFILE_LABEL_WIDTH
, "Total cycles (*approximate*):",
1007 COMMAS (total_cycles
));
1008 profile_printf (sd
, cpu
, "\n");
1014 sim_profile_print_bar (SIM_DESC sd
, sim_cpu
*cpu
, unsigned int width
,
1015 unsigned int val
, unsigned int max_val
)
1017 unsigned int i
, count
;
1019 count
= ((double) val
/ (double) max_val
) * (double) width
;
1021 for (i
= 0; i
< count
; ++i
)
1022 profile_printf (sd
, cpu
, "*");
1025 /* Print the simulator's execution speed for CPU. */
1028 profile_print_speed (sim_cpu
*cpu
)
1030 SIM_DESC sd
= CPU_STATE (cpu
);
1031 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1032 unsigned long milliseconds
= sim_events_elapsed_time (sd
);
1033 unsigned long total
= PROFILE_TOTAL_INSN_COUNT (data
);
1038 profile_printf (sd
, cpu
, "Simulator Execution Speed\n\n");
1041 profile_printf (sd
, cpu
, " Total instructions: %s\n", COMMAS (total
));
1043 if (milliseconds
< 1000)
1044 profile_printf (sd
, cpu
, " Total execution time: < 1 second\n\n");
1047 /* The printing of the time rounded to 2 decimal places makes the speed
1048 calculation seem incorrect [even though it is correct]. So round
1049 MILLISECONDS first. This can marginally affect the result, but it's
1050 better that the user not perceive there's a math error. */
1051 secs
= (double) milliseconds
/ 1000;
1052 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1053 profile_printf (sd
, cpu
, " Total execution time : %.2f seconds\n", secs
);
1054 /* Don't confuse things with data that isn't useful.
1055 If we ran for less than 2 seconds, only use the data if we
1056 executed more than 100,000 insns. */
1057 if (secs
>= 2 || total
>= 100000)
1058 profile_printf (sd
, cpu
, " Simulator speed: %s insns/second\n",
1059 COMMAS ((unsigned long) ((double) total
/ secs
)));
1062 /* Print simulated execution time if the cpu frequency has been specified. */
1063 clock
= PROFILE_CPU_FREQ (data
);
1066 if (clock
>= 1000000)
1067 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f MHz\n",
1070 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f Hz\n", clock
);
1072 #if WITH_PROFILE_MODEL_P
1073 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1075 /* The printing of the time rounded to 2 decimal places makes the
1076 speed calculation seem incorrect [even though it is correct].
1077 So round SECS first. This can marginally affect the result,
1078 but it's better that the user not perceive there's a math
1080 secs
= PROFILE_MODEL_TOTAL_CYCLES (data
) / clock
;
1081 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1082 profile_printf (sd
, cpu
, " Simulated execution time: %.2f seconds\n",
1085 #endif /* WITH_PROFILE_MODEL_P */
1089 #ifdef SIM_HAVE_ADDR_RANGE
1090 /* Print selected address ranges. */
1093 profile_print_addr_ranges (sim_cpu
*cpu
)
1095 ADDR_SUBRANGE
*asr
= PROFILE_RANGE (CPU_PROFILE_DATA (cpu
))->ranges
;
1096 SIM_DESC sd
= CPU_STATE (cpu
);
1100 profile_printf (sd
, cpu
, "Selected address ranges\n\n");
1103 profile_printf (sd
, cpu
, " 0x%lx - 0x%lx\n",
1104 (long) asr
->start
, (long) asr
->end
);
1107 profile_printf (sd
, cpu
, "\n");
1112 /* Top level function to print all summary profile information.
1113 It is [currently] intended that all such data is printed by this function.
1114 I'd rather keep it all in one place for now. To that end, MISC_CPU and
1115 MISC are callbacks used to print any miscellaneous data.
1117 One might want to add a user option that allows printing by type or by cpu
1118 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
1119 This may be a case of featuritis so it's currently left out.
1121 Note that results are indented two spaces to distinguish them from
1125 profile_info (SIM_DESC sd
, int verbose
)
1128 int print_title_p
= 0;
1130 /* Only print the title if some data has been collected. */
1131 /* ??? Why don't we just exit if no data collected? */
1132 /* FIXME: If the number of processors can be selected on the command line,
1133 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
1135 for (c
= 0; c
< MAX_NR_PROCESSORS
&& !print_title_p
; ++c
)
1137 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1138 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1140 for (i
= 0; i
< MAX_PROFILE_VALUES
; ++i
)
1141 if (PROFILE_FLAGS (data
) [i
])
1143 profile_printf (sd
, cpu
, "Summary profiling results:\n\n");
1149 /* Loop, cpu by cpu, printing results. */
1151 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
1153 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1154 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1156 if (MAX_NR_PROCESSORS
> 1
1158 #if WITH_PROFILE_INSN_P
1159 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
]
1161 #if WITH_PROFILE_MEMORY_P
1162 || PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
]
1164 #if WITH_PROFILE_CORE_P
1165 || PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
]
1167 #if WITH_PROFILE_MODEL_P
1168 || PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
]
1170 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1171 || PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
]
1173 #if WITH_PROFILE_PC_P
1174 || PROFILE_FLAGS (data
) [PROFILE_PC_IDX
]
1178 profile_printf (sd
, cpu
, "CPU %d\n\n", c
);
1181 #ifdef SIM_HAVE_ADDR_RANGE
1183 && (PROFILE_INSN_P (cpu
)
1184 || PROFILE_MODEL_P (cpu
)))
1185 profile_print_addr_ranges (cpu
);
1188 #if WITH_PROFILE_INSN_P
1189 if (PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1190 profile_print_insn (cpu
, verbose
);
1193 #if WITH_PROFILE_MEMORY_P
1194 if (PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
])
1195 profile_print_memory (cpu
, verbose
);
1198 #if WITH_PROFILE_CORE_P
1199 if (PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
])
1200 profile_print_core (cpu
, verbose
);
1203 #if WITH_PROFILE_MODEL_P
1204 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1205 profile_print_model (cpu
, verbose
);
1208 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1209 if (PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
])
1210 scache_print_profile (cpu
, verbose
);
1213 #if WITH_PROFILE_PC_P
1214 if (PROFILE_FLAGS (data
) [PROFILE_PC_IDX
])
1215 profile_print_pc (cpu
, verbose
);
1218 /* Print cpu-specific data before the execution speed. */
1219 if (PROFILE_INFO_CPU_CALLBACK (data
) != NULL
)
1220 PROFILE_INFO_CPU_CALLBACK (data
) (cpu
, verbose
);
1222 /* Always try to print execution time and speed. */
1224 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1225 profile_print_speed (cpu
);
1228 /* Finally print non-cpu specific miscellaneous data. */
1229 if (STATE_PROFILE_INFO_CALLBACK (sd
))
1230 STATE_PROFILE_INFO_CALLBACK (sd
) (sd
, verbose
);
1234 /* Install profiling support in the simulator. */
1237 profile_install (SIM_DESC sd
)
1241 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1242 sim_add_option_table (sd
, NULL
, profile_options
);
1243 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1244 memset (CPU_PROFILE_DATA (STATE_CPU (sd
, i
)), 0,
1245 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd
, i
))));
1246 #if WITH_PROFILE_INSN_P
1247 sim_module_add_init_fn (sd
, profile_insn_init
);
1249 #if WITH_PROFILE_PC_P
1250 sim_module_add_uninstall_fn (sd
, profile_pc_uninstall
);
1251 sim_module_add_init_fn (sd
, profile_pc_init
);
1253 sim_module_add_init_fn (sd
, profile_init
);
1254 sim_module_add_uninstall_fn (sd
, profile_uninstall
);
1255 sim_module_add_info_fn (sd
, profile_info
);
1260 profile_init (SIM_DESC sd
)
1262 #ifdef SIM_HAVE_ADDR_RANGE
1263 /* Check if a range has been specified without specifying what to
1268 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1270 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1272 if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)))
1273 && ! (PROFILE_INSN_P (cpu
)
1274 || PROFILE_MODEL_P (cpu
)))
1276 sim_io_eprintf_cpu (cpu
, "Profiling address range specified without --profile-insn or --profile-model.\n");
1277 sim_io_eprintf_cpu (cpu
, "Address range ignored.\n");
1278 sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
1279 0, ~ (address_word
) 0);
1289 profile_uninstall (SIM_DESC sd
)
1293 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1295 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1296 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1298 if (PROFILE_FILE (data
) != NULL
)
1300 /* If output from different cpus is going to the same file,
1301 avoid closing the file twice. */
1302 for (j
= 0; j
< i
; ++j
)
1303 if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, j
)))
1304 == PROFILE_FILE (data
))
1307 fclose (PROFILE_FILE (data
));
1310 if (PROFILE_INSN_COUNT (data
) != NULL
)
1311 free (PROFILE_INSN_COUNT (data
));