1classdef Cache < StatefulNode
2 % Multi-level cache node with hit/miss
class switching
4 % Models cache memory systems with multiple levels and replacement strategies.
6 % Copyright (c) 2012-2026, Imperial College London
24 function self = Cache(model, name, nitems, itemLevelCap, replStrat, graph)
25 % CACHE Create a Cache node instance
27 % @brief Creates a Cache node with configurable levels and replacement strategy
28 % @param model Network model to add the cache to
29 % @param name String identifier
for the cache node
30 % @param nitems Total number of cacheable items
31 % @param itemLevelCap Vector specifying capacity of each cache level
32 % @param replStrat Replacement strategy (LRU, FIFO, Random, etc.)
33 % @param graph Optional graph structure
for cache hierarchy
34 % @
return self Cache instance configured
for the given model
36 % The constructor creates a multi-level cache with the specified
37 % total items and per-level capacities. The replacement strategy
38 % determines how items are evicted when cache levels become full.
40 self@StatefulNode(name);
41 if model.isMatlabNative()
42 if ~exist(
'itemLevelCap',
'var')
47 self.output = Dispatcher(
classes);
48 self.schedPolicy = SchedStrategyType.NP;
49 self.schedStrategy = SchedStrategy.FCFS;
50 self.items = ItemSet(model, [name,'_','Items'], nitems, self);
51 self.nLevels = nnz(itemLevelCap);
52 self.cap = Inf; % job capacity
54 self.itemLevelCap = itemLevelCap; % item capacity
55 if sum(itemLevelCap) > nitems
56 line_error(mfilename,sprintf(
'The number of items is smaller than the capacity of %s.',name));
58 self.replacestrategy = replStrat;
59 %probHit = min(sum(itemLevelCap)/nitems,1.0); % initial estimate of hit probability
60 %self.setResultHitProb(probHit);
61 %self.setResultMissProb(1-probHit);
62 self.server = CacheClassSwitcher(
classes, self.nLevels, itemLevelCap); % replace Server created by Queue
65 self.model.addNode(self);
71 elseif model.isJavaNative()
73 if nargin<6 || isempty(graph)
74 self.obj = jline.lang.nodes.Cache(model.obj, name, nitems, itemLevelCap, replStrat);
76 self.obj = jline.lang.nodes.Cache(model.obj, name, nitems, itemLevelCap, replStrat, graph);
78 self.index = model.obj.getNodeIndex(self.obj);
82 % function setMissTime(self, distribution)
83 % SETMISSTIME(DISTRIBUTION)
85 % itemclass = self.items;
86 % self.server.serviceProcess{1, itemclass.index} = {[], ServiceStrategy.SD, distribution};
89 % function setHitTime(self, distribution, level)
90 % SETHITTIME(DISTRIBUTION, LEVEL)
92 % itemclass = self.items;
93 %
if ~exist(
'level',
'var')
94 % levels = 2:self.nLevels;
99 % self.server.serviceProcess{1+level, itemclass.index} = {[], ServiceStrategy.SD, distribution};
103 function self = reset(self)
106 % Reset internal data structures when the network model
is
108 self.server.actualHitProb = sparse([]);
109 self.server.actualMissProb = sparse([]);
112 function self = setResultHitProb(self, actualHitProb)
113 self.server.actualHitProb = actualHitProb;
116 function self = setResultMissProb(self, actualMissProb)
117 self.server.actualMissProb = actualMissProb;
120 function p = getHitRatio(self)
121 p = full(self.server.actualHitProb);
124 function p = getMissRatio(self)
125 p = full(self.server.actualMissProb);
128 function setHitClass(self, jobinclass, joboutclass)
129 % SETHITCLASS(JOBINCLASS, JOBOUTCLASS)
131 self.server.hitClass(jobinclass.index) = joboutclass.index;
134 function setMissClass(self, jobinclass, joboutclass)
135 % SETMISSCLASS(JOBINCLASS, JOBOUTCLASS)
137 self.server.missClass(jobinclass.index) = joboutclass.index;
141 function setRead(self,
jobclass, distribution)
142 % SETREAD(JOBCLASS, DISTRIBUTION)
144 itemclass = self.items;
145 if distribution.isDiscrete
146 self.popularity{itemclass.index,
jobclass.index} = distribution.copy;
147 if self.popularity{itemclass.index,
jobclass.index}.support(2) ~= itemclass.nitems
148 line_error(mfilename,sprintf(
'The reference model is defined on a number of items different from the ones used to instantiate %s.',self.name));
150 switch class(distribution)
152 self.popularity{itemclass.index,
jobclass.index}.setParam(2,
'n', itemclass.nitems);
154 % self.probselect(itemclass.index,
jobclass.index) = probselect;
156 line_error(mfilename,
'A discrete popularity distribution is required.');
160 function setReadItemEntry(self,
jobclass, popularity, cardinality)
161 % SETREAD(JOBCLASS, DISTRIBUTION)
163 if popularity.isDiscrete
165 self.popularity{
jobclass.index} = popularity.copy;
166 switch class(popularity)
168 self.popularity{
jobclass.index}.setParam(2,
'n', cardinality);
172 line_error(mfilename,
'A discrete popularity distribution is required.');
175 function setAccessProb(self, R)
182 function setProbRouting(self,
class, destination, probability)
183 % SETPROBROUTING(CLASS, DESTINATION, PROBABILITY)
185 setRouting(self,
class, RoutingStrategy.PROB, destination, probability);
188 function hitClass = getHitClass(self)
189 % HITCLASS = GETHITCLASS
191 % For an incoming job of
class r, HITCLASS(r)
is the new class
192 % of that job after a hit
194 hitClass = self.server.hitClass;
197 function missClass = getMissClass(self)
198 % MISSCLASS = GETMISSCLASS
200 % For an incoming job of
class r, MISSCLASS(r)
is the new class
201 % of that job after a miss
203 missClass = self.server.missClass;