all.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2019 Intel Corporation. All rights reserved.
  4. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. #
  6. import argparse
  7. import multiprocessing as mp
  8. import os
  9. import pathlib
  10. import subprocess
  11. import sys
  12. import time
  13. """
  14. The script itself has to be put under the same directory with the "spec".
  15. To run a single non-GC case with interpreter mode:
  16. cd workspace
  17. python3 runtest.py --wast2wasm wabt/bin/wat2wasm --interpreter iwasm \
  18. spec/test/core/xxx.wast
  19. To run a single non-GC case with aot mode:
  20. cd workspace
  21. python3 runtest.py --aot --wast2wasm wabt/bin/wat2wasm --interpreter iwasm \
  22. --aot-compiler wamrc spec/test/core/xxx.wast
  23. To run a single GC case:
  24. cd workspace
  25. python3 runtest.py --wast2wasm spec/interpreter/wasm --interpreter iwasm \
  26. --aot-compiler wamrc --gc spec/test/core/xxx.wast
  27. """
  28. PLATFORM_NAME = os.uname().sysname.lower()
  29. IWASM_CMD = "../../../product-mini/platforms/" + PLATFORM_NAME + "/build/iwasm"
  30. IWASM_SGX_CMD = "../../../product-mini/platforms/linux-sgx/enclave-sample/iwasm"
  31. IWASM_QEMU_CMD = "iwasm"
  32. SPEC_TEST_DIR = "spec/test/core"
  33. WAST2WASM_CMD = "./wabt/out/gcc/Release/wat2wasm"
  34. SPEC_INTERPRETER_CMD = "spec/interpreter/wasm"
  35. WAMRC_CMD = "../../../wamr-compiler/build/wamrc"
  36. class TargetAction(argparse.Action):
  37. TARGET_MAP = {
  38. "ARMV7_VFP": "armv7",
  39. "RISCV32": "riscv32_ilp32",
  40. "RISCV32_ILP32": "riscv32_ilp32",
  41. "RISCV32_ILP32D": "riscv32_ilp32d",
  42. "RISCV64": "riscv64_lp64",
  43. "RISCV64_LP64": "riscv64_lp64",
  44. "RISCV64_LP64D": "riscv64_lp64",
  45. "THUMBV7_VFP": "thumbv7",
  46. "X86_32": "i386",
  47. "X86_64": "x86_64",
  48. }
  49. def __call__(self, parser, namespace, values, option_string=None):
  50. setattr(namespace, self.dest, self.TARGET_MAP.get(values, "x86_64"))
  51. def ignore_the_case(
  52. case_name,
  53. target,
  54. aot_flag=False,
  55. sgx_flag=False,
  56. multi_module_flag=False,
  57. multi_thread_flag=False,
  58. simd_flag=False,
  59. gc_flag=False,
  60. xip_flag=False,
  61. qemu_flag=False
  62. ):
  63. if case_name in ["comments", "inline-module", "names"]:
  64. return True
  65. if not multi_module_flag and case_name in ["imports", "linking"]:
  66. return True
  67. if "i386" == target and case_name in ["float_exprs"]:
  68. return True
  69. if gc_flag:
  70. if case_name in ["type-canon", "type-equivalence", "type-rec"]:
  71. return True;
  72. if sgx_flag:
  73. if case_name in ["conversions", "f32_bitwise", "f64_bitwise"]:
  74. return True
  75. if aot_flag and case_name in [
  76. "call_indirect",
  77. "call",
  78. "fac",
  79. "skip-stack-guard-page",
  80. ]:
  81. return True
  82. if qemu_flag:
  83. if case_name in ["f32_bitwise", "f64_bitwise", "loop", "f64", "f64_cmp",
  84. "conversions", "f32", "f32_cmp", "float_exprs",
  85. "float_misc", "select", "memory_grow"]:
  86. return True
  87. return False
  88. def preflight_check(aot_flag):
  89. if not pathlib.Path(SPEC_TEST_DIR).resolve().exists():
  90. print(f"Can not find {SPEC_TEST_DIR}")
  91. return False
  92. if not pathlib.Path(WAST2WASM_CMD).resolve().exists():
  93. print(f"Can not find {WAST2WASM_CMD}")
  94. return False
  95. if aot_flag and not pathlib.Path(WAMRC_CMD).resolve().exists():
  96. print(f"Can not find {WAMRC_CMD}")
  97. return False
  98. return True
  99. def test_case(
  100. case_path,
  101. target,
  102. aot_flag=False,
  103. sgx_flag=False,
  104. multi_module_flag=False,
  105. multi_thread_flag=False,
  106. simd_flag=False,
  107. xip_flag=False,
  108. clean_up_flag=True,
  109. verbose_flag=True,
  110. gc_flag=False,
  111. qemu_flag=False,
  112. qemu_firmware='',
  113. log='',
  114. ):
  115. case_path = pathlib.Path(case_path).resolve()
  116. case_name = case_path.stem
  117. if ignore_the_case(
  118. case_name,
  119. target,
  120. aot_flag,
  121. sgx_flag,
  122. multi_module_flag,
  123. multi_thread_flag,
  124. simd_flag,
  125. gc_flag,
  126. xip_flag,
  127. qemu_flag
  128. ):
  129. return True
  130. CMD = ["python3", "runtest.py"]
  131. CMD.append("--wast2wasm")
  132. CMD.append(WAST2WASM_CMD if not gc_flag else SPEC_INTERPRETER_CMD)
  133. CMD.append("--interpreter")
  134. if sgx_flag:
  135. CMD.append(IWASM_SGX_CMD)
  136. elif qemu_flag:
  137. CMD.append(IWASM_QEMU_CMD)
  138. else:
  139. CMD.append(IWASM_CMD)
  140. CMD.append("--aot-compiler")
  141. CMD.append(WAMRC_CMD)
  142. if aot_flag:
  143. CMD.append("--aot")
  144. CMD.append("--target")
  145. CMD.append(target)
  146. if multi_module_flag:
  147. CMD.append("--multi-module")
  148. if multi_thread_flag:
  149. CMD.append("--multi-thread")
  150. if sgx_flag:
  151. CMD.append("--sgx")
  152. if simd_flag:
  153. CMD.append("--simd")
  154. if xip_flag:
  155. CMD.append("--xip")
  156. if qemu_flag:
  157. CMD.append("--qemu")
  158. CMD.append("--qemu-firmware")
  159. CMD.append(qemu_firmware)
  160. if not clean_up_flag:
  161. CMD.append("--no_cleanup")
  162. if gc_flag:
  163. CMD.append("--gc")
  164. if log != '':
  165. CMD.append("--log-dir")
  166. CMD.append(log)
  167. CMD.append(case_path)
  168. print(f"============> run {case_name} ", end="")
  169. with subprocess.Popen(
  170. CMD,
  171. bufsize=1,
  172. stdout=subprocess.PIPE,
  173. stderr=subprocess.PIPE,
  174. universal_newlines=True,
  175. ) as p:
  176. try:
  177. case_last_words = []
  178. while not p.poll():
  179. output = p.stdout.readline()
  180. if not output:
  181. break
  182. if verbose_flag:
  183. print(output, end="")
  184. else:
  185. if len(case_last_words) == 16:
  186. case_last_words.pop(0)
  187. case_last_words.append(output)
  188. p.wait(60)
  189. if p.returncode:
  190. print(f"failed with a non-zero return code {p.returncode}")
  191. if not verbose_flag:
  192. print(
  193. f"\n==================== LAST LOG of {case_name} ====================\n"
  194. )
  195. print("".join(case_last_words))
  196. print("\n==================== LAST LOG END ====================\n")
  197. raise Exception(case_name)
  198. else:
  199. print("successful")
  200. return True
  201. except subprocess.CalledProcessError:
  202. print("failed with CalledProcessError")
  203. raise Exception(case_name)
  204. except subprocess.TimeoutExpired:
  205. print("failed with TimeoutExpired")
  206. raise Exception(case_name)
  207. def test_suite(
  208. target,
  209. aot_flag=False,
  210. sgx_flag=False,
  211. multi_module_flag=False,
  212. multi_thread_flag=False,
  213. simd_flag=False,
  214. xip_flag=False,
  215. clean_up_flag=True,
  216. verbose_flag=True,
  217. gc_flag=False,
  218. parl_flag=False,
  219. qemu_flag=False,
  220. qemu_firmware='',
  221. log='',
  222. ):
  223. suite_path = pathlib.Path(SPEC_TEST_DIR).resolve()
  224. if not suite_path.exists():
  225. print(f"can not find spec test cases at {suite_path}")
  226. return False
  227. case_list = sorted(suite_path.glob("*.wast"))
  228. if simd_flag:
  229. simd_case_list = sorted(suite_path.glob("simd/*.wast"))
  230. case_list.extend(simd_case_list)
  231. if gc_flag:
  232. gc_case_list = sorted(suite_path.glob("gc/*.wast"))
  233. case_list.extend(gc_case_list)
  234. case_count = len(case_list)
  235. failed_case = 0
  236. successful_case = 0
  237. if parl_flag:
  238. print(f"----- Run the whole spec test suite on {mp.cpu_count()} cores -----")
  239. with mp.Pool() as pool:
  240. results = {}
  241. for case_path in case_list:
  242. results[case_path.stem] = pool.apply_async(
  243. test_case,
  244. [
  245. str(case_path),
  246. target,
  247. aot_flag,
  248. sgx_flag,
  249. multi_module_flag,
  250. multi_thread_flag,
  251. simd_flag,
  252. xip_flag,
  253. clean_up_flag,
  254. verbose_flag,
  255. gc_flag,
  256. qemu_flag,
  257. qemu_firmware,
  258. log,
  259. ],
  260. )
  261. for case_name, result in results.items():
  262. try:
  263. if qemu_flag:
  264. # 60 min / case, testing on QEMU may be very slow
  265. result.wait(7200)
  266. else:
  267. # 5 min / case
  268. result.wait(300)
  269. if not result.successful():
  270. failed_case += 1
  271. else:
  272. successful_case += 1
  273. except mp.TimeoutError:
  274. print(f"{case_name} meets TimeoutError")
  275. failed_case += 1
  276. else:
  277. print(f"----- Run the whole spec test suite -----")
  278. for case_path in case_list:
  279. try:
  280. test_case(
  281. str(case_path),
  282. target,
  283. aot_flag,
  284. sgx_flag,
  285. multi_module_flag,
  286. multi_thread_flag,
  287. simd_flag,
  288. xip_flag,
  289. clean_up_flag,
  290. verbose_flag,
  291. gc_flag,
  292. qemu_flag,
  293. qemu_firmware,
  294. log,
  295. )
  296. successful_case += 1
  297. except Exception as e:
  298. failed_case += 1
  299. raise e
  300. print(
  301. f"IN ALL {case_count} cases: {successful_case} PASS, {failed_case} FAIL, {case_count - successful_case - failed_case} SKIP"
  302. )
  303. return 0 == failed_case
  304. def main():
  305. parser = argparse.ArgumentParser(description="run the whole spec test suite")
  306. parser.add_argument(
  307. "-M",
  308. action="store_true",
  309. default=False,
  310. dest="multi_module_flag",
  311. help="Running with the Multi-Module feature",
  312. )
  313. parser.add_argument(
  314. "-m",
  315. action=TargetAction,
  316. choices=list(TargetAction.TARGET_MAP.keys()),
  317. type=str,
  318. dest="target",
  319. default="X86_64",
  320. help="Specify Target ",
  321. )
  322. parser.add_argument(
  323. "-p",
  324. action="store_true",
  325. default=False,
  326. dest="multi_thread_flag",
  327. help="Running with the Multi-Thread feature",
  328. )
  329. parser.add_argument(
  330. "-S",
  331. action="store_true",
  332. default=False,
  333. dest="simd_flag",
  334. help="Running with the SIMD feature",
  335. )
  336. parser.add_argument(
  337. "-X",
  338. action="store_true",
  339. default=False,
  340. dest="xip_flag",
  341. help="Running with the XIP feature",
  342. )
  343. parser.add_argument(
  344. "-t",
  345. action="store_true",
  346. default=False,
  347. dest="aot_flag",
  348. help="Running with AOT mode",
  349. )
  350. parser.add_argument(
  351. "-x",
  352. action="store_true",
  353. default=False,
  354. dest="sgx_flag",
  355. help="Running with SGX environment",
  356. )
  357. parser.add_argument(
  358. "--no_clean_up",
  359. action="store_false",
  360. default=True,
  361. dest="clean_up_flag",
  362. help="Does not remove tmpfiles. But it will be enabled while running parallelly",
  363. )
  364. parser.add_argument(
  365. "--parl",
  366. action="store_true",
  367. default=False,
  368. dest="parl_flag",
  369. help="To run whole test suite parallelly",
  370. )
  371. parser.add_argument(
  372. "--qemu",
  373. action="store_true",
  374. default=False,
  375. dest="qemu_flag",
  376. help="To run whole test suite in qemu",
  377. )
  378. parser.add_argument(
  379. "--qemu-firmware",
  380. default="",
  381. dest="qemu_firmware",
  382. help="Firmware required by qemu",
  383. )
  384. parser.add_argument(
  385. "--log",
  386. default='',
  387. dest="log",
  388. help="Log directory",
  389. )
  390. parser.add_argument(
  391. "--quiet",
  392. action="store_false",
  393. default=True,
  394. dest="verbose_flag",
  395. help="Close real time output while running cases, only show last words of failed ones",
  396. )
  397. parser.add_argument(
  398. "--gc",
  399. action="store_true",
  400. default=False,
  401. dest="gc_flag",
  402. help="Running with GC feature",
  403. )
  404. parser.add_argument(
  405. "cases",
  406. metavar="path_to__case",
  407. type=str,
  408. nargs="*",
  409. help=f"Specify all wanted cases. If not the script will go through all cases under {SPEC_TEST_DIR}",
  410. )
  411. options = parser.parse_args()
  412. print(options)
  413. if not preflight_check(options.aot_flag):
  414. return False
  415. if not options.cases:
  416. if options.parl_flag:
  417. # several cases might share the same workspace/tempfile at the same time
  418. # so, disable it while running parallelly
  419. options.clean_up_flag = False
  420. options.verbose_flag = False
  421. start = time.time_ns()
  422. ret = test_suite(
  423. options.target,
  424. options.aot_flag,
  425. options.sgx_flag,
  426. options.multi_module_flag,
  427. options.multi_thread_flag,
  428. options.simd_flag,
  429. options.xip_flag,
  430. options.clean_up_flag,
  431. options.verbose_flag,
  432. options.gc_flag,
  433. options.parl_flag,
  434. options.qemu_flag,
  435. options.qemu_firmware,
  436. options.log,
  437. )
  438. end = time.time_ns()
  439. print(
  440. f"It takes {((end - start) / 1000000):,} ms to run test_suite {'parallelly' if options.parl_flag else ''}"
  441. )
  442. else:
  443. try:
  444. for case in options.cases:
  445. test_case(
  446. case,
  447. options.target,
  448. options.aot_flag,
  449. options.sgx_flag,
  450. options.multi_module_flag,
  451. options.multi_thread_flag,
  452. options.simd_flag,
  453. options.xip_flag,
  454. options.clean_up_flag,
  455. options.verbose_flag,
  456. options.gc_flag,
  457. options.qemu_flag,
  458. options.qemu_firmware,
  459. options.log
  460. )
  461. else:
  462. ret = True
  463. except Exception:
  464. ret = False
  465. return ret
  466. if __name__ == "__main__":
  467. sys.exit(0 if main() else 1)