Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Default profiling support. |
3666a048 | 2 | Copyright (C) 1996-2021 Free Software Foundation, Inc. |
c906108c SS |
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 | |
4744ac1b JB |
9 | the Free Software Foundation; either version 3 of the License, or |
10 | (at your option) any later version. | |
c906108c SS |
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 | ||
4744ac1b JB |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
c906108c SS |
19 | |
20 | #include "sim-main.h" | |
21 | #include "sim-io.h" | |
22 | #include "sim-options.h" | |
23 | #include "sim-assert.h" | |
24 | ||
c906108c | 25 | #include <stdlib.h> |
c906108c | 26 | #include <string.h> |
09032128 | 27 | #include <ctype.h> |
c906108c | 28 | |
5be229c0 MF |
29 | #if !WITH_PROFILE_PC_P |
30 | static unsigned int _profile_stub; | |
31 | # define PROFILE_PC_FREQ(p) _profile_stub | |
32 | # define PROFILE_PC_NR_BUCKETS(p) _profile_stub | |
33 | # define PROFILE_PC_SHIFT(p) _profile_stub | |
34 | # define PROFILE_PC_START(p) _profile_stub | |
35 | # define PROFILE_PC_END(p) _profile_stub | |
36 | # define PROFILE_INSN_COUNT(p) &_profile_stub | |
37 | #endif | |
38 | ||
c906108c SS |
39 | #define COMMAS(n) sim_add_commas (comma_buf, sizeof (comma_buf), (n)) |
40 | ||
41 | static MODULE_INIT_FN profile_init; | |
42 | static MODULE_UNINSTALL_FN profile_uninstall; | |
43 | ||
44 | static DECLARE_OPTION_HANDLER (profile_option_handler); | |
45 | ||
46 | enum { | |
47 | OPTION_PROFILE_INSN = OPTION_START, | |
48 | OPTION_PROFILE_MEMORY, | |
49 | OPTION_PROFILE_MODEL, | |
50 | OPTION_PROFILE_FILE, | |
51 | OPTION_PROFILE_CORE, | |
09032128 | 52 | OPTION_PROFILE_CPU_FREQUENCY, |
c906108c SS |
53 | OPTION_PROFILE_PC, |
54 | OPTION_PROFILE_PC_RANGE, | |
55 | OPTION_PROFILE_PC_GRANULARITY, | |
56 | OPTION_PROFILE_RANGE, | |
57 | OPTION_PROFILE_FUNCTION | |
58 | }; | |
59 | ||
60 | static const OPTION profile_options[] = { | |
61 | { {"profile", optional_argument, NULL, 'p'}, | |
62 | 'p', "on|off", "Perform profiling", | |
21cf617c | 63 | profile_option_handler, NULL }, |
c906108c SS |
64 | { {"profile-insn", optional_argument, NULL, OPTION_PROFILE_INSN}, |
65 | '\0', "on|off", "Perform instruction profiling", | |
21cf617c | 66 | profile_option_handler, NULL }, |
c906108c SS |
67 | { {"profile-memory", optional_argument, NULL, OPTION_PROFILE_MEMORY}, |
68 | '\0', "on|off", "Perform memory profiling", | |
21cf617c | 69 | profile_option_handler, NULL }, |
c906108c SS |
70 | { {"profile-core", optional_argument, NULL, OPTION_PROFILE_CORE}, |
71 | '\0', "on|off", "Perform CORE profiling", | |
21cf617c | 72 | profile_option_handler, NULL }, |
c906108c SS |
73 | { {"profile-model", optional_argument, NULL, OPTION_PROFILE_MODEL}, |
74 | '\0', "on|off", "Perform model profiling", | |
21cf617c | 75 | profile_option_handler, NULL }, |
09032128 DB |
76 | { {"profile-cpu-frequency", required_argument, NULL, |
77 | OPTION_PROFILE_CPU_FREQUENCY}, | |
78 | '\0', "CPU FREQUENCY", "Specify the speed of the simulated cpu clock", | |
21cf617c | 79 | profile_option_handler, NULL }, |
c906108c SS |
80 | |
81 | { {"profile-file", required_argument, NULL, OPTION_PROFILE_FILE}, | |
82 | '\0', "FILE NAME", "Specify profile output file", | |
21cf617c | 83 | profile_option_handler, NULL }, |
c906108c SS |
84 | |
85 | { {"profile-pc", optional_argument, NULL, OPTION_PROFILE_PC}, | |
86 | '\0', "on|off", "Perform PC profiling", | |
21cf617c | 87 | profile_option_handler, NULL }, |
c906108c SS |
88 | { {"profile-pc-frequency", required_argument, NULL, 'F'}, |
89 | 'F', "PC PROFILE FREQUENCY", "Specified PC profiling frequency", | |
21cf617c | 90 | profile_option_handler, NULL }, |
c906108c SS |
91 | { {"profile-pc-size", required_argument, NULL, 'S'}, |
92 | 'S', "PC PROFILE SIZE", "Specify PC profiling size", | |
21cf617c | 93 | profile_option_handler, NULL }, |
c906108c SS |
94 | { {"profile-pc-granularity", required_argument, NULL, OPTION_PROFILE_PC_GRANULARITY}, |
95 | '\0', "PC PROFILE GRANULARITY", "Specify PC profiling sample coverage", | |
21cf617c | 96 | profile_option_handler, NULL }, |
c906108c SS |
97 | { {"profile-pc-range", required_argument, NULL, OPTION_PROFILE_PC_RANGE}, |
98 | '\0', "BASE,BOUND", "Specify PC profiling address range", | |
21cf617c | 99 | profile_option_handler, NULL }, |
c906108c SS |
100 | |
101 | #ifdef SIM_HAVE_ADDR_RANGE | |
102 | { {"profile-range", required_argument, NULL, OPTION_PROFILE_RANGE}, | |
103 | '\0', "START,END", "Specify range of addresses for instruction and model profiling", | |
21cf617c | 104 | profile_option_handler, NULL }, |
c906108c SS |
105 | #if 0 /*wip*/ |
106 | { {"profile-function", required_argument, NULL, OPTION_PROFILE_FUNCTION}, | |
107 | '\0', "FUNCTION", "Specify function to profile", | |
21cf617c | 108 | profile_option_handler, NULL }, |
c906108c SS |
109 | #endif |
110 | #endif | |
111 | ||
21cf617c | 112 | { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL } |
c906108c SS |
113 | }; |
114 | ||
115 | /* Set/reset the profile options indicated in MASK. */ | |
116 | ||
ed9a39eb | 117 | SIM_RC |
c906108c SS |
118 | set_profile_option_mask (SIM_DESC sd, const char *name, int mask, const char *arg) |
119 | { | |
120 | int profile_nr; | |
121 | int cpu_nr; | |
122 | int profile_val = 1; | |
123 | ||
124 | if (arg != NULL) | |
125 | { | |
126 | if (strcmp (arg, "yes") == 0 | |
127 | || strcmp (arg, "on") == 0 | |
128 | || strcmp (arg, "1") == 0) | |
129 | profile_val = 1; | |
130 | else if (strcmp (arg, "no") == 0 | |
131 | || strcmp (arg, "off") == 0 | |
132 | || strcmp (arg, "0") == 0) | |
133 | profile_val = 0; | |
134 | else | |
135 | { | |
136 | sim_io_eprintf (sd, "Argument `%s' for `--profile%s' invalid, one of `on', `off', `yes', `no' expected\n", arg, name); | |
137 | return SIM_RC_FAIL; | |
138 | } | |
139 | } | |
140 | ||
141 | /* update applicable profile bits */ | |
142 | for (profile_nr = 0; profile_nr < MAX_PROFILE_VALUES; ++profile_nr) | |
143 | { | |
144 | if ((mask & (1 << profile_nr)) == 0) | |
145 | continue; | |
146 | ||
147 | #if 0 /* see sim-trace.c, set flags in STATE here if/when there are any */ | |
148 | /* Set non-cpu specific values. */ | |
149 | switch (profile_nr) | |
150 | { | |
151 | case ??? : | |
152 | break; | |
153 | } | |
154 | #endif | |
155 | ||
156 | /* Set cpu values. */ | |
157 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++) | |
158 | { | |
159 | CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[profile_nr] = profile_val; | |
160 | } | |
161 | } | |
162 | ||
163 | /* Re-compute the cpu profile summary. */ | |
164 | if (profile_val) | |
165 | { | |
166 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++) | |
167 | CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))->profile_any_p = 1; | |
168 | } | |
169 | else | |
170 | { | |
171 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++) | |
172 | { | |
173 | CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))->profile_any_p = 0; | |
174 | for (profile_nr = 0; profile_nr < MAX_PROFILE_VALUES; ++profile_nr) | |
175 | { | |
176 | if (CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[profile_nr]) | |
177 | { | |
178 | CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))->profile_any_p = 1; | |
179 | break; | |
180 | } | |
181 | } | |
182 | } | |
028f6515 | 183 | } |
c906108c SS |
184 | |
185 | return SIM_RC_OK; | |
186 | } | |
187 | ||
188 | /* Set one profile option based on its IDX value. | |
189 | Not static as cgen-scache.c uses it. */ | |
190 | ||
191 | SIM_RC | |
192 | sim_profile_set_option (SIM_DESC sd, const char *name, int idx, const char *arg) | |
193 | { | |
194 | return set_profile_option_mask (sd, name, 1 << idx, arg); | |
195 | } | |
196 | ||
09032128 DB |
197 | static SIM_RC |
198 | parse_frequency (SIM_DESC sd, const char *arg, unsigned long *freq) | |
199 | { | |
200 | const char *ch; | |
201 | /* First, parse a decimal number. */ | |
202 | *freq = 0; | |
203 | ch = arg; | |
204 | if (isdigit (*arg)) | |
205 | { | |
206 | for (/**/; *ch != '\0'; ++ch) | |
207 | { | |
208 | if (! isdigit (*ch)) | |
209 | break; | |
210 | *freq = *freq * 10 + (*ch - '0'); | |
211 | } | |
212 | ||
213 | /* Accept KHz, MHz or Hz as a suffix. */ | |
214 | if (tolower (*ch) == 'm') | |
215 | { | |
216 | *freq *= 1000000; | |
217 | ++ch; | |
218 | } | |
219 | else if (tolower (*ch) == 'k') | |
220 | { | |
221 | *freq *= 1000; | |
222 | ++ch; | |
223 | } | |
224 | ||
225 | if (tolower (*ch) == 'h') | |
226 | { | |
227 | ++ch; | |
228 | if (tolower (*ch) == 'z') | |
229 | ++ch; | |
230 | } | |
231 | } | |
232 | ||
233 | if (*ch != '\0') | |
234 | { | |
235 | sim_io_eprintf (sd, "Invalid argument for --profile-cpu-frequency: %s\n", | |
236 | arg); | |
237 | *freq = 0; | |
238 | return SIM_RC_FAIL; | |
239 | } | |
240 | ||
241 | return SIM_RC_OK; | |
242 | } | |
243 | ||
c906108c SS |
244 | static SIM_RC |
245 | profile_option_handler (SIM_DESC sd, | |
246 | sim_cpu *cpu, | |
247 | int opt, | |
248 | char *arg, | |
249 | int is_command) | |
250 | { | |
cdc2a5c3 | 251 | int cpu_nr; |
c906108c SS |
252 | |
253 | /* FIXME: Need to handle `cpu' arg. */ | |
254 | ||
255 | switch (opt) | |
256 | { | |
257 | case 'p' : | |
258 | if (! WITH_PROFILE) | |
259 | sim_io_eprintf (sd, "Profiling not compiled in, `-p' ignored\n"); | |
260 | else | |
261 | return set_profile_option_mask (sd, "profile", PROFILE_USEFUL_MASK, | |
262 | arg); | |
263 | break; | |
264 | ||
265 | case OPTION_PROFILE_INSN : | |
266 | if (WITH_PROFILE_INSN_P) | |
267 | return sim_profile_set_option (sd, "-insn", PROFILE_INSN_IDX, arg); | |
268 | else | |
269 | sim_io_eprintf (sd, "Instruction profiling not compiled in, `--profile-insn' ignored\n"); | |
270 | break; | |
271 | ||
272 | case OPTION_PROFILE_MEMORY : | |
273 | if (WITH_PROFILE_MEMORY_P) | |
274 | return sim_profile_set_option (sd, "-memory", PROFILE_MEMORY_IDX, arg); | |
275 | else | |
276 | sim_io_eprintf (sd, "Memory profiling not compiled in, `--profile-memory' ignored\n"); | |
277 | break; | |
278 | ||
279 | case OPTION_PROFILE_CORE : | |
280 | if (WITH_PROFILE_CORE_P) | |
281 | return sim_profile_set_option (sd, "-core", PROFILE_CORE_IDX, arg); | |
282 | else | |
283 | sim_io_eprintf (sd, "CORE profiling not compiled in, `--profile-core' ignored\n"); | |
284 | break; | |
285 | ||
286 | case OPTION_PROFILE_MODEL : | |
287 | if (WITH_PROFILE_MODEL_P) | |
288 | return sim_profile_set_option (sd, "-model", PROFILE_MODEL_IDX, arg); | |
289 | else | |
290 | sim_io_eprintf (sd, "Model profiling not compiled in, `--profile-model' ignored\n"); | |
291 | break; | |
292 | ||
09032128 DB |
293 | case OPTION_PROFILE_CPU_FREQUENCY : |
294 | { | |
295 | unsigned long val; | |
296 | SIM_RC rc = parse_frequency (sd, arg, &val); | |
297 | if (rc == SIM_RC_OK) | |
298 | { | |
299 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr) | |
300 | PROFILE_CPU_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd,cpu_nr))) = val; | |
301 | } | |
302 | return rc; | |
303 | } | |
304 | ||
c906108c SS |
305 | case OPTION_PROFILE_FILE : |
306 | /* FIXME: Might want this to apply to pc profiling only, | |
307 | or have two profile file options. */ | |
308 | if (! WITH_PROFILE) | |
309 | sim_io_eprintf (sd, "Profiling not compiled in, `--profile-file' ignored\n"); | |
310 | else | |
311 | { | |
312 | FILE *f = fopen (arg, "w"); | |
313 | ||
314 | if (f == NULL) | |
315 | { | |
316 | sim_io_eprintf (sd, "Unable to open profile output file `%s'\n", arg); | |
317 | return SIM_RC_FAIL; | |
318 | } | |
319 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr) | |
320 | PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = f; | |
321 | } | |
322 | break; | |
323 | ||
324 | case OPTION_PROFILE_PC: | |
325 | if (WITH_PROFILE_PC_P) | |
326 | return sim_profile_set_option (sd, "-pc", PROFILE_PC_IDX, arg); | |
327 | else | |
328 | sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc' ignored\n"); | |
329 | break; | |
330 | ||
331 | case 'F' : | |
332 | if (WITH_PROFILE_PC_P) | |
333 | { | |
334 | /* FIXME: Validate arg. */ | |
335 | int val = atoi (arg); | |
336 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr) | |
337 | PROFILE_PC_FREQ (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = val; | |
338 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr) | |
339 | CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1; | |
340 | } | |
341 | else | |
342 | sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-frequency' ignored\n"); | |
343 | break; | |
344 | ||
345 | case 'S' : | |
346 | if (WITH_PROFILE_PC_P) | |
347 | { | |
348 | /* FIXME: Validate arg. */ | |
349 | int val = atoi (arg); | |
350 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr) | |
351 | PROFILE_PC_NR_BUCKETS (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = val; | |
352 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr) | |
353 | CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1; | |
354 | } | |
355 | else | |
356 | sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-size' ignored\n"); | |
357 | break; | |
358 | ||
359 | case OPTION_PROFILE_PC_GRANULARITY: | |
360 | if (WITH_PROFILE_PC_P) | |
361 | { | |
362 | int shift; | |
363 | int val = atoi (arg); | |
364 | /* check that the granularity is a power of two */ | |
365 | shift = 0; | |
366 | while (val > (1 << shift)) | |
367 | { | |
368 | shift += 1; | |
369 | } | |
370 | if (val != (1 << shift)) | |
371 | { | |
372 | sim_io_eprintf (sd, "PC profiling granularity not a power of two\n"); | |
373 | return SIM_RC_FAIL; | |
374 | } | |
375 | if (shift == 0) | |
376 | { | |
377 | sim_io_eprintf (sd, "PC profiling granularity too small"); | |
378 | return SIM_RC_FAIL; | |
379 | } | |
380 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr) | |
381 | PROFILE_PC_SHIFT (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = shift; | |
382 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr) | |
383 | CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1; | |
384 | } | |
385 | else | |
386 | sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-granularity' ignored\n"); | |
387 | break; | |
388 | ||
389 | case OPTION_PROFILE_PC_RANGE: | |
390 | if (WITH_PROFILE_PC_P) | |
391 | { | |
392 | /* FIXME: Validate args */ | |
393 | char *chp = arg; | |
394 | unsigned long base; | |
395 | unsigned long bound; | |
396 | base = strtoul (chp, &chp, 0); | |
397 | if (*chp != ',') | |
398 | { | |
399 | sim_io_eprintf (sd, "--profile-pc-range missing BOUND argument\n"); | |
400 | return SIM_RC_FAIL; | |
401 | } | |
402 | bound = strtoul (chp + 1, NULL, 0); | |
403 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr) | |
404 | { | |
405 | PROFILE_PC_START (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = base; | |
406 | PROFILE_PC_END (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))) = bound; | |
028f6515 | 407 | } |
c906108c SS |
408 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr) |
409 | CPU_PROFILE_FLAGS (STATE_CPU (sd, cpu_nr))[PROFILE_PC_IDX] = 1; | |
410 | } | |
411 | else | |
412 | sim_io_eprintf (sd, "PC profiling not compiled in, `--profile-pc-range' ignored\n"); | |
413 | break; | |
414 | ||
415 | #ifdef SIM_HAVE_ADDR_RANGE | |
416 | case OPTION_PROFILE_RANGE : | |
417 | if (WITH_PROFILE) | |
418 | { | |
419 | char *chp = arg; | |
420 | unsigned long start,end; | |
421 | start = strtoul (chp, &chp, 0); | |
422 | if (*chp != ',') | |
423 | { | |
424 | sim_io_eprintf (sd, "--profile-range missing END argument\n"); | |
425 | return SIM_RC_FAIL; | |
426 | } | |
427 | end = strtoul (chp + 1, NULL, 0); | |
428 | /* FIXME: Argument validation. */ | |
429 | if (cpu != NULL) | |
430 | sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)), | |
431 | start, end); | |
432 | else | |
433 | for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr) | |
434 | sim_addr_range_add (PROFILE_RANGE (CPU_PROFILE_DATA (STATE_CPU (sd, cpu_nr))), | |
435 | start, end); | |
436 | } | |
437 | else | |
438 | sim_io_eprintf (sd, "Profiling not compiled in, `--profile-range' ignored\n"); | |
439 | break; | |
440 | ||
441 | case OPTION_PROFILE_FUNCTION : | |
442 | if (WITH_PROFILE) | |
443 | { | |
444 | /*wip: need to compute function range given name*/ | |
445 | } | |
446 | else | |
447 | sim_io_eprintf (sd, "Profiling not compiled in, `--profile-function' ignored\n"); | |
448 | break; | |
449 | #endif /* SIM_HAVE_ADDR_RANGE */ | |
450 | } | |
451 | ||
452 | return SIM_RC_OK; | |
453 | } | |
454 | \f | |
0d3d2c71 MF |
455 | /* Profiling output hooks. */ |
456 | ||
457 | static void | |
458 | profile_vprintf (SIM_DESC sd, sim_cpu *cpu, const char *fmt, va_list ap) | |
459 | { | |
460 | FILE *fp = PROFILE_FILE (CPU_PROFILE_DATA (cpu)); | |
461 | ||
462 | /* If an output file was given, redirect output to that. */ | |
463 | if (fp != NULL) | |
464 | vfprintf (fp, fmt, ap); | |
465 | else | |
466 | sim_io_evprintf (sd, fmt, ap); | |
467 | } | |
468 | ||
f0c4dc40 | 469 | ATTRIBUTE_PRINTF (3, 4) |
0d3d2c71 MF |
470 | static void |
471 | profile_printf (SIM_DESC sd, sim_cpu *cpu, const char *fmt, ...) | |
472 | { | |
473 | va_list ap; | |
474 | ||
475 | va_start (ap, fmt); | |
476 | profile_vprintf (sd, cpu, fmt, ap); | |
477 | va_end (ap); | |
478 | } | |
479 | \f | |
c906108c SS |
480 | /* PC profiling support */ |
481 | ||
482 | #if WITH_PROFILE_PC_P | |
483 | ||
484 | static void | |
485 | profile_pc_cleanup (SIM_DESC sd) | |
486 | { | |
487 | int n; | |
488 | for (n = 0; n < MAX_NR_PROCESSORS; n++) | |
489 | { | |
490 | sim_cpu *cpu = STATE_CPU (sd, n); | |
491 | PROFILE_DATA *data = CPU_PROFILE_DATA (cpu); | |
492 | if (PROFILE_PC_COUNT (data) != NULL) | |
d79fe0d6 | 493 | free (PROFILE_PC_COUNT (data)); |
c906108c SS |
494 | PROFILE_PC_COUNT (data) = NULL; |
495 | if (PROFILE_PC_EVENT (data) != NULL) | |
496 | sim_events_deschedule (sd, PROFILE_PC_EVENT (data)); | |
497 | PROFILE_PC_EVENT (data) = NULL; | |
498 | } | |
499 | } | |
500 | ||
501 | ||
502 | static void | |
503 | profile_pc_uninstall (SIM_DESC sd) | |
504 | { | |
505 | profile_pc_cleanup (sd); | |
506 | } | |
507 | ||
508 | static void | |
509 | profile_pc_event (SIM_DESC sd, | |
510 | void *data) | |
511 | { | |
512 | sim_cpu *cpu = (sim_cpu*) data; | |
513 | PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu); | |
596f8827 | 514 | address_word pc = sim_pc_get (cpu); |
c906108c | 515 | unsigned i; |
c906108c SS |
516 | i = (pc - PROFILE_PC_START (profile)) >> PROFILE_PC_SHIFT (profile); |
517 | if (i < PROFILE_PC_NR_BUCKETS (profile)) | |
518 | PROFILE_PC_COUNT (profile) [i] += 1; /* Overflow? */ | |
519 | else | |
520 | PROFILE_PC_COUNT (profile) [PROFILE_PC_NR_BUCKETS (profile)] += 1; | |
028f6515 | 521 | PROFILE_PC_EVENT (profile) = |
c906108c SS |
522 | sim_events_schedule (sd, PROFILE_PC_FREQ (profile), profile_pc_event, cpu); |
523 | } | |
524 | ||
525 | static SIM_RC | |
526 | profile_pc_init (SIM_DESC sd) | |
527 | { | |
528 | int n; | |
529 | profile_pc_cleanup (sd); | |
530 | for (n = 0; n < MAX_NR_PROCESSORS; n++) | |
531 | { | |
532 | sim_cpu *cpu = STATE_CPU (sd, n); | |
533 | PROFILE_DATA *data = CPU_PROFILE_DATA (cpu); | |
596f8827 | 534 | if (CPU_PROFILE_FLAGS (STATE_CPU (sd, n))[PROFILE_PC_IDX]) |
c906108c SS |
535 | { |
536 | int bucket_size; | |
537 | /* fill in the frequency if not specified */ | |
538 | if (PROFILE_PC_FREQ (data) == 0) | |
41ec9f20 | 539 | PROFILE_PC_FREQ (data) = 257; |
c906108c SS |
540 | /* fill in the start/end if not specified */ |
541 | if (PROFILE_PC_END (data) == 0) | |
542 | { | |
543 | PROFILE_PC_START (data) = STATE_TEXT_START (sd); | |
544 | PROFILE_PC_END (data) = STATE_TEXT_END (sd); | |
545 | } | |
546 | /* Compute the number of buckets if not specified. */ | |
547 | if (PROFILE_PC_NR_BUCKETS (data) == 0) | |
548 | { | |
549 | if (PROFILE_PC_BUCKET_SIZE (data) == 0) | |
550 | PROFILE_PC_NR_BUCKETS (data) = 16; | |
551 | else | |
552 | { | |
553 | if (PROFILE_PC_END (data) == 0) | |
554 | { | |
555 | /* nr_buckets = (full-address-range / 2) / (bucket_size / 2) */ | |
556 | PROFILE_PC_NR_BUCKETS (data) = | |
18d4b488 | 557 | ((1ULL << sizeof (sim_cia) * (8 - 1)) |
c906108c SS |
558 | / (PROFILE_PC_BUCKET_SIZE (data) / 2)); |
559 | } | |
560 | else | |
561 | { | |
562 | PROFILE_PC_NR_BUCKETS (data) = | |
563 | ((PROFILE_PC_END (data) | |
564 | - PROFILE_PC_START (data) | |
565 | + PROFILE_PC_BUCKET_SIZE (data) - 1) | |
566 | / PROFILE_PC_BUCKET_SIZE (data)); | |
567 | } | |
568 | } | |
569 | } | |
570 | /* Compute the bucket size if not specified. Ensure that it | |
571 | is rounded up to the next power of two */ | |
572 | if (PROFILE_PC_BUCKET_SIZE (data) == 0) | |
573 | { | |
574 | if (PROFILE_PC_END (data) == 0) | |
575 | /* bucket_size = (full-address-range / 2) / (nr_buckets / 2) */ | |
18d4b488 | 576 | bucket_size = ((1ULL << ((sizeof (sim_cia) * 8) - 1)) |
c906108c SS |
577 | / (PROFILE_PC_NR_BUCKETS (data) / 2)); |
578 | else | |
579 | bucket_size = ((PROFILE_PC_END (data) | |
580 | - PROFILE_PC_START (data) | |
581 | + PROFILE_PC_NR_BUCKETS (data) - 1) | |
582 | / PROFILE_PC_NR_BUCKETS (data)); | |
583 | PROFILE_PC_SHIFT (data) = 0; | |
c43ad8eb | 584 | while (bucket_size > PROFILE_PC_BUCKET_SIZE (data)) |
c906108c SS |
585 | { |
586 | PROFILE_PC_SHIFT (data) += 1; | |
587 | } | |
588 | } | |
589 | /* Align the end address with bucket size */ | |
590 | if (PROFILE_PC_END (data) != 0) | |
591 | PROFILE_PC_END (data) = (PROFILE_PC_START (data) | |
592 | + (PROFILE_PC_BUCKET_SIZE (data) | |
593 | * PROFILE_PC_NR_BUCKETS (data))); | |
594 | /* create the relevant buffers */ | |
595 | PROFILE_PC_COUNT (data) = | |
596 | NZALLOC (unsigned, PROFILE_PC_NR_BUCKETS (data) + 1); | |
597 | PROFILE_PC_EVENT (data) = | |
598 | sim_events_schedule (sd, | |
599 | PROFILE_PC_FREQ (data), | |
600 | profile_pc_event, | |
601 | cpu); | |
602 | } | |
603 | } | |
604 | return SIM_RC_OK; | |
605 | } | |
606 | ||
607 | static void | |
608 | profile_print_pc (sim_cpu *cpu, int verbose) | |
609 | { | |
610 | SIM_DESC sd = CPU_STATE (cpu); | |
611 | PROFILE_DATA *profile = CPU_PROFILE_DATA (cpu); | |
612 | char comma_buf[20]; | |
613 | unsigned max_val; | |
614 | unsigned total; | |
615 | unsigned i; | |
616 | ||
617 | if (PROFILE_PC_COUNT (profile) == 0) | |
618 | return; | |
619 | ||
0d3d2c71 | 620 | profile_printf (sd, cpu, "Program Counter Statistics:\n\n"); |
c906108c SS |
621 | |
622 | /* First pass over data computes various things. */ | |
623 | max_val = 0; | |
624 | total = 0; | |
625 | for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i) | |
626 | { | |
627 | total += PROFILE_PC_COUNT (profile) [i]; | |
628 | if (PROFILE_PC_COUNT (profile) [i] > max_val) | |
629 | max_val = PROFILE_PC_COUNT (profile) [i]; | |
630 | } | |
631 | ||
0d3d2c71 MF |
632 | profile_printf (sd, cpu, " Total samples: %s\n", |
633 | COMMAS (total)); | |
634 | profile_printf (sd, cpu, " Granularity: %s bytes per bucket\n", | |
635 | COMMAS (PROFILE_PC_BUCKET_SIZE (profile))); | |
636 | profile_printf (sd, cpu, " Size: %s buckets\n", | |
637 | COMMAS (PROFILE_PC_NR_BUCKETS (profile))); | |
638 | profile_printf (sd, cpu, " Frequency: %s cycles per sample\n", | |
639 | COMMAS (PROFILE_PC_FREQ (profile))); | |
c906108c SS |
640 | |
641 | if (PROFILE_PC_END (profile) != 0) | |
0d3d2c71 MF |
642 | profile_printf (sd, cpu, " Range: 0x%lx 0x%lx\n", |
643 | (long) PROFILE_PC_START (profile), | |
c906108c SS |
644 | (long) PROFILE_PC_END (profile)); |
645 | ||
646 | if (verbose && max_val != 0) | |
647 | { | |
648 | /* Now we can print the histogram. */ | |
0d3d2c71 | 649 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
650 | for (i = 0; i <= PROFILE_PC_NR_BUCKETS (profile); ++i) |
651 | { | |
652 | if (PROFILE_PC_COUNT (profile) [i] != 0) | |
653 | { | |
0d3d2c71 | 654 | profile_printf (sd, cpu, " "); |
c906108c | 655 | if (i == PROFILE_PC_NR_BUCKETS (profile)) |
0d3d2c71 | 656 | profile_printf (sd, cpu, "%10s:", "overflow"); |
c906108c | 657 | else |
0d3d2c71 MF |
658 | profile_printf (sd, cpu, "0x%08lx:", |
659 | (long) (PROFILE_PC_START (profile) | |
660 | + (i * PROFILE_PC_BUCKET_SIZE (profile)))); | |
661 | profile_printf (sd, cpu, " %*s", | |
662 | max_val < 10000 ? 5 : 10, | |
663 | COMMAS (PROFILE_PC_COUNT (profile) [i])); | |
664 | profile_printf (sd, cpu, " %4.1f", | |
665 | (PROFILE_PC_COUNT (profile) [i] * 100.0) / total); | |
666 | profile_printf (sd, cpu, ": "); | |
667 | sim_profile_print_bar (sd, cpu, PROFILE_HISTOGRAM_WIDTH, | |
c906108c SS |
668 | PROFILE_PC_COUNT (profile) [i], |
669 | max_val); | |
0d3d2c71 | 670 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
671 | } |
672 | } | |
673 | } | |
674 | ||
675 | /* dump the histogram to the file "gmon.out" using BSD's gprof file | |
676 | format */ | |
677 | /* Since a profile data file is in the native format of the host on | |
678 | which the profile is being, endian issues are not considered in | |
679 | the code below. */ | |
680 | /* FIXME: Is this the best place for this code? */ | |
681 | { | |
682 | FILE *pf = fopen ("gmon.out", "wb"); | |
028f6515 | 683 | |
c906108c SS |
684 | if (pf == NULL) |
685 | sim_io_eprintf (sd, "Failed to open \"gmon.out\" profile file\n"); | |
686 | else | |
687 | { | |
688 | int ok; | |
689 | /* FIXME: what if the target has a 64 bit PC? */ | |
690 | unsigned32 header[3]; | |
691 | unsigned loop; | |
692 | if (PROFILE_PC_END (profile) != 0) | |
693 | { | |
694 | header[0] = PROFILE_PC_START (profile); | |
695 | header[1] = PROFILE_PC_END (profile); | |
696 | } | |
697 | else | |
698 | { | |
699 | header[0] = 0; | |
700 | header[1] = 0; | |
701 | } | |
702 | /* size of sample buffer (+ header) */ | |
703 | header[2] = PROFILE_PC_NR_BUCKETS (profile) * 2 + sizeof (header); | |
c43ad8eb BE |
704 | |
705 | /* Header must be written out in target byte order. */ | |
706 | H2T (header[0]); | |
707 | H2T (header[1]); | |
708 | H2T (header[2]); | |
709 | ||
c906108c SS |
710 | ok = fwrite (&header, sizeof (header), 1, pf); |
711 | for (loop = 0; | |
712 | ok && (loop < PROFILE_PC_NR_BUCKETS (profile)); | |
713 | loop++) | |
714 | { | |
715 | signed16 sample; | |
716 | if (PROFILE_PC_COUNT (profile) [loop] >= 0xffff) | |
717 | sample = 0xffff; | |
718 | else | |
719 | sample = PROFILE_PC_COUNT (profile) [loop]; | |
41ec9f20 | 720 | H2T (sample); |
c906108c SS |
721 | ok = fwrite (&sample, sizeof (sample), 1, pf); |
722 | } | |
723 | if (ok == 0) | |
724 | sim_io_eprintf (sd, "Failed to write to \"gmon.out\" profile file\n"); | |
34b47c38 | 725 | fclose (pf); |
c906108c SS |
726 | } |
727 | } | |
728 | ||
0d3d2c71 | 729 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
730 | } |
731 | ||
732 | #endif | |
733 | \f | |
734 | /* Summary printing support. */ | |
735 | ||
736 | #if WITH_PROFILE_INSN_P | |
737 | ||
738 | static SIM_RC | |
739 | profile_insn_init (SIM_DESC sd) | |
740 | { | |
741 | int c; | |
742 | ||
743 | for (c = 0; c < MAX_NR_PROCESSORS; ++c) | |
744 | { | |
745 | sim_cpu *cpu = STATE_CPU (sd, c); | |
746 | ||
747 | if (CPU_MAX_INSNS (cpu) > 0) | |
748 | PROFILE_INSN_COUNT (CPU_PROFILE_DATA (cpu)) = NZALLOC (unsigned int, CPU_MAX_INSNS (cpu)); | |
749 | } | |
750 | ||
751 | return SIM_RC_OK; | |
752 | } | |
753 | ||
754 | static void | |
755 | profile_print_insn (sim_cpu *cpu, int verbose) | |
756 | { | |
757 | unsigned int i, n, total, max_val, max_name_len; | |
758 | SIM_DESC sd = CPU_STATE (cpu); | |
759 | PROFILE_DATA *data = CPU_PROFILE_DATA (cpu); | |
760 | char comma_buf[20]; | |
761 | ||
762 | /* If MAX_INSNS not set, insn profiling isn't supported. */ | |
763 | if (CPU_MAX_INSNS (cpu) == 0) | |
764 | return; | |
765 | ||
0d3d2c71 | 766 | profile_printf (sd, cpu, "Instruction Statistics"); |
c906108c SS |
767 | #ifdef SIM_HAVE_ADDR_RANGE |
768 | if (PROFILE_RANGE (data)->ranges) | |
0d3d2c71 | 769 | profile_printf (sd, cpu, " (for selected address range(s))"); |
c906108c | 770 | #endif |
0d3d2c71 | 771 | profile_printf (sd, cpu, "\n\n"); |
c906108c SS |
772 | |
773 | /* First pass over data computes various things. */ | |
774 | max_val = 0; | |
775 | total = 0; | |
776 | max_name_len = 0; | |
777 | for (i = 0; i < CPU_MAX_INSNS (cpu); ++i) | |
778 | { | |
779 | const char *name = (*CPU_INSN_NAME (cpu)) (cpu, i); | |
780 | ||
781 | if (name == NULL) | |
782 | continue; | |
783 | total += PROFILE_INSN_COUNT (data) [i]; | |
784 | if (PROFILE_INSN_COUNT (data) [i] > max_val) | |
785 | max_val = PROFILE_INSN_COUNT (data) [i]; | |
786 | n = strlen (name); | |
787 | if (n > max_name_len) | |
788 | max_name_len = n; | |
789 | } | |
790 | /* set the total insn count, in case client is being lazy */ | |
791 | if (! PROFILE_TOTAL_INSN_COUNT (data)) | |
792 | PROFILE_TOTAL_INSN_COUNT (data) = total; | |
793 | ||
0d3d2c71 | 794 | profile_printf (sd, cpu, " Total: %s insns\n", COMMAS (total)); |
c906108c SS |
795 | |
796 | if (verbose && max_val != 0) | |
797 | { | |
798 | /* Now we can print the histogram. */ | |
0d3d2c71 | 799 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
800 | for (i = 0; i < CPU_MAX_INSNS (cpu); ++i) |
801 | { | |
802 | const char *name = (*CPU_INSN_NAME (cpu)) (cpu, i); | |
803 | ||
804 | if (name == NULL) | |
805 | continue; | |
806 | if (PROFILE_INSN_COUNT (data) [i] != 0) | |
807 | { | |
0d3d2c71 MF |
808 | profile_printf (sd, cpu, " %*s: %*s: ", |
809 | max_name_len, name, | |
810 | max_val < 10000 ? 5 : 10, | |
811 | COMMAS (PROFILE_INSN_COUNT (data) [i])); | |
812 | sim_profile_print_bar (sd, cpu, PROFILE_HISTOGRAM_WIDTH, | |
c906108c SS |
813 | PROFILE_INSN_COUNT (data) [i], |
814 | max_val); | |
0d3d2c71 | 815 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
816 | } |
817 | } | |
818 | } | |
819 | ||
0d3d2c71 | 820 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
821 | } |
822 | ||
823 | #endif | |
824 | ||
825 | #if WITH_PROFILE_MEMORY_P | |
826 | ||
827 | static void | |
828 | profile_print_memory (sim_cpu *cpu, int verbose) | |
829 | { | |
830 | unsigned int i, n; | |
831 | unsigned int total_read, total_write; | |
832 | unsigned int max_val, max_name_len; | |
833 | /* FIXME: Need to add smp support. */ | |
834 | SIM_DESC sd = CPU_STATE (cpu); | |
835 | PROFILE_DATA *data = CPU_PROFILE_DATA (cpu); | |
836 | char comma_buf[20]; | |
837 | ||
0d3d2c71 | 838 | profile_printf (sd, cpu, "Memory Access Statistics\n\n"); |
c906108c SS |
839 | |
840 | /* First pass over data computes various things. */ | |
841 | max_val = total_read = total_write = max_name_len = 0; | |
842 | for (i = 0; i < MODE_TARGET_MAX; ++i) | |
843 | { | |
844 | total_read += PROFILE_READ_COUNT (data) [i]; | |
845 | total_write += PROFILE_WRITE_COUNT (data) [i]; | |
846 | if (PROFILE_READ_COUNT (data) [i] > max_val) | |
847 | max_val = PROFILE_READ_COUNT (data) [i]; | |
848 | if (PROFILE_WRITE_COUNT (data) [i] > max_val) | |
849 | max_val = PROFILE_WRITE_COUNT (data) [i]; | |
850 | n = strlen (MODE_NAME (i)); | |
851 | if (n > max_name_len) | |
852 | max_name_len = n; | |
853 | } | |
854 | ||
855 | /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */ | |
0d3d2c71 MF |
856 | profile_printf (sd, cpu, " Total read: %s accesses\n", |
857 | COMMAS (total_read)); | |
858 | profile_printf (sd, cpu, " Total write: %s accesses\n", | |
859 | COMMAS (total_write)); | |
c906108c SS |
860 | |
861 | if (verbose && max_val != 0) | |
862 | { | |
863 | /* FIXME: Need to separate instruction fetches from data fetches | |
864 | as the former swamps the latter. */ | |
865 | /* Now we can print the histogram. */ | |
0d3d2c71 | 866 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
867 | for (i = 0; i < MODE_TARGET_MAX; ++i) |
868 | { | |
869 | if (PROFILE_READ_COUNT (data) [i] != 0) | |
870 | { | |
0d3d2c71 MF |
871 | profile_printf (sd, cpu, " %*s read: %*s: ", |
872 | max_name_len, MODE_NAME (i), | |
873 | max_val < 10000 ? 5 : 10, | |
874 | COMMAS (PROFILE_READ_COUNT (data) [i])); | |
875 | sim_profile_print_bar (sd, cpu, PROFILE_HISTOGRAM_WIDTH, | |
c906108c SS |
876 | PROFILE_READ_COUNT (data) [i], |
877 | max_val); | |
0d3d2c71 | 878 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
879 | } |
880 | if (PROFILE_WRITE_COUNT (data) [i] != 0) | |
881 | { | |
0d3d2c71 MF |
882 | profile_printf (sd, cpu, " %*s write: %*s: ", |
883 | max_name_len, MODE_NAME (i), | |
884 | max_val < 10000 ? 5 : 10, | |
885 | COMMAS (PROFILE_WRITE_COUNT (data) [i])); | |
886 | sim_profile_print_bar (sd, cpu, PROFILE_HISTOGRAM_WIDTH, | |
c906108c SS |
887 | PROFILE_WRITE_COUNT (data) [i], |
888 | max_val); | |
0d3d2c71 | 889 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
890 | } |
891 | } | |
892 | } | |
893 | ||
0d3d2c71 | 894 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
895 | } |
896 | ||
897 | #endif | |
898 | ||
899 | #if WITH_PROFILE_CORE_P | |
900 | ||
901 | static void | |
902 | profile_print_core (sim_cpu *cpu, int verbose) | |
903 | { | |
904 | unsigned int total; | |
905 | unsigned int max_val; | |
906 | /* FIXME: Need to add smp support. */ | |
907 | SIM_DESC sd = CPU_STATE (cpu); | |
908 | PROFILE_DATA *data = CPU_PROFILE_DATA (cpu); | |
909 | char comma_buf[20]; | |
910 | ||
0d3d2c71 | 911 | profile_printf (sd, cpu, "CORE Statistics\n\n"); |
c906108c SS |
912 | |
913 | /* First pass over data computes various things. */ | |
914 | { | |
915 | unsigned map; | |
916 | total = 0; | |
917 | max_val = 0; | |
918 | for (map = 0; map < nr_maps; map++) | |
919 | { | |
920 | total += PROFILE_CORE_COUNT (data) [map]; | |
921 | if (PROFILE_CORE_COUNT (data) [map] > max_val) | |
922 | max_val = PROFILE_CORE_COUNT (data) [map]; | |
923 | } | |
924 | } | |
925 | ||
926 | /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */ | |
0d3d2c71 MF |
927 | profile_printf (sd, cpu, " Total: %s accesses\n", |
928 | COMMAS (total)); | |
c906108c SS |
929 | |
930 | if (verbose && max_val != 0) | |
931 | { | |
932 | unsigned map; | |
933 | /* Now we can print the histogram. */ | |
0d3d2c71 | 934 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
935 | for (map = 0; map < nr_maps; map++) |
936 | { | |
937 | if (PROFILE_CORE_COUNT (data) [map] != 0) | |
938 | { | |
0d3d2c71 MF |
939 | profile_printf (sd, cpu, "%10s:", map_to_str (map)); |
940 | profile_printf (sd, cpu, "%*s: ", | |
941 | max_val < 10000 ? 5 : 10, | |
942 | COMMAS (PROFILE_CORE_COUNT (data) [map])); | |
943 | sim_profile_print_bar (sd, cpu, PROFILE_HISTOGRAM_WIDTH, | |
c906108c SS |
944 | PROFILE_CORE_COUNT (data) [map], |
945 | max_val); | |
0d3d2c71 | 946 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
947 | } |
948 | } | |
949 | } | |
950 | ||
0d3d2c71 | 951 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
952 | } |
953 | ||
954 | #endif | |
955 | ||
956 | #if WITH_PROFILE_MODEL_P | |
957 | ||
958 | static void | |
959 | profile_print_model (sim_cpu *cpu, int verbose) | |
960 | { | |
961 | SIM_DESC sd = CPU_STATE (cpu); | |
962 | PROFILE_DATA *data = CPU_PROFILE_DATA (cpu); | |
963 | unsigned long cti_stall_cycles = PROFILE_MODEL_CTI_STALL_CYCLES (data); | |
964 | unsigned long load_stall_cycles = PROFILE_MODEL_LOAD_STALL_CYCLES (data); | |
965 | unsigned long total_cycles = PROFILE_MODEL_TOTAL_CYCLES (data); | |
966 | char comma_buf[20]; | |
967 | ||
0d3d2c71 MF |
968 | profile_printf (sd, cpu, "Model %s Timing Information", |
969 | MODEL_NAME (CPU_MODEL (cpu))); | |
c906108c SS |
970 | #ifdef SIM_HAVE_ADDR_RANGE |
971 | if (PROFILE_RANGE (data)->ranges) | |
0d3d2c71 | 972 | profile_printf (sd, cpu, " (for selected address range(s))"); |
c906108c | 973 | #endif |
0d3d2c71 MF |
974 | profile_printf (sd, cpu, "\n\n"); |
975 | profile_printf (sd, cpu, " %-*s %s\n", | |
976 | PROFILE_LABEL_WIDTH, "Taken branches:", | |
977 | COMMAS (PROFILE_MODEL_TAKEN_COUNT (data))); | |
978 | profile_printf (sd, cpu, " %-*s %s\n", | |
979 | PROFILE_LABEL_WIDTH, "Untaken branches:", | |
980 | COMMAS (PROFILE_MODEL_UNTAKEN_COUNT (data))); | |
981 | profile_printf (sd, cpu, " %-*s %s\n", | |
982 | PROFILE_LABEL_WIDTH, "Cycles stalled due to branches:", | |
983 | COMMAS (cti_stall_cycles)); | |
984 | profile_printf (sd, cpu, " %-*s %s\n", | |
985 | PROFILE_LABEL_WIDTH, "Cycles stalled due to loads:", | |
986 | COMMAS (load_stall_cycles)); | |
987 | profile_printf (sd, cpu, " %-*s %s\n", | |
988 | PROFILE_LABEL_WIDTH, "Total cycles (*approximate*):", | |
989 | COMMAS (total_cycles)); | |
990 | profile_printf (sd, cpu, "\n"); | |
c906108c SS |
991 | } |
992 | ||
993 | #endif | |
994 | ||
995 | void | |
0d3d2c71 | 996 | sim_profile_print_bar (SIM_DESC sd, sim_cpu *cpu, unsigned int width, |
c906108c SS |
997 | unsigned int val, unsigned int max_val) |
998 | { | |
999 | unsigned int i, count; | |
1000 | ||
1001 | count = ((double) val / (double) max_val) * (double) width; | |
1002 | ||
1003 | for (i = 0; i < count; ++i) | |
0d3d2c71 | 1004 | profile_printf (sd, cpu, "*"); |
c906108c SS |
1005 | } |
1006 | ||
1007 | /* Print the simulator's execution speed for CPU. */ | |
1008 | ||
1009 | static void | |
1010 | profile_print_speed (sim_cpu *cpu) | |
1011 | { | |
1012 | SIM_DESC sd = CPU_STATE (cpu); | |
1013 | PROFILE_DATA *data = CPU_PROFILE_DATA (cpu); | |
1014 | unsigned long milliseconds = sim_events_elapsed_time (sd); | |
1015 | unsigned long total = PROFILE_TOTAL_INSN_COUNT (data); | |
09032128 DB |
1016 | double clock; |
1017 | double secs; | |
c906108c SS |
1018 | char comma_buf[20]; |
1019 | ||
0d3d2c71 | 1020 | profile_printf (sd, cpu, "Simulator Execution Speed\n\n"); |
c906108c SS |
1021 | |
1022 | if (total != 0) | |
0d3d2c71 | 1023 | profile_printf (sd, cpu, " Total instructions: %s\n", COMMAS (total)); |
c906108c SS |
1024 | |
1025 | if (milliseconds < 1000) | |
0d3d2c71 | 1026 | profile_printf (sd, cpu, " Total execution time: < 1 second\n\n"); |
c906108c SS |
1027 | else |
1028 | { | |
1029 | /* The printing of the time rounded to 2 decimal places makes the speed | |
1030 | calculation seem incorrect [even though it is correct]. So round | |
1031 | MILLISECONDS first. This can marginally affect the result, but it's | |
1032 | better that the user not perceive there's a math error. */ | |
09032128 | 1033 | secs = (double) milliseconds / 1000; |
c906108c | 1034 | secs = ((double) (unsigned long) (secs * 100 + .5)) / 100; |
0d3d2c71 | 1035 | profile_printf (sd, cpu, " Total execution time : %.2f seconds\n", secs); |
c906108c SS |
1036 | /* Don't confuse things with data that isn't useful. |
1037 | If we ran for less than 2 seconds, only use the data if we | |
1038 | executed more than 100,000 insns. */ | |
1039 | if (secs >= 2 || total >= 100000) | |
0d3d2c71 MF |
1040 | profile_printf (sd, cpu, " Simulator speed: %s insns/second\n", |
1041 | COMMAS ((unsigned long) ((double) total / secs))); | |
c906108c | 1042 | } |
09032128 | 1043 | |
09032128 DB |
1044 | /* Print simulated execution time if the cpu frequency has been specified. */ |
1045 | clock = PROFILE_CPU_FREQ (data); | |
1046 | if (clock != 0) | |
1047 | { | |
1048 | if (clock >= 1000000) | |
0d3d2c71 MF |
1049 | profile_printf (sd, cpu, " Simulated cpu frequency: %.2f MHz\n", |
1050 | clock / 1000000); | |
09032128 | 1051 | else |
0d3d2c71 | 1052 | profile_printf (sd, cpu, " Simulated cpu frequency: %.2f Hz\n", clock); |
09032128 | 1053 | |
80dbae7a | 1054 | #if WITH_PROFILE_MODEL_P |
09032128 DB |
1055 | if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX]) |
1056 | { | |
1057 | /* The printing of the time rounded to 2 decimal places makes the | |
1058 | speed calculation seem incorrect [even though it is correct]. | |
1059 | So round SECS first. This can marginally affect the result, | |
1060 | but it's better that the user not perceive there's a math | |
1061 | error. */ | |
1062 | secs = PROFILE_MODEL_TOTAL_CYCLES (data) / clock; | |
1063 | secs = ((double) (unsigned long) (secs * 100 + .5)) / 100; | |
0d3d2c71 MF |
1064 | profile_printf (sd, cpu, " Simulated execution time: %.2f seconds\n", |
1065 | secs); | |
09032128 | 1066 | } |
09032128 | 1067 | #endif /* WITH_PROFILE_MODEL_P */ |
80dbae7a | 1068 | } |
c906108c SS |
1069 | } |
1070 | ||
739dfd28 | 1071 | #ifdef SIM_HAVE_ADDR_RANGE |
c906108c SS |
1072 | /* Print selected address ranges. */ |
1073 | ||
1074 | static void | |
1075 | profile_print_addr_ranges (sim_cpu *cpu) | |
1076 | { | |
1077 | ADDR_SUBRANGE *asr = PROFILE_RANGE (CPU_PROFILE_DATA (cpu))->ranges; | |
1078 | SIM_DESC sd = CPU_STATE (cpu); | |
1079 | ||
1080 | if (asr) | |
1081 | { | |
0d3d2c71 | 1082 | profile_printf (sd, cpu, "Selected address ranges\n\n"); |
c906108c SS |
1083 | while (asr != NULL) |
1084 | { | |
0d3d2c71 MF |
1085 | profile_printf (sd, cpu, " 0x%lx - 0x%lx\n", |
1086 | (long) asr->start, (long) asr->end); | |
c906108c SS |
1087 | asr = asr->next; |
1088 | } | |
0d3d2c71 | 1089 | profile_printf (sd, cpu, "\n"); |
c906108c SS |
1090 | } |
1091 | } | |
739dfd28 | 1092 | #endif |
c906108c SS |
1093 | |
1094 | /* Top level function to print all summary profile information. | |
1095 | It is [currently] intended that all such data is printed by this function. | |
1096 | I'd rather keep it all in one place for now. To that end, MISC_CPU and | |
1097 | MISC are callbacks used to print any miscellaneous data. | |
1098 | ||
1099 | One might want to add a user option that allows printing by type or by cpu | |
1100 | (i.e. print all insn data for each cpu first, or print data cpu by cpu). | |
1101 | This may be a case of featuritis so it's currently left out. | |
1102 | ||
1103 | Note that results are indented two spaces to distinguish them from | |
1104 | section titles. */ | |
1105 | ||
1106 | static void | |
1107 | profile_info (SIM_DESC sd, int verbose) | |
1108 | { | |
1109 | int i,c; | |
1110 | int print_title_p = 0; | |
1111 | ||
1112 | /* Only print the title if some data has been collected. */ | |
1113 | /* ??? Why don't we just exit if no data collected? */ | |
1114 | /* FIXME: If the number of processors can be selected on the command line, | |
1115 | then MAX_NR_PROCESSORS will need to take an argument of `sd'. */ | |
1116 | ||
f1a81b37 | 1117 | for (c = 0; c < MAX_NR_PROCESSORS && !print_title_p; ++c) |
c906108c SS |
1118 | { |
1119 | sim_cpu *cpu = STATE_CPU (sd, c); | |
1120 | PROFILE_DATA *data = CPU_PROFILE_DATA (cpu); | |
1121 | ||
1122 | for (i = 0; i < MAX_PROFILE_VALUES; ++i) | |
1123 | if (PROFILE_FLAGS (data) [i]) | |
0d3d2c71 MF |
1124 | { |
1125 | profile_printf (sd, cpu, "Summary profiling results:\n\n"); | |
1126 | print_title_p = 1; | |
f1a81b37 | 1127 | break; |
0d3d2c71 | 1128 | } |
c906108c | 1129 | } |
c906108c SS |
1130 | |
1131 | /* Loop, cpu by cpu, printing results. */ | |
1132 | ||
1133 | for (c = 0; c < MAX_NR_PROCESSORS; ++c) | |
1134 | { | |
1135 | sim_cpu *cpu = STATE_CPU (sd, c); | |
1136 | PROFILE_DATA *data = CPU_PROFILE_DATA (cpu); | |
1137 | ||
1138 | if (MAX_NR_PROCESSORS > 1 | |
1139 | && (0 | |
1140 | #if WITH_PROFILE_INSN_P | |
1141 | || PROFILE_FLAGS (data) [PROFILE_INSN_IDX] | |
1142 | #endif | |
1143 | #if WITH_PROFILE_MEMORY_P | |
1144 | || PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX] | |
1145 | #endif | |
1146 | #if WITH_PROFILE_CORE_P | |
1147 | || PROFILE_FLAGS (data) [PROFILE_CORE_IDX] | |
1148 | #endif | |
1149 | #if WITH_PROFILE_MODEL_P | |
1150 | || PROFILE_FLAGS (data) [PROFILE_MODEL_IDX] | |
1151 | #endif | |
1152 | #if WITH_PROFILE_SCACHE_P && WITH_SCACHE | |
1153 | || PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX] | |
1154 | #endif | |
1155 | #if WITH_PROFILE_PC_P | |
1156 | || PROFILE_FLAGS (data) [PROFILE_PC_IDX] | |
1157 | #endif | |
1158 | )) | |
1159 | { | |
0d3d2c71 | 1160 | profile_printf (sd, cpu, "CPU %d\n\n", c); |
c906108c SS |
1161 | } |
1162 | ||
1163 | #ifdef SIM_HAVE_ADDR_RANGE | |
1164 | if (print_title_p | |
1165 | && (PROFILE_INSN_P (cpu) | |
1166 | || PROFILE_MODEL_P (cpu))) | |
1167 | profile_print_addr_ranges (cpu); | |
1168 | #endif | |
1169 | ||
1170 | #if WITH_PROFILE_INSN_P | |
1171 | if (PROFILE_FLAGS (data) [PROFILE_INSN_IDX]) | |
1172 | profile_print_insn (cpu, verbose); | |
1173 | #endif | |
1174 | ||
1175 | #if WITH_PROFILE_MEMORY_P | |
1176 | if (PROFILE_FLAGS (data) [PROFILE_MEMORY_IDX]) | |
1177 | profile_print_memory (cpu, verbose); | |
1178 | #endif | |
1179 | ||
1180 | #if WITH_PROFILE_CORE_P | |
1181 | if (PROFILE_FLAGS (data) [PROFILE_CORE_IDX]) | |
1182 | profile_print_core (cpu, verbose); | |
1183 | #endif | |
1184 | ||
1185 | #if WITH_PROFILE_MODEL_P | |
1186 | if (PROFILE_FLAGS (data) [PROFILE_MODEL_IDX]) | |
1187 | profile_print_model (cpu, verbose); | |
1188 | #endif | |
1189 | ||
1190 | #if WITH_PROFILE_SCACHE_P && WITH_SCACHE | |
1191 | if (PROFILE_FLAGS (data) [PROFILE_SCACHE_IDX]) | |
1192 | scache_print_profile (cpu, verbose); | |
1193 | #endif | |
1194 | ||
1195 | #if WITH_PROFILE_PC_P | |
1196 | if (PROFILE_FLAGS (data) [PROFILE_PC_IDX]) | |
1197 | profile_print_pc (cpu, verbose); | |
1198 | #endif | |
1199 | ||
1200 | /* Print cpu-specific data before the execution speed. */ | |
1201 | if (PROFILE_INFO_CPU_CALLBACK (data) != NULL) | |
1202 | PROFILE_INFO_CPU_CALLBACK (data) (cpu, verbose); | |
1203 | ||
1204 | /* Always try to print execution time and speed. */ | |
1205 | if (verbose | |
1206 | || PROFILE_FLAGS (data) [PROFILE_INSN_IDX]) | |
1207 | profile_print_speed (cpu); | |
1208 | } | |
1209 | ||
1210 | /* Finally print non-cpu specific miscellaneous data. */ | |
1211 | if (STATE_PROFILE_INFO_CALLBACK (sd)) | |
1212 | STATE_PROFILE_INFO_CALLBACK (sd) (sd, verbose); | |
1213 | ||
1214 | } | |
1215 | \f | |
1216 | /* Install profiling support in the simulator. */ | |
1217 | ||
1218 | SIM_RC | |
1219 | profile_install (SIM_DESC sd) | |
1220 | { | |
1221 | int i; | |
1222 | ||
1223 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
1224 | sim_add_option_table (sd, NULL, profile_options); | |
1225 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
1226 | memset (CPU_PROFILE_DATA (STATE_CPU (sd, i)), 0, | |
1227 | sizeof (* CPU_PROFILE_DATA (STATE_CPU (sd, i)))); | |
1228 | #if WITH_PROFILE_INSN_P | |
1229 | sim_module_add_init_fn (sd, profile_insn_init); | |
1230 | #endif | |
1231 | #if WITH_PROFILE_PC_P | |
1232 | sim_module_add_uninstall_fn (sd, profile_pc_uninstall); | |
1233 | sim_module_add_init_fn (sd, profile_pc_init); | |
1234 | #endif | |
1235 | sim_module_add_init_fn (sd, profile_init); | |
1236 | sim_module_add_uninstall_fn (sd, profile_uninstall); | |
1237 | sim_module_add_info_fn (sd, profile_info); | |
1238 | return SIM_RC_OK; | |
1239 | } | |
1240 | ||
1241 | static SIM_RC | |
1242 | profile_init (SIM_DESC sd) | |
1243 | { | |
1244 | #ifdef SIM_HAVE_ADDR_RANGE | |
1245 | /* Check if a range has been specified without specifying what to | |
1246 | collect. */ | |
1247 | { | |
1248 | int i; | |
1249 | ||
1250 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
1251 | { | |
1252 | sim_cpu *cpu = STATE_CPU (sd, i); | |
1253 | ||
1254 | if (ADDR_RANGE_RANGES (PROFILE_RANGE (CPU_PROFILE_DATA (cpu))) | |
1255 | && ! (PROFILE_INSN_P (cpu) | |
1256 | || PROFILE_MODEL_P (cpu))) | |
1257 | { | |
1258 | sim_io_eprintf_cpu (cpu, "Profiling address range specified without --profile-insn or --profile-model.\n"); | |
1259 | sim_io_eprintf_cpu (cpu, "Address range ignored.\n"); | |
1260 | sim_addr_range_delete (PROFILE_RANGE (CPU_PROFILE_DATA (cpu)), | |
1261 | 0, ~ (address_word) 0); | |
1262 | } | |
1263 | } | |
1264 | } | |
1265 | #endif | |
1266 | ||
1267 | return SIM_RC_OK; | |
1268 | } | |
1269 | ||
1270 | static void | |
1271 | profile_uninstall (SIM_DESC sd) | |
1272 | { | |
1273 | int i,j; | |
1274 | ||
1275 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
1276 | { | |
1277 | sim_cpu *cpu = STATE_CPU (sd, i); | |
1278 | PROFILE_DATA *data = CPU_PROFILE_DATA (cpu); | |
1279 | ||
1280 | if (PROFILE_FILE (data) != NULL) | |
1281 | { | |
1282 | /* If output from different cpus is going to the same file, | |
1283 | avoid closing the file twice. */ | |
1284 | for (j = 0; j < i; ++j) | |
1285 | if (PROFILE_FILE (CPU_PROFILE_DATA (STATE_CPU (sd, j))) | |
1286 | == PROFILE_FILE (data)) | |
1287 | break; | |
1288 | if (i == j) | |
1289 | fclose (PROFILE_FILE (data)); | |
1290 | } | |
1291 | ||
1292 | if (PROFILE_INSN_COUNT (data) != NULL) | |
d79fe0d6 | 1293 | free (PROFILE_INSN_COUNT (data)); |
c906108c SS |
1294 | } |
1295 | } |