Skip to content

Commit f3eac30

Browse files
committed
Cache Builder design pattern implemented
1 parent 45b92e1 commit f3eac30

File tree

6 files changed

+80
-31
lines changed

6 files changed

+80
-31
lines changed

CacheSimulation/Cache.cs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,64 +57,63 @@ public class Cache
5757
public int SetIndexLength { get; set; } = 0;
5858
public CacheConfiguration CacheConfig { get; set; }
5959

60-
public readonly int NumberOfSets;
61-
public readonly int SetSize;
60+
public int NumberOfSets;
61+
public int SetSize;
6262

6363
public string RamFileName { get; set; }
6464
public string TraceFileName { get; set; }
6565

6666
private List<int> fifoIndexQueue { get; set; }
6767

68-
private readonly SecureRandom csprng;
68+
private SecureRandom csprng;
6969

7070
/// <summary>
7171
/// Index of the latest cache entry.
7272
/// </summary>
7373
private int lifoIndex { get; set; }
7474

75-
public Cache((string ramFileName, int size, int associativity) cacheInfo, CacheConfiguration config)
75+
public Cache(string ramFileName, CacheConfiguration config)
7676
{
77-
if (config.BlockSize >= cacheInfo.size)
77+
RamFileName = ramFileName;
78+
CacheConfig = config;
79+
}
80+
81+
public void CreateCache()
82+
{
83+
if (CacheConfig.BlockSize >= Size)
7884
{
79-
throw new Exception($"Size of the cache line ({config.BlockSize} B) can't be larger than the total cache size ({cacheInfo.size} B).");
85+
throw new Exception($"Size of the cache line ({CacheConfig.BlockSize} B) can't be larger than the total cache size ({Size} B).");
8086
}
8187

82-
RamFileName = cacheInfo.ramFileName;
83-
8488
// Explanation for this check implementation https://stackoverflow.com/questions/2751593/how-to-determine-if-a-decimal-double-is-an-integer .
85-
if (!CheckNumberForPowerOfTwo(config.BlockSize))
89+
if (!CheckNumberForPowerOfTwo(CacheConfig.BlockSize))
8690
{
8791
throw new Exception("Block size is not a power of 2.");
8892
}
89-
else if (!CheckNumberForPowerOfTwo(cacheInfo.associativity))
93+
else if (!CheckNumberForPowerOfTwo(Associativity))
9094
{
9195
throw new Exception("Associativity is not a power of 2.");
9296
}
9397

94-
Size = cacheInfo.size;
95-
96-
// Add cache config information.
97-
CacheConfig = config;
98-
9998
NumberOfLines = Size / CacheConfig.BlockSize;
10099

101-
if (cacheInfo.associativity > NumberOfLines)
100+
if (Associativity > NumberOfLines)
102101
{
103-
throw new Exception($"The cache with {NumberOfLines}-lines can't be {cacheInfo.associativity}-way set-associative.");
102+
throw new Exception($"The cache with {NumberOfLines}-lines can't be {Associativity}-way set-associative.");
104103
}
105104

106-
SetSize = Associativity = cacheInfo.associativity;
105+
SetSize = Associativity;
107106

108107
NumberOfSets = Size / (SetSize * CacheConfig.BlockSize);
109108
BlockOffsetLength = (int)Math.Ceiling(Math.Log(CacheConfig.BlockSize, 2));
110109
SetIndexLength = (int)Math.Ceiling(Math.Log(Size / (SetSize * CacheConfig.BlockSize), 2));
111110

112111

113-
if (config.ReplacementPolicy == ReplacementPolicy.FirstInFirstOut)
112+
if (CacheConfig.ReplacementPolicy == ReplacementPolicy.FirstInFirstOut)
114113
{
115114
fifoIndexQueue = new List<int>();
116115
}
117-
else if (config.ReplacementPolicy == ReplacementPolicy.RandomReplacement)
116+
else if (CacheConfig.ReplacementPolicy == ReplacementPolicy.RandomReplacement)
118117
{
119118
csprng = new(new DigestRandomGenerator(new Sha256Digest()));
120119
csprng.SetSeed(DateTime.Now.Ticks);
@@ -153,7 +152,7 @@ public Instruction TraceLineParser(string line)
153152
}
154153
}
155154

156-
public void CreateColdCache()
155+
private void CreateColdCache()
157156
{
158157
CacheEntries = new List<CacheEntry>();
159158
for (var i = 0; i < NumberOfLines; ++i)

CacheSimulation/CacheBuilder.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace CacheSimulation
2+
{
3+
public class CacheBuilder : ICacheBuilder
4+
{
5+
private readonly string ramFileName;
6+
private readonly CacheConfiguration config;
7+
private int size;
8+
private int associativity;
9+
10+
public CacheBuilder(string ramFileName, CacheConfiguration config)
11+
{
12+
this.ramFileName = ramFileName;
13+
this.config = config;
14+
}
15+
16+
public void Size(int size)
17+
{
18+
this.size = size;
19+
}
20+
21+
public void Associativity(int associativity)
22+
{
23+
this.associativity = associativity;
24+
}
25+
26+
public Cache Build()
27+
{
28+
var cache = new Cache(ramFileName, config)
29+
{
30+
Size = size,
31+
Associativity = associativity
32+
};
33+
cache.CreateCache();
34+
35+
return cache;
36+
}
37+
}
38+
}

CacheSimulation/CacheSimulation.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
</ItemGroup>
4747
<ItemGroup>
4848
<Compile Include="Cache.cs" />
49+
<Compile Include="CacheBuilder.cs" />
4950
<Compile Include="CacheConfiguration.cs" />
5051
<Compile Include="CacheConfigurationBuilder.cs" />
5152
<Compile Include="CacheEntry.cs" />

CacheSimulator/CPU.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ public class CPU
77
{
88
private List<CpuCore> cores { get; set; }
99

10-
public CPU((string ramFileName, int size, int associativity) cacheInfo, CacheConfiguration config, int numberOfCores)
10+
public CPU(CacheBuilder cacheBuilder, int numberOfCores)
1111
{
1212
cores = new List<CpuCore>(numberOfCores);
1313

1414
//CPU cores initialization.
1515
for (var i = 0; i < numberOfCores; ++i)
1616
{
17-
cores.Add(new CpuCore(cacheInfo, config));
17+
cores.Add(new CpuCore(cacheBuilder));
1818
}
1919
}
2020

CacheSimulator/CpuCore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public class CpuCore
99
{
1010
private Cache L1d;
1111

12-
public CpuCore((string ramFileName, int size, int associativity) cacheInfo, CacheConfiguration config)
12+
public CpuCore(CacheBuilder cacheBuilder)
1313
{
14-
L1d = new Cache(cacheInfo, config);
14+
L1d = cacheBuilder.Build();
1515
}
1616

1717
public string ExecuteTraceLine(string traceLine, int traceIndex, int coreNumber)

CacheSimulator/MainWindow.xaml.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,17 @@ private async void StartSimulation(object sender, RoutedEventArgs e)
7575

7676
try
7777
{
78-
var size = Int32.Parse(cacheSize.Text);
79-
80-
var associativity = GetCacheAssociativity(size, GetCacheLineSize());
8178
var numberOfCores = GetNumberOfCores();
8279

8380
if (numberOfCores > 128)
8481
{
8582
numberOfCores = 128;
8683
}
8784

88-
// Build cache config information.
89-
var cacheConfigBuilder = GetCacheConfigBuilder();
85+
// Get cache builder.
86+
var cacheBuilder = GetCacheBuilder();
9087

91-
cpu = new CPU((ramFileFullPath, size, associativity), cacheConfigBuilder.Build(), numberOfCores);
88+
cpu = new CPU(cacheBuilder, numberOfCores);
9289

9390
logLines.Append($"Simulation {numberOfSimulation++}\n");
9491

@@ -220,6 +217,20 @@ private CacheConfigurationBuilder GetCacheConfigBuilder()
220217
return cacheConfigBuilder;
221218
}
222219

220+
private CacheBuilder GetCacheBuilder()
221+
{
222+
var cacheConfigBuilder = GetCacheConfigBuilder();
223+
var cacheBuilder = new CacheBuilder(ramFileFullPath, cacheConfigBuilder.Build());
224+
225+
var size = Int32.Parse(cacheSize.Text);
226+
var associativity = GetCacheAssociativity(size, GetCacheLineSize());
227+
228+
cacheBuilder.Size(size);
229+
cacheBuilder.Associativity(associativity);
230+
231+
return cacheBuilder;
232+
}
233+
223234
private void StopSimulation(object sender, RoutedEventArgs e)
224235
{
225236
if (!isRunning || isCancelRequested)

0 commit comments

Comments
 (0)