0001 % SPDX-License-Identifier: GPL-2.0
0002 %
0003 % run as: octave-cli memcg_protection.m
0004 %
0005 % This script simulates reclaim protection behavior on a single level of memcg
0006 % hierarchy to illustrate how overcommitted protection spreads among siblings
0007 % (as it depends also on their current consumption).
0008 %
0009 % Simulation assumes siblings consumed the initial amount of memory (w/out
0010 % reclaim) and then the reclaim starts, all memory is reclaimable, i.e. treated
0011 % same. It simulates only non-low reclaim and assumes all memory.min = 0.
0012 %
0013 % Input configurations
0014 % --------------------
0015 % E number parent effective protection
0016 % n vector nominal protection of siblings set at the given level (memory.low)
0017 % c vector current consumption -,,- (memory.current)
0018
0019 % example from testcase (values in GB)
0020 E = 50 / 1024;
0021 n = [75 25 0 500 ] / 1024;
0022 c = [50 50 50 0] / 1024;
0023
0024 % Reclaim parameters
0025 % ------------------
0026
0027 % Minimal reclaim amount (GB)
0028 cluster = 32*4 / 2**20;
0029
0030 % Reclaim coefficient (think as 0.5^sc->priority)
0031 alpha = .1
0032
0033 % Simulation parameters
0034 % ---------------------
0035 epsilon = 1e-7;
0036 timeout = 1000;
0037
0038 % Simulation loop
0039 % ---------------
0040
0041 ch = [];
0042 eh = [];
0043 rh = [];
0044
0045 for t = 1:timeout
0046 % low_usage
0047 u = min(c, n);
0048 siblings = sum(u);
0049
0050 % effective_protection()
0051 protected = min(n, c); % start with nominal
0052 e = protected * min(1, E / siblings); % normalize overcommit
0053
0054 % recursive protection
0055 unclaimed = max(0, E - siblings);
0056 parent_overuse = sum(c) - siblings;
0057 if (unclaimed > 0 && parent_overuse > 0)
0058 overuse = max(0, c - protected);
0059 e += unclaimed * (overuse / parent_overuse);
0060 endif
0061
0062 % get_scan_count()
0063 r = alpha * c; % assume all memory is in a single LRU list
0064
0065 % commit 1bc63fb1272b ("mm, memcg: make scan aggression always exclude protection")
0066 sz = max(e, c);
0067 r .*= (1 - (e+epsilon) ./ (sz+epsilon));
0068
0069 % uncomment to debug prints
0070 % e, c, r
0071
0072 % nothing to reclaim, reached equilibrium
0073 if max(r) < epsilon
0074 break;
0075 endif
0076
0077 % SWAP_CLUSTER_MAX roundup
0078 r = max(r, (r > epsilon) .* cluster);
0079 % XXX here I do parallel reclaim of all siblings
0080 % in reality reclaim is serialized and each sibling recalculates own residual
0081 c = max(c - r, 0);
0082
0083 ch = [ch ; c];
0084 eh = [eh ; e];
0085 rh = [rh ; r];
0086 endfor
0087
0088 t
0089 c, e