Part II of adding callback argument to sim_open(). Update all the
[deliverable/binutils-gdb.git] / sim / ppc / mon.c
CommitLineData
a983c8f0
MM
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
a983c8f0
MM
29#include "basics.h"
30#include "cpu.h"
31#include "mon.h"
73c4941b 32#include <stdio.h>
a983c8f0 33
c494cadd
MM
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
f2181eff
MM
46#ifdef HAVE_STDLIB_H
47#include <stdlib.h>
48#endif
49
70fc4ad3
MM
50#ifdef HAVE_SYS_TYPES_H
51#include <sys/types.h>
52#endif
53
c494cadd
MM
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
70fc4ad3
MM
62#ifdef HAVE_SYS_TIME_H
63#include <sys/time.h>
64#endif
65
c494cadd
MM
66#ifdef HAVE_SYS_RESOURCE_H
67#include <sys/resource.h>
80948f39 68int getrusage();
c494cadd
MM
69#endif
70
a983c8f0 71struct _cpu_mon {
f2181eff
MM
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];
a983c8f0
MM
78};
79
80struct _mon {
81 int nr_cpus;
82 cpu_mon cpu_monitor[MAX_NR_PROCESSORS];
83};
84
85
86INLINE_MON mon *
87mon_create(void)
88{
89 mon *monitor = ZALLOC(mon);
90 return monitor;
91}
92
93
94INLINE_MON cpu_mon *
95mon_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
104INLINE_MON void
105mon_init(mon *monitor,
106 int nr_cpus)
107{
80948f39 108 memset(monitor, 0, sizeof(*monitor));
a983c8f0
MM
109 monitor->nr_cpus = nr_cpus;
110}
111
112
113INLINE_MON void
114mon_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
124INLINE_MON void
125mon_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;
80948f39
MM
133 if ((nr_bytes - 1) & ea)
134 monitor->unaligned_read_count += 1;
a983c8f0
MM
135}
136
137
138INLINE_MON void
139mon_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;
80948f39
MM
147 if ((nr_bytes - 1) & ea)
148 monitor->unaligned_write_count += 1;
149}
150
151INLINE_MON void
152mon_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;
a983c8f0
MM
159}
160
f2181eff 161STATIC_INLINE_MON count_type
a983c8f0
MM
162mon_get_number_of_insns(cpu_mon *monitor)
163{
164 itable_index index;
f2181eff 165 count_type total_insns = 0;
a983c8f0
MM
166 for (index = 0; index < nr_itable_entries; index++)
167 total_insns += monitor->issue_count[index];
168 return total_insns;
169}
170
f2181eff
MM
171static int
172mon_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
a983c8f0
MM
180STATIC_INLINE_MON char *
181mon_add_commas(char *buf,
182 int sizeof_buf,
f2181eff 183 count_type value)
a983c8f0
MM
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
204INLINE_MON void
73c4941b
MM
205mon_print_info(psim *system,
206 mon *monitor,
a983c8f0
MM
207 int verbose)
208{
209 char buffer[20];
210 int cpu_nr;
211 int len_cpu;
212 int len_num = 0;
213 int len;
c494cadd 214 long total_insns = 0;
73c4941b 215 long cpu_insns_second = 0;
c494cadd 216 double cpu_time = 0.0;
a983c8f0
MM
217
218 for (cpu_nr = 0; cpu_nr < monitor->nr_cpus; cpu_nr++) {
f2181eff 219 count_type num_insns = mon_get_number_of_insns(&monitor->cpu_monitor[cpu_nr]);
c494cadd
MM
220
221 total_insns += num_insns;
222 len = strlen (mon_add_commas(buffer, sizeof(buffer), num_insns));
a983c8f0
MM
223 if (len_num < len)
224 len_num = len;
225 }
f2181eff 226
a983c8f0
MM
227 sprintf (buffer, "%d", (int)monitor->nr_cpus + 1);
228 len_cpu = strlen (buffer);
229
c494cadd 230#ifdef HAVE_GETRUSAGE
290ad14a 231 if (total_insns && verbose)
c494cadd
MM
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
a983c8f0
MM
243 for (cpu_nr = 0; cpu_nr < monitor->nr_cpus; cpu_nr++) {
244
245 if (verbose > 1) {
f2181eff
MM
246 itable_index sort_insns[nr_itable_entries];
247 int nr_sort_insns = 0;
a983c8f0 248 itable_index index;
f2181eff 249 int index2;
c494cadd
MM
250
251 if (cpu_nr)
252 printf_filtered ("\n");
253
a983c8f0 254 for (index = 0; index < nr_itable_entries; index++) {
f2181eff
MM
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]),
a983c8f0
MM
269 itable[index].name,
270 (monitor->cpu_monitor[cpu_nr].issue_count[index] == 1) ? "" : "s");
271 }
c494cadd 272
73c4941b 273 printf_filtered ("\n");
a983c8f0 274 }
73c4941b 275
290ad14a 276 if (CURRENT_MODEL_ISSUE > 0)
73c4941b 277 {
80948f39
MM
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;
73c4941b
MM
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
80948f39 297 model_mon_info_free(model_ptr, orig_ptr);
73c4941b
MM
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");
80948f39
MM
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");
a983c8f0 331
80948f39
MM
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
c494cadd
MM
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])));
73c4941b 345
a983c8f0 346 }
c494cadd
MM
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)
73c4941b 353 printf_filtered ("%sSimulator speed was %s instructions/second\n",
290ad14a 354 (monitor->nr_cpus > 1) ? "" : "\n",
c494cadd 355 mon_add_commas(buffer, sizeof(buffer), cpu_insns_second));
a983c8f0
MM
356}
357
358#endif /* _MON_C_ */
This page took 0.081493 seconds and 4 git commands to generate.