1 /* Default profiling support.
2 Copyright (C) 1996, 1997, 1998, 2000 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 2, or (at your option)
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 along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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 },
64 { {"profile-insn", optional_argument, NULL, OPTION_PROFILE_INSN},
65 '\0', "on|off", "Perform instruction profiling",
66 profile_option_handler },
67 { {"profile-memory", optional_argument, NULL, OPTION_PROFILE_MEMORY},
68 '\0', "on|off", "Perform memory profiling",
69 profile_option_handler },
70 { {"profile-core", optional_argument, NULL, OPTION_PROFILE_CORE},
71 '\0', "on|off", "Perform CORE profiling",
72 profile_option_handler },
73 { {"profile-model", optional_argument, NULL, OPTION_PROFILE_MODEL},
74 '\0', "on|off", "Perform model profiling",
75 profile_option_handler },
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 },
81 { {"profile-file", required_argument, NULL, OPTION_PROFILE_FILE},
82 '\0', "FILE NAME", "Specify profile output file",
83 profile_option_handler },
85 { {"profile-pc", optional_argument, NULL, OPTION_PROFILE_PC},
86 '\0', "on|off", "Perform PC profiling",
87 profile_option_handler },
88 { {"profile-pc-frequency", required_argument, NULL, 'F'},
89 'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency",
90 profile_option_handler },
91 { {"profile-pc-size", required_argument, NULL, 'S'},
92 'S', "PC PROFILE SIZE", "Specify PC profiling size",
93 profile_option_handler },
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 },
97 { {"profile-pc-range", required_argument, NULL, OPTION_PROFILE_PC_RANGE},
98 '\0', "BASE,BOUND", "Specify PC profiling address range",
99 profile_option_handler },
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 },
106 { {"profile-function", required_argument, NULL, OPTION_PROFILE_FUNCTION},
107 '\0', "FUNCTION", "Specify function to profile",
108 profile_option_handler },
112 { {NULL, no_argument, NULL, 0}, '\0', 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 /* PC profiling support */
457 #if WITH_PROFILE_PC_P
460 profile_pc_cleanup (SIM_DESC sd)
463 for (n = 0; n < MAX_NR_PROCESSORS; n++)
465 sim_cpu *cpu = STATE_CPU (sd, n);
466 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
467 if (PROFILE_PC_COUNT (data) != NULL)
468 zfree (PROFILE_PC_COUNT (data));
469 PROFILE_PC_COUNT (data) = NULL;
470 if (PROFILE_PC_EVENT (data) != NULL)
471 sim_events_deschedule (sd, PROFILE_PC_EVENT (data));
472 PROFILE_PC_EVENT (data) = NULL;
478 profile_pc_uninstall (SIM_DESC sd)
480 profile_pc_cleanup (sd);
484 profile_pc_event (SIM_DESC sd,
487 sim_cpu *cpu = (sim_cpu*) data;
488 PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu);
491 switch (STATE_WATCHPOINTS (sd)->sizeof_pc)
493 case 2: pc = *(unsigned_2*)(STATE_WATCHPOINTS (sd)->pc) ; break;
494 case 4: pc = *(unsigned_4*)(STATE_WATCHPOINTS (sd)->pc) ; break;
495 case 8: pc = *(unsigned_8*)(STATE_WATCHPOINTS (sd)->pc) ; break;
498 i = (pc - PROFILE_PC_START (profile)) >> PROFILE_PC_SHIFT (profile);
499 if (i < PROFILE_PC_NR_BUCKETS (profile))
500 PROFILE_PC_COUNT (profile) [i] += 1; /* Overflow? */
502 PROFILE_PC_COUNT (profile) [PROFILE_PC_NR_BUCKETS (profile)] += 1;
503 PROFILE_PC_EVENT (profile) =
504 sim_events_schedule (sd, PROFILE_PC_FREQ (profile), profile_pc_event, cpu);
508 profile_pc_init (SIM_DESC sd)
511 profile_pc_cleanup (sd);
512 for (n = 0; n < MAX_NR_PROCESSORS; n++)
514 sim_cpu *cpu = STATE_CPU (sd, n);
515 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
516 if (CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_PC_IDX]
517 && STATE_WATCHPOINTS (sd)->pc != NULL)
520 /* fill in the frequency if not specified */
521 if (PROFILE_PC_FREQ (data) == 0)
522 PROFILE_PC_FREQ (data) = 256;
523 /* fill in the start/end if not specified */
524 if (PROFILE_PC_END (data) == 0)
526 PROFILE_PC_START (data) = STATE_TEXT_START (sd);
527 PROFILE_PC_END (data) = STATE_TEXT_END (sd);
529 /* Compute the number of buckets if not specified. */
530 if (PROFILE_PC_NR_BUCKETS (data) == 0)
532 if (PROFILE_PC_BUCKET_SIZE (data) == 0)
533 PROFILE_PC_NR_BUCKETS (data) = 16;
536 if (PROFILE_PC_END (data) == 0)
538 /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
539 PROFILE_PC_NR_BUCKETS (data) =
540 ((1 << (STATE_WATCHPOINTS (sd)->sizeof_pc) * (8 - 1))
541 / (PROFILE_PC_BUCKET_SIZE (data) / 2));
545 PROFILE_PC_NR_BUCKETS (data) =
546 ((PROFILE_PC_END (data)
547 - PROFILE_PC_START (data)
548 + PROFILE_PC_BUCKET_SIZE (data) - 1)
549 / PROFILE_PC_BUCKET_SIZE (data));
553 /* Compute the bucket size if not specified. Ensure that it
554 is rounded up to the next power of two */
555 if (PROFILE_PC_BUCKET_SIZE (data) == 0)
557 if (PROFILE_PC_END (data) == 0)
558 /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
559 bucket_size = ((1 << ((STATE_WATCHPOINTS (sd)->sizeof_pc * 8) - 1))
560 / (PROFILE_PC_NR_BUCKETS (data) / 2));
562 bucket_size = ((PROFILE_PC_END (data)
563 - PROFILE_PC_START (data)
564 + PROFILE_PC_NR_BUCKETS (data) - 1)
565 / PROFILE_PC_NR_BUCKETS (data));
566 PROFILE_PC_SHIFT (data) = 0;
567 while (bucket_size < PROFILE_PC_BUCKET_SIZE (data))
569 PROFILE_PC_SHIFT (data) += 1;
572 /* Align the end address with bucket size */
573 if (PROFILE_PC_END (data) != 0)
574 PROFILE_PC_END (data) = (PROFILE_PC_START (data)
575 + (PROFILE_PC_BUCKET_SIZE (data)
576 * PROFILE_PC_NR_BUCKETS (data)));
577 /* create the relevant buffers */
578 PROFILE_PC_COUNT (data) =
579 NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data) + 1);
580 PROFILE_PC_EVENT (data) =
581 sim_events_schedule (sd,
582 PROFILE_PC_FREQ (data),
591 profile_print_pc (sim_cpu *cpu, int verbose)
593 SIM_DESC sd = CPU_STATE (cpu);
594 PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu);
600 if (PROFILE_PC_COUNT (profile) == 0)
603 sim_io_printf (sd, "Program Counter Statistics:\n\n");
605 /* First pass over data computes various things. */
608 for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i)
610 total += PROFILE_PC_COUNT (profile) [i];
611 if (PROFILE_PC_COUNT (profile) [i] > max_val)
612 max_val = PROFILE_PC_COUNT (profile) [i];
615 sim_io_printf (sd, " Total samples: %s\n",
617 sim_io_printf (sd, " Granularity: %s bytes per bucket\n",
618 COMMAS (PROFILE_PC_BUCKET_SIZE (profile)));
619 sim_io_printf (sd, " Size: %s buckets\n",
620 COMMAS (PROFILE_PC_NR_BUCKETS (profile)));
621 sim_io_printf (sd, " Frequency: %s cycles per sample\n",
622 COMMAS (PROFILE_PC_FREQ (profile)));
624 if (PROFILE_PC_END (profile) != 0)
625 sim_io_printf (sd, " Range: 0x%lx 0x%lx\n",
626 (long) PROFILE_PC_START (profile),
627 (long) PROFILE_PC_END (profile));
629 if (verbose && max_val != 0)
631 /* Now we can print the histogram. */
632 sim_io_printf (sd, "\n");
633 for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i)
635 if (PROFILE_PC_COUNT (profile) [i] != 0)
637 sim_io_printf (sd, " ");
638 if (i == PROFILE_PC_NR_BUCKETS (profile))
639 sim_io_printf (sd, "%10s:", "overflow");
641 sim_io_printf (sd, "0x%08lx:",
642 (long) (PROFILE_PC_START (profile)
643 + (i * PROFILE_PC_BUCKET_SIZE (profile))));
644 sim_io_printf (sd, " %*s",
645 max_val < 10000 ? 5 : 10,
646 COMMAS (PROFILE_PC_COUNT (profile) [i]));
647 sim_io_printf (sd, " %4.1f",
648 (PROFILE_PC_COUNT (profile) [i] * 100.0) / total);
649 sim_io_printf (sd, ": ");
650 sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
651 PROFILE_PC_COUNT (profile) [i],
653 sim_io_printf (sd, "\n");
658 /* dump the histogram to the file "gmon.out" using BSD's gprof file
660 /* Since a profile data file is in the native format of the host on
661 which the profile is being, endian issues are not considered in
663 /* FIXME: Is this the best place for this code? */
665 FILE *pf = fopen ("gmon.out", "wb");
668 sim_io_eprintf (sd, "Failed to open \"gmon.out\" profile file\n");
672 /* FIXME: what if the target has a 64 bit PC? */
673 unsigned32 header[3];
675 if (PROFILE_PC_END (profile) != 0)
677 header[0] = PROFILE_PC_START (profile);
678 header[1] = PROFILE_PC_END (profile);
685 /* size of sample buffer (+ header) */
686 header[2] = PROFILE_PC_NR_BUCKETS (profile) * 2 + sizeof (header);
687 ok = fwrite (&header, sizeof (header), 1, pf);
689 ok && (loop < PROFILE_PC_NR_BUCKETS (profile));
693 if (PROFILE_PC_COUNT (profile) [loop] >= 0xffff)
696 sample = PROFILE_PC_COUNT (profile) [loop];
697 ok = fwrite (&sample, sizeof (sample), 1, pf);
700 sim_io_eprintf (sd, "Failed to write to \"gmon.out\" profile file\n");
705 sim_io_printf (sd, "\n");
710 /* Summary printing support. */
712 #if WITH_PROFILE_INSN_P
715 profile_insn_init (SIM_DESC sd)
719 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
721 sim_cpu *cpu = STATE_CPU (sd, c);
723 if (CPU_MAX_INSNS (cpu) > 0)
724 PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu));
731 profile_print_insn (sim_cpu *cpu, int verbose)
733 unsigned int i, n, total, max_val, max_name_len;
734 SIM_DESC sd = CPU_STATE (cpu);
735 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
738 /* If MAX_INSNS not set, insn profiling isn't supported. */
739 if (CPU_MAX_INSNS (cpu) == 0)
742 sim_io_printf (sd, "Instruction Statistics");
743 #ifdef SIM_HAVE_ADDR_RANGE
744 if (PROFILE_RANGE (data)->ranges)
745 sim_io_printf (sd, " (for selected address range(s))");
747 sim_io_printf (sd, "\n\n");
749 /* First pass over data computes various things. */
753 for (i = 0; i < CPU_MAX_INSNS (cpu); ++i)
755 const char *name = (*CPU_INSN_NAME (cpu)) (cpu, i);
759 total += PROFILE_INSN_COUNT (data) [i];
760 if (PROFILE_INSN_COUNT (data) [i] > max_val)
761 max_val = PROFILE_INSN_COUNT (data) [i];
763 if (n > max_name_len)
766 /* set the total insn count, in case client is being lazy */
767 if (! PROFILE_TOTAL_INSN_COUNT (data))
768 PROFILE_TOTAL_INSN_COUNT (data) = total;
770 sim_io_printf (sd, " Total: %s insns\n", COMMAS (total));
772 if (verbose && max_val != 0)
774 /* Now we can print the histogram. */
775 sim_io_printf (sd, "\n");
776 for (i = 0; i < CPU_MAX_INSNS (cpu); ++i)
778 const char *name = (*CPU_INSN_NAME (cpu)) (cpu, i);
782 if (PROFILE_INSN_COUNT (data) [i] != 0)
784 sim_io_printf (sd, " %*s: %*s: ",
786 max_val < 10000 ? 5 : 10,
787 COMMAS (PROFILE_INSN_COUNT (data) [i]));
788 sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
789 PROFILE_INSN_COUNT (data) [i],
791 sim_io_printf (sd, "\n");
796 sim_io_printf (sd, "\n");
801 #if WITH_PROFILE_MEMORY_P
804 profile_print_memory (sim_cpu *cpu, int verbose)
807 unsigned int total_read, total_write;
808 unsigned int max_val, max_name_len;
809 /* FIXME: Need to add smp support. */
810 SIM_DESC sd = CPU_STATE (cpu);
811 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
814 sim_io_printf (sd, "Memory Access Statistics\n\n");
816 /* First pass over data computes various things. */
817 max_val = total_read = total_write = max_name_len = 0;
818 for (i = 0; i < MODE_TARGET_MAX; ++i)
820 total_read += PROFILE_READ_COUNT (data) [i];
821 total_write += PROFILE_WRITE_COUNT (data) [i];
822 if (PROFILE_READ_COUNT (data) [i] > max_val)
823 max_val = PROFILE_READ_COUNT (data) [i];
824 if (PROFILE_WRITE_COUNT (data) [i] > max_val)
825 max_val = PROFILE_WRITE_COUNT (data) [i];
826 n = strlen (MODE_NAME (i));
827 if (n > max_name_len)
831 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
832 sim_io_printf (sd, " Total read: %s accesses\n",
833 COMMAS (total_read));
834 sim_io_printf (sd, " Total write: %s accesses\n",
835 COMMAS (total_write));
837 if (verbose && max_val != 0)
839 /* FIXME: Need to separate instruction fetches from data fetches
840 as the former swamps the latter. */
841 /* Now we can print the histogram. */
842 sim_io_printf (sd, "\n");
843 for (i = 0; i < MODE_TARGET_MAX; ++i)
845 if (PROFILE_READ_COUNT (data) [i] != 0)
847 sim_io_printf (sd, " %*s read: %*s: ",
848 max_name_len, MODE_NAME (i),
849 max_val < 10000 ? 5 : 10,
850 COMMAS (PROFILE_READ_COUNT (data) [i]));
851 sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
852 PROFILE_READ_COUNT (data) [i],
854 sim_io_printf (sd, "\n");
856 if (PROFILE_WRITE_COUNT (data) [i] != 0)
858 sim_io_printf (sd, " %*s write: %*s: ",
859 max_name_len, MODE_NAME (i),
860 max_val < 10000 ? 5 : 10,
861 COMMAS (PROFILE_WRITE_COUNT (data) [i]));
862 sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
863 PROFILE_WRITE_COUNT (data) [i],
865 sim_io_printf (sd, "\n");
870 sim_io_printf (sd, "\n");
875 #if WITH_PROFILE_CORE_P
878 profile_print_core (sim_cpu *cpu, int verbose)
881 unsigned int max_val;
882 /* FIXME: Need to add smp support. */
883 SIM_DESC sd = CPU_STATE (cpu);
884 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
887 sim_io_printf (sd, "CORE Statistics\n\n");
889 /* First pass over data computes various things. */
894 for (map = 0; map < nr_maps; map++)
896 total += PROFILE_CORE_COUNT (data) [map];
897 if (PROFILE_CORE_COUNT (data) [map] > max_val)
898 max_val = PROFILE_CORE_COUNT (data) [map];
902 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
903 sim_io_printf (sd, " Total: %s accesses\n",
906 if (verbose && max_val != 0)
909 /* Now we can print the histogram. */
910 sim_io_printf (sd, "\n");
911 for (map = 0; map < nr_maps; map++)
913 if (PROFILE_CORE_COUNT (data) [map] != 0)
915 sim_io_printf (sd, "%10s:", map_to_str (map));
916 sim_io_printf (sd, "%*s: ",
917 max_val < 10000 ? 5 : 10,
918 COMMAS (PROFILE_CORE_COUNT (data) [map]));
919 sim_profile_print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
920 PROFILE_CORE_COUNT (data) [map],
922 sim_io_printf (sd, "\n");
927 sim_io_printf (sd, "\n");
932 #if WITH_PROFILE_MODEL_P
935 profile_print_model (sim_cpu *cpu, int verbose)
937 SIM_DESC sd = CPU_STATE (cpu);
938 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
939 unsigned long cti_stall_cycles = PROFILE_MODEL_CTI_STALL_CYCLES (data);
940 unsigned long load_stall_cycles = PROFILE_MODEL_LOAD_STALL_CYCLES (data);
941 unsigned long total_cycles = PROFILE_MODEL_TOTAL_CYCLES (data);
944 sim_io_printf (sd, "Model %s Timing Information",
945 MODEL_NAME (CPU_MODEL (cpu)));
946 #ifdef SIM_HAVE_ADDR_RANGE
947 if (PROFILE_RANGE (data)->ranges)
948 sim_io_printf (sd, " (for selected address range(s))");
950 sim_io_printf (sd, "\n\n");
951 sim_io_printf (sd, " %-*s %s\n",
952 PROFILE_LABEL_WIDTH, "Taken branches:",
953 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data)));
954 sim_io_printf (sd, " %-*s %s\n",
955 PROFILE_LABEL_WIDTH, "Untaken branches:",
956 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data)));
957 sim_io_printf (sd, " %-*s %s\n",
958 PROFILE_LABEL_WIDTH, "Cycles stalled due to branches:",
959 COMMAS (cti_stall_cycles));
960 sim_io_printf (sd, " %-*s %s\n",
961 PROFILE_LABEL_WIDTH, "Cycles stalled due to loads:",
962 COMMAS (load_stall_cycles));
963 sim_io_printf (sd, " %-*s %s\n",
964 PROFILE_LABEL_WIDTH, "Total cycles (*approximate*):",
965 COMMAS (total_cycles));
966 sim_io_printf (sd, "\n");
972 sim_profile_print_bar (SIM_DESC sd, unsigned int width,
973 unsigned int val, unsigned int max_val)
975 unsigned int i, count;
977 count = ((double) val / (double) max_val) * (double) width;
979 for (i = 0; i < count; ++i)
980 sim_io_printf (sd, "*");
983 /* Print the simulator's execution speed for CPU. */
986 profile_print_speed (sim_cpu *cpu)
988 SIM_DESC sd = CPU_STATE (cpu);
989 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
990 unsigned long milliseconds = sim_events_elapsed_time (sd);
991 unsigned long total = PROFILE_TOTAL_INSN_COUNT (data);
996 sim_io_printf (sd, "Simulator Execution Speed\n\n");
999 sim_io_printf (sd, " Total instructions: %s\n", COMMAS (total));
1001 if (milliseconds < 1000)
1002 sim_io_printf (sd, " Total execution time: < 1 second\n\n");
1005 /* The printing of the time rounded to 2 decimal places makes the speed
1006 calculation seem incorrect [even though it is correct]. So round
1007 MILLISECONDS first. This can marginally affect the result, but it's
1008 better that the user not perceive there's a math error. */
1009 secs = (double) milliseconds / 1000;
1010 secs = ((double) (unsigned long) (secs * 100 + .5)) / 100;
1011 sim_io_printf (sd, " Total execution time : %.2f seconds\n", secs);
1012 /* Don't confuse things with data that isn't useful.
1013 If we ran for less than 2 seconds, only use the data if we
1014 executed more than 100,000 insns. */
1015 if (secs >= 2 || total >= 100000)
1016 sim_io_printf (sd, " Simulator speed: %s insns/second\n",
1017 COMMAS ((unsigned long) ((double) total / secs)));
1020 #if WITH_PROFILE_MODEL_P
1021 /* Print simulated execution time if the cpu frequency has been specified. */
1022 clock = PROFILE_CPU_FREQ (data);
1025 if (clock >= 1000000)
1026 sim_io_printf (sd, " Simulated cpu frequency: %.2f MHz\n",
1029 sim_io_printf (sd, " Simulated cpu frequency: %.2f Hz\n", clock);
1031 if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX])
1033 /* The printing of the time rounded to 2 decimal places makes the
1034 speed calculation seem incorrect [even though it is correct].
1035 So round SECS first. This can marginally affect the result,
1036 but it's better that the user not perceive there's a math
1038 secs = PROFILE_MODEL_TOTAL_CYCLES (data) / clock;
1039 secs = ((double) (unsigned long) (secs * 100 + .5)) / 100;
1040 sim_io_printf (sd, " Simulated execution time: %.2f seconds\n",
1044 #endif /* WITH_PROFILE_MODEL_P */
1047 /* Print selected address ranges. */
1050 profile_print_addr_ranges (sim_cpu *cpu)
1052 ADDR_SUBRANGE *asr = PROFILE_RANGE (CPU_PROFILE_DATA (cpu))->ranges;
1053 SIM_DESC sd = CPU_STATE (cpu);
1057 sim_io_printf (sd, "Selected address ranges\n\n");
1060 sim_io_printf (sd, " 0x%lx - 0x%lx\n",
1061 (long) asr->start, (long) asr->end);
1064 sim_io_printf (sd, "\n");
1068 /* Top level function to print all summary profile information.
1069 It is [currently] intended that all such data is printed by this function.
1070 I'd rather keep it all in one place for now. To that end, MISC_CPU and
1071 MISC are callbacks used to print any miscellaneous data.
1073 One might want to add a user option that allows printing by type or by cpu
1074 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
1075 This may be a case of featuritis so it's currently left out.
1077 Note that results are indented two spaces to distinguish them from
1081 profile_info (SIM_DESC sd, int verbose)
1084 int print_title_p = 0;
1086 /* Only print the title if some data has been collected. */
1087 /* ??? Why don't we just exit if no data collected? */
1088 /* FIXME: If the number of processors can be selected on the command line,
1089 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
1091 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
1093 sim_cpu *cpu = STATE_CPU (sd, c);
1094 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
1096 for (i = 0; i < MAX_PROFILE_VALUES; ++i)
1097 if (PROFILE_FLAGS (data) [i])
1099 /* One could break out early if print_title_p is set. */
1102 sim_io_printf (sd, "Summary profiling results:\n\n");
1104 /* Loop, cpu by cpu, printing results. */
1106 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
1108 sim_cpu *cpu = STATE_CPU (sd, c);
1109 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
1111 if (MAX_NR_PROCESSORS > 1
1113 #if WITH_PROFILE_INSN_P
1114 || PROFILE_FLAGS (data) [PROFILE_INSN_IDX]
1116 #if WITH_PROFILE_MEMORY_P
1117 || PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX]
1119 #if WITH_PROFILE_CORE_P
1120 || PROFILE_FLAGS (data) [PROFILE_CORE_IDX]
1122 #if WITH_PROFILE_MODEL_P
1123 || PROFILE_FLAGS (data) [PROFILE_MODEL_IDX]
1125 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1126 || PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX]
1128 #if WITH_PROFILE_PC_P
1129 || PROFILE_FLAGS (data) [PROFILE_PC_IDX]
1133 sim_io_printf (sd, "CPU %d\n\n", c);
1136 #ifdef SIM_HAVE_ADDR_RANGE
1138 && (PROFILE_INSN_P (cpu)
1139 || PROFILE_MODEL_P (cpu)))
1140 profile_print_addr_ranges (cpu);
1143 #if WITH_PROFILE_INSN_P
1144 if (PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
1145 profile_print_insn (cpu, verbose);
1148 #if WITH_PROFILE_MEMORY_P
1149 if (PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX])
1150 profile_print_memory (cpu, verbose);
1153 #if WITH_PROFILE_CORE_P
1154 if (PROFILE_FLAGS (data) [PROFILE_CORE_IDX])
1155 profile_print_core (cpu, verbose);
1158 #if WITH_PROFILE_MODEL_P
1159 if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX])
1160 profile_print_model (cpu, verbose);
1163 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1164 if (PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX])
1165 scache_print_profile (cpu, verbose);
1168 #if WITH_PROFILE_PC_P
1169 if (PROFILE_FLAGS (data) [PROFILE_PC_IDX])
1170 profile_print_pc (cpu, verbose);
1173 /* Print cpu-specific data before the execution speed. */
1174 if (PROFILE_INFO_CPU_CALLBACK (data) != NULL)
1175 PROFILE_INFO_CPU_CALLBACK (data) (cpu, verbose);
1177 /* Always try to print execution time and speed. */
1179 || PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
1180 profile_print_speed (cpu);
1183 /* Finally print non-cpu specific miscellaneous data. */
1184 if (STATE_PROFILE_INFO_CALLBACK (sd))
1185 STATE_PROFILE_INFO_CALLBACK (sd) (sd, verbose);
1189 /* Install profiling support in the simulator. */
1192 profile_install (SIM_DESC sd)
1196 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
1197 sim_add_option_table (sd, NULL, profile_options);
1198 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1199 memset (CPU_PROFILE_DATA (STATE_CPU (sd, i)), 0,
1200 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd, i))));
1201 #if WITH_PROFILE_INSN_P
1202 sim_module_add_init_fn (sd, profile_insn_init);
1204 #if WITH_PROFILE_PC_P
1205 sim_module_add_uninstall_fn (sd, profile_pc_uninstall);
1206 sim_module_add_init_fn (sd, profile_pc_init);
1208 sim_module_add_init_fn (sd, profile_init);
1209 sim_module_add_uninstall_fn (sd, profile_uninstall);
1210 sim_module_add_info_fn (sd, profile_info);
1215 profile_init (SIM_DESC sd)
1217 #ifdef SIM_HAVE_ADDR_RANGE
1218 /* Check if a range has been specified without specifying what to
1223 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1225 sim_cpu *cpu = STATE_CPU (sd, i);
1227 if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)))
1228 && ! (PROFILE_INSN_P (cpu)
1229 || PROFILE_MODEL_P (cpu)))
1231 sim_io_eprintf_cpu (cpu, "Profiling address range specified without --profile-insn or --profile-model.\n");
1232 sim_io_eprintf_cpu (cpu, "Address range ignored.\n");
1233 sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)),
1234 0, ~ (address_word) 0);
1244 profile_uninstall (SIM_DESC sd)
1248 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1250 sim_cpu *cpu = STATE_CPU (sd, i);
1251 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
1253 if (PROFILE_FILE (data) != NULL)
1255 /* If output from different cpus is going to the same file,
1256 avoid closing the file twice. */
1257 for (j = 0; j < i; ++j)
1258 if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd, j)))
1259 == PROFILE_FILE (data))
1262 fclose (PROFILE_FILE (data));
1265 if (PROFILE_INSN_COUNT (data) != NULL)
1266 zfree (PROFILE_INSN_COUNT (data));