Skip to content

Commit

Permalink
Update CPU scripts for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
minseongg committed Sep 3, 2024
1 parent b07a603 commit 6aaf7bc
Show file tree
Hide file tree
Showing 12 changed files with 1,502 additions and 193 deletions.
10 changes: 8 additions & 2 deletions scripts/cpu/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
__pycache__
openroad/*
!openroad/nangate45
openroad/logs
openroad/objects
openroad/reports
openroad/results
openroad/vsrc/*
!openroad/vsrc/Core.v
!openroad/vsrc/CoreWrapper.v
!openroad/vsrc/CSRFileWrapper.v
output
emulator-debug
77 changes: 77 additions & 0 deletions scripts/cpu/bench_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python3

import os
import sys
import subprocess
from pathlib import Path
from rich import print
from rich.console import Console

from trace import *
from cpi import *
from constants import *


console = Console()


def run_tests(total_tests):
count = 0

for tb in bench_dir.iterdir():
tb_filename = tb.name
if tb.is_file() and not tb.suffix in [".dump", ".trace"]:
count += 1
vcd_option = f"-v{log_dir}/{tb_filename}.vcd" if waves_flag else ""
txt_file = log_dir / f"{tb_filename}.txt"

console.print(f"Extract trace from benchmark ({count}/{total_tests}): {tb_filename} .. ", end="")
result = subprocess.run(
f"{emulator} {vcd_option} +max-cycles=100000 {tb}",
stdout=open(txt_file, "w"),
stderr=subprocess.STDOUT,
shell=True
)

console.print("DONE")


if __name__ == "__main__":
# Current file absolute directory path
curr_dir = Path(__file__).resolve().parent
log_dir = curr_dir / "output"
emulator = curr_dir / "emulator-debug"
bench_dir = curr_dir / "program/bench"

# Check if the emulator exists
if not emulator.is_file():
logger.error(f"{emulator} does not exist.")
logger.error("Please run `python3 scripts/cpu/build.py` first.")
sys.exit(1)

# Flags
trace_flag = False
cpi_flag = False
waves_flag = False

# Parse arguments
for arg in sys.argv[1:]:
if arg == "trace":
trace_flag = True
elif arg == "cpi":
cpi_flag = True
elif arg == "--waves":
waves_flag = True

# Ensure log directory exists
log_dir.mkdir(parents=True, exist_ok=True)

# Running tests
if trace_flag:
logger.info("Running benchmark trace tests")
run_tests(9)
check_trace()
elif cpi_flag:
logger.info("Running benchmark cpi tests")
run_tests(9)
calculate_cpi_hf("branch_prediction")
79 changes: 0 additions & 79 deletions scripts/cpu/bench_test.sh

This file was deleted.

1 change: 1 addition & 0 deletions scripts/cpu/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def build_core():
"--wire-cache",
"--deadcode",
"--merge",
"--system-task",
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
Expand Down
2 changes: 1 addition & 1 deletion scripts/cpu/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
hazardflow_dir = hazardflow_dir = pathlib.Path(__file__).absolute().parent.parent.parent
cpu_script_dir = hazardflow_dir / "scripts" / "cpu"

openroad_flow = os.environ['OPENROAD_FLOW']
openroad_flow = os.environ.get("OPENROAD_FLOW")

sodor_dir = hazardflow_dir / "riscv-sodor"

Expand Down
88 changes: 88 additions & 0 deletions scripts/cpu/isa_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env python3

import os
import sys
import subprocess
from pathlib import Path
from rich import print
from rich.console import Console

from constants import *


console = Console()


def run_tests(test_dir, total_tests):
count = 0
count_passed = 0
count_failed = 0

for tb in test_dir.iterdir():
tb_filename = tb.name
if tb.is_file() and not tb.suffix == ".dump":
count += 1
vcd_option = f"-v{log_dir}/{tb_filename}.vcd" if waves_flag else ""
txt_file = log_dir / f"{tb_filename}.txt"

console.print(f"Test ({count}/{total_tests}): {tb_filename} .. ", end="")
result = subprocess.run(
f"{emulator} {vcd_option} {tb}",
stdout=open(txt_file, "w"),
stderr=subprocess.STDOUT,
shell=True,
)

if result.returncode == 0:
console.print("[green]PASSED[/green]")
count_passed += 1
else:
console.print("[red]FAILED[/red]")
count_failed += 1

logger.info(f"Number of success tests: {count_passed} / {total_tests}")

if count_failed > 0:
logger.error(f"You can check the log file for failed test cases in `{cpu_script_dir}/output` directory.")
sys.exit(1)


if __name__ == "__main__":
# Setup
curr_dir = Path(__file__).resolve().parent
log_dir = curr_dir / "output"
emulator = curr_dir / "emulator-debug"

# Check if the emulator exists
if not emulator.is_file():
logger.error(f"{emulator} does not exist.")
logger.error("Please run `python3 scripts/cpu/build.py` first.")
sys.exit(1)

# Flags
base_flag = False
mext_flag = False
waves_flag = False

# Parse arguments
for arg in sys.argv[1:]:
if arg == "base":
base_flag = True
elif arg == "mext":
mext_flag = True
elif arg == "--waves":
waves_flag = True

# Ensure log directory exists
log_dir.mkdir(parents=True, exist_ok=True)

# Running tests
if base_flag:
logger.info("Running base ISA tests")
base_dir = curr_dir / "program/isa/base"
run_tests(base_dir, 43)

elif mext_flag:
logger.info("Running M-extension ISA tests")
mext_dir = curr_dir / "program/isa/mext"
run_tests(mext_dir, 8)
106 changes: 0 additions & 106 deletions scripts/cpu/isa_test.sh

This file was deleted.

Loading

0 comments on commit 6aaf7bc

Please sign in to comment.