Make WITH_MODEL_ISSUE==0 not core dump
[deliverable/binutils-gdb.git] / sim / ppc / mon.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20
21
22 #ifndef _MON_C_
23 #define _MON_C_
24
25 #ifndef STATIC_INLINE_MON
26 #define STATIC_INLINE_MON STATIC_INLINE
27 #endif
28
29 #include "basics.h"
30 #include "cpu.h"
31 #include "mon.h"
32 #include <stdio.h>
33
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #else
37 #ifdef HAVE_STRINGS_H
38 #include <strings.h>
39 #endif
40 #endif
41
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45
46 #ifdef HAVE_STDLIB_H
47 #include <stdlib.h>
48 #endif
49
50 #ifdef HAVE_TIME_H
51 #include <time.h>
52 #endif
53
54 #ifdef HAVE_SYS_TIMES_H
55 #include <sys/times.h>
56 #endif
57
58 #ifdef HAVE_SYS_RESOURCE_H
59 #include <sys/resource.h>
60 int getrusage();
61 #endif
62
63 struct _cpu_mon {
64 count_type issue_count[nr_itable_entries];
65 count_type read_count;
66 count_type write_count;
67 count_type unaligned_read_count;
68 count_type unaligned_write_count;
69 count_type event_count[nr_mon_events];
70 };
71
72 struct _mon {
73 int nr_cpus;
74 cpu_mon cpu_monitor[MAX_NR_PROCESSORS];
75 };
76
77
78 INLINE_MON mon *
79 mon_create(void)
80 {
81 mon *monitor = ZALLOC(mon);
82 return monitor;
83 }
84
85
86 INLINE_MON cpu_mon *
87 mon_cpu(mon *monitor,
88 int cpu_nr)
89 {
90 if (cpu_nr < 0 || cpu_nr >= MAX_NR_PROCESSORS)
91 error("mon_cpu() - invalid cpu number\n");
92 return &monitor->cpu_monitor[cpu_nr];
93 }
94
95
96 INLINE_MON void
97 mon_init(mon *monitor,
98 int nr_cpus)
99 {
100 memset(monitor, 0, sizeof(*monitor));
101 monitor->nr_cpus = nr_cpus;
102 }
103
104
105 INLINE_MON void
106 mon_issue(itable_index index,
107 cpu *processor,
108 unsigned_word cia)
109 {
110 cpu_mon *monitor = cpu_monitor(processor);
111 ASSERT(index <= nr_itable_entries);
112 monitor->issue_count[index] += 1;
113 }
114
115
116 INLINE_MON void
117 mon_read(unsigned_word ea,
118 unsigned_word ra,
119 unsigned nr_bytes,
120 cpu *processor,
121 unsigned_word cia)
122 {
123 cpu_mon *monitor = cpu_monitor(processor);
124 monitor->read_count += 1;
125 if ((nr_bytes - 1) & ea)
126 monitor->unaligned_read_count += 1;
127 }
128
129
130 INLINE_MON void
131 mon_write(unsigned_word ea,
132 unsigned_word ra,
133 unsigned nr_bytes,
134 cpu *processor,
135 unsigned_word cia)
136 {
137 cpu_mon *monitor = cpu_monitor(processor);
138 monitor->write_count += 1;
139 if ((nr_bytes - 1) & ea)
140 monitor->unaligned_write_count += 1;
141 }
142
143 INLINE_MON void
144 mon_event(mon_events event,
145 cpu *processor,
146 unsigned_word cia)
147 {
148 cpu_mon *monitor = cpu_monitor(processor);
149 ASSERT(event >= 0 && event < nr_mon_events);
150 monitor->event_count[event] += 1;
151 }
152
153 STATIC_INLINE_MON count_type
154 mon_get_number_of_insns(cpu_mon *monitor)
155 {
156 itable_index index;
157 count_type total_insns = 0;
158 for (index = 0; index < nr_itable_entries; index++)
159 total_insns += monitor->issue_count[index];
160 return total_insns;
161 }
162
163 static int
164 mon_sort_instruction_names(const void *ptr_a, const void *ptr_b)
165 {
166 itable_index a = *(const itable_index *)ptr_a;
167 itable_index b = *(const itable_index *)ptr_b;
168
169 return strcmp (itable[a].name, itable[b].name);
170 }
171
172 STATIC_INLINE_MON char *
173 mon_add_commas(char *buf,
174 int sizeof_buf,
175 count_type value)
176 {
177 int comma = 3;
178 char *endbuf = buf + sizeof_buf - 1;
179
180 *--endbuf = '\0';
181 do {
182 if (comma-- == 0)
183 {
184 *--endbuf = ',';
185 comma = 2;
186 }
187
188 *--endbuf = (value % 10) + '0';
189 } while ((value /= 10) != 0);
190
191 ASSERT(endbuf >= buf);
192 return endbuf;
193 }
194
195
196 INLINE_MON void
197 mon_print_info(psim *system,
198 mon *monitor,
199 int verbose)
200 {
201 char buffer[20];
202 int cpu_nr;
203 int len_cpu;
204 int len_num = 0;
205 int len;
206 long total_insns = 0;
207 long cpu_insns_second = 0;
208 double cpu_time = 0.0;
209
210 for (cpu_nr = 0; cpu_nr < monitor->nr_cpus; cpu_nr++) {
211 count_type num_insns = mon_get_number_of_insns(&monitor->cpu_monitor[cpu_nr]);
212
213 total_insns += num_insns;
214 len = strlen (mon_add_commas(buffer, sizeof(buffer), num_insns));
215 if (len_num < len)
216 len_num = len;
217 }
218
219 sprintf (buffer, "%d", (int)monitor->nr_cpus + 1);
220 len_cpu = strlen (buffer);
221
222 #ifdef HAVE_GETRUSAGE
223 if (total_insns && verbose > 1)
224 {
225 struct rusage mytime;
226 if (getrusage (RUSAGE_SELF, &mytime) == 0
227 && (mytime.ru_utime.tv_sec > 0 || mytime.ru_utime.tv_usec > 0)) {
228
229 cpu_time = (double)mytime.ru_utime.tv_sec + (((double)mytime.ru_utime.tv_usec) / 1000000.0);
230 cpu_insns_second = (long)(((double)total_insns / cpu_time) + 0.5);
231 }
232 }
233 #endif
234
235 for (cpu_nr = 0; cpu_nr < monitor->nr_cpus; cpu_nr++) {
236
237 if (verbose > 1) {
238 itable_index sort_insns[nr_itable_entries];
239 int nr_sort_insns = 0;
240 itable_index index;
241 int index2;
242
243 if (cpu_nr)
244 printf_filtered ("\n");
245
246 for (index = 0; index < nr_itable_entries; index++) {
247 if (monitor->cpu_monitor[cpu_nr].issue_count[index]) {
248 sort_insns[nr_sort_insns++] = index;
249 }
250 }
251
252 qsort((void *)sort_insns, nr_sort_insns, sizeof(sort_insns[0]), mon_sort_instruction_names);
253
254 for (index2 = 0; index2 < nr_sort_insns; index2++) {
255 index = sort_insns[index2];
256 printf_filtered("CPU #%*d executed %*s %s instruction%s.\n",
257 len_cpu, cpu_nr+1,
258 len_num, mon_add_commas(buffer,
259 sizeof(buffer),
260 monitor->cpu_monitor[cpu_nr].issue_count[index]),
261 itable[index].name,
262 (monitor->cpu_monitor[cpu_nr].issue_count[index] == 1) ? "" : "s");
263 }
264
265 printf_filtered ("\n");
266 }
267
268 if (WITH_MODEL_ISSUE)
269 {
270 model_data *model_ptr = cpu_model(psim_cpu(system, cpu_nr));
271 model_print *ptr = model_mon_info(model_ptr);
272 model_print *orig_ptr = ptr;
273
274 while (ptr) {
275 if (ptr->count)
276 printf_filtered("CPU #%*d executed %*s %s%s.\n",
277 len_cpu, cpu_nr+1,
278 len_num, mon_add_commas(buffer,
279 sizeof(buffer),
280 ptr->count),
281 ptr->name,
282 ((ptr->count == 1)
283 ? ptr->suffix_singular
284 : ptr->suffix_plural));
285
286 ptr = ptr->next;
287 }
288
289 model_mon_info_free(model_ptr, orig_ptr);
290 }
291
292 if (monitor->cpu_monitor[cpu_nr].read_count)
293 printf_filtered ("CPU #%*d executed %*s data read%s.\n",
294 len_cpu, cpu_nr+1,
295 len_num, mon_add_commas(buffer,
296 sizeof(buffer),
297 monitor->cpu_monitor[cpu_nr].read_count),
298 (monitor->cpu_monitor[cpu_nr].read_count == 1) ? "" : "s");
299
300 if (monitor->cpu_monitor[cpu_nr].write_count)
301 printf_filtered ("CPU #%*d executed %*s data write%s.\n",
302 len_cpu, cpu_nr+1,
303 len_num, mon_add_commas(buffer,
304 sizeof(buffer),
305 monitor->cpu_monitor[cpu_nr].write_count),
306 (monitor->cpu_monitor[cpu_nr].write_count == 1) ? "" : "s");
307
308 if (monitor->cpu_monitor[cpu_nr].unaligned_read_count)
309 printf_filtered ("CPU #%*d executed %*s unaligned data read%s.\n",
310 len_cpu, cpu_nr+1,
311 len_num, mon_add_commas(buffer,
312 sizeof(buffer),
313 monitor->cpu_monitor[cpu_nr].read_count),
314 (monitor->cpu_monitor[cpu_nr].read_count == 1) ? "" : "s");
315
316 if (monitor->cpu_monitor[cpu_nr].unaligned_write_count)
317 printf_filtered ("CPU #%*d executed %*s unaligned data write%s.\n",
318 len_cpu, cpu_nr+1,
319 len_num, mon_add_commas(buffer,
320 sizeof(buffer),
321 monitor->cpu_monitor[cpu_nr].write_count),
322 (monitor->cpu_monitor[cpu_nr].write_count == 1) ? "" : "s");
323
324 if (monitor->cpu_monitor[cpu_nr].event_count[mon_event_icache_miss])
325 printf_filtered ("CPU #%*d executed %*s icache miss%s.\n",
326 len_cpu, cpu_nr+1,
327 len_num, mon_add_commas(buffer,
328 sizeof(buffer),
329 monitor->cpu_monitor[cpu_nr].event_count[mon_event_icache_miss]),
330 (monitor->cpu_monitor[cpu_nr].event_count[mon_event_icache_miss] == 1) ? "" : "es");
331
332 printf_filtered("CPU #%*d executed %*s instructions in total.\n",
333 len_cpu, cpu_nr+1,
334 len_num, mon_add_commas(buffer,
335 sizeof(buffer),
336 mon_get_number_of_insns(&monitor->cpu_monitor[cpu_nr])));
337
338 }
339
340 if (monitor->nr_cpus > 1)
341 printf_filtered("\nAll CPUs executed %s instructions in total.\n",
342 mon_add_commas(buffer, sizeof(buffer), total_insns));
343
344 if (cpu_insns_second)
345 printf_filtered ("%sSimulator speed was %s instructions/second\n",
346 (monitor->nr_cpus <= 1 && verbose <= 1) ? "" : "\n",
347 mon_add_commas(buffer, sizeof(buffer), cpu_insns_second));
348 }
349
350 #endif /* _MON_C_ */
This page took 0.036068 seconds and 5 git commands to generate.