aboutsummaryrefslogtreecommitdiff
path: root/sim/ppc/mon.c
blob: 3fe2f3206f0c41fa1cc3e178bc67b9e99b3568ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*  This file is part of the program psim.

    Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
    */


#ifndef _MON_C_
#define _MON_C_

#ifndef STATIC_INLINE_MON
#define STATIC_INLINE_MON STATIC_INLINE
#endif

#include "basics.h"
#include "cpu.h"
#include "mon.h"
#include <stdio.h>

#ifdef HAVE_STRING_H
#include <string.h>
#else
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_TIME_H
#include <time.h>
#endif

#ifdef HAVE_SYS_TIMES_H
#include <sys/times.h>
#endif

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
int getrusage();
#endif

struct _cpu_mon {
  count_type issue_count[nr_itable_entries];
  count_type read_count;
  count_type write_count;
  count_type unaligned_read_count;
  count_type unaligned_write_count;
  count_type event_count[nr_mon_events];
};

struct _mon {
  int nr_cpus;
  cpu_mon cpu_monitor[MAX_NR_PROCESSORS];
};


INLINE_MON mon *
mon_create(void)
{
  mon *monitor = ZALLOC(mon);
  return monitor;
}


INLINE_MON cpu_mon *
mon_cpu(mon *monitor,
	int cpu_nr)
{
  if (cpu_nr < 0 || cpu_nr >= MAX_NR_PROCESSORS)
    error("mon_cpu() - invalid cpu number\n");
  return &monitor->cpu_monitor[cpu_nr];
}


INLINE_MON void
mon_init(mon *monitor,
	 int nr_cpus)
{
  memset(monitor, 0, sizeof(*monitor));
  monitor->nr_cpus = nr_cpus;
}


INLINE_MON void
mon_issue(itable_index index,
	  cpu *processor, 
	  unsigned_word cia)
{
  cpu_mon *monitor = cpu_monitor(processor);
  ASSERT(index <= nr_itable_entries);
  monitor->issue_count[index] += 1;
}


INLINE_MON void
mon_read(unsigned_word ea,
	 unsigned_word ra,
	 unsigned nr_bytes,
	 cpu *processor,
	 unsigned_word cia)
{
  cpu_mon *monitor = cpu_monitor(processor);
  monitor->read_count += 1;
  if ((nr_bytes - 1) & ea)
    monitor->unaligned_read_count += 1;
}


INLINE_MON void
mon_write(unsigned_word ea,
	  unsigned_word ra,
	  unsigned nr_bytes,
	  cpu *processor,
	  unsigned_word cia)
{
  cpu_mon *monitor = cpu_monitor(processor);
  monitor->write_count += 1;
  if ((nr_bytes - 1) & ea)
    monitor->unaligned_write_count += 1;
}

INLINE_MON void
mon_event(mon_events event,
	  cpu *processor,
	  unsigned_word cia)
{
  cpu_mon *monitor = cpu_monitor(processor);
  ASSERT(event >= 0 && event < nr_mon_events);
  monitor->event_count[event] += 1;
}

STATIC_INLINE_MON count_type
mon_get_number_of_insns(cpu_mon *monitor)
{
  itable_index index;
  count_type total_insns = 0;
  for (index = 0; index < nr_itable_entries; index++)
    total_insns += monitor->issue_count[index];
  return total_insns;
}

static int
mon_sort_instruction_names(const void *ptr_a, const void *ptr_b)
{
  itable_index a = *(const itable_index *)ptr_a;
  itable_index b = *(const itable_index *)ptr_b;

  return strcmp (itable[a].name, itable[b].name);
}

STATIC_INLINE_MON char *
mon_add_commas(char *buf,
	       int sizeof_buf,
	       count_type value)
{
  int comma = 3;
  char *endbuf = buf + sizeof_buf - 1;

  *--endbuf = '\0';
  do {
    if (comma-- == 0)
      {
	*--endbuf = ',';
	comma = 2;
      }

    *--endbuf = (value % 10) + '0';
  } while ((value /= 10) != 0);

  ASSERT(endbuf >= buf);
  return endbuf;
}


INLINE_MON void
mon_print_info(psim *system,
	       mon *monitor,
	       int verbose)
{
  char buffer[20];
  int cpu_nr;
  int len_cpu;
  int len_num = 0;
  int len;
  long total_insns = 0;
  long cpu_insns_second = 0;
  double cpu_time = 0.0;

  for (cpu_nr = 0; cpu_nr < monitor->nr_cpus; cpu_nr++) {
    count_type num_insns = mon_get_number_of_insns(&monitor->cpu_monitor[cpu_nr]);

    total_insns += num_insns;
    len = strlen (mon_add_commas(buffer, sizeof(buffer), num_insns));
    if (len_num < len)
      len_num = len;
  }

  sprintf (buffer, "%d", (int)monitor->nr_cpus + 1);
  len_cpu = strlen (buffer);

#ifdef HAVE_GETRUSAGE
  if (total_insns && verbose)
    {
      struct rusage mytime;
      if (getrusage (RUSAGE_SELF, &mytime) == 0
	  && (mytime.ru_utime.tv_sec > 0 || mytime.ru_utime.tv_usec > 0)) {

	cpu_time = (double)mytime.ru_utime.tv_sec + (((double)mytime.ru_utime.tv_usec) / 1000000.0);
	cpu_insns_second = (long)(((double)total_insns / cpu_time) + 0.5);
      }
  }
#endif

  for (cpu_nr = 0; cpu_nr < monitor->nr_cpus; cpu_nr++) {

    if (verbose > 1) {
      itable_index sort_insns[nr_itable_entries];
      int nr_sort_insns = 0;
      itable_index index;
      int index2;

      if (cpu_nr)
	printf_filtered ("\n");

      for (index = 0; index < nr_itable_entries; index++) {
	if (monitor->cpu_monitor[cpu_nr].issue_count[index]) {
	  sort_insns[nr_sort_insns++] = index;
	}
      }

      qsort((void *)sort_insns, nr_sort_insns, sizeof(sort_insns[0]), mon_sort_instruction_names);

      for (index2 = 0; index2 < nr_sort_insns; index2++) {
	index = sort_insns[index2];
	printf_filtered("CPU #%*d executed %*s %s instruction%s.\n",
			len_cpu, cpu_nr+1,
			len_num, mon_add_commas(buffer,
						sizeof(buffer),
						monitor->cpu_monitor[cpu_nr].issue_count[index]),
			  itable[index].name,
			  (monitor->cpu_monitor[cpu_nr].issue_count[index] == 1) ? "" : "s");
      }

      printf_filtered ("\n");
    }

    if (CURRENT_MODEL_ISSUE > 0)
      {
	model_data *model_ptr = cpu_model(psim_cpu(system, cpu_nr));
	model_print *ptr = model_mon_info(model_ptr);
	model_print *orig_ptr = ptr;

	while (ptr) {
	  if (ptr->count)
	    printf_filtered("CPU #%*d executed %*s %s%s.\n",
			    len_cpu, cpu_nr+1,
			    len_num, mon_add_commas(buffer,
						    sizeof(buffer),
						    ptr->count),
			    ptr->name,
			    ((ptr->count == 1)
			     ? ptr->suffix_singular
			     : ptr->suffix_plural));

	  ptr = ptr->next;
	}

	model_mon_info_free(model_ptr, orig_ptr);
      }

    if (monitor->cpu_monitor[cpu_nr].read_count)
      printf_filtered ("CPU #%*d executed %*s data read%s.\n",
		       len_cpu, cpu_nr+1,
		       len_num, mon_add_commas(buffer,
					       sizeof(buffer),
					       monitor->cpu_monitor[cpu_nr].read_count),
		       (monitor->cpu_monitor[cpu_nr].read_count == 1) ? "" : "s");

    if (monitor->cpu_monitor[cpu_nr].write_count)
      printf_filtered ("CPU #%*d executed %*s data write%s.\n",
		       len_cpu, cpu_nr+1,
		       len_num, mon_add_commas(buffer,
					       sizeof(buffer),
					       monitor->cpu_monitor[cpu_nr].write_count),
		       (monitor->cpu_monitor[cpu_nr].write_count == 1) ? "" : "s");

    if (monitor->cpu_monitor[cpu_nr].unaligned_read_count)
      printf_filtered ("CPU #%*d executed %*s unaligned data read%s.\n",
		       len_cpu, cpu_nr+1,
		       len_num, mon_add_commas(buffer,
					       sizeof(buffer),
					       monitor->cpu_monitor[cpu_nr].read_count),
		       (monitor->cpu_monitor[cpu_nr].read_count == 1) ? "" : "s");

    if (monitor->cpu_monitor[cpu_nr].unaligned_write_count)
      printf_filtered ("CPU #%*d executed %*s unaligned data write%s.\n",
		       len_cpu, cpu_nr+1,
		       len_num, mon_add_commas(buffer,
					       sizeof(buffer),
					       monitor->cpu_monitor[cpu_nr].write_count),
		       (monitor->cpu_monitor[cpu_nr].write_count == 1) ? "" : "s");
    
    if (monitor->cpu_monitor[cpu_nr].event_count[mon_event_icache_miss])
      printf_filtered ("CPU #%*d executed %*s icache miss%s.\n",
		       len_cpu, cpu_nr+1,
		       len_num, mon_add_commas(buffer,
					       sizeof(buffer),
					       monitor->cpu_monitor[cpu_nr].event_count[mon_event_icache_miss]),
		       (monitor->cpu_monitor[cpu_nr].event_count[mon_event_icache_miss] == 1) ? "" : "es");

    printf_filtered("CPU #%*d executed %*s instructions in total.\n",
		    len_cpu, cpu_nr+1,
		    len_num, mon_add_commas(buffer,
					    sizeof(buffer),
					    mon_get_number_of_insns(&monitor->cpu_monitor[cpu_nr])));

  }

  if (monitor->nr_cpus > 1)
    printf_filtered("\nAll CPUs executed %s instructions in total.\n",
		    mon_add_commas(buffer, sizeof(buffer), total_insns));

  if (cpu_insns_second)
    printf_filtered ("%sSimulator speed was %s instructions/second\n",
		     (monitor->nr_cpus > 1) ? "" : "\n",
		     mon_add_commas(buffer, sizeof(buffer), cpu_insns_second));
}

#endif /* _MON_C_ */