Skip to content

Commit

Permalink
Centralise flashing of binaries (mupq#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkannwischer authored and joostrijneveld committed Oct 17, 2018
1 parent 766ce08 commit b3c8f8d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 122 deletions.
46 changes: 5 additions & 41 deletions benchmarks.py
@@ -1,14 +1,10 @@
#!/usr/bin/env python3
import serial
import sys
import os
import subprocess
import hashlib
import datetime
import time
from utils import m4ignore
import utils

dev = serial.Serial("/dev/ttyUSB0", 115200,timeout=10)

def benchmarkBinary(benchmark, binary):
binpath = os.path.join("bin", binary)
Expand All @@ -18,51 +14,19 @@ def benchmarkBinary(benchmark, binary):
scheme = '_'.join(info[2:-2])
implementation = info[-2]

if m4ignore(primitive, scheme, implementation):
if utils.m4ignore(primitive, scheme, implementation):
return

if len(sys.argv) > 1 and scheme not in sys.argv[1:]:
return

print("Flashing {}..".format(binpath))
subprocess.run(["st-flash", "write", binpath, "0x8000000"],
stdout=sys.stdout.buffer, stderr=sys.stdout.buffer)
print("Flashed, now running benchmarks..".format(binary))
state = 'waiting'
marker = b''
# This parses test vector output starting with a number of leading '=',
# and expects a hashtag '#' after the test vector output.
while True:
x = dev.read()
if x == b'' and state == 'waiting':
print("timed out while waiting for the markers")
benchmarkBinary(benchmark, binary)
return

if state == 'waiting':
if x == b'=':
marker += x
continue
# If we saw at least 5 equal signs, assume we've probably started
elif marker.count(b'=') > 5:
state = 'beginning'
vector = []
print(" .. found output marker..")
if state == 'beginning':
if x == b'=':
continue
else:
state = 'reading'
elif state == 'reading':
if x == b'#':
break
else:
vector.append(x)
result = utils.m4run(binpath)

timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d%H%M%S')
filename = os.path.join('benchmarks/', benchmark, primitive, scheme, implementation, timestamp)
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'w') as f:
f.write(b''.join(vector).decode('utf-8').strip())
f.write(result.strip())
print(" .. wrote benchmarks!")

def doBenchmarks(benchmark):
Expand Down
49 changes: 5 additions & 44 deletions test.py
Expand Up @@ -3,9 +3,8 @@
import sys
import os
import subprocess
from utils import m4ignore
import utils

dev = serial.Serial("/dev/ttyUSB0", 115200, timeout=10)

try:
binaries = [x for x in os.listdir('bin') if 'test.bin' in x]
Expand All @@ -20,60 +19,22 @@ def doTest(binary):
scheme = '_'.join(info[2:-2])
implementation = info[-2]

if m4ignore(primitive, scheme, implementation):
if utils.m4ignore(primitive, scheme, implementation):
return

if len(sys.argv) > 1 and scheme not in sys.argv[1:]:
return

print("Flashing {}..".format(binpath))
result = utils.m4run(binpath)

subprocess.run(["st-flash", "write", binpath, "0x8000000"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

print("Flashed, now running tests...")

state = 'waiting'
marker = b''

# This parses test vector output starting with a number of leading '=',
# and expects a hashtag '#' after the test vector output.
while True:
x = dev.read()
if x == b'' and state == 'waiting':
print("timed out while waiting for the markers")
doTest(binary)
return
if state == 'waiting':
if x == b'=':
marker += x
continue
# If we saw at least 5 equal signs, assume we've probably started
elif marker.count(b'=') > 5:
state = 'beginning'
vector = []
print(" .. found output marker..")
if state == 'beginning':
if x == b'=':
continue
else:
state = 'reading'
elif state == 'reading':
if x == b'#':
break
else:
vector.append(x)
print("Testing if tests were successful..")

contents = b''.join(vector).decode('utf-8').strip()
contents = result.strip()
# can we find a nicer way of checking if tests ran correctly?
if contents.count("ERROR") != 0 or contents.count("OK") != 30:
print("FAILED!")
else:
print("passed.")




for binary in binaries:
doTest(binary)
doTest(binary)
41 changes: 4 additions & 37 deletions testvectors.py
@@ -1,12 +1,10 @@
#!/usr/bin/env python3
import serial
import sys
import os
import subprocess
import hashlib
from utils import m4ignore
import utils

dev = serial.Serial("/dev/ttyUSB0", 115200)

try:
binaries = [x for x in os.listdir('bin') if 'testvectors.bin' in x]
Expand Down Expand Up @@ -36,46 +34,15 @@

# if this is a binary that needs to be ran on the board:
if binary[-4:] == '.bin':
if m4ignore(primitive, scheme, impl):
if utils.m4ignore(primitive, scheme, impl):
continue

binpath = os.path.join("bin", binary)
print("Flashing {}..".format(binpath))
subprocess.run(["st-flash", "write", binpath, "0x8000000"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

print("Flashed, now computing test vectors..".format(binary))
state = 'waiting'
marker = b''

# This parses test vector output starting with a number of leading '=',
# and expects a hashtag '#' after the test vector output.
while True:
x = dev.read()
if state == 'waiting':
if x == b'=':
marker += x
continue
# If we saw >5 equal signs, assume we've probably started
elif marker.count(b'=') > 5:
state = 'beginning'
vector = []
print(" .. found output marker..")
if state == 'beginning':
if x == b'=':
continue
else:
state = 'reading'
elif state == 'reading':
if x == b'#':
break
else:
vector.append(x)

results = utils.m4run(binpath)
filename = os.path.join('testvectors/', primitive, scheme, impl)
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'w') as f:
f.write(b''.join(vector).decode('utf-8').lstrip())
f.write(results.lstrip())
else:
binpath = os.path.join("bin-host", binary)
print("Running {}..".format(binpath))
Expand Down
45 changes: 45 additions & 0 deletions utils.py
@@ -1,6 +1,51 @@
import os
import serial
import subprocess
import sys


dev = serial.Serial("/dev/ttyUSB0", 115200,timeout=10)

def m4run(binpath):
print("Flashing {}..".format(binpath))
rc = subprocess.run(["st-flash", "--reset", "write", binpath, "0x8000000"],
stdout=sys.stdout.buffer, stderr=sys.stdout.buffer)

if rc.returncode != 0:
raise Exception("Flashing failed.")

state = 'waiting'
marker = b''
# This parses test vector output starting with a number of leading '=',
# and expects a hashtag '#' after the test vector output.
while True:
x = dev.read()
if x == b'' and state == 'waiting':
print("timed out while waiting for the markers")
run(binpath)
return

if state == 'waiting':
if x == b'=':
marker += x
continue
# If we saw at least 5 equal signs, assume we've probably started
elif marker.count(b'=') > 5:
state = 'beginning'
vector = []
print(" .. found output marker..")
if state == 'beginning':
if x == b'=':
continue
else:
state = 'reading'
elif state == 'reading':
if x == b'#':
break
else:
vector.append(x)
return b''.join(vector).decode('utf-8')

def m4ignore(primitive, scheme, implementation):
ignores = [os.path.join(primitive, scheme, implementation, '.m4ignore'),
os.path.join(primitive, scheme, '.m4ignore'),
Expand Down

0 comments on commit b3c8f8d

Please sign in to comment.