LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
solver_mva_cache_analyzer.m
1function [QN,UN,RN,TN,CN,XN,lGN,runtime,iter,method,hitproblist,itemprob] = solver_mva_cache_analyzer(sn, options)
2% [Q,U,R,T,C,X,LG,RUNTIME,ITER,METHOD,HITPROBLIST] = SOLVER_MVA_CACHE_ANALYZER(QN, OPTIONS)
3
4% Copyright (c) 2012-2026, Imperial College London
5% All rights reserved.
6
7T0=tic;
8QN = []; UN = [];
9RN = []; TN = [];
10CN = [];
11XN = zeros(1,sn.nclasses);
12lGN = NaN;
13iter = NaN;
14
15line_debug('MVA cache analyzer starting: method=%s, nclasses=%d', options.method, sn.nclasses);
16
17source_ist = sn.nodeToStation(sn.nodetype == NodeType.Source);
18sourceRate = sn.rates(source_ist,:);
19sourceRate(isnan(sourceRate)) = 0;
20TN(source_ist,:) = sourceRate;
21
22ch = sn.nodeparam{sn.nodetype == NodeType.Cache};
23
24m = ch.itemcap;
25n = ch.nitems;
26h = length(m);
27u = sn.nclasses;
28lambda = zeros(u,n,h);
29
30for v=1:u
31 for k=1:n
32 for l=1:(h+1)
33 if ~isnan(ch.pread{v})
34 lambda(v,k,l) = sourceRate(v) * ch.pread{v}(k);
35 end
36 end
37 end
38end
39
40Rcost = ch.accost;
41if isempty(Rcost)
42 % Default linear cache routing: items flow from list l to list l+1
43 Rcost = cell(u, n);
44 for v = 1:u
45 for k = 1:n
46 Rmat = diag(ones(1, h), 1);
47 Rmat(h+1, h+1) = 1;
48 Rcost{v, k} = Rmat;
49 end
50 end
51end
52
53gamma = cache_gamma_lp(lambda,Rcost);
54
55% per-list hit probabilities are genuine only on the exact branch; see
56% _kb/09-ldes-and-cache.md on cache-analyzer per-list/per-item reporting
57pijlist = []; % genuine per-list occupancy (n x h), set in the exact branch
58switch options.method
59 case 'exact'
60 line_debug('Using exact cache method');
61 switch sn.nodeparam{sn.nodetype == NodeType.Cache}.replacestrat
62 case {ReplacementStrategy.RR, ReplacementStrategy.FIFO}
63 line_debug('Replacement strategy: RR/FIFO, calling cache_mva');
64 [~,~,pij] = cache_mva(gamma, m);
65 pij = [abs(1-sum(pij,2)),pij];
66 pijlist = pij(:, 2:end);
67 otherwise
68 line_error(mfilename,'MVA does not support exact solution of the specified cache replacement policy.')
69 end
70 otherwise
71 line_debug('Default method: using approximate cache method\n');
72 line_debug('Using approximate cache method');
73 switch sn.nodeparam{sn.nodetype == NodeType.Cache}.replacestrat
74 case {ReplacementStrategy.RR, ReplacementStrategy.FIFO}
75 line_debug('Replacement strategy: RR/FIFO, calling cache_prob_fpi');
76 pij = cache_prob_fpi(gamma,m); % FPI method
77 case ReplacementStrategy.LRU
78 % Marked (MMAP) source is not IRM -> LRU(m)-MAP TTL approximation;
79 % see _kb/09-ldes-and-cache.md on cache-analyzer reporting
80 markedreaders = isfield(sn,'markidx') && ~isempty(sn.markidx) ...
81 && any(sn.markidx(source_ist,:) > 0);
82 if markedreaders
83 Dcell = sn.proc{source_ist}{find(sn.markidx(source_ist,:)>0,1)};
84 D0 = Dcell{1}; D1agg = Dcell{2};
85 D0c = cell(1,n); D1c = cell(1,n);
86 allmarked = true;
87 for k=1:n
88 D1c{k} = zeros(size(D0));
89 for v=1:u
90 if ~isnan(ch.pread{v})
91 if sn.markidx(source_ist,v) > 0
92 D1c{k} = D1c{k} + Dcell{2+sn.markidx(source_ist,v)} * ch.pread{v}(k);
93 elseif sourceRate(v) > 0 && any(ch.pread{v} > 0)
94 allmarked = false; % unmarked reader mixed in
95 end
96 end
97 end
98 D0c{k} = D0 + D1agg - D1c{k};
99 end
100 if allmarked
101 line_debug('Replacement strategy: LRU with marked MAP source, calling cache_ttl_lrum_map');
102 pij = cache_ttl_lrum_map(D0c, D1c, m);
103 else
104 line_debug('Replacement strategy: LRU, calling cache_ttl_lrua');
105 pij = cache_ttl_lrua(lambda, Rcost, m); % allows trees and access costs
106 end
107 else
108 line_debug('Replacement strategy: LRU, calling cache_ttl_lrua');
109 pij = cache_ttl_lrua(lambda, Rcost, m); % allows trees and access costs
110 end
111 case ReplacementStrategy.HLRU
112 % h-LRU / LRU(m) characteristic-time approximation (linear
113 % list topology; access-cost graphs are not supported)
114 line_debug('Replacement strategy: HLRU, calling cache_ttl_hlru');
115 pij = cache_ttl_hlru(lambda, m);
116 otherwise
117 line_error(mfilename,'MVA does not support approximate solution of the specified cache replacement policy.')
118 end
119end
120missRate = zeros(1,u);
121for v=1:u
122 missRate(v) = lambda(v,:,1)*pij(:,1);
123end
124
125% per-list (per-level) hit probabilities (access-weighted), only where the
126% exact algorithm produced a genuine per-list occupancy matrix.
127hitproblist = NaN(u, h);
128if ~isempty(pijlist)
129 for v=1:u
130 if any(~isnan(ch.pread{v}))
131 pread = ch.pread{v}(:).';
132 for l=1:h
133 hitproblist(v,l) = pread * pijlist(:,l);
134 end
135 end
136 end
137end
138
139% per-item occupancy [nitems x (lists+1)] (col 1 = miss); derived from the exact
140% cache_mva recursion (skipped, NaN, for >10 items); see
141% _kb/09-ldes-and-cache.md on cache-analyzer per-list/per-item reporting
142itemprob = [];
143if ~isempty(pijlist)
144 itemprob = pij; % exact branch: already [n x (h+1)] with col 1 = miss
145elseif size(pij,2) == h+1
146 switch sn.nodeparam{sn.nodetype == NodeType.Cache}.replacestrat
147 case {ReplacementStrategy.RR, ReplacementStrategy.FIFO}
148 if n > 10
149 line_warning(mfilename, 'Per-item cache occupancy (getAvgItemTable) requires the exact algorithm for RR/FIFO and is skipped for caches with more than 10 items (%d items); reporting NaN.', n);
150 itemprob = NaN(n, h+1);
151 else
152 [~,~,pij_ex] = cache_mva(gamma, m);
153 itemprob = [abs(1-sum(pij_ex,2)), pij_ex];
154 end
155 otherwise
156 itemprob = pij; % LRU-TTL: genuine per-list distribution
157 end
158end
159
160for r = 1:sn.nclasses
161 if length(ch.hitclass)>=r && ch.missclass(r)>0 && ch.hitclass(r)>0
162 XN(ch.missclass(r)) = XN(ch.missclass(r)) + missRate(r);
163 XN(ch.hitclass(r)) = XN(ch.hitclass(r)) + (sourceRate(r) - missRate(r));
164 end
165end
166
167% Set the actual method used
168if strcmp(options.method, 'exact')
169 method = 'exact';
170else
171 switch sn.nodeparam{sn.nodetype == NodeType.Cache}.replacestrat
172 case {ReplacementStrategy.RR, ReplacementStrategy.FIFO}
173 method = 'fpi';
174 case ReplacementStrategy.LRU
175 method = 'ttl';
176 otherwise
177 method = options.method;
178 end
179end
180
181runtime=toc(T0);
182end