"""Host resource policy must honor cgroup ancestors and not scale with huge CPU counts. Daemon E2E cannot change its host's memory or cgroup layout to cover these rules. """ import os from pathlib import Path import sys import tempfile import unittest from unittest import mock sys.path.insert(0, str(Path(__file__).resolve().parents[1])) import hypothesis_profiles # noqa: E402, F401 (loads the gate profile) import resources # noqa: E402 from hypothesis import given, strategies as st # noqa: E402 MIB = resources.MIB # Below this much available memory one E2E lane does not fit its budget. FLOOR = resources.E2E_LANE_MEMORY * 8 // 3 def plan_or_none(cpus, memory, quota): try: return resources.plan(cpus, memory, quota) except ValueError as error: assert "available memory" in str(error) return None # Hosts from one CPU to far past the caps, memory around the floor and up to # 128 GiB, and a quota that may be anything up to the host's processors. hosts = st.integers(1, 512).flatmap( lambda cpus: st.tuples( st.just(cpus), st.one_of( st.integers(0, 4096), st.integers(4096, 131072), st.just(FLOOR // MIB) ).map(lambda mib: mib * MIB), st.integers(1, cpus), ) ) class ResourceTests(unittest.TestCase): @given(hosts, st.integers(0, 131072).map(lambda mib: mib * MIB)) def test_lanes_follow_memory_and_cpus_and_fit_the_budget(self, host, more): cpus, memory, quota = host workers = plan_or_none(cpus, memory, quota) # Too little memory for one E2E lane is refused, never a zero plan. self.assertEqual( workers is None, memory * 3 // 4 // 2 < resources.E2E_LANE_MEMORY ) if workers is None: return lanes = workers["shared"] self.assertTrue(1 <= lanes <= 4) self.assertLessEqual(lanes, max(1, cpus // 4)) self.assertEqual((workers["exclusive"], workers["go"]), (2 * lanes, lanes)) for field in resources.FIELDS: self.assertGreaterEqual(workers[field], 1) # Computing lanes follow the quota; waiting E2E lanes follow the host. self.assertLessEqual(workers["gleam"], 4 * quota) self.assertLessEqual(workers["build"], quota) host_wide = resources.plan(cpus, memory, cpus) for field in ("shared", "exclusive", "go", "harness"): self.assertEqual(workers[field], host_wide[field]) # Every reservation together fits three quarters of the memory. reserved = ( lanes * resources.E2E_LANE_MEMORY + workers["gleam"] * 4 * MIB + workers["harness"] * 32 * MIB + workers["build"] * 128 * MIB ) self.assertLessEqual(reserved, memory * 3 // 4) # More memory never takes a lane away. richer = resources.plan(cpus, memory + more, quota) for field in resources.FIELDS: self.assertGreaterEqual(richer[field], workers[field]) def test_a_frozen_plan_is_not_rediscovered(self): frozen = {name: "1" for name in resources.ENVIRONMENT} with ( mock.patch.dict(os.environ, frozen), mock.patch.object(resources, "available_memory", return_value=0), ): self.assertEqual(set(resources.limits().values()), {1}) def test_cgroup_headroom_includes_parent_usage(self): with tempfile.TemporaryDirectory() as root: parent = Path(root) child = parent / "child" child.mkdir() (parent / "memory.max").write_text(str(1000 * resources.MIB)) (parent / "memory.current").write_text(str(900 * resources.MIB)) (child / "memory.max").write_text("max") with mock.patch.object(resources, "cgroups", return_value=[child, parent]): self.assertEqual(resources.available_memory(), 100 * resources.MIB) def test_cpu_quota_limits_builds_but_not_waiting_scenarios(self): with tempfile.TemporaryDirectory() as root: directory = Path(root) (directory / "cpu.max").write_text("150000 100000") with ( mock.patch.object(resources, "cgroups", return_value=[directory]), mock.patch.object(os, "cpu_count", return_value=24), mock.patch.object( os, "sched_getaffinity", return_value=set(range(24)), create=True ), ): self.assertEqual(resources.cpu_count(), 2) self.assertEqual(resources.cpu_count(include_quota=False), 24) if __name__ == "__main__": unittest.main()