1 /* This file is part of the program psim.
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
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.
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.
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/>.
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
37 #ifdef HAVE_SYS_RESOURCE_H
38 #include <sys/resource.h>
45 #define MAX_BYTE_READWRITE 9
46 #define MAX_SHIFT_READWRITE 3
49 count_type issue_count
[nr_itable_entries
];
50 count_type read_count
;
51 count_type read_byte_count
[MAX_BYTE_READWRITE
];
52 count_type write_count
;
53 count_type write_byte_count
[MAX_BYTE_READWRITE
];
54 count_type unaligned_read_count
;
55 count_type unaligned_write_count
;
56 count_type event_count
[nr_mon_events
];
61 cpu_mon cpu_monitor
[MAX_NR_PROCESSORS
];
69 mon
*monitor
= ZALLOC(mon
);
79 if (cpu_nr
< 0 || cpu_nr
>= MAX_NR_PROCESSORS
)
80 error("mon_cpu() - invalid cpu number\n");
81 return &monitor
->cpu_monitor
[cpu_nr
];
87 mon_init(mon
*monitor
,
90 memset(monitor
, 0, sizeof(*monitor
));
91 monitor
->nr_cpus
= nr_cpus
;
97 mon_issue(itable_index index
,
101 cpu_mon
*monitor
= cpu_monitor(processor
);
102 ASSERT(index
<= nr_itable_entries
);
103 monitor
->issue_count
[index
] += 1;
109 mon_read(unsigned_word ea
,
115 cpu_mon
*monitor
= cpu_monitor(processor
);
116 monitor
->read_count
+= 1;
117 monitor
->read_byte_count
[nr_bytes
] += 1;
118 if ((nr_bytes
- 1) & ea
)
119 monitor
->unaligned_read_count
+= 1;
125 mon_write(unsigned_word ea
,
131 cpu_mon
*monitor
= cpu_monitor(processor
);
132 monitor
->write_count
+= 1;
133 monitor
->write_byte_count
[nr_bytes
] += 1;
134 if ((nr_bytes
- 1) & ea
)
135 monitor
->unaligned_write_count
+= 1;
140 mon_event(mon_events event
,
144 cpu_mon
*monitor
= cpu_monitor(processor
);
145 ASSERT(event
< nr_mon_events
);
146 monitor
->event_count
[event
] += 1;
151 mon_get_number_of_insns(mon
*monitor
,
155 unsigned total_insns
= 0;
156 ASSERT(cpu_nr
>= 0 && cpu_nr
< monitor
->nr_cpus
);
157 for (index
= 0; index
< nr_itable_entries
; index
++)
158 total_insns
+= monitor
->cpu_monitor
[cpu_nr
].issue_count
[index
];
164 mon_sort_instruction_names(const void *ptr_a
, const void *ptr_b
)
166 itable_index a
= *(const itable_index
*)ptr_a
;
167 itable_index b
= *(const itable_index
*)ptr_b
;
169 return strcmp (itable
[a
].name
, itable
[b
].name
);
174 mon_add_commas(char *buf
,
179 char *endbuf
= buf
+ sizeof_buf
- 1;
189 *--endbuf
= (value
% 10) + '0';
190 } while ((value
/= 10) != 0);
192 ASSERT(endbuf
>= buf
);
199 mon_print_info(psim
*system
,
211 int len_sub_num
[MAX_BYTE_READWRITE
];
214 long total_insns
= 0;
215 long cpu_insns_second
= 0;
216 long total_sim_cycles
= 0;
217 long sim_cycles_second
= 0;
218 double cpu_time
= 0.0;
220 for (i
= 0; i
< MAX_BYTE_READWRITE
; i
++)
223 for (cpu_nr
= 0; cpu_nr
< monitor
->nr_cpus
; cpu_nr
++) {
224 count_type num_insns
= mon_get_number_of_insns(monitor
, cpu_nr
);
226 total_insns
+= num_insns
;
227 len
= strlen (mon_add_commas(buffer
, sizeof(buffer
), num_insns
));
231 for (i
= 0; i
<= MAX_SHIFT_READWRITE
; i
++) {
233 len
= strlen (mon_add_commas(buffer
, sizeof(buffer
),
234 monitor
->cpu_monitor
[cpu_nr
].read_byte_count
[size
]));
235 if (len_sub_num
[size
] < len
)
236 len_sub_num
[size
] = len
;
238 len
= strlen (mon_add_commas(buffer
, sizeof(buffer
),
239 monitor
->cpu_monitor
[cpu_nr
].write_byte_count
[size
]));
240 if (len_sub_num
[size
] < len
)
241 len_sub_num
[size
] = len
;
245 sprintf (buffer
, "%d", (int)monitor
->nr_cpus
+ 1);
246 len_cpu
= strlen (buffer
);
248 #ifdef HAVE_GETRUSAGE
250 struct rusage mytime
;
251 if (getrusage (RUSAGE_SELF
, &mytime
) == 0
252 && (mytime
.ru_utime
.tv_sec
> 0 || mytime
.ru_utime
.tv_usec
> 0)) {
254 cpu_time
= (double)mytime
.ru_utime
.tv_sec
+ (((double)mytime
.ru_utime
.tv_usec
) / 1000000.0);
258 total_sim_cycles
= event_queue_time(psim_event_queue(system
)) - 1;
261 cpu_insns_second
= (long)(((double)total_insns
/ cpu_time
) + 0.5);
262 if (total_sim_cycles
) {
263 sim_cycles_second
= (long)(((double)total_sim_cycles
/ cpu_time
) + 0.5);
268 for (cpu_nr
= 0; cpu_nr
< monitor
->nr_cpus
; cpu_nr
++) {
271 itable_index sort_insns
[nr_itable_entries
];
272 int nr_sort_insns
= 0;
277 printf_filtered ("\n");
279 for (index
= 0; index
< nr_itable_entries
; index
++) {
280 if (monitor
->cpu_monitor
[cpu_nr
].issue_count
[index
]) {
281 sort_insns
[nr_sort_insns
++] = index
;
285 qsort((void *)sort_insns
, nr_sort_insns
, sizeof(sort_insns
[0]), mon_sort_instruction_names
);
287 for (index2
= 0; index2
< nr_sort_insns
; index2
++) {
288 index
= sort_insns
[index2
];
289 printf_filtered("CPU #%*d executed %*s %s instruction%s.\n",
291 len_num
, mon_add_commas(buffer
,
293 monitor
->cpu_monitor
[cpu_nr
].issue_count
[index
]),
295 (monitor
->cpu_monitor
[cpu_nr
].issue_count
[index
] == 1) ? "" : "s");
298 printf_filtered ("\n");
301 if (CURRENT_MODEL_ISSUE
> 0)
303 model_data
*model_ptr
= cpu_model(psim_cpu(system
, cpu_nr
));
304 model_print
*ptr
= model_mon_info(model_ptr
);
305 model_print
*orig_ptr
= ptr
;
309 printf_filtered("CPU #%*d executed %*s %s%s.\n",
311 len_num
, mon_add_commas(buffer
,
316 ? ptr
->suffix_singular
317 : ptr
->suffix_plural
));
322 model_mon_info_free(model_ptr
, orig_ptr
);
325 if (monitor
->cpu_monitor
[cpu_nr
].read_count
)
326 printf_filtered ("CPU #%*d executed %*s read%s (%*s 1-byte, %*s 2-byte, %*s 4-byte, %*s 8-byte).\n",
328 len_num
, mon_add_commas(buffer
,
330 monitor
->cpu_monitor
[cpu_nr
].read_count
),
331 (monitor
->cpu_monitor
[cpu_nr
].read_count
== 1) ? "" : "s",
332 len_sub_num
[1], mon_add_commas(buffer1
,
334 monitor
->cpu_monitor
[cpu_nr
].read_byte_count
[1]),
335 len_sub_num
[2], mon_add_commas(buffer2
,
337 monitor
->cpu_monitor
[cpu_nr
].read_byte_count
[2]),
338 len_sub_num
[4], mon_add_commas(buffer4
,
340 monitor
->cpu_monitor
[cpu_nr
].read_byte_count
[4]),
341 len_sub_num
[8], mon_add_commas(buffer8
,
343 monitor
->cpu_monitor
[cpu_nr
].read_byte_count
[8]));
345 if (monitor
->cpu_monitor
[cpu_nr
].write_count
)
346 printf_filtered ("CPU #%*d executed %*s write%s (%*s 1-byte, %*s 2-byte, %*s 4-byte, %*s 8-byte).\n",
348 len_num
, mon_add_commas(buffer
,
350 monitor
->cpu_monitor
[cpu_nr
].write_count
),
351 (monitor
->cpu_monitor
[cpu_nr
].write_count
== 1) ? "" : "s",
352 len_sub_num
[1], mon_add_commas(buffer1
,
354 monitor
->cpu_monitor
[cpu_nr
].write_byte_count
[1]),
355 len_sub_num
[2], mon_add_commas(buffer2
,
357 monitor
->cpu_monitor
[cpu_nr
].write_byte_count
[2]),
358 len_sub_num
[4], mon_add_commas(buffer4
,
360 monitor
->cpu_monitor
[cpu_nr
].write_byte_count
[4]),
361 len_sub_num
[8], mon_add_commas(buffer8
,
363 monitor
->cpu_monitor
[cpu_nr
].write_byte_count
[8]));
365 if (monitor
->cpu_monitor
[cpu_nr
].unaligned_read_count
)
366 printf_filtered ("CPU #%*d executed %*s unaligned read%s.\n",
368 len_num
, mon_add_commas(buffer
,
370 monitor
->cpu_monitor
[cpu_nr
].unaligned_read_count
),
371 (monitor
->cpu_monitor
[cpu_nr
].unaligned_read_count
== 1) ? "" : "s");
373 if (monitor
->cpu_monitor
[cpu_nr
].unaligned_write_count
)
374 printf_filtered ("CPU #%*d executed %*s unaligned write%s.\n",
376 len_num
, mon_add_commas(buffer
,
378 monitor
->cpu_monitor
[cpu_nr
].unaligned_write_count
),
379 (monitor
->cpu_monitor
[cpu_nr
].unaligned_write_count
== 1) ? "" : "s");
381 if (monitor
->cpu_monitor
[cpu_nr
].event_count
[mon_event_icache_miss
])
382 printf_filtered ("CPU #%*d executed %*s icache miss%s.\n",
384 len_num
, mon_add_commas(buffer
,
386 monitor
->cpu_monitor
[cpu_nr
].event_count
[mon_event_icache_miss
]),
387 (monitor
->cpu_monitor
[cpu_nr
].event_count
[mon_event_icache_miss
] == 1) ? "" : "es");
390 long nr_insns
= mon_get_number_of_insns(monitor
, cpu_nr
);
392 printf_filtered("CPU #%*d executed %*s instructions in total.\n",
394 len_num
, mon_add_commas(buffer
,
400 if (total_insns
> 0) {
401 if (monitor
->nr_cpus
> 1)
402 printf_filtered("\nAll CPUs executed %s instructions in total.\n",
403 mon_add_commas(buffer
, sizeof(buffer
), total_insns
));
405 else if (total_sim_cycles
> 0) {
406 printf_filtered("\nSimulator performed %s simulation cycles.\n",
407 mon_add_commas(buffer
, sizeof(buffer
), total_sim_cycles
));
410 if (cpu_insns_second
)
411 printf_filtered ("%sSimulator speed was %s instructions/second.\n",
412 (monitor
->nr_cpus
> 1) ? "" : "\n",
413 mon_add_commas(buffer
, sizeof(buffer
), cpu_insns_second
));
414 else if (sim_cycles_second
)
415 printf_filtered ("Simulator speed was %s simulation cycles/second\n",
416 mon_add_commas(buffer
, sizeof(buffer
), sim_cycles_second
));
417 else if (cpu_time
> 0.0)
418 printf_filtered ("%sSimulator executed for %.2f seconds\n",
419 (monitor
->nr_cpus
> 1) ? "" : "\n", cpu_time
);