* gx prototype: simulator I/O bug fix
[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_SYS_TYPES_H
51 #include <sys/types.h>
52 #endif
53
54 #ifdef HAVE_TIME_H
55 #include <time.h>
56 #endif
57
58 #ifdef HAVE_SYS_TIMES_H
59 #include <sys/times.h>
60 #endif
61
62 #ifdef HAVE_SYS_TIME_H
63 #include <sys/time.h>
64 #endif
65
66 #ifdef HAVE_SYS_RESOURCE_H
67 #include <sys/resource.h>
68 int getrusage();
69 #endif
70
71 struct _cpu_mon {
72 count_type issue_count[nr_itable_entries];
73 count_type read_count;
74 count_type write_count;
75 count_type unaligned_read_count;
76 count_type unaligned_write_count;
77 count_type event_count[nr_mon_events];
78 };
79
80 struct _mon {
81 int nr_cpus;
82 cpu_mon cpu_monitor[MAX_NR_PROCESSORS];
83 };
84
85
86 INLINE_MON mon *
87 mon_create(void)
88 {
89 mon *monitor = ZALLOC(mon);
90 return monitor;
91 }
92
93
94 INLINE_MON cpu_mon *
95 mon_cpu(mon *monitor,
96 int cpu_nr)
97 {
98 if (cpu_nr < 0 || cpu_nr >= MAX_NR_PROCESSORS)
99 error("mon_cpu() - invalid cpu number\n");
100 return &monitor->cpu_monitor[cpu_nr];
101 }
102
103
104 INLINE_MON void
105 mon_init(mon *monitor,
106 int nr_cpus)
107 {
108 memset(monitor, 0, sizeof(*monitor));
109 monitor->nr_cpus = nr_cpus;
110 }
111
112
113 INLINE_MON void
114 mon_issue(itable_index index,
115 cpu *processor,
116 unsigned_word cia)
117 {
118 cpu_mon *monitor = cpu_monitor(processor);
119 ASSERT(index <= nr_itable_entries);
120 monitor->issue_count[index] += 1;
121 }
122
123
124 INLINE_MON void
125 mon_read(unsigned_word ea,
126 unsigned_word ra,
127 unsigned nr_bytes,
128 cpu *processor,
129 unsigned_word cia)
130 {
131 cpu_mon *monitor = cpu_monitor(processor);
132 monitor->read_count += 1;
133 if ((nr_bytes - 1) & ea)
134 monitor->unaligned_read_count += 1;
135 }
136
137
138 INLINE_MON void
139 mon_write(unsigned_word ea,
140 unsigned_word ra,
141 unsigned nr_bytes,
142 cpu *processor,
143 unsigned_word cia)
144 {
145 cpu_mon *monitor = cpu_monitor(processor);
146 monitor->write_count += 1;
147 if ((nr_bytes - 1) & ea)
148 monitor->unaligned_write_count += 1;
149 }
150
151 INLINE_MON void
152 mon_event(mon_events event,
153 cpu *processor,
154 unsigned_word cia)
155 {
156 cpu_mon *monitor = cpu_monitor(processor);
157 ASSERT(event >= 0 && event < nr_mon_events);
158 monitor->event_count[event] += 1;
159 }
160
161 STATIC_INLINE_MON count_type
162 mon_get_number_of_insns(cpu_mon *monitor)
163 {
164 itable_index index;
165 count_type total_insns = 0;
166 for (index = 0; index < nr_itable_entries; index++)
167 total_insns += monitor->issue_count[index];
168 return total_insns;
169 }
170
171 static int
172 mon_sort_instruction_names(const void *ptr_a, const void *ptr_b)
173 {
174 itable_index a = *(const itable_index *)ptr_a;
175 itable_index b = *(const itable_index *)ptr_b;
176
177 return strcmp (itable[a].name, itable[b].name);
178 }
179
180 STATIC_INLINE_MON char *
181 mon_add_commas(char *buf,
182 int sizeof_buf,
183 count_type value)
184 {
185 int comma = 3;
186 char *endbuf = buf + sizeof_buf - 1;
187
188 *--endbuf = '\0';
189 do {
190 if (comma-- == 0)
191 {
192 *--endbuf = ',';
193 comma = 2;
194 }
195
196 *--endbuf = (value % 10) + '0';
197 } while ((value /= 10) != 0);
198
199 ASSERT(endbuf >= buf);
200 return endbuf;
201 }
202
203
204 INLINE_MON void
205 mon_print_info(psim *system,
206 mon *monitor,
207 int verbose)
208 {
209 char buffer[20];
210 int cpu_nr;
211 int len_cpu;
212 int len_num = 0;
213 int len;
214 long total_insns = 0;
215 long cpu_insns_second = 0;
216 double cpu_time = 0.0;
217
218 for (cpu_nr = 0; cpu_nr < monitor->nr_cpus; cpu_nr++) {
219 count_type num_insns = mon_get_number_of_insns(&monitor->cpu_monitor[cpu_nr]);
220
221 total_insns += num_insns;
222 len = strlen (mon_add_commas(buffer, sizeof(buffer), num_insns));
223 if (len_num < len)
224 len_num = len;
225 }
226
227 sprintf (buffer, "%d", (int)monitor->nr_cpus + 1);
228 len_cpu = strlen (buffer);
229
230 #ifdef HAVE_GETRUSAGE
231 if (total_insns && verbose)
232 {
233 struct rusage mytime;
234 if (getrusage (RUSAGE_SELF, &mytime) == 0
235 && (mytime.ru_utime.tv_sec > 0 || mytime.ru_utime.tv_usec > 0)) {
236
237 cpu_time = (double)mytime.ru_utime.tv_sec + (((double)mytime.ru_utime.tv_usec) / 1000000.0);
238 cpu_insns_second = (long)(((double)total_insns / cpu_time) + 0.5);
239 }
240 }
241 #endif
242
243 for (cpu_nr = 0; cpu_nr < monitor->nr_cpus; cpu_nr++) {
244
245 if (verbose > 1) {
246 itable_index sort_insns[nr_itable_entries];
247 int nr_sort_insns = 0;
248 itable_index index;
249 int index2;
250
251 if (cpu_nr)
252 printf_filtered ("\n");
253
254 for (index = 0; index < nr_itable_entries; index++) {
255 if (monitor->cpu_monitor[cpu_nr].issue_count[index]) {
256 sort_insns[nr_sort_insns++] = index;
257 }
258 }
259
260 qsort((void *)sort_insns, nr_sort_insns, sizeof(sort_insns[0]), mon_sort_instruction_names);
261
262 for (index2 = 0; index2 < nr_sort_insns; index2++) {
263 index = sort_insns[index2];
264 printf_filtered("CPU #%*d executed %*s %s instruction%s.\n",
265 len_cpu, cpu_nr+1,
266 len_num, mon_add_commas(buffer,
267 sizeof(buffer),
268 monitor->cpu_monitor[cpu_nr].issue_count[index]),
269 itable[index].name,
270 (monitor->cpu_monitor[cpu_nr].issue_count[index] == 1) ? "" : "s");
271 }
272
273 printf_filtered ("\n");
274 }
275
276 if (CURRENT_MODEL_ISSUE > 0)
277 {
278 model_data *model_ptr = cpu_model(psim_cpu(system, cpu_nr));
279 model_print *ptr = model_mon_info(model_ptr);
280 model_print *orig_ptr = ptr;
281
282 while (ptr) {
283 if (ptr->count)
284 printf_filtered("CPU #%*d executed %*s %s%s.\n",
285 len_cpu, cpu_nr+1,
286 len_num, mon_add_commas(buffer,
287 sizeof(buffer),
288 ptr->count),
289 ptr->name,
290 ((ptr->count == 1)
291 ? ptr->suffix_singular
292 : ptr->suffix_plural));
293
294 ptr = ptr->next;
295 }
296
297 model_mon_info_free(model_ptr, orig_ptr);
298 }
299
300 if (monitor->cpu_monitor[cpu_nr].read_count)
301 printf_filtered ("CPU #%*d executed %*s data read%s.\n",
302 len_cpu, cpu_nr+1,
303 len_num, mon_add_commas(buffer,
304 sizeof(buffer),
305 monitor->cpu_monitor[cpu_nr].read_count),
306 (monitor->cpu_monitor[cpu_nr].read_count == 1) ? "" : "s");
307
308 if (monitor->cpu_monitor[cpu_nr].write_count)
309 printf_filtered ("CPU #%*d executed %*s data write%s.\n",
310 len_cpu, cpu_nr+1,
311 len_num, mon_add_commas(buffer,
312 sizeof(buffer),
313 monitor->cpu_monitor[cpu_nr].write_count),
314 (monitor->cpu_monitor[cpu_nr].write_count == 1) ? "" : "s");
315
316 if (monitor->cpu_monitor[cpu_nr].unaligned_read_count)
317 printf_filtered ("CPU #%*d executed %*s unaligned data read%s.\n",
318 len_cpu, cpu_nr+1,
319 len_num, mon_add_commas(buffer,
320 sizeof(buffer),
321 monitor->cpu_monitor[cpu_nr].read_count),
322 (monitor->cpu_monitor[cpu_nr].read_count == 1) ? "" : "s");
323
324 if (monitor->cpu_monitor[cpu_nr].unaligned_write_count)
325 printf_filtered ("CPU #%*d executed %*s unaligned data write%s.\n",
326 len_cpu, cpu_nr+1,
327 len_num, mon_add_commas(buffer,
328 sizeof(buffer),
329 monitor->cpu_monitor[cpu_nr].write_count),
330 (monitor->cpu_monitor[cpu_nr].write_count == 1) ? "" : "s");
331
332 if (monitor->cpu_monitor[cpu_nr].event_count[mon_event_icache_miss])
333 printf_filtered ("CPU #%*d executed %*s icache miss%s.\n",
334 len_cpu, cpu_nr+1,
335 len_num, mon_add_commas(buffer,
336 sizeof(buffer),
337 monitor->cpu_monitor[cpu_nr].event_count[mon_event_icache_miss]),
338 (monitor->cpu_monitor[cpu_nr].event_count[mon_event_icache_miss] == 1) ? "" : "es");
339
340 printf_filtered("CPU #%*d executed %*s instructions in total.\n",
341 len_cpu, cpu_nr+1,
342 len_num, mon_add_commas(buffer,
343 sizeof(buffer),
344 mon_get_number_of_insns(&monitor->cpu_monitor[cpu_nr])));
345
346 }
347
348 if (monitor->nr_cpus > 1)
349 printf_filtered("\nAll CPUs executed %s instructions in total.\n",
350 mon_add_commas(buffer, sizeof(buffer), total_insns));
351
352 if (cpu_insns_second)
353 printf_filtered ("%sSimulator speed was %s instructions/second\n",
354 (monitor->nr_cpus > 1) ? "" : "\n",
355 mon_add_commas(buffer, sizeof(buffer), cpu_insns_second));
356 }
357
358 #endif /* _MON_C_ */
This page took 0.035793 seconds and 4 git commands to generate.