You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-firmware/core/prof/prof.py

92 lines
2.4 KiB

ENABLE_COVERAGE = True
ENABLE_MEMPROFILE = False
import sys
from uio import open
from uos import getenv
if ENABLE_MEMPROFILE:
from micropython import mem_total, mem_current, mem_peak
# We need to insert "" to sys.path so that the frozen build can import main from the
# frozen modules, and regular build can import it from current directory.
sys.path.insert(0, "")
PATH_PREFIX = (getenv("TREZOR_SRC") or ".") + "/"
class Coverage:
def __init__(self):
self.__files = {}
def line_tick(self, filename, lineno):
if not filename in self.__files:
self.__files[filename] = set()
self.__files[filename].add(lineno)
def lines_execution(self):
lines_execution = {"lines": {}}
lines = lines_execution["lines"]
this_file = globals()["__file__"]
for filename in self.__files:
if not filename == this_file:
lines[PATH_PREFIX + filename] = list(self.__files[filename])
return lines_execution
class _Prof:
trace_count = 0
display_flags = 0
if ENABLE_COVERAGE:
__coverage = Coverage()
if ENABLE_MEMPROFILE:
__memprofile = open(".memprofile", "wt")
__memprofile.write("trace;mem_total;mem_current;mem_peak\n")
def trace_tick(self, frame, event, arg):
self.trace_count += 1
if ENABLE_COVERAGE:
if event == "line":
self.__coverage.line_tick(frame.f_code.co_filename, frame.f_lineno)
if ENABLE_MEMPROFILE:
self.__memprofile.write(
"%d;%d;%d;%d\n"
% (self.trace_count, mem_total(), mem_current(), mem_peak())
)
def coverage_data(self):
return self.__coverage.lines_execution()
def trace_handler(frame, event, arg):
__prof__.trace_tick(frame, event, arg)
return trace_handler
def atexit():
print("\n------------------ script exited ------------------")
print("Total traces executed: ", __prof__.trace_count)
if ENABLE_COVERAGE:
with open(".coverage", "w") as f:
# wtf so private much beautiful wow
f.write("!coverage.py: This is a private format, don't read it directly!")
# poormans json
f.write(str(__prof__.coverage_data()).replace("'", '"'))
if ENABLE_MEMPROFILE:
__prof__.__memprofile.close()
sys.atexit(atexit)
global __prof__
if not "__prof__" in globals():
__prof__ = _Prof()
sys.settrace(trace_handler)
import main