Skip to content

Commit 61298e3

Browse files
JordanYatescfriedt
authored andcommitted
scripts: twisterlib: more efficient coverage merging
Use `collections.defaultdict` to simplify code, and use the `bytes` type internally rather than hex strings. Signed-off-by: Jordan Yates <[email protected]>
1 parent aa08e0d commit 61298e3

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

scripts/pylib/twister/twisterlib/coverage.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Copyright (c) 2018-2025 Intel Corporation
44
# SPDX-License-Identifier: Apache-2.0
55

6+
import collections
67
import contextlib
78
import filecmp
89
import glob
@@ -52,7 +53,7 @@ def factory(tool, jobs=None):
5253
@staticmethod
5354
def retrieve_gcov_data(input_file):
5455
logger.debug(f"Working on {input_file}")
55-
extracted_coverage_info = {}
56+
extracted_coverage_info = collections.defaultdict(list)
5657
capture_data = False
5758
capture_complete = False
5859
with open(input_file) as fp:
@@ -78,10 +79,8 @@ def retrieve_gcov_data(input_file):
7879
continue
7980
else:
8081
continue
81-
if file_name in extracted_coverage_info:
82-
extracted_coverage_info[file_name].append(hex_dump)
83-
else:
84-
extracted_coverage_info[file_name] = [hex_dump]
82+
hex_bytes = bytes.fromhex(hex_dump)
83+
extracted_coverage_info[file_name].append(hex_bytes)
8584
if not capture_data:
8685
capture_complete = True
8786
return {'complete': capture_complete, 'data': extracted_coverage_info}
@@ -99,7 +98,7 @@ def merge_hexdumps(self, hexdumps):
9998
os.mkdir(subdir)
10099
dirs.append(subdir)
101100
with open(f'{subdir}/tmp.gcda', 'wb') as fp:
102-
fp.write(bytes.fromhex(dump))
101+
fp.write(dump)
103102

104103
# Iteratively call gcov-tool (not gcov) to merge the files
105104
merge_tool = self.gcov_tool + '-tool'
@@ -109,7 +108,7 @@ def merge_hexdumps(self, hexdumps):
109108

110109
# Read back the final output file
111110
with open(f'{dirs[-1]}/tmp.gcda', 'rb') as fp:
112-
return fp.read(-1).hex()
111+
return fp.read(-1)
113112

114113
def create_gcda_files(self, extracted_coverage_info):
115114
gcda_created = True
@@ -125,9 +124,8 @@ def create_gcda_files(self, extracted_coverage_info):
125124

126125
try:
127126
hexdump_val = self.merge_hexdumps(hexdumps)
128-
hex_bytes = bytes.fromhex(hexdump_val)
129127
with open(filename, 'wb') as fp:
130-
fp.write(hex_bytes)
128+
fp.write(hexdump_val)
131129
except ValueError:
132130
logger.exception(f"Unable to convert hex data for file: {filename}")
133131
gcda_created = False

0 commit comments

Comments
 (0)