okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-142b-serve into f-1426e1085c1d

main
7.9 KiB158 linesraw
1"""Step 1's page-file check: only the size the page files have for sure counts - their size now (the commit limit
2minus RAM), or a file's configured initial size when larger - never what Windows may grow them to, and the warning
3(below 4 GB, as before) advises a fixed size.  When the Virtual memory setting (the registry's PagingFiles) has Windows
4grow a file on demand ("System managed", or an initial size below the maximum), the warning says why that is not
5enough (issue #60).  The registry, the drives and GlobalMemoryStatusEx are mocked, so it runs on Linux too.
6
7    python -m unittest tools.test_setup_page_file
8"""
9from __future__ import annotations
10
11import contextlib
12import sys
13import types
14import unittest
15from pathlib import Path
16from unittest import mock
17
18ROOT = Path(__file__).resolve().parents[1]
19sys.path.insert(0, str(ROOT))
20sys.path.insert(0, str(ROOT / "tools"))
21import setup  # noqa: E402
22from test_setup_golden import PROFILES, install  # noqa: E402
23
24PAGE_FILE_GB, PAGE_FILE_SETTING = setup.page_file_gb, setup.page_file_setting   # the golden harness mocks the first
25GROWS = "It is set to grow on demand"
26FIXED = f"Custom size with initial and maximum size {setup.PAGE_FILE_FIXED_MB} MB"
27
28
29def windows(paging, ram=31.9, now_mb=0, drives=None, existing=("\\??\\C:\\pagefile.sys",)):
30    """What Windows answers page_file_gb() and page_file_setting(), mocked: PagingFiles (None: it cannot be read),
31    ExistingPageFiles, each drive's (free MB, page file MB now), and GlobalMemoryStatusEx (commit limit = RAM + the
32    files' size now)."""
33    drives = {"C": (400000, now_mb)} if drives is None else drives
34    ms = types.SimpleNamespace(ullTotalPhys=int(ram * 2**30), ullTotalPageFile=int(ram * 2**30) + now_mb * 2**20)
35    return [mock.patch.object(setup, "_mm_multi_sz", lambda name: None if paging is None else
36                              list(paging) if name == "PagingFiles" else list(existing)),
37            mock.patch.object(setup, "_drive_mb", lambda d: drives.get(d)),
38            mock.patch.object(setup, "_memory_status", lambda: ms)]
39
40
41def on_windows(fn):
42    def call(*a):
43        with mock.patch.object(setup, "WIN", True):
44            return fn(*a)
45    return call
46
47
48class Parse(unittest.TestCase):
49    def test_entries(self):
50        self.assertEqual(setup.parse_paging_file("c:\\pagefile.sys 64000 64000"), ("C", 64000, 64000))
51        self.assertEqual(setup.parse_paging_file("D:\\pagefile.sys 1024 32768"), ("D", 1024, 32768))
52        for managed in ("C:\\pagefile.sys 0 0", "C:\\pagefile.sys", "C:\\pagefile.sys x y"):
53            self.assertEqual(setup.parse_paging_file(managed), ("C", -1, -1), managed)
54        self.assertEqual(setup.parse_paging_file("?:\\pagefile.sys"), ("?", -1, -1))      # every drive automatic
55        for junk in ("", "x", "pagefile.sys 1 2"):
56            self.assertIsNone(setup.parse_paging_file(junk), junk)
57
58
59class Reading(unittest.TestCase):
60    def read(self, paging, **k):
61        with contextlib.ExitStack() as st:
62            for p in windows(paging, **k):
63                st.enter_context(p)
64            files = on_windows(PAGE_FILE_SETTING)()
65            return on_windows(PAGE_FILE_GB)(), files
66
67    def test_two_fixed_files(self):                   # this PR's author: 2 x 64000 MB on C: and D:
68        gb, files = self.read(["c:\\pagefile.sys 64000 64000", "d:\\pagefile.sys 64000 64000"], ram=127.2,
69                              now_mb=128000, drives={"C": (334983, 64000), "D": (178396, 64000)})
70        self.assertEqual(gb, 125.0)
71        self.assertFalse(setup.page_file_grows(files))
72
73    def test_a_growing_file_counts_its_size_now(self):
74        for paging in (["?:\\pagefile.sys"], ["C:\\pagefile.sys 0 0"], ["C:\\pagefile.sys"]):   # system-managed
75            with self.subTest(paging):
76                gb, files = self.read(paging, now_mb=4083)
77                self.assertAlmostEqual(gb, 4083 / 1024)      # not 3 x RAM: it may not grow in time (#60)
78                self.assertTrue(setup.page_file_grows(files))
79        gb, files = self.read(["C:\\pagefile.sys 1024 32768"], now_mb=1024)
80        self.assertEqual(gb, 1.0)                            # its initial size, not its maximum
81        self.assertTrue(setup.page_file_grows(files))
82        _, files = self.read(["?:\\pagefile.sys"], now_mb=4083, existing=("\\??\\D:\\pagefile.sys",),
83                             drives={"D": (100000, 4083)})
84        self.assertEqual(files, [("D", -1, -1, 4083, 100000)])    # "?:" is where Windows keeps it
85
86    def test_an_initial_size_larger_than_the_file_now_counts(self):
87        self.assertEqual(self.read(["C:\\pagefile.sys 8192 8192"], now_mb=2048, drives={"C": (400000, 2048)})[0], 8.0)
88        gb, _ = self.read(["C:\\pagefile.sys 8192 8192"], now_mb=2048, drives={"C": (1000, 2048)})   # a full drive
89        self.assertAlmostEqual(gb, 3048 / 1024)
90
91    def test_off_and_unread(self):
92        gb, files = self.read([])
93        self.assertEqual((gb, files), (0.0, []))
94        self.assertFalse(setup.page_file_grows(files))
95        gb, files = self.read(None, now_mb=4083)             # the setting cannot be read: the commit limit alone
96        self.assertAlmostEqual(gb, 4083 / 1024)
97        self.assertIsNone(files)
98        self.assertFalse(setup.page_file_grows(files))
99
100    def test_not_windows(self):
101        with mock.patch.object(setup, "WIN", False):
102            self.assertIsNone(setup.page_file_gb())
103            self.assertIsNone(setup.page_file_setting())
104
105
106class Advice(unittest.TestCase):
107    def test_fixed_size_always_and_why_when_it_grows(self):
108        fixed = setup.page_file_advice(2.0, [("C", 2048, 2048, 2048, 400000)])
109        self.assertIn("Windows' page file is 2.0 GB: the graphics card's memory needs room there too", fixed)
110        self.assertIn(FIXED, fixed)
111        self.assertNotIn(GROWS, fixed)
112        self.assertNotIn("Set it to \"System managed\"", fixed)
113        for files in ([("C", -1, -1, 4083, 400000)], [("C", 1024, 32768, 1024, 400000)],
114                      [("C", 2048, 2048, 2048, 400000), ("D", -1, -1, 1024, 400000)]):
115            with self.subTest(files):
116                text = setup.page_file_advice(3.99, files)
117                self.assertIn(GROWS, text)
118                self.assertIn("a fixed 64 GB worked", text)
119                self.assertIn(FIXED, text)
120        self.assertNotIn(GROWS, setup.page_file_advice(0.0, []))
121        self.assertNotIn(GROWS, setup.page_file_advice(1.0, None))   # the setting unread
122
123
124class StepOne(unittest.TestCase):
125    """setup --check on the harness's 32 GB PC with the page-file reading of a Windows PC: warned below 4 GB."""
126    WARN = "Windows' page file is"
127
128    def check(self, paging, now_mb, ram=None):
129        ram0, found = PROFILES["32GB-2x24GB"]
130        ram = ram or ram0
131        extra = [mock.patch.object(setup, "page_file_gb", on_windows(PAGE_FILE_GB)),
132                 mock.patch.object(setup, "page_file_setting", on_windows(PAGE_FILE_SETTING)),
133                 *windows(paging, ram=ram, now_mb=now_mb)]
134        code, out, _, _ = install(ram, found, ["--check"], extra=extra)
135        self.assertEqual(code, 0, out[-3000:])
136        return next((x for x in out.splitlines() if self.WARN in x), None)
137
138    def test_a_system_managed_file_is_warned_with_the_reason(self):
139        line = self.check(["?:\\pagefile.sys"], 4083)
140        self.assertIn("Windows' page file is 4.0 GB", line)
141        self.assertIn(GROWS, line)
142        self.assertIn(FIXED, line)
143
144    def test_a_small_fixed_file_and_none(self):
145        for paging, now in ((["C:\\pagefile.sys 2048 2048"], 2048), ([], 0)):
146            with self.subTest(paging):
147                line = self.check(paging, now)
148                self.assertIn(f"Windows' page file is {now / 1024:.1f} GB", line)
149                self.assertIn(FIXED, line)
150                self.assertNotIn(GROWS, line)
151
152    def test_the_threshold_is_unchanged(self):
153        self.assertIsNone(self.check(["?:\\pagefile.sys"], 8192))          # 8 GB now: not warned, as before
154        self.assertIsNone(self.check(["C:\\pagefile.sys 64000 64000"], 64000))
155
156
157if __name__ == "__main__":
158    unittest.main()