Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / sim / common / cgen-scache.c
CommitLineData
c906108c 1/* Simulator cache routines for CGEN simulators (and maybe others).
88b9d363 2 Copyright (C) 1996-2022 Free Software Foundation, Inc.
c906108c
SS
3 Contributed by Cygnus Support.
4
5This file is part of GDB, the GNU debugger.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
4744ac1b
JB
9the Free Software Foundation; either version 3 of the License, or
10(at your option) any later version.
c906108c
SS
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
4744ac1b
JB
17You should have received a copy of the GNU General Public License
18along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 19
6df01ab8
MF
20/* This must come before any other includes. */
21#include "defs.h"
22
c906108c
SS
23#define SCACHE_DEFINE_INLINE
24
25#include "sim-main.h"
c906108c 26#include <stdlib.h>
c906108c
SS
27#include "libiberty.h"
28#include "sim-options.h"
29#include "sim-io.h"
30
c906108c
SS
31/* Unused address. */
32#define UNUSED_ADDR 0xffffffff
33
34/* Scache configuration parameters.
35 ??? Experiments to determine reasonable values is wip.
36 These are just guesses. */
37
38/* Default number of scache elements.
39 The size of an element is typically 32-64 bytes, so the size of the
40 default scache will be between 512K and 1M bytes. */
41#ifdef CONFIG_SIM_CACHE_SIZE
42#define SCACHE_DEFAULT_CACHE_SIZE CONFIG_SIM_CACHE_SIZE
43#else
44#define SCACHE_DEFAULT_CACHE_SIZE 16384
45#endif
46
47/* Minimum cache size.
48 The m32r port assumes a cache size of at least 2 so it can decode both 16
49 bit insns. When compiling we need an extra for the chain entry. And this
50 must be a multiple of 2. Hence 4 is the minimum (though, for those with
51 featuritis or itchy pedantic bits, we could make this conditional on
52 WITH_SCACHE_PBB). */
53#define MIN_SCACHE_SIZE 4
54
55/* Ratio of size of text section to size of scache.
56 When compiling, we don't want to flush the scache more than we have to
57 but we also don't want it to be exorbitantly(sp?) large. So we pick a high
58 default value, then reduce it by the size of the program being simulated,
59 but we don't override any value specified on the command line.
60 If not specified on the command line, the size to use is computed as
61 max (MIN_SCACHE_SIZE,
62 min (DEFAULT_SCACHE_SIZE,
63 text_size / (base_insn_size * INSN_SCACHE_RATIO))). */
64/* ??? Interesting idea but not currently used. */
65#define INSN_SCACHE_RATIO 4
66
67/* Default maximum insn chain length.
68 The only reason for a maximum is so we can place a maximum size on the
69 profiling table. Chain lengths are determined by cti's.
70 32 is a more reasonable number, but when profiling, the before/after
71 handlers take up that much more space. The scache is filled from front to
72 back so all this determines is when the scache needs to be flushed. */
73#define MAX_CHAIN_LENGTH 64
74
75/* Default maximum hash list length. */
76#define MAX_HASH_CHAIN_LENGTH 4
77
78/* Minimum hash table size. */
79#define MIN_HASH_CHAINS 32
80
81/* Ratio of number of scache elements to number of hash lists.
82 Since the user can only specify the size of the scache, we compute the
83 size of the hash table as
84 max (MIN_HASH_CHAINS, scache_size / SCACHE_HASH_RATIO). */
85#define SCACHE_HASH_RATIO 8
86
87/* Hash a PC value.
88 FIXME: May wish to make the hashing architecture specific.
89 FIXME: revisit */
90#define HASH_PC(pc) (((pc) >> 2) + ((pc) >> 5))
91
92static MODULE_INIT_FN scache_init;
93static MODULE_UNINSTALL_FN scache_uninstall;
94
95static DECLARE_OPTION_HANDLER (scache_option_handler);
96
97#define OPTION_PROFILE_SCACHE (OPTION_START + 0)
98
99static const OPTION scache_options[] = {
100 { {"scache-size", optional_argument, NULL, 'c'},
101 'c', "[SIZE]", "Specify size of simulator execution cache",
102 scache_option_handler },
103#if WITH_SCACHE_PBB
104 /* ??? It might be nice to allow the user to specify the size of the hash
105 table, the maximum hash list length, and the maximum chain length, but
106 for now that might be more akin to featuritis. */
107#endif
108 { {"profile-scache", optional_argument, NULL, OPTION_PROFILE_SCACHE},
109 '\0', "on|off", "Perform simulator execution cache profiling",
110 scache_option_handler },
111 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
112};
113
114static SIM_RC
115scache_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
116 char *arg, int is_command)
117{
c906108c
SS
118 switch (opt)
119 {
120 case 'c' :
121 if (WITH_SCACHE)
122 {
123 if (arg != NULL)
124 {
13754e4c 125 unsigned int n = (unsigned int) strtoul (arg, NULL, 0);
c906108c
SS
126 if (n < MIN_SCACHE_SIZE)
127 {
13754e4c
NC
128 sim_io_eprintf (sd, "invalid scache size `%u', must be at least %u",
129 n, MIN_SCACHE_SIZE);
c906108c
SS
130 return SIM_RC_FAIL;
131 }
132 /* Ensure it's a multiple of 2. */
133 if ((n & (n - 1)) != 0)
134 {
13754e4c
NC
135 unsigned int i;
136 sim_io_eprintf (sd, "scache size `%u' not a multiple of 2\n", n);
137 /* Round up to nearest multiple of 2. */
138 for (i = 1; i && i < n; i <<= 1)
139 continue;
140 if (i)
141 {
142 n = i;
143 sim_io_eprintf (sd, "rounding scache size up to %u\n", n);
144 }
c906108c
SS
145 }
146 if (cpu == NULL)
147 STATE_SCACHE_SIZE (sd) = n;
148 else
149 CPU_SCACHE_SIZE (cpu) = n;
150 }
151 else
152 {
153 if (cpu == NULL)
154 STATE_SCACHE_SIZE (sd) = SCACHE_DEFAULT_CACHE_SIZE;
155 else
156 CPU_SCACHE_SIZE (cpu) = SCACHE_DEFAULT_CACHE_SIZE;
157 }
158 }
159 else
160 sim_io_eprintf (sd, "Simulator execution cache not enabled, `--scache-size' ignored\n");
161 break;
162
163 case OPTION_PROFILE_SCACHE :
164 if (WITH_SCACHE && WITH_PROFILE_SCACHE_P)
165 {
166 /* FIXME: handle cpu != NULL. */
167 return sim_profile_set_option (sd, "-scache", PROFILE_SCACHE_IDX,
168 arg);
169 }
170 else
171 sim_io_eprintf (sd, "Simulator cache profiling not compiled in, `--profile-scache' ignored\n");
172 break;
173 }
174
175 return SIM_RC_OK;
176}
177
6cf75d89
MF
178/* Provide a prototype to silence -Wmissing-prototypes. */
179SIM_RC sim_install_scache (SIM_DESC sd);
180
181/* Install the simulator cache into the simulator. */
c906108c 182SIM_RC
6cf75d89 183sim_install_scache (SIM_DESC sd)
c906108c
SS
184{
185 sim_add_option_table (sd, NULL, scache_options);
186 sim_module_add_init_fn (sd, scache_init);
187 sim_module_add_uninstall_fn (sd, scache_uninstall);
188
189 /* This is the default, it may be overridden on the command line. */
190 STATE_SCACHE_SIZE (sd) = WITH_SCACHE;
191
192 return SIM_RC_OK;
193}
194
195static SIM_RC
196scache_init (SIM_DESC sd)
197{
198 int c;
199
200 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
201 {
202 SIM_CPU *cpu = STATE_CPU (sd, c);
203 int elm_size = IMP_PROPS_SCACHE_ELM_SIZE (MACH_IMP_PROPS (CPU_MACH (cpu)));
204
205 /* elm_size is 0 if the cpu doesn't not have scache support */
206 if (elm_size == 0)
207 {
208 CPU_SCACHE_SIZE (cpu) = 0;
209 CPU_SCACHE_CACHE (cpu) = NULL;
210 }
211 else
212 {
213 if (CPU_SCACHE_SIZE (cpu) == 0)
214 CPU_SCACHE_SIZE (cpu) = STATE_SCACHE_SIZE (sd);
215 CPU_SCACHE_CACHE (cpu) =
216 (SCACHE *) xmalloc (CPU_SCACHE_SIZE (cpu) * elm_size);
217#if WITH_SCACHE_PBB
218 CPU_SCACHE_MAX_CHAIN_LENGTH (cpu) = MAX_CHAIN_LENGTH;
219 CPU_SCACHE_NUM_HASH_CHAIN_ENTRIES (cpu) = MAX_HASH_CHAIN_LENGTH;
bc273e17 220 CPU_SCACHE_NUM_HASH_CHAINS (cpu) = max (MIN_HASH_CHAINS,
c906108c
SS
221 CPU_SCACHE_SIZE (cpu)
222 / SCACHE_HASH_RATIO);
223 CPU_SCACHE_HASH_TABLE (cpu) =
224 (SCACHE_MAP *) xmalloc (CPU_SCACHE_NUM_HASH_CHAINS (cpu)
225 * CPU_SCACHE_NUM_HASH_CHAIN_ENTRIES (cpu)
226 * sizeof (SCACHE_MAP));
227 CPU_SCACHE_PBB_BEGIN (cpu) = (SCACHE *) zalloc (elm_size);
228 CPU_SCACHE_CHAIN_LENGTHS (cpu) =
229 (unsigned long *) zalloc ((CPU_SCACHE_MAX_CHAIN_LENGTH (cpu) + 1)
230 * sizeof (long));
231#endif
232 }
233 }
234
235 scache_flush (sd);
236
237 return SIM_RC_OK;
238}
239
240static void
241scache_uninstall (SIM_DESC sd)
242{
243 int c;
244
245 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
246 {
247 SIM_CPU *cpu = STATE_CPU (sd, c);
248
249 if (CPU_SCACHE_CACHE (cpu) != NULL)
250 free (CPU_SCACHE_CACHE (cpu));
251#if WITH_SCACHE_PBB
252 if (CPU_SCACHE_HASH_TABLE (cpu) != NULL)
253 free (CPU_SCACHE_HASH_TABLE (cpu));
254 if (CPU_SCACHE_PBB_BEGIN (cpu) != NULL)
255 free (CPU_SCACHE_PBB_BEGIN (cpu));
256 if (CPU_SCACHE_CHAIN_LENGTHS (cpu) != NULL)
257 free (CPU_SCACHE_CHAIN_LENGTHS (cpu));
258#endif
259 }
260}
261
262void
263scache_flush (SIM_DESC sd)
264{
265 int c;
266
267 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
268 {
269 SIM_CPU *cpu = STATE_CPU (sd, c);
270 scache_flush_cpu (cpu);
271 }
272}
273
274void
275scache_flush_cpu (SIM_CPU *cpu)
276{
277 int i,n;
278
279 /* Don't bother if cache not in use. */
280 if (CPU_SCACHE_SIZE (cpu) == 0)
281 return;
282
283#if WITH_SCACHE_PBB
284 /* It's important that this be reasonably fast as this can be done when
285 the simulation is running. */
286 CPU_SCACHE_NEXT_FREE (cpu) = CPU_SCACHE_CACHE (cpu);
287 n = CPU_SCACHE_NUM_HASH_CHAINS (cpu) * CPU_SCACHE_NUM_HASH_CHAIN_ENTRIES (cpu);
288 /* ??? Might be faster to just set the first entry, then update the
289 "last entry" marker during allocation. */
290 for (i = 0; i < n; ++i)
291 CPU_SCACHE_HASH_TABLE (cpu) [i] . pc = UNUSED_ADDR;
292#else
293 {
294 int elm_size = IMP_PROPS_SCACHE_ELM_SIZE (MACH_IMP_PROPS (CPU_MACH (cpu)));
295 SCACHE *sc;
296
297 /* Technically, this may not be necessary, but it helps debugging. */
298 memset (CPU_SCACHE_CACHE (cpu), 0,
299 CPU_SCACHE_SIZE (cpu) * elm_size);
300
301 for (i = 0, sc = CPU_SCACHE_CACHE (cpu); i < CPU_SCACHE_SIZE (cpu);
302 ++i, sc = (SCACHE *) ((char *) sc + elm_size))
303 {
304 sc->argbuf.addr = UNUSED_ADDR;
305 }
306 }
307#endif
308}
309
310#if WITH_SCACHE_PBB
311
312/* Look up PC in the hash table of scache entry points.
313 Returns the entry or NULL if not found. */
314
315SCACHE *
316scache_lookup (SIM_CPU *cpu, IADDR pc)
317{
96baa820
JM
318 /* FIXME: hash computation is wrong, doesn't take into account
319 NUM_HASH_CHAIN_ENTRIES. A lot of the hash table will be unused! */
c906108c
SS
320 unsigned int slot = HASH_PC (pc) & (CPU_SCACHE_NUM_HASH_CHAINS (cpu) - 1);
321 int i, max_i = CPU_SCACHE_NUM_HASH_CHAIN_ENTRIES (cpu);
322 SCACHE_MAP *scm;
323
324 /* We don't update hit/miss statistics as this is only used when recording
325 branch target addresses. */
326
327 scm = & CPU_SCACHE_HASH_TABLE (cpu) [slot];
328 for (i = 0; i < max_i && scm->pc != UNUSED_ADDR; ++i, ++scm)
329 {
330 if (scm->pc == pc)
331 return scm->sc;
332 }
333 return 0;
334}
335
336/* Look up PC and if not found create an entry for it.
337 If found the result is a pointer to the SCACHE entry.
338 If not found the result is NULL, and the address of a buffer of at least
339 N entries is stored in BUFP.
340 It's done this way so the caller can still distinguish found/not-found.
341 If the table is full, it is emptied to make room.
342 If the maximum length of a hash list is reached a random entry is thrown out
343 to make room.
344 ??? One might want to try to make this smarter, but let's see some
345 measurable benefit first. */
346
347SCACHE *
348scache_lookup_or_alloc (SIM_CPU *cpu, IADDR pc, int n, SCACHE **bufp)
349{
96baa820
JM
350 /* FIXME: hash computation is wrong, doesn't take into account
351 NUM_HASH_CHAIN_ENTRIES. A lot of the hash table will be unused! */
c906108c
SS
352 unsigned int slot = HASH_PC (pc) & (CPU_SCACHE_NUM_HASH_CHAINS (cpu) - 1);
353 int i, max_i = CPU_SCACHE_NUM_HASH_CHAIN_ENTRIES (cpu);
354 SCACHE_MAP *scm;
355 SCACHE *sc;
356
357 scm = & CPU_SCACHE_HASH_TABLE (cpu) [slot];
358 for (i = 0; i < max_i && scm->pc != UNUSED_ADDR; ++i, ++scm)
359 {
360 if (scm->pc == pc)
361 {
362 PROFILE_COUNT_SCACHE_HIT (cpu);
363 return scm->sc;
364 }
365 }
366 PROFILE_COUNT_SCACHE_MISS (cpu);
367
368 /* The address we want isn't cached. Bummer.
369 If the hash chain we have for this address is full, throw out an entry
370 to make room. */
371
372 if (i == max_i)
373 {
374 /* Rather than do something sophisticated like LRU, we just throw out
375 a semi-random entry. Let someone else have the joy of saying how
376 wrong this is. NEXT_FREE is the entry to throw out and cycles
377 through all possibilities. */
378 static int next_free = 0;
379
380 scm = & CPU_SCACHE_HASH_TABLE (cpu) [slot];
96baa820 381 /* FIXME: This seems rather clumsy. */
c906108c
SS
382 for (i = 0; i < next_free; ++i, ++scm)
383 continue;
384 ++next_free;
385 if (next_free == CPU_SCACHE_NUM_HASH_CHAIN_ENTRIES (cpu))
386 next_free = 0;
387 }
388
389 /* At this point SCM points to the hash table entry to use.
390 Now make sure there's room in the cache. */
96baa820
JM
391 /* FIXME: Kinda weird to use a next_free adjusted scm when cache is
392 flushed. */
c906108c
SS
393
394 {
395 int elm_size = IMP_PROPS_SCACHE_ELM_SIZE (MACH_IMP_PROPS (CPU_MACH (cpu)));
396 int elms_used = (((char *) CPU_SCACHE_NEXT_FREE (cpu)
397 - (char *) CPU_SCACHE_CACHE (cpu))
398 / elm_size);
399 int elms_left = CPU_SCACHE_SIZE (cpu) - elms_used;
400
401 if (elms_left < n)
402 {
403 PROFILE_COUNT_SCACHE_FULL_FLUSH (cpu);
404 scache_flush_cpu (cpu);
405 }
406 }
407
408 sc = CPU_SCACHE_NEXT_FREE (cpu);
409 scm->pc = pc;
410 scm->sc = sc;
411
412 *bufp = sc;
413 return NULL;
414}
415
416#endif /* WITH_SCACHE_PBB */
417
418/* Print cache access statics for CPU. */
419
420void
421scache_print_profile (SIM_CPU *cpu, int verbose)
422{
423 SIM_DESC sd = CPU_STATE (cpu);
424 unsigned long hits = CPU_SCACHE_HITS (cpu);
425 unsigned long misses = CPU_SCACHE_MISSES (cpu);
426 char buf[20];
427 unsigned long max_val;
428 unsigned long *lengths;
429 int i;
430
431 if (CPU_SCACHE_SIZE (cpu) == 0)
432 return;
433
434 sim_io_printf (sd, "Simulator Cache Statistics\n\n");
435
436 /* One could use PROFILE_LABEL_WIDTH here. I chose not to. */
437 sim_io_printf (sd, " Cache size: %s\n",
438 sim_add_commas (buf, sizeof (buf), CPU_SCACHE_SIZE (cpu)));
439 sim_io_printf (sd, " Hits: %s\n",
440 sim_add_commas (buf, sizeof (buf), hits));
441 sim_io_printf (sd, " Misses: %s\n",
442 sim_add_commas (buf, sizeof (buf), misses));
443 if (hits + misses != 0)
444 sim_io_printf (sd, " Hit rate: %.2f%%\n",
445 ((double) hits / ((double) hits + (double) misses)) * 100);
446
447#if WITH_SCACHE_PBB
448 sim_io_printf (sd, "\n");
449 sim_io_printf (sd, " Hash table size: %s\n",
450 sim_add_commas (buf, sizeof (buf), CPU_SCACHE_NUM_HASH_CHAINS (cpu)));
451 sim_io_printf (sd, " Max hash list length: %s\n",
452 sim_add_commas (buf, sizeof (buf), CPU_SCACHE_NUM_HASH_CHAIN_ENTRIES (cpu)));
453 sim_io_printf (sd, " Max insn chain length: %s\n",
454 sim_add_commas (buf, sizeof (buf), CPU_SCACHE_MAX_CHAIN_LENGTH (cpu)));
455 sim_io_printf (sd, " Cache full flushes: %s\n",
456 sim_add_commas (buf, sizeof (buf), CPU_SCACHE_FULL_FLUSHES (cpu)));
457 sim_io_printf (sd, "\n");
458
459 if (verbose)
460 {
461 sim_io_printf (sd, " Insn chain lengths:\n\n");
462 max_val = 0;
463 lengths = CPU_SCACHE_CHAIN_LENGTHS (cpu);
464 for (i = 1; i < CPU_SCACHE_MAX_CHAIN_LENGTH (cpu); ++i)
465 if (lengths[i] > max_val)
466 max_val = lengths[i];
467 for (i = 1; i < CPU_SCACHE_MAX_CHAIN_LENGTH (cpu); ++i)
468 {
469 sim_io_printf (sd, " %2d: %*s: ",
470 i,
471 max_val < 10000 ? 5 : 10,
472 sim_add_commas (buf, sizeof (buf), lengths[i]));
ef93a840 473 sim_profile_print_bar (sd, cpu, PROFILE_HISTOGRAM_WIDTH,
c906108c
SS
474 lengths[i], max_val);
475 sim_io_printf (sd, "\n");
476 }
477 sim_io_printf (sd, "\n");
478 }
479#endif /* WITH_SCACHE_PBB */
480}
This page took 1.079353 seconds and 4 git commands to generate.