Repository navigation
Expand file tree
/
Copy pathCPU.cs
More file actions
111 lines (95 loc) · 3.39 KB
/
Copy pathCPU.cs
File metadata and controls
111 lines (95 loc) · 3.39 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
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace TraceDrivenSimulation
{
/// <summary>
/// CPUクラス
/// </summary>
class CPU
{
/// <summary>
/// バスに流れるメッセージ
/// </summary>
public enum BusMessage
{
/// <summary>
/// No Transaction
/// </summary>
None,
/// <summary>
/// Read ミス
/// </summary>
ReadMiss,
/// <summary>
/// Write ミス
/// </summary>
WriteMiss,
/// <summary>
/// Invalidate
/// </summary>
Invalidation,
}
List<Processor> _processors; // プロセッサ(コア)
Protocol _protocol; // キャッシュ制御プロトコル
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="core_num">プロセッサ(コア)数</param>
/// <param name="protocol">キャッシュのプロトコル</param>
public CPU(int core_num, Cache.Protocol protocol)
{
_processors = new List<Processor>(core_num);
for (int i = 0; i < core_num; ++i)
_processors.Add(new Processor(protocol));
switch (protocol)
{
case Cache.Protocol.ThreeStateBasic:
_protocol = new ThreeStateBasicProtocol(_processors);
break;
case Cache.Protocol.Berkley:
_protocol = new BerkleyProtocol(_processors);
break;
default:
throw new NotImplementedException();
}
}
/// <summary>
/// Trace-driven Simulationを行う
/// PCA特論 第3回課題用
/// </summary>
/// <param name="fileName">simulate対象ファイル名</param>
/// <param name="title">結果表示タイトル</param>
public void Simulate(string fileName, string title)
{
string line;
string[] elements;
var separator = new[] { ' ' };
int PU; // プロセッサ番号
string rw, address;
using (var sr = new StreamReader(fileName)) // NOTE: 簡単のためファイルの例外は考えない
{
while (!string.IsNullOrEmpty(line = sr.ReadLine()))
{
elements = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
if (elements.Length < 4)
continue;
PU = int.Parse(elements[1]);
rw = elements[2];
address = elements[3].HexToBinary(32);
if (rw == "r")
_protocol.Read(address, PU);
else
_protocol.Write(address, PU);
}
}
// 結果表示
Console.WriteLine(title);
for (int i = 0; i < _processors.Count; ++i)
System.Console.WriteLine("Processor[" + (i+1) + "] " + "Miss rate : " + _processors[i].Cache.MissRate * 100 + " %");
System.Console.WriteLine("Write-back count : " + _protocol.WriteBackCount);
System.Console.WriteLine("");
}
}
}