Add translations for various sub-directories
[binutils-gdb.git] / sim / common / sim-profile.c
blobe1d44e9d25f21cf60dd6c139f4c1042b2f8ade5d
1 /* Default profiling support.
2 Copyright (C) 1996-2024 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/>. */
20 /* This must come before any other includes. */
21 #include "defs.h"
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "ansidecl.h"
30 #include "sim-main.h"
31 #include "sim-io.h"
32 #include "sim-options.h"
33 #include "sim-assert.h"
35 #if !WITH_PROFILE_PC_P
36 static unsigned int _profile_stub;
37 # define PROFILE_PC_FREQ(p) _profile_stub
38 # define PROFILE_PC_NR_BUCKETS(p) _profile_stub
39 # define PROFILE_PC_SHIFT(p) _profile_stub
40 # define PROFILE_PC_START(p) _profile_stub
41 # define PROFILE_PC_END(p) _profile_stub
42 # define PROFILE_INSN_COUNT(p) &_profile_stub
43 #endif
45 #define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n))
47 static MODULE_INIT_FN profile_init;
48 static MODULE_UNINSTALL_FN profile_uninstall;
50 static DECLARE_OPTION_HANDLER (profile_option_handler);
52 enum {
53 OPTION_PROFILE_INSN = OPTION_START,
54 OPTION_PROFILE_MEMORY,
55 OPTION_PROFILE_MODEL,
56 OPTION_PROFILE_FILE,
57 OPTION_PROFILE_CORE,
58 OPTION_PROFILE_CPU_FREQUENCY,
59 OPTION_PROFILE_PC,
60 OPTION_PROFILE_PC_RANGE,
61 OPTION_PROFILE_PC_GRANULARITY,
62 OPTION_PROFILE_RANGE,
63 OPTION_PROFILE_FUNCTION
66 static const OPTION profile_options[] = {
67 { {"profile", optional_argument, NULL, 'p'},
68 'p', "on|off", "Perform profiling",
69 profile_option_handler, NULL },
70 { {"profile-insn", optional_argument, NULL, OPTION_PROFILE_INSN},
71 '\0', "on|off", "Perform instruction profiling",
72 profile_option_handler, NULL },
73 { {"profile-memory", optional_argument, NULL, OPTION_PROFILE_MEMORY},
74 '\0', "on|off", "Perform memory profiling",
75 profile_option_handler, NULL },
76 { {"profile-core", optional_argument, NULL, OPTION_PROFILE_CORE},
77 '\0', "on|off", "Perform CORE profiling",
78 profile_option_handler, NULL },
79 { {"profile-model", optional_argument, NULL, OPTION_PROFILE_MODEL},
80 '\0', "on|off", "Perform model profiling",
81 profile_option_handler, NULL },
82 { {"profile-cpu-frequency", required_argument, NULL,
83 OPTION_PROFILE_CPU_FREQUENCY},
84 '\0', "CPU FREQUENCY", "Specify the speed of the simulated cpu clock",
85 profile_option_handler, NULL },
87 { {"profile-file", required_argument, NULL, OPTION_PROFILE_FILE},
88 '\0', "FILE NAME", "Specify profile output file",
89 profile_option_handler, NULL },
91 { {"profile-pc", optional_argument, NULL, OPTION_PROFILE_PC},
92 '\0', "on|off", "Perform PC profiling",
93 profile_option_handler, NULL },
94 { {"profile-pc-frequency", required_argument, NULL, 'F'},
95 'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency",
96 profile_option_handler, NULL },
97 { {"profile-pc-size", required_argument, NULL, 'S'},
98 'S', "PC PROFILE SIZE", "Specify PC profiling size",
99 profile_option_handler, NULL },
100 { {"profile-pc-granularity", required_argument, NULL, OPTION_PROFILE_PC_GRANULARITY},
101 '\0', "PC PROFILE GRANULARITY", "Specify PC profiling sample coverage",
102 profile_option_handler, NULL },
103 { {"profile-pc-range", required_argument, NULL, OPTION_PROFILE_PC_RANGE},
104 '\0', "BASE,BOUND", "Specify PC profiling address range",
105 profile_option_handler, NULL },
107 #ifdef SIM_HAVE_ADDR_RANGE
108 { {"profile-range", required_argument, NULL, OPTION_PROFILE_RANGE},
109 '\0', "START,END", "Specify range of addresses for instruction and model profiling",
110 profile_option_handler, NULL },
111 #if 0 /*wip*/
112 { {"profile-function", required_argument, NULL, OPTION_PROFILE_FUNCTION},
113 '\0', "FUNCTION", "Specify function to profile",
114 profile_option_handler, NULL },
115 #endif
116 #endif
118 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
121 /* Set/reset the profile options indicated in MASK. */
123 SIM_RC
124 set_profile_option_mask (SIM_DESC sd, const char *name, int mask, const char *arg)
126 int profile_nr;
127 int cpu_nr;
128 int profile_val = 1;
130 if (arg != NULL)
132 if (strcmp (arg, "yes") == 0
133 || strcmp (arg, "on") == 0
134 || strcmp (arg, "1") == 0)
135 profile_val = 1;
136 else if (strcmp (arg, "no") == 0
137 || strcmp (arg, "off") == 0
138 || strcmp (arg, "0") == 0)
139 profile_val = 0;
140 else
142 sim_io_eprintf (sd, "Argument `%s' for `--profile%s' invalid, one of `on', `off', `yes', `no' expected\n", arg, name);
143 return SIM_RC_FAIL;
147 /* update applicable profile bits */
148 for (profile_nr = 0; profile_nr < MAX_PROFILE_VALUES; ++profile_nr)
150 if ((mask & (1 << profile_nr)) == 0)
151 continue;
153 #if 0 /* see sim-trace.c, set flags in STATE here if/when there are any */
154 /* Set non-cpu specific values. */
155 switch (profile_nr)
157 case ??? :
158 break;
160 #endif
162 /* Set cpu values. */
163 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
165 CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[profile_nr] = profile_val;
169 /* Re-compute the cpu profile summary. */
170 if (profile_val)
172 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
173 CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))->profile_any_p = 1;
175 else
177 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
179 CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))->profile_any_p = 0;
180 for (profile_nr = 0; profile_nr < MAX_PROFILE_VALUES; ++profile_nr)
182 if (CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[profile_nr])
184 CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))->profile_any_p = 1;
185 break;
191 return SIM_RC_OK;
194 /* Set one profile option based on its IDX value.
195 Not static as cgen-scache.c uses it. */
197 SIM_RC
198 sim_profile_set_option (SIM_DESC sd, const char *name, int idx, const char *arg)
200 return set_profile_option_mask (sd, name, 1 << idx, arg);
203 static SIM_RC
204 parse_frequency (SIM_DESC sd, const char *arg, unsigned long *freq)
206 const char *ch;
207 /* First, parse a decimal number. */
208 *freq = 0;
209 ch = arg;
210 if (isdigit (*arg))
212 for (/**/; *ch != '\0'; ++ch)
214 if (! isdigit (*ch))
215 break;
216 *freq = *freq * 10 + (*ch - '0');
219 /* Accept KHz, MHz or Hz as a suffix. */
220 if (tolower (*ch) == 'm')
222 *freq *= 1000000;
223 ++ch;
225 else if (tolower (*ch) == 'k')
227 *freq *= 1000;
228 ++ch;
231 if (tolower (*ch) == 'h')
233 ++ch;
234 if (tolower (*ch) == 'z')
235 ++ch;
239 if (*ch != '\0')
241 sim_io_eprintf (sd, "Invalid argument for --profile-cpu-frequency: %s\n",
242 arg);
243 *freq = 0;
244 return SIM_RC_FAIL;
247 return SIM_RC_OK;
250 static SIM_RC
251 profile_option_handler (SIM_DESC sd,
252 sim_cpu *cpu,
253 int opt,
254 char *arg,
255 int is_command)
257 int cpu_nr;
259 /* FIXME: Need to handle `cpu' arg. */
261 switch (opt)
263 case 'p' :
264 if (! WITH_PROFILE)
265 sim_io_eprintf (sd, "Profiling not compiled in, `-p' ignored\n");
266 else
267 return set_profile_option_mask (sd, "profile", PROFILE_USEFUL_MASK,
268 arg);
269 break;
271 case OPTION_PROFILE_INSN :
272 if (WITH_PROFILE_INSN_P)
273 return sim_profile_set_option (sd, "-insn", PROFILE_INSN_IDX, arg);
274 else
275 sim_io_eprintf (sd, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
276 break;
278 case OPTION_PROFILE_MEMORY :
279 if (WITH_PROFILE_MEMORY_P)
280 return sim_profile_set_option (sd, "-memory", PROFILE_MEMORY_IDX, arg);
281 else
282 sim_io_eprintf (sd, "Memory profiling not compiled in, `--profile-memory' ignored\n");
283 break;
285 case OPTION_PROFILE_CORE :
286 if (WITH_PROFILE_CORE_P)
287 return sim_profile_set_option (sd, "-core", PROFILE_CORE_IDX, arg);
288 else
289 sim_io_eprintf (sd, "CORE profiling not compiled in, `--profile-core' ignored\n");
290 break;
292 case OPTION_PROFILE_MODEL :
293 if (WITH_PROFILE_MODEL_P)
294 return sim_profile_set_option (sd, "-model", PROFILE_MODEL_IDX, arg);
295 else
296 sim_io_eprintf (sd, "Model profiling not compiled in, `--profile-model' ignored\n");
297 break;
299 case OPTION_PROFILE_CPU_FREQUENCY :
301 unsigned long val;
302 SIM_RC rc = parse_frequency (sd, arg, &val);
303 if (rc == SIM_RC_OK)
305 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
306 PROFILE_CPU_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd,cpu_nr))) = val;
308 return rc;
311 case OPTION_PROFILE_FILE :
312 /* FIXME: Might want this to apply to pc profiling only,
313 or have two profile file options. */
314 if (! WITH_PROFILE)
315 sim_io_eprintf (sd, "Profiling not compiled in, `--profile-file' ignored\n");
316 else
318 FILE *f = fopen (arg, "w");
320 if (f == NULL)
322 sim_io_eprintf (sd, "Unable to open profile output file `%s'\n", arg);
323 return SIM_RC_FAIL;
325 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
326 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = f;
328 break;
330 case OPTION_PROFILE_PC:
331 if (WITH_PROFILE_PC_P)
332 return sim_profile_set_option (sd, "-pc", PROFILE_PC_IDX, arg);
333 else
334 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc' ignored\n");
335 break;
337 case 'F' :
338 if (WITH_PROFILE_PC_P)
340 /* FIXME: Validate arg. */
341 int val = atoi (arg);
342 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
343 PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = val;
344 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
345 CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1;
347 else
348 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
349 break;
351 case 'S' :
352 if (WITH_PROFILE_PC_P)
354 /* FIXME: Validate arg. */
355 int val = atoi (arg);
356 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
357 PROFILE_PC_NR_BUCKETS (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = val;
358 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
359 CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1;
361 else
362 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
363 break;
365 case OPTION_PROFILE_PC_GRANULARITY:
366 if (WITH_PROFILE_PC_P)
368 int shift;
369 int val = atoi (arg);
370 /* check that the granularity is a power of two */
371 shift = 0;
372 while (val > (1 << shift))
374 shift += 1;
376 if (val != (1 << shift))
378 sim_io_eprintf (sd, "PC profiling granularity not a power of two\n");
379 return SIM_RC_FAIL;
381 if (shift == 0)
383 sim_io_eprintf (sd, "PC profiling granularity too small");
384 return SIM_RC_FAIL;
386 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
387 PROFILE_PC_SHIFT (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = shift;
388 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
389 CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1;
391 else
392 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
393 break;
395 case OPTION_PROFILE_PC_RANGE:
396 if (WITH_PROFILE_PC_P)
398 /* FIXME: Validate args */
399 char *chp = arg;
400 unsigned long base;
401 unsigned long bound;
402 base = strtoul (chp, &chp, 0);
403 if (*chp != ',')
405 sim_io_eprintf (sd, "--profile-pc-range missing BOUND argument\n");
406 return SIM_RC_FAIL;
408 bound = strtoul (chp + 1, NULL, 0);
409 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
411 PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = base;
412 PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = bound;
414 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
415 CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1;
417 else
418 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
419 break;
421 #ifdef SIM_HAVE_ADDR_RANGE
422 case OPTION_PROFILE_RANGE :
423 if (WITH_PROFILE)
425 char *chp = arg;
426 unsigned long start,end;
427 start = strtoul (chp, &chp, 0);
428 if (*chp != ',')
430 sim_io_eprintf (sd, "--profile-range missing END argument\n");
431 return SIM_RC_FAIL;
433 end = strtoul (chp + 1, NULL, 0);
434 /* FIXME: Argument validation. */
435 if (cpu != NULL)
436 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)),
437 start, end);
438 else
439 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
440 sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))),
441 start, end);
443 else
444 sim_io_eprintf (sd, "Profiling not compiled in, `--profile-range' ignored\n");
445 break;
447 case OPTION_PROFILE_FUNCTION :
448 if (WITH_PROFILE)
450 /*wip: need to compute function range given name*/
452 else
453 sim_io_eprintf (sd, "Profiling not compiled in, `--profile-function' ignored\n");
454 break;
455 #endif /* SIM_HAVE_ADDR_RANGE */
458 return SIM_RC_OK;
461 /* Profiling output hooks. */
463 static void ATTRIBUTE_PRINTF (3, 0)
464 profile_vprintf (SIM_DESC sd, sim_cpu *cpu, const char *fmt, va_list ap)
466 FILE *fp = PROFILE_FILE (CPU_PROFILE_DATA (cpu));
468 /* If an output file was given, redirect output to that. */
469 if (fp != NULL)
470 vfprintf (fp, fmt, ap);
471 else
472 sim_io_evprintf (sd, fmt, ap);
475 ATTRIBUTE_PRINTF (3, 4)
476 static void
477 profile_printf (SIM_DESC sd, sim_cpu *cpu, const char *fmt, ...)
479 va_list ap;
481 va_start (ap, fmt);
482 profile_vprintf (sd, cpu, fmt, ap);
483 va_end (ap);
486 /* PC profiling support */
488 #if WITH_PROFILE_PC_P
490 static void
491 profile_pc_cleanup (SIM_DESC sd)
493 int n;
494 for (n = 0; n < MAX_NR_PROCESSORS; n++)
496 sim_cpu *cpu = STATE_CPU (sd, n);
497 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
498 if (PROFILE_PC_COUNT (data) != NULL)
499 free (PROFILE_PC_COUNT (data));
500 PROFILE_PC_COUNT (data) = NULL;
501 if (PROFILE_PC_EVENT (data) != NULL)
502 sim_events_deschedule (sd, PROFILE_PC_EVENT (data));
503 PROFILE_PC_EVENT (data) = NULL;
508 static void
509 profile_pc_uninstall (SIM_DESC sd)
511 profile_pc_cleanup (sd);
514 static void
515 profile_pc_event (SIM_DESC sd,
516 void *data)
518 sim_cpu *cpu = (sim_cpu*) data;
519 PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu);
520 address_word pc = sim_pc_get (cpu);
521 unsigned i;
522 i = (pc - PROFILE_PC_START (profile)) >> PROFILE_PC_SHIFT (profile);
523 if (i < PROFILE_PC_NR_BUCKETS (profile))
524 PROFILE_PC_COUNT (profile) [i] += 1; /* Overflow? */
525 else
526 PROFILE_PC_COUNT (profile) [PROFILE_PC_NR_BUCKETS (profile)] += 1;
527 PROFILE_PC_EVENT (profile) =
528 sim_events_schedule (sd, PROFILE_PC_FREQ (profile), profile_pc_event, cpu);
531 static SIM_RC
532 profile_pc_init (SIM_DESC sd)
534 int n;
535 profile_pc_cleanup (sd);
536 for (n = 0; n < MAX_NR_PROCESSORS; n++)
538 sim_cpu *cpu = STATE_CPU (sd, n);
539 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
540 if (CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_PC_IDX])
542 int bucket_size;
543 /* fill in the frequency if not specified */
544 if (PROFILE_PC_FREQ (data) == 0)
545 PROFILE_PC_FREQ (data) = 257;
546 /* fill in the start/end if not specified */
547 if (PROFILE_PC_END (data) == 0)
549 PROFILE_PC_START (data) = STATE_TEXT_START (sd);
550 PROFILE_PC_END (data) = STATE_TEXT_END (sd);
552 /* Compute the number of buckets if not specified. */
553 if (PROFILE_PC_NR_BUCKETS (data) == 0)
555 if (PROFILE_PC_BUCKET_SIZE (data) == 0)
556 PROFILE_PC_NR_BUCKETS (data) = 16;
557 else
559 if (PROFILE_PC_END (data) == 0)
561 /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
562 PROFILE_PC_NR_BUCKETS (data) =
563 ((1ULL << sizeof (sim_cia) * (8 - 1))
564 / (PROFILE_PC_BUCKET_SIZE (data) / 2));
566 else
568 PROFILE_PC_NR_BUCKETS (data) =
569 ((PROFILE_PC_END (data)
570 - PROFILE_PC_START (data)
571 + PROFILE_PC_BUCKET_SIZE (data) - 1)
572 / PROFILE_PC_BUCKET_SIZE (data));
576 /* Compute the bucket size if not specified. Ensure that it
577 is rounded up to the next power of two */
578 if (PROFILE_PC_BUCKET_SIZE (data) == 0)
580 if (PROFILE_PC_END (data) == 0)
581 /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
582 bucket_size = ((1ULL << ((sizeof (sim_cia) * 8) - 1))
583 / (PROFILE_PC_NR_BUCKETS (data) / 2));
584 else
585 bucket_size = ((PROFILE_PC_END (data)
586 - PROFILE_PC_START (data)
587 + PROFILE_PC_NR_BUCKETS (data) - 1)
588 / PROFILE_PC_NR_BUCKETS (data));
589 PROFILE_PC_SHIFT (data) = 0;
590 while (bucket_size > PROFILE_PC_BUCKET_SIZE (data))
592 PROFILE_PC_SHIFT (data) += 1;
595 /* Align the end address with bucket size */
596 if (PROFILE_PC_END (data) != 0)
597 PROFILE_PC_END (data) = (PROFILE_PC_START (data)
598 + (PROFILE_PC_BUCKET_SIZE (data)
599 * PROFILE_PC_NR_BUCKETS (data)));
600 /* create the relevant buffers */
601 PROFILE_PC_COUNT (data) =
602 NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data) + 1);
603 PROFILE_PC_EVENT (data) =
604 sim_events_schedule (sd,
605 PROFILE_PC_FREQ (data),
606 profile_pc_event,
607 cpu);
610 return SIM_RC_OK;
613 static void
614 profile_print_pc (sim_cpu *cpu, bool verbose)
616 SIM_DESC sd = CPU_STATE (cpu);
617 PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu);
618 char comma_buf[20];
619 unsigned max_val;
620 unsigned total;
621 unsigned i;
623 if (PROFILE_PC_COUNT (profile) == 0)
624 return;
626 profile_printf (sd, cpu, "Program Counter Statistics:\n\n");
628 /* First pass over data computes various things. */
629 max_val = 0;
630 total = 0;
631 for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i)
633 total += PROFILE_PC_COUNT (profile) [i];
634 if (PROFILE_PC_COUNT (profile) [i] > max_val)
635 max_val = PROFILE_PC_COUNT (profile) [i];
638 profile_printf (sd, cpu, " Total samples: %s\n",
639 COMMAS (total));
640 profile_printf (sd, cpu, " Granularity: %s bytes per bucket\n",
641 COMMAS (PROFILE_PC_BUCKET_SIZE (profile)));
642 profile_printf (sd, cpu, " Size: %s buckets\n",
643 COMMAS (PROFILE_PC_NR_BUCKETS (profile)));
644 profile_printf (sd, cpu, " Frequency: %s cycles per sample\n",
645 COMMAS (PROFILE_PC_FREQ (profile)));
647 if (PROFILE_PC_END (profile) != 0)
648 profile_printf (sd, cpu, " Range: 0x%lx 0x%lx\n",
649 (long) PROFILE_PC_START (profile),
650 (long) PROFILE_PC_END (profile));
652 if (verbose && max_val != 0)
654 /* Now we can print the histogram. */
655 profile_printf (sd, cpu, "\n");
656 for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i)
658 if (PROFILE_PC_COUNT (profile) [i] != 0)
660 profile_printf (sd, cpu, " ");
661 if (i == PROFILE_PC_NR_BUCKETS (profile))
662 profile_printf (sd, cpu, "%10s:", "overflow");
663 else
664 profile_printf (sd, cpu, "0x%08lx:",
665 (long) (PROFILE_PC_START (profile)
666 + (i * PROFILE_PC_BUCKET_SIZE (profile))));
667 profile_printf (sd, cpu, " %*s",
668 max_val < 10000 ? 5 : 10,
669 COMMAS (PROFILE_PC_COUNT (profile) [i]));
670 profile_printf (sd, cpu, " %4.1f",
671 (PROFILE_PC_COUNT (profile) [i] * 100.0) / total);
672 profile_printf (sd, cpu, ": ");
673 sim_profile_print_bar (sd, cpu, PROFILE_HISTOGRAM_WIDTH,
674 PROFILE_PC_COUNT (profile) [i],
675 max_val);
676 profile_printf (sd, cpu, "\n");
681 /* dump the histogram to the file "gmon.out" using BSD's gprof file
682 format */
683 /* Since a profile data file is in the native format of the host on
684 which the profile is being, endian issues are not considered in
685 the code below. */
686 /* FIXME: Is this the best place for this code? */
688 FILE *pf = fopen ("gmon.out", "wb");
690 if (pf == NULL)
691 sim_io_eprintf (sd, "Failed to open \"gmon.out\" profile file\n");
692 else
694 int ok;
695 /* FIXME: what if the target has a 64 bit PC? */
696 uint32_t header[3];
697 unsigned loop;
698 if (PROFILE_PC_END (profile) != 0)
700 header[0] = PROFILE_PC_START (profile);
701 header[1] = PROFILE_PC_END (profile);
703 else
705 header[0] = 0;
706 header[1] = 0;
708 /* size of sample buffer (+ header) */
709 header[2] = PROFILE_PC_NR_BUCKETS (profile) * 2 + sizeof (header);
711 /* Header must be written out in target byte order. */
712 H2T (header[0]);
713 H2T (header[1]);
714 H2T (header[2]);
716 ok = fwrite (&header, sizeof (header), 1, pf);
717 for (loop = 0;
718 ok && (loop < PROFILE_PC_NR_BUCKETS (profile));
719 loop++)
721 int16_t sample;
722 if (PROFILE_PC_COUNT (profile) [loop] >= 0xffff)
723 sample = 0xffff;
724 else
725 sample = PROFILE_PC_COUNT (profile) [loop];
726 H2T (sample);
727 ok = fwrite (&sample, sizeof (sample), 1, pf);
729 if (ok == 0)
730 sim_io_eprintf (sd, "Failed to write to \"gmon.out\" profile file\n");
731 fclose (pf);
735 profile_printf (sd, cpu, "\n");
738 #endif
740 /* Summary printing support. */
742 #if WITH_PROFILE_INSN_P
744 static SIM_RC
745 profile_insn_init (SIM_DESC sd)
747 int c;
749 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
751 sim_cpu *cpu = STATE_CPU (sd, c);
753 if (CPU_MAX_INSNS (cpu) > 0)
754 PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu));
757 return SIM_RC_OK;
760 static void
761 profile_print_insn (sim_cpu *cpu, bool verbose)
763 unsigned int i, n, total, max_val, max_name_len;
764 SIM_DESC sd = CPU_STATE (cpu);
765 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
766 char comma_buf[20];
768 /* If MAX_INSNS not set, insn profiling isn't supported. */
769 if (CPU_MAX_INSNS (cpu) == 0)
770 return;
772 profile_printf (sd, cpu, "Instruction Statistics");
773 #ifdef SIM_HAVE_ADDR_RANGE
774 if (PROFILE_RANGE (data)->ranges)
775 profile_printf (sd, cpu, " (for selected address range(s))");
776 #endif
777 profile_printf (sd, cpu, "\n\n");
779 /* First pass over data computes various things. */
780 max_val = 0;
781 total = 0;
782 max_name_len = 0;
783 for (i = 0; i < CPU_MAX_INSNS (cpu); ++i)
785 const char *name = (*CPU_INSN_NAME (cpu)) (cpu, i);
787 if (name == NULL)
788 continue;
789 total += PROFILE_INSN_COUNT (data) [i];
790 if (PROFILE_INSN_COUNT (data) [i] > max_val)
791 max_val = PROFILE_INSN_COUNT (data) [i];
792 n = strlen (name);
793 if (n > max_name_len)
794 max_name_len = n;
796 /* set the total insn count, in case client is being lazy */
797 if (! PROFILE_TOTAL_INSN_COUNT (data))
798 PROFILE_TOTAL_INSN_COUNT (data) = total;
800 profile_printf (sd, cpu, " Total: %s insns\n", COMMAS (total));
802 if (verbose && max_val != 0)
804 /* Now we can print the histogram. */
805 profile_printf (sd, cpu, "\n");
806 for (i = 0; i < CPU_MAX_INSNS (cpu); ++i)
808 const char *name = (*CPU_INSN_NAME (cpu)) (cpu, i);
810 if (name == NULL)
811 continue;
812 if (PROFILE_INSN_COUNT (data) [i] != 0)
814 profile_printf (sd, cpu, " %*s: %*s: ",
815 max_name_len, name,
816 max_val < 10000 ? 5 : 10,
817 COMMAS (PROFILE_INSN_COUNT (data) [i]));
818 sim_profile_print_bar (sd, cpu, PROFILE_HISTOGRAM_WIDTH,
819 PROFILE_INSN_COUNT (data) [i],
820 max_val);
821 profile_printf (sd, cpu, "\n");
826 profile_printf (sd, cpu, "\n");
829 #endif
831 #if WITH_PROFILE_MEMORY_P
833 static void
834 profile_print_memory (sim_cpu *cpu, bool verbose)
836 unsigned int i, n;
837 unsigned int total_read, total_write;
838 unsigned int max_val, max_name_len;
839 /* FIXME: Need to add smp support. */
840 SIM_DESC sd = CPU_STATE (cpu);
841 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
842 char comma_buf[20];
844 profile_printf (sd, cpu, "Memory Access Statistics\n\n");
846 /* First pass over data computes various things. */
847 max_val = total_read = total_write = max_name_len = 0;
848 for (i = 0; i < MODE_TARGET_MAX; ++i)
850 total_read += PROFILE_READ_COUNT (data) [i];
851 total_write += PROFILE_WRITE_COUNT (data) [i];
852 if (PROFILE_READ_COUNT (data) [i] > max_val)
853 max_val = PROFILE_READ_COUNT (data) [i];
854 if (PROFILE_WRITE_COUNT (data) [i] > max_val)
855 max_val = PROFILE_WRITE_COUNT (data) [i];
856 n = strlen (MODE_NAME (i));
857 if (n > max_name_len)
858 max_name_len = n;
861 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
862 profile_printf (sd, cpu, " Total read: %s accesses\n",
863 COMMAS (total_read));
864 profile_printf (sd, cpu, " Total write: %s accesses\n",
865 COMMAS (total_write));
867 if (verbose && max_val != 0)
869 /* FIXME: Need to separate instruction fetches from data fetches
870 as the former swamps the latter. */
871 /* Now we can print the histogram. */
872 profile_printf (sd, cpu, "\n");
873 for (i = 0; i < MODE_TARGET_MAX; ++i)
875 if (PROFILE_READ_COUNT (data) [i] != 0)
877 profile_printf (sd, cpu, " %*s read: %*s: ",
878 max_name_len, MODE_NAME (i),
879 max_val < 10000 ? 5 : 10,
880 COMMAS (PROFILE_READ_COUNT (data) [i]));
881 sim_profile_print_bar (sd, cpu, PROFILE_HISTOGRAM_WIDTH,
882 PROFILE_READ_COUNT (data) [i],
883 max_val);
884 profile_printf (sd, cpu, "\n");
886 if (PROFILE_WRITE_COUNT (data) [i] != 0)
888 profile_printf (sd, cpu, " %*s write: %*s: ",
889 max_name_len, MODE_NAME (i),
890 max_val < 10000 ? 5 : 10,
891 COMMAS (PROFILE_WRITE_COUNT (data) [i]));
892 sim_profile_print_bar (sd, cpu, PROFILE_HISTOGRAM_WIDTH,
893 PROFILE_WRITE_COUNT (data) [i],
894 max_val);
895 profile_printf (sd, cpu, "\n");
900 profile_printf (sd, cpu, "\n");
903 #endif
905 #if WITH_PROFILE_CORE_P
907 static void
908 profile_print_core (sim_cpu *cpu, bool verbose)
910 unsigned int total;
911 unsigned int max_val;
912 /* FIXME: Need to add smp support. */
913 SIM_DESC sd = CPU_STATE (cpu);
914 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
915 char comma_buf[20];
917 profile_printf (sd, cpu, "CORE Statistics\n\n");
919 /* First pass over data computes various things. */
921 unsigned map;
922 total = 0;
923 max_val = 0;
924 for (map = 0; map < nr_maps; map++)
926 total += PROFILE_CORE_COUNT (data) [map];
927 if (PROFILE_CORE_COUNT (data) [map] > max_val)
928 max_val = PROFILE_CORE_COUNT (data) [map];
932 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
933 profile_printf (sd, cpu, " Total: %s accesses\n",
934 COMMAS (total));
936 if (verbose && max_val != 0)
938 unsigned map;
939 /* Now we can print the histogram. */
940 profile_printf (sd, cpu, "\n");
941 for (map = 0; map < nr_maps; map++)
943 if (PROFILE_CORE_COUNT (data) [map] != 0)
945 profile_printf (sd, cpu, "%10s:", map_to_str (map));
946 profile_printf (sd, cpu, "%*s: ",
947 max_val < 10000 ? 5 : 10,
948 COMMAS (PROFILE_CORE_COUNT (data) [map]));
949 sim_profile_print_bar (sd, cpu, PROFILE_HISTOGRAM_WIDTH,
950 PROFILE_CORE_COUNT (data) [map],
951 max_val);
952 profile_printf (sd, cpu, "\n");
957 profile_printf (sd, cpu, "\n");
960 #endif
962 #if WITH_PROFILE_MODEL_P
964 static void
965 profile_print_model (sim_cpu *cpu, bool verbose)
967 SIM_DESC sd = CPU_STATE (cpu);
968 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
969 unsigned long cti_stall_cycles = PROFILE_MODEL_CTI_STALL_CYCLES (data);
970 unsigned long load_stall_cycles = PROFILE_MODEL_LOAD_STALL_CYCLES (data);
971 unsigned long total_cycles = PROFILE_MODEL_TOTAL_CYCLES (data);
972 char comma_buf[20];
974 profile_printf (sd, cpu, "Model %s Timing Information",
975 MODEL_NAME (CPU_MODEL (cpu)));
976 #ifdef SIM_HAVE_ADDR_RANGE
977 if (PROFILE_RANGE (data)->ranges)
978 profile_printf (sd, cpu, " (for selected address range(s))");
979 #endif
980 profile_printf (sd, cpu, "\n\n");
981 profile_printf (sd, cpu, " %-*s %s\n",
982 PROFILE_LABEL_WIDTH, "Taken branches:",
983 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data)));
984 profile_printf (sd, cpu, " %-*s %s\n",
985 PROFILE_LABEL_WIDTH, "Untaken branches:",
986 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data)));
987 profile_printf (sd, cpu, " %-*s %s\n",
988 PROFILE_LABEL_WIDTH, "Cycles stalled due to branches:",
989 COMMAS (cti_stall_cycles));
990 profile_printf (sd, cpu, " %-*s %s\n",
991 PROFILE_LABEL_WIDTH, "Cycles stalled due to loads:",
992 COMMAS (load_stall_cycles));
993 profile_printf (sd, cpu, " %-*s %s\n",
994 PROFILE_LABEL_WIDTH, "Total cycles (*approximate*):",
995 COMMAS (total_cycles));
996 profile_printf (sd, cpu, "\n");
999 #endif
1001 void
1002 sim_profile_print_bar (SIM_DESC sd, sim_cpu *cpu, unsigned int width,
1003 unsigned int val, unsigned int max_val)
1005 unsigned int i, count;
1007 count = ((double) val / (double) max_val) * (double) width;
1009 for (i = 0; i < count; ++i)
1010 profile_printf (sd, cpu, "*");
1013 /* Print the simulator's execution speed for CPU. */
1015 static void
1016 profile_print_speed (sim_cpu *cpu)
1018 SIM_DESC sd = CPU_STATE (cpu);
1019 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
1020 unsigned long milliseconds = sim_events_elapsed_time (sd);
1021 unsigned long total = PROFILE_TOTAL_INSN_COUNT (data);
1022 double clock;
1023 double secs;
1024 char comma_buf[20];
1026 profile_printf (sd, cpu, "Simulator Execution Speed\n\n");
1028 if (total != 0)
1029 profile_printf (sd, cpu, " Total instructions: %s\n", COMMAS (total));
1031 if (milliseconds < 1000)
1032 profile_printf (sd, cpu, " Total execution time: < 1 second\n\n");
1033 else
1035 /* The printing of the time rounded to 2 decimal places makes the speed
1036 calculation seem incorrect [even though it is correct]. So round
1037 MILLISECONDS first. This can marginally affect the result, but it's
1038 better that the user not perceive there's a math error. */
1039 secs = (double) milliseconds / 1000;
1040 secs = ((double) (unsigned long) (secs * 100 + .5)) / 100;
1041 profile_printf (sd, cpu, " Total execution time : %.2f seconds\n", secs);
1042 /* Don't confuse things with data that isn't useful.
1043 If we ran for less than 2 seconds, only use the data if we
1044 executed more than 100,000 insns. */
1045 if (secs >= 2 || total >= 100000)
1046 profile_printf (sd, cpu, " Simulator speed: %s insns/second\n",
1047 COMMAS ((unsigned long) ((double) total / secs)));
1050 /* Print simulated execution time if the cpu frequency has been specified. */
1051 clock = PROFILE_CPU_FREQ (data);
1052 if (clock != 0)
1054 if (clock >= 1000000)
1055 profile_printf (sd, cpu, " Simulated cpu frequency: %.2f MHz\n",
1056 clock / 1000000);
1057 else
1058 profile_printf (sd, cpu, " Simulated cpu frequency: %.2f Hz\n", clock);
1060 #if WITH_PROFILE_MODEL_P
1061 if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX])
1063 /* The printing of the time rounded to 2 decimal places makes the
1064 speed calculation seem incorrect [even though it is correct].
1065 So round SECS first. This can marginally affect the result,
1066 but it's better that the user not perceive there's a math
1067 error. */
1068 secs = PROFILE_MODEL_TOTAL_CYCLES (data) / clock;
1069 secs = ((double) (unsigned long) (secs * 100 + .5)) / 100;
1070 profile_printf (sd, cpu, " Simulated execution time: %.2f seconds\n",
1071 secs);
1073 #endif /* WITH_PROFILE_MODEL_P */
1077 #ifdef SIM_HAVE_ADDR_RANGE
1078 /* Print selected address ranges. */
1080 static void
1081 profile_print_addr_ranges (sim_cpu *cpu)
1083 ADDR_SUBRANGE *asr = PROFILE_RANGE (CPU_PROFILE_DATA (cpu))->ranges;
1084 SIM_DESC sd = CPU_STATE (cpu);
1086 if (asr)
1088 profile_printf (sd, cpu, "Selected address ranges\n\n");
1089 while (asr != NULL)
1091 profile_printf (sd, cpu, " 0x%lx - 0x%lx\n",
1092 (long) asr->start, (long) asr->end);
1093 asr = asr->next;
1095 profile_printf (sd, cpu, "\n");
1098 #endif
1100 /* Top level function to print all summary profile information.
1101 It is [currently] intended that all such data is printed by this function.
1102 I'd rather keep it all in one place for now. To that end, MISC_CPU and
1103 MISC are callbacks used to print any miscellaneous data.
1105 One might want to add a user option that allows printing by type or by cpu
1106 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
1107 This may be a case of featuritis so it's currently left out.
1109 Note that results are indented two spaces to distinguish them from
1110 section titles. */
1112 static void
1113 profile_info (SIM_DESC sd, bool verbose)
1115 int i,c;
1116 int print_title_p = 0;
1118 /* Only print the title if some data has been collected. */
1119 /* ??? Why don't we just exit if no data collected? */
1120 /* FIXME: If the number of processors can be selected on the command line,
1121 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
1123 for (c = 0; c < MAX_NR_PROCESSORS && !print_title_p; ++c)
1125 sim_cpu *cpu = STATE_CPU (sd, c);
1126 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
1128 for (i = 0; i < MAX_PROFILE_VALUES; ++i)
1129 if (PROFILE_FLAGS (data) [i])
1131 profile_printf (sd, cpu, "Summary profiling results:\n\n");
1132 print_title_p = 1;
1133 break;
1137 /* Loop, cpu by cpu, printing results. */
1139 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
1141 sim_cpu *cpu = STATE_CPU (sd, c);
1142 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
1144 if (MAX_NR_PROCESSORS > 1
1145 && (0
1146 #if WITH_PROFILE_INSN_P
1147 || PROFILE_FLAGS (data) [PROFILE_INSN_IDX]
1148 #endif
1149 #if WITH_PROFILE_MEMORY_P
1150 || PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX]
1151 #endif
1152 #if WITH_PROFILE_CORE_P
1153 || PROFILE_FLAGS (data) [PROFILE_CORE_IDX]
1154 #endif
1155 #if WITH_PROFILE_MODEL_P
1156 || PROFILE_FLAGS (data) [PROFILE_MODEL_IDX]
1157 #endif
1158 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE && defined(CGEN_ARCH)
1159 || PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX]
1160 #endif
1161 #if WITH_PROFILE_PC_P
1162 || PROFILE_FLAGS (data) [PROFILE_PC_IDX]
1163 #endif
1166 profile_printf (sd, cpu, "CPU %d\n\n", c);
1169 #ifdef SIM_HAVE_ADDR_RANGE
1170 if (print_title_p
1171 && (PROFILE_INSN_P (cpu)
1172 || PROFILE_MODEL_P (cpu)))
1173 profile_print_addr_ranges (cpu);
1174 #endif
1176 #if WITH_PROFILE_INSN_P
1177 if (PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
1178 profile_print_insn (cpu, verbose);
1179 #endif
1181 #if WITH_PROFILE_MEMORY_P
1182 if (PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX])
1183 profile_print_memory (cpu, verbose);
1184 #endif
1186 #if WITH_PROFILE_CORE_P
1187 if (PROFILE_FLAGS (data) [PROFILE_CORE_IDX])
1188 profile_print_core (cpu, verbose);
1189 #endif
1191 #if WITH_PROFILE_MODEL_P
1192 if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX])
1193 profile_print_model (cpu, verbose);
1194 #endif
1196 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE && defined(CGEN_ARCH)
1197 if (PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX])
1198 scache_print_profile (cpu, verbose);
1199 #endif
1201 #if WITH_PROFILE_PC_P
1202 if (PROFILE_FLAGS (data) [PROFILE_PC_IDX])
1203 profile_print_pc (cpu, verbose);
1204 #endif
1206 /* Print cpu-specific data before the execution speed. */
1207 if (PROFILE_INFO_CPU_CALLBACK (data) != NULL)
1208 PROFILE_INFO_CPU_CALLBACK (data) (cpu, verbose);
1210 /* Always try to print execution time and speed. */
1211 if (verbose
1212 || PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
1213 profile_print_speed (cpu);
1216 /* Finally print non-cpu specific miscellaneous data. */
1217 if (STATE_PROFILE_INFO_CALLBACK (sd))
1218 STATE_PROFILE_INFO_CALLBACK (sd) (sd, verbose);
1222 /* Provide a prototype to silence -Wmissing-prototypes. */
1223 SIM_RC sim_install_profile (SIM_DESC sd);
1225 /* Install profiling support in the simulator. */
1226 SIM_RC
1227 sim_install_profile (SIM_DESC sd)
1229 int i;
1231 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
1232 sim_add_option_table (sd, NULL, profile_options);
1233 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1234 memset (CPU_PROFILE_DATA (STATE_CPU (sd, i)), 0,
1235 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd, i))));
1236 #if WITH_PROFILE_INSN_P
1237 sim_module_add_init_fn (sd, profile_insn_init);
1238 #endif
1239 #if WITH_PROFILE_PC_P
1240 sim_module_add_uninstall_fn (sd, profile_pc_uninstall);
1241 sim_module_add_init_fn (sd, profile_pc_init);
1242 #endif
1243 sim_module_add_init_fn (sd, profile_init);
1244 sim_module_add_uninstall_fn (sd, profile_uninstall);
1245 sim_module_add_info_fn (sd, profile_info);
1246 return SIM_RC_OK;
1249 static SIM_RC
1250 profile_init (SIM_DESC sd)
1252 #ifdef SIM_HAVE_ADDR_RANGE
1253 /* Check if a range has been specified without specifying what to
1254 collect. */
1256 int i;
1258 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1260 sim_cpu *cpu = STATE_CPU (sd, i);
1262 if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)))
1263 && ! (PROFILE_INSN_P (cpu)
1264 || PROFILE_MODEL_P (cpu)))
1266 sim_io_eprintf_cpu (cpu, "Profiling address range specified without --profile-insn or --profile-model.\n");
1267 sim_io_eprintf_cpu (cpu, "Address range ignored.\n");
1268 sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)),
1269 0, ~ (address_word) 0);
1273 #endif
1275 return SIM_RC_OK;
1278 static void
1279 profile_uninstall (SIM_DESC sd)
1281 int i,j;
1283 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1285 sim_cpu *cpu = STATE_CPU (sd, i);
1286 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
1288 if (PROFILE_FILE (data) != NULL)
1290 /* If output from different cpus is going to the same file,
1291 avoid closing the file twice. */
1292 for (j = 0; j < i; ++j)
1293 if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd, j)))
1294 == PROFILE_FILE (data))
1295 break;
1296 if (i == j)
1297 fclose (PROFILE_FILE (data));
1300 if (PROFILE_INSN_COUNT (data) != NULL)
1301 free (PROFILE_INSN_COUNT (data));