/
cds.c
199 lines (154 loc) · 6.08 KB
/
cds.c
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
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
/* cache description support code */
#include "global.h"
#include "cds.h"
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
String print_sets_and_ways(CDS *cds)
{
if (cds->number_of_ways == 1) return("direct-mapped");
if (cds->number_of_ways == cds->number_of_cache_entries) return("fully associative");
static char buffer[64];
sprintf (buffer, "%d sets of %d ways",
cds->number_of_cache_entries/cds->number_of_ways, cds->number_of_ways);
return(buffer);
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
String memory_reference_type_name(enum memory_access_type type)
{
switch(type)
{
case MAT_FETCH: return("Fetch");
case MAT_LOAD: return("Load");
case MAT_STORE: return("Store");
}
return("invalid");
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
String CRP_name(CDS *cds)
{
switch(cds->replacement_policy)
{
case CRP_FIFO: return("FIFO");
case CRP_RANDOM: return("RANDOM");
case CRP_LRU: return("LRU");
case CRP_LFU:
{
static char buffer[64];
sprintf(buffer, "LFU (decay=%d)", cds->LFU_Decay_Interval);
return(buffer);
}
};
return("Invalid policy");
}
void debug_print_cds(CDS *cds)
{
fprintf(debug_file, "%s: Total number of entries: %d\n", cds->name, cds->number_of_cache_entries);
fprintf(debug_file, "%s: %s\n", cds->name, print_sets_and_ways(cds));
fprintf(debug_file, "%s: Each cache line is %d bytes\n", cds->name, cds->cache_line_size);
fprintf(debug_file, "%s: Cache is %s\n", cds->name, cds->write_back ? "write-back" : "write-thru");
fprintf(debug_file, "%s: With a %s replacement policy\n", cds->name, CRP_name(cds));
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
void Print_Cache_Statistics_for_one_cache(CDS *cds)
{
fprintf(stdout, "%s: %d entries of lines of %d bytes; %s, %s, %s\n",
cds->name, cds->number_of_cache_entries, cds->cache_line_size,
print_sets_and_ways(cds),
cds->write_back ? "write-back" : "write-thru",
CRP_name(cds));
fprintf(stdout, " %d addresses (%d %s, %d %s, %d %s)\n",
cds->number_of_memory_reference,
cds->number_of_type[MAT_FETCH], memory_reference_type_name(MAT_FETCH),
cds->number_of_type[MAT_LOAD], memory_reference_type_name(MAT_LOAD),
cds->number_of_type[MAT_STORE], memory_reference_type_name(MAT_STORE));
fprintf(stdout, " %d hits, %d misses, %d memory reads, %d memory writes\n",
cds->number_cache_hits, cds->number_cache_misses,
cds->number_memory_reads, cds->number_memory_writes);
fprintf(stdout, "\n");
}
void Print_Cache_Statistics(void)
{
CDS *cds = CDS_root;
while (cds != NULL)
{
Print_Cache_Statistics_for_one_cache(cds);
cds = cds->next;
}
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
void init_cache(CDS *cds)
{
/* we need one cache line for every entry */
cds->c = calloc(cds->number_of_cache_entries, sizeof(cache_line));
}
void Init_Caches(void)
{
CDS *cds = CDS_root;
while (cds != NULL)
{
init_cache(cds);
cds = cds->next;
}
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
void init_cache_for_trace(CDS *cds)
{
int i;
for (i = 0; i < NUMBER_OF_MEMORY_ACCESS_TYPE; i++) cds->number_of_type[i] = 0;
cds->number_of_memory_reference = 0;
cds->number_cache_hits = 0;
cds->number_cache_misses = 0;
cds->number_memory_reads = 0;
cds->number_memory_writes = 0;
}
void Init_caches_for_trace(void)
{
CDS *cds = CDS_root;
while (cds != NULL)
{
init_cache_for_trace(cds);
cds = cds->next;
}
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
void delete_cache(CDS *cds)
{
/* we need one cache line for every entry */
free(cds->c);
free(cds->name);
free(cds);
}
void Delete_Caches(void)
{
CDS *cds = CDS_root;
while (cds != NULL)
{
CDS *old = cds;
cds = cds->next;
delete_cache(old);
}
}