Column: Embedded design
Using Tracealyzer to evaluate Python algorithms in Linux
(ML) at the edge, Python is becoming more common in embedded application developments, since most ML frameworks are implemented in Python.
Mohammed Billoo, founder of MAB Labs, charts his experiences with the new Linux support by Percepio’s Tracealyzer v4.4 tool through real-world projects
I
n a previous article we discussed how to use Tracealyzer to evaluate user space applications on a Linux- based embedded system. Tere we emulated a potential feature, and wished to evaluate the performance
of multiple implementation candidates. We learned the necessary function invocations in our C/C++ application to trigger tracepoints, which would serve as markers in the Tracealyzer timeline. We also learned the necessary configuration in Tracealyzer to convert the tracepoints into “Custom Intervals”, ultimately used to provide the necessary timing information to evaluate the various implementations. In this article, we will see how to use
Tracealyzer quickly and efficiently to evaluate multiple implementations of an algorithm in Python. Due to the increased trend for performing machine learning
How (not) to compute Fibonacci numbers I will use a simple example to highlight an interesting caveat of application development in Python. It will also demonstrate how to use the combination of LTTng and Tracealyzer to efficiently compare the performance of two implementations of the same algorithm. Specifically, we’re going to implement the Fibonacci sequence using two common techniques: a recursive algorithm and a standard iterative algorithm. LTTng and Tracealyzer will be applied to compare their performance. First, however, we must ensure that the
Python LTTng soſtware package is installed (further information is available at https://
lttng.org/docs/v2.12/#doc-ubuntu). Both implementations of the Fibonacci sequence will use a single module:
def recur_fibo(n): if n <=1 n: return n else:
return(recur_fibo(n-1) + recur_ fibo(n-2))
def non_recur_fibo(n): result = [ ] a,b = 0,1 while a < n:
result.append(a) a,b = b, a+b return result
A separate Python source file will call the
two functions, with some key lines in bold (to be discussed later):
20 July/August 2021
www.electronicsworld.co.uk
import lttngust import logging import fib
def example():
logging.basicConfig() logger =
logging.getLogger(‘my-logger’)
logger.info(‘Start’)
fib.recur_fibo(10)
logger.info(‘Stop’)
logger.info(‘Start’)
fib.non_recur_fibo(10)
logger.info(‘Stop’)
if __name__ == ‘__main__’: example()
We then execute the following commands to capture a trace for consumption in Tracealyzer:
$> lttng create
$> lttng enable-event --kernel sched_ switch
$> lttng enable-event --python my- logger
$> lttng start $> python3 <example source file>.py $> lttng stop $> lttng destroy
Te command in bold is key. Here, we
replace the standard Python logger with one called “my-logger”, and store these events in the resulting LTTng trace. Te lines in bold in the Python snippet above establish this “my-logger” logger and emit the events surrounding our test functions. Te actual severity of the logs can be anything, since they will be ignored. We can see that the mechanism used to emit events to mark function boundaries is similar to that seen in the previous article. Once the trace has been generated, it can
be opened in Tracealyzer, with the events showing in the Trace View; see Figure 1. Since we are not capturing any data in
this particular example, we don’t need to configure event interpretations to read the data values. All we need is to create a Custom Interval to mark the entry and exit of both functions. Whilst Trace View shows
Page 1 |
Page 2 |
Page 3 |
Page 4 |
Page 5 |
Page 6 |
Page 7 |
Page 8 |
Page 9 |
Page 10 |
Page 11 |
Page 12 |
Page 13 |
Page 14 |
Page 15 |
Page 16 |
Page 17 |
Page 18 |
Page 19 |
Page 20 |
Page 21 |
Page 22 |
Page 23 |
Page 24 |
Page 25 |
Page 26 |
Page 27 |
Page 28 |
Page 29 |
Page 30 |
Page 31 |
Page 32 |
Page 33 |
Page 34 |
Page 35 |
Page 36 |
Page 37 |
Page 38 |
Page 39 |
Page 40 |
Page 41 |
Page 42 |
Page 43 |
Page 44 |
Page 45 |
Page 46 |
Page 47 |
Page 48 |
Page 49 |
Page 50 |
Page 51 |
Page 52 |
Page 53 |
Page 54 |
Page 55 |
Page 56 |
Page 57 |
Page 58