1 /* Default profiling support.
2 Copyright (C) 1996, 1997, 1998 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"
38 #define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n))
40 static MODULE_INIT_FN profile_init
;
41 static MODULE_UNINSTALL_FN profile_uninstall
;
43 static DECLARE_OPTION_HANDLER (profile_option_handler
);
46 OPTION_PROFILE_INSN
= OPTION_START
,
47 OPTION_PROFILE_MEMORY
,
52 OPTION_PROFILE_PC_RANGE
,
53 OPTION_PROFILE_PC_GRANULARITY
,
55 OPTION_PROFILE_FUNCTION
58 static const OPTION profile_options
[] = {
59 { {"profile", optional_argument
, NULL
, 'p'},
60 'p', "on|off", "Perform profiling",
61 profile_option_handler
},
62 { {"profile-insn", optional_argument
, NULL
, OPTION_PROFILE_INSN
},
63 '\0', "on|off", "Perform instruction profiling",
64 profile_option_handler
},
65 { {"profile-memory", optional_argument
, NULL
, OPTION_PROFILE_MEMORY
},
66 '\0', "on|off", "Perform memory profiling",
67 profile_option_handler
},
68 { {"profile-core", optional_argument
, NULL
, OPTION_PROFILE_CORE
},
69 '\0', "on|off", "Perform CORE profiling",
70 profile_option_handler
},
71 { {"profile-model", optional_argument
, NULL
, OPTION_PROFILE_MODEL
},
72 '\0', "on|off", "Perform model profiling",
73 profile_option_handler
},
75 { {"profile-file", required_argument
, NULL
, OPTION_PROFILE_FILE
},
76 '\0', "FILE NAME", "Specify profile output file",
77 profile_option_handler
},
79 { {"profile-pc", optional_argument
, NULL
, OPTION_PROFILE_PC
},
80 '\0', "on|off", "Perform PC profiling",
81 profile_option_handler
},
82 { {"profile-pc-frequency", required_argument
, NULL
, 'F'},
83 'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency",
84 profile_option_handler
},
85 { {"profile-pc-size", required_argument
, NULL
, 'S'},
86 'S', "PC PROFILE SIZE", "Specify PC profiling size",
87 profile_option_handler
},
88 { {"profile-pc-granularity", required_argument
, NULL
, OPTION_PROFILE_PC_GRANULARITY
},
89 '\0', "PC PROFILE GRANULARITY", "Specify PC profiling sample coverage",
90 profile_option_handler
},
91 { {"profile-pc-range", required_argument
, NULL
, OPTION_PROFILE_PC_RANGE
},
92 '\0', "BASE,BOUND", "Specify PC profiling address range",
93 profile_option_handler
},
95 #ifdef SIM_HAVE_ADDR_RANGE
96 { {"profile-range", required_argument
, NULL
, OPTION_PROFILE_RANGE
},
97 '\0', "START,END", "Specify range of addresses for instruction and model profiling",
98 profile_option_handler
},
100 { {"profile-function", required_argument
, NULL
, OPTION_PROFILE_FUNCTION
},
101 '\0', "FUNCTION", "Specify function to profile",
102 profile_option_handler
},
106 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
109 /* Set/reset the profile options indicated in MASK. */
112 set_profile_option_mask (SIM_DESC sd
, const char *name
, int mask
, const char *arg
)
120 if (strcmp (arg
, "yes") == 0
121 || strcmp (arg
, "on") == 0
122 || strcmp (arg
, "1") == 0)
124 else if (strcmp (arg
, "no") == 0
125 || strcmp (arg
, "off") == 0
126 || strcmp (arg
, "0") == 0)
130 sim_io_eprintf (sd
, "Argument `%s' for `--profile%s' invalid, one of `on', `off', `yes', `no' expected\n", arg
, name
);
135 /* update applicable profile bits */
136 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
138 if ((mask
& (1 << profile_nr
)) == 0)
141 #if 0 /* see sim-trace.c, set flags in STATE here if/when there are any */
142 /* Set non-cpu specific values. */
150 /* Set cpu values. */
151 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
153 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
] = profile_val
;
157 /* Re-compute the cpu profile summary. */
160 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
161 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
165 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; cpu_nr
++)
167 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 0;
168 for (profile_nr
= 0; profile_nr
< MAX_PROFILE_VALUES
; ++profile_nr
)
170 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[profile_nr
])
172 CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))->profile_any_p
= 1;
182 /* Set one profile option based on its IDX value.
183 Not static as cgen-scache.c uses it. */
186 sim_profile_set_option (SIM_DESC sd
, const char *name
, int idx
, const char *arg
)
188 return set_profile_option_mask (sd
, name
, 1 << idx
, arg
);
192 profile_option_handler (SIM_DESC sd
,
200 /* FIXME: Need to handle `cpu' arg. */
206 sim_io_eprintf (sd
, "Profiling not compiled in, `-p' ignored\n");
208 return set_profile_option_mask (sd
, "profile", PROFILE_USEFUL_MASK
,
212 case OPTION_PROFILE_INSN
:
213 if (WITH_PROFILE_INSN_P
)
214 return sim_profile_set_option (sd
, "-insn", PROFILE_INSN_IDX
, arg
);
216 sim_io_eprintf (sd
, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
219 case OPTION_PROFILE_MEMORY
:
220 if (WITH_PROFILE_MEMORY_P
)
221 return sim_profile_set_option (sd
, "-memory", PROFILE_MEMORY_IDX
, arg
);
223 sim_io_eprintf (sd
, "Memory profiling not compiled in, `--profile-memory' ignored\n");
226 case OPTION_PROFILE_CORE
:
227 if (WITH_PROFILE_CORE_P
)
228 return sim_profile_set_option (sd
, "-core", PROFILE_CORE_IDX
, arg
);
230 sim_io_eprintf (sd
, "CORE profiling not compiled in, `--profile-core' ignored\n");
233 case OPTION_PROFILE_MODEL
:
234 if (WITH_PROFILE_MODEL_P
)
235 return sim_profile_set_option (sd
, "-model", PROFILE_MODEL_IDX
, arg
);
237 sim_io_eprintf (sd
, "Model profiling not compiled in, `--profile-model' ignored\n");
240 case OPTION_PROFILE_FILE
:
241 /* FIXME: Might want this to apply to pc profiling only,
242 or have two profile file options. */
244 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-file' ignored\n");
247 FILE *f
= fopen (arg
, "w");
251 sim_io_eprintf (sd
, "Unable to open profile output file `%s'\n", arg
);
254 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
255 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = f
;
259 case OPTION_PROFILE_PC
:
260 if (WITH_PROFILE_PC_P
)
261 return sim_profile_set_option (sd
, "-pc", PROFILE_PC_IDX
, arg
);
263 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc' ignored\n");
267 if (WITH_PROFILE_PC_P
)
269 /* FIXME: Validate arg. */
270 int val
= atoi (arg
);
271 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
272 PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
273 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
274 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
277 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
281 if (WITH_PROFILE_PC_P
)
283 /* FIXME: Validate arg. */
284 int val
= atoi (arg
);
285 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
286 PROFILE_PC_NR_BUCKETS (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = val
;
287 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
288 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
291 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
294 case OPTION_PROFILE_PC_GRANULARITY
:
295 if (WITH_PROFILE_PC_P
)
298 int val
= atoi (arg
);
299 /* check that the granularity is a power of two */
301 while (val
> (1 << shift
))
305 if (val
!= (1 << shift
))
307 sim_io_eprintf (sd
, "PC profiling granularity not a power of two\n");
312 sim_io_eprintf (sd
, "PC profiling granularity too small");
315 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
316 PROFILE_PC_SHIFT (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = shift
;
317 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
318 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
321 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
324 case OPTION_PROFILE_PC_RANGE
:
325 if (WITH_PROFILE_PC_P
)
327 /* FIXME: Validate args */
331 base
= strtoul (chp
, &chp
, 0);
334 sim_io_eprintf (sd
, "--profile-pc-range missing BOUND argument\n");
337 bound
= strtoul (chp
+ 1, NULL
, 0);
338 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
340 PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = base
;
341 PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))) = bound
;
343 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
344 CPU_PROFILE_FLAGS (STATE_CPU (sd
, cpu_nr
))[PROFILE_PC_IDX
] = 1;
347 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
350 #ifdef SIM_HAVE_ADDR_RANGE
351 case OPTION_PROFILE_RANGE
:
355 unsigned long start
,end
;
356 start
= strtoul (chp
, &chp
, 0);
359 sim_io_eprintf (sd
, "--profile-range missing END argument\n");
362 end
= strtoul (chp
+ 1, NULL
, 0);
363 /* FIXME: Argument validation. */
365 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
368 for (cpu_nr
= 0; cpu_nr
< MAX_NR_PROCESSORS
; ++cpu_nr
)
369 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (STATE_CPU (sd
, cpu_nr
))),
373 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-range' ignored\n");
376 case OPTION_PROFILE_FUNCTION
:
379 /*wip: need to compute function range given name*/
382 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-function' ignored\n");
384 #endif /* SIM_HAVE_ADDR_RANGE */
390 /* PC profiling support */
392 #if WITH_PROFILE_PC_P
395 profile_pc_cleanup (SIM_DESC sd
)
398 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
400 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
401 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
402 if (PROFILE_PC_COUNT (data
) != NULL
)
403 zfree (PROFILE_PC_COUNT (data
));
404 PROFILE_PC_COUNT (data
) = NULL
;
405 if (PROFILE_PC_EVENT (data
) != NULL
)
406 sim_events_deschedule (sd
, PROFILE_PC_EVENT (data
));
407 PROFILE_PC_EVENT (data
) = NULL
;
413 profile_pc_uninstall (SIM_DESC sd
)
415 profile_pc_cleanup (sd
);
419 profile_pc_event (SIM_DESC sd
,
422 sim_cpu
*cpu
= (sim_cpu
*) data
;
423 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
426 switch (STATE_WATCHPOINTS (sd
)->sizeof_pc
)
428 case 2: pc
= *(unsigned_2
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
429 case 4: pc
= *(unsigned_4
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
430 case 8: pc
= *(unsigned_8
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
433 i
= (pc
- PROFILE_PC_START (profile
)) >> PROFILE_PC_SHIFT (profile
);
434 if (i
< PROFILE_PC_NR_BUCKETS (profile
))
435 PROFILE_PC_COUNT (profile
) [i
] += 1; /* Overflow? */
437 PROFILE_PC_COUNT (profile
) [PROFILE_PC_NR_BUCKETS (profile
)] += 1;
438 PROFILE_PC_EVENT (profile
) =
439 sim_events_schedule (sd
, PROFILE_PC_FREQ (profile
), profile_pc_event
, cpu
);
443 profile_pc_init (SIM_DESC sd
)
446 profile_pc_cleanup (sd
);
447 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
449 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
450 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
451 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
]
452 && STATE_WATCHPOINTS (sd
)->pc
!= NULL
)
455 /* fill in the frequency if not specified */
456 if (PROFILE_PC_FREQ (data
) == 0)
457 PROFILE_PC_FREQ (data
) = 256;
458 /* fill in the start/end if not specified */
459 if (PROFILE_PC_END (data
) == 0)
461 PROFILE_PC_START (data
) = STATE_TEXT_START (sd
);
462 PROFILE_PC_END (data
) = STATE_TEXT_END (sd
);
464 /* Compute the number of buckets if not specified. */
465 if (PROFILE_PC_NR_BUCKETS (data
) == 0)
467 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
468 PROFILE_PC_NR_BUCKETS (data
) = 16;
471 if (PROFILE_PC_END (data
) == 0)
473 /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
474 PROFILE_PC_NR_BUCKETS (data
) =
475 ((1 << (STATE_WATCHPOINTS (sd
)->sizeof_pc
) * (8 - 1))
476 / (PROFILE_PC_BUCKET_SIZE (data
) / 2));
480 PROFILE_PC_NR_BUCKETS (data
) =
481 ((PROFILE_PC_END (data
)
482 - PROFILE_PC_START (data
)
483 + PROFILE_PC_BUCKET_SIZE (data
) - 1)
484 / PROFILE_PC_BUCKET_SIZE (data
));
488 /* Compute the bucket size if not specified. Ensure that it
489 is rounded up to the next power of two */
490 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
492 if (PROFILE_PC_END (data
) == 0)
493 /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
494 bucket_size
= ((1 << ((STATE_WATCHPOINTS (sd
)->sizeof_pc
* 8) - 1))
495 / (PROFILE_PC_NR_BUCKETS (data
) / 2));
497 bucket_size
= ((PROFILE_PC_END (data
)
498 - PROFILE_PC_START (data
)
499 + PROFILE_PC_NR_BUCKETS (data
) - 1)
500 / PROFILE_PC_NR_BUCKETS (data
));
501 PROFILE_PC_SHIFT (data
) = 0;
502 while (bucket_size
< PROFILE_PC_BUCKET_SIZE (data
))
504 PROFILE_PC_SHIFT (data
) += 1;
507 /* Align the end address with bucket size */
508 if (PROFILE_PC_END (data
) != 0)
509 PROFILE_PC_END (data
) = (PROFILE_PC_START (data
)
510 + (PROFILE_PC_BUCKET_SIZE (data
)
511 * PROFILE_PC_NR_BUCKETS (data
)));
512 /* create the relevant buffers */
513 PROFILE_PC_COUNT (data
) =
514 NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data
) + 1);
515 PROFILE_PC_EVENT (data
) =
516 sim_events_schedule (sd
,
517 PROFILE_PC_FREQ (data
),
526 profile_print_pc (sim_cpu
*cpu
, int verbose
)
528 SIM_DESC sd
= CPU_STATE (cpu
);
529 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
535 if (PROFILE_PC_COUNT (profile
) == 0)
538 sim_io_printf (sd
, "Program Counter Statistics:\n\n");
540 /* First pass over data computes various things. */
543 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
545 total
+= PROFILE_PC_COUNT (profile
) [i
];
546 if (PROFILE_PC_COUNT (profile
) [i
] > max_val
)
547 max_val
= PROFILE_PC_COUNT (profile
) [i
];
550 sim_io_printf (sd
, " Total samples: %s\n",
552 sim_io_printf (sd
, " Granularity: %s bytes per bucket\n",
553 COMMAS (PROFILE_PC_BUCKET_SIZE (profile
)));
554 sim_io_printf (sd
, " Size: %s buckets\n",
555 COMMAS (PROFILE_PC_NR_BUCKETS (profile
)));
556 sim_io_printf (sd
, " Frequency: %s cycles per sample\n",
557 COMMAS (PROFILE_PC_FREQ (profile
)));
559 if (PROFILE_PC_END (profile
) != 0)
560 sim_io_printf (sd
, " Range: 0x%lx 0x%lx\n",
561 (long) PROFILE_PC_START (profile
),
562 (long) PROFILE_PC_END (profile
));
564 if (verbose
&& max_val
!= 0)
566 /* Now we can print the histogram. */
567 sim_io_printf (sd
, "\n");
568 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
570 if (PROFILE_PC_COUNT (profile
) [i
] != 0)
572 sim_io_printf (sd
, " ");
573 if (i
== PROFILE_PC_NR_BUCKETS (profile
))
574 sim_io_printf (sd
, "%10s:", "overflow");
576 sim_io_printf (sd
, "0x%08lx:",
577 (long) (PROFILE_PC_START (profile
)
578 + (i
* PROFILE_PC_BUCKET_SIZE (profile
))));
579 sim_io_printf (sd
, " %*s",
580 max_val
< 10000 ? 5 : 10,
581 COMMAS (PROFILE_PC_COUNT (profile
) [i
]));
582 sim_io_printf (sd
, " %4.1f",
583 (PROFILE_PC_COUNT (profile
) [i
] * 100.0) / total
);
584 sim_io_printf (sd
, ": ");
585 sim_profile_print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
586 PROFILE_PC_COUNT (profile
) [i
],
588 sim_io_printf (sd
, "\n");
593 /* dump the histogram to the file "gmon.out" using BSD's gprof file
595 /* Since a profile data file is in the native format of the host on
596 which the profile is being, endian issues are not considered in
598 /* FIXME: Is this the best place for this code? */
600 FILE *pf
= fopen ("gmon.out", "wb");
603 sim_io_eprintf (sd
, "Failed to open \"gmon.out\" profile file\n");
607 /* FIXME: what if the target has a 64 bit PC? */
608 unsigned32 header
[3];
610 if (PROFILE_PC_END (profile
) != 0)
612 header
[0] = PROFILE_PC_START (profile
);
613 header
[1] = PROFILE_PC_END (profile
);
620 /* size of sample buffer (+ header) */
621 header
[2] = PROFILE_PC_NR_BUCKETS (profile
) * 2 + sizeof (header
);
622 ok
= fwrite (&header
, sizeof (header
), 1, pf
);
624 ok
&& (loop
< PROFILE_PC_NR_BUCKETS (profile
));
628 if (PROFILE_PC_COUNT (profile
) [loop
] >= 0xffff)
631 sample
= PROFILE_PC_COUNT (profile
) [loop
];
632 ok
= fwrite (&sample
, sizeof (sample
), 1, pf
);
635 sim_io_eprintf (sd
, "Failed to write to \"gmon.out\" profile file\n");
640 sim_io_printf (sd
, "\n");
645 /* Summary printing support. */
647 #if WITH_PROFILE_INSN_P
650 profile_insn_init (SIM_DESC sd
)
654 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
656 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
658 if (CPU_MAX_INSNS (cpu
) > 0)
659 PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu
)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu
));
666 profile_print_insn (sim_cpu
*cpu
, int verbose
)
668 unsigned int i
, n
, total
, max_val
, max_name_len
;
669 SIM_DESC sd
= CPU_STATE (cpu
);
670 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
673 /* If MAX_INSNS not set, insn profiling isn't supported. */
674 if (CPU_MAX_INSNS (cpu
) == 0)
677 sim_io_printf (sd
, "Instruction Statistics");
678 #ifdef SIM_HAVE_ADDR_RANGE
679 if (PROFILE_RANGE (data
)->ranges
)
680 sim_io_printf (sd
, " (for selected address range(s))");
682 sim_io_printf (sd
, "\n\n");
684 /* First pass over data computes various things. */
688 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
690 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
694 total
+= PROFILE_INSN_COUNT (data
) [i
];
695 if (PROFILE_INSN_COUNT (data
) [i
] > max_val
)
696 max_val
= PROFILE_INSN_COUNT (data
) [i
];
698 if (n
> max_name_len
)
701 /* set the total insn count, in case client is being lazy */
702 if (! PROFILE_TOTAL_INSN_COUNT (data
))
703 PROFILE_TOTAL_INSN_COUNT (data
) = total
;
705 sim_io_printf (sd
, " Total: %s insns\n", COMMAS (total
));
707 if (verbose
&& max_val
!= 0)
709 /* Now we can print the histogram. */
710 sim_io_printf (sd
, "\n");
711 for (i
= 0; i
< CPU_MAX_INSNS (cpu
); ++i
)
713 const char *name
= (*CPU_INSN_NAME (cpu
)) (cpu
, i
);
717 if (PROFILE_INSN_COUNT (data
) [i
] != 0)
719 sim_io_printf (sd
, " %*s: %*s: ",
721 max_val
< 10000 ? 5 : 10,
722 COMMAS (PROFILE_INSN_COUNT (data
) [i
]));
723 sim_profile_print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
724 PROFILE_INSN_COUNT (data
) [i
],
726 sim_io_printf (sd
, "\n");
731 sim_io_printf (sd
, "\n");
736 #if WITH_PROFILE_MEMORY_P
739 profile_print_memory (sim_cpu
*cpu
, int verbose
)
742 unsigned int total_read
, total_write
;
743 unsigned int max_val
, max_name_len
;
744 /* FIXME: Need to add smp support. */
745 SIM_DESC sd
= CPU_STATE (cpu
);
746 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
749 sim_io_printf (sd
, "Memory Access Statistics\n\n");
751 /* First pass over data computes various things. */
752 max_val
= total_read
= total_write
= max_name_len
= 0;
753 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
755 total_read
+= PROFILE_READ_COUNT (data
) [i
];
756 total_write
+= PROFILE_WRITE_COUNT (data
) [i
];
757 if (PROFILE_READ_COUNT (data
) [i
] > max_val
)
758 max_val
= PROFILE_READ_COUNT (data
) [i
];
759 if (PROFILE_WRITE_COUNT (data
) [i
] > max_val
)
760 max_val
= PROFILE_WRITE_COUNT (data
) [i
];
761 n
= strlen (MODE_NAME (i
));
762 if (n
> max_name_len
)
766 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
767 sim_io_printf (sd
, " Total read: %s accesses\n",
768 COMMAS (total_read
));
769 sim_io_printf (sd
, " Total write: %s accesses\n",
770 COMMAS (total_write
));
772 if (verbose
&& max_val
!= 0)
774 /* FIXME: Need to separate instruction fetches from data fetches
775 as the former swamps the latter. */
776 /* Now we can print the histogram. */
777 sim_io_printf (sd
, "\n");
778 for (i
= 0; i
< MODE_TARGET_MAX
; ++i
)
780 if (PROFILE_READ_COUNT (data
) [i
] != 0)
782 sim_io_printf (sd
, " %*s read: %*s: ",
783 max_name_len
, MODE_NAME (i
),
784 max_val
< 10000 ? 5 : 10,
785 COMMAS (PROFILE_READ_COUNT (data
) [i
]));
786 sim_profile_print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
787 PROFILE_READ_COUNT (data
) [i
],
789 sim_io_printf (sd
, "\n");
791 if (PROFILE_WRITE_COUNT (data
) [i
] != 0)
793 sim_io_printf (sd
, " %*s write: %*s: ",
794 max_name_len
, MODE_NAME (i
),
795 max_val
< 10000 ? 5 : 10,
796 COMMAS (PROFILE_WRITE_COUNT (data
) [i
]));
797 sim_profile_print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
798 PROFILE_WRITE_COUNT (data
) [i
],
800 sim_io_printf (sd
, "\n");
805 sim_io_printf (sd
, "\n");
810 #if WITH_PROFILE_CORE_P
813 profile_print_core (sim_cpu
*cpu
, int verbose
)
816 unsigned int max_val
;
817 /* FIXME: Need to add smp support. */
818 SIM_DESC sd
= CPU_STATE (cpu
);
819 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
822 sim_io_printf (sd
, "CORE Statistics\n\n");
824 /* First pass over data computes various things. */
829 for (map
= 0; map
< nr_maps
; map
++)
831 total
+= PROFILE_CORE_COUNT (data
) [map
];
832 if (PROFILE_CORE_COUNT (data
) [map
] > max_val
)
833 max_val
= PROFILE_CORE_COUNT (data
) [map
];
837 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
838 sim_io_printf (sd
, " Total: %s accesses\n",
841 if (verbose
&& max_val
!= 0)
844 /* Now we can print the histogram. */
845 sim_io_printf (sd
, "\n");
846 for (map
= 0; map
< nr_maps
; map
++)
848 if (PROFILE_CORE_COUNT (data
) [map
] != 0)
850 sim_io_printf (sd
, "%10s:", map_to_str (map
));
851 sim_io_printf (sd
, "%*s: ",
852 max_val
< 10000 ? 5 : 10,
853 COMMAS (PROFILE_CORE_COUNT (data
) [map
]));
854 sim_profile_print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
855 PROFILE_CORE_COUNT (data
) [map
],
857 sim_io_printf (sd
, "\n");
862 sim_io_printf (sd
, "\n");
867 #if WITH_PROFILE_MODEL_P
870 profile_print_model (sim_cpu
*cpu
, int verbose
)
872 SIM_DESC sd
= CPU_STATE (cpu
);
873 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
874 unsigned long cti_stall_cycles
= PROFILE_MODEL_CTI_STALL_CYCLES (data
);
875 unsigned long load_stall_cycles
= PROFILE_MODEL_LOAD_STALL_CYCLES (data
);
876 unsigned long total_cycles
= PROFILE_MODEL_TOTAL_CYCLES (data
);
879 sim_io_printf (sd
, "Model %s Timing Information",
880 MODEL_NAME (CPU_MODEL (cpu
)));
881 #ifdef SIM_HAVE_ADDR_RANGE
882 if (PROFILE_RANGE (data
)->ranges
)
883 sim_io_printf (sd
, " (for selected address range(s))");
885 sim_io_printf (sd
, "\n\n");
886 sim_io_printf (sd
, " %-*s %s\n",
887 PROFILE_LABEL_WIDTH
, "Taken branches:",
888 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data
)));
889 sim_io_printf (sd
, " %-*s %s\n",
890 PROFILE_LABEL_WIDTH
, "Untaken branches:",
891 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data
)));
892 sim_io_printf (sd
, " %-*s %s\n",
893 PROFILE_LABEL_WIDTH
, "Cycles stalled due to branches:",
894 COMMAS (cti_stall_cycles
));
895 sim_io_printf (sd
, " %-*s %s\n",
896 PROFILE_LABEL_WIDTH
, "Cycles stalled due to loads:",
897 COMMAS (load_stall_cycles
));
898 sim_io_printf (sd
, " %-*s %s\n",
899 PROFILE_LABEL_WIDTH
, "Total cycles (*approximate*):",
900 COMMAS (total_cycles
));
901 sim_io_printf (sd
, "\n");
907 sim_profile_print_bar (SIM_DESC sd
, unsigned int width
,
908 unsigned int val
, unsigned int max_val
)
910 unsigned int i
, count
;
912 count
= ((double) val
/ (double) max_val
) * (double) width
;
914 for (i
= 0; i
< count
; ++i
)
915 sim_io_printf (sd
, "*");
918 /* Print the simulator's execution speed for CPU. */
921 profile_print_speed (sim_cpu
*cpu
)
923 SIM_DESC sd
= CPU_STATE (cpu
);
924 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
925 unsigned long milliseconds
= sim_events_elapsed_time (sd
);
926 unsigned long total
= PROFILE_TOTAL_INSN_COUNT (data
);
929 sim_io_printf (sd
, "Simulator Execution Speed\n\n");
932 sim_io_printf (sd
, " Total instructions: %s\n", COMMAS (total
));
934 if (milliseconds
< 1000)
935 sim_io_printf (sd
, " Total execution time: < 1 second\n\n");
938 /* The printing of the time rounded to 2 decimal places makes the speed
939 calculation seem incorrect [even though it is correct]. So round
940 MILLISECONDS first. This can marginally affect the result, but it's
941 better that the user not perceive there's a math error. */
942 double secs
= (double) milliseconds
/ 1000;
943 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
944 sim_io_printf (sd
, " Total execution time: %.2f seconds\n", secs
);
945 /* Don't confuse things with data that isn't useful.
946 If we ran for less than 2 seconds, only use the data if we
947 executed more than 100,000 insns. */
948 if (secs
>= 2 || total
>= 100000)
949 sim_io_printf (sd
, " Simulator speed: %s insns/second\n\n",
950 COMMAS ((unsigned long) ((double) total
/ secs
)));
954 /* Print selected address ranges. */
957 profile_print_addr_ranges (sim_cpu
*cpu
)
959 ADDR_SUBRANGE
*asr
= PROFILE_RANGE (CPU_PROFILE_DATA (cpu
))->ranges
;
960 SIM_DESC sd
= CPU_STATE (cpu
);
964 sim_io_printf (sd
, "Selected address ranges\n\n");
967 sim_io_printf (sd
, " 0x%lx - 0x%lx\n",
968 (long) asr
->start
, (long) asr
->end
);
971 sim_io_printf (sd
, "\n");
975 /* Top level function to print all summary profile information.
976 It is [currently] intended that all such data is printed by this function.
977 I'd rather keep it all in one place for now. To that end, MISC_CPU and
978 MISC are callbacks used to print any miscellaneous data.
980 One might want to add a user option that allows printing by type or by cpu
981 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
982 This may be a case of featuritis so it's currently left out.
984 Note that results are indented two spaces to distinguish them from
988 profile_info (SIM_DESC sd
, int verbose
)
991 int print_title_p
= 0;
993 /* Only print the title if some data has been collected. */
994 /* ??? Why don't we just exit if no data collected? */
995 /* FIXME: If the number of processors can be selected on the command line,
996 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
998 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
1000 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1001 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1003 for (i
= 0; i
< MAX_PROFILE_VALUES
; ++i
)
1004 if (PROFILE_FLAGS (data
) [i
])
1006 /* One could break out early if print_title_p is set. */
1009 sim_io_printf (sd
, "Summary profiling results:\n\n");
1011 /* Loop, cpu by cpu, printing results. */
1013 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
1015 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
1016 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1018 if (MAX_NR_PROCESSORS
> 1
1020 #if WITH_PROFILE_INSN_P
1021 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
]
1023 #if WITH_PROFILE_MEMORY_P
1024 || PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
]
1026 #if WITH_PROFILE_CORE_P
1027 || PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
]
1029 #if WITH_PROFILE_MODEL_P
1030 || PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
]
1032 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1033 || PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
]
1035 #if WITH_PROFILE_PC_P
1036 || PROFILE_FLAGS (data
) [PROFILE_PC_IDX
]
1040 sim_io_printf (sd
, "CPU %d\n\n", c
);
1043 #ifdef SIM_HAVE_ADDR_RANGE
1045 && (PROFILE_INSN_P (cpu
)
1046 || PROFILE_MODEL_P (cpu
)))
1047 profile_print_addr_ranges (cpu
);
1050 #if WITH_PROFILE_INSN_P
1051 if (PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1052 profile_print_insn (cpu
, verbose
);
1055 #if WITH_PROFILE_MEMORY_P
1056 if (PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
])
1057 profile_print_memory (cpu
, verbose
);
1060 #if WITH_PROFILE_CORE_P
1061 if (PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
])
1062 profile_print_core (cpu
, verbose
);
1065 #if WITH_PROFILE_MODEL_P
1066 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
1067 profile_print_model (cpu
, verbose
);
1070 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
1071 if (PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
])
1072 scache_print_profile (cpu
, verbose
);
1075 #if WITH_PROFILE_PC_P
1076 if (PROFILE_FLAGS (data
) [PROFILE_PC_IDX
])
1077 profile_print_pc (cpu
, verbose
);
1080 /* Print cpu-specific data before the execution speed. */
1081 if (PROFILE_INFO_CPU_CALLBACK (data
) != NULL
)
1082 PROFILE_INFO_CPU_CALLBACK (data
) (cpu
, verbose
);
1084 /* Always try to print execution time and speed. */
1086 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
1087 profile_print_speed (cpu
);
1090 /* Finally print non-cpu specific miscellaneous data. */
1091 if (STATE_PROFILE_INFO_CALLBACK (sd
))
1092 STATE_PROFILE_INFO_CALLBACK (sd
) (sd
, verbose
);
1096 /* Install profiling support in the simulator. */
1099 profile_install (SIM_DESC sd
)
1103 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1104 sim_add_option_table (sd
, NULL
, profile_options
);
1105 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1106 memset (CPU_PROFILE_DATA (STATE_CPU (sd
, i
)), 0,
1107 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd
, i
))));
1108 #if WITH_PROFILE_INSN_P
1109 sim_module_add_init_fn (sd
, profile_insn_init
);
1111 #if WITH_PROFILE_PC_P
1112 sim_module_add_uninstall_fn (sd
, profile_pc_uninstall
);
1113 sim_module_add_init_fn (sd
, profile_pc_init
);
1115 sim_module_add_init_fn (sd
, profile_init
);
1116 sim_module_add_uninstall_fn (sd
, profile_uninstall
);
1117 sim_module_add_info_fn (sd
, profile_info
);
1122 profile_init (SIM_DESC sd
)
1124 #ifdef SIM_HAVE_ADDR_RANGE
1125 /* Check if a range has been specified without specifying what to
1130 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1132 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1134 if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)))
1135 && ! (PROFILE_INSN_P (cpu
)
1136 || PROFILE_MODEL_P (cpu
)))
1138 sim_io_eprintf_cpu (cpu
, "Profiling address range specified without --profile-insn or --profile-model.\n");
1139 sim_io_eprintf_cpu (cpu
, "Address range ignored.\n");
1140 sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu
)),
1141 0, ~ (address_word
) 0);
1151 profile_uninstall (SIM_DESC sd
)
1155 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
1157 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
1158 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
1160 if (PROFILE_FILE (data
) != NULL
)
1162 /* If output from different cpus is going to the same file,
1163 avoid closing the file twice. */
1164 for (j
= 0; j
< i
; ++j
)
1165 if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, j
)))
1166 == PROFILE_FILE (data
))
1169 fclose (PROFILE_FILE (data
));
1172 if (PROFILE_INSN_COUNT (data
) != NULL
)
1173 zfree (PROFILE_INSN_COUNT (data
));