-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.cs
More file actions
308 lines (263 loc) · 9.62 KB
/
Cache.cs
File metadata and controls
308 lines (263 loc) · 9.62 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
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace TraceDrivenSimulation
{
/// <summary>
/// キャッシュデータのタグ
/// </summary>
class CacheTag
{
/// <summary>
/// タグ(アドレス上位ビット)
/// </summary>
public string Tag { get; set; }
/// <summary>
/// 状態
/// </summary>
public int State { get; set; }
/// <summary>
/// LRU用カウンタ
/// </summary>
public int Counter { get; set; }
}
/// <summary>
/// キャッシュライン
/// </summary>
/// <remarks>キャッシュするデータにタグをつけたもの</remarks>
class Line
{
/// <summary>
/// データ(今回は不使用)
/// 32Byte
/// </summary>
public object Data { get; set; }
/// <summary>
/// キャッシュのタグ
/// </summary>
public CacheTag CacheTag { get; set; }
}
/// <summary>
/// ラインのLRUセット
/// </summary>
/// <remarks>LRUは簡易実装</remarks>
class LineSetLRU
{
private int _setSize;
private List<Line> _lineSet;
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="setSize">セットに含まれるラインの数</param>
public LineSetLRU(int setSize)
{
_setSize = setSize;
_lineSet = new List<Line>(setSize);
}
/// <summary>
/// インデクサ
/// </summary>
public Line this[int index]
{
get { return _lineSet[index]; }
}
/// <summary>
/// タグが等しいラインを取得
/// </summary>
public Line GetLineByTag(string tag)
{
var lines = _lineSet.Where(l => l.CacheTag.Tag == tag);
return (lines.Count() == 0) ? null : lines.First();
}
/// <summary>
/// セットへ追加(LRU Policy)
/// </summary>
/// <returns>追い出しがあったら状態, なかったら-1</returns>
public int Push(Line line)
{
if (_lineSet.Any(l => l.CacheTag.Tag == line.CacheTag.Tag)) // 同タグのラインが存在する
{
// 同タグのラインのデータを更新
// 状態はここでは変更しない(Cache.SetStateメソッドからのみ変更させる)
var updateLineIndex = _lineSet.FindIndex(l => l.CacheTag.Tag == line.CacheTag.Tag);
_lineSet[updateLineIndex].Data = line.Data;
// LRU用カウンタ設定
_lineSet[updateLineIndex].CacheTag.Counter = 0;
_lineSet.Where(l => l.CacheTag.Tag != line.CacheTag.Tag)
.ForEach(l => l.CacheTag.Counter++);
// カウント順に並び替える(常にカウント順に並べておく)
_lineSet = _lineSet.OrderByDescending(l => l.CacheTag.Counter).ToList();
}
else
{
// セットに含まれるラインの数で, 追加か追い出しかを決定
if (_lineSet.Count < _setSize)
{
foreach (var l in _lineSet)
l.CacheTag.Counter++;
line.CacheTag.Counter = 0;
_lineSet.Add(line);
}
else
{
var envictState = _lineSet[0].CacheTag.State;
_lineSet.RemoveAt(0); // 先頭要素を削除
foreach (var l in _lineSet)
l.CacheTag.Counter++;
line.CacheTag.Counter = 0;
_lineSet.Add(line);
return envictState; // 追い出したラインの状態を返す
}
}
return -1;
}
}
/// <summary>
/// 読み込み後のメッセージ
/// </summary>
class ReadContent
{
/// <summary>
/// バスへのメッセージ
/// </summary>
public CPU.BusMessage BusMessage { get; set; }
/// <summary>
/// キャッシュへの書き込みでWriteBackが生じたか
/// </summary>
public bool WriteBacked { get; set; }
}
/// <summary>
/// キャッシュメモリ
/// Mappingは4-wayのSetAssociative固定
/// </summary>
abstract class Cache
{
/// <summary>
/// キャッシュメモリの動作方式
/// </summary>
public enum Protocol
{
/// <summary>
/// 3-State Basic プロトコル
/// </summary>
ThreeStateBasic,
/// <summary>
/// Berkley プロトコル
/// </summary>
Berkley,
}
/// <summary>
/// キャッシュするブロック
/// </summary>
protected List<LineSetLRU> _lineDatas;
/// <summary>
/// Way数 N-Way Set Associative
/// </summary>
public int Way { get; } = 4;
/// <summary>
/// キャッシュメモリサイズ
/// </summary>
public int Size { get; }
/// <summary>
/// ブロックサイズ (とりあえず32Byte固定)
/// </summary>
public int BlockSize { get; } = (int)Math.Pow(2,5);
private int _accessCount = 0;
protected int _hitCount = 0;
/// <summary>
/// キャッシュのヒット率
/// </summary>
public double HitRate { get { return (double)_hitCount / _accessCount; } }
/// <summary>
/// キャッシュのミス率
/// </summary>
public double MissRate { get { return 1 - this.HitRate; } }
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="size">キャッシュサイズ</param>
public Cache(int size)
{
this.Size = size;
// キャッシュの行サイズ
int index_num = this.Size / this.BlockSize / this.Way;
_lineDatas = new List<LineSetLRU>(index_num);
for (int i = 0; i < index_num; ++i)
_lineDatas.Add(new LineSetLRU(this.Way));
}
/// <summary>
/// 対象のラインの状態を取得する
/// </summary>
/// <returns>-1:対象ラインは存在しない</returns>
public int GetState(string tag, int index)
{
var line = _lineDatas[index].GetLineByTag(tag);
return (line != null) ? line.CacheTag.State : -1;
}
/// <summary>
/// 対象のラインの状態がどれかの状態と一致しているか
/// </summary>
public bool AnyState(string tag, int index, int stateFlags)
{
var line = _lineDatas[index].GetLineByTag(tag);
if (line == null)
return false;
return line.CacheTag.State == (stateFlags & line.CacheTag.State);
}
/// <summary>
/// 対象のラインの状態を書き換える
/// </summary>
public void SetState(string tag, int index, int state)
{
var line = _lineDatas[index].GetLineByTag(tag);
if (line != null)
line.CacheTag.State = state;
}
/// <summary>
/// ラインを持っているか
/// </summary>
public bool HasLine(string tag, int index)
{
return (_lineDatas[index].GetLineByTag(tag) == null) ? false : true;
}
/// <summary>
/// ラインのデータを取得
/// </summary>
public object GetLineData(string tag, int index)
{
var line = _lineDatas[index].GetLineByTag(tag);
return (line != null) ? line.Data : null;
}
/// <summary>
/// キャッシュから対象ブロックを読み込む
/// 今回の課題では実際にはデータを返さない
/// </summary>
public ReadContent Read(string tag, int index, int offset)
{
_accessCount++;
var targetLine = _lineDatas[index].GetLineByTag(tag);
return this.Read(targetLine, _lineDatas[index]);
}
/// <summary>
/// キャッシュへ対象ブロックを書き込む
/// </summary>
public CPU.BusMessage Write(object data, string tag, int index, int offset)
{
_accessCount++;
var targetLine = _lineDatas[index].GetLineByTag(tag);
return this.Write(data, targetLine, _lineDatas[index]);
}
/// <summary>
/// キャッシュから対象ブロックを読み込む abstract
/// </summary>
abstract protected ReadContent Read(Line targetLine, LineSetLRU targetSet);
/// <summary>
/// キャッシュへ対象ブロックを書き込む abstract
/// </summary>
abstract protected CPU.BusMessage Write(object data, Line targetLine, LineSetLRU targetSet);
/// <summary>
/// メモリ/キャッシュからの転送データを書き込む abstract
/// </summary>
abstract public bool Transfer(object data, string tag, int index);
}
}