* sim-base.h (sim_state_base): Delete member `model'.
[deliverable/binutils-gdb.git] / sim / common / sim-profile.c
1 /* Default profiling support.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4
5 This file is part of GDB, the GNU debugger.
6
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)
10 any later version.
11
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.
16
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. */
20
21 #include "sim-main.h"
22 #include "sim-io.h"
23 #include "sim-options.h"
24 #include "sim-assert.h"
25
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #else
33 #ifdef HAVE_STRINGS_H
34 #include <strings.h>
35 #endif
36 #endif
37
38 #define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n))
39
40 static MODULE_UNINSTALL_FN profile_uninstall;
41
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);
44 #endif
45
46 static DECLARE_OPTION_HANDLER (profile_option_handler);
47
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)
57
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 },
74
75 { {"profile-file", required_argument, NULL, OPTION_PROFILE_FILE},
76 '\0', "FILE NAME", "Specify profile output file",
77 profile_option_handler },
78
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 },
94
95 #if 0 /*FIXME:wip*/
96 { {"profile-range", required_argument, NULL, OPTION_PROFILE_RANGE},
97 0, NULL, "Specify range of addresses to profile",
98 profile_option_handler },
99 #endif
100
101 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
102 };
103
104 static SIM_RC
105 profile_option_handler (SIM_DESC sd,
106 int opt,
107 char *arg,
108 int is_command)
109 {
110 int i,n;
111
112 switch (opt)
113 {
114 case 'p' :
115 if (! WITH_PROFILE)
116 sim_io_eprintf (sd, "Profiling not compiled in, -p option ignored\n");
117 else
118 {
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;
122 }
123 break;
124
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;
129 #else
130 sim_io_eprintf (sd, "Instruction profiling not compiled in, `--profile-insn' ignored\n");
131 #endif
132 break;
133
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;
138 #else
139 sim_io_eprintf (sd, "Memory profiling not compiled in, `--profile-memory' ignored\n");
140 #endif
141 break;
142
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;
147 #else
148 sim_io_eprintf (sd, "CORE profiling not compiled in, `--profile-core' ignored\n");
149 #endif
150 break;
151
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;
156 #else
157 sim_io_eprintf (sd, "Model profiling not compiled in, `--profile-model' ignored\n");
158 #endif
159 break;
160
161 case OPTION_PROFILE_FILE :
162 /* FIXME: Might want this to apply to pc profiling only,
163 or have two profile file options. */
164 if (! WITH_PROFILE)
165 sim_io_eprintf (sd, "Profiling not compiled in, `--profile-file' ignored\n");
166 else
167 {
168 FILE *f = fopen (arg, "w");
169
170 if (f == NULL)
171 {
172 sim_io_eprintf (sd, "Unable to open profile output file `%s'\n", arg);
173 return SIM_RC_FAIL;
174 }
175 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
176 PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd, n))) = f;
177 }
178 break;
179
180 case OPTION_PROFILE_PC:
181 if (WITH_PROFILE_PC_P)
182 {
183 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
184 CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_PC_IDX] = 1;
185 }
186 else
187 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc' ignored\n");
188 break;
189
190 case 'F' :
191 if (WITH_PROFILE_PC_P)
192 {
193 /* FIXME: Validate arg. */
194 i = atoi (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;
199 }
200 else
201 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n");
202 break;
203
204 case 'S' :
205 if (WITH_PROFILE_PC_P)
206 {
207 /* FIXME: Validate arg. */
208 i = atoi (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;
213 }
214 else
215 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-size' ignored\n");
216 break;
217
218 case OPTION_PROFILE_PC_GRANULARITY:
219 if (WITH_PROFILE_PC_P)
220 {
221 int shift;
222 i = atoi (arg);
223 /* check that the granularity is a power of two */
224 shift = 0;
225 while (i > (1 << shift))
226 {
227 shift += 1;
228 }
229 if (i != (1 << shift))
230 {
231 sim_io_eprintf (sd, "PC profiling granularity not a power of two\n");
232 return SIM_RC_FAIL;
233 }
234 if (shift == 0)
235 {
236 sim_io_eprintf (sd, "PC profiling granularity too small");
237 return SIM_RC_FAIL;
238 }
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;
243 }
244 else
245 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n");
246 break;
247
248 case OPTION_PROFILE_PC_RANGE:
249 if (WITH_PROFILE_PC_P)
250 {
251 /* FIXME: Validate args */
252 char *chp = arg;
253 unsigned long base;
254 unsigned long bound;
255 base = strtoul (chp, &chp, 0);
256 if (*chp != ',')
257 {
258 sim_io_eprintf (sd, "--profile-pc-range missing BOUND argument\n");
259 return SIM_RC_FAIL;
260 }
261 bound = strtoul (chp + 1, NULL, 0);
262 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
263 {
264 PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd, n))) = base;
265 PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd, n))) = bound;
266 }
267 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
268 CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_PC_IDX] = 1;
269 }
270 else
271 sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-range' ignored\n");
272
273
274 #if 0 /* FIXME:wip */
275 case OPTION_PROFILE_RANGE :
276 break;
277 #endif
278 }
279
280 return SIM_RC_OK;
281 }
282 \f
283 /* PC profiling support */
284
285 #if WITH_PROFILE_PC_P
286
287 static void
288 profile_pc_cleanup (SIM_DESC sd)
289 {
290 int n;
291 for (n = 0; n < MAX_NR_PROCESSORS; n++)
292 {
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;
301 }
302 }
303
304
305 static void
306 profile_pc_uninstall (SIM_DESC sd)
307 {
308 profile_pc_cleanup (sd);
309 }
310
311 static void
312 profile_pc_event (SIM_DESC sd,
313 void *data)
314 {
315 sim_cpu *cpu = (sim_cpu*) data;
316 PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu);
317 address_word pc;
318 unsigned i;
319 switch (STATE_WATCHPOINTS (sd)->sizeof_pc)
320 {
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;
324 default: pc = 0;
325 }
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? */
329 else
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);
333 }
334
335 static SIM_RC
336 profile_pc_init (SIM_DESC sd)
337 {
338 int n;
339 profile_pc_cleanup (sd);
340 for (n = 0; n < MAX_NR_PROCESSORS; n++)
341 {
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)
346 {
347 int bucket_size;
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)
353 {
354 PROFILE_PC_START (data) = STATE_TEXT_START (sd);
355 PROFILE_PC_END (data) = STATE_TEXT_END (sd);
356 }
357 /* Compute the number of buckets if not specified. */
358 if (PROFILE_PC_NR_BUCKETS (data) == 0)
359 {
360 if (PROFILE_PC_BUCKET_SIZE (data) == 0)
361 PROFILE_PC_NR_BUCKETS (data) = 16;
362 else
363 {
364 if (PROFILE_PC_END (data) == 0)
365 {
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));
370 }
371 else
372 {
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));
378 }
379 }
380 }
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)
384 {
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));
389 else
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))
396 {
397 PROFILE_PC_SHIFT (data) += 1;
398 }
399 }
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),
411 profile_pc_event,
412 cpu);
413 }
414 }
415 return SIM_RC_OK;
416 }
417
418 static void
419 profile_print_pc (sim_cpu *cpu, int verbose)
420 {
421 SIM_DESC sd = CPU_STATE (cpu);
422 PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu);
423 char comma_buf[20];
424 unsigned max_val;
425 unsigned total;
426 unsigned i;
427
428 sim_io_printf (sd, "Program Counter Statistics:\n\n");
429
430 /* First pass over data computes various things. */
431 max_val = 0;
432 total = 0;
433 for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i)
434 {
435 total += PROFILE_PC_COUNT (profile) [i];
436 if (PROFILE_PC_COUNT (profile) [i] > max_val)
437 max_val = PROFILE_PC_COUNT (profile) [i];
438 }
439
440 sim_io_printf (sd, " Total samples: %s\n",
441 COMMAS (total));
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)));
448
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));
453
454 if (verbose && max_val != 0)
455 {
456 /* Now we can print the histogram. */
457 sim_io_printf (sd, "\n");
458 for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i)
459 {
460 if (PROFILE_PC_COUNT (profile) [i] != 0)
461 {
462 sim_io_printf (sd, " ");
463 if (i == PROFILE_PC_NR_BUCKETS (profile))
464 sim_io_printf (sd, "%10s:", "overflow");
465 else
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],
477 max_val);
478 sim_io_printf (sd, "\n");
479 }
480 }
481 }
482
483 /* dump the histogram to the file "gmon.out" using BSD's gprof file
484 format */
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
487 the code below. */
488 /* FIXME: Is this the best place for this code? */
489 {
490 FILE *pf = fopen ("gmon.out", "wb");
491
492 if (pf == NULL)
493 sim_io_eprintf (sd, "Failed to open \"gmon.out\" profile file\n");
494 else
495 {
496 int ok;
497 /* FIXME: what if the target has a 64 bit PC? */
498 unsigned32 header[3];
499 unsigned loop;
500 if (PROFILE_PC_END (profile) != 0)
501 {
502 header[0] = PROFILE_PC_START (profile);
503 header[1] = PROFILE_PC_END (profile);
504 }
505 else
506 {
507 header[0] = 0;
508 header[1] = 0;
509 }
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);
513 for (loop = 0;
514 ok && (loop < PROFILE_PC_NR_BUCKETS (profile));
515 loop++)
516 {
517 signed16 sample;
518 if (PROFILE_PC_COUNT (profile) [loop] >= 0xffff)
519 sample = 0xffff;
520 else
521 sample = PROFILE_PC_COUNT (profile) [loop];
522 ok = fwrite (&sample, sizeof (sample), 1, pf);
523 }
524 if (ok == 0)
525 sim_io_eprintf (sd, "Failed to write to \"gmon.out\" profile file\n");
526 fclose(pf);
527 }
528 }
529
530 sim_io_printf (sd, "\n");
531 }
532
533 #endif
534 \f
535 /* Summary printing support. */
536
537 #if WITH_PROFILE_INSN_P
538
539 static void
540 profile_print_insn (sim_cpu *cpu, int verbose)
541 {
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);
545 char comma_buf[20];
546
547 sim_io_printf (sd, "Instruction Statistics:\n\n");
548
549 /* First pass over data computes various things. */
550 max_val = 0;
551 total = 0;
552 max_name_len = 0;
553 for (i = 0; i < MAX_INSNS; ++i)
554 {
555 if (INSN_NAME (i) == NULL)
556 continue;
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)
562 max_name_len = n;
563 }
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;
567
568 sim_io_printf (sd, " Total: %s insns\n", COMMAS (total));
569
570 if (verbose && max_val != 0)
571 {
572 /* Now we can print the histogram. */
573 sim_io_printf (sd, "\n");
574 for (i = 0; i < MAX_INSNS; ++i)
575 {
576 if (INSN_NAME (i) == NULL)
577 continue;
578 if (PROFILE_INSN_COUNT (data) [i] != 0)
579 {
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],
586 max_val);
587 sim_io_printf (sd, "\n");
588 }
589 }
590 }
591
592 sim_io_printf (sd, "\n");
593 }
594
595 #endif
596
597 #if WITH_PROFILE_MEMORY_P
598
599 static void
600 profile_print_memory (sim_cpu *cpu, int verbose)
601 {
602 unsigned int i, n;
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);
608 char comma_buf[20];
609
610 sim_io_printf (sd, "Memory Access Statistics:\n\n");
611
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)
615 {
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)
624 max_name_len = n;
625 }
626
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));
632
633 if (verbose && max_val != 0)
634 {
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)
640 {
641 if (PROFILE_READ_COUNT (data) [i] != 0)
642 {
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],
649 max_val);
650 sim_io_printf (sd, "\n");
651 }
652 if (PROFILE_WRITE_COUNT (data) [i] != 0)
653 {
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],
660 max_val);
661 sim_io_printf (sd, "\n");
662 }
663 }
664 }
665
666 sim_io_printf (sd, "\n");
667 }
668
669 #endif
670
671 #if WITH_PROFILE_CORE_P
672
673 static void
674 profile_print_core (sim_cpu *cpu, int verbose)
675 {
676 unsigned int total;
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);
681 char comma_buf[20];
682
683 sim_io_printf (sd, "CORE Statistics:\n\n");
684
685 /* First pass over data computes various things. */
686 {
687 sim_core_maps map;
688 total = 0;
689 max_val = 0;
690 for (map = 0; map < nr_sim_core_maps; map++)
691 {
692 total += PROFILE_CORE_COUNT (data) [map];
693 if (PROFILE_CORE_COUNT (data) [map] > max_val)
694 max_val = PROFILE_CORE_COUNT (data) [map];
695 }
696 }
697
698 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
699 sim_io_printf (sd, " Total: %s accesses\n",
700 COMMAS (total));
701
702 if (verbose && max_val != 0)
703 {
704 sim_core_maps map;
705 /* Now we can print the histogram. */
706 sim_io_printf (sd, "\n");
707 for (map = 0; map < nr_sim_core_maps; map++)
708 {
709 if (PROFILE_CORE_COUNT (data) [map] != 0)
710 {
711 switch (map)
712 {
713 case sim_core_read_map:
714 sim_io_printf (sd, " read:");
715 break;
716 case sim_core_write_map:
717 sim_io_printf (sd, " write:");
718 break;
719 case sim_core_execute_map:
720 sim_io_printf (sd, " exec:");
721 break;
722 case nr_sim_core_maps:
723 ; /* ignore */
724 }
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],
730 max_val);
731 sim_io_printf (sd, "\n");
732 }
733 }
734 }
735
736 sim_io_printf (sd, "\n");
737 }
738
739 #endif
740
741 #if WITH_PROFILE_MODEL_P
742
743 static void
744 profile_print_model (sim_cpu *cpu, int verbose)
745 {
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;
752 char comma_buf[20];
753
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*):",
770 COMMAS (total));
771 sim_io_printf (sd, "\n");
772 }
773
774 #endif
775
776
777 #if WITH_PROFILE_INSN_P || WITH_PROFILE_MEMORY_P || WITH_PROFILE_CORE_P || WITH_PROFILE_PC_P
778
779 static void
780 print_bar (SIM_DESC sd, unsigned int width,
781 unsigned int val, unsigned int max_val)
782 {
783 unsigned int i, count;
784
785 count = ((double) val / (double) max_val) * (double) width;
786
787 for (i = 0; i < count; ++i)
788 sim_io_printf (sd, "*");
789 }
790
791 #endif
792
793 /* Print the simulator's execution speed for CPU. */
794
795 static void
796 profile_print_speed (sim_cpu *cpu)
797 {
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);
802 char comma_buf[20];
803
804 sim_io_printf (sd, "Simulator Execution Speed\n\n");
805
806 if (total != 0)
807 sim_io_printf (sd, " Total instructions: %s\n", COMMAS (total));
808
809 if (milliseconds < 1000)
810 sim_io_printf (sd, " Total Execution Time: < 1 second\n\n");
811 else
812 {
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)));
826 }
827 }
828
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.
833
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.
837
838 Note that results are indented two spaces to distinguish them from
839 section titles. */
840
841 void
842 profile_print (SIM_DESC sd, int verbose,
843 PROFILE_CALLBACK *misc, PROFILE_CPU_CALLBACK *misc_cpu)
844 {
845 int i,c;
846 int print_title_p = 0;
847
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'. */
851
852 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
853 {
854 sim_cpu *cpu = STATE_CPU (sd, c);
855 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
856
857 for (i = 0; i < MAX_PROFILE_VALUES; ++i)
858 if (PROFILE_FLAGS (data) [i])
859 print_title_p = 1;
860 /* One could break out early if print_title_p is set. */
861 }
862 if (print_title_p)
863 sim_io_printf (sd, "Summary profiling results:\n\n");
864
865 /* Loop, cpu by cpu, printing results. */
866
867 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
868 {
869 sim_cpu *cpu = STATE_CPU (sd, c);
870 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
871
872 if (MAX_NR_PROCESSORS > 1)
873 sim_io_printf (sd, "CPU %d\n\n", c);
874
875 #if WITH_PROFILE_INSN_P
876 if (PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
877 profile_print_insn (cpu, verbose);
878 #endif
879
880 #if WITH_PROFILE_MEMORY_P
881 if (PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX])
882 profile_print_memory (cpu, verbose);
883 #endif
884
885 #if WITH_PROFILE_CORE_P
886 if (PROFILE_FLAGS (data) [PROFILE_CORE_IDX])
887 profile_print_core (cpu, verbose);
888 #endif
889
890 #if WITH_PROFILE_MODEL_P
891 if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX])
892 profile_print_model (cpu, verbose);
893 #endif
894
895 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
896 if (PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX])
897 scache_print_profile (cpu, verbose);
898 #endif
899
900 #if WITH_PROFILE_PC_P
901 if (PROFILE_FLAGS (data) [PROFILE_PC_IDX])
902 profile_print_pc (cpu, verbose);
903 #endif
904
905 /* Print cpu-specific data before the execution speed. */
906 if (misc_cpu != NULL)
907 (*misc_cpu) (cpu, verbose);
908
909 /* Always try to print execution time and speed. */
910 if (verbose
911 || PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
912 profile_print_speed (cpu);
913 }
914
915 /* Finally print non-cpu specific miscellaneous data. */
916
917 if (misc != NULL)
918 (*misc) (sd, verbose);
919 }
920 \f
921 /* Install profiling support in the simulator. */
922
923 SIM_RC
924 profile_install (SIM_DESC sd)
925 {
926 int i;
927
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);
936 #endif
937 sim_module_add_uninstall_fn (sd, profile_uninstall);
938 return SIM_RC_OK;
939 }
940
941 static void
942 profile_uninstall (SIM_DESC sd)
943 {
944 int i,j;
945
946 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
947 {
948 PROFILE_DATA *data = CPU_PROFILE_DATA (STATE_CPU (sd, i));
949 if (PROFILE_FILE (data) != NULL)
950 {
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))
956 break;
957 if (i == j)
958 fclose (PROFILE_FILE (data));
959 }
960 }
961 }
This page took 0.049444 seconds and 5 git commands to generate.