Cotton code
53 lines · 2.5 KB · PythonRaw
#!/usr/bin/env python3"""Pick the simulator the fan is measured on: a ProMotion iPhone (the newest"iPhone N Pro") on iOS 26.5, the runtime the owner's phone runs. The fan'smemory grows 403 MB over 100 turns on iOS 26.5 and −1 MB on iOS 27 at thesame commit and settings (fanperf2, 15 Oct 2026), so a budget measured onanother runtime says nothing about his phone: there is no fallback, and ahost without iOS 26.5 is refused. Prints `<device type id> <runtime id>`."""import jsonimport platformimport reimport subprocessimport sysfrom pathlib import Pathsys.path.insert(0, str(Path(__file__).resolve().parent.parent / 'speedgate'))from runtime import has_dyld_cache  # noqa: E402# The owner's phone runs iOS 26.5.2 (architect, 15 Oct 2026).RUNTIME = '26.5'def choose(device_types, runtimes, host_build, arch, caches=None):    pinned = [r for r in runtimes if r.get('isAvailable') and r['identifier'].startswith('com.apple.CoreSimulator.SimRuntime.iOS-')              and (r['version'] + '.').startswith(RUNTIME + '.')]    if not pinned:        raise ValueError(f'the fan is measured on iOS {RUNTIME}, which is not installed on this host '                         f'(xcodebuild -downloadPlatform iOS -buildVersion {RUNTIME})')    # Prefer a build of it whose dyld_sim shared cache exists (one without loads    # every framework by hand — scripts/speedgate/runtime.py).    pinned.sort(key=lambda r: (has_dyld_cache(r, host_build, arch, *([caches] if caches else [])),                               tuple(map(int, r['version'].split('.')))), reverse=True)    pick = pinned[0]    # The runtime names the device types it runs; a newer phone may need a newer one.    supported = {t['identifier'] for t in pick.get('supportedDeviceTypes', device_types)}    pros = [t for t in device_types if re.fullmatch(r'iPhone [0-9]+ Pro', t['name']) and t['identifier'] in supported]    if not pros:        raise ValueError(f"no ProMotion iPhone runs on {pick['name']}")    phone = max(pros, key=lambda t: int(t['name'].split()[1]))    return phone['identifier'], pick['identifier']def main():    read = lambda kind: json.loads(subprocess.check_output(['xcrun', 'simctl', 'list', kind, '-j']))[kind]    host = subprocess.check_output(['sw_vers', '-buildVersion'], text=True).strip()    print(*choose(read('devicetypes'), read('runtimes'), host, platform.machine()))if __name__ == '__main__':    try:        main()    except (ValueError, OSError, subprocess.CalledProcessError) as error:        sys.exit(f'[fan-perf] FAILED: {error}')