okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fb7c62963
main
1"""The default count of CPU expert workers on mock CPUs, and what setup writes about thread counts. 2 3The engine's choice is include/strata/kernels/cpu/pool_plan.hpp (tested on its own by the pool_plan_test binary); setup mirrors 4it so it can warn about a configured count. This test keeps the two in step and checks the mirror on the same mock 5topologies (8c/16t, 16c/32t, 64c/128t, a 2-socket 128c/256t box) plus the reports behind it: a 256-thread machine started 6~256 workers and slowed down. 7 8python -m unittest tools.test_pool_plan 9""" 10from __future__import annotations 11 12import re 13import sys 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 22 23HEADER = ROOT / "include" / "strata" / "kernels" / "cpu" / "pool_plan.hpp" 24 25 26class InStepWithTheEngine (unittest .TestCase ): 27def header_constant (self ,name ): 28m = re .search (rf"constexpr int { name } \s*=\s*(\d+);" ,HEADER .read_text (encoding = "utf-8" )) 29self .assertIsNotNone (m ,name ) 30return int (m .group (1 )) 31 32def test_constants (self ): 33self .assertEqual (setup .POOL_WORKERS_CAP ,self .header_constant ("kPoolWorkersCap" )) 34self .assertEqual (setup .POOL_RESERVE_FROM ,self .header_constant ("kPoolReserveFrom" )) 35self .assertEqual (setup .POOL_RESERVE ,self .header_constant ("kPoolReserve" )) 36 37 38class DefaultWorkers (unittest .TestCase ): 39def test_mock_topologies (self ): 40cap = setup .POOL_WORKERS_CAP 41# physical cores, not threads, minus the host thread's core 42self .assertEqual (setup .plan_pool_workers (16 , [8 ]),7 )# 8c/16t 43self .assertEqual (setup .plan_pool_workers (12 , [6 ]),5 )# 6c/12t 44self .assertEqual (setup .plan_pool_workers (32 , [16 ]),15 )# 16c/32t 45self .assertEqual (setup .plan_pool_workers (128 , [64 ]),cap )# 64c/128t: the cap 46self .assertEqual (setup .plan_pool_workers (256 , [64 ,64 ]),cap )# 2-socket 128c/256t: the cap, never 255 47self .assertLess (setup .plan_pool_workers (256 , [64 ,64 ]),128 ) 48# two sockets of 20 cores: the host's socket only (bench #674 #707: 17-18 workers beat 35) 49self .assertEqual (setup .plan_pool_workers (80 , [20 ,20 ]),18 ) 50self .assertEqual (setup .plan_pool_workers (48 , [6 ,18 ]),5 ) 51self .assertEqual (setup .plan_pool_workers (1 , [1 ]),1 ) 52 53def test_hybrid (self ): 54self .assertEqual (setup .plan_pool_workers (20 , [20 ],hybrid = (8 ,12 )),13 )# Core Ultra 7 265 (measured) 55self .assertEqual (setup .plan_pool_workers (32 , [24 ],hybrid = (8 ,16 )),15 )# i9-14900KF (#642) 56self .assertEqual (setup .plan_pool_workers (24 , [16 ],hybrid = (8 ,8 )),15 )# not more E-cores than P-cores 57self .assertEqual (setup .hybrid_pool_workers ((8 ,12 )),13 )# setup's own rule agrees 58 59def test_unknown_layout_and_quota (self ): 60self .assertLessEqual (setup .plan_pool_workers (256 , []),setup .POOL_WORKERS_CAP ) 61self .assertLess (setup .plan_pool_workers (256 ,None ),255 ) 62self .assertEqual (setup .plan_pool_workers (8 ,None ),7 ) 63self .assertEqual (setup .plan_pool_workers (128 , [64 ],quota = 4.0 ),3 )# a container limit of 4 CPUs 64 65def test_never_writes_a_big_count (self ): 66# the hybrid rule (P-cores + half the E-cores) is capped like every other count 67with mock .patch .object (setup ,"cpu_cores" ,return_value = (8 ,120 )): 68args = setup .recommend_pool_workers (["--spec" ,"4" ]) 69n = int (args [args .index ("--pool-workers" )+ 1 ]) 70self .assertLessEqual (n ,setup .POOL_WORKERS_CAP ) 71with mock .patch .object (setup ,"cpu_cores" ,return_value = (8 ,16 )):# an ordinary hybrid: the usual 15 72args = setup .recommend_pool_workers ([]) 73self .assertEqual (args , ["--pool-workers" ,"15" ]) 74with mock .patch .object (setup ,"cpu_cores" ,return_value = None ):# not hybrid: nothing written 75self .assertEqual (setup .recommend_pool_workers (["--spec" ,"4" ]), ["--spec" ,"4" ]) 76# a count that is already there is never changed 77with mock .patch .object (setup ,"cpu_cores" ,return_value = (8 ,120 )): 78self .assertEqual (setup .recommend_pool_workers (["--pool-workers" ,"200" ]), ["--pool-workers" ,"200" ]) 79 80def test_vision_threads (self ): 81self .assertEqual (setup .vision_threads (16 ),8 ) 82self .assertEqual (setup .vision_threads (256 ),setup .VISION_THREADS_CAP ) 83self .assertEqual (setup .vision_threads (1 ),1 ) 84 85 86class ConfiguredCountWarns (unittest .TestCase ): 87big = (256 , [64 ,64 ]) 88 89def test_warns_above_the_plan_and_keeps_the_count (self ): 90args = ["--pool-workers" ,"255" ] 91note = setup .pool_workers_note (args ,self .big ) 92self .assertEqual (len (note ),1 ) 93self .assertIn ("--pool-workers %d" % setup .plan_pool_workers (* self .big ),note [0 ]) 94self .assertIn ("stays as you set it" ,note [0 ]) 95self .assertEqual (args , ["--pool-workers" ,"255" ])# nothing rewritten 96 97def test_quiet_when_fine (self ): 98self .assertEqual (setup .pool_workers_note ([],self .big ), []) 99self .assertEqual (setup .pool_workers_note (["--pool-workers" ,"8" ],self .big ), []) 100self .assertEqual (setup .pool_workers_note (["--pool-workers" ,str (setup .plan_pool_workers (* self .big ))],self .big ), []) 101self .assertEqual (setup .pool_workers_note (["--pool-workers" ],self .big ), []) 102self .assertEqual (setup .pool_workers_note (["--pool-workers" ,"x" ],self .big ), []) 103 104def test_reasons (self ): 105self .assertIn ("CPU threads" ,setup .pool_workers_note (["--pool-workers" ,"300" ],self .big )[0 ]) 106self .assertIn ("physical cores" ,setup .pool_workers_note (["--pool-workers" ,"200" ],self .big )[0 ]) 107self .assertIn ("one CPU socket" ,setup .pool_workers_note (["--pool-workers" ,"30" ], (80 , [20 ,20 ]))[0 ]) 108 109 110if __name__ == "__main__" : 111unittest .main ()