]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/common/sim-profile.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / sim / common / sim-profile.c
CommitLineData
c906108c 1/* Default profiling support.
1d506c26 2 Copyright (C) 1996-2024 Free Software Foundation, Inc.
c906108c
SS
3 Contributed by Cygnus Support.
4
5This file is part of GDB, the GNU debugger.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4744ac1b
JB
9the Free Software Foundation; either version 3 of the License, or
10(at your option) any later version.
c906108c
SS
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
4744ac1b
JB
17You should have received a copy of the GNU General Public License
18along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 19
6df01ab8
MF
20/* This must come before any other includes. */
21#include "defs.h"
22
20a8e078 23#include <ctype.h>
2c29882f 24#include <stdarg.h>
20a8e078
MF
25#include <stdlib.h>
26#include <string.h>
27
843bf754
MF
28#include "ansidecl.h"
29
c906108c
SS
30#include "sim-main.h"
31#include "sim-io.h"
32#include "sim-options.h"
33#include "sim-assert.h"
34
5be229c0
MF
35#if !WITH_PROFILE_PC_P
36static 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
44
c906108c
SS
45#define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n))
46
47static MODULE_INIT_FN profile_init;
48static MODULE_UNINSTALL_FN profile_uninstall;
49
50static DECLARE_OPTION_HANDLER (profile_option_handler);
51
52enum {
53 OPTION_PROFILE_INSN = OPTION_START,
54 OPTION_PROFILE_MEMORY,
55 OPTION_PROFILE_MODEL,
56 OPTION_PROFILE_FILE,
57 OPTION_PROFILE_CORE,
09032128 58 OPTION_PROFILE_CPU_FREQUENCY,
c906108c
SS
59 OPTION_PROFILE_PC,
60 OPTION_PROFILE_PC_RANGE,
61 OPTION_PROFILE_PC_GRANULARITY,
62 OPTION_PROFILE_RANGE,
63 OPTION_PROFILE_FUNCTION
64};
65
66static const OPTION profile_options[] = {
67 { {"profile", optional_argument, NULL, 'p'},
68 'p', "on|off", "Perform profiling",
21cf617c 69 profile_option_handler, NULL },
c906108c
SS
70 { {"profile-insn", optional_argument, NULL, OPTION_PROFILE_INSN},
71 '\0', "on|off", "Perform instruction profiling",
21cf617c 72 profile_option_handler, NULL },
c906108c
SS
73 { {"profile-memory", optional_argument, NULL, OPTION_PROFILE_MEMORY},
74 '\0', "on|off", "Perform memory profiling",
21cf617c 75 profile_option_handler, NULL },
c906108c
SS
76 { {"profile-core", optional_argument, NULL, OPTION_PROFILE_CORE},
77 '\0', "on|off", "Perform CORE profiling",
21cf617c 78 profile_option_handler, NULL },
c906108c
SS
79 { {"profile-model", optional_argument, NULL, OPTION_PROFILE_MODEL},
80 '\0', "on|off", "Perform model profiling",
21cf617c 81 profile_option_handler, NULL },
09032128
DB
82 { {"profile-cpu-frequency", required_argument, NULL,
83 OPTION_PROFILE_CPU_FREQUENCY},
84 '\0', "CPU FREQUENCY", "Specify the speed of the simulated cpu clock",
21cf617c 85 profile_option_handler, NULL },
c906108c
SS
86
87 { {"profile-file", required_argument, NULL, OPTION_PROFILE_FILE},
88 '\0', "FILE NAME", "Specify profile output file",
21cf617c 89 profile_option_handler, NULL },
c906108c
SS
90
91 { {"profile-pc", optional_argument, NULL, OPTION_PROFILE_PC},
92 '\0', "on|off", "Perform PC profiling",
21cf617c 93 profile_option_handler, NULL },
c906108c
SS
94 { {"profile-pc-frequency", required_argument, NULL, 'F'},
95 'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency",
21cf617c 96 profile_option_handler, NULL },
c906108c
SS
97 { {"profile-pc-size", required_argument, NULL, 'S'},
98 'S', "PC PROFILE SIZE", "Specify PC profiling size",
21cf617c 99 profile_option_handler, NULL },
c906108c
SS
100 { {"profile-pc-granularity", required_argument, NULL, OPTION_PROFILE_PC_GRANULARITY},
101 '\0', "PC PROFILE GRANULARITY", "Specify PC profiling sample coverage",
21cf617c 102 profile_option_handler, NULL },
c906108c
SS
103 { {"profile-pc-range", required_argument, NULL, OPTION_PROFILE_PC_RANGE},
104 '\0', "BASE,BOUND", "Specify PC profiling address range",
21cf617c 105 profile_option_handler, NULL },
c906108c
SS
106
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",
21cf617c 110 profile_option_handler, NULL },
c906108c
SS
111#if 0 /*wip*/
112 { {"profile-function", required_argument, NULL, OPTION_PROFILE_FUNCTION},
113 '\0', "FUNCTION", "Specify function to profile",
21cf617c 114 profile_option_handler, NULL },
c906108c
SS
115#endif
116#endif
117
21cf617c 118 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
c906108c
SS
119};
120
121/* Set/reset the profile options indicated in MASK. */
122
ed9a39eb 123SIM_RC
c906108c
SS
124set_profile_option_mask (SIM_DESC sd, const char *name, int mask, const char *arg)
125{
126 int profile_nr;
127 int cpu_nr;
128 int profile_val = 1;
129
130 if (arg != NULL)
131 {
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
141 {
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;
144 }
145 }
146
147 /* update applicable profile bits */
148 for (profile_nr = 0; profile_nr < MAX_PROFILE_VALUES; ++profile_nr)
149 {
150 if ((mask & (1 << profile_nr)) == 0)
151 continue;
152
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)
156 {
157 case ??? :
158 break;
159 }
160#endif
161
162 /* Set cpu values. */
163 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
164 {
165 CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[profile_nr] = profile_val;
166 }
167 }
168
169 /* Re-compute the cpu profile summary. */
170 if (profile_val)
171 {
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;
174 }
175 else
176 {
177 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
178 {
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)
181 {
182 if (CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[profile_nr])
183 {
184 CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))->profile_any_p = 1;
185 break;
186 }
187 }
188 }
028f6515 189 }
c906108c
SS
190
191 return SIM_RC_OK;
192}
193
194/* Set one profile option based on its IDX value.
195 Not static as cgen-scache.c uses it. */
196
197SIM_RC
198sim_profile_set_option (SIM_DESC sd, const char *name, int idx, const char *arg)
199{
200 return set_profile_option_mask (sd, name, 1 << idx, arg);
201}
202
09032128
DB
203static SIM_RC
204parse_frequency (SIM_DESC sd, const char *arg, unsigned long *freq)
205{
206 const char *ch;
207 /* First, parse a decimal number. */
208 *freq = 0;
209 ch = arg;
210 if (isdigit (*arg))
211 {
212 for (/**/; *ch != '\0'; ++ch)
213 {
214 if (! isdigit (*ch))
215 break;
216 *freq = *freq * 10 + (*ch - '0');
217 }
218
219 /* Accept KHz, MHz or Hz as a suffix. */
220 if (tolower (*ch) == 'm')
221 {
222 *freq *= 1000000;
223 ++ch;
224 }
225 else if (tolower (*ch) == 'k')
226 {
227 *freq *= 1000;
228 ++ch;
229 }
230
231 if (tolower (*ch) == 'h')
232 {
233 ++ch;
234 if (tolower (*ch) == 'z')
235 ++ch;
236 }
237 }
238
239 if (*ch != '\0')
240 {
241 sim_io_eprintf (sd, "Invalid argument for --profile-cpu-frequency: %s\n",
242 arg);
243 *freq = 0;
244 return SIM_RC_FAIL;
245 }
246
247 return SIM_RC_OK;
248}
249
c906108c
SS
250static SIM_RC
251profile_option_handler (SIM_DESC sd,
252 sim_cpu *cpu,
253 int opt,
254 char *arg,
255 int is_command)
256{
cdc2a5c3 257 int cpu_nr;
c906108c
SS
258
259 /* FIXME: Need to handle `cpu' arg. */
260
261 switch (opt)
262 {
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;
270
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;
277
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;
284
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;
291
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;
298
09032128
DB
299 case OPTION_PROFILE_CPU_FREQUENCY :
300 {
301 unsigned long val;
302 SIM_RC rc = parse_frequency (sd, arg, &val);
303 if (rc == SIM_RC_OK)
304 {
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;
307 }
308 return rc;
309 }
310
c906108c
SS
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
317 {
318 FILE *f = fopen (arg, "w");
319
320 if (f == NULL)
321 {
322 sim_io_eprintf (sd, "Unable to open profile output file `%s'\n", arg);
323 return SIM_RC_FAIL;
324 }
325 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
326 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = f;
327 }
328 break;
329
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;
336
337 case 'F' :
338 if (WITH_PROFILE_PC_P)
339 {
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;
346 }
347 else
348 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
349 break;
350
351 case 'S' :
352 if (WITH_PROFILE_PC_P)
353 {
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;
360 }
361 else
362 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
363 break;
364
365 case OPTION_PROFILE_PC_GRANULARITY:
366 if (WITH_PROFILE_PC_P)
367 {
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))
373 {
374 shift += 1;
375 }
376 if (val != (1 << shift))
377 {
378 sim_io_eprintf (sd, "PC profiling granularity not a power of two\n");
379 return SIM_RC_FAIL;
380 }
381 if (shift == 0)
382 {
383 sim_io_eprintf (sd, "PC profiling granularity too small");
384 return SIM_RC_FAIL;
385 }
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;
390 }
391 else
392 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
393 break;
394
395 case OPTION_PROFILE_PC_RANGE:
396 if (WITH_PROFILE_PC_P)
397 {
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 != ',')
404 {
405 sim_io_eprintf (sd, "--profile-pc-range missing BOUND argument\n");
406 return SIM_RC_FAIL;
407 }
408 bound = strtoul (chp + 1, NULL, 0);
409 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
410 {
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;
028f6515 413 }
c906108c
SS
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;
416 }
417 else
418 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
419 break;
420
421#ifdef SIM_HAVE_ADDR_RANGE
422 case OPTION_PROFILE_RANGE :
423 if (WITH_PROFILE)
424 {
425 char *chp = arg;
426 unsigned long start,end;
427 start = strtoul (chp, &chp, 0);
428 if (*chp != ',')
429 {
430 sim_io_eprintf (sd, "--profile-range missing END argument\n");
431 return SIM_RC_FAIL;
432 }
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);
442 }
443 else
444 sim_io_eprintf (sd, "Profiling not compiled in, `--profile-range' ignored\n");
445 break;
446
447 case OPTION_PROFILE_FUNCTION :
448 if (WITH_PROFILE)
449 {
450 /*wip: need to compute function range given name*/
451 }
452 else
453 sim_io_eprintf (sd, "Profiling not compiled in, `--profile-function' ignored\n");
454 break;
455#endif /* SIM_HAVE_ADDR_RANGE */
456 }
457
458 return SIM_RC_OK;
459}
460\f
0d3d2c71
MF
461/* Profiling output hooks. */
462
f08708cb 463static void ATTRIBUTE_PRINTF (3, 0)
0d3d2c71
MF
464profile_vprintf (SIM_DESC sd, sim_cpu *cpu, const char *fmt, va_list ap)
465{
466 FILE *fp = PROFILE_FILE (CPU_PROFILE_DATA (cpu));
467
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);
473}
474
f0c4dc40 475ATTRIBUTE_PRINTF (3, 4)
0d3d2c71
MF
476static void
477profile_printf (SIM_DESC sd, sim_cpu *cpu, const char *fmt, ...)
478{
479 va_list ap;
480
481 va_start (ap, fmt);
482 profile_vprintf (sd, cpu, fmt, ap);
483 va_end (ap);
484}
485\f
c906108c
SS
486/* PC profiling support */
487
488#if WITH_PROFILE_PC_P
489
490static void
491profile_pc_cleanup (SIM_DESC sd)
492{
493 int n;
494 for (n = 0; n < MAX_NR_PROCESSORS; n++)
495 {
496 sim_cpu *cpu = STATE_CPU (sd, n);
497 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
498 if (PROFILE_PC_COUNT (data) != NULL)
d79fe0d6 499 free (PROFILE_PC_COUNT (data));
c906108c
SS
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;
504 }
505}
506
507
508static void
509profile_pc_uninstall (SIM_DESC sd)
510{
511 profile_pc_cleanup (sd);
512}
513
514static void
515profile_pc_event (SIM_DESC sd,
516 void *data)
517{
518 sim_cpu *cpu = (sim_cpu*) data;
519 PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu);
596f8827 520 address_word pc = sim_pc_get (cpu);
c906108c 521 unsigned i;
c906108c
SS
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;
028f6515 527 PROFILE_PC_EVENT (profile) =
c906108c
SS
528 sim_events_schedule (sd, PROFILE_PC_FREQ (profile), profile_pc_event, cpu);
529}
530
531static SIM_RC
532profile_pc_init (SIM_DESC sd)
533{
534 int n;
535 profile_pc_cleanup (sd);
536 for (n = 0; n < MAX_NR_PROCESSORS; n++)
537 {
538 sim_cpu *cpu = STATE_CPU (sd, n);
539 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
596f8827 540 if (CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_PC_IDX])
c906108c
SS
541 {
542 int bucket_size;
543 /* fill in the frequency if not specified */
544 if (PROFILE_PC_FREQ (data) == 0)
41ec9f20 545 PROFILE_PC_FREQ (data) = 257;
c906108c
SS
546 /* fill in the start/end if not specified */
547 if (PROFILE_PC_END (data) == 0)
548 {
549 PROFILE_PC_START (data) = STATE_TEXT_START (sd);
550 PROFILE_PC_END (data) = STATE_TEXT_END (sd);
551 }
552 /* Compute the number of buckets if not specified. */
553 if (PROFILE_PC_NR_BUCKETS (data) == 0)
554 {
555 if (PROFILE_PC_BUCKET_SIZE (data) == 0)
556 PROFILE_PC_NR_BUCKETS (data) = 16;
557 else
558 {
559 if (PROFILE_PC_END (data) == 0)
560 {
561 /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */
562 PROFILE_PC_NR_BUCKETS (data) =
18d4b488 563 ((1ULL << sizeof (sim_cia) * (8 - 1))
c906108c
SS
564 / (PROFILE_PC_BUCKET_SIZE (data) / 2));
565 }
566 else
567 {
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));
573 }
574 }
575 }
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)
579 {
580 if (PROFILE_PC_END (data) == 0)
581 /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */
18d4b488 582 bucket_size = ((1ULL << ((sizeof (sim_cia) * 8) - 1))
c906108c
SS
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;
c43ad8eb 590 while (bucket_size > PROFILE_PC_BUCKET_SIZE (data))
c906108c
SS
591 {
592 PROFILE_PC_SHIFT (data) += 1;
593 }
594 }
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);
608 }
609 }
610 return SIM_RC_OK;
611}
612
613static void
cc67f780 614profile_print_pc (sim_cpu *cpu, bool verbose)
c906108c
SS
615{
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;
622
623 if (PROFILE_PC_COUNT (profile) == 0)
624 return;
625
0d3d2c71 626 profile_printf (sd, cpu, "Program Counter Statistics:\n\n");
c906108c
SS
627
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)
632 {
633 total += PROFILE_PC_COUNT (profile) [i];
634 if (PROFILE_PC_COUNT (profile) [i] > max_val)
635 max_val = PROFILE_PC_COUNT (profile) [i];
636 }
637
0d3d2c71
MF
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)));
c906108c
SS
646
647 if (PROFILE_PC_END (profile) != 0)
0d3d2c71
MF
648 profile_printf (sd, cpu, " Range: 0x%lx 0x%lx\n",
649 (long) PROFILE_PC_START (profile),
c906108c
SS
650 (long) PROFILE_PC_END (profile));
651
652 if (verbose && max_val != 0)
653 {
654 /* Now we can print the histogram. */
0d3d2c71 655 profile_printf (sd, cpu, "\n");
c906108c
SS
656 for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i)
657 {
658 if (PROFILE_PC_COUNT (profile) [i] != 0)
659 {
0d3d2c71 660 profile_printf (sd, cpu, " ");
c906108c 661 if (i == PROFILE_PC_NR_BUCKETS (profile))
0d3d2c71 662 profile_printf (sd, cpu, "%10s:", "overflow");
c906108c 663 else
0d3d2c71
MF
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,
c906108c
SS
674 PROFILE_PC_COUNT (profile) [i],
675 max_val);
0d3d2c71 676 profile_printf (sd, cpu, "\n");
c906108c
SS
677 }
678 }
679 }
680
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? */
687 {
688 FILE *pf = fopen ("gmon.out", "wb");
028f6515 689
c906108c
SS
690 if (pf == NULL)
691 sim_io_eprintf (sd, "Failed to open \"gmon.out\" profile file\n");
692 else
693 {
694 int ok;
695 /* FIXME: what if the target has a 64 bit PC? */
e4c803f5 696 uint32_t header[3];
c906108c
SS
697 unsigned loop;
698 if (PROFILE_PC_END (profile) != 0)
699 {
700 header[0] = PROFILE_PC_START (profile);
701 header[1] = PROFILE_PC_END (profile);
702 }
703 else
704 {
705 header[0] = 0;
706 header[1] = 0;
707 }
708 /* size of sample buffer (+ header) */
709 header[2] = PROFILE_PC_NR_BUCKETS (profile) * 2 + sizeof (header);
c43ad8eb
BE
710
711 /* Header must be written out in target byte order. */
712 H2T (header[0]);
713 H2T (header[1]);
714 H2T (header[2]);
715
c906108c
SS
716 ok = fwrite (&header, sizeof (header), 1, pf);
717 for (loop = 0;
718 ok && (loop < PROFILE_PC_NR_BUCKETS (profile));
719 loop++)
720 {
e4c803f5 721 int16_t sample;
c906108c
SS
722 if (PROFILE_PC_COUNT (profile) [loop] >= 0xffff)
723 sample = 0xffff;
724 else
725 sample = PROFILE_PC_COUNT (profile) [loop];
41ec9f20 726 H2T (sample);
c906108c
SS
727 ok = fwrite (&sample, sizeof (sample), 1, pf);
728 }
729 if (ok == 0)
730 sim_io_eprintf (sd, "Failed to write to \"gmon.out\" profile file\n");
34b47c38 731 fclose (pf);
c906108c
SS
732 }
733 }
734
0d3d2c71 735 profile_printf (sd, cpu, "\n");
c906108c
SS
736}
737
738#endif
739\f
740/* Summary printing support. */
741
742#if WITH_PROFILE_INSN_P
743
744static SIM_RC
745profile_insn_init (SIM_DESC sd)
746{
747 int c;
748
749 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
750 {
751 sim_cpu *cpu = STATE_CPU (sd, c);
752
753 if (CPU_MAX_INSNS (cpu) > 0)
754 PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu));
755 }
756
757 return SIM_RC_OK;
758}
759
760static void
cc67f780 761profile_print_insn (sim_cpu *cpu, bool verbose)
c906108c
SS
762{
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];
767
768 /* If MAX_INSNS not set, insn profiling isn't supported. */
769 if (CPU_MAX_INSNS (cpu) == 0)
770 return;
771
0d3d2c71 772 profile_printf (sd, cpu, "Instruction Statistics");
c906108c
SS
773#ifdef SIM_HAVE_ADDR_RANGE
774 if (PROFILE_RANGE (data)->ranges)
0d3d2c71 775 profile_printf (sd, cpu, " (for selected address range(s))");
c906108c 776#endif
0d3d2c71 777 profile_printf (sd, cpu, "\n\n");
c906108c
SS
778
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)
784 {
785 const char *name = (*CPU_INSN_NAME (cpu)) (cpu, i);
786
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;
795 }
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;
799
0d3d2c71 800 profile_printf (sd, cpu, " Total: %s insns\n", COMMAS (total));
c906108c
SS
801
802 if (verbose && max_val != 0)
803 {
804 /* Now we can print the histogram. */
0d3d2c71 805 profile_printf (sd, cpu, "\n");
c906108c
SS
806 for (i = 0; i < CPU_MAX_INSNS (cpu); ++i)
807 {
808 const char *name = (*CPU_INSN_NAME (cpu)) (cpu, i);
809
810 if (name == NULL)
811 continue;
812 if (PROFILE_INSN_COUNT (data) [i] != 0)
813 {
0d3d2c71
MF
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,
c906108c
SS
819 PROFILE_INSN_COUNT (data) [i],
820 max_val);
0d3d2c71 821 profile_printf (sd, cpu, "\n");
c906108c
SS
822 }
823 }
824 }
825
0d3d2c71 826 profile_printf (sd, cpu, "\n");
c906108c
SS
827}
828
829#endif
830
831#if WITH_PROFILE_MEMORY_P
832
833static void
cc67f780 834profile_print_memory (sim_cpu *cpu, bool verbose)
c906108c
SS
835{
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];
843
0d3d2c71 844 profile_printf (sd, cpu, "Memory Access Statistics\n\n");
c906108c
SS
845
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)
849 {
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;
859 }
860
861 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
0d3d2c71
MF
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));
c906108c
SS
866
867 if (verbose && max_val != 0)
868 {
869 /* FIXME: Need to separate instruction fetches from data fetches
870 as the former swamps the latter. */
871 /* Now we can print the histogram. */
0d3d2c71 872 profile_printf (sd, cpu, "\n");
c906108c
SS
873 for (i = 0; i < MODE_TARGET_MAX; ++i)
874 {
875 if (PROFILE_READ_COUNT (data) [i] != 0)
876 {
0d3d2c71
MF
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,
c906108c
SS
882 PROFILE_READ_COUNT (data) [i],
883 max_val);
0d3d2c71 884 profile_printf (sd, cpu, "\n");
c906108c
SS
885 }
886 if (PROFILE_WRITE_COUNT (data) [i] != 0)
887 {
0d3d2c71
MF
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,
c906108c
SS
893 PROFILE_WRITE_COUNT (data) [i],
894 max_val);
0d3d2c71 895 profile_printf (sd, cpu, "\n");
c906108c
SS
896 }
897 }
898 }
899
0d3d2c71 900 profile_printf (sd, cpu, "\n");
c906108c
SS
901}
902
903#endif
904
905#if WITH_PROFILE_CORE_P
906
907static void
cc67f780 908profile_print_core (sim_cpu *cpu, bool verbose)
c906108c
SS
909{
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];
916
0d3d2c71 917 profile_printf (sd, cpu, "CORE Statistics\n\n");
c906108c
SS
918
919 /* First pass over data computes various things. */
920 {
921 unsigned map;
922 total = 0;
923 max_val = 0;
924 for (map = 0; map < nr_maps; map++)
925 {
926 total += PROFILE_CORE_COUNT (data) [map];
927 if (PROFILE_CORE_COUNT (data) [map] > max_val)
928 max_val = PROFILE_CORE_COUNT (data) [map];
929 }
930 }
931
932 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
0d3d2c71
MF
933 profile_printf (sd, cpu, " Total: %s accesses\n",
934 COMMAS (total));
c906108c
SS
935
936 if (verbose && max_val != 0)
937 {
938 unsigned map;
939 /* Now we can print the histogram. */
0d3d2c71 940 profile_printf (sd, cpu, "\n");
c906108c
SS
941 for (map = 0; map < nr_maps; map++)
942 {
943 if (PROFILE_CORE_COUNT (data) [map] != 0)
944 {
0d3d2c71
MF
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,
c906108c
SS
950 PROFILE_CORE_COUNT (data) [map],
951 max_val);
0d3d2c71 952 profile_printf (sd, cpu, "\n");
c906108c
SS
953 }
954 }
955 }
956
0d3d2c71 957 profile_printf (sd, cpu, "\n");
c906108c
SS
958}
959
960#endif
961
962#if WITH_PROFILE_MODEL_P
963
964static void
cc67f780 965profile_print_model (sim_cpu *cpu, bool verbose)
c906108c
SS
966{
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];
973
0d3d2c71
MF
974 profile_printf (sd, cpu, "Model %s Timing Information",
975 MODEL_NAME (CPU_MODEL (cpu)));
c906108c
SS
976#ifdef SIM_HAVE_ADDR_RANGE
977 if (PROFILE_RANGE (data)->ranges)
0d3d2c71 978 profile_printf (sd, cpu, " (for selected address range(s))");
c906108c 979#endif
0d3d2c71
MF
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");
c906108c
SS
997}
998
999#endif
1000
1001void
0d3d2c71 1002sim_profile_print_bar (SIM_DESC sd, sim_cpu *cpu, unsigned int width,
c906108c
SS
1003 unsigned int val, unsigned int max_val)
1004{
1005 unsigned int i, count;
1006
1007 count = ((double) val / (double) max_val) * (double) width;
1008
1009 for (i = 0; i < count; ++i)
0d3d2c71 1010 profile_printf (sd, cpu, "*");
c906108c
SS
1011}
1012
1013/* Print the simulator's execution speed for CPU. */
1014
1015static void
1016profile_print_speed (sim_cpu *cpu)
1017{
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);
09032128
DB
1022 double clock;
1023 double secs;
c906108c
SS
1024 char comma_buf[20];
1025
0d3d2c71 1026 profile_printf (sd, cpu, "Simulator Execution Speed\n\n");
c906108c
SS
1027
1028 if (total != 0)
0d3d2c71 1029 profile_printf (sd, cpu, " Total instructions: %s\n", COMMAS (total));
c906108c
SS
1030
1031 if (milliseconds < 1000)
0d3d2c71 1032 profile_printf (sd, cpu, " Total execution time: < 1 second\n\n");
c906108c
SS
1033 else
1034 {
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. */
09032128 1039 secs = (double) milliseconds / 1000;
c906108c 1040 secs = ((double) (unsigned long) (secs * 100 + .5)) / 100;
0d3d2c71 1041 profile_printf (sd, cpu, " Total execution time : %.2f seconds\n", secs);
c906108c
SS
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)
0d3d2c71
MF
1046 profile_printf (sd, cpu, " Simulator speed: %s insns/second\n",
1047 COMMAS ((unsigned long) ((double) total / secs)));
c906108c 1048 }
09032128 1049
09032128
DB
1050 /* Print simulated execution time if the cpu frequency has been specified. */
1051 clock = PROFILE_CPU_FREQ (data);
1052 if (clock != 0)
1053 {
1054 if (clock >= 1000000)
0d3d2c71
MF
1055 profile_printf (sd, cpu, " Simulated cpu frequency: %.2f MHz\n",
1056 clock / 1000000);
09032128 1057 else
0d3d2c71 1058 profile_printf (sd, cpu, " Simulated cpu frequency: %.2f Hz\n", clock);
09032128 1059
80dbae7a 1060#if WITH_PROFILE_MODEL_P
09032128
DB
1061 if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX])
1062 {
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;
0d3d2c71
MF
1070 profile_printf (sd, cpu, " Simulated execution time: %.2f seconds\n",
1071 secs);
09032128 1072 }
09032128 1073#endif /* WITH_PROFILE_MODEL_P */
80dbae7a 1074 }
c906108c
SS
1075}
1076
739dfd28 1077#ifdef SIM_HAVE_ADDR_RANGE
c906108c
SS
1078/* Print selected address ranges. */
1079
1080static void
1081profile_print_addr_ranges (sim_cpu *cpu)
1082{
1083 ADDR_SUBRANGE *asr = PROFILE_RANGE (CPU_PROFILE_DATA (cpu))->ranges;
1084 SIM_DESC sd = CPU_STATE (cpu);
1085
1086 if (asr)
1087 {
0d3d2c71 1088 profile_printf (sd, cpu, "Selected address ranges\n\n");
c906108c
SS
1089 while (asr != NULL)
1090 {
0d3d2c71
MF
1091 profile_printf (sd, cpu, " 0x%lx - 0x%lx\n",
1092 (long) asr->start, (long) asr->end);
c906108c
SS
1093 asr = asr->next;
1094 }
0d3d2c71 1095 profile_printf (sd, cpu, "\n");
c906108c
SS
1096 }
1097}
739dfd28 1098#endif
c906108c
SS
1099
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.
1104
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.
1108
1109 Note that results are indented two spaces to distinguish them from
1110 section titles. */
1111
1112static void
cc67f780 1113profile_info (SIM_DESC sd, bool verbose)
c906108c
SS
1114{
1115 int i,c;
1116 int print_title_p = 0;
1117
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'. */
1122
f1a81b37 1123 for (c = 0; c < MAX_NR_PROCESSORS && !print_title_p; ++c)
c906108c
SS
1124 {
1125 sim_cpu *cpu = STATE_CPU (sd, c);
1126 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
1127
1128 for (i = 0; i < MAX_PROFILE_VALUES; ++i)
1129 if (PROFILE_FLAGS (data) [i])
0d3d2c71
MF
1130 {
1131 profile_printf (sd, cpu, "Summary profiling results:\n\n");
1132 print_title_p = 1;
f1a81b37 1133 break;
0d3d2c71 1134 }
c906108c 1135 }
c906108c
SS
1136
1137 /* Loop, cpu by cpu, printing results. */
1138
1139 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
1140 {
1141 sim_cpu *cpu = STATE_CPU (sd, c);
1142 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
1143
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
b79efe26 1158#if WITH_PROFILE_SCACHE_P && WITH_SCACHE && defined(CGEN_ARCH)
c906108c
SS
1159 || PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX]
1160#endif
1161#if WITH_PROFILE_PC_P
1162 || PROFILE_FLAGS (data) [PROFILE_PC_IDX]
1163#endif
1164 ))
1165 {
0d3d2c71 1166 profile_printf (sd, cpu, "CPU %d\n\n", c);
c906108c
SS
1167 }
1168
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
1175
1176#if WITH_PROFILE_INSN_P
1177 if (PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
1178 profile_print_insn (cpu, verbose);
1179#endif
1180
1181#if WITH_PROFILE_MEMORY_P
1182 if (PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX])
1183 profile_print_memory (cpu, verbose);
1184#endif
1185
1186#if WITH_PROFILE_CORE_P
1187 if (PROFILE_FLAGS (data) [PROFILE_CORE_IDX])
1188 profile_print_core (cpu, verbose);
1189#endif
1190
1191#if WITH_PROFILE_MODEL_P
1192 if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX])
1193 profile_print_model (cpu, verbose);
1194#endif
1195
b79efe26 1196#if WITH_PROFILE_SCACHE_P && WITH_SCACHE && defined(CGEN_ARCH)
c906108c
SS
1197 if (PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX])
1198 scache_print_profile (cpu, verbose);
1199#endif
1200
1201#if WITH_PROFILE_PC_P
1202 if (PROFILE_FLAGS (data) [PROFILE_PC_IDX])
1203 profile_print_pc (cpu, verbose);
1204#endif
1205
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);
1209
1210 /* Always try to print execution time and speed. */
1211 if (verbose
1212 || PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
1213 profile_print_speed (cpu);
1214 }
1215
1216 /* Finally print non-cpu specific miscellaneous data. */
1217 if (STATE_PROFILE_INFO_CALLBACK (sd))
1218 STATE_PROFILE_INFO_CALLBACK (sd) (sd, verbose);
1219
1220}
1221\f
953fac64
MF
1222/* Provide a prototype to silence -Wmissing-prototypes. */
1223SIM_RC sim_install_profile (SIM_DESC sd);
c906108c 1224
953fac64 1225/* Install profiling support in the simulator. */
c906108c 1226SIM_RC
953fac64 1227sim_install_profile (SIM_DESC sd)
c906108c
SS
1228{
1229 int i;
1230
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;
1247}
1248
1249static SIM_RC
1250profile_init (SIM_DESC sd)
1251{
1252#ifdef SIM_HAVE_ADDR_RANGE
1253 /* Check if a range has been specified without specifying what to
1254 collect. */
1255 {
1256 int i;
1257
1258 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1259 {
1260 sim_cpu *cpu = STATE_CPU (sd, i);
1261
1262 if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)))
1263 && ! (PROFILE_INSN_P (cpu)
1264 || PROFILE_MODEL_P (cpu)))
1265 {
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);
1270 }
1271 }
1272 }
1273#endif
1274
1275 return SIM_RC_OK;
1276}
1277
1278static void
1279profile_uninstall (SIM_DESC sd)
1280{
1281 int i,j;
1282
1283 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1284 {
1285 sim_cpu *cpu = STATE_CPU (sd, i);
1286 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
1287
1288 if (PROFILE_FILE (data) != NULL)
1289 {
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));
1298 }
1299
1300 if (PROFILE_INSN_COUNT (data) != NULL)
d79fe0d6 1301 free (PROFILE_INSN_COUNT (data));
c906108c
SS
1302 }
1303}