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