1 /* Default profiling support.
2 Copyright (C) 1996, 1997, 1998, 2000, 2001, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
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 #define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n))
41 static MODULE_INIT_FN profile_init
;
42 static MODULE_UNINSTALL_FN profile_uninstall
;
44 static DECLARE_OPTION_HANDLER (profile_option_handler
);
47 OPTION_PROFILE_INSN
= OPTION_START
,
48 OPTION_PROFILE_MEMORY
,
52 OPTION_PROFILE_CPU_FREQUENCY
,
54 OPTION_PROFILE_PC_RANGE
,
55 OPTION_PROFILE_PC_GRANULARITY
,
57 OPTION_PROFILE_FUNCTION
60 static const OPTION profile_options
[] = {
61 { {"profile", optional_argument
, NULL
, 'p'},
62 'p', "on|off", "Perform profiling",
63 profile_option_handler
, NULL
},
64 { {"profile-insn", optional_argument
, NULL
, OPTION_PROFILE_INSN
},
65 '\0', "on|off", "Perform instruction profiling",
66 profile_option_handler
, NULL
},
67 { {"profile-memory", optional_argument
, NULL
, OPTION_PROFILE_MEMORY
},
68 '\0', "on|off", "Perform memory profiling",
69 profile_option_handler
, NULL
},
70 { {"profile-core", optional_argument
, NULL
, OPTION_PROFILE_CORE
},
71 '\0', "on|off", "Perform CORE profiling",
72 profile_option_handler
, NULL
},
73 { {"profile-model", optional_argument
, NULL
, OPTION_PROFILE_MODEL
},
74 '\0', "on|off", "Perform model profiling",
75 profile_option_handler
, NULL
},
76 { {"profile-cpu-frequency", required_argument
, NULL
,
77 OPTION_PROFILE_CPU_FREQUENCY
},
78 '\0', "CPU FREQUENCY", "Specify the speed of the simulated cpu clock",
79 profile_option_handler
, NULL
},
81 { {"profile-file", required_argument
, NULL
, OPTION_PROFILE_FILE
},
82 '\0', "FILE NAME", "Specify profile output file",
83 profile_option_handler
, NULL
},
85 { {"profile-pc", optional_argument
, NULL
, OPTION_PROFILE_PC
},
86 '\0', "on|off", "Perform PC profiling",
87 profile_option_handler
, NULL
},
88 { {"profile-pc-frequency", required_argument
, NULL
, 'F'},
89 'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency",
90 profile_option_handler
, NULL
},
91 { {"profile-pc-size", required_argument
, NULL
, 'S'},
92 'S', "PC PROFILE SIZE", "Specify PC profiling size",
93 profile_option_handler
, NULL
},
94 { {"profile-pc-granularity", required_argument
, NULL
, OPTION_PROFILE_PC_GRANULARITY
},
95 '\0', "PC PROFILE GRANULARITY", "Specify PC profiling sample coverage",
96 profile_option_handler
, NULL
},
97 { {"profile-pc-range", required_argument
, NULL
, OPTION_PROFILE_PC_RANGE
},
98 '\0', "BASE,BOUND", "Specify PC profiling address range",
99 profile_option_handler
, NULL
},
101 #ifdef SIM_HAVE_ADDR_RANGE
102 { {"profile-range", required_argument
, NULL
, OPTION_PROFILE_RANGE
},
103 '\0', "START,END", "Specify range of addresses for instruction and model profiling",
104 profile_option_handler
, NULL
},
106 { {"profile-function", required_argument
, NULL
, OPTION_PROFILE_FUNCTION
},
107 '\0', "FUNCTION", "Specify function to profile",
108 profile_option_handler
, NULL
},
112 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
115 /* Set/reset the profile options indicated in MASK. */
118 set_profile_option_mask (SIM_DESC sd
, const char *name
, int mask
, const char *arg
)
126 if (strcmp (arg
, "yes") == 0
127 || strcmp (arg
, "on") == 0
128 || strcmp (arg
, "1") == 0)
130 else if (strcmp (arg
, "no") == 0
131 || strcmp (arg
, "off") == 0
132 || strcmp (arg
, "0") == 0)
136 sim_io_eprintf (sd
, "Argument `%s' for `--profile%s' invalid, one of `on', `off', `yes', `no' expected\n", arg
, name
);
141 /* update applicable profile bits */
142 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
144 if ((mask
& (1 << profile_nr
)) == 0)
147 #if 0 /* see sim-trace.c, set flags in STATE here if/when there are any */
148 /* Set non-cpu specific values. */
156 /* Set cpu values. */
157 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
159 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
] = profile_val
;
163 /* Re-compute the cpu profile summary. */
166 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
167 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
171 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
173 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 0;
174 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
176 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
])
178 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
188 /* Set one profile option based on its IDX value.
189 Not static as cgen-scache.c uses it. */
192 sim_profile_set_option (SIM_DESC sd
, const char *name
, int idx
, const char *arg
)
194 return set_profile_option_mask (sd
, name
, 1 << idx
, arg
);
198 parse_frequency (SIM_DESC sd
, const char *arg
, unsigned long *freq
)
201 /* First, parse a decimal number. */
206 for (/**/; *ch
!= '\0'; ++ch
)
210 *freq
= *freq
* 10 + (*ch
- '0');
213 /* Accept KHz, MHz or Hz as a suffix. */
214 if (tolower (*ch
) == 'm')
219 else if (tolower (*ch
) == 'k')
225 if (tolower (*ch
) == 'h')
228 if (tolower (*ch
) == 'z')
235 sim_io_eprintf (sd
, "Invalid argument for --profile-cpu-frequency: %s\n",
245 profile_option_handler (SIM_DESC sd
,
253 /* FIXME: Need to handle `cpu' arg. */
259 sim_io_eprintf (sd
, "Profiling not compiled in, `-p' ignored\n");
261 return set_profile_option_mask (sd
, "profile", PROFILE_USEFUL_MASK
,
265 case OPTION_PROFILE_INSN
:
266 if (WITH_PROFILE_INSN_P
)
267 return sim_profile_set_option (sd
, "-insn", PROFILE_INSN_IDX
, arg
);
269 sim_io_eprintf (sd
, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
272 case OPTION_PROFILE_MEMORY
:
273 if (WITH_PROFILE_MEMORY_P
)
274 return sim_profile_set_option (sd
, "-memory", PROFILE_MEMORY_IDX
, arg
);
276 sim_io_eprintf (sd
, "Memory profiling not compiled in, `--profile-memory' ignored\n");
279 case OPTION_PROFILE_CORE
:
280 if (WITH_PROFILE_CORE_P
)
281 return sim_profile_set_option (sd
, "-core", PROFILE_CORE_IDX
, arg
);
283 sim_io_eprintf (sd
, "CORE profiling not compiled in, `--profile-core' ignored\n");
286 case OPTION_PROFILE_MODEL
:
287 if (WITH_PROFILE_MODEL_P
)
288 return sim_profile_set_option (sd
, "-model", PROFILE_MODEL_IDX
, arg
);
290 sim_io_eprintf (sd
, "Model profiling not compiled in, `--profile-model' ignored\n");
293 case OPTION_PROFILE_CPU_FREQUENCY
:
296 SIM_RC rc
= parse_frequency (sd
, arg
, &val
);
299 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
300 PROFILE_CPU_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
,cpu_nr
))) = val
;
305 case OPTION_PROFILE_FILE
:
306 /* FIXME: Might want this to apply to pc profiling only,
307 or have two profile file options. */
309 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-file' ignored\n");
312 FILE *f
= fopen (arg
, "w");
316 sim_io_eprintf (sd
, "Unable to open profile output file `%s'\n", arg
);
319 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
320 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = f
;
324 case OPTION_PROFILE_PC
:
325 if (WITH_PROFILE_PC_P
)
326 return sim_profile_set_option (sd
, "-pc", PROFILE_PC_IDX
, arg
);
328 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc' ignored\n");
332 if (WITH_PROFILE_PC_P
)
334 /* FIXME: Validate arg. */
335 int val
= atoi (arg
);
336 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
337 PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
338 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
339 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
342 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
346 if (WITH_PROFILE_PC_P
)
348 /* FIXME: Validate arg. */
349 int val
= atoi (arg
);
350 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
351 PROFILE_PC_NR_BUCKETS (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
352 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
353 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
356 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
359 case OPTION_PROFILE_PC_GRANULARITY
:
360 if (WITH_PROFILE_PC_P
)
363 int val
= atoi (arg
);
364 /* check that the granularity is a power of two */
366 while (val
> (1 << shift
))
370 if (val
!= (1 << shift
))
372 sim_io_eprintf (sd
, "PC profiling granularity not a power of two\n");
377 sim_io_eprintf (sd
, "PC profiling granularity too small");
380 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
381 PROFILE_PC_SHIFT (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = shift
;
382 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
383 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
386 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
389 case OPTION_PROFILE_PC_RANGE
:
390 if (WITH_PROFILE_PC_P
)
392 /* FIXME: Validate args */
396 base
= strtoul (chp
, &chp
, 0);
399 sim_io_eprintf (sd
, "--profile-pc-range missing BOUND argument\n");
402 bound
= strtoul (chp
+ 1, NULL
, 0);
403 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
405 PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = base
;
406 PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = bound
;
408 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
409 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
412 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
415 #ifdef SIM_HAVE_ADDR_RANGE
416 case OPTION_PROFILE_RANGE
:
420 unsigned long start
,end
;
421 start
= strtoul (chp
, &chp
, 0);
424 sim_io_eprintf (sd
, "--profile-range missing END argument\n");
427 end
= strtoul (chp
+ 1, NULL
, 0);
428 /* FIXME: Argument validation. */
430 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
433 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
434 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))),
438 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-range' ignored\n");
441 case OPTION_PROFILE_FUNCTION
:
444 /*wip: need to compute function range given name*/
447 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-function' ignored\n");
449 #endif /* SIM_HAVE_ADDR_RANGE */
455 /* Profiling output hooks. */
458 profile_vprintf (SIM_DESC sd
, sim_cpu
*cpu
, const char *fmt
, va_list ap
)
460 FILE *fp
= PROFILE_FILE (CPU_PROFILE_DATA (cpu
));
462 /* If an output file was given, redirect output to that. */
464 vfprintf (fp
, fmt
, ap
);
466 sim_io_evprintf (sd
, fmt
, ap
);
469 __attribute__ ((format (printf
, 3, 4)))
471 profile_printf (SIM_DESC sd
, sim_cpu
*cpu
, const char *fmt
, ...)
476 profile_vprintf (sd
, cpu
, fmt
, ap
);
480 /* PC profiling support */
482 #if WITH_PROFILE_PC_P
485 profile_pc_cleanup (SIM_DESC sd
)
488 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
490 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
491 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
492 if (PROFILE_PC_COUNT (data
) != NULL
)
493 zfree (PROFILE_PC_COUNT (data
));
494 PROFILE_PC_COUNT (data
) = NULL
;
495 if (PROFILE_PC_EVENT (data
) != NULL
)
496 sim_events_deschedule (sd
, PROFILE_PC_EVENT (data
));
497 PROFILE_PC_EVENT (data
) = NULL
;
503 profile_pc_uninstall (SIM_DESC sd
)
505 profile_pc_cleanup (sd
);
509 profile_pc_event (SIM_DESC sd
,
512 sim_cpu
*cpu
= (sim_cpu
*) data
;
513 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
516 switch (STATE_WATCHPOINTS (sd
)->sizeof_pc
)
518 case 2: pc
= *(unsigned_2
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
519 case 4: pc
= *(unsigned_4
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
520 case 8: pc
= *(unsigned_8
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
523 i
= (pc
- PROFILE_PC_START (profile
)) >> PROFILE_PC_SHIFT (profile
);
524 if (i
< PROFILE_PC_NR_BUCKETS (profile
))
525 PROFILE_PC_COUNT (profile
) [i
] += 1; /* Overflow? */
527 PROFILE_PC_COUNT (profile
) [PROFILE_PC_NR_BUCKETS (profile
)] += 1;
528 PROFILE_PC_EVENT (profile
) =
529 sim_events_schedule (sd
, PROFILE_PC_FREQ (profile
), profile_pc_event
, cpu
);
533 profile_pc_init (SIM_DESC sd
)
536 profile_pc_cleanup (sd
);
537 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
539 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
540 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
541 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
]
542 && STATE_WATCHPOINTS (sd
)->pc
!= NULL
)
545 /* fill in the frequency if not specified */
546 if (PROFILE_PC_FREQ (data
) == 0)
547 PROFILE_PC_FREQ (data
) = 257;
548 /* fill in the start/end if not specified */
549 if (PROFILE_PC_END (data
) == 0)
551 PROFILE_PC_START (data
) = STATE_TEXT_START (sd
);
552 PROFILE_PC_END (data
) = STATE_TEXT_END (sd
);
554 /* Compute the number of buckets if not specified. */
555 if (PROFILE_PC_NR_BUCKETS (data
) == 0)
557 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
558 PROFILE_PC_NR_BUCKETS (data
) = 16;
561 if (PROFILE_PC_END (data
) == 0)
563 /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
564 PROFILE_PC_NR_BUCKETS (data
) =
565 ((1 << (STATE_WATCHPOINTS (sd
)->sizeof_pc
) * (8 - 1))
566 / (PROFILE_PC_BUCKET_SIZE (data
) / 2));
570 PROFILE_PC_NR_BUCKETS (data
) =
571 ((PROFILE_PC_END (data
)
572 - PROFILE_PC_START (data
)
573 + PROFILE_PC_BUCKET_SIZE (data
) - 1)
574 / PROFILE_PC_BUCKET_SIZE (data
));
578 /* Compute the bucket size if not specified. Ensure that it
579 is rounded up to the next power of two */
580 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
582 if (PROFILE_PC_END (data
) == 0)
583 /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
584 bucket_size
= ((1 << ((STATE_WATCHPOINTS (sd
)->sizeof_pc
* 8) - 1))
585 / (PROFILE_PC_NR_BUCKETS (data
) / 2));
587 bucket_size
= ((PROFILE_PC_END (data
)
588 - PROFILE_PC_START (data
)
589 + PROFILE_PC_NR_BUCKETS (data
) - 1)
590 / PROFILE_PC_NR_BUCKETS (data
));
591 PROFILE_PC_SHIFT (data
) = 0;
592 while (bucket_size
> PROFILE_PC_BUCKET_SIZE (data
))
594 PROFILE_PC_SHIFT (data
) += 1;
597 /* Align the end address with bucket size */
598 if (PROFILE_PC_END (data
) != 0)
599 PROFILE_PC_END (data
) = (PROFILE_PC_START (data
)
600 + (PROFILE_PC_BUCKET_SIZE (data
)
601 * PROFILE_PC_NR_BUCKETS (data
)));
602 /* create the relevant buffers */
603 PROFILE_PC_COUNT (data
) =
604 NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data
) + 1);
605 PROFILE_PC_EVENT (data
) =
606 sim_events_schedule (sd
,
607 PROFILE_PC_FREQ (data
),
616 profile_print_pc (sim_cpu
*cpu
, int verbose
)
618 SIM_DESC sd
= CPU_STATE (cpu
);
619 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
625 if (PROFILE_PC_COUNT (profile
) == 0)
628 profile_printf (sd
, cpu
, "Program Counter Statistics:\n\n");
630 /* First pass over data computes various things. */
633 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
635 total
+= PROFILE_PC_COUNT (profile
) [i
];
636 if (PROFILE_PC_COUNT (profile
) [i
] > max_val
)
637 max_val
= PROFILE_PC_COUNT (profile
) [i
];
640 profile_printf (sd
, cpu
, " Total samples: %s\n",
642 profile_printf (sd
, cpu
, " Granularity: %s bytes per bucket\n",
643 COMMAS (PROFILE_PC_BUCKET_SIZE (profile
)));
644 profile_printf (sd
, cpu
, " Size: %s buckets\n",
645 COMMAS (PROFILE_PC_NR_BUCKETS (profile
)));
646 profile_printf (sd
, cpu
, " Frequency: %s cycles per sample\n",
647 COMMAS (PROFILE_PC_FREQ (profile
)));
649 if (PROFILE_PC_END (profile
) != 0)
650 profile_printf (sd
, cpu
, " Range: 0x%lx 0x%lx\n",
651 (long) PROFILE_PC_START (profile
),
652 (long) PROFILE_PC_END (profile
));
654 if (verbose
&& max_val
!= 0)
656 /* Now we can print the histogram. */
657 profile_printf (sd
, cpu
, "\n");
658 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
660 if (PROFILE_PC_COUNT (profile
) [i
] != 0)
662 profile_printf (sd
, cpu
, " ");
663 if (i
== PROFILE_PC_NR_BUCKETS (profile
))
664 profile_printf (sd
, cpu
, "%10s:", "overflow");
666 profile_printf (sd
, cpu
, "0x%08lx:",
667 (long) (PROFILE_PC_START (profile
)
668 + (i
* PROFILE_PC_BUCKET_SIZE (profile
))));
669 profile_printf (sd
, cpu
, " %*s",
670 max_val
< 10000 ? 5 : 10,
671 COMMAS (PROFILE_PC_COUNT (profile
) [i
]));
672 profile_printf (sd
, cpu
, " %4.1f",
673 (PROFILE_PC_COUNT (profile
) [i
] * 100.0) / total
);
674 profile_printf (sd
, cpu
, ": ");
675 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
676 PROFILE_PC_COUNT (profile
) [i
],
678 profile_printf (sd
, cpu
, "\n");
683 /* dump the histogram to the file "gmon.out" using BSD's gprof file
685 /* Since a profile data file is in the native format of the host on
686 which the profile is being, endian issues are not considered in
688 /* FIXME: Is this the best place for this code? */
690 FILE *pf
= fopen ("gmon.out", "wb");
693 sim_io_eprintf (sd
, "Failed to open \"gmon.out\" profile file\n");
697 /* FIXME: what if the target has a 64 bit PC? */
698 unsigned32 header
[3];
700 if (PROFILE_PC_END (profile
) != 0)
702 header
[0] = PROFILE_PC_START (profile
);
703 header
[1] = PROFILE_PC_END (profile
);
710 /* size of sample buffer (+ header) */
711 header
[2] = PROFILE_PC_NR_BUCKETS (profile
) * 2 + sizeof (header
);
713 /* Header must be written out in target byte order. */
718 ok
= fwrite (&header
, sizeof (header
), 1, pf
);
720 ok
&& (loop
< PROFILE_PC_NR_BUCKETS (profile
));
724 if (PROFILE_PC_COUNT (profile
) [loop
] >= 0xffff)
727 sample
= PROFILE_PC_COUNT (profile
) [loop
];
729 ok
= fwrite (&sample
, sizeof (sample
), 1, pf
);
732 sim_io_eprintf (sd
, "Failed to write to \"gmon.out\" profile file\n");
737 profile_printf (sd
, cpu
, "\n");
742 /* Summary printing support. */
744 #if WITH_PROFILE_INSN_P
747 profile_insn_init (SIM_DESC sd
)
751 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
753 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
755 if (CPU_MAX_INSNS (cpu
) > 0)
756 PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu
)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu
));
763 profile_print_insn (sim_cpu
*cpu
, int verbose
)
765 unsigned int i
, n
, total
, max_val
, max_name_len
;
766 SIM_DESC sd
= CPU_STATE (cpu
);
767 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
770 /* If MAX_INSNS not set, insn profiling isn't supported. */
771 if (CPU_MAX_INSNS (cpu
) == 0)
774 profile_printf (sd
, cpu
, "Instruction Statistics");
775 #ifdef SIM_HAVE_ADDR_RANGE
776 if (PROFILE_RANGE (data
)->ranges
)
777 profile_printf (sd
, cpu
, " (for selected address range(s))");
779 profile_printf (sd
, cpu
, "\n\n");
781 /* First pass over data computes various things. */
785 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
787 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
791 total
+= PROFILE_INSN_COUNT (data
) [i
];
792 if (PROFILE_INSN_COUNT (data
) [i
] > max_val
)
793 max_val
= PROFILE_INSN_COUNT (data
) [i
];
795 if (n
> max_name_len
)
798 /* set the total insn count, in case client is being lazy */
799 if (! PROFILE_TOTAL_INSN_COUNT (data
))
800 PROFILE_TOTAL_INSN_COUNT (data
) = total
;
802 profile_printf (sd
, cpu
, " Total: %s insns\n", COMMAS (total
));
804 if (verbose
&& max_val
!= 0)
806 /* Now we can print the histogram. */
807 profile_printf (sd
, cpu
, "\n");
808 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
810 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
814 if (PROFILE_INSN_COUNT (data
) [i
] != 0)
816 profile_printf (sd
, cpu
, " %*s: %*s: ",
818 max_val
< 10000 ? 5 : 10,
819 COMMAS (PROFILE_INSN_COUNT (data
) [i
]));
820 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
821 PROFILE_INSN_COUNT (data
) [i
],
823 profile_printf (sd
, cpu
, "\n");
828 profile_printf (sd
, cpu
, "\n");
833 #if WITH_PROFILE_MEMORY_P
836 profile_print_memory (sim_cpu
*cpu
, int verbose
)
839 unsigned int total_read
, total_write
;
840 unsigned int max_val
, max_name_len
;
841 /* FIXME: Need to add smp support. */
842 SIM_DESC sd
= CPU_STATE (cpu
);
843 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
846 profile_printf (sd
, cpu
, "Memory Access Statistics\n\n");
848 /* First pass over data computes various things. */
849 max_val
= total_read
= total_write
= max_name_len
= 0;
850 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
852 total_read
+= PROFILE_READ_COUNT (data
) [i
];
853 total_write
+= PROFILE_WRITE_COUNT (data
) [i
];
854 if (PROFILE_READ_COUNT (data
) [i
] > max_val
)
855 max_val
= PROFILE_READ_COUNT (data
) [i
];
856 if (PROFILE_WRITE_COUNT (data
) [i
] > max_val
)
857 max_val
= PROFILE_WRITE_COUNT (data
) [i
];
858 n
= strlen (MODE_NAME (i
));
859 if (n
> max_name_len
)
863 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
864 profile_printf (sd
, cpu
, " Total read: %s accesses\n",
865 COMMAS (total_read
));
866 profile_printf (sd
, cpu
, " Total write: %s accesses\n",
867 COMMAS (total_write
));
869 if (verbose
&& max_val
!= 0)
871 /* FIXME: Need to separate instruction fetches from data fetches
872 as the former swamps the latter. */
873 /* Now we can print the histogram. */
874 profile_printf (sd
, cpu
, "\n");
875 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
877 if (PROFILE_READ_COUNT (data
) [i
] != 0)
879 profile_printf (sd
, cpu
, " %*s read: %*s: ",
880 max_name_len
, MODE_NAME (i
),
881 max_val
< 10000 ? 5 : 10,
882 COMMAS (PROFILE_READ_COUNT (data
) [i
]));
883 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
884 PROFILE_READ_COUNT (data
) [i
],
886 profile_printf (sd
, cpu
, "\n");
888 if (PROFILE_WRITE_COUNT (data
) [i
] != 0)
890 profile_printf (sd
, cpu
, " %*s write: %*s: ",
891 max_name_len
, MODE_NAME (i
),
892 max_val
< 10000 ? 5 : 10,
893 COMMAS (PROFILE_WRITE_COUNT (data
) [i
]));
894 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
895 PROFILE_WRITE_COUNT (data
) [i
],
897 profile_printf (sd
, cpu
, "\n");
902 profile_printf (sd
, cpu
, "\n");
907 #if WITH_PROFILE_CORE_P
910 profile_print_core (sim_cpu
*cpu
, int verbose
)
913 unsigned int max_val
;
914 /* FIXME: Need to add smp support. */
915 SIM_DESC sd
= CPU_STATE (cpu
);
916 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
919 profile_printf (sd
, cpu
, "CORE Statistics\n\n");
921 /* First pass over data computes various things. */
926 for (map
= 0; map
< nr_maps
; map
++)
928 total
+= PROFILE_CORE_COUNT (data
) [map
];
929 if (PROFILE_CORE_COUNT (data
) [map
] > max_val
)
930 max_val
= PROFILE_CORE_COUNT (data
) [map
];
934 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
935 profile_printf (sd
, cpu
, " Total: %s accesses\n",
938 if (verbose
&& max_val
!= 0)
941 /* Now we can print the histogram. */
942 profile_printf (sd
, cpu
, "\n");
943 for (map
= 0; map
< nr_maps
; map
++)
945 if (PROFILE_CORE_COUNT (data
) [map
] != 0)
947 profile_printf (sd
, cpu
, "%10s:", map_to_str (map
));
948 profile_printf (sd
, cpu
, "%*s: ",
949 max_val
< 10000 ? 5 : 10,
950 COMMAS (PROFILE_CORE_COUNT (data
) [map
]));
951 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
952 PROFILE_CORE_COUNT (data
) [map
],
954 profile_printf (sd
, cpu
, "\n");
959 profile_printf (sd
, cpu
, "\n");
964 #if WITH_PROFILE_MODEL_P
967 profile_print_model (sim_cpu
*cpu
, int verbose
)
969 SIM_DESC sd
= CPU_STATE (cpu
);
970 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
971 unsigned long cti_stall_cycles
= PROFILE_MODEL_CTI_STALL_CYCLES (data
);
972 unsigned long load_stall_cycles
= PROFILE_MODEL_LOAD_STALL_CYCLES (data
);
973 unsigned long total_cycles
= PROFILE_MODEL_TOTAL_CYCLES (data
);
976 profile_printf (sd
, cpu
, "Model %s Timing Information",
977 MODEL_NAME (CPU_MODEL (cpu
)));
978 #ifdef SIM_HAVE_ADDR_RANGE
979 if (PROFILE_RANGE (data
)->ranges
)
980 profile_printf (sd
, cpu
, " (for selected address range(s))");
982 profile_printf (sd
, cpu
, "\n\n");
983 profile_printf (sd
, cpu
, " %-*s %s\n",
984 PROFILE_LABEL_WIDTH
, "Taken branches:",
985 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data
)));
986 profile_printf (sd
, cpu
, " %-*s %s\n",
987 PROFILE_LABEL_WIDTH
, "Untaken branches:",
988 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data
)));
989 profile_printf (sd
, cpu
, " %-*s %s\n",
990 PROFILE_LABEL_WIDTH
, "Cycles stalled due to branches:",
991 COMMAS (cti_stall_cycles
));
992 profile_printf (sd
, cpu
, " %-*s %s\n",
993 PROFILE_LABEL_WIDTH
, "Cycles stalled due to loads:",
994 COMMAS (load_stall_cycles
));
995 profile_printf (sd
, cpu
, " %-*s %s\n",
996 PROFILE_LABEL_WIDTH
, "Total cycles (*approximate*):",
997 COMMAS (total_cycles
));
998 profile_printf (sd
, cpu
, "\n");
1004 sim_profile_print_bar (SIM_DESC sd
, sim_cpu
*cpu
, unsigned int width
,
1005 unsigned int val
, unsigned int max_val
)
1007 unsigned int i
, count
;
1009 count
= ((double) val
/ (double) max_val
) * (double) width
;
1011 for (i
= 0; i
< count
; ++i
)
1012 profile_printf (sd
, cpu
, "*");
1015 /* Print the simulator's execution speed for CPU. */
1018 profile_print_speed (sim_cpu
*cpu
)
1020 SIM_DESC sd
= CPU_STATE (cpu
);
1021 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1022 unsigned long milliseconds
= sim_events_elapsed_time (sd
);
1023 unsigned long total
= PROFILE_TOTAL_INSN_COUNT (data
);
1028 profile_printf (sd
, cpu
, "Simulator Execution Speed\n\n");
1031 profile_printf (sd
, cpu
, " Total instructions: %s\n", COMMAS (total
));
1033 if (milliseconds
< 1000)
1034 profile_printf (sd
, cpu
, " Total execution time: < 1 second\n\n");
1037 /* The printing of the time rounded to 2 decimal places makes the speed
1038 calculation seem incorrect [even though it is correct]. So round
1039 MILLISECONDS first. This can marginally affect the result, but it's
1040 better that the user not perceive there's a math error. */
1041 secs
= (double) milliseconds
/ 1000;
1042 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1043 profile_printf (sd
, cpu
, " Total execution time : %.2f seconds\n", secs
);
1044 /* Don't confuse things with data that isn't useful.
1045 If we ran for less than 2 seconds, only use the data if we
1046 executed more than 100,000 insns. */
1047 if (secs
>= 2 || total
>= 100000)
1048 profile_printf (sd
, cpu
, " Simulator speed: %s insns/second\n",
1049 COMMAS ((unsigned long) ((double) total
/ secs
)));
1052 /* Print simulated execution time if the cpu frequency has been specified. */
1053 clock
= PROFILE_CPU_FREQ (data
);
1056 if (clock
>= 1000000)
1057 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f MHz\n",
1060 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f Hz\n", clock
);
1062 #if WITH_PROFILE_MODEL_P
1063 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1065 /* The printing of the time rounded to 2 decimal places makes the
1066 speed calculation seem incorrect [even though it is correct].
1067 So round SECS first. This can marginally affect the result,
1068 but it's better that the user not perceive there's a math
1070 secs
= PROFILE_MODEL_TOTAL_CYCLES (data
) / clock
;
1071 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1072 profile_printf (sd
, cpu
, " Simulated execution time: %.2f seconds\n",
1075 #endif /* WITH_PROFILE_MODEL_P */
1079 #ifdef SIM_HAVE_ADDR_RANGE
1080 /* Print selected address ranges. */
1083 profile_print_addr_ranges (sim_cpu
*cpu
)
1085 ADDR_SUBRANGE
*asr
= PROFILE_RANGE (CPU_PROFILE_DATA (cpu
))->ranges
;
1086 SIM_DESC sd
= CPU_STATE (cpu
);
1090 profile_printf (sd
, cpu
, "Selected address ranges\n\n");
1093 profile_printf (sd
, cpu
, " 0x%lx - 0x%lx\n",
1094 (long) asr
->start
, (long) asr
->end
);
1097 profile_printf (sd
, cpu
, "\n");
1102 /* Top level function to print all summary profile information.
1103 It is [currently] intended that all such data is printed by this function.
1104 I'd rather keep it all in one place for now. To that end, MISC_CPU and
1105 MISC are callbacks used to print any miscellaneous data.
1107 One might want to add a user option that allows printing by type or by cpu
1108 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
1109 This may be a case of featuritis so it's currently left out.
1111 Note that results are indented two spaces to distinguish them from
1115 profile_info (SIM_DESC sd
, int verbose
)
1118 int print_title_p
= 0;
1120 /* Only print the title if some data has been collected. */
1121 /* ??? Why don't we just exit if no data collected? */
1122 /* FIXME: If the number of processors can be selected on the command line,
1123 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
1125 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
1127 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1128 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1130 for (i
= 0; i
< MAX_PROFILE_VALUES
; ++i
)
1131 if (PROFILE_FLAGS (data
) [i
])
1133 profile_printf (sd
, cpu
, "Summary profiling results:\n\n");
1138 /* Loop, cpu by cpu, printing results. */
1140 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
1142 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1143 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1145 if (MAX_NR_PROCESSORS
> 1
1147 #if WITH_PROFILE_INSN_P
1148 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
]
1150 #if WITH_PROFILE_MEMORY_P
1151 || PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
]
1153 #if WITH_PROFILE_CORE_P
1154 || PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
]
1156 #if WITH_PROFILE_MODEL_P
1157 || PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
]
1159 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1160 || PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
]
1162 #if WITH_PROFILE_PC_P
1163 || PROFILE_FLAGS (data
) [PROFILE_PC_IDX
]
1167 profile_printf (sd
, cpu
, "CPU %d\n\n", c
);
1170 #ifdef SIM_HAVE_ADDR_RANGE
1172 && (PROFILE_INSN_P (cpu
)
1173 || PROFILE_MODEL_P (cpu
)))
1174 profile_print_addr_ranges (cpu
);
1177 #if WITH_PROFILE_INSN_P
1178 if (PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1179 profile_print_insn (cpu
, verbose
);
1182 #if WITH_PROFILE_MEMORY_P
1183 if (PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
])
1184 profile_print_memory (cpu
, verbose
);
1187 #if WITH_PROFILE_CORE_P
1188 if (PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
])
1189 profile_print_core (cpu
, verbose
);
1192 #if WITH_PROFILE_MODEL_P
1193 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1194 profile_print_model (cpu
, verbose
);
1197 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1198 if (PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
])
1199 scache_print_profile (cpu
, verbose
);
1202 #if WITH_PROFILE_PC_P
1203 if (PROFILE_FLAGS (data
) [PROFILE_PC_IDX
])
1204 profile_print_pc (cpu
, verbose
);
1207 /* Print cpu-specific data before the execution speed. */
1208 if (PROFILE_INFO_CPU_CALLBACK (data
) != NULL
)
1209 PROFILE_INFO_CPU_CALLBACK (data
) (cpu
, verbose
);
1211 /* Always try to print execution time and speed. */
1213 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1214 profile_print_speed (cpu
);
1217 /* Finally print non-cpu specific miscellaneous data. */
1218 if (STATE_PROFILE_INFO_CALLBACK (sd
))
1219 STATE_PROFILE_INFO_CALLBACK (sd
) (sd
, verbose
);
1223 /* Install profiling support in the simulator. */
1226 profile_install (SIM_DESC sd
)
1230 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1231 sim_add_option_table (sd
, NULL
, profile_options
);
1232 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1233 memset (CPU_PROFILE_DATA (STATE_CPU (sd
, i
)), 0,
1234 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd
, i
))));
1235 #if WITH_PROFILE_INSN_P
1236 sim_module_add_init_fn (sd
, profile_insn_init
);
1238 #if WITH_PROFILE_PC_P
1239 sim_module_add_uninstall_fn (sd
, profile_pc_uninstall
);
1240 sim_module_add_init_fn (sd
, profile_pc_init
);
1242 sim_module_add_init_fn (sd
, profile_init
);
1243 sim_module_add_uninstall_fn (sd
, profile_uninstall
);
1244 sim_module_add_info_fn (sd
, profile_info
);
1249 profile_init (SIM_DESC sd
)
1251 #ifdef SIM_HAVE_ADDR_RANGE
1252 /* Check if a range has been specified without specifying what to
1257 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1259 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1261 if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)))
1262 && ! (PROFILE_INSN_P (cpu
)
1263 || PROFILE_MODEL_P (cpu
)))
1265 sim_io_eprintf_cpu (cpu
, "Profiling address range specified without --profile-insn or --profile-model.\n");
1266 sim_io_eprintf_cpu (cpu
, "Address range ignored.\n");
1267 sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
1268 0, ~ (address_word
) 0);
1278 profile_uninstall (SIM_DESC sd
)
1282 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1284 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1285 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1287 if (PROFILE_FILE (data
) != NULL
)
1289 /* If output from different cpus is going to the same file,
1290 avoid closing the file twice. */
1291 for (j
= 0; j
< i
; ++j
)
1292 if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, j
)))
1293 == PROFILE_FILE (data
))
1296 fclose (PROFILE_FILE (data
));
1299 if (PROFILE_INSN_COUNT (data
) != NULL
)
1300 zfree (PROFILE_INSN_COUNT (data
));