Include ld-lib.exp from ctf-lib.exp
[binutils-gdb.git] / sim / ppc / mon.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1997, 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 3 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, see <http://www.gnu.org/licenses/>.
17
18 */
19
20
21 #ifndef _MON_C_
22 #define _MON_C_
23
24 #include "basics.h"
25 #include "cpu.h"
26 #include "mon.h"
27 #include <stdio.h>
28
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #include <stdlib.h>
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37 #include <time.h>
38 #ifdef HAVE_SYS_TIMES_H
39 #include <sys/times.h>
40 #endif
41
42 #ifdef HAVE_SYS_TIME_H
43 #include <sys/time.h>
44 #endif
45
46 #ifdef HAVE_SYS_RESOURCE_H
47 #include <sys/resource.h>
48 int getrusage();
49 #endif
50
51 #define MAX_BYTE_READWRITE 9
52 #define MAX_SHIFT_READWRITE 3
53
54 struct _cpu_mon {
55 count_type issue_count[nr_itable_entries];
56 count_type read_count;
57 count_type read_byte_count[MAX_BYTE_READWRITE];
58 count_type write_count;
59 count_type write_byte_count[MAX_BYTE_READWRITE];
60 count_type unaligned_read_count;
61 count_type unaligned_write_count;
62 count_type event_count[nr_mon_events];
63 };
64
65 struct _mon {
66 int nr_cpus;
67 cpu_mon cpu_monitor[MAX_NR_PROCESSORS];
68 };
69
70
71 INLINE_MON\
72 (mon *)
73 mon_create(void)
74 {
75 mon *monitor = ZALLOC(mon);
76 return monitor;
77 }
78
79
80 INLINE_MON\
81 (cpu_mon *)
82 mon_cpu(mon *monitor,
83 int cpu_nr)
84 {
85 if (cpu_nr < 0 || cpu_nr >= MAX_NR_PROCESSORS)
86 error("mon_cpu() - invalid cpu number\n");
87 return &monitor->cpu_monitor[cpu_nr];
88 }
89
90
91 INLINE_MON\
92 (void)
93 mon_init(mon *monitor,
94 int nr_cpus)
95 {
96 memset(monitor, 0, sizeof(*monitor));
97 monitor->nr_cpus = nr_cpus;
98 }
99
100
101 INLINE_MON\
102 (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 }
111
112
113 INLINE_MON\
114 (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 monitor->read_byte_count[nr_bytes] += 1;
124 if ((nr_bytes - 1) & ea)
125 monitor->unaligned_read_count += 1;
126 }
127
128
129 INLINE_MON\
130 (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 monitor->write_byte_count[nr_bytes] += 1;
140 if ((nr_bytes - 1) & ea)
141 monitor->unaligned_write_count += 1;
142 }
143
144 INLINE_MON\
145 (void)
146 mon_event(mon_events event,
147 cpu *processor,
148 unsigned_word cia)
149 {
150 cpu_mon *monitor = cpu_monitor(processor);
151 ASSERT(event < nr_mon_events);
152 monitor->event_count[event] += 1;
153 }
154
155 INLINE_MON\
156 (unsigned)
157 mon_get_number_of_insns(mon *monitor,
158 int cpu_nr)
159 {
160 itable_index index;
161 unsigned total_insns = 0;
162 ASSERT(cpu_nr >= 0 && cpu_nr < monitor->nr_cpus);
163 for (index = 0; index < nr_itable_entries; index++)
164 total_insns += monitor->cpu_monitor[cpu_nr].issue_count[index];
165 return total_insns;
166 }
167
168 STATIC_INLINE_MON\
169 (int)
170 mon_sort_instruction_names(const void *ptr_a, const void *ptr_b)
171 {
172 itable_index a = *(const itable_index *)ptr_a;
173 itable_index b = *(const itable_index *)ptr_b;
174
175 return strcmp (itable[a].name, itable[b].name);
176 }
177
178 STATIC_INLINE_MON\
179 (char *)
180 mon_add_commas(char *buf,
181 int sizeof_buf,
182 count_type value)
183 {
184 int comma = 3;
185 char *endbuf = buf + sizeof_buf - 1;
186
187 *--endbuf = '\0';
188 do {
189 if (comma-- == 0)
190 {
191 *--endbuf = ',';
192 comma = 2;
193 }
194
195 *--endbuf = (value % 10) + '0';
196 } while ((value /= 10) != 0);
197
198 ASSERT(endbuf >= buf);
199 return endbuf;
200 }
201
202
203 INLINE_MON\
204 (void)
205 mon_print_info(psim *system,
206 mon *monitor,
207 int verbose)
208 {
209 char buffer[20];
210 char buffer1[20];
211 char buffer2[20];
212 char buffer4[20];
213 char buffer8[20];
214 int cpu_nr;
215 int len_cpu;
216 int len_num = 0;
217 int len_sub_num[MAX_BYTE_READWRITE];
218 int len;
219 int i;
220 long total_insns = 0;
221 long cpu_insns_second = 0;
222 long total_sim_cycles = 0;
223 long sim_cycles_second = 0;
224 double cpu_time = 0.0;
225
226 for (i = 0; i < MAX_BYTE_READWRITE; i++)
227 len_sub_num[i] = 0;
228
229 for (cpu_nr = 0; cpu_nr < monitor->nr_cpus; cpu_nr++) {
230 count_type num_insns = mon_get_number_of_insns(monitor, cpu_nr);
231
232 total_insns += num_insns;
233 len = strlen (mon_add_commas(buffer, sizeof(buffer), num_insns));
234 if (len_num < len)
235 len_num = len;
236
237 for (i = 0; i <= MAX_SHIFT_READWRITE; i++) {
238 int size = 1<<i;
239 len = strlen (mon_add_commas(buffer, sizeof(buffer),
240 monitor->cpu_monitor[cpu_nr].read_byte_count[size]));
241 if (len_sub_num[size] < len)
242 len_sub_num[size] = len;
243
244 len = strlen (mon_add_commas(buffer, sizeof(buffer),
245 monitor->cpu_monitor[cpu_nr].write_byte_count[size]));
246 if (len_sub_num[size] < len)
247 len_sub_num[size] = len;
248 }
249 }
250
251 sprintf (buffer, "%d", (int)monitor->nr_cpus + 1);
252 len_cpu = strlen (buffer);
253
254 #ifdef HAVE_GETRUSAGE
255 {
256 struct rusage mytime;
257 if (getrusage (RUSAGE_SELF, &mytime) == 0
258 && (mytime.ru_utime.tv_sec > 0 || mytime.ru_utime.tv_usec > 0)) {
259
260 cpu_time = (double)mytime.ru_utime.tv_sec + (((double)mytime.ru_utime.tv_usec) / 1000000.0);
261 }
262 }
263 if (WITH_EVENTS)
264 total_sim_cycles = event_queue_time(psim_event_queue(system)) - 1;
265 if (cpu_time > 0) {
266 if (total_insns > 0)
267 cpu_insns_second = (long)(((double)total_insns / cpu_time) + 0.5);
268 if (total_sim_cycles) {
269 sim_cycles_second = (long)(((double)total_sim_cycles / cpu_time) + 0.5);
270 }
271 }
272 #endif
273
274 for (cpu_nr = 0; cpu_nr < monitor->nr_cpus; cpu_nr++) {
275
276 if (verbose > 1) {
277 itable_index sort_insns[nr_itable_entries];
278 int nr_sort_insns = 0;
279 itable_index index;
280 int index2;
281
282 if (cpu_nr)
283 printf_filtered ("\n");
284
285 for (index = 0; index < nr_itable_entries; index++) {
286 if (monitor->cpu_monitor[cpu_nr].issue_count[index]) {
287 sort_insns[nr_sort_insns++] = index;
288 }
289 }
290
291 qsort((void *)sort_insns, nr_sort_insns, sizeof(sort_insns[0]), mon_sort_instruction_names);
292
293 for (index2 = 0; index2 < nr_sort_insns; index2++) {
294 index = sort_insns[index2];
295 printf_filtered("CPU #%*d executed %*s %s instruction%s.\n",
296 len_cpu, cpu_nr+1,
297 len_num, mon_add_commas(buffer,
298 sizeof(buffer),
299 monitor->cpu_monitor[cpu_nr].issue_count[index]),
300 itable[index].name,
301 (monitor->cpu_monitor[cpu_nr].issue_count[index] == 1) ? "" : "s");
302 }
303
304 printf_filtered ("\n");
305 }
306
307 if (CURRENT_MODEL_ISSUE > 0)
308 {
309 model_data *model_ptr = cpu_model(psim_cpu(system, cpu_nr));
310 model_print *ptr = model_mon_info(model_ptr);
311 model_print *orig_ptr = ptr;
312
313 while (ptr) {
314 if (ptr->count)
315 printf_filtered("CPU #%*d executed %*s %s%s.\n",
316 len_cpu, cpu_nr+1,
317 len_num, mon_add_commas(buffer,
318 sizeof(buffer),
319 ptr->count),
320 ptr->name,
321 ((ptr->count == 1)
322 ? ptr->suffix_singular
323 : ptr->suffix_plural));
324
325 ptr = ptr->next;
326 }
327
328 model_mon_info_free(model_ptr, orig_ptr);
329 }
330
331 if (monitor->cpu_monitor[cpu_nr].read_count)
332 printf_filtered ("CPU #%*d executed %*s read%s (%*s 1-byte, %*s 2-byte, %*s 4-byte, %*s 8-byte).\n",
333 len_cpu, cpu_nr+1,
334 len_num, mon_add_commas(buffer,
335 sizeof(buffer),
336 monitor->cpu_monitor[cpu_nr].read_count),
337 (monitor->cpu_monitor[cpu_nr].read_count == 1) ? "" : "s",
338 len_sub_num[1], mon_add_commas(buffer1,
339 sizeof(buffer1),
340 monitor->cpu_monitor[cpu_nr].read_byte_count[1]),
341 len_sub_num[2], mon_add_commas(buffer2,
342 sizeof(buffer2),
343 monitor->cpu_monitor[cpu_nr].read_byte_count[2]),
344 len_sub_num[4], mon_add_commas(buffer4,
345 sizeof(buffer4),
346 monitor->cpu_monitor[cpu_nr].read_byte_count[4]),
347 len_sub_num[8], mon_add_commas(buffer8,
348 sizeof(buffer8),
349 monitor->cpu_monitor[cpu_nr].read_byte_count[8]));
350
351 if (monitor->cpu_monitor[cpu_nr].write_count)
352 printf_filtered ("CPU #%*d executed %*s write%s (%*s 1-byte, %*s 2-byte, %*s 4-byte, %*s 8-byte).\n",
353 len_cpu, cpu_nr+1,
354 len_num, mon_add_commas(buffer,
355 sizeof(buffer),
356 monitor->cpu_monitor[cpu_nr].write_count),
357 (monitor->cpu_monitor[cpu_nr].write_count == 1) ? "" : "s",
358 len_sub_num[1], mon_add_commas(buffer1,
359 sizeof(buffer1),
360 monitor->cpu_monitor[cpu_nr].write_byte_count[1]),
361 len_sub_num[2], mon_add_commas(buffer2,
362 sizeof(buffer2),
363 monitor->cpu_monitor[cpu_nr].write_byte_count[2]),
364 len_sub_num[4], mon_add_commas(buffer4,
365 sizeof(buffer4),
366 monitor->cpu_monitor[cpu_nr].write_byte_count[4]),
367 len_sub_num[8], mon_add_commas(buffer8,
368 sizeof(buffer8),
369 monitor->cpu_monitor[cpu_nr].write_byte_count[8]));
370
371 if (monitor->cpu_monitor[cpu_nr].unaligned_read_count)
372 printf_filtered ("CPU #%*d executed %*s unaligned read%s.\n",
373 len_cpu, cpu_nr+1,
374 len_num, mon_add_commas(buffer,
375 sizeof(buffer),
376 monitor->cpu_monitor[cpu_nr].unaligned_read_count),
377 (monitor->cpu_monitor[cpu_nr].unaligned_read_count == 1) ? "" : "s");
378
379 if (monitor->cpu_monitor[cpu_nr].unaligned_write_count)
380 printf_filtered ("CPU #%*d executed %*s unaligned write%s.\n",
381 len_cpu, cpu_nr+1,
382 len_num, mon_add_commas(buffer,
383 sizeof(buffer),
384 monitor->cpu_monitor[cpu_nr].unaligned_write_count),
385 (monitor->cpu_monitor[cpu_nr].unaligned_write_count == 1) ? "" : "s");
386
387 if (monitor->cpu_monitor[cpu_nr].event_count[mon_event_icache_miss])
388 printf_filtered ("CPU #%*d executed %*s icache miss%s.\n",
389 len_cpu, cpu_nr+1,
390 len_num, mon_add_commas(buffer,
391 sizeof(buffer),
392 monitor->cpu_monitor[cpu_nr].event_count[mon_event_icache_miss]),
393 (monitor->cpu_monitor[cpu_nr].event_count[mon_event_icache_miss] == 1) ? "" : "es");
394
395 {
396 long nr_insns = mon_get_number_of_insns(monitor, cpu_nr);
397 if (nr_insns > 0)
398 printf_filtered("CPU #%*d executed %*s instructions in total.\n",
399 len_cpu, cpu_nr+1,
400 len_num, mon_add_commas(buffer,
401 sizeof(buffer),
402 nr_insns));
403 }
404 }
405
406 if (total_insns > 0) {
407 if (monitor->nr_cpus > 1)
408 printf_filtered("\nAll CPUs executed %s instructions in total.\n",
409 mon_add_commas(buffer, sizeof(buffer), total_insns));
410 }
411 else if (total_sim_cycles > 0) {
412 printf_filtered("\nSimulator performed %s simulation cycles.\n",
413 mon_add_commas(buffer, sizeof(buffer), total_sim_cycles));
414 }
415
416 if (cpu_insns_second)
417 printf_filtered ("%sSimulator speed was %s instructions/second.\n",
418 (monitor->nr_cpus > 1) ? "" : "\n",
419 mon_add_commas(buffer, sizeof(buffer), cpu_insns_second));
420 else if (sim_cycles_second)
421 printf_filtered ("Simulator speed was %s simulation cycles/second\n",
422 mon_add_commas(buffer, sizeof(buffer), sim_cycles_second));
423 else if (cpu_time > 0.0)
424 printf_filtered ("%sSimulator executed for %.2f seconds\n",
425 (monitor->nr_cpus > 1) ? "" : "\n", cpu_time);
426
427 }
428
429 #endif /* _MON_C_ */