dash_prefetch.c
7.59 KB
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
/*
* Copyright (C) 1996-1998 by the Board of Trustees
* of Leland Stanford Junior University.
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
/*****************************************************************
* dash_prefetch.c
*
* Since the f77 compiler cannot issue prefetch opcodes, we
* simulate the effect through the use of a shadow array.
* The parameters that determine the mapping between shadow
* and effective arrays are set by Tcl commands. This style of
* prefetch was commonly used on dash.
*
* The Tcl interface is as follows
* dashPrefetch enable
* dashPrefetch disable
* dashPrefetch (shared|exlcusive) baseAddr shadowAddr
*****************************************************************/
#include <string.h>
#include "tcl_init.h"
#include "cpu_interface.h"
#include "hw_events.h"
#include "sim_error.h"
#include "memref.h"
#include "cp0.h"
#include "cpu.h"
#include "cpu_stats.h"
#define DASH_MAXENTRIES 512
#define DASH_ENCODING 10000
#define PF_NOTMAPPED (PF_NUMSTATS)
static struct DashPrefetchState {
VA shadowStart;
VA shadowEnd;
int arrayEntries;
int except;
VA arrayShadowStart[DASH_MAXENTRIES];
VA arrayStart[DASH_MAXENTRIES];
int64 issued[DASH_MAXENTRIES];
int64 stat[DASH_MAXENTRIES][PF_NUMSTATS+1];
SimTime nextOutput;
} dashState;
int dashPrefetchEnabled = 0;
/*****************************************************************
* prefetch command
*****************************************************************/
int
DashPrefetch(int cpuNum, VA shadowAddr, void *vPtr, int size, int *retVal)
{
uint offset;
int ret, id, typeSize;
VA vAddr;
PA pAddr=0;
int exclusive;
uint tlbFlavor;
void *bdoorAddr = 0;
if (simosCPUType != MIPSY) {
return 0;
}
if (shadowAddr < dashState.shadowStart ||
shadowAddr >= dashState.shadowEnd) {
return 0; /* no prefetch */
}
/*
* now, size should always be 4
*/
ASSERT (size==4);
typeSize = (*(int*)vPtr) / DASH_ENCODING;
id = (*(int*)vPtr) % DASH_ENCODING;
ASSERT (typeSize==4 || typeSize==8);
exclusive = (id%2);
id = id / 2;
/*
* debug stats
*/
if (CPUVec.CycleCount(cpuNum) >= dashState.nextOutput) {
int i,j;
CPUPrint("\n\n PREFETCH STATS at %lld cycles \n", CPUVec.CycleCount(cpuNum));
for (i=0;i<DASH_MAXENTRIES;i++) {
if (dashState.issued[i]) {
double x[PF_NUMSTATS+1];
for (j=0; j<PF_NUMSTATS+1; j++) {
x[j] = 100.0 * dashState.stat[i][j]
/ dashState.issued[i];
}
CPUPrint("PREFETCH id=%3d addr=0x%08x Issued=%10lld res=%6.2f%% merg=%6.2f%% fetch=%6.2f%% failure=%6.2f%% upg=%6.2f%% notmapped=%6.2f%%\n",
i,
dashState.arrayStart[i],
dashState.issued[i],
x[ PF_RESIDENT],x[PF_MERGE],x[PF_STALL],
x[PF_FAILURE],x[PF_UPGRADE],x[PF_NOTMAPPED]);
}
}
dashState.nextOutput += 32* 1024 * 1024 ;
}
ASSERT (id >= 0 && id < DASH_MAXENTRIES);
if (!dashState.arrayStart[id]) {
CPUError("Shadow entry %d not defined \n",id);
}
offset = shadowAddr - dashState.arrayShadowStart[id];
if (typeSize==8) {
offset *=2;
}
vAddr = dashState.arrayStart[id] + offset;
PREFETCH_ISSUE_EVENT(cpuNum,PE[cpuNum].PC,vAddr);
dashState.issued[id]++;
if (dashState.except) {
tlbFlavor = TLB_DFETCH;
} else {
tlbFlavor = TLB_DFETCH | TLB_PREFETCH;
}
tlbFlavor = TLB_DFETCH | TLB_PREFETCH;
ret = TranslateVirtual(&PE[cpuNum], vAddr, &pAddr, &tlbFlavor,&bdoorAddr);
if (ret == SUCCESS) {
if (exclusive) {
if (MemRefPrefetch(cpuNum, vAddr, pAddr, 1)) {
ret = PF_FAILURE;
} else {
ret = PF_SUCCESS;
}
} else {
if (MemRefPrefetch(cpuNum, vAddr, pAddr, 0)) {
ret = PF_FAILURE;
} else {
ret = PF_SUCCESS;
}
}
if (interest(pAddr)) {
DebugDetail('g', "dash-pf",cpuNum,
"pAddr=0x%x vAddr=0x%x, exclusive=%d ret=%d \n",
pAddr,vAddr,exclusive,ret);
}
dashState.stat[id][ret]++;
if (ret == PF_FAILURE) {
/* Think of a way to do this commonly to all cpus. */
STATS_INC(cpuNum, prefStats.prefFails, 1);
PE[cpuNum].cpuStatus = cpu_stalled;
PE[cpuNum].stalledInst = 0;
STATS_SET(cpuNum, stallStart, CPUVec.CycleCount(cpuNum));
} else {
ret = SUCCESS;
}
} else {
if (!dashState.except) {
ret = SUCCESS;
}
dashState.stat[id][PF_NOTMAPPED]++;
PREFETCH_FAILED_TRANS(cpuNum,PE[cpuNum].PC,vAddr);
}
*retVal = ret;
return 1; /* was a prefetch, ignore store instruction */
}
/*****************************************************************
* Tcl interface to dash_prefetch
*****************************************************************/
int
DashTclCmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{
int x[3];
if (simosCPUType != MIPSY) {
Tcl_AppendResult(interp,
"Can only use dashPrefetch in mipsy\n",
NULL);
return TCL_ERROR;
}
if (argc < 2) {
Tcl_AppendResult(interp,
"Use dashPrefetch [enable|disable|shadow|define|exception] \n",
NULL);
return TCL_ERROR;
}
if (!strcmp(argv[1], "enable")) {
if (dashState.shadowStart < dashState.shadowEnd) {
dashPrefetchEnabled = 1;
return TCL_OK;
} else {
Tcl_AppendResult(interp,
"dashPrefetch not initialized \n", NULL);
return TCL_ERROR;
}
}
if (!strcmp(argv[1],"shadow")) {
if (argc<4) {
Tcl_AppendResult(interp,
"Use dashPrefetch shadow start size \n", NULL);
return TCL_ERROR;
}
if (Tcl_GetInt(interp, argv[2], &x[0]) != TCL_OK ||
Tcl_GetInt(interp, argv[3], &x[1]) != TCL_OK ) {
Tcl_AppendResult(interp,
"Use dashPrefetch shadow start end \n", NULL);
return TCL_ERROR;
}
dashState.shadowStart = x[0];
dashState.shadowEnd = x[0] + x[1];
return TCL_OK;
}
if (!strcmp(argv[1],"disable")) {
dashPrefetchEnabled = 0;
return TCL_OK;
}
if (!strcmp(argv[1],"exception")) {
dashState.except = 1;
CPUWarning("DASH PREFETCH: excepting on TLB misses \n");
return TCL_OK;
}
if (!strcmp(argv[1],"define")) {
if (argc < 5) {
Tcl_AppendResult(interp,
"Use dashPrefetch [define] id baseAddr shadowAddr \n", NULL);
return TCL_ERROR;
}
if (Tcl_GetInt(interp, argv[2], &x[0]) != TCL_OK ||
Tcl_GetInt(interp, argv[3], &x[1]) != TCL_OK ||
Tcl_GetInt(interp, argv[4], &x[2]) != TCL_OK ) {
Tcl_AppendResult(interp,
"Use dashPrefetch define id baseAddr shadowAddr \n", NULL);
return TCL_ERROR;
}
if (x[0] < 0 || x[0] >DASH_MAXENTRIES) {
Tcl_AppendResult(interp,
"dashPrefetch id must in 0-255 range \n", NULL);
return TCL_ERROR;
}
dashState.arrayStart[x[0]] = x[1];
dashState.arrayShadowStart[x[0]] = x[2];
return TCL_OK;
}
Tcl_AppendResult(interp,
"Use dashPrefetch [enable|disable|define] \n", NULL);
return TCL_ERROR;
}