okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6b63729a9
main
1"""The RAM figure the Terminal's tools read (strata_control/cgroup_memory.py; split_engine.ram_gib, tune_machine.memory, 2and setup's ram_gb through the same limit reader): inside a container the cgroup limit, outside it /proc/meminfo exactly 3as before. Fake /proc and cgroup trees in a temporary folder; no container needed. 4""" 5import os 6import tempfile 7import unittest 8from pathlib import Path 9from unittest import mock 10 11from strata_control import cgroup_memory as C 12from strata_control import split_engine ,tune_machine 13 14GIB = 1 << 30 15MIB = 1 << 20 16HOST_KB = 527_799_316 # a 503 GiB host, as /proc/meminfo shows it inside the rented container 17HOST_AVAIL_KB = 500_000_000 18POD_LIMIT = 61_998_571_520 # that container's memory.max: 57.74 GiB 19 20 21def stat (inactive = 0 ,active = 0 ,dirty = 0 ,writeback = 0 ,shmem = 0 ,v1 = False ): 22p = "total_" if v1 else "" 23d ,w = ("total_dirty" ,"total_writeback" )if v1 else ("file_dirty" ,"file_writeback" ) 24return (f" { p } anon 1000\n { p } file { inactive + active + shmem } \n { p } shmem { shmem } \n { p } inactive_anon { shmem } \n" 25f" { p } active_file { active } \n { p } inactive_file { inactive } \n { d } { dirty } \n { w } { writeback } \n" ) 26 27 28class Tree : 29def __init__ (self ,total_kb = HOST_KB ,avail_kb = HOST_AVAIL_KB ): 30self .tmp = tempfile .TemporaryDirectory () 31self .dir = Path (self .tmp .name ) 32 (self .dir / "cg" ).mkdir () 33self .put ("meminfo" ,f"MemTotal: { total_kb } kB\nMemFree: 1000 kB\nMemAvailable: { avail_kb } kB" ) 34 35def put (self ,rel ,text ): 36p = self .dir / rel 37p .parent .mkdir (parents = True ,exist_ok = True ) 38p .write_text (f" { text } \n" ) 39 40def group (self ,rel ,limit = None ,current = 0 ,high = None ,stat_text = "" ,v1 = False ): 41base = f"cg/memory/ { rel } " if v1 else f"cg/ { rel } " 42if v1 : 43self .put (f" { base } /memory.limit_in_bytes" ,limit if limit is not None else 9223372036854771712 ) 44self .put (f" { base } /memory.usage_in_bytes" ,current ) 45else : 46self .put (f" { base } /memory.max" ,limit if limit is not None else "max" ) 47self .put (f" { base } /memory.high" ,high if high is not None else "max" ) 48self .put (f" { base } /memory.current" ,current ) 49self .put (f" { base } /memory.stat" ,stat_text ) 50 51def ram (self ): 52return C .ram_bytes (str (self .dir / "meminfo" ),str (self .dir / "self_cgroup" ),str (self .dir / "cg" )) 53 54def close (self ): 55self .tmp .cleanup () 56 57 58class RamBytes (unittest .TestCase ): 59def setUp (self ): 60self .t = Tree () 61 62def tearDown (self ): 63self .t .close () 64 65def test_v2_container_limit (self ): 66# the pod: 20 GiB used, of which 8 GiB clean file cache (5 active, 3 inactive) and 1 GiB of it dirty 67self .t .put ("self_cgroup" ,"0::/" ) 68self .t .group ("." ,limit = POD_LIMIT ,current = 20 * GIB ,stat_text = stat (inactive = 3 * GIB ,active = 5 * GIB ,dirty = GIB )) 69total ,avail = self .t .ram () 70self .assertEqual (total ,POD_LIMIT ) 71self .assertEqual (avail ,POD_LIMIT - (20 * GIB - 7 * GIB )) 72 73def test_shmem_is_not_credited (self ): 74self .t .put ("self_cgroup" ,"0::/" ) 75self .t .group ("." ,limit = 24 * GIB ,current = 10 * GIB ,stat_text = stat (inactive = 2 * GIB ,shmem = 3 * GIB )) 76self .assertEqual (self .t .ram (), (24 * GIB ,24 * GIB - 8 * GIB )) 77 78def test_memory_high_counts_as_the_limit (self ): 79self .t .put ("self_cgroup" ,"0::/" ) 80self .t .group ("." ,limit = 40 * GIB ,high = 30 * GIB ,current = 10 * GIB ,stat_text = stat ()) 81self .assertEqual (self .t .ram (), (30 * GIB ,20 * GIB )) 82 83def test_v1_container_limit (self ): 84self .t .put ("self_cgroup" ,"4:memory:/docker/abc\n1:cpu,cpuacct:/docker/abc" ) 85self .t .group ("docker/abc" ,limit = 32 * GIB ,current = 12 * GIB ,stat_text = stat (inactive = GIB ,active = GIB ,v1 = True ),v1 = True ) 86self .t .group ("docker" ,v1 = True ) 87self .t .group ("." ,v1 = True ) 88self .assertEqual (self .t .ram (), (32 * GIB ,32 * GIB - 10 * GIB )) 89 90def test_limit_on_a_parent_level (self ): 91# the process's own group has no limit; its parent's 24 GiB is the one that holds (and its usage counts) 92self .t .put ("self_cgroup" ,"0::/box/job" ) 93self .t .group ("." ) 94self .t .group ("box" ,limit = 24 * GIB ,current = 18 * GIB ,stat_text = stat (active = 2 * GIB )) 95self .t .group ("box/job" ,current = 6 * GIB ,stat_text = stat (active = GIB )) 96self .assertEqual (self .t .ram (), (24 * GIB ,24 * GIB - 16 * GIB )) 97 98def test_the_tighter_of_two_levels (self ): 99self .t .put ("self_cgroup" ,"0::/box/job" ) 100self .t .group ("." ) 101self .t .group ("box" ,limit = 40 * GIB ,current = 36 * GIB ,stat_text = stat ()) 102self .t .group ("box/job" ,limit = 30 * GIB ,current = 10 * GIB ,stat_text = stat ()) 103self .assertEqual (self .t .ram (), (30 * GIB ,4 * GIB )) 104 105def test_no_limit_is_meminfo_exactly (self ): 106self .t .put ("self_cgroup" ,"0::/user.slice/u.scope" ) 107self .t .group ("." ) 108self .t .group ("user.slice" ,current = 50 * GIB ,stat_text = stat ()) 109self .t .group ("user.slice/u.scope" ,current = GIB ,stat_text = stat ()) 110self .assertEqual (self .t .ram (), (HOST_KB * 1024 ,HOST_AVAIL_KB * 1024 )) 111 112def test_limit_at_or_above_memtotal_is_meminfo_exactly (self ): 113for limit in (HOST_KB * 1024 ,HOST_KB * 1024 + GIB ): 114self .t .put ("self_cgroup" ,"0::/" ) 115self .t .group ("." ,limit = limit ,current = 400 * GIB ,stat_text = stat ()) 116self .assertEqual (self .t .ram (), (HOST_KB * 1024 ,HOST_AVAIL_KB * 1024 ),limit ) 117 118def test_no_cgroup_files_is_meminfo_exactly (self ): 119self .t .put ("self_cgroup" ,"0::/" ) 120self .assertEqual (self .t .ram (), (HOST_KB * 1024 ,HOST_AVAIL_KB * 1024 )) 121 122def test_container_free_never_above_the_hosts (self ): 123t = Tree (avail_kb = 4 * MIB )# the host has 4 GiB available; the container's room is larger 124try : 125t .put ("self_cgroup" ,"0::/" ) 126t .group ("." ,limit = 24 * GIB ,current = GIB ,stat_text = stat ()) 127self .assertEqual (t .ram (), (24 * GIB ,4 * GIB )) 128finally : 129t .close () 130 131def test_no_meminfo (self ): 132self .assertEqual (C .ram_bytes (str (self .t .dir / "none" ),str (self .t .dir / "self_cgroup" ),str (self .t .dir / "cg" )), 133 (None ,None )) 134 135def test_limit_reader_matches_ram (self ): 136self .t .put ("self_cgroup" ,"0::/box" ) 137self .t .group ("." ) 138self .t .group ("box" ,limit = 24 * GIB ,current = GIB ,stat_text = stat ()) 139self .assertEqual (C .memory_limit (str (self .t .dir / "self_cgroup" ),str (self .t .dir / "cg" )),24 * GIB ) 140 141 142class Callers (unittest .TestCase ): 143"""split_engine.ram_gib and tune_machine.memory give the helper's figures, in their units; outside a container the 144same numbers as their earlier /proc/meminfo arithmetic.""" 145 146def test_split_engine_ram_gib (self ): 147with mock .patch .object (C ,"ram_bytes" ,lambda : (POD_LIMIT ,13 * GIB )): 148self .assertEqual (split_engine .ram_gib (), (POD_LIMIT / GIB ,13.0 )) 149with mock .patch .object (C ,"ram_bytes" ,lambda : (HOST_KB * 1024 ,HOST_AVAIL_KB * 1024 )): 150self .assertEqual (split_engine .ram_gib (), (HOST_KB / (1 << 20 ),HOST_AVAIL_KB / (1 << 20 ))) 151with mock .patch .object (C ,"ram_bytes" ,lambda : (None ,None )): 152self .assertEqual (split_engine .ram_gib (), (0.0 ,0.0 )) 153 154@ unittest . skipIf ( os . name == "nt" , "the Linux branch of tune_machine.memory" ) 155def test_tune_machine_memory (self ): 156with mock .patch .object (tune_machine .sys ,"platform" ,"linux" ), \ 157mock .patch .object (C ,"ram_bytes" ,lambda : (HOST_KB * 1024 ,HOST_AVAIL_KB * 1024 )): 158self .assertEqual (tune_machine .memory (), {"total_mib" :HOST_KB // 1024 ,"available_mib" :HOST_AVAIL_KB // 1024 }) 159with mock .patch .object (tune_machine .sys ,"platform" ,"linux" ), \ 160mock .patch .object (C ,"ram_bytes" ,lambda : (POD_LIMIT ,13 * GIB )): 161self .assertEqual (tune_machine .memory (), {"total_mib" :POD_LIMIT // MIB ,"available_mib" :13 * 1024 }) 162with mock .patch .object (tune_machine .sys ,"platform" ,"linux" ),mock .patch .object (C ,"ram_bytes" ,lambda : (None ,None )): 163self .assertEqual (tune_machine .memory (), {"total_mib" :None ,"available_mib" :None }) 164 165 166if __name__ == "__main__" : 167unittest .main ()