ff882dfb3ca1b478a747758a7dbd905392581d8e
[deliverable/binutils-gdb.git] / sim / common / sim-profile.c
1 /* Default profiling support.
2 Copyright (C) 1996, 1997, 1998 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 if (PROFILE_PC_COUNT (profile) == 0)
429 return;
430
431 sim_io_printf (sd, "Program Counter Statistics:\n\n");
432
433 /* First pass over data computes various things. */
434 max_val = 0;
435 total = 0;
436 for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i)
437 {
438 total += PROFILE_PC_COUNT (profile) [i];
439 if (PROFILE_PC_COUNT (profile) [i] > max_val)
440 max_val = PROFILE_PC_COUNT (profile) [i];
441 }
442
443 sim_io_printf (sd, " Total samples: %s\n",
444 COMMAS (total));
445 sim_io_printf (sd, " Granularity: %s bytes per bucket\n",
446 COMMAS (PROFILE_PC_BUCKET_SIZE (profile)));
447 sim_io_printf (sd, " Size: %s buckets\n",
448 COMMAS (PROFILE_PC_NR_BUCKETS (profile)));
449 sim_io_printf (sd, " Frequency: %s cycles per sample\n",
450 COMMAS (PROFILE_PC_FREQ (profile)));
451
452 if (PROFILE_PC_END (profile) != 0)
453 sim_io_printf (sd, " Range: 0x%lx 0x%lx\n",
454 (long) PROFILE_PC_START (profile),
455 (long) PROFILE_PC_END (profile));
456
457 if (verbose && max_val != 0)
458 {
459 /* Now we can print the histogram. */
460 sim_io_printf (sd, "\n");
461 for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i)
462 {
463 if (PROFILE_PC_COUNT (profile) [i] != 0)
464 {
465 sim_io_printf (sd, " ");
466 if (i == PROFILE_PC_NR_BUCKETS (profile))
467 sim_io_printf (sd, "%10s:", "overflow");
468 else
469 sim_io_printf (sd, "0x%08lx:",
470 (long) (PROFILE_PC_START (profile)
471 + (i * PROFILE_PC_BUCKET_SIZE (profile))));
472 sim_io_printf (sd, " %*s",
473 max_val < 10000 ? 5 : 10,
474 COMMAS (PROFILE_PC_COUNT (profile) [i]));
475 sim_io_printf (sd, " %4.1f",
476 (PROFILE_PC_COUNT (profile) [i] * 100.0) / total);
477 sim_io_printf (sd, ": ");
478 print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
479 PROFILE_PC_COUNT (profile) [i],
480 max_val);
481 sim_io_printf (sd, "\n");
482 }
483 }
484 }
485
486 /* dump the histogram to the file "gmon.out" using BSD's gprof file
487 format */
488 /* Since a profile data file is in the native format of the host on
489 which the profile is being, endian issues are not considered in
490 the code below. */
491 /* FIXME: Is this the best place for this code? */
492 {
493 FILE *pf = fopen ("gmon.out", "wb");
494
495 if (pf == NULL)
496 sim_io_eprintf (sd, "Failed to open \"gmon.out\" profile file\n");
497 else
498 {
499 int ok;
500 /* FIXME: what if the target has a 64 bit PC? */
501 unsigned32 header[3];
502 unsigned loop;
503 if (PROFILE_PC_END (profile) != 0)
504 {
505 header[0] = PROFILE_PC_START (profile);
506 header[1] = PROFILE_PC_END (profile);
507 }
508 else
509 {
510 header[0] = 0;
511 header[1] = 0;
512 }
513 /* size of sample buffer (+ header) */
514 header[2] = PROFILE_PC_NR_BUCKETS (profile) * 2 + sizeof (header);
515 ok = fwrite (&header, sizeof (header), 1, pf);
516 for (loop = 0;
517 ok && (loop < PROFILE_PC_NR_BUCKETS (profile));
518 loop++)
519 {
520 signed16 sample;
521 if (PROFILE_PC_COUNT (profile) [loop] >= 0xffff)
522 sample = 0xffff;
523 else
524 sample = PROFILE_PC_COUNT (profile) [loop];
525 ok = fwrite (&sample, sizeof (sample), 1, pf);
526 }
527 if (ok == 0)
528 sim_io_eprintf (sd, "Failed to write to \"gmon.out\" profile file\n");
529 fclose(pf);
530 }
531 }
532
533 sim_io_printf (sd, "\n");
534 }
535
536 #endif
537 \f
538 /* Summary printing support. */
539
540 #if WITH_PROFILE_INSN_P
541
542 static void
543 profile_print_insn (sim_cpu *cpu, int verbose)
544 {
545 unsigned int i, n, total, max_val, max_name_len;
546 SIM_DESC sd = CPU_STATE (cpu);
547 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
548 char comma_buf[20];
549
550 sim_io_printf (sd, "Instruction Statistics:\n\n");
551
552 /* First pass over data computes various things. */
553 max_val = 0;
554 total = 0;
555 max_name_len = 0;
556 for (i = 0; i < MAX_INSNS; ++i)
557 {
558 if (INSN_NAME (i) == NULL)
559 continue;
560 total += PROFILE_INSN_COUNT (data) [i];
561 if (PROFILE_INSN_COUNT (data) [i] > max_val)
562 max_val = PROFILE_INSN_COUNT (data) [i];
563 n = strlen (INSN_NAME (i));
564 if (n > max_name_len)
565 max_name_len = n;
566 }
567 /* set the total insn count, in case client is being lazy */
568 if (PROFILE_TOTAL_INSN_COUNT (data))
569 PROFILE_TOTAL_INSN_COUNT (data) = total;
570
571 sim_io_printf (sd, " Total: %s insns\n", COMMAS (total));
572
573 if (verbose && max_val != 0)
574 {
575 /* Now we can print the histogram. */
576 sim_io_printf (sd, "\n");
577 for (i = 0; i < MAX_INSNS; ++i)
578 {
579 if (INSN_NAME (i) == NULL)
580 continue;
581 if (PROFILE_INSN_COUNT (data) [i] != 0)
582 {
583 sim_io_printf (sd, " %*s: %*s: ",
584 max_name_len, INSN_NAME (i),
585 max_val < 10000 ? 5 : 10,
586 COMMAS (PROFILE_INSN_COUNT (data) [i]));
587 print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
588 PROFILE_INSN_COUNT (data) [i],
589 max_val);
590 sim_io_printf (sd, "\n");
591 }
592 }
593 }
594
595 sim_io_printf (sd, "\n");
596 }
597
598 #endif
599
600 #if WITH_PROFILE_MEMORY_P
601
602 static void
603 profile_print_memory (sim_cpu *cpu, int verbose)
604 {
605 unsigned int i, n;
606 unsigned int total_read, total_write;
607 unsigned int max_val, max_name_len;
608 /* FIXME: Need to add smp support. */
609 SIM_DESC sd = CPU_STATE (cpu);
610 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
611 char comma_buf[20];
612
613 sim_io_printf (sd, "Memory Access Statistics:\n\n");
614
615 /* First pass over data computes various things. */
616 max_val = total_read = total_write = max_name_len = 0;
617 for (i = 0; i < MAX_MODES; ++i)
618 {
619 total_read += PROFILE_READ_COUNT (data) [i];
620 total_write += PROFILE_WRITE_COUNT (data) [i];
621 if (PROFILE_READ_COUNT (data) [i] > max_val)
622 max_val = PROFILE_READ_COUNT (data) [i];
623 if (PROFILE_WRITE_COUNT (data) [i] > max_val)
624 max_val = PROFILE_WRITE_COUNT (data) [i];
625 n = strlen (MODE_NAME (i));
626 if (n > max_name_len)
627 max_name_len = n;
628 }
629
630 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
631 sim_io_printf (sd, " Total read: %s accesses\n",
632 COMMAS (total_read));
633 sim_io_printf (sd, " Total write: %s accesses\n",
634 COMMAS (total_write));
635
636 if (verbose && max_val != 0)
637 {
638 /* FIXME: Need to separate instruction fetches from data fetches
639 as the former swamps the latter. */
640 /* Now we can print the histogram. */
641 sim_io_printf (sd, "\n");
642 for (i = 0; i < MAX_MODES; ++i)
643 {
644 if (PROFILE_READ_COUNT (data) [i] != 0)
645 {
646 sim_io_printf (sd, " %*s read: %*s: ",
647 max_name_len, MODE_NAME (i),
648 max_val < 10000 ? 5 : 10,
649 COMMAS (PROFILE_READ_COUNT (data) [i]));
650 print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
651 PROFILE_READ_COUNT (data) [i],
652 max_val);
653 sim_io_printf (sd, "\n");
654 }
655 if (PROFILE_WRITE_COUNT (data) [i] != 0)
656 {
657 sim_io_printf (sd, " %*s write: %*s: ",
658 max_name_len, MODE_NAME (i),
659 max_val < 10000 ? 5 : 10,
660 COMMAS (PROFILE_WRITE_COUNT (data) [i]));
661 print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
662 PROFILE_WRITE_COUNT (data) [i],
663 max_val);
664 sim_io_printf (sd, "\n");
665 }
666 }
667 }
668
669 sim_io_printf (sd, "\n");
670 }
671
672 #endif
673
674 #if WITH_PROFILE_CORE_P
675
676 static void
677 profile_print_core (sim_cpu *cpu, int verbose)
678 {
679 unsigned int total;
680 unsigned int max_val;
681 /* FIXME: Need to add smp support. */
682 SIM_DESC sd = CPU_STATE (cpu);
683 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
684 char comma_buf[20];
685
686 sim_io_printf (sd, "CORE Statistics:\n\n");
687
688 /* First pass over data computes various things. */
689 {
690 sim_core_maps map;
691 total = 0;
692 max_val = 0;
693 for (map = 0; map < nr_sim_core_maps; map++)
694 {
695 total += PROFILE_CORE_COUNT (data) [map];
696 if (PROFILE_CORE_COUNT (data) [map] > max_val)
697 max_val = PROFILE_CORE_COUNT (data) [map];
698 }
699 }
700
701 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
702 sim_io_printf (sd, " Total: %s accesses\n",
703 COMMAS (total));
704
705 if (verbose && max_val != 0)
706 {
707 sim_core_maps map;
708 /* Now we can print the histogram. */
709 sim_io_printf (sd, "\n");
710 for (map = 0; map < nr_sim_core_maps; map++)
711 {
712 if (PROFILE_CORE_COUNT (data) [map] != 0)
713 {
714 switch (map)
715 {
716 case sim_core_read_map:
717 sim_io_printf (sd, " read:");
718 break;
719 case sim_core_write_map:
720 sim_io_printf (sd, " write:");
721 break;
722 case sim_core_execute_map:
723 sim_io_printf (sd, " exec:");
724 break;
725 case nr_sim_core_maps:
726 ; /* ignore */
727 }
728 sim_io_printf (sd, "%*s: ",
729 max_val < 10000 ? 5 : 10,
730 COMMAS (PROFILE_CORE_COUNT (data) [map]));
731 print_bar (sd, PROFILE_HISTOGRAM_WIDTH,
732 PROFILE_CORE_COUNT (data) [map],
733 max_val);
734 sim_io_printf (sd, "\n");
735 }
736 }
737 }
738
739 sim_io_printf (sd, "\n");
740 }
741
742 #endif
743
744 #if WITH_PROFILE_MODEL_P
745
746 static void
747 profile_print_model (sim_cpu *cpu, int verbose)
748 {
749 SIM_DESC sd = CPU_STATE (cpu);
750 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
751 unsigned long cti_stalls = PROFILE_MODEL_CTI_STALL_COUNT (data);
752 unsigned long load_stalls = PROFILE_MODEL_LOAD_STALL_COUNT (data);
753 unsigned long total = PROFILE_MODEL_CYCLE_COUNT (data)
754 + cti_stalls + load_stalls;
755 char comma_buf[20];
756
757 sim_io_printf (sd, "Model %s Timing Information\n\n",
758 MODEL_NAME (CPU_MODEL (cpu)));
759 sim_io_printf (sd, " %-*s %s\n",
760 PROFILE_LABEL_WIDTH, "Taken branches:",
761 COMMAS (PROFILE_MODEL_TAKEN_COUNT (data)));
762 sim_io_printf (sd, " %-*s %s\n",
763 PROFILE_LABEL_WIDTH, "Untaken branches:",
764 COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data)));
765 sim_io_printf (sd, " %-*s %s\n",
766 PROFILE_LABEL_WIDTH, "Cycles stalled due to branches:",
767 COMMAS (cti_stalls));
768 sim_io_printf (sd, " %-*s %s\n",
769 PROFILE_LABEL_WIDTH, "Cycles stalled due to loads:",
770 COMMAS (load_stalls));
771 sim_io_printf (sd, " %-*s %s\n",
772 PROFILE_LABEL_WIDTH, "Total cycles (*approximate*):",
773 COMMAS (total));
774 sim_io_printf (sd, "\n");
775 }
776
777 #endif
778
779
780 #if WITH_PROFILE_INSN_P || WITH_PROFILE_MEMORY_P || WITH_PROFILE_CORE_P || WITH_PROFILE_PC_P
781
782 static void
783 print_bar (SIM_DESC sd, unsigned int width,
784 unsigned int val, unsigned int max_val)
785 {
786 unsigned int i, count;
787
788 count = ((double) val / (double) max_val) * (double) width;
789
790 for (i = 0; i < count; ++i)
791 sim_io_printf (sd, "*");
792 }
793
794 #endif
795
796 /* Print the simulator's execution speed for CPU. */
797
798 static void
799 profile_print_speed (sim_cpu *cpu)
800 {
801 SIM_DESC sd = CPU_STATE (cpu);
802 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
803 unsigned long milliseconds = sim_events_elapsed_time (sd);
804 unsigned long total = PROFILE_TOTAL_INSN_COUNT (data);
805 char comma_buf[20];
806
807 sim_io_printf (sd, "Simulator Execution Speed\n\n");
808
809 if (total != 0)
810 sim_io_printf (sd, " Total instructions: %s\n", COMMAS (total));
811
812 if (milliseconds < 1000)
813 sim_io_printf (sd, " Total Execution Time: < 1 second\n\n");
814 else
815 {
816 /* The printing of the time rounded to 2 decimal places makes the speed
817 calculation seem incorrect [even though it is correct]. So round
818 MILLISECONDS first. This can marginally affect the result, but it's
819 better that the user not perceive there's a math error. */
820 double secs = (double) milliseconds / 1000;
821 secs = ((double) (unsigned long) (secs * 100 + .5)) / 100;
822 sim_io_printf (sd, " Total Execution Time: %.2f seconds\n", secs);
823 /* Don't confuse things with data that isn't useful.
824 If we ran for less than 2 seconds, only use the data if we
825 executed more than 100,000 insns. */
826 if (secs >= 2 || total >= 100000)
827 sim_io_printf (sd, " Simulator Speed: %s insns/second\n\n",
828 COMMAS ((unsigned long) ((double) total / secs)));
829 }
830 }
831
832 /* Top level function to print all summary profile information.
833 It is [currently] intended that all such data is printed by this function.
834 I'd rather keep it all in one place for now. To that end, MISC_CPU and
835 MISC are callbacks used to print any miscellaneous data.
836
837 One might want to add a user option that allows printing by type or by cpu
838 (i.e. print all insn data for each cpu first, or print data cpu by cpu).
839 This may be a case of featuritis so it's currently left out.
840
841 Note that results are indented two spaces to distinguish them from
842 section titles. */
843
844 void
845 profile_print (SIM_DESC sd, int verbose,
846 PROFILE_CALLBACK *misc, PROFILE_CPU_CALLBACK *misc_cpu)
847 {
848 int i,c;
849 int print_title_p = 0;
850
851 /* Only print the title if some data has been collected. */
852 /* FIXME: If the number of processors can be selected on the command line,
853 then MAX_NR_PROCESSORS will need to take an argument of `sd'. */
854
855 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
856 {
857 sim_cpu *cpu = STATE_CPU (sd, c);
858 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
859
860 for (i = 0; i < MAX_PROFILE_VALUES; ++i)
861 if (PROFILE_FLAGS (data) [i])
862 print_title_p = 1;
863 /* One could break out early if print_title_p is set. */
864 }
865 if (print_title_p)
866 sim_io_printf (sd, "Summary profiling results:\n\n");
867
868 /* Loop, cpu by cpu, printing results. */
869
870 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
871 {
872 sim_cpu *cpu = STATE_CPU (sd, c);
873 PROFILE_DATA *data = CPU_PROFILE_DATA (cpu);
874
875 if (MAX_NR_PROCESSORS > 1
876 && (0
877 #if WITH_PROFILE_INSN_P
878 || PROFILE_FLAGS (data) [PROFILE_INSN_IDX]
879 #endif
880 #if WITH_PROFILE_MEMORY_P
881 || PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX]
882 #endif
883 #if WITH_PROFILE_CORE_P
884 || PROFILE_FLAGS (data) [PROFILE_CORE_IDX]
885 #endif
886 #if WITH_PROFILE_MODEL_P
887 || PROFILE_FLAGS (data) [PROFILE_MODEL_IDX]
888 #endif
889 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
890 || PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX]
891 #endif
892 #if WITH_PROFILE_PC_P
893 || PROFILE_FLAGS (data) [PROFILE_PC_IDX]
894 #endif
895 ))
896 {
897 sim_io_printf (sd, "CPU %d\n\n", c);
898 }
899
900 #if WITH_PROFILE_INSN_P
901 if (PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
902 profile_print_insn (cpu, verbose);
903 #endif
904
905 #if WITH_PROFILE_MEMORY_P
906 if (PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX])
907 profile_print_memory (cpu, verbose);
908 #endif
909
910 #if WITH_PROFILE_CORE_P
911 if (PROFILE_FLAGS (data) [PROFILE_CORE_IDX])
912 profile_print_core (cpu, verbose);
913 #endif
914
915 #if WITH_PROFILE_MODEL_P
916 if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX])
917 profile_print_model (cpu, verbose);
918 #endif
919
920 #if WITH_PROFILE_SCACHE_P && WITH_SCACHE
921 if (PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX])
922 scache_print_profile (cpu, verbose);
923 #endif
924
925 #if WITH_PROFILE_PC_P
926 if (PROFILE_FLAGS (data) [PROFILE_PC_IDX])
927 profile_print_pc (cpu, verbose);
928 #endif
929
930 /* Print cpu-specific data before the execution speed. */
931 if (misc_cpu != NULL)
932 (*misc_cpu) (cpu, verbose);
933
934 /* Always try to print execution time and speed. */
935 if (verbose
936 || PROFILE_FLAGS (data) [PROFILE_INSN_IDX])
937 profile_print_speed (cpu);
938 }
939
940 /* Finally print non-cpu specific miscellaneous data. */
941
942 if (misc != NULL)
943 (*misc) (sd, verbose);
944 }
945 \f
946 /* Install profiling support in the simulator. */
947
948 SIM_RC
949 profile_install (SIM_DESC sd)
950 {
951 int i;
952
953 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
954 sim_add_option_table (sd, profile_options);
955 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
956 memset (CPU_PROFILE_DATA (STATE_CPU (sd, i)), 0,
957 sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd, i))));
958 #if WITH_PROFILE_PC_P
959 sim_module_add_uninstall_fn (sd, profile_pc_uninstall);
960 sim_module_add_init_fn (sd, profile_pc_init);
961 #endif
962 sim_module_add_uninstall_fn (sd, profile_uninstall);
963 return SIM_RC_OK;
964 }
965
966 static void
967 profile_uninstall (SIM_DESC sd)
968 {
969 int i,j;
970
971 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
972 {
973 PROFILE_DATA *data = CPU_PROFILE_DATA (STATE_CPU (sd, i));
974 if (PROFILE_FILE (data) != NULL)
975 {
976 /* If output from different cpus is going to the same file,
977 avoid closing the file twice. */
978 for (j = 0; j < i; ++j)
979 if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd, j)))
980 == PROFILE_FILE (data))
981 break;
982 if (i == j)
983 fclose (PROFILE_FILE (data));
984 }
985 }
986 }
This page took 0.048556 seconds and 4 git commands to generate.