1function [outspace, outrate, outprob, eventCache] = afterEventCache(sn, ind, event,
class, isSimulation, eventCache, R, space_buf, space_srv, space_var, key)
2% job arrives in
class, then reads and moves into hit or miss
10 space_srv(:,class) = space_srv(:,class) + 1;
11 outspace = [space_srv, space_var]; % buf
is empty
12 outrate = -1*ones(size(outspace,1)); % passive action, rate
is unspecified
15 space_srv(:,class) = space_srv(:,class) - 1;
16 switch sn.routing(ind,
class)
17 case RoutingStrategy.RROBIN
18 idx = find(space_var(sum(sn.nvars(ind,1:(R+class)))) == sn.nodeparam{ind}{
class}.outlinks);
19 if idx < length(sn.nodeparam{ind}{class}.outlinks)
20 space_var(sum(sn.nvars(ind,1:(R+class)))) = sn.nodeparam{ind}{
class}.outlinks(idx+1);
22 space_var(sum(sn.nvars(ind,1:(R+class)))) = sn.nodeparam{ind}{
class}.outlinks(1);
25 outspace = [space_srv, space_var]; % buf
is empty
26 outrate = GlobalConstants.Immediate*ones(size(outspace,1)); % immediate action
29 n = sn.nodeparam{ind}.nitems; % n items
30 m = sn.nodeparam{ind}.itemcap; % capacity
31 ac = sn.nodeparam{ind}.accost; % access cost
32 hitclass = sn.nodeparam{ind}.hitclass;
33 missclass = sn.nodeparam{ind}.missclass;
35 replacement_id = sn.nodeparam{ind}.replacestrat;
36 if space_srv(
class)>0 && sum(space_srv)==1 % a job of
class is in
37 p = sn.nodeparam{ind}.pread{
class};
38 en = space_srv(:,class) > 0;
46 kset = 1 + max([0,find( rand > cumsum(p) )]);
47 % pick one entry list for cache miss
48 % do not move this entry
49 l = 1 + max([0,find( rand > cumsum(ac{class,kset}(1,:)) )]);
53 for k=kset % request to item k
54 space_srv_e = space_srv(e,:);
55 space_srv_e(class) = space_srv_e(class) - 1;
57 posk = find(k==var,1,'first
');
59 if isempty(posk) % CACHE MISS, can enter any list based on accessCost
60 space_srv_e(missclass(class)) = space_srv_e(missclass(class)) + 1;
62 case {ReplacementStrategy.FIFO, ReplacementStrategy.LRU, ReplacementStrategy.SFIFO}
64 listidx = l - 1; % l is accessCost column index, listidx is actual list (1-indexed)
65 if listidx > 0 % only cache if listidx is valid (l >= 2)
67 varp(cpos(listidx,2):cpos(listidx,m(listidx))) = var(cpos(listidx,1):cpos(listidx,m(listidx)-1));
68 varp(cpos(listidx,1)) = k; % head of list listidx
69 space_srv_k = [space_srv_k; space_srv_e];
70 space_var_k = [space_var_k; varp];
71 %% no p(k) weighting since that goes in the outprob vec
72 outrate(end+1,1) = GlobalConstants.Immediate;
73 outprob(end+1,1) = ac{class,k}(1,l) * p(k);
75 % Cache reject (l=1): pass through without caching
76 space_srv_k = [space_srv_k; space_srv_e];
77 space_var_k = [space_var_k; var];
78 outrate(end+1,1) = GlobalConstants.Immediate;
79 outprob(end+1,1) = ac{class,k}(1,l) * p(k);
82 for l=2:(h+1) % iterate over all possible target lists (columns 2 to h+1)
83 listidx = l - 1; % l is accessCost column index, listidx is actual list (1-indexed)
85 varp(cpos(listidx,2):cpos(listidx,m(listidx))) = var(cpos(listidx,1):cpos(listidx,m(listidx)-1));
86 varp(cpos(listidx,1)) = k; % head of list listidx
87 space_srv_k = [space_srv_k; space_srv_e];
88 space_var_k = [space_var_k; varp];
89 outrate(end+1,1) = ac{class,k}(1,l) * p(k) * GlobalConstants.Immediate;
93 case ReplacementStrategy.RR
95 listidx = l - 1; % l is accessCost column index, listidx is actual list (1-indexed)
96 if listidx > 0 % only cache if listidx is valid (l >= 2)
98 r = randi(m(listidx),1,1);
99 varp(cpos(listidx,r)) = k;
100 space_srv_k = [space_srv_k; space_srv_e];
101 space_var_k = [space_var_k; (varp)];
102 outrate(end+1,1) = GlobalConstants.Immediate;
103 outprob(end+1,1) = ac{
class,k}(1,l) * p(k);
105 % Cache reject (l=1): pass through without caching
106 space_srv_k = [space_srv_k; space_srv_e];
107 space_var_k = [space_var_k; var];
108 outrate(end+1,1) = GlobalConstants.Immediate;
109 outprob(end+1,1) = ac{
class,k}(1,l) * p(k);
112 for l=2:(h+1) % iterate over all possible target lists
113 listidx = l - 1; % l
is accessCost column index, listidx
is actual list (1-indexed)
114 for r=1:m(listidx) % random position in list listidx
116 varp(cpos(listidx,r)) = k;
117 space_srv_k = [space_srv_k; space_srv_e];
118 space_var_k = [space_var_k; (varp)];
119 outrate(end+1,1) = ac{
class,k}(1,l) * p(k)/m(listidx) * GlobalConstants.Immediate;
124 elseif posk <= sum(m(1:h-1)) % CACHE HIT in list i < h, move to list i+1
125 space_srv_e(hitclass(class)) = space_srv_e(hitclass(class)) + 1;
126 i = min(find(posk <= cumsum(m)));
127 j = posk - sum(m(1:i-1));
129 switch replacement_id
130 case ReplacementStrategy.FIFO
133 inew = i+probchoose(ac{
class,k}(1+i,(1+i):end)/sum(ac{class,k}(1+i,(1+i):end)))-1; % can choose i
135 varp(cpos(i,j)) = var(cpos(inew,m(inew)));
136 varp(cpos(inew,2):cpos(inew,m(inew))) = var(cpos(inew,1):cpos(inew,m(inew)-1));
137 varp(cpos(inew,1)) = k;
139 %varp(cpos(i,j)) = var(cpos(i+1,m(i+1)));
140 %varp(cpos(i+1,2):cpos(i+1,m(i+1))) = var(cpos(i+1,1):cpos(i+1,m(i+1)-1));
141 %varp(cpos(i+1,1)) = k;
143 space_srv_k = [space_srv_k; space_srv_e];
144 space_var_k = [space_var_k; varp];
145 outrate(end+1,1) = GlobalConstants.Immediate;
146 outprob(end+1,1) = ac{
class,k}(1+i,1+inew) * p(k);
150 varp(cpos(i,j)) = var(cpos(inew,m(inew)));
151 varp(cpos(inew,2):cpos(inew,m(inew))) = var(cpos(inew,1):cpos(inew,m(inew)-1));
152 varp(cpos(inew,1)) = k;
153 space_srv_k = [space_srv_k; space_srv_e];
154 space_var_k = [space_var_k; varp];
155 outrate(end+1,1) = ac{
class,k}(1+i,1+inew) * p(k) * GlobalConstants.Immediate;
158 case ReplacementStrategy.RR
160 inew = i+probchoose(ac{
class,k}(1+i,(1+i):end)/sum(ac{class,k}(1+i,(1+i):end)))-1; % can choose i
162 r = randi(m(inew),1,1);
163 varp(cpos(i,j)) = var(cpos(inew,r));
164 varp(cpos(inew,r)) = k;
165 space_srv_k = [space_srv_k; space_srv_e];
166 space_var_k = [space_var_k; varp];
167 outrate(end+1,1) = GlobalConstants.Immediate;
168 outprob(end+1,1) = ac{
class,k}(1+i,1+inew) * p(k)/m(inew);
171 for r=1:m(inew) % random position in
new list
173 varp(cpos(i,j)) = var(cpos(inew,r));
174 varp(cpos(inew,r)) = k;
175 space_srv_k = [space_srv_k; space_srv_e];
176 space_var_k = [space_var_k; varp];
177 outrate(end+1,1) = ac{
class,k}(1+i,1+inew) * p(k)/m(inew) * GlobalConstants.Immediate;
181 case {ReplacementStrategy.LRU, ReplacementStrategy.SFIFO}
184 inew = i+probchoose(ac{
class,k}(1+i,(1+i):end)/sum(ac{class,k}(1+i,(1+i):end)))-1; % can choose i
185 varp(cpos(i,2):cpos(i,j)) = var(cpos(i,1):cpos(i,j-1));
186 varp(cpos(i,1)) = var(cpos(inew,m(inew)));
187 varp(cpos(inew,2):cpos(inew,m(inew))) = var(cpos(inew,1):cpos(inew,m(inew)-1));
188 varp(cpos(inew,1)) = k;
189 space_srv_k = [space_srv_k; space_srv_e];
190 space_var_k = [space_var_k; varp];
191 outrate(end+1,1) = GlobalConstants.Immediate;
192 outprob(end+1,1) = ac{
class,k}(1+i,1+inew) * p(k);
196 varp(cpos(i,2):cpos(i,j)) = var(cpos(i,1):cpos(i,j-1));
197 varp(cpos(i,1)) = var(cpos(inew,m(inew)));
198 varp(cpos(inew,2):cpos(inew,m(inew))) = var(cpos(inew,1):cpos(inew,m(inew)-1));
199 varp(cpos(inew,1)) = k;
200 space_srv_k = [space_srv_k; space_srv_e];
201 space_var_k = [space_var_k; varp];
202 outrate(end+1,1) = ac{
class,k}(1+i,1+inew) * p(k) * GlobalConstants.Immediate;
206 else % CACHE HIT in list h
207 space_srv_e(hitclass(
class)) = space_srv_e(hitclass(
class)) + 1;
209 j = posk - sum(m(1:i-1));
210 switch replacement_id
211 case {ReplacementStrategy.RR, ReplacementStrategy.FIFO, ReplacementStrategy.SFIFO}
212 space_srv_k = [space_srv_k; space_srv_e];
213 space_var_k = [space_var_k; var];
215 outrate(end+1,1) = GlobalConstants.Immediate;
216 outprob(end+1,1) = p(k);
218 outrate(end+1,1) = p(k) * GlobalConstants.Immediate;
220 case ReplacementStrategy.LRU
222 varp(cpos(h,2):cpos(h,j)) = var(cpos(h,1):cpos(h,j-1));
223 varp(cpos(h,1)) = var(cpos(h,j));
224 space_srv_k = [space_srv_k; space_srv_e];
225 space_var_k = [space_var_k; varp];
227 outrate(end+1,1) = GlobalConstants.Immediate;
228 outprob(end+1,1) = p(k);
230 outrate(end+1,1) = p(k) * GlobalConstants.Immediate;
236 %
if state
is unchanged, still add with rate 0
237 outspace = [space_srv_k, space_var_k];
241 function pos = cpos(i,j)
244 pos = sum(m(1:i-1)) + j;