1 /* Default profiling support.
2 Copyright (C) 1996-2015 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
);
525 switch (STATE_WATCHPOINTS (sd
)->sizeof_pc
)
527 case 2: pc
= *(unsigned_2
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
528 case 4: pc
= *(unsigned_4
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
529 case 8: pc
= *(unsigned_8
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
532 i
= (pc
- PROFILE_PC_START (profile
)) >> PROFILE_PC_SHIFT (profile
);
533 if (i
< PROFILE_PC_NR_BUCKETS (profile
))
534 PROFILE_PC_COUNT (profile
) [i
] += 1; /* Overflow? */
536 PROFILE_PC_COUNT (profile
) [PROFILE_PC_NR_BUCKETS (profile
)] += 1;
537 PROFILE_PC_EVENT (profile
) =
538 sim_events_schedule (sd
, PROFILE_PC_FREQ (profile
), profile_pc_event
, cpu
);
542 profile_pc_init (SIM_DESC sd
)
545 profile_pc_cleanup (sd
);
546 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
548 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
549 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
550 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
]
551 && STATE_WATCHPOINTS (sd
)->pc
!= NULL
)
554 /* fill in the frequency if not specified */
555 if (PROFILE_PC_FREQ (data
) == 0)
556 PROFILE_PC_FREQ (data
) = 257;
557 /* fill in the start/end if not specified */
558 if (PROFILE_PC_END (data
) == 0)
560 PROFILE_PC_START (data
) = STATE_TEXT_START (sd
);
561 PROFILE_PC_END (data
) = STATE_TEXT_END (sd
);
563 /* Compute the number of buckets if not specified. */
564 if (PROFILE_PC_NR_BUCKETS (data
) == 0)
566 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
567 PROFILE_PC_NR_BUCKETS (data
) = 16;
570 if (PROFILE_PC_END (data
) == 0)
572 /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
573 PROFILE_PC_NR_BUCKETS (data
) =
574 ((1 << (STATE_WATCHPOINTS (sd
)->sizeof_pc
) * (8 - 1))
575 / (PROFILE_PC_BUCKET_SIZE (data
) / 2));
579 PROFILE_PC_NR_BUCKETS (data
) =
580 ((PROFILE_PC_END (data
)
581 - PROFILE_PC_START (data
)
582 + PROFILE_PC_BUCKET_SIZE (data
) - 1)
583 / PROFILE_PC_BUCKET_SIZE (data
));
587 /* Compute the bucket size if not specified. Ensure that it
588 is rounded up to the next power of two */
589 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
591 if (PROFILE_PC_END (data
) == 0)
592 /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
593 bucket_size
= ((1 << ((STATE_WATCHPOINTS (sd
)->sizeof_pc
* 8) - 1))
594 / (PROFILE_PC_NR_BUCKETS (data
) / 2));
596 bucket_size
= ((PROFILE_PC_END (data
)
597 - PROFILE_PC_START (data
)
598 + PROFILE_PC_NR_BUCKETS (data
) - 1)
599 / PROFILE_PC_NR_BUCKETS (data
));
600 PROFILE_PC_SHIFT (data
) = 0;
601 while (bucket_size
> PROFILE_PC_BUCKET_SIZE (data
))
603 PROFILE_PC_SHIFT (data
) += 1;
606 /* Align the end address with bucket size */
607 if (PROFILE_PC_END (data
) != 0)
608 PROFILE_PC_END (data
) = (PROFILE_PC_START (data
)
609 + (PROFILE_PC_BUCKET_SIZE (data
)
610 * PROFILE_PC_NR_BUCKETS (data
)));
611 /* create the relevant buffers */
612 PROFILE_PC_COUNT (data
) =
613 NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data
) + 1);
614 PROFILE_PC_EVENT (data
) =
615 sim_events_schedule (sd
,
616 PROFILE_PC_FREQ (data
),
625 profile_print_pc (sim_cpu
*cpu
, int verbose
)
627 SIM_DESC sd
= CPU_STATE (cpu
);
628 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
634 if (PROFILE_PC_COUNT (profile
) == 0)
637 profile_printf (sd
, cpu
, "Program Counter Statistics:\n\n");
639 /* First pass over data computes various things. */
642 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
644 total
+= PROFILE_PC_COUNT (profile
) [i
];
645 if (PROFILE_PC_COUNT (profile
) [i
] > max_val
)
646 max_val
= PROFILE_PC_COUNT (profile
) [i
];
649 profile_printf (sd
, cpu
, " Total samples: %s\n",
651 profile_printf (sd
, cpu
, " Granularity: %s bytes per bucket\n",
652 COMMAS (PROFILE_PC_BUCKET_SIZE (profile
)));
653 profile_printf (sd
, cpu
, " Size: %s buckets\n",
654 COMMAS (PROFILE_PC_NR_BUCKETS (profile
)));
655 profile_printf (sd
, cpu
, " Frequency: %s cycles per sample\n",
656 COMMAS (PROFILE_PC_FREQ (profile
)));
658 if (PROFILE_PC_END (profile
) != 0)
659 profile_printf (sd
, cpu
, " Range: 0x%lx 0x%lx\n",
660 (long) PROFILE_PC_START (profile
),
661 (long) PROFILE_PC_END (profile
));
663 if (verbose
&& max_val
!= 0)
665 /* Now we can print the histogram. */
666 profile_printf (sd
, cpu
, "\n");
667 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
669 if (PROFILE_PC_COUNT (profile
) [i
] != 0)
671 profile_printf (sd
, cpu
, " ");
672 if (i
== PROFILE_PC_NR_BUCKETS (profile
))
673 profile_printf (sd
, cpu
, "%10s:", "overflow");
675 profile_printf (sd
, cpu
, "0x%08lx:",
676 (long) (PROFILE_PC_START (profile
)
677 + (i
* PROFILE_PC_BUCKET_SIZE (profile
))));
678 profile_printf (sd
, cpu
, " %*s",
679 max_val
< 10000 ? 5 : 10,
680 COMMAS (PROFILE_PC_COUNT (profile
) [i
]));
681 profile_printf (sd
, cpu
, " %4.1f",
682 (PROFILE_PC_COUNT (profile
) [i
] * 100.0) / total
);
683 profile_printf (sd
, cpu
, ": ");
684 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
685 PROFILE_PC_COUNT (profile
) [i
],
687 profile_printf (sd
, cpu
, "\n");
692 /* dump the histogram to the file "gmon.out" using BSD's gprof file
694 /* Since a profile data file is in the native format of the host on
695 which the profile is being, endian issues are not considered in
697 /* FIXME: Is this the best place for this code? */
699 FILE *pf
= fopen ("gmon.out", "wb");
702 sim_io_eprintf (sd
, "Failed to open \"gmon.out\" profile file\n");
706 /* FIXME: what if the target has a 64 bit PC? */
707 unsigned32 header
[3];
709 if (PROFILE_PC_END (profile
) != 0)
711 header
[0] = PROFILE_PC_START (profile
);
712 header
[1] = PROFILE_PC_END (profile
);
719 /* size of sample buffer (+ header) */
720 header
[2] = PROFILE_PC_NR_BUCKETS (profile
) * 2 + sizeof (header
);
722 /* Header must be written out in target byte order. */
727 ok
= fwrite (&header
, sizeof (header
), 1, pf
);
729 ok
&& (loop
< PROFILE_PC_NR_BUCKETS (profile
));
733 if (PROFILE_PC_COUNT (profile
) [loop
] >= 0xffff)
736 sample
= PROFILE_PC_COUNT (profile
) [loop
];
738 ok
= fwrite (&sample
, sizeof (sample
), 1, pf
);
741 sim_io_eprintf (sd
, "Failed to write to \"gmon.out\" profile file\n");
746 profile_printf (sd
, cpu
, "\n");
751 /* Summary printing support. */
753 #if WITH_PROFILE_INSN_P
756 profile_insn_init (SIM_DESC sd
)
760 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
762 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
764 if (CPU_MAX_INSNS (cpu
) > 0)
765 PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu
)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu
));
772 profile_print_insn (sim_cpu
*cpu
, int verbose
)
774 unsigned int i
, n
, total
, max_val
, max_name_len
;
775 SIM_DESC sd
= CPU_STATE (cpu
);
776 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
779 /* If MAX_INSNS not set, insn profiling isn't supported. */
780 if (CPU_MAX_INSNS (cpu
) == 0)
783 profile_printf (sd
, cpu
, "Instruction Statistics");
784 #ifdef SIM_HAVE_ADDR_RANGE
785 if (PROFILE_RANGE (data
)->ranges
)
786 profile_printf (sd
, cpu
, " (for selected address range(s))");
788 profile_printf (sd
, cpu
, "\n\n");
790 /* First pass over data computes various things. */
794 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
796 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
800 total
+= PROFILE_INSN_COUNT (data
) [i
];
801 if (PROFILE_INSN_COUNT (data
) [i
] > max_val
)
802 max_val
= PROFILE_INSN_COUNT (data
) [i
];
804 if (n
> max_name_len
)
807 /* set the total insn count, in case client is being lazy */
808 if (! PROFILE_TOTAL_INSN_COUNT (data
))
809 PROFILE_TOTAL_INSN_COUNT (data
) = total
;
811 profile_printf (sd
, cpu
, " Total: %s insns\n", COMMAS (total
));
813 if (verbose
&& max_val
!= 0)
815 /* Now we can print the histogram. */
816 profile_printf (sd
, cpu
, "\n");
817 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
819 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
823 if (PROFILE_INSN_COUNT (data
) [i
] != 0)
825 profile_printf (sd
, cpu
, " %*s: %*s: ",
827 max_val
< 10000 ? 5 : 10,
828 COMMAS (PROFILE_INSN_COUNT (data
) [i
]));
829 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
830 PROFILE_INSN_COUNT (data
) [i
],
832 profile_printf (sd
, cpu
, "\n");
837 profile_printf (sd
, cpu
, "\n");
842 #if WITH_PROFILE_MEMORY_P
845 profile_print_memory (sim_cpu
*cpu
, int verbose
)
848 unsigned int total_read
, total_write
;
849 unsigned int max_val
, max_name_len
;
850 /* FIXME: Need to add smp support. */
851 SIM_DESC sd
= CPU_STATE (cpu
);
852 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
855 profile_printf (sd
, cpu
, "Memory Access Statistics\n\n");
857 /* First pass over data computes various things. */
858 max_val
= total_read
= total_write
= max_name_len
= 0;
859 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
861 total_read
+= PROFILE_READ_COUNT (data
) [i
];
862 total_write
+= PROFILE_WRITE_COUNT (data
) [i
];
863 if (PROFILE_READ_COUNT (data
) [i
] > max_val
)
864 max_val
= PROFILE_READ_COUNT (data
) [i
];
865 if (PROFILE_WRITE_COUNT (data
) [i
] > max_val
)
866 max_val
= PROFILE_WRITE_COUNT (data
) [i
];
867 n
= strlen (MODE_NAME (i
));
868 if (n
> max_name_len
)
872 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
873 profile_printf (sd
, cpu
, " Total read: %s accesses\n",
874 COMMAS (total_read
));
875 profile_printf (sd
, cpu
, " Total write: %s accesses\n",
876 COMMAS (total_write
));
878 if (verbose
&& max_val
!= 0)
880 /* FIXME: Need to separate instruction fetches from data fetches
881 as the former swamps the latter. */
882 /* Now we can print the histogram. */
883 profile_printf (sd
, cpu
, "\n");
884 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
886 if (PROFILE_READ_COUNT (data
) [i
] != 0)
888 profile_printf (sd
, cpu
, " %*s read: %*s: ",
889 max_name_len
, MODE_NAME (i
),
890 max_val
< 10000 ? 5 : 10,
891 COMMAS (PROFILE_READ_COUNT (data
) [i
]));
892 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
893 PROFILE_READ_COUNT (data
) [i
],
895 profile_printf (sd
, cpu
, "\n");
897 if (PROFILE_WRITE_COUNT (data
) [i
] != 0)
899 profile_printf (sd
, cpu
, " %*s write: %*s: ",
900 max_name_len
, MODE_NAME (i
),
901 max_val
< 10000 ? 5 : 10,
902 COMMAS (PROFILE_WRITE_COUNT (data
) [i
]));
903 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
904 PROFILE_WRITE_COUNT (data
) [i
],
906 profile_printf (sd
, cpu
, "\n");
911 profile_printf (sd
, cpu
, "\n");
916 #if WITH_PROFILE_CORE_P
919 profile_print_core (sim_cpu
*cpu
, int verbose
)
922 unsigned int max_val
;
923 /* FIXME: Need to add smp support. */
924 SIM_DESC sd
= CPU_STATE (cpu
);
925 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
928 profile_printf (sd
, cpu
, "CORE Statistics\n\n");
930 /* First pass over data computes various things. */
935 for (map
= 0; map
< nr_maps
; map
++)
937 total
+= PROFILE_CORE_COUNT (data
) [map
];
938 if (PROFILE_CORE_COUNT (data
) [map
] > max_val
)
939 max_val
= PROFILE_CORE_COUNT (data
) [map
];
943 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
944 profile_printf (sd
, cpu
, " Total: %s accesses\n",
947 if (verbose
&& max_val
!= 0)
950 /* Now we can print the histogram. */
951 profile_printf (sd
, cpu
, "\n");
952 for (map
= 0; map
< nr_maps
; map
++)
954 if (PROFILE_CORE_COUNT (data
) [map
] != 0)
956 profile_printf (sd
, cpu
, "%10s:", map_to_str (map
));
957 profile_printf (sd
, cpu
, "%*s: ",
958 max_val
< 10000 ? 5 : 10,
959 COMMAS (PROFILE_CORE_COUNT (data
) [map
]));
960 sim_profile_print_bar (sd
, cpu
, PROFILE_HISTOGRAM_WIDTH
,
961 PROFILE_CORE_COUNT (data
) [map
],
963 profile_printf (sd
, cpu
, "\n");
968 profile_printf (sd
, cpu
, "\n");
973 #if WITH_PROFILE_MODEL_P
976 profile_print_model (sim_cpu
*cpu
, int verbose
)
978 SIM_DESC sd
= CPU_STATE (cpu
);
979 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
980 unsigned long cti_stall_cycles
= PROFILE_MODEL_CTI_STALL_CYCLES (data
);
981 unsigned long load_stall_cycles
= PROFILE_MODEL_LOAD_STALL_CYCLES (data
);
982 unsigned long total_cycles
= PROFILE_MODEL_TOTAL_CYCLES (data
);
985 profile_printf (sd
, cpu
, "Model %s Timing Information",
986 MODEL_NAME (CPU_MODEL (cpu
)));
987 #ifdef SIM_HAVE_ADDR_RANGE
988 if (PROFILE_RANGE (data
)->ranges
)
989 profile_printf (sd
, cpu
, " (for selected address range(s))");
991 profile_printf (sd
, cpu
, "\n\n");
992 profile_printf (sd
, cpu
, " %-*s %s\n",
993 PROFILE_LABEL_WIDTH
, "Taken branches:",
994 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data
)));
995 profile_printf (sd
, cpu
, " %-*s %s\n",
996 PROFILE_LABEL_WIDTH
, "Untaken branches:",
997 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data
)));
998 profile_printf (sd
, cpu
, " %-*s %s\n",
999 PROFILE_LABEL_WIDTH
, "Cycles stalled due to branches:",
1000 COMMAS (cti_stall_cycles
));
1001 profile_printf (sd
, cpu
, " %-*s %s\n",
1002 PROFILE_LABEL_WIDTH
, "Cycles stalled due to loads:",
1003 COMMAS (load_stall_cycles
));
1004 profile_printf (sd
, cpu
, " %-*s %s\n",
1005 PROFILE_LABEL_WIDTH
, "Total cycles (*approximate*):",
1006 COMMAS (total_cycles
));
1007 profile_printf (sd
, cpu
, "\n");
1013 sim_profile_print_bar (SIM_DESC sd
, sim_cpu
*cpu
, unsigned int width
,
1014 unsigned int val
, unsigned int max_val
)
1016 unsigned int i
, count
;
1018 count
= ((double) val
/ (double) max_val
) * (double) width
;
1020 for (i
= 0; i
< count
; ++i
)
1021 profile_printf (sd
, cpu
, "*");
1024 /* Print the simulator's execution speed for CPU. */
1027 profile_print_speed (sim_cpu
*cpu
)
1029 SIM_DESC sd
= CPU_STATE (cpu
);
1030 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1031 unsigned long milliseconds
= sim_events_elapsed_time (sd
);
1032 unsigned long total
= PROFILE_TOTAL_INSN_COUNT (data
);
1037 profile_printf (sd
, cpu
, "Simulator Execution Speed\n\n");
1040 profile_printf (sd
, cpu
, " Total instructions: %s\n", COMMAS (total
));
1042 if (milliseconds
< 1000)
1043 profile_printf (sd
, cpu
, " Total execution time: < 1 second\n\n");
1046 /* The printing of the time rounded to 2 decimal places makes the speed
1047 calculation seem incorrect [even though it is correct]. So round
1048 MILLISECONDS first. This can marginally affect the result, but it's
1049 better that the user not perceive there's a math error. */
1050 secs
= (double) milliseconds
/ 1000;
1051 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1052 profile_printf (sd
, cpu
, " Total execution time : %.2f seconds\n", secs
);
1053 /* Don't confuse things with data that isn't useful.
1054 If we ran for less than 2 seconds, only use the data if we
1055 executed more than 100,000 insns. */
1056 if (secs
>= 2 || total
>= 100000)
1057 profile_printf (sd
, cpu
, " Simulator speed: %s insns/second\n",
1058 COMMAS ((unsigned long) ((double) total
/ secs
)));
1061 /* Print simulated execution time if the cpu frequency has been specified. */
1062 clock
= PROFILE_CPU_FREQ (data
);
1065 if (clock
>= 1000000)
1066 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f MHz\n",
1069 profile_printf (sd
, cpu
, " Simulated cpu frequency: %.2f Hz\n", clock
);
1071 #if WITH_PROFILE_MODEL_P
1072 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1074 /* The printing of the time rounded to 2 decimal places makes the
1075 speed calculation seem incorrect [even though it is correct].
1076 So round SECS first. This can marginally affect the result,
1077 but it's better that the user not perceive there's a math
1079 secs
= PROFILE_MODEL_TOTAL_CYCLES (data
) / clock
;
1080 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
1081 profile_printf (sd
, cpu
, " Simulated execution time: %.2f seconds\n",
1084 #endif /* WITH_PROFILE_MODEL_P */
1088 #ifdef SIM_HAVE_ADDR_RANGE
1089 /* Print selected address ranges. */
1092 profile_print_addr_ranges (sim_cpu
*cpu
)
1094 ADDR_SUBRANGE
*asr
= PROFILE_RANGE (CPU_PROFILE_DATA (cpu
))->ranges
;
1095 SIM_DESC sd
= CPU_STATE (cpu
);
1099 profile_printf (sd
, cpu
, "Selected address ranges\n\n");
1102 profile_printf (sd
, cpu
, " 0x%lx - 0x%lx\n",
1103 (long) asr
->start
, (long) asr
->end
);
1106 profile_printf (sd
, cpu
, "\n");
1111 /* Top level function to print all summary profile information.
1112 It is [currently] intended that all such data is printed by this function.
1113 I'd rather keep it all in one place for now. To that end, MISC_CPU and
1114 MISC are callbacks used to print any miscellaneous data.
1116 One might want to add a user option that allows printing by type or by cpu
1117 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
1118 This may be a case of featuritis so it's currently left out.
1120 Note that results are indented two spaces to distinguish them from
1124 profile_info (SIM_DESC sd
, int verbose
)
1127 int print_title_p
= 0;
1129 /* Only print the title if some data has been collected. */
1130 /* ??? Why don't we just exit if no data collected? */
1131 /* FIXME: If the number of processors can be selected on the command line,
1132 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
1134 for (c
= 0; c
< MAX_NR_PROCESSORS
&& !print_title_p
; ++c
)
1136 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1137 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1139 for (i
= 0; i
< MAX_PROFILE_VALUES
; ++i
)
1140 if (PROFILE_FLAGS (data
) [i
])
1142 profile_printf (sd
, cpu
, "Summary profiling results:\n\n");
1148 /* Loop, cpu by cpu, printing results. */
1150 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
1152 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1153 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1155 if (MAX_NR_PROCESSORS
> 1
1157 #if WITH_PROFILE_INSN_P
1158 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
]
1160 #if WITH_PROFILE_MEMORY_P
1161 || PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
]
1163 #if WITH_PROFILE_CORE_P
1164 || PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
]
1166 #if WITH_PROFILE_MODEL_P
1167 || PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
]
1169 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1170 || PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
]
1172 #if WITH_PROFILE_PC_P
1173 || PROFILE_FLAGS (data
) [PROFILE_PC_IDX
]
1177 profile_printf (sd
, cpu
, "CPU %d\n\n", c
);
1180 #ifdef SIM_HAVE_ADDR_RANGE
1182 && (PROFILE_INSN_P (cpu
)
1183 || PROFILE_MODEL_P (cpu
)))
1184 profile_print_addr_ranges (cpu
);
1187 #if WITH_PROFILE_INSN_P
1188 if (PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1189 profile_print_insn (cpu
, verbose
);
1192 #if WITH_PROFILE_MEMORY_P
1193 if (PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
])
1194 profile_print_memory (cpu
, verbose
);
1197 #if WITH_PROFILE_CORE_P
1198 if (PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
])
1199 profile_print_core (cpu
, verbose
);
1202 #if WITH_PROFILE_MODEL_P
1203 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1204 profile_print_model (cpu
, verbose
);
1207 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1208 if (PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
])
1209 scache_print_profile (cpu
, verbose
);
1212 #if WITH_PROFILE_PC_P
1213 if (PROFILE_FLAGS (data
) [PROFILE_PC_IDX
])
1214 profile_print_pc (cpu
, verbose
);
1217 /* Print cpu-specific data before the execution speed. */
1218 if (PROFILE_INFO_CPU_CALLBACK (data
) != NULL
)
1219 PROFILE_INFO_CPU_CALLBACK (data
) (cpu
, verbose
);
1221 /* Always try to print execution time and speed. */
1223 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1224 profile_print_speed (cpu
);
1227 /* Finally print non-cpu specific miscellaneous data. */
1228 if (STATE_PROFILE_INFO_CALLBACK (sd
))
1229 STATE_PROFILE_INFO_CALLBACK (sd
) (sd
, verbose
);
1233 /* Install profiling support in the simulator. */
1236 profile_install (SIM_DESC sd
)
1240 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1241 sim_add_option_table (sd
, NULL
, profile_options
);
1242 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1243 memset (CPU_PROFILE_DATA (STATE_CPU (sd
, i
)), 0,
1244 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd
, i
))));
1245 #if WITH_PROFILE_INSN_P
1246 sim_module_add_init_fn (sd
, profile_insn_init
);
1248 #if WITH_PROFILE_PC_P
1249 sim_module_add_uninstall_fn (sd
, profile_pc_uninstall
);
1250 sim_module_add_init_fn (sd
, profile_pc_init
);
1252 sim_module_add_init_fn (sd
, profile_init
);
1253 sim_module_add_uninstall_fn (sd
, profile_uninstall
);
1254 sim_module_add_info_fn (sd
, profile_info
);
1259 profile_init (SIM_DESC sd
)
1261 #ifdef SIM_HAVE_ADDR_RANGE
1262 /* Check if a range has been specified without specifying what to
1267 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1269 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1271 if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)))
1272 && ! (PROFILE_INSN_P (cpu
)
1273 || PROFILE_MODEL_P (cpu
)))
1275 sim_io_eprintf_cpu (cpu
, "Profiling address range specified without --profile-insn or --profile-model.\n");
1276 sim_io_eprintf_cpu (cpu
, "Address range ignored.\n");
1277 sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
1278 0, ~ (address_word
) 0);
1288 profile_uninstall (SIM_DESC sd
)
1292 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1294 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1295 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1297 if (PROFILE_FILE (data
) != NULL
)
1299 /* If output from different cpus is going to the same file,
1300 avoid closing the file twice. */
1301 for (j
= 0; j
< i
; ++j
)
1302 if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, j
)))
1303 == PROFILE_FILE (data
))
1306 fclose (PROFILE_FILE (data
));
1309 if (PROFILE_INSN_COUNT (data
) != NULL
)
1310 free (PROFILE_INSN_COUNT (data
));