blob: a9e133e209b72153176c9f436cbb1c3b35d0c21d [file] [log] [blame]
nethercote27fc1da2004-01-04 16:56:57 +00001
2/*--------------------------------------------------------------------*/
3/*--- Cache simulation cg_sim.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Cachegrind, a Valgrind tool for cache
8 profiling programs.
9
sewardj03f8d3f2012-08-05 15:46:46 +000010 Copyright (C) 2002-2012 Nicholas Nethercote
njn2bc10122005-05-08 02:10:27 +000011 njn@valgrind.org
nethercote27fc1da2004-01-04 16:56:57 +000012
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
31/* Notes:
32 - simulates a write-allocate cache
33 - (block --> set) hash function uses simple bit selection
34 - handling of references straddling two cache blocks:
35 - counts as only one cache access (not two)
36 - both blocks hit --> one hit
37 - one block hits, the other misses --> one miss
38 - both blocks miss --> one miss (not two)
39*/
40
41typedef struct {
njn0103de52005-10-10 16:49:01 +000042 Int size; /* bytes */
43 Int assoc;
44 Int line_size; /* bytes */
45 Int sets;
46 Int sets_min_1;
njn0103de52005-10-10 16:49:01 +000047 Int line_size_bits;
48 Int tag_shift;
49 Char desc_line[128];
njnb619ca72005-10-10 16:18:09 +000050 UWord* tags;
nethercote27fc1da2004-01-04 16:56:57 +000051} cache_t2;
52
53/* By this point, the size/assoc/line_size has been checked. */
54static void cachesim_initcache(cache_t config, cache_t2* c)
55{
njn0103de52005-10-10 16:49:01 +000056 Int i;
nethercote27fc1da2004-01-04 16:56:57 +000057
58 c->size = config.size;
59 c->assoc = config.assoc;
60 c->line_size = config.line_size;
61
62 c->sets = (c->size / c->line_size) / c->assoc;
63 c->sets_min_1 = c->sets - 1;
nethercote27fc1da2004-01-04 16:56:57 +000064 c->line_size_bits = VG_(log2)(c->line_size);
65 c->tag_shift = c->line_size_bits + VG_(log2)(c->sets);
66
67 if (c->assoc == 1) {
68 VG_(sprintf)(c->desc_line, "%d B, %d B, direct-mapped",
69 c->size, c->line_size);
70 } else {
71 VG_(sprintf)(c->desc_line, "%d B, %d B, %d-way associative",
72 c->size, c->line_size, c->assoc);
73 }
74
sewardj9c606bd2008-09-18 18:12:50 +000075 c->tags = VG_(malloc)("cg.sim.ci.1",
76 sizeof(UWord) * c->sets * c->assoc);
nethercote27fc1da2004-01-04 16:56:57 +000077
78 for (i = 0; i < c->sets * c->assoc; i++)
79 c->tags[i] = 0;
80}
81
weidendoc1e94262012-10-05 23:58:17 +000082/* This attribute forces GCC to inline the function, getting rid of a
83 * lot of indirection around the cache_t2 pointer, if it is known to be
84 * constant in the caller (the caller is inlined itself).
85 * Without inlining of simulator functions, cachegrind can get 40% slower.
86 */
87__attribute__((always_inline))
88static Bool cachesim_setref_is_miss(cache_t2* c, UInt set_no, UWord tag)
89{
90 int i, j;
91 UWord *set;
nethercote27fc1da2004-01-04 16:56:57 +000092
weidendoc1e94262012-10-05 23:58:17 +000093 set = &(c->tags[set_no * c->assoc]);
94
95 /* This loop is unrolled for just the first case, which is the most */
96 /* common. We can't unroll any further because it would screw up */
97 /* if we have a direct-mapped (1-way) cache. */
98 if (tag == set[0])
99 return False;
100
101 /* If the tag is one other than the MRU, move it into the MRU spot */
102 /* and shuffle the rest down. */
103 for (i = 1; i < c->assoc; i++) {
104 if (tag == set[i]) {
105 for (j = i; j > 0; j--) {
106 set[j] = set[j - 1];
107 }
108 set[0] = tag;
109
110 return False;
111 }
112 }
113
114 /* A miss; install this tag as MRU, shuffle rest down. */
115 for (j = c->assoc - 1; j > 0; j--) {
116 set[j] = set[j - 1];
117 }
118 set[0] = tag;
119
120 return True;
nethercote27fc1da2004-01-04 16:56:57 +0000121}
122
weidendoc1e94262012-10-05 23:58:17 +0000123__attribute__((always_inline))
124static Bool cachesim_ref_is_miss(cache_t2* c, Addr a, UChar size)
125{
126 UInt set1 = ( a >> c->line_size_bits) & (c->sets_min_1);
127 UInt set2 = ((a+size-1) >> c->line_size_bits) & (c->sets_min_1);
128 UWord tag = a >> c->tag_shift;
129
130 /* Access entirely within line. */
131 if (set1 == set2)
132 return cachesim_setref_is_miss(c, set1, tag);
133
134 /* Access straddles two lines. */
135 /* Nb: this is a fast way of doing ((set1+1) % c->sets) */
136 else if (((set1 + 1) & (c->sets_min_1)) == set2) {
137 UWord tag2 = (a+size-1) >> c->tag_shift;
138
139 /* always do both, as state is updated as side effect */
140 if (cachesim_setref_is_miss(c, set1, tag)) {
141 cachesim_setref_is_miss(c, set2, tag2);
142 return True;
143 }
144 return cachesim_setref_is_miss(c, set2, tag2);
145 }
146 VG_(printf)("addr: %lx size: %u sets: %d %d", a, size, set1, set2);
147 VG_(tool_panic)("item straddles more than two cache sets");
148 /* not reached */
149 return True;
150}
151
152
153static cache_t2 LL;
154static cache_t2 I1;
155static cache_t2 D1;
156
157static void cachesim_initcaches(cache_t I1c, cache_t D1c, cache_t LLc)
158{
159 cachesim_initcache(I1c, &I1);
160 cachesim_initcache(D1c, &D1);
161 cachesim_initcache(LLc, &LL);
162}
163
164__attribute__((always_inline))
165static void cachesim_I1_doref(Addr a, UChar size, ULong* m1, ULong *mL)
166{
167 if (cachesim_ref_is_miss(&I1, a, size)) {
168 (*m1)++;
169 if (cachesim_ref_is_miss(&LL, a, size))
170 (*mL)++;
171 }
172}
173
174__attribute__((always_inline))
175static void cachesim_D1_doref(Addr a, UChar size, ULong* m1, ULong *mL)
176{
177 if (cachesim_ref_is_miss(&D1, a, size)) {
178 (*m1)++;
179 if (cachesim_ref_is_miss(&LL, a, size))
180 (*mL)++;
181 }
182}
nethercote27fc1da2004-01-04 16:56:57 +0000183
184/*--------------------------------------------------------------------*/
185/*--- end cg_sim.c ---*/
186/*--------------------------------------------------------------------*/
187