Skip to content

Commit af1e43d

Browse files
tests: Add Rust basic testcase
Add Rust-lang abc test for rust uftrace supports. Signed-off-by: Paran Lee <[email protected]> Co-authored-by: Michelle Jin <[email protected]> Reviewed-by: Gichoel Choi <[email protected]>
1 parent 4213eb7 commit af1e43d

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

tests/r001_basic.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
3+
from runtest import RustTestBase
4+
5+
class TestCase(RustTestBase):
6+
def __init__(self):
7+
RustTestBase.__init__(self, 'abc', """
8+
# DURATION TID FUNCTION
9+
1.852 us [1008471] | getauxval();
10+
0.204 us [1008471] | getauxval();
11+
0.203 us [1008471] | getauxval();
12+
[1008471] | std::rt::lang_start() {
13+
14.260 us [1008471] | poll();
14+
5.056 us [1008471] | signal();
15+
2.759 us [1008471] | sigaction();
16+
2.426 us [1008471] | sigaction();
17+
2.537 us [1008471] | sigaction();
18+
2.722 us [1008471] | sigaltstack();
19+
0.408 us [1008471] | sysconf();
20+
13.907 us [1008471] | mmap64();
21+
0.260 us [1008471] | sysconf();
22+
26.444 us [1008471] | mprotect();
23+
0.296 us [1008471] | sysconf();
24+
2.019 us [1008471] | sigaltstack();
25+
0.185 us [1008471] | sysconf();
26+
0.278 us [1008471] | pthread_self();
27+
618.405 us [1008471] | pthread_getattr_np();
28+
0.389 us [1008471] | pthread_attr_getstack();
29+
0.371 us [1008471] | pthread_attr_destroy();
30+
0.166 us [1008471] | malloc();
31+
0.389 us [1008471] | malloc();
32+
4.241 us [1008471] | __cxa_thread_atexit_impl();
33+
[1008471] | std::rt::lang_start::_{{closure}}() {
34+
[1008471] | std::sys_common::backtrace::__rust_begin_short_backtrace() {
35+
[1008471] | core::ops::function::FnOnce::call_once() {
36+
[1008471] | s_abc::main() {
37+
[1008471] | s_abc::a() {
38+
[1008471] | s_abc::b() {
39+
[1008471] | s_abc::c() {
40+
2.389 us [1008471] | getpid();
41+
4.630 us [1008471] | } /* s_abc::c */
42+
5.148 us [1008471] | } /* s_abc::b */
43+
5.500 us [1008471] | } /* s_abc::a */
44+
5.889 us [1008471] | } /* s_abc::main */
45+
6.426 us [1008471] | } /* core::ops::function::FnOnce::call_once */
46+
6.908 us [1008471] | } /* std::sys_common::backtrace::__rust_begin_short_backtrace */
47+
0.111 us [1008471] | _<()>::report();
48+
8.037 us [1008471] | } /* std::rt::lang_start::_{{closure}} */
49+
2.408 us [1008471] | sigaltstack();
50+
0.259 us [1008471] | sysconf();
51+
0.167 us [1008471] | sysconf();
52+
41.648 us [1008471] | munmap();
53+
780.960 us [1008471] | } /* std::rt::lang_start */
54+
0.259 us [1008471] | free();
55+
0.166 us [1008471] | free();
56+
""")

tests/runtest.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class TestBase:
6969
supported_lang = {
7070
'C': { 'cc': 'gcc', 'flags': 'CFLAGS', 'ext': '.c' },
7171
'C++': { 'cc': 'g++', 'flags': 'CXXFLAGS', 'ext': '.cpp' },
72+
# https://github.com/emosenkis/esp-rs/issues/10
73+
'Rust': { 'cc': 'rustc', 'flags': "+nightly -Z instrument-mcount -C passes=ee-instrument<post-inline> ", 'ext': '.rs' },
7274
}
7375

7476
TEST_SUCCESS = 0
@@ -714,6 +716,22 @@ def __init__(self, name, result, lang='Python', cflags='', ldflags='', sort='sim
714716
if orig_path != "":
715717
os.environ["PYTHONPATH"] += ':' + orig_path
716718

719+
class RustTestBase(TestBase):
720+
def __init__(self, name, result, lang='Rust', cflags='', ldflags='', sort='simple', serial=False):
721+
TestBase.__init__(self, name, result, lang, cflags, ldflags, sort, serial)
722+
723+
def build(self, name, rflags='', ldflags=''):
724+
725+
lang = TestBase.supported_lang[self.lang]
726+
prog = 't-' + name
727+
src = 's-' + name + ".rs"
728+
rflags = self.supported_lang['Rust']['flags']
729+
730+
build_cmd = '%s %s -o %s %s' % (lang['cc'], rflags, prog, src)
731+
732+
self.pr_debug("build command: %s" % build_cmd)
733+
return self.build_it(build_cmd)
734+
717735
RED = '\033[1;31m'
718736
GREEN = '\033[1;32m'
719737
YELLOW = '\033[1;33m'
@@ -780,6 +798,18 @@ def run_python_case(T, case, timeout):
780798
ret = tc.postrun(ret)
781799
return (ret, dif)
782800

801+
def run_rust_case(T, case, timeout):
802+
tc = T.TestCase()
803+
tc.set_debug(arg.debug)
804+
tc.set_keep(arg.keep)
805+
ret = tc.build(tc.name, "")
806+
ret = tc.prerun(timeout)
807+
dif = ''
808+
if ret == TestBase.TEST_SUCCESS:
809+
ret, dif = tc.run(case, "", arg.diff, timeout)
810+
ret = tc.postrun(ret)
811+
return (ret, dif)
812+
783813
def run_single_case(case, flags, opts, arg, compilers):
784814
result = []
785815
timeout = int(arg.timeout)
@@ -795,6 +825,11 @@ def run_single_case(case, flags, opts, arg, compilers):
795825
result.append((ret, dif))
796826
continue
797827

828+
if compiler == 'rustc':
829+
ret, dif = run_rust_case(T, case, timeout)
830+
result.append((ret, dif))
831+
continue
832+
798833
for flag in flags:
799834
for opt in opts:
800835
tc = T.TestCase()
@@ -910,6 +945,30 @@ def print_python_test_header(ftests):
910945
ftests.write(header2 + '\n')
911946
ftests.flush()
912947

948+
def print_rust_test_header(flags, ftests, compilers):
949+
header1 = '%-24s ' % 'Compiler'
950+
header2 = '%-24s ' % 'Runtime test case'
951+
header3 = '-' * 24 + ':'
952+
empty = ' ' * 100
953+
954+
for i, compiler in enumerate(compilers):
955+
if i != 0:
956+
header1 += ' '
957+
header2 += ' '
958+
header3 += ' '
959+
for flag in flags:
960+
# align with optimization flags
961+
header2 += ' ' + flag
962+
header1 += ' ' + compiler
963+
964+
print("")
965+
print(header1)
966+
print(header2)
967+
print(header3)
968+
ftests.write(header1 + '\n')
969+
ftests.write(header2 + '\n')
970+
ftests.write(header3 + '\n')
971+
ftests.flush()
913972

914973
def print_test_report(color, shared):
915974
success = shared.stats[TestBase.TEST_SUCCESS] + shared.stats[TestBase.TEST_SUCCESS_FIXED]
@@ -962,6 +1021,8 @@ def parse_argument():
9621021
help="Hide normal results and print only abnormal results.")
9631022
parser.add_argument("-P", "--python", dest='python', action='store_true',
9641023
help="Run python test cases instead")
1024+
parser.add_argument("-R", "--rust", dest='rust', action='store_true',
1025+
help="Run rust test cases instead")
9651026

9661027
return parser.parse_args()
9671028

@@ -976,6 +1037,8 @@ def parse_argument():
9761037
if arg.cases == 'all':
9771038
if arg.python:
9781039
testcases = glob.glob('p???_*.py')
1040+
elif arg.rust:
1041+
testcases = glob.glob('r???_*.py')
9791042
else:
9801043
testcases = glob.glob('t???_*.py')
9811044
else:
@@ -984,6 +1047,8 @@ def parse_argument():
9841047
for case in cases:
9851048
if arg.python:
9861049
testcases.extend(glob.glob('p*' + case + '*.py'))
1050+
elif arg.rust:
1051+
testcases.extend(glob.glob('r*' + case + '*.py'))
9871052
else:
9881053
testcases.extend(glob.glob('t*' + case + '*.py'))
9891054
arg.worker = min(arg.worker, len(testcases))
@@ -1029,6 +1094,9 @@ def has_compiler(compiler):
10291094
compilers = []
10301095
if arg.python:
10311096
compilers.append('python')
1097+
elif arg.rust:
1098+
if has_compiler('rustc') and os.system('rustup default nightly > /dev/null') == 0:
1099+
compilers.append('rustc')
10321100
elif arg.compiler == 'all':
10331101
for compiler in ['gcc', 'clang']:
10341102
if has_compiler(compiler):
@@ -1083,6 +1151,8 @@ class dotdict(dict):
10831151

10841152
if arg.python:
10851153
print_python_test_header(ftests)
1154+
elif arg.rust:
1155+
print_rust_test_header(flags, ftests, ['rustc'])
10861156
else:
10871157
print_test_header(opts, flags, ftests, compilers)
10881158

tests/s-abc.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::process;
2+
3+
fn a() -> u32 {
4+
return b();
5+
}
6+
7+
fn b() -> u32 {
8+
return c();
9+
}
10+
11+
fn c() -> u32 {
12+
return process::id();
13+
}
14+
15+
fn main() {
16+
a();
17+
}

0 commit comments

Comments
 (0)