Skip to content

Commit

Permalink
aliases for feature names
Browse files Browse the repository at this point in the history
  • Loading branch information
graebm committed Jan 28, 2025
1 parent 5a4318f commit ead4604
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
4 changes: 2 additions & 2 deletions bin/system_info/print_system_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ int main(void) {
fprintf(stdout, " 'arm_crypto': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_ARM_CRYPTO) ? "true" : "false");
fprintf(stdout, " 'amd_sse4_1': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_SSE_4_1) ? "true" : "false");
fprintf(stdout, " 'amd_sse4_2': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_SSE_4_2) ? "true" : "false");
fprintf(stdout, " 'amd_pclmulqdq': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_CLMUL) ? "true" : "false");
fprintf(stdout, " 'amd_clmul': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_CLMUL) ? "true" : "false");
fprintf(stdout, " 'amd_vpclmulqdq': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_VPCLMULQDQ) ? "true" : "false");
fprintf(stdout, " 'amd_avx2': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_AVX2) ? "true" : "false");
fprintf(stdout, " 'amd_avx512f': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_AVX512) ? "true" : "false");
fprintf(stdout, " 'amd_avx512': %s,\n", aws_cpu_has_feature(AWS_CPU_FEATURE_AVX512) ? "true" : "false");
fprintf(stdout, " 'amd_bmi2': %s\n", aws_cpu_has_feature(AWS_CPU_FEATURE_BMI2) ? "true" : "false");
fprintf(stdout, " }\n");

Expand Down
61 changes: 40 additions & 21 deletions bin/system_info/test-print-system-info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path
import platform
import re
from subprocess import run
import subprocess
from typing import Dict, List, Optional

PARSER = argparse.ArgumentParser(
Expand All @@ -15,6 +15,14 @@
help="Search this dir for print-sys-info app")


FEATURE_ALIASES = {
'avx512': ['avx512f'],
'clmul': ['pclmulqdq'],
'crc': ['crc32'],
'crypto': ['aes'],
}


def main():
args = PARSER.parse_args()

Expand All @@ -23,19 +31,26 @@ def main():
else:
app = find_app(args.build_dir)

# run print-sys-info. get feature names and whether it thinks they're supported
app_features_presence: Dict[str, bool] = detect_features_from_app(app)

# get feature info from OS (i.e. read /proc/cpuinfo on linux), get back list of supported features
os_features_list = detect_features_from_os()

for (feature, is_present) in app_features_presence.items():
if is_present:
if not feature in os_features_list:
exit(f"FAILED: aws-c-common thinks CPU supports '{feature}', " +
"but OS doesn't seem to think so")
else:
if feature in os_features_list:
exit("FAILED: aws-c-common doesn't think CPU supports '{feature}', " +
"but OS says it does")
# For each feature that print-sys-info says was (or wasn't) there,
# check the os_features_list and make sure it is (or isn't_ present.
for (feature, app_presence) in app_features_presence.items():
os_presence = feature in os_features_list
if not os_presence:
# sometimes a feature has a mildly different name across platforms
for alias in FEATURE_ALIASES.get(feature, []):
if alias in os_features_list:
os_presence = True
break

if app_presence != os_presence:
exit(f"FAILED: aws-c-common and OS disagree on whether CPU supports feature '{feature}'\n" +
f"\taws-c-common:{app_presence} OS:{os_presence}")

print("SUCCESS: aws-c-common and OS agree on CPU features")

Expand All @@ -44,8 +59,7 @@ def find_app(build_dir: Optional[str]) -> Path:
if build_dir is None:
build_dir = find_build_dir()
else:
build_dir = Path(build_dir)
build_dir = build_dir.absolute()
build_dir = Path(build_dir).absolute()

app_name = 'print-sys-info'
if os.name == 'nt':
Expand All @@ -59,7 +73,7 @@ def find_app(build_dir: Optional[str]) -> Path:


def find_build_dir() -> Path:
dir = Path(__file__).parent
dir = Path(__file__).parent.absolute()
while dir is not None:
for build_dir in dir.glob('build'):
return build_dir
Expand All @@ -69,9 +83,10 @@ def find_build_dir() -> Path:


def detect_features_from_app(app_path: Path) -> Dict[str, bool]:
result = run([str(app_path)],
capture_output=True,
text=True)
result = subprocess.run([str(app_path)],
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
print(f"--- {app_path} ---")
print(result.stderr)
print(result.stdout)
Expand Down Expand Up @@ -116,16 +131,20 @@ def detect_features_from_os() -> List[str]:
print(f"SKIP TEST: currently, this test only works on machines with /proc/cpuinfo")
exit(0)

# looking for line like: flags : fpu vme de pse ...
# looking for line like:
# flags : fpu vme de pse ...
# OR
# features : fp asimd evtstrm ...
print(f"--- {cpuinfo_path} ---")
for line in cpuinfo_text.splitlines():
line = line.lower()
print(line)
m = re.match(r"flags\s+:(.*)", line)
m = re.match(r"(flags|features)\s+:(.*)", line)
if m:
flags = m.group(1).split()
return flags
features = m.group(2).split()
return features

exit(f"FAILED: Cannot detect 'flags' in {cpuinfo_path}")
exit(f"FAILED: Cannot detect CPU features in {cpuinfo_path}")


if __name__ == '__main__':
Expand Down

0 comments on commit ead4604

Please sign in to comment.