| 1 |
|
| 2 |
import argparse |
| 3 |
import os |
| 4 |
from pathlib import Path |
| 5 |
import platform |
| 6 |
import re |
| 7 |
import subprocess |
| 8 |
from typing import Dict, List, Optional |
| 9 |
|
| 10 |
PARSER = argparse.ArgumentParser( |
| 11 |
description="Run print-sys-info to detect CPU features. Fail if the results seem wrong.") |
| 12 |
PARSER.add_argument("--print-sys-info-path", |
| 13 |
help="Path to print-sys-info app") |
| 14 |
PARSER.add_argument("--build-dir", |
| 15 |
help="Search this dir for print-sys-info app") |
| 16 |
|
| 17 |
|
| 18 |
FEATURE_ALIASES = { |
| 19 |
'avx512': ['avx512f'], |
| 20 |
'clmul': ['pclmulqdq'], |
| 21 |
'crc': ['crc32'], |
| 22 |
'crypto': ['aes'], |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
def main(): |
| 27 |
args = PARSER.parse_args() |
| 28 |
|
| 29 |
if args.print_sys_info_path: |
| 30 |
app = Path(args.print_sys_info_path) |
| 31 |
else: |
| 32 |
app = find_app(args.build_dir) |
| 33 |
|
| 34 |
|
| 35 |
app_features_presence: Dict[str, bool] = detect_features_from_app(app) |
| 36 |
|
| 37 |
|
| 38 |
os_features_list = detect_features_from_os() |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
for (feature, app_presence) in app_features_presence.items(): |
| 43 |
os_presence = feature in os_features_list |
| 44 |
if not os_presence: |
| 45 |
|
| 46 |
for alias in FEATURE_ALIASES.get(feature, []): |
| 47 |
if alias in os_features_list: |
| 48 |
os_presence = True |
| 49 |
break |
| 50 |
|
| 51 |
if app_presence != os_presence: |
| 52 |
exit(f"FAILED: aws-c-common and OS disagree on whether CPU supports feature '{feature}'\n" + |
| 53 |
f"\taws-c-common:{app_presence} OS:{os_presence}") |
| 54 |
|
| 55 |
print("SUCCESS: aws-c-common and OS agree on CPU features") |
| 56 |
|
| 57 |
|
| 58 |
def find_app(build_dir: Optional[str]) -> Path: |
| 59 |
if build_dir is None: |
| 60 |
build_dir = find_build_dir() |
| 61 |
else: |
| 62 |
build_dir = Path(build_dir).absolute() |
| 63 |
|
| 64 |
app_name = 'print-sys-info' |
| 65 |
if os.name == 'nt': |
| 66 |
app_name = app_name + '.exe' |
| 67 |
|
| 68 |
for file in build_dir.glob(f"**/{app_name}"): |
| 69 |
return file |
| 70 |
|
| 71 |
exit(f"FAILED: Can't find '{app_name}' under: {build_dir}" |
| 72 |
"\nPass --build-dir to hint location.") |
| 73 |
|
| 74 |
|
| 75 |
def find_build_dir() -> Path: |
| 76 |
dir = Path(__file__).parent.absolute() |
| 77 |
while dir is not None: |
| 78 |
for build_dir in dir.glob('build'): |
| 79 |
return build_dir |
| 80 |
dir = dir.parent |
| 81 |
|
| 82 |
exit("FAILED: Can't find build dir. Pass --build-dir to hint location.") |
| 83 |
|
| 84 |
|
| 85 |
def detect_features_from_app(app_path: Path) -> Dict[str, bool]: |
| 86 |
result = subprocess.run([str(app_path)], |
| 87 |
universal_newlines=True, |
| 88 |
stdout=subprocess.PIPE, |
| 89 |
stderr=subprocess.PIPE) |
| 90 |
print(f"--- {app_path} ---") |
| 91 |
print(result.stderr) |
| 92 |
print(result.stdout) |
| 93 |
if result.returncode != 0: |
| 94 |
exit(f"FAILED: {app_path.name} exited with {result.returncode}") |
| 95 |
|
| 96 |
lines = result.stdout.splitlines() |
| 97 |
|
| 98 |
machine = platform.machine().lower() |
| 99 |
if machine in ['x86_64', 'amd64']: |
| 100 |
machine = 'amd' |
| 101 |
elif machine.startswith('arm') or machine == 'aarch64': |
| 102 |
machine = 'arm' |
| 103 |
else: |
| 104 |
print(f"SKIP TEST: unknown platform.machine(): {machine}") |
| 105 |
exit(0) |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
features = {} |
| 111 |
for line in lines: |
| 112 |
m = re.search(f"'{machine}_(.+)': (false|true)", line) |
| 113 |
if m: |
| 114 |
name = m.group(1) |
| 115 |
is_present = m.group(2) == 'true' |
| 116 |
features[name] = is_present |
| 117 |
|
| 118 |
|
| 119 |
for line in lines: |
| 120 |
m = re.search(f"'use cpu extensions': false", line) |
| 121 |
if m: |
| 122 |
print("SKIP TEST: aws-c-common compiled with -DUSE_CPU_EXTENSIONS=OFF") |
| 123 |
exit(0) |
| 124 |
|
| 125 |
if not features: |
| 126 |
raise RuntimeError("Cannot find features text in stdout ???") |
| 127 |
|
| 128 |
return features |
| 129 |
|
| 130 |
|
| 131 |
def detect_features_from_os() -> List[str]: |
| 132 |
features = [] |
| 133 |
|
| 134 |
cpuinfo_path = '/proc/cpuinfo' |
| 135 |
try: |
| 136 |
with open(cpuinfo_path) as f: |
| 137 |
cpuinfo_text = f.read() |
| 138 |
except: |
| 139 |
print(f"SKIP TEST: currently, this test only works on machines with /proc/cpuinfo") |
| 140 |
exit(0) |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
print(f"--- {cpuinfo_path} ---") |
| 147 |
for line in cpuinfo_text.splitlines(): |
| 148 |
line = line.lower() |
| 149 |
print(line) |
| 150 |
m = re.match(r"(flags|features)\s+:(.*)", line) |
| 151 |
if m: |
| 152 |
features = m.group(2).split() |
| 153 |
return features |
| 154 |
|
| 155 |
exit(f"FAILED: Cannot detect CPU features in {cpuinfo_path}") |
| 156 |
|
| 157 |
|
| 158 |
if __name__ == '__main__': |
| 159 |
main() |