Skip to content

Commit c3921df

Browse files
authored
Fix testing on Ubuntu 19.10 (#12)
* Get code_benches running on my machine Ubuntu 19.10, Rust 1.45.0, LLVM 9.0.0 Changes: - Replaced shopt with python's glob.glob. Needed as Linux defaults to sh with shell=True, there is an executable argument to allow passing bash, but I'm not sure it's portable to other OSs - Added -DSQLITE_MUTEX_NOOP and -ldl to get custom_sqlite compiling - Disabled app_nn for now, depends on arm_math and won't work portably - Added -I. to tinycrypt since it seems to use <anglebrackets> for its includes. * Minor cleanup in run.py compile_to_executable * Added lightweight integration into `cargo test` Also: - code_benches/run.py to cd into code_benches - ./run.py vs ./run.py --release - chmod +x run.py * Changes from feedback - cleaned up imports, code style - added timestamp based query for which silverfish version to use - added a bit more prints to run.py * Fixed include paths for app_* tests
1 parent b5fe80d commit c3921df

File tree

3 files changed

+83
-10
lines changed

3 files changed

+83
-10
lines changed

code_benches/run.py

100644100755
Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
#!/usr/bin/env python3
2+
3+
from itertools import chain
14
import csv
5+
import glob
26
import os
37
import subprocess as sp
48
import sys
@@ -13,13 +17,32 @@
1317
# CSV file name
1418
CSV_NAME = "benchmarks.csv"
1519

20+
# Make sure we're in the code_benches directory
21+
if os.path.dirname(sys.argv[0]):
22+
os.chdir(os.path.dirname(sys.argv[0]))
1623
# Absolute path to the `code_benches` directory
1724
BENCH_ROOT = os.getcwd()
1825
# Absolute path to the `silverfish` directory
1926
ROOT_PATH = os.path.dirname(BENCH_ROOT)
2027

2128
RUNTIME_PATH = ROOT_PATH + "/runtime"
22-
SILVERFISH_PATH = ROOT_PATH + "/target/release/silverfish"
29+
30+
SILVERFISH_RELEASE_PATH = ROOT_PATH + "/target/release/silverfish"
31+
SILVERFISH_DEBUG_PATH = ROOT_PATH + "/target/debug/silverfish"
32+
assert all(arg in {"--release", "--debug"} for arg in sys.argv[1:])
33+
if "--release" in sys.argv:
34+
SILVERFISH_PATH = SILVERFISH_RELEASE_PATH
35+
elif "--debug" in sys.argv:
36+
SILVERFISH_PATH = SILVERFISH_DEBUG_PATH
37+
else:
38+
def getmtime_or_zero(path):
39+
try:
40+
return os.path.getmtime(path)
41+
except FileNotFoundError:
42+
return 0
43+
SILVERFISH_PATH = max(
44+
[SILVERFISH_RELEASE_PATH, SILVERFISH_DEBUG_PATH],
45+
key=getmtime_or_zero)
2346

2447
WASMCEPTION_PATH = ROOT_PATH + "/wasmception"
2548

@@ -61,6 +84,17 @@ def __init__(self, name, parameters, stack_size, custom_arguments=None, do_lto=T
6184
def __str__(self):
6285
return "{}({})".format(self.name, " ".join(map(str, self.parameters)))
6386

87+
def sources(self):
88+
# glob here, avoids issues with glob expansion in shell
89+
if self.is_cpp:
90+
patterns = ["*.c", "*.cpp"]
91+
else:
92+
patterns = ["*.c"]
93+
94+
paths = (os.path.join(self.name, pattern) for pattern in patterns)
95+
sources = chain.from_iterable(glob.glob(path) for path in paths)
96+
return " ".join(source[len(self.name)+1:] for source in sources)
97+
6498

6599
# These are the programs we're going to test with
66100
# TODO: Fix ispell, which doesn't compile on OS X
@@ -76,13 +110,13 @@ def __str__(self):
76110
"-Wno-shift-negative-value"]),
77111
Program("custom_matrix_multiply", [], 2 ** 14),
78112
Program("custom_memcmp", [], 2 ** 14),
79-
Program("custom_sqlite", [], 2 ** 15),
113+
Program("custom_sqlite", [], 2 ** 15, custom_arguments=["-DSQLITE_MUTEX_NOOP", "-ldl"]),
80114

81115
# == Apps ==
82-
Program("app_nn", [], 2 ** 14, custom_arguments=["-std=c99", "-Wno-unknown-attributes", "-DARM_MATH_CM3", "-I/Users/peachg/Projects/CMSIS_5_NN/CMSIS_5/CMSIS/DSP/Include", "-I/Users/peachg/Projects/CMSIS_5_NN/CMSIS_5/CMSIS/Core/Include", "-I/Users/peachg/Projects/CMSIS_5_NN/CMSIS_5/CMSIS/NN/Include"]),
116+
#Program("app_nn", [], 2 ** 14, custom_arguments=["-std=c99", "-Wno-unknown-attributes", "-DARM_MATH_CM3", "-I/Users/peachg/Projects/CMSIS_5_NN/CMSIS_5/CMSIS/DSP/Include", "-I/Users/peachg/Projects/CMSIS_5_NN/CMSIS_5/CMSIS/Core/Include", "-I/Users/peachg/Projects/CMSIS_5_NN/CMSIS_5/CMSIS/NN/Include"]),
83117
Program("app_pid", ["-std=c++11", "-Wall"], 2 ** 8, custom_arguments=[], is_cpp=True),
84118
Program("app_tiny_ekf", ["-std=c++11", "-Wall"], 2 ** 14, custom_arguments=[], is_cpp=True),
85-
Program("app_tinycrypt", [], 2 ** 15 + 2**14, custom_arguments=[ "-Wall", "-Wpedantic", "-Wno-gnu-zero-variadic-macro-arguments", "-std=c11", "-I/Users/peachg/Projects/silverfish/code_benches/app_tinycrypt/", "-DENABLE_TESTS"]),
119+
Program("app_tinycrypt", [], 2 ** 15 + 2**14, custom_arguments=[ "-Wall", "-Wpedantic", "-Wno-gnu-zero-variadic-macro-arguments", "-std=c11", "-DENABLE_TESTS", "-I."]),
86120
# Program("app_v9", [], 2 ** 18, custom_arguments=[], do_lto=False),
87121

88122
# == MiBench ==
@@ -165,17 +199,20 @@ def compile_to_executable(program):
165199
if ENABLE_DEBUG_SYMBOLS:
166200
opt += " -g"
167201
if program.is_cpp:
168-
sp.check_call("shopt -s nullglob; clang++ {} -lm {} *.c *.cpp -o bin/{}".format(program.custom_arguments, opt, program.name), shell=True, cwd=program.name)
202+
clang = "clang++"
169203
else:
170-
sp.check_call("clang {} -lm {} *.c -o bin/{}".format(program.custom_arguments, opt, program.name), shell=True, cwd=program.name)
171-
# sp.check_call("clang {} -lm {} *.c -o bin/{}".format(program.custom_arguments, opt, program.name), shell=True, cwd=program.name)
204+
clang = "clang"
172205

206+
command = "{clang} {args} -lm {opt} {sources} -o bin/{pname}" \
207+
.format(clang=clang, args=program.custom_arguments, opt=opt, sources=program.sources(), pname=program.name)
208+
print(command)
209+
sp.check_call(command, shell=True, cwd=program.name)
173210

174211
# Compile the C code in `program`'s directory into WASM
175212
def compile_to_wasm(program):
176213
flags = WASM_FLAGS.format(stack_size=program.stack_size)
177-
command = "shopt -s nullglob; {clang} {flags} {args} -O3 -flto ../dummy.c *.c *.cpp -o bin/{pname}.wasm" \
178-
.format(clang=WASM_CLANG, flags=flags, args=program.custom_arguments, pname=program.name)
214+
command = "{clang} {flags} {args} -O3 -flto ../dummy.c {sources} -o bin/{pname}.wasm" \
215+
.format(clang=WASM_CLANG, flags=flags, sources=program.sources(), args=program.custom_arguments, pname=program.name)
179216
print(command)
180217
sp.check_call(command, shell=True, cwd=program.name)
181218

@@ -189,10 +226,12 @@ def compile_wasm_to_bc(program):
189226

190227
command = "{silverfish} {target} bin/{pname}.wasm -o bin/{pname}.bc"\
191228
.format(silverfish=SILVERFISH_PATH, target=target_flag, pname=program.name)
229+
print(command)
192230
sp.check_call(command, shell=True, cwd=program.name)
193231
# Also compile an unsafe version, so we can see the performance difference
194232
command = "{silverfish} {target} -u bin/{pname}.wasm -o bin/{pname}_us.bc"\
195233
.format(silverfish=SILVERFISH_PATH, target=target_flag, pname=program.name)
234+
print(command)
196235
sp.check_call(command, shell=True, cwd=program.name)
197236

198237

tests/code_benches.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::error;
2+
use std::process;
3+
4+
#[cfg(debug_assertions)]
5+
const CLI: &str = "./target/debug/silverfish";
6+
#[cfg(not(debug_assertions))]
7+
const CLI: &str = "./target/release/silverfish";
8+
9+
#[test]
10+
fn cli_help_test() -> Result<(), Box<dyn error::Error>> {
11+
// sanity check that code compiles and runs
12+
let mut command = process::Command::new(CLI);
13+
command.args(&["--help"]);
14+
println!("{:?}", command);
15+
let status = command.output()?.status;
16+
assert!(status.success());
17+
Ok(())
18+
}
19+
20+
#[test]
21+
fn code_benches_test() -> Result<(), Box<dyn error::Error>> {
22+
// run oode_benches
23+
let mut command = process::Command::new("code_benches/run.py");
24+
if cfg!(debug_assertions) {
25+
command.args(&["--debug"]);
26+
} else {
27+
command.args(&["--release"]);
28+
}
29+
println!("{:?}", command);
30+
let status = command.status()?;
31+
assert!(status.success());
32+
Ok(())
33+
}
34+

wasmception

0 commit comments

Comments
 (0)