Path: blob/master/tools/testing/selftests/cgroup/memcg_protection.m
26285 views
% SPDX-License-Identifier: GPL-2.01%2% run as: octave-cli memcg_protection.m3%4% This script simulates reclaim protection behavior on a single level of memcg5% hierarchy to illustrate how overcommitted protection spreads among siblings6% (as it depends also on their current consumption).7%8% Simulation assumes siblings consumed the initial amount of memory (w/out9% reclaim) and then the reclaim starts, all memory is reclaimable, i.e. treated10% same. It simulates only non-low reclaim and assumes all memory.min = 0.11%12% Input configurations13% --------------------14% E number parent effective protection15% n vector nominal protection of siblings set at the given level (memory.low)16% c vector current consumption -,,- (memory.current)1718% example from testcase (values in GB)19E = 50 / 1024;20n = [75 25 0 500 ] / 1024;21c = [50 50 50 0] / 1024;2223% Reclaim parameters24% ------------------2526% Minimal reclaim amount (GB)27cluster = 32*4 / 2**20;2829% Reclaim coefficient (think as 0.5^sc->priority)30alpha = .13132% Simulation parameters33% ---------------------34epsilon = 1e-7;35timeout = 1000;3637% Simulation loop38% ---------------3940ch = [];41eh = [];42rh = [];4344for t = 1:timeout45% low_usage46u = min(c, n);47siblings = sum(u);4849% effective_protection()50protected = min(n, c); % start with nominal51e = protected * min(1, E / siblings); % normalize overcommit5253% recursive protection54unclaimed = max(0, E - siblings);55parent_overuse = sum(c) - siblings;56if (unclaimed > 0 && parent_overuse > 0)57overuse = max(0, c - protected);58e += unclaimed * (overuse / parent_overuse);59endif6061% get_scan_count()62r = alpha * c; % assume all memory is in a single LRU list6364% commit 1bc63fb1272b ("mm, memcg: make scan aggression always exclude protection")65sz = max(e, c);66r .*= (1 - (e+epsilon) ./ (sz+epsilon));6768% uncomment to debug prints69% e, c, r7071% nothing to reclaim, reached equilibrium72if max(r) < epsilon73break;74endif7576% SWAP_CLUSTER_MAX roundup77r = max(r, (r > epsilon) .* cluster);78% XXX here I do parallel reclaim of all siblings79% in reality reclaim is serialized and each sibling recalculates own residual80c = max(c - r, 0);8182ch = [ch ; c];83eh = [eh ; e];84rh = [rh ; r];85endfor8687t88c, e899091