1function [QN,UN,RN,TN,CN,XN,lG,pij,runtime,method,hitproblist,itemprob] = solver_nc_cache_analyzer(sn, options)
2% [Q,U,R,T,C,X,LG,PIJ,RUNTIME,METHOD,HITPROBLIST] = SOLVER_NC_CACHE_ANALYZER(QN, OPTIONS)
4% Copyright (c) 2012-2026, Imperial College London
11XN = zeros(1,sn.nclasses);
15line_debug(
'NC cache analyzer starting: method=%s, nclasses=%d', options.method, sn.nclasses);
17source_ist = sn.nodeToStation(sn.nodetype == NodeType.Source);
18sourceRate = sn.rates(source_ist,:);
19sourceRate(isnan(sourceRate)) = 0;
20TN(source_ist,:) = sourceRate;
22ch = sn.nodeparam{sn.nodetype == NodeType.Cache};
28 line_error(mfilename,
'NC requires the number of items to exceed the cache capacity at least by 2.');
38 if ~isnan(ch.pread{v})
39 lambda(v,k,l) = sourceRate(v) * ch.pread{v}(k);
47 % Default linear cache routing: items flow from list l to list l+1
51 Rmat = diag(ones(1, h), 1);
57gamma = cache_gamma_lp(lambda,R);
58% per-list (per-level) hit probabilities are reported only from
the exact
59% algorithm, where
the miss
column and
the per-list columns of pij share one
60% consistent solution (so
the per-list rows sum to
the aggregate hit). The
61% approximate algorithms derive miss and per-list from different expansions, so
62% their per-list breakdown
is left undefined (NaN).
63pijlist = []; % genuine per-list occupancy (n x h), set in
the exact branch
66 % cache_prob_erec
is exact only
for the exchangeable (product-form)
67 % family (RR/FIFO/RANDOM-type); recency-based policies (LRU, h-LRU,
68 % q-LRU, CLIMB) have no product form and would silently
return the
69 % exchangeable solution instead.
70 switch sn.nodeparam{sn.nodetype == NodeType.Cache}.replacestrat
71 case {ReplacementStrategy.RR, ReplacementStrategy.FIFO}
74 line_error(mfilename,
'NC does not support exact solution of the specified cache replacement policy; use the default (approximate) method or SolverCTMC.');
76 line_debug(
'Using exact method, calling cache_prob_erec');
77 [pij] = cache_prob_erec(gamma, m);
78 missRate = zeros(1,u);
80 missRate(v) = lambda(v,:,1)*pij(:,1);
82 pijlist = pij(:, 2:end);
85 line_debug(
'Using sampling method, calling cache_miss_is');
86 [~,missRate,~,~,lE] = cache_miss_is(gamma, m, lambda, options.samples);
87 pij = cache_prob_is(gamma, m, options.samples);
90 line_debug(
'Default method: using SPM approximation method\n');
91 line_debug(
'Using SPM approximation method, calling cache_miss_spm');
92 [~,missRate,~,~,lE] = cache_miss_spm(gamma, m, lambda);
93 pij = cache_prob_spm(gamma, m, lE);
98 if length(ch.hitclass)>=r && ch.missclass(r)>0 && ch.hitclass(r)>0
99 XN(ch.missclass(r)) = XN(ch.missclass(r)) + missRate(r);
100 XN(ch.hitclass(r)) = XN(ch.hitclass(r)) + (sourceRate(r) - missRate(r));
104% per-list (per-level) hit probabilities (access-weighted), only where
the
105% exact algorithm produced a genuine per-list occupancy matrix.
106hitproblist = NaN(u, h);
109 if any(~isnan(ch.pread{v}))
110 pread = ch.pread{v}(:).
';
112 hitproblist(v,l) = pread * pijlist(:,l);
117% per-item occupancy [nitems x (lists+1)] (col 1 = miss, cols 2..end per-list).
118% Only the exact recursion (cache_prob_erec) yields a genuine per-list
119% distribution: the spm/is algorithms approximate each per-list column
120% independently and do not form a proper distribution for h>1. Since the cache
121% is product-form, derive the per-item table from the exact algorithm in the
122% approximate branches too (the approximate pij is still used above for the
123% aggregate miss rate). The exact recursion is only tractable for small item
124% sets, so it is skipped (NaN, with a warning) for caches with more than 10
127 line_warning(mfilename, 'Per-item cache occupancy (getAvgItemTable)
requires the exact algorithm and
is skipped
for caches with more than 10 items (%d items); reporting NaN.
', n);
128 itemprob = NaN(n, h+1);
129elseif ~isempty(pijlist)
130 itemprob = pij; % exact branch: already [n x (h+1)] with col 1 = miss
132 itemprob = cache_prob_erec(gamma, m); % [n x (h+1)] with col 1 = miss