LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
cache_miss_sfifo_rmf.m
1%{ @file cache_miss_sfifo_rmf.m
2 % @brief Position-resolved mean-field miss rates for strict FIFO(m) caches
3 %
4 % @author LINE Development Team
5%}
6
7%{
8 % @brief Miss rates for strict FIFO(m) replacement via a position-resolved
9 % density-dependent population process (DDPP) mean field.
10 %
11 % @details
12 % Strict FIFO(m) is NOT equivalent to RANDOM(m)/FIFO(m). Gast and Van Houdt
13 % (SIGMETRICS 2015) prove pi_FIFO(m) = pi_RAND(m) exactly but show strict
14 % FIFO(m) differs and give it no mean-field model (only trace simulation).
15 % The difference is the within-list age ordering: on a hit in list i < h the
16 % demoted tail of list i+1 is reinserted at position 1 of list i (positions
17 % 1..j-1 shift back), which the per-item per-list occupancy of RANDOM(m)
18 % cannot express. This routine tracks x[k,i,j] = P(item k in position j of
19 % list i) with deterministic (age-based) demotion/eviction and returns the
20 % plain mean-field fixed point; it reduces to the RANDOM(m)/FIFO(m) result
21 % when m_1 = ... = m_{h-1} = 1 (the strict-FIFO == FIFO degeneracy).
22 %
23 % Strict FIFO(m) dynamics (aggregate IRM stream):
24 % - miss: insert missed item at position 1 of list 1; list 1 shifts back;
25 % tail (position m_1) is evicted.
26 % - hit at position j of list i < h: promote that item to position 1 of
27 % list i+1 (list i+1 shifts back, tail demoted); demoted tail enters
28 % position 1 of list i and positions 1..j-1 of list i shift back.
29 % - hit in the top list h: no change.
30 %
31 % Reference:
32 % N. Gast and B. Van Houdt, "Transient and Steady-state Regime of a Family
33 % of List-based Cache Replacement Algorithms", ACM SIGMETRICS 2015.
34 %
35 % @par Syntax:
36 % @code
37 % [M, MU, MI, pi0] = cache_miss_sfifo_rmf(gamma, m, lambda)
38 % @endcode
39%}
40function [M,MU,MI,pi0,tout,pi0_t,MU_t,xtraj] = cache_miss_sfifo_rmf(gamma, m, lambda, tspan, x0init, accost) %#ok<INUSL>
41if nargin < 6, accost = []; end
42if nargin < 4, tspan = []; end
43if nargin < 5, x0init = []; end
44u = size(lambda,1);
45n = size(lambda,2);
46h = length(m);
47m = m(:)';
48
49% aggregate per-item request rates over users
50lam_i = zeros(1, n);
51for v = 1:u
52 row = lambda(v,:,1);
53 row(~isfinite(row)) = 0;
54 lam_i = lam_i + row;
55end
56tot = sum(lam_i);
57if tot > 0, p = lam_i / tot; else, p = ones(1,n)/n; end
58
59% slot map: (list i, position j) -> flat slot index; item k occupies k*S+s
60slots = zeros(sum(m), 2);
61s = 0;
62for i = 1:h
63 for j = 1:m(i)
64 s = s + 1;
65 slots(s,:) = [i j];
66 end
67end
68S = s;
69sidx = zeros(h, max(m));
70for s2 = 1:S
71 sidx(slots(s2,1), slots(s2,2)) = s2;
72end
73dim = n * S;
74
75% popularity-ordered warm start
76[~, order] = sort(p, 'descend');
77x0 = zeros(dim,1);
78pos = 0;
79for s2 = 1:S
80 pos = pos + 1;
81 if pos <= n
82 x0((order(pos)-1)*S + s2) = 1.0;
83 end
84end
85
86% A non-default access graph (accost) uses the general position-resolved drift
87% from a COLD (empty) cache so non-admissible items drain; the linear default
88% keeps the pre-filled path.
89G = cache_build_item_graphs(accost, lambda, n, h);
90if ~isempty(G)
91 drift_h = @(t, x) cache_pos_drift_graph(x, p, G, m, n, h, slots, sidx, S, 'head');
92 x0s = zeros(dim,1); % cold start so non-admissible items drain
93else
94 drift_h = @(t, x) sfifo_drift(x, p, m, n, h, slots, sidx, S, dim);
95 x0s = x0;
96end
97odeopt = odeset('AbsTol', 1e-10, 'RelTol', 1e-8);
98[~, xvec] = ode15s(drift_h, [0, 20000], x0s, odeopt);
99xss = xvec(end, :)';
100
101pi0 = zeros(n,1);
102for k = 1:n
103 pi0(k) = sfifo_out(xss, k, slots, S);
104end
105MI = lam_i(:) .* pi0;
106MU = zeros(1, u);
107for v = 1:u
108 row = lambda(v,:,1);
109 row(~isfinite(row)) = 0;
110 MU(v) = row * pi0;
111end
112M = sum(MI);
113
114tout = []; pi0_t = []; MU_t = []; xtraj = [];
115if ~isempty(tspan)
116 if isempty(x0init), x0t = x0s; else, x0t = x0init(:); end
117 [tout, xtraj] = ode15s(drift_h, tspan, x0t, odeopt);
118 xtraj = xtraj';
119 nt = numel(tout);
120 pi0_t = zeros(n, nt);
121 for k = 1:n
122 for c = 1:nt
123 pi0_t(k,c) = sfifo_out(xtraj(:,c), k, slots, S);
124 end
125 end
126 MU_t = zeros(u, nt);
127 for v = 1:u
128 row = lambda(v,:,1);
129 row(~isfinite(row)) = 0;
130 MU_t(v,:) = row * pi0_t;
131 end
132end
133end
134
135function o = sfifo_out(x, k, slots, S)
136% Out-of-cache occupancy of item k (1 minus its total in-cache occupancy)
137S2 = size(slots,1);
138acc = 0;
139for s = 1:S2
140 acc = acc + x((k-1)*S + s);
141end
142o = max(0, min(1, 1 - acc));
143end
144
145function dX = sfifo_drift(x, p, m, n, h, slots, sidx, S, dim)
146% Mean-field drift F(x) for strict FIFO(m); x is dim x 1 over in-cache slots.
147x = max(0, min(1, x));
148K = @(k,i,j) (k-1)*S + sidx(i,j);
149
150% per-position and per-list hit rates, and the miss rate
151Hpos = zeros(h, max(m));
152Hi = zeros(1, h+1);
153for s = 1:S
154 i = slots(s,1); j = slots(s,2);
155 acc = 0;
156 for k = 1:n
157 acc = acc + p(k) * x(K(k,i,j));
158 end
159 Hpos(i,j) = acc;
160 Hi(i) = Hi(i) + acc;
161end
162M = 0;
163for k = 1:n
164 M = M + p(k) * sfifo_out(x, k, slots, S);
165end
166
167% full-shift rate of each list
168Sfull = zeros(1, h+1);
169Sfull(1) = M;
170for i = 2:h
171 Sfull(i) = Hi(i-1);
172end
173
174dX = zeros(dim,1);
175for k = 1:n
176 for s = 1:S
177 i = slots(s,1); j = slots(s,2);
178 xk = x(K(k,i,j));
179 % outflow: shift toward j+1 (or leave list at tail); promotion up if i<h
180 o = (Sfull(i) + sfifo_gi(i,j,Hpos,m,h)) * xk;
181 if i < h
182 o = o + p(k) * xk;
183 end
184 dX(K(k,i,j)) = dX(K(k,i,j)) - o;
185 % inflow
186 if j >= 2
187 dX(K(k,i,j)) = dX(K(k,i,j)) + (Sfull(i) + sfifo_gi(i,j-1,Hpos,m,h)) * x(K(k,i,j-1));
188 else
189 if i == 1
190 dX(K(k,1,1)) = dX(K(k,1,1)) + p(k) * sfifo_out(x, k, slots, S);
191 else
192 occ = 0;
193 for jprev = 1:m(i-1)
194 occ = occ + x(K(k,i-1,jprev));
195 end
196 dX(K(k,i,1)) = dX(K(k,i,1)) + p(k) * occ;
197 end
198 if i < h
199 dX(K(k,i,1)) = dX(K(k,i,1)) + Hi(i) * x(K(k,i+1,m(i+1)));
200 end
201 end
202 end
203end
204end
205
206function g = sfifo_gi(i, jp, Hpos, m, h)
207% Partial-shift rate of a slot at position jp of list i: aggregate hit rate at
208% deeper positions of list i. The top list h never moves on a hit.
209if i == h
210 g = 0; return;
211end
212g = 0;
213for jj = (jp+1):m(i)
214 g = g + Hpos(i,jj);
215end
216end