memcg_protection.m 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. % SPDX-License-Identifier: GPL-2.0
  2. %
  3. % run as: octave-cli memcg_protection.m
  4. %
  5. % This script simulates reclaim protection behavior on a single level of memcg
  6. % hierarchy to illustrate how overcommitted protection spreads among siblings
  7. % (as it depends also on their current consumption).
  8. %
  9. % Simulation assumes siblings consumed the initial amount of memory (w/out
  10. % reclaim) and then the reclaim starts, all memory is reclaimable, i.e. treated
  11. % same. It simulates only non-low reclaim and assumes all memory.min = 0.
  12. %
  13. % Input configurations
  14. % --------------------
  15. % E number parent effective protection
  16. % n vector nominal protection of siblings set at the given level (memory.low)
  17. % c vector current consumption -,,- (memory.current)
  18. % example from testcase (values in GB)
  19. E = 50 / 1024;
  20. n = [75 25 0 500 ] / 1024;
  21. c = [50 50 50 0] / 1024;
  22. % Reclaim parameters
  23. % ------------------
  24. % Minimal reclaim amount (GB)
  25. cluster = 32*4 / 2**20;
  26. % Reclaim coefficient (think as 0.5^sc->priority)
  27. alpha = .1
  28. % Simulation parameters
  29. % ---------------------
  30. epsilon = 1e-7;
  31. timeout = 1000;
  32. % Simulation loop
  33. % ---------------
  34. ch = [];
  35. eh = [];
  36. rh = [];
  37. for t = 1:timeout
  38. % low_usage
  39. u = min(c, n);
  40. siblings = sum(u);
  41. % effective_protection()
  42. protected = min(n, c); % start with nominal
  43. e = protected * min(1, E / siblings); % normalize overcommit
  44. % recursive protection
  45. unclaimed = max(0, E - siblings);
  46. parent_overuse = sum(c) - siblings;
  47. if (unclaimed > 0 && parent_overuse > 0)
  48. overuse = max(0, c - protected);
  49. e += unclaimed * (overuse / parent_overuse);
  50. endif
  51. % get_scan_count()
  52. r = alpha * c; % assume all memory is in a single LRU list
  53. % commit 1bc63fb1272b ("mm, memcg: make scan aggression always exclude protection")
  54. sz = max(e, c);
  55. r .*= (1 - (e+epsilon) ./ (sz+epsilon));
  56. % uncomment to debug prints
  57. % e, c, r
  58. % nothing to reclaim, reached equilibrium
  59. if max(r) < epsilon
  60. break;
  61. endif
  62. % SWAP_CLUSTER_MAX roundup
  63. r = max(r, (r > epsilon) .* cluster);
  64. % XXX here I do parallel reclaim of all siblings
  65. % in reality reclaim is serialized and each sibling recalculates own residual
  66. c = max(c - r, 0);
  67. ch = [ch ; c];
  68. eh = [eh ; e];
  69. rh = [rh ; r];
  70. endfor
  71. t
  72. c, e