LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
Cache.m
1classdef Cache < StatefulNode
2 % Multi-level cache node with hit/miss class switching
3 %
4 % Models cache memory systems with multiple levels and replacement strategies.
5 %
6 % Copyright (c) 2012-2026, Imperial College London
7 % All rights reserved.
8
9 properties
10 cap;
11 schedPolicy;
12 schedStrategy;
13 replacestrategy;
14 popularity;
15 nLevels;
16 itemLevelCap;
17 items;
18 accessProb;
19 graph;
20 end
21
22 methods
23 %Constructor
24 function self = Cache(model, name, nitems, itemLevelCap, replStrat, graph)
25 % CACHE Create a Cache node instance
26 %
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
35 %
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.
39
40 self@StatefulNode(name);
41 if model.isMatlabNative()
42 if ~exist('itemLevelCap','var')
43 levels = 1;
44 end
45 classes = model.getClasses();
46 self.input = Buffer(classes);
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
53 self.accessProb = {};
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));
57 end
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
63 self.popularity = {};
64 self.setModel(model);
65 self.model.addNode(self);
66 if nargin<6
67 self.graph = [];
68 else
69 self.graph = graph;
70 end
71 elseif model.isJavaNative()
72 self.setModel(model);
73 if nargin<6 || isempty(graph)
74 self.obj = jline.lang.nodes.Cache(model.obj, name, nitems, itemLevelCap, replStrat);
75 else
76 self.obj = jline.lang.nodes.Cache(model.obj, name, nitems, itemLevelCap, replStrat, graph);
77 end
78 self.index = model.obj.getNodeIndex(self.obj);
79 end
80 end
81
82 % function setMissTime(self, distribution)
83 % SETMISSTIME(DISTRIBUTION)
84
85 % itemclass = self.items;
86 % self.server.serviceProcess{1, itemclass.index} = {[], ServiceStrategy.SD, distribution};
87 % end
88 %
89 % function setHitTime(self, distribution, level)
90 % SETHITTIME(DISTRIBUTION, LEVEL)
91
92 % itemclass = self.items;
93 % if ~exist('level','var')
94 % levels = 2:self.nLevels;
95 % else
96 % levels = level;
97 % end
98 % for level = levels
99 % self.server.serviceProcess{1+level, itemclass.index} = {[], ServiceStrategy.SD, distribution};
100 % end
101 % end
102
103 function self = reset(self)
104 % SELF = RESET()
105 %
106 % Reset internal data structures when the network model is
107 % reset
108 self.server.actualHitProb = sparse([]);
109 self.server.actualMissProb = sparse([]);
110 end
111
112 function self = setResultHitProb(self, actualHitProb)
113 self.server.actualHitProb = actualHitProb;
114 end
115
116 function self = setResultMissProb(self, actualMissProb)
117 self.server.actualMissProb = actualMissProb;
118 end
119
120 function p = getHitRatio(self)
121 p = full(self.server.actualHitProb);
122 end
123
124 function p = getMissRatio(self)
125 p = full(self.server.actualMissProb);
126 end
127
128 function setHitClass(self, jobinclass, joboutclass)
129 % SETHITCLASS(JOBINCLASS, JOBOUTCLASS)
130
131 self.server.hitClass(jobinclass.index) = joboutclass.index;
132 end
133
134 function setMissClass(self, jobinclass, joboutclass)
135 % SETMISSCLASS(JOBINCLASS, JOBOUTCLASS)
136
137 self.server.missClass(jobinclass.index) = joboutclass.index;
138 end
139
140
141 function setRead(self, jobclass, distribution)
142 % SETREAD(JOBCLASS, DISTRIBUTION)
143
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));
149 end
150 switch class(distribution)
151 case 'Zipf'
152 self.popularity{itemclass.index, jobclass.index}.setParam(2, 'n', itemclass.nitems);
153 end
154 % self.probselect(itemclass.index, jobclass.index) = probselect;
155 else
156 line_error(mfilename,'A discrete popularity distribution is required.');
157 end
158 end
159
160 function setReadItemEntry(self, jobclass, popularity, cardinality)
161 % SETREAD(JOBCLASS, DISTRIBUTION)
162
163 if popularity.isDiscrete
164
165 self.popularity{jobclass.index} = popularity.copy;
166 switch class(popularity)
167 case 'Zipf'
168 self.popularity{jobclass.index}.setParam(2, 'n', cardinality);
169 end
170
171 else
172 line_error(mfilename,'A discrete popularity distribution is required.');
173 end
174 end
175 function setAccessProb(self, R)
176 % SETACCESSCOSTS(R)
177
178 self.accessProb = R;
179 end
180
181
182 function setProbRouting(self, class, destination, probability)
183 % SETPROBROUTING(CLASS, DESTINATION, PROBABILITY)
184
185 setRouting(self, class, RoutingStrategy.PROB, destination, probability);
186 end
187
188 function hitClass = getHitClass(self)
189 % HITCLASS = GETHITCLASS
190 %
191 % For an incoming job of class r, HITCLASS(r) is the new class
192 % of that job after a hit
193
194 hitClass = self.server.hitClass;
195 end
196
197 function missClass = getMissClass(self)
198 % MISSCLASS = GETMISSCLASS
199 %
200 % For an incoming job of class r, MISSCLASS(r) is the new class
201 % of that job after a miss
202
203 missClass = self.server.missClass;
204 end
205 end
206end