1 /* Default profiling support.
2 Copyright (C) 1996, 1997 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_UNINSTALL_FN profile_uninstall
;
42 #if WITH_PROFILE_INSN_P || WITH_PROFILE_MEMORY_P || WITH_PROFILE_CORE_P || WITH_PROFILE_PC_P
43 static void print_bar (SIM_DESC
, unsigned int, unsigned int, unsigned int);
46 static DECLARE_OPTION_HANDLER (profile_option_handler
);
48 #define OPTION_PROFILE_INSN (OPTION_START + 0)
49 #define OPTION_PROFILE_MEMORY (OPTION_START + 1)
50 #define OPTION_PROFILE_MODEL (OPTION_START + 2)
51 #define OPTION_PROFILE_FILE (OPTION_START + 3)
52 #define OPTION_PROFILE_RANGE (OPTION_START + 4)
53 #define OPTION_PROFILE_CORE (OPTION_START + 5)
54 #define OPTION_PROFILE_PC (OPTION_START + 6)
55 #define OPTION_PROFILE_PC_RANGE (OPTION_START + 7)
56 #define OPTION_PROFILE_PC_GRANULARITY (OPTION_START + 8)
58 static const OPTION profile_options
[] = {
59 { {"profile", no_argument
, NULL
, 'p'},
60 'p', NULL
, "Perform profiling",
61 profile_option_handler
},
62 { {"profile-insn", no_argument
, NULL
, OPTION_PROFILE_INSN
},
63 '\0', NULL
, "Perform instruction profiling",
64 profile_option_handler
},
65 { {"profile-memory", no_argument
, NULL
, OPTION_PROFILE_MEMORY
},
66 '\0', NULL
, "Perform memory profiling",
67 profile_option_handler
},
68 { {"profile-core", no_argument
, NULL
, OPTION_PROFILE_CORE
},
69 '\0', NULL
, "Perform CORE profiling",
70 profile_option_handler
},
71 { {"profile-model", no_argument
, NULL
, OPTION_PROFILE_MODEL
},
72 '\0', NULL
, "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", no_argument
, NULL
, OPTION_PROFILE_PC
},
80 '\0', NULL
, "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
},
96 { {"profile-range", required_argument
, NULL
, OPTION_PROFILE_RANGE
},
97 0, NULL
, "Specify range of addresses to profile",
98 profile_option_handler
},
101 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
105 profile_option_handler (SIM_DESC sd
,
116 sim_io_eprintf (sd
, "Profiling not compiled in, -p option ignored\n");
119 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
120 for (i
= 0; i
< MAX_PROFILE_VALUES
; ++i
)
121 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[i
] = 1;
125 case OPTION_PROFILE_INSN
:
126 #if WITH_PROFILE_INSN_P
127 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
128 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_INSN_IDX
] = 1;
130 sim_io_eprintf (sd
, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
134 case OPTION_PROFILE_MEMORY
:
135 #if WITH_PROFILE_MEMORY_P
136 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
137 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_MEMORY_IDX
] = 1;
139 sim_io_eprintf (sd
, "Memory profiling not compiled in, `--profile-memory' ignored\n");
143 case OPTION_PROFILE_CORE
:
144 #if WITH_PROFILE_CORE_P
145 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
146 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_CORE_IDX
] = 1;
148 sim_io_eprintf (sd
, "CORE profiling not compiled in, `--profile-core' ignored\n");
152 case OPTION_PROFILE_MODEL
:
153 #if WITH_PROFILE_MODEL_P
154 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
155 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_MODEL_IDX
] = 1;
157 sim_io_eprintf (sd
, "Model profiling not compiled in, `--profile-model' ignored\n");
161 case OPTION_PROFILE_FILE
:
162 /* FIXME: Might want this to apply to pc profiling only,
163 or have two profile file options. */
165 sim_io_eprintf (sd
, "Profiling not compiled in, `--profile-file' ignored\n");
168 FILE *f
= fopen (arg
, "w");
172 sim_io_eprintf (sd
, "Unable to open profile output file `%s'\n", arg
);
175 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
176 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, n
))) = f
;
180 case OPTION_PROFILE_PC
:
181 if (WITH_PROFILE_PC_P
)
183 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
184 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
] = 1;
187 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc' ignored\n");
191 if (WITH_PROFILE_PC_P
)
193 /* FIXME: Validate arg. */
195 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
196 PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd
, n
))) = i
;
197 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
198 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
] = 1;
201 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
205 if (WITH_PROFILE_PC_P
)
207 /* FIXME: Validate arg. */
209 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
210 PROFILE_PC_NR_BUCKETS (CPU_PROFILE_DATA (STATE_CPU (sd
, n
))) = i
;
211 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
212 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
] = 1;
215 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
218 case OPTION_PROFILE_PC_GRANULARITY
:
219 if (WITH_PROFILE_PC_P
)
223 /* check that the granularity is a power of two */
225 while (i
> (1 << shift
))
229 if (i
!= (1 << shift
))
231 sim_io_eprintf (sd
, "PC profiling granularity not a power of two\n");
236 sim_io_eprintf (sd
, "PC profiling granularity too small");
239 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
240 PROFILE_PC_SHIFT (CPU_PROFILE_DATA (STATE_CPU (sd
, n
))) = shift
;
241 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
242 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
] = 1;
245 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
248 case OPTION_PROFILE_PC_RANGE
:
249 if (WITH_PROFILE_PC_P
)
251 /* FIXME: Validate args */
255 base
= strtoul (chp
, &chp
, 0);
258 sim_io_eprintf (sd
, "--profile-pc-range missing BOUND argument\n");
261 bound
= strtoul (chp
+ 1, NULL
, 0);
262 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
264 PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd
, n
))) = base
;
265 PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd
, n
))) = bound
;
267 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
268 CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
] = 1;
271 sim_io_eprintf (sd
, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
274 #if 0 /* FIXME:wip */
275 case OPTION_PROFILE_RANGE
:
283 /* PC profiling support */
285 #if WITH_PROFILE_PC_P
288 profile_pc_cleanup (SIM_DESC sd
)
291 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
293 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
294 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
295 if (PROFILE_PC_COUNT (data
) != NULL
)
296 zfree (PROFILE_PC_COUNT (data
));
297 PROFILE_PC_COUNT (data
) = NULL
;
298 if (PROFILE_PC_EVENT (data
) != NULL
)
299 sim_events_deschedule (sd
, PROFILE_PC_EVENT (data
));
300 PROFILE_PC_EVENT (data
) = NULL
;
306 profile_pc_uninstall (SIM_DESC sd
)
308 profile_pc_cleanup (sd
);
312 profile_pc_event (SIM_DESC sd
,
315 sim_cpu
*cpu
= (sim_cpu
*) data
;
316 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
319 switch (STATE_WATCHPOINTS (sd
)->sizeof_pc
)
321 case 2: pc
= *(unsigned_2
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
322 case 4: pc
= *(unsigned_4
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
323 case 8: pc
= *(unsigned_8
*)(STATE_WATCHPOINTS (sd
)->pc
) ; break;
326 i
= (pc
- PROFILE_PC_START (profile
)) >> PROFILE_PC_SHIFT (profile
);
327 if (i
< PROFILE_PC_NR_BUCKETS (profile
))
328 PROFILE_PC_COUNT (profile
) [i
] += 1; /* Overflow? */
330 PROFILE_PC_COUNT (profile
) [PROFILE_PC_NR_BUCKETS (profile
)] += 1;
331 PROFILE_PC_EVENT (profile
) =
332 sim_events_schedule (sd
, PROFILE_PC_FREQ (profile
), profile_pc_event
, cpu
);
336 profile_pc_init (SIM_DESC sd
)
339 profile_pc_cleanup (sd
);
340 for (n
= 0; n
< MAX_NR_PROCESSORS
; n
++)
342 sim_cpu
*cpu
= STATE_CPU (sd
, n
);
343 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
344 if (CPU_PROFILE_FLAGS (STATE_CPU (sd
, n
))[PROFILE_PC_IDX
]
345 && STATE_WATCHPOINTS (sd
)->pc
!= NULL
)
348 /* fill in the frequency if not specified */
349 if (PROFILE_PC_FREQ (data
) == 0)
350 PROFILE_PC_FREQ (data
) = 256;
351 /* fill in the start/end if not specified */
352 if (PROFILE_PC_END (data
) == 0)
354 PROFILE_PC_START (data
) = STATE_TEXT_START (sd
);
355 PROFILE_PC_END (data
) = STATE_TEXT_END (sd
);
357 /* Compute the number of buckets if not specified. */
358 if (PROFILE_PC_NR_BUCKETS (data
) == 0)
360 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
361 PROFILE_PC_NR_BUCKETS (data
) = 16;
364 if (PROFILE_PC_END (data
) == 0)
366 /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
367 PROFILE_PC_NR_BUCKETS (data
) =
368 ((1 << (STATE_WATCHPOINTS (sd
)->sizeof_pc
) * (8 - 1))
369 / (PROFILE_PC_BUCKET_SIZE (data
) / 2));
373 PROFILE_PC_NR_BUCKETS (data
) =
374 ((PROFILE_PC_END (data
)
375 - PROFILE_PC_START (data
)
376 + PROFILE_PC_BUCKET_SIZE (data
) - 1)
377 / PROFILE_PC_BUCKET_SIZE (data
));
381 /* Compute the bucket size if not specified. Ensure that it
382 is rounded up to the next power of two */
383 if (PROFILE_PC_BUCKET_SIZE (data
) == 0)
385 if (PROFILE_PC_END (data
) == 0)
386 /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
387 bucket_size
= ((1 << ((STATE_WATCHPOINTS (sd
)->sizeof_pc
* 8) - 1))
388 / (PROFILE_PC_NR_BUCKETS (data
) / 2));
390 bucket_size
= ((PROFILE_PC_END (data
)
391 - PROFILE_PC_START (data
)
392 + PROFILE_PC_NR_BUCKETS (data
) - 1)
393 / PROFILE_PC_NR_BUCKETS (data
));
394 PROFILE_PC_SHIFT (data
) = 0;
395 while (bucket_size
< PROFILE_PC_BUCKET_SIZE (data
))
397 PROFILE_PC_SHIFT (data
) += 1;
400 /* Align the end address with bucket size */
401 if (PROFILE_PC_END (data
) != 0)
402 PROFILE_PC_END (data
) = (PROFILE_PC_START (data
)
403 + (PROFILE_PC_BUCKET_SIZE (data
)
404 * PROFILE_PC_NR_BUCKETS (data
)));
405 /* create the relevant buffers */
406 PROFILE_PC_COUNT (data
) =
407 NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data
) + 1);
408 PROFILE_PC_EVENT (data
) =
409 sim_events_schedule (sd
,
410 PROFILE_PC_FREQ (data
),
419 profile_print_pc (sim_cpu
*cpu
, int verbose
)
421 SIM_DESC sd
= CPU_STATE (cpu
);
422 PROFILE_DATA
*profile
= CPU_PROFILE_DATA (cpu
);
428 sim_io_printf (sd
, "Program Counter Statistics:\n\n");
430 /* First pass over data computes various things. */
433 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
435 total
+= PROFILE_PC_COUNT (profile
) [i
];
436 if (PROFILE_PC_COUNT (profile
) [i
] > max_val
)
437 max_val
= PROFILE_PC_COUNT (profile
) [i
];
440 sim_io_printf (sd
, " Total samples: %s\n",
442 sim_io_printf (sd
, " Granularity: %s bytes per bucket\n",
443 COMMAS (PROFILE_PC_BUCKET_SIZE (profile
)));
444 sim_io_printf (sd
, " Size: %s buckets\n",
445 COMMAS (PROFILE_PC_NR_BUCKETS (profile
)));
446 sim_io_printf (sd
, " Frequency: %s cycles per sample\n",
447 COMMAS (PROFILE_PC_FREQ (profile
)));
449 if (PROFILE_PC_END (profile
) != 0)
450 sim_io_printf (sd
, " Range: 0x%lx 0x%lx\n",
451 (long) PROFILE_PC_START (profile
),
452 (long) PROFILE_PC_END (profile
));
454 if (verbose
&& max_val
!= 0)
456 /* Now we can print the histogram. */
457 sim_io_printf (sd
, "\n");
458 for (i
= 0; i
<= PROFILE_PC_NR_BUCKETS (profile
); ++i
)
460 if (PROFILE_PC_COUNT (profile
) [i
] != 0)
462 sim_io_printf (sd
, " ");
463 if (i
== PROFILE_PC_NR_BUCKETS (profile
))
464 sim_io_printf (sd
, "%10s:", "overflow");
466 sim_io_printf (sd
, "0x%08lx:",
467 (long) (PROFILE_PC_START (profile
)
468 + (i
* PROFILE_PC_BUCKET_SIZE (profile
))));
469 sim_io_printf (sd
, " %*s",
470 max_val
< 10000 ? 5 : 10,
471 COMMAS (PROFILE_PC_COUNT (profile
) [i
]));
472 sim_io_printf (sd
, " %4.1f",
473 (PROFILE_PC_COUNT (profile
) [i
] * 100.0) / total
);
474 sim_io_printf (sd
, ": ");
475 print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
476 PROFILE_PC_COUNT (profile
) [i
],
478 sim_io_printf (sd
, "\n");
483 /* dump the histogram to the file "gmon.out" using BSD's gprof file
485 /* Since a profile data file is in the native format of the host on
486 which the profile is being, endian issues are not considered in
488 /* FIXME: Is this the best place for this code? */
490 FILE *pf
= fopen ("gmon.out", "wb");
493 sim_io_eprintf (sd
, "Failed to open \"gmon.out\" profile file\n");
497 /* FIXME: what if the target has a 64 bit PC? */
498 unsigned32 header
[3];
500 if (PROFILE_PC_END (profile
) != 0)
502 header
[0] = PROFILE_PC_START (profile
);
503 header
[1] = PROFILE_PC_END (profile
);
510 /* size of sample buffer (+ header) */
511 header
[2] = PROFILE_PC_NR_BUCKETS (profile
) * 2 + sizeof (header
);
512 ok
= fwrite (&header
, sizeof (header
), 1, pf
);
514 ok
&& (loop
< PROFILE_PC_NR_BUCKETS (profile
));
518 if (PROFILE_PC_COUNT (profile
) [loop
] >= 0xffff)
521 sample
= PROFILE_PC_COUNT (profile
) [loop
];
522 ok
= fwrite (&sample
, sizeof (sample
), 1, pf
);
525 sim_io_eprintf (sd
, "Failed to write to \"gmon.out\" profile file\n");
530 sim_io_printf (sd
, "\n");
535 /* Summary printing support. */
537 #if WITH_PROFILE_INSN_P
540 profile_print_insn (sim_cpu
*cpu
, int verbose
)
542 unsigned int i
, n
, total
, max_val
, max_name_len
;
543 SIM_DESC sd
= CPU_STATE (cpu
);
544 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
547 sim_io_printf (sd
, "Instruction Statistics:\n\n");
549 /* First pass over data computes various things. */
553 for (i
= 0; i
< MAX_INSNS
; ++i
)
555 if (INSN_NAME (i
) == NULL
)
557 total
+= PROFILE_INSN_COUNT (data
) [i
];
558 if (PROFILE_INSN_COUNT (data
) [i
] > max_val
)
559 max_val
= PROFILE_INSN_COUNT (data
) [i
];
560 n
= strlen (INSN_NAME (i
));
561 if (n
> max_name_len
)
564 /* set the total insn count, in case client is being lazy */
565 if (PROFILE_TOTAL_INSN_COUNT (data
))
566 PROFILE_TOTAL_INSN_COUNT (data
) = total
;
568 sim_io_printf (sd
, " Total: %s insns\n", COMMAS (total
));
570 if (verbose
&& max_val
!= 0)
572 /* Now we can print the histogram. */
573 sim_io_printf (sd
, "\n");
574 for (i
= 0; i
< MAX_INSNS
; ++i
)
576 if (INSN_NAME (i
) == NULL
)
578 if (PROFILE_INSN_COUNT (data
) [i
] != 0)
580 sim_io_printf (sd
, " %*s: %*s: ",
581 max_name_len
, INSN_NAME (i
),
582 max_val
< 10000 ? 5 : 10,
583 COMMAS (PROFILE_INSN_COUNT (data
) [i
]));
584 print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
585 PROFILE_INSN_COUNT (data
) [i
],
587 sim_io_printf (sd
, "\n");
592 sim_io_printf (sd
, "\n");
597 #if WITH_PROFILE_MEMORY_P
600 profile_print_memory (sim_cpu
*cpu
, int verbose
)
603 unsigned int total_read
, total_write
;
604 unsigned int max_val
, max_name_len
;
605 /* FIXME: Need to add smp support. */
606 SIM_DESC sd
= CPU_STATE (cpu
);
607 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
610 sim_io_printf (sd
, "Memory Access Statistics:\n\n");
612 /* First pass over data computes various things. */
613 max_val
= total_read
= total_write
= max_name_len
= 0;
614 for (i
= 0; i
< MAX_MODES
; ++i
)
616 total_read
+= PROFILE_READ_COUNT (data
) [i
];
617 total_write
+= PROFILE_WRITE_COUNT (data
) [i
];
618 if (PROFILE_READ_COUNT (data
) [i
] > max_val
)
619 max_val
= PROFILE_READ_COUNT (data
) [i
];
620 if (PROFILE_WRITE_COUNT (data
) [i
] > max_val
)
621 max_val
= PROFILE_WRITE_COUNT (data
) [i
];
622 n
= strlen (MODE_NAME (i
));
623 if (n
> max_name_len
)
627 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
628 sim_io_printf (sd
, " Total read: %s accesses\n",
629 COMMAS (total_read
));
630 sim_io_printf (sd
, " Total write: %s accesses\n",
631 COMMAS (total_write
));
633 if (verbose
&& max_val
!= 0)
635 /* FIXME: Need to separate instruction fetches from data fetches
636 as the former swamps the latter. */
637 /* Now we can print the histogram. */
638 sim_io_printf (sd
, "\n");
639 for (i
= 0; i
< MAX_MODES
; ++i
)
641 if (PROFILE_READ_COUNT (data
) [i
] != 0)
643 sim_io_printf (sd
, " %*s read: %*s: ",
644 max_name_len
, MODE_NAME (i
),
645 max_val
< 10000 ? 5 : 10,
646 COMMAS (PROFILE_READ_COUNT (data
) [i
]));
647 print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
648 PROFILE_READ_COUNT (data
) [i
],
650 sim_io_printf (sd
, "\n");
652 if (PROFILE_WRITE_COUNT (data
) [i
] != 0)
654 sim_io_printf (sd
, " %*s write: %*s: ",
655 max_name_len
, MODE_NAME (i
),
656 max_val
< 10000 ? 5 : 10,
657 COMMAS (PROFILE_WRITE_COUNT (data
) [i
]));
658 print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
659 PROFILE_WRITE_COUNT (data
) [i
],
661 sim_io_printf (sd
, "\n");
666 sim_io_printf (sd
, "\n");
671 #if WITH_PROFILE_CORE_P
674 profile_print_core (sim_cpu
*cpu
, int verbose
)
677 unsigned int max_val
;
678 /* FIXME: Need to add smp support. */
679 SIM_DESC sd
= CPU_STATE (cpu
);
680 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
683 sim_io_printf (sd
, "CORE Statistics:\n\n");
685 /* First pass over data computes various things. */
690 for (map
= 0; map
< nr_sim_core_maps
; map
++)
692 total
+= PROFILE_CORE_COUNT (data
) [map
];
693 if (PROFILE_CORE_COUNT (data
) [map
] > max_val
)
694 max_val
= PROFILE_CORE_COUNT (data
) [map
];
698 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
699 sim_io_printf (sd
, " Total: %s accesses\n",
702 if (verbose
&& max_val
!= 0)
705 /* Now we can print the histogram. */
706 sim_io_printf (sd
, "\n");
707 for (map
= 0; map
< nr_sim_core_maps
; map
++)
709 if (PROFILE_CORE_COUNT (data
) [map
] != 0)
713 case sim_core_read_map
:
714 sim_io_printf (sd
, " read:");
716 case sim_core_write_map
:
717 sim_io_printf (sd
, " write:");
719 case sim_core_execute_map
:
720 sim_io_printf (sd
, " exec:");
722 case nr_sim_core_maps
:
725 sim_io_printf (sd
, "%*s: ",
726 max_val
< 10000 ? 5 : 10,
727 COMMAS (PROFILE_CORE_COUNT (data
) [map
]));
728 print_bar (sd
, PROFILE_HISTOGRAM_WIDTH
,
729 PROFILE_CORE_COUNT (data
) [map
],
731 sim_io_printf (sd
, "\n");
736 sim_io_printf (sd
, "\n");
741 #if WITH_PROFILE_MODEL_P
744 profile_print_model (sim_cpu
*cpu
, int verbose
)
746 SIM_DESC sd
= CPU_STATE (cpu
);
747 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
748 unsigned long cti_stalls
= PROFILE_MODEL_CTI_STALL_COUNT (data
);
749 unsigned long load_stalls
= PROFILE_MODEL_LOAD_STALL_COUNT (data
);
750 unsigned long total
= PROFILE_MODEL_CYCLE_COUNT (data
)
751 + cti_stalls
+ load_stalls
;
754 sim_io_printf (sd
, "Model %s Timing Information\n\n",
755 MODEL_NAME (CPU_MODEL (cpu
)));
756 sim_io_printf (sd
, " %-*s %s\n",
757 PROFILE_LABEL_WIDTH
, "Taken branches:",
758 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data
)));
759 sim_io_printf (sd
, " %-*s %s\n",
760 PROFILE_LABEL_WIDTH
, "Untaken branches:",
761 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data
)));
762 sim_io_printf (sd
, " %-*s %s\n",
763 PROFILE_LABEL_WIDTH
, "Cycles stalled due to branches:",
764 COMMAS (cti_stalls
));
765 sim_io_printf (sd
, " %-*s %s\n",
766 PROFILE_LABEL_WIDTH
, "Cycles stalled due to loads:",
767 COMMAS (load_stalls
));
768 sim_io_printf (sd
, " %-*s %s\n",
769 PROFILE_LABEL_WIDTH
, "Total cycles (*approximate*):",
771 sim_io_printf (sd
, "\n");
777 #if WITH_PROFILE_INSN_P || WITH_PROFILE_MEMORY_P || WITH_PROFILE_CORE_P || WITH_PROFILE_PC_P
780 print_bar (SIM_DESC sd
, unsigned int width
,
781 unsigned int val
, unsigned int max_val
)
783 unsigned int i
, count
;
785 count
= ((double) val
/ (double) max_val
) * (double) width
;
787 for (i
= 0; i
< count
; ++i
)
788 sim_io_printf (sd
, "*");
793 /* Print the simulator's execution speed for CPU. */
796 profile_print_speed (sim_cpu
*cpu
)
798 SIM_DESC sd
= CPU_STATE (cpu
);
799 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
800 unsigned long milliseconds
= sim_events_elapsed_time (sd
);
801 unsigned long total
= PROFILE_TOTAL_INSN_COUNT (data
);
804 sim_io_printf (sd
, "Simulator Execution Speed\n\n");
807 sim_io_printf (sd
, " Total instructions: %s\n", COMMAS (total
));
809 if (milliseconds
< 1000)
810 sim_io_printf (sd
, " Total Execution Time: < 1 second\n\n");
813 /* The printing of the time rounded to 2 decimal places makes the speed
814 calculation seem incorrect [even though it is correct]. So round
815 MILLISECONDS first. This can marginally affect the result, but it's
816 better that the user not perceive there's a math error. */
817 double secs
= (double) milliseconds
/ 1000;
818 secs
= ((double) (unsigned long) (secs
* 100 + .5)) / 100;
819 sim_io_printf (sd
, " Total Execution Time: %.2f seconds\n", secs
);
820 /* Don't confuse things with data that isn't useful.
821 If we ran for less than 2 seconds, only use the data if we
822 executed more than 100,000 insns. */
823 if (secs
>= 2 || total
>= 100000)
824 sim_io_printf (sd
, " Simulator Speed: %s insns/second\n\n",
825 COMMAS ((unsigned long) ((double) total
/ secs
)));
829 /* Top level function to print all summary profile information.
830 It is [currently] intended that all such data is printed by this function.
831 I'd rather keep it all in one place for now. To that end, MISC_CPU and
832 MISC are callbacks used to print any miscellaneous data.
834 One might want to add a user option that allows printing by type or by cpu
835 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
836 This may be a case of featuritis so it's currently left out.
838 Note that results are indented two spaces to distinguish them from
842 profile_print (SIM_DESC sd
, int verbose
,
843 PROFILE_CALLBACK
*misc
, PROFILE_CPU_CALLBACK
*misc_cpu
)
846 int print_title_p
= 0;
848 /* Only print the title if some data has been collected. */
849 /* FIXME: If the number of processors can be selected on the command line,
850 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
852 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
854 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
855 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
857 for (i
= 0; i
< MAX_PROFILE_VALUES
; ++i
)
858 if (PROFILE_FLAGS (data
) [i
])
860 /* One could break out early if print_title_p is set. */
863 sim_io_printf (sd
, "Summary profiling results:\n\n");
865 /* Loop, cpu by cpu, printing results. */
867 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
869 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
870 PROFILE_DATA
*data
= CPU_PROFILE_DATA (cpu
);
872 if (MAX_NR_PROCESSORS
> 1)
873 sim_io_printf (sd
, "CPU %d\n\n", c
);
875 #if WITH_PROFILE_INSN_P
876 if (PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
877 profile_print_insn (cpu
, verbose
);
880 #if WITH_PROFILE_MEMORY_P
881 if (PROFILE_FLAGS (data
) [PROFILE_MEMORY_IDX
])
882 profile_print_memory (cpu
, verbose
);
885 #if WITH_PROFILE_CORE_P
886 if (PROFILE_FLAGS (data
) [PROFILE_CORE_IDX
])
887 profile_print_core (cpu
, verbose
);
890 #if WITH_PROFILE_MODEL_P
891 if (PROFILE_FLAGS (data
) [PROFILE_MODEL_IDX
])
892 profile_print_model (cpu
, verbose
);
895 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
896 if (PROFILE_FLAGS (data
) [PROFILE_SCACHE_IDX
])
897 scache_print_profile (cpu
, verbose
);
900 #if WITH_PROFILE_PC_P
901 if (PROFILE_FLAGS (data
) [PROFILE_PC_IDX
])
902 profile_print_pc (cpu
, verbose
);
905 /* Print cpu-specific data before the execution speed. */
906 if (misc_cpu
!= NULL
)
907 (*misc_cpu
) (cpu
, verbose
);
909 /* Always try to print execution time and speed. */
911 || PROFILE_FLAGS (data
) [PROFILE_INSN_IDX
])
912 profile_print_speed (cpu
);
915 /* Finally print non-cpu specific miscellaneous data. */
918 (*misc
) (sd
, verbose
);
921 /* Install profiling support in the simulator. */
924 profile_install (SIM_DESC sd
)
928 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
929 sim_add_option_table (sd
, profile_options
);
930 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
931 memset (CPU_PROFILE_DATA (STATE_CPU (sd
, i
)), 0,
932 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd
, i
))));
933 #if WITH_PROFILE_PC_P
934 sim_module_add_uninstall_fn (sd
, profile_pc_uninstall
);
935 sim_module_add_init_fn (sd
, profile_pc_init
);
937 sim_module_add_uninstall_fn (sd
, profile_uninstall
);
942 profile_uninstall (SIM_DESC sd
)
946 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
948 PROFILE_DATA
*data
= CPU_PROFILE_DATA (STATE_CPU (sd
, i
));
949 if (PROFILE_FILE (data
) != NULL
)
951 /* If output from different cpus is going to the same file,
952 avoid closing the file twice. */
953 for (j
= 0; j
< i
; ++j
)
954 if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd
, j
)))
955 == PROFILE_FILE (data
))
958 fclose (PROFILE_FILE (data
));