Skip to content

Commit 0d2224b

Browse files
committed
Added Code coverage support in renode script, makefile target for coverage, GitHub Action for coverage and explaination file
1 parent ae65ec9 commit 0d2224b

6 files changed

Lines changed: 103 additions & 4 deletions

File tree

.github/workflows/disabaled/test-and-coverage.yml renamed to .github/workflows/disabaled/test-and-coverage_with_Bot_Badges.yml

File renamed without changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Code Coverage
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
build-test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- name: Install dependencies
15+
run: sudo apt-get update && sudo apt-get install -y make gcc gcovr build-essential
16+
- name: Install Renode
17+
run: ./Lab3/install_tools.sh
18+
- name: Run renode based code coverage analysis
19+
run: make coverage -C Lab3

Lab3/Makefile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,22 @@ SRC = \
1919
src/syscalls.c \
2020
unity/unity.c
2121

22+
.PHONY: all clean run coverage
23+
2224
all:
2325
$(CC) $(CFLAGS) $(SRC) $(LDFLAGS) -o $(TARGET).elf
2426

2527
clean:
26-
rm -f *.elf
28+
rm -f *.elf coverage/trace.*
29+
30+
run: clean all
31+
touch coverage/trace.bin
32+
renode --disable-xwt renode/stm32.resc
2733

28-
run: all
29-
renode --disable-xwt renode/stm32.resc
34+
coverage: run
35+
@python3 /opt/renode/tools/execution_tracer/execution_tracer_reader.py \
36+
coverage ./coverage/trace.bin \
37+
--binary Lab3.elf \
38+
--sources ./src/checksum.c | \
39+
awk -F',' '/DA:/ {found++; if($$2>0) hit++} END \
40+
{printf "\nTotal Coverage: %.2f%% (%d/%d lines hit)\n", (hit/found)*100, hit, found}'

Lab3/coverage/check_coverage.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# How to measure code coverage in renode
2+
3+
* Add these lines after **LoadELF** command in **stm32.resc** file:
4+
```
5+
sysbus.cpu CreateExecutionTracing "tracer" @./coverage/trace.bin PC true
6+
sysbus.cpu EnableOpcodesCounting true
7+
```
8+
9+
* Also add the below line before quit but after execution of test cases to get details of execution
10+
```
11+
sysbus.cpu GetAllOpcodesCounters
12+
```
13+
14+
* Run renode execution tracer
15+
```
16+
python3 /opt/renode/tools/execution_tracer/execution_tracer_reader.py \
17+
coverage ./coverage/trace.bin \
18+
--binary Lab3.elf \
19+
--sources ./src/checksum.c
20+
```
21+
22+
* The above command will generate information of which lines ran and for how many times.
23+
* Example output:
24+
```
25+
TN:
26+
SF:./src/checksum.c
27+
DA:5,5
28+
DA:6,5
29+
DA:7,3
30+
DA:10,2
31+
DA:11,2
32+
DA:13,10
33+
DA:15,8
34+
DA:17,2
35+
DA:18,5
36+
end_of_record
37+
```
38+
* This is a Address cache which tells which lines ran, how manu times
39+
```
40+
DA:5,2 means line 5 ran 2 times.
41+
```
42+
43+
* To extract coverage, multiple methods can be tried.
44+
* Save the above output to a coverage.info
45+
1. Run lcov to get % of coverage
46+
```
47+
lcov --summary coverage/test.info
48+
```
49+
2. Use awk command to extract % of coverage.
50+
```
51+
cat coverage/test.info | awk -F',' '/DA:/ {found++; if($2>0) hit++} END {printf "Coverage: %.2f%% (%d/%d)\n", (hit/found)*100, hit, found}'
52+
```
53+
54+
* Both of the above commands give % of coverage of code.

Lab3/coverage/test.info

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
TN:
2+
SF:./src/checksum.c
3+
DA:5,1
4+
DA:6,1
5+
DA:7,0
6+
DA:10,1
7+
DA:11,1
8+
DA:13,5
9+
DA:15,4
10+
DA:17,1
11+
DA:18,1
12+
end_of_record

Lab3/renode/stm32.resc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ machine LoadPlatformDescription @platforms/boards/stm32f4_discovery-kit.repl
33

44
sysbus LoadELF @Lab3.elf
55

6+
sysbus.cpu CreateExecutionTracing "tracer" @./coverage/trace.bin PC true
7+
sysbus.cpu EnableOpcodesCounting true
8+
69
showAnalyzer sysbus.usart2
710

8-
sysbus.usart2 AddLineHook "TEST_RESULT:" "#print 'completed'";quit
11+
sysbus.usart2 AddLineHook "TEST_RESULT:" "#print 'completed'";sysbus.cpu GetAllOpcodesCounters;quit
912

1013
start

0 commit comments

Comments
 (0)