Obey --silent
[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 #endif
57
58 struct _cpu_mon {
59 unsigned issue_count[nr_itable_entries];
60 unsigned read_count;
61 unsigned write_count;
62 function_unit_print *func_unit_print;
63 };
64
65 struct _mon {
66 int nr_cpus;
67 cpu_mon cpu_monitor[MAX_NR_PROCESSORS];
68 };
69
70
71 INLINE_MON mon *
72 mon_create(void)
73 {
74 mon *monitor = ZALLOC(mon);
75 return monitor;
76 }
77
78
79 INLINE_MON cpu_mon *
80 mon_cpu(mon *monitor,
81 int cpu_nr)
82 {
83 if (cpu_nr < 0 || cpu_nr >= MAX_NR_PROCESSORS)
84 error("mon_cpu() - invalid cpu number\n");
85 return &monitor->cpu_monitor[cpu_nr];
86 }
87
88
89 INLINE_MON void
90 mon_init(mon *monitor,
91 int nr_cpus)
92 {
93 bzero(monitor, sizeof(*monitor));
94 monitor->nr_cpus = nr_cpus;
95 }
96
97
98 INLINE_MON void
99 mon_issue(itable_index index,
100 cpu *processor,
101 unsigned_word cia)
102 {
103 cpu_mon *monitor = cpu_monitor(processor);
104 ASSERT(index <= nr_itable_entries);
105 monitor->issue_count[index] += 1;
106
107 if (WITH_FUNCTION_UNIT)
108 function_unit_issue(index, cpu_function_unit(processor), cia);
109 }
110
111
112 INLINE_MON void
113 mon_read(unsigned_word ea,
114 unsigned_word ra,
115 unsigned nr_bytes,
116 cpu *processor,
117 unsigned_word cia)
118 {
119 cpu_mon *monitor = cpu_monitor(processor);
120 monitor->read_count += 1;
121 }
122
123
124 INLINE_MON void
125 mon_write(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->write_count += 1;
133 }
134
135 STATIC_INLINE_MON unsigned
136 mon_get_number_of_insns(cpu_mon *monitor)
137 {
138 itable_index index;
139 unsigned total_insns = 0;
140 for (index = 0; index < nr_itable_entries; index++)
141 total_insns += monitor->issue_count[index];
142 return total_insns;
143 }
144
145 STATIC_INLINE_MON char *
146 mon_add_commas(char *buf,
147 int sizeof_buf,
148 long value)
149 {
150 int comma = 3;
151 char *endbuf = buf + sizeof_buf - 1;
152
153 *--endbuf = '\0';
154 do {
155 if (comma-- == 0)
156 {
157 *--endbuf = ',';
158 comma = 2;
159 }
160
161 *--endbuf = (value % 10) + '0';
162 } while ((value /= 10) != 0);
163
164 ASSERT(endbuf >= buf);
165 return endbuf;
166 }
167
168
169 INLINE_MON void
170 mon_print_info(psim *system,
171 mon *monitor,
172 int verbose)
173 {
174 char buffer[20];
175 int cpu_nr;
176 int len_cpu;
177 int len_num = 0;
178 int len;
179 long total_insns = 0;
180 long cpu_insns_second = 0;
181 double cpu_time = 0.0;
182
183 for (cpu_nr = 0; cpu_nr < monitor->nr_cpus; cpu_nr++) {
184 unsigned num_insns = mon_get_number_of_insns(&monitor->cpu_monitor[cpu_nr]);
185
186 total_insns += num_insns;
187 len = strlen (mon_add_commas(buffer, sizeof(buffer), num_insns));
188 if (len_num < len)
189 len_num = len;
190 }
191
192 sprintf (buffer, "%d", (int)monitor->nr_cpus + 1);
193 len_cpu = strlen (buffer);
194
195 #ifdef HAVE_GETRUSAGE
196 if (total_insns && verbose > 1)
197 {
198 struct rusage mytime;
199 if (getrusage (RUSAGE_SELF, &mytime) == 0
200 && (mytime.ru_utime.tv_sec > 0 || mytime.ru_utime.tv_usec > 0)) {
201
202 cpu_time = (double)mytime.ru_utime.tv_sec + (((double)mytime.ru_utime.tv_usec) / 1000000.0);
203 cpu_insns_second = (long)(((double)total_insns / cpu_time) + 0.5);
204 }
205 }
206 #endif
207
208 for (cpu_nr = 0; cpu_nr < monitor->nr_cpus; cpu_nr++) {
209
210 if (verbose > 1) {
211 itable_index index;
212
213 if (cpu_nr)
214 printf_filtered ("\n");
215
216 for (index = 0; index < nr_itable_entries; index++) {
217 if (monitor->cpu_monitor[cpu_nr].issue_count[index])
218 printf_filtered("CPU #%*d executed %*s %s instruction%s.\n",
219 len_cpu, cpu_nr+1,
220 len_num, mon_add_commas(buffer,
221 sizeof(buffer),
222 monitor->cpu_monitor[cpu_nr].issue_count[index]),
223 itable[index].name,
224 (monitor->cpu_monitor[cpu_nr].issue_count[index] == 1) ? "" : "s");
225 }
226
227 printf_filtered ("\n");
228 }
229
230 if (WITH_FUNCTION_UNIT)
231 {
232 function_unit *func_unit = cpu_function_unit(psim_cpu(system, cpu_nr));
233 function_unit_print *ptr = function_unit_mon_info(func_unit);
234 function_unit_print *orig_ptr = ptr;
235
236 while (ptr) {
237 if (ptr->count)
238 printf_filtered("CPU #%*d executed %*s %s%s.\n",
239 len_cpu, cpu_nr+1,
240 len_num, mon_add_commas(buffer,
241 sizeof(buffer),
242 ptr->count),
243 ptr->name,
244 ((ptr->count == 1)
245 ? ptr->suffix_singular
246 : ptr->suffix_plural));
247
248 ptr = ptr->next;
249 }
250
251 function_unit_mon_free(func_unit, orig_ptr);
252 }
253
254 if (monitor->cpu_monitor[cpu_nr].read_count)
255 printf_filtered ("CPU #%*d executed %*s data read%s.\n",
256 len_cpu, cpu_nr+1,
257 len_num, mon_add_commas(buffer,
258 sizeof(buffer),
259 monitor->cpu_monitor[cpu_nr].read_count),
260 (monitor->cpu_monitor[cpu_nr].read_count == 1) ? "" : "s");
261
262 if (monitor->cpu_monitor[cpu_nr].write_count)
263 printf_filtered ("CPU #%*d executed %*s data write%s.\n",
264 len_cpu, cpu_nr+1,
265 len_num, mon_add_commas(buffer,
266 sizeof(buffer),
267 monitor->cpu_monitor[cpu_nr].write_count),
268 (monitor->cpu_monitor[cpu_nr].write_count == 1) ? "" : "s");
269
270 printf_filtered("CPU #%*d executed %*s instructions in total.\n",
271 len_cpu, cpu_nr+1,
272 len_num, mon_add_commas(buffer,
273 sizeof(buffer),
274 mon_get_number_of_insns(&monitor->cpu_monitor[cpu_nr])));
275
276 }
277
278 if (monitor->nr_cpus > 1)
279 printf_filtered("\nAll CPUs executed %s instructions in total.\n",
280 mon_add_commas(buffer, sizeof(buffer), total_insns));
281
282 if (cpu_insns_second)
283 printf_filtered ("%sSimulator speed was %s instructions/second\n",
284 (monitor->nr_cpus <= 1 && verbose <= 1) ? "" : "\n",
285 mon_add_commas(buffer, sizeof(buffer), cpu_insns_second));
286 }
287
288 #endif /* _MON_C_ */