Skip to contentSkip to conversation

code coverage: some improvements

PATCH Superseded Mauro Carvalho Chehab 3 participants 23 messages Apr 12, 2022 – Apr 14, 2022 Arm/Amlogic Meson SoC support

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover code coverage: some improvements

From: Mauro Carvalho Chehab <[email protected]>

This series:

1. remove the code coverage script extensions and install them on bindir;
2. adds a small script to test if the code coverage and its scripts are working properly, if excecuted with a kernel compiled with code coverage support;
3. adds a script that allows filtering, printing statistics, printing used/unused functions and generating html reports.

The parsing script enhances the code coverage data collect, as it allows quickly checking the code coverage results per test.

For instance, using the script below:

<script>
TESTLIST="my_tests.testlist"
OUT_DIR="${HOME}/results"

mkdir -p $OUT_DIR/html

echo "igt@debugfs_test@read_all_entries" > $TESTLIST echo "igt@core_auth@basic-auth" >> $TESTLIST echo "igt@gem_exec_basic@basic" >> $TESTLIST

sudo IGT_KERNEL_TREE="${HOME}/linux" igt_runner -s -o --coverage-per-test \
                  --collect-script code_cov_capture --test-list $TESTLIST \
                  /usr/local/libexec/igt-gpu-tools $OUT_DIR/ | sed s,$HOME/,,

sudo chown -R $(id -u):$(id -g) $OUT_DIR/

for i in $OUT_DIR/code_cov/*.info; do

	echo -e "\n$(basename $i):"
        code_cov_parse_info --only-drm --ignore-unused --stat $i
done
echo -e "\nTOTAL:"
code_cov_parse_info --only-drm --stat --output $OUT_DIR/results.info \
        $OUT_DIR/code_cov/*.info

cd $OUT_DIR/html
genhtml -q -s --legend --branch-coverage $OUT_DIR/results.info </script>

The parse script will filter and display the function, branch and like coverage per test, and the total one:

core_auth_basic_auth.info:
lines......: 11.7% (8219 of 70257 lines)
functions..: 7.1% (776 of 10971 functions)
branches...: 7.0% (3597 of 51041 branches)
Ignored......: non-drm headers and source files where none of its code ran. Source files.: 23.27% (165 of 709 total), 29.57% (165 of 558 filtered)

debugfs_test_read_all_entries.info:
lines......: 19.3% (20249 of 104802 lines)
functions..: 17.5% (1922 of 10971 functions)
branches...: 12.7% (9449 of 74555 branches)
Ignored......: non-drm headers and source files where none of its code ran. Source files.: 34.70% (246 of 709 total), 44.09% (246 of 558 filtered)

gem_exec_basic_basic.info:
lines......: 17.4% (15196 of 87570 lines)
functions..: 13.1% (1437 of 10971 functions)
branches...: 10.3% (6562 of 63806 branches)
Ignored......: non-drm headers and source files where none of its code ran. Source files.: 31.17% (221 of 709 total), 39.61% (221 of 558 filtered)

TOTAL:
lines......: 15.6% (26031 of 166849 lines)
functions..: 22.3% (2443 of 10971 functions)
branches...: 10.6% (11968 of 112665 branches)
Ignored......: non-drm headers.
Source files.: 78.70% (558 of 709 total)

A much more detailed report can also be produced with --print, --print-unused (optionally with --show-lines). For instance, reporting code coverage usage for functions with "edid_" on its name is as simple as:

$ echo "+edid_" >filter.txt; code_cov_parse_info --func-filters filter.txt results/results.info --print --print-unused
TEST: Code_coverage_tests
__drm_get_edid_firmware_path(): unused
__drm_set_edid_firmware_path(): unused
displayid_iter_edid_begin(): executed 10 times
drm_add_edid_modes(): executed 2 times
drm_add_override_edid_modes(): unused
drm_connector_attach_edid_property(): unused
drm_connector_update_edid_property(): executed 8 times
drm_dp_send_real_edid_checksum(): unused
drm_edid_are_equal(): executed 4 times
drm_edid_block_valid(): executed 8 times
drm_edid_duplicate(): unused
drm_edid_get_monitor_name(): unused
drm_edid_header_is_valid(): executed 4 times
drm_edid_is_valid(): executed 2 times
drm_edid_to_eld(): executed 2 times
drm_edid_to_sad(): unused
drm_edid_to_speaker_allocation(): unused
drm_find_edid_extension(): executed 22 times
drm_get_edid_switcheroo(): unused
drm_load_edid_firmware(): executed 2 times
edid_firmware_get(): unused
edid_firmware_set(): unused
edid_fixup_preferred(): unused
edid_get_quirks(): executed 6 times
edid_load(): unused
edid_open(): executed 4 times
edid_show() from linux/drivers/gpu/drm/drm_debugfs.c: executed 4 times
edid_show() from linux/drivers/gpu/drm/drm_sysfs.c: unused
edid_vendor(): executed 348 times
edid_write(): unused
intel_panel_edid_downclock_mode(): unused
intel_panel_edid_fixed_mode(): unused
is_edid_digital_input_dp(): unused
Ignored......: unmatched functions m/(?^:edid_)/ and source files where none of its code ran.
Source files.: 0.90% (5 of 558 total), 55.56% (5 of 9 filtered)

Finally, it can place the data above on an html report and, when multiple .info files are used, it places the data collected from each .info file on a row, allowing to compare the code coverage from different tests and from different machines.

Regards,
Mauro

Mauro Carvalho Chehab (12):
scripts/code_cov*: remove the extensions from them scripts/code_cov_parse_info: add a tool to parse code coverage info
files
scripts/code_cov_gen_report: add support for filtering info files
runner: execute code coverage script also from PATH scripts/meson.build: install code coverage scripts scripts/code_cov_selftest.sh: test if IGT code coverage is working docs/code_coverage.md: document the code coverage filter script scripts/code_cov_parse_info: better handle test name
code_cov_parse_info: fix error handling when opening files
code_cov_parse_info: fix --show-lines logic
code_cov_parse_info: add support for exclude filters
code_cov_parse_info: add support for generating html reports

 docs/code_coverage.md                         |  238 +++-
 meson.build                                   |    1 +
 runner/settings.c                             |  109 +-
 .../{code_cov_capture.sh => code_cov_capture} |    0
 ...r_on_build.sh => code_cov_gather_on_build} |    0
 ...her_on_test.py => code_cov_gather_on_test} |    0
 ..._cov_gen_report.sh => code_cov_gen_report} |   19 +
 scripts/code_cov_parse_info                   | 1179 +++++++++++++++++
 scripts/code_cov_selftest.sh                  |   47 +
 scripts/meson.build                           |   13 +
 10 files changed, 1551 insertions(+), 55 deletions(-)
 rename scripts/{code_cov_capture.sh => code_cov_capture} (100%)
 rename scripts/{code_cov_gather_on_build.sh => code_cov_gather_on_build} (100%)
 rename scripts/{code_cov_gather_on_test.py => code_cov_gather_on_test} (100%)
 rename scripts/{code_cov_gen_report.sh => code_cov_gen_report} (85%)
 create mode 100755 scripts/code_cov_parse_info
 create mode 100755 scripts/code_cov_selftest.sh
 create mode 100644 scripts/meson.build
Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 1/12 scripts/code_cov*: remove the extensions from them

From: Mauro Carvalho Chehab <[email protected]>

As those scripts will be installed and executed from the PATH, remove the extensions from them, in order to make it more elegant when installed on distros.

---

To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

 scripts/{code_cov_capture.sh => code_cov_capture}                 | 0
 scripts/{code_cov_gather_on_build.sh => code_cov_gather_on_build} | 0
 scripts/{code_cov_gather_on_test.py => code_cov_gather_on_test}   | 0
 scripts/{code_cov_gen_report.sh => code_cov_gen_report}           | 0
 4 files changed, 0 insertions(+), 0 deletions(-)
 rename scripts/{code_cov_capture.sh => code_cov_capture} (100%)
 rename scripts/{code_cov_gather_on_build.sh => code_cov_gather_on_build} (100%)
 rename scripts/{code_cov_gather_on_test.py => code_cov_gather_on_test} (100%)
 rename scripts/{code_cov_gen_report.sh => code_cov_gen_report} (100%)
scripts/code_cov_capture renamed from scripts/code_cov_capture.sh +0 -0
similarity index 100%
rename from scripts/code_cov_capture.sh
rename to scripts/code_cov_capture
scripts/code_cov_gather_on_build renamed from scripts/code_cov_gather_on_build.sh +0 -0
similarity index 100%
rename from scripts/code_cov_gather_on_build.sh
rename to scripts/code_cov_gather_on_build
scripts/code_cov_gather_on_test renamed from scripts/code_cov_gather_on_test.py +0 -0
similarity index 100%
rename from scripts/code_cov_gather_on_test.py
rename to scripts/code_cov_gather_on_test
scripts/code_cov_gen_report renamed from scripts/code_cov_gen_report.sh +0 -0
similarity index 100%
rename from scripts/code_cov_gen_report.sh
rename to scripts/code_cov_gen_report
Signature · 1 line
2.35.1
#2 lore raw

PATCH v2 2/12 scripts/code_cov_parse_info: add a tool to parse code coverage info files

In reply to Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

The code coverage info files provide useful information about what functions were called and how many times. It can also contain data that it is not useful for the code coverage, like calls to non-DRM related Kernel APIs.

Add a tool that helps filtering out non-DRM files and prints what functions were called.

Both the stats, prints and output files are affected by the filters:

$ code_cov_parse_info --stat total.info
  lines......: 40.8% (71989 of 176442 lines)
  functions..: 50.2% (5975 of 11909 functions)
  branches...: 28.7% (35006 of 121893 branches)
Source files: 744
$ code_cov_parse_info total.info --stat --only-drm
  lines......: 40.3% (70244 of 174170 lines)
  functions..: 50.8% (5835 of 11491 functions)
  branches...: 28.4% (33096 of 116707 branches)
Ignored......: non-drm headers.
Source files.: 78.36% (583 of 744 total)
$ code_cov_parse_info total.info --stat --only-drm --ignore-unused -o o.info
  lines......: 47.9% (70244 of 146752 lines)
  functions..: 50.8% (5835 of 11491 functions)
  branches...: 32.8% (33096 of 100900 branches)
Ignored......: non-drm headers and source files where none of its code ran.
Source files.: 58.87% (438 of 744 total), 75.13% (438 of 583 filtered)
$ code_cov_parse_info --stat o.info
  lines......: 47.9% (70244 of 146752 lines)
  functions..: 59.8% (5835 of 9763 functions)
  branches...: 32.8% (33096 of 100900 branches)
Source files: 438

---

To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

 scripts/code_cov_parse_info | 775 ++++++++++++++++++++++++++++++++++++
 1 file changed, 775 insertions(+)
 create mode 100755 scripts/code_cov_parse_info
scripts/code_cov_parse_info new file +775 -0
new file mode 100755
index 000000000000..604812b41580
@@ -0,0 +1,775 @@
+#!/usr/bin/perl
+# SPDX-License-Identifier: MIT
+use strict;
+use warnings;
+use Getopt::Long;
+BEGIN { $Pod::Usage::Formatter = 'Pod::Text::Termcap'; }
+use Pod::Usage;
+use Pod::Man;
+
+my $prefix = qr ".*?(linux)\w*/";
+
+my %used_func;
+my %all_func;
+my %all_branch;
+my %all_line;
+my %used_source;
+my %record;
+my %files;
+my @func_regexes;
+my @src_regexes;
+my $testname = "";
+
+my $verbose = 0;
+my $ignore_unused = 0;
+my $only_i915 = 0;
+my $only_drm = 0;
+my $skip_func = 0;
+
+sub is_function_excluded($)
+{
+	return 0 if (!@func_regexes);
+
+	my $func = shift;
+
+	foreach my $r (@func_regexes) {
+		return 0 if ($func =~ m/$r/);
+	}
+
+	return 1;
+}
+
+sub filter_file($)
+{
+	my $s = shift;
+
+	if ($only_drm) {
+		# Please keep --only-drm doc updated with any changes her
+		if ($s =~ m/\.h$/) {
+			if ($s =~ m/trace/ || !($s =~ m/drm/)) {
+				return 1;
+			}
+		}
+	}
+
+	if ($only_i915) {
+		# Please keep --only-i915 doc updated with any changes here
+		if ($s =~ m/selftest/) {
+			return 1;
+		}
+
+		# Please keep --only-i915 doc updated with any changes here
+		if (!($s =~ m#drm/i915/# || $s =~ m#drm/ttm# || $s =~ m#drm/vgem#)) {
+			return 1;
+		}
+	}
+
+	return 0 if (!@src_regexes);
+
+	my $func = shift;
+
+	foreach my $r (@src_regexes) {
+		return 0 if ($s =~ m/$r/);
+	}
+
+	return 1;
+}
+
+# Use something that comes before any real function
+my $before_sf = "!!!!";
+
+sub parse_info_data($)
+{
+	my $file = shift;
+	my $was_used = 0;
+	my $has_func = 0;
+	my $ignore = 0;
+	my $source = $before_sf;
+	my $func = $before_sf;
+	my $cur_test = "";
+
+	# First step: parse data
+
+	print "reading $file...\n" if ($verbose);
+	open IN, $file or die "can't open $file";
+	# For details on .info file format, see "man geninfo"
+	# http://ltp.sourceforge.net/coverage/lcov/geninfo.1.php
+	while (<IN>) {
+		# TN:<test name>
+		if (m/^TN:(.*)/) {
+			if ($1 ne $cur_test) {
+				$cur_test = $1;
+				if (!$testname) {
+					$testname = $cur_test;
+				} else {
+					$testname = "Code_coverage_tests";
+				}
+			}
+			$source = $before_sf;
+			$func = $before_sf;
+			next;
+		}
+
+		# SF:<absolute path to the source file>
+		if (m/^[SK]F:(.*)/) {
+			$source = $1;
+
+			$was_used = 0;
+			$has_func = 0;
+			$func = $before_sf;
+			$files{$source} = 1;
+
+			# Just ignore files explictly set as such
+			$ignore = filter_file($source);
+			next;
+		}
+
+		# End of record
+		if (m/^end_of_record/) {
+			if (!$source) {
+				print "bad end_of_record field at $file, line $. Ignoring...\n";
+				next;
+			}
+
+			my $s = $source;
+
+			$source = $before_sf;
+			$func = $before_sf;
+
+			next if ($ignore);
+			next if ($ignore_unused && !$was_used);
+
+			# Mark that the source was not ignored
+			$used_source{$s} = 1;
+			next;
+		}
+
+		next if ($ignore);
+
+		# Function coverage
+
+		# FN:<line number of function start>,<function name>
+		if (m/^FN:(-?\d+),(.*)/) {
+			my $ln = $1;
+
+			$func = $2;
+			$has_func = 1;
+
+			if (is_function_excluded($func)) {
+				$skip_func = 1;
+				next;
+			}
+
+			$skip_func = 0;
+
+			$record{$source}{$func}{fn} = $ln;
+			$all_func{$func}{$source}->{ln} = $ln;
+			next;
+		}
+
+		# Parse functions that were actually used
+		# FNDA:<execution count>,<function name>
+		if (m/^FNDA:(-?\d+),(.*)/) {
+			my $count = $1;
+
+			# Negative gcov results are possible, as reported at:
+			# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67937
+			# Lcov ignores those. So, let's do the same here.
+			next if ($count <= 0);
+
+			$func = $2;
+			$has_func = 1;
+
+			if (is_function_excluded($func)) {
+				$skip_func = 1;
+				next;
+			}
+
+			$skip_func = 0;
+			$was_used = 1;
+
+			$record{$source}{$func}{fnda} += $count;
+			$used_func{$func}{$source}->{count} += $count;
+			next;
+		}
+
+		# Ignore data from skipped functions
+		next if ($skip_func);
+
+		# Ignore DA/BRDA that aren't associated with functions
+		# Those are present on header files (maybe defines?)
+		next if (@func_regexes && !$has_func);
+
+		# FNF:<number of functions found>
+		if (m/^FNF:(-?\d+)/) {
+			$record{$source}{$func}{fnf} = $1;
+			next;
+		}
+		# FNH:<number of function hit>
+		if (m/^FNH:(-?\d+)/) {
+			my $hits = $1;
+			if ($record{$source}{$func}{fnh} < $hits) {
+				$record{$source}{$func}{fnh} = $hits;
+			}
+			next;
+		}
+
+		# Branch coverage
+
+		# BRDA:<line number>,<block number>,<branch number>,<taken>
+		if (m/^BRDA:(-?\d+),(-?\d+),(-?\d+),(.*)/) {
+			my $ln = $1;
+			my $block = $2;
+			my $branch = $3;
+			my $taken = $4;
+
+			my $where = "$ln,$block,$branch";
+
+			$taken = 0 if ($taken eq '-');
+
+			# Negative gcov results are possible, as reported at:
+			# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67937
+			# Lcov ignores those. So, let's do the same here.
+			$taken = 0 if ($taken < 0);
+
+			$was_used = 1 if ($taken > 0);
+
+			$record{$source}{$func}{brda}{$where} += $taken;
+			$all_branch{$source}{"$where"} += $taken;
+			next;
+		}
+
+		# BRF:<number of branches found>
+		if (m/^BRF:(-?\d+)/) {
+			$record{$source}{brf} = $1;
+			next;
+		}
+		# BRH:<number of branches hit>
+		if (m/^BRH:(-?\d+)/) {
+			my $hits = $1;
+			if ($record{$source}{$func}{brh} < $hits) {
+				$record{$source}{$func}{brh} = $hits;
+			}
+			next;
+		}
+
+		# Line coverage
+
+		# DA:<line number>,<execution count>[,<checksum>]
+		if (m/^DA:(-?\d+),(-?\d+)(,.*)?/) {
+			my $ln = $1;
+			my $count = $2;
+
+			# Negative gcov results are possible, as reported at:
+			# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67937
+			# Lcov ignores those. So, let's do the same here.
+			$count = 0 if ($count < 0);
+
+			$was_used = 1 if ($count > 0);
+
+			$record{$source}{$func}{da}{$ln} += $count;
+			$all_line{$source}{"$ln"} += $count;
+			next;
+		}
+
+		# LF:<number of instrumented lines>
+		if (m/^LF:(-?\d+)/) {
+			$record{$source}{$func}{lf} = $1;
+			next;
+		}
+
+		# LH:<number of lines with a non-zero execution count>
+		if (m/^LH:(-?\d+)/) {
+			my $hits = $1;
+			if ($record{$source}{$func}{lh} < $hits) {
+				$record{$source}{$func}{lh} = $hits;
+			}
+			next;
+		}
+
+		printf("Warning: invalid line: $_");
+	}
+
+	close IN or die;
+}
+
+sub write_filtered_file($)
+{
+	my $filter = shift;
+
+	# Generates filtered data
+	my $filtered = "TN:$testname\n";
+
+	foreach my $source(sort keys %record) {
+		next if (!$used_source{$source});
+
+		if ($source ne $before_sf) {
+			$filtered .= "SF:$source\n";
+		}
+
+		foreach my $func(sort keys %{ $record{$source} }) {
+			if ($func ne $before_sf) {
+				my $fn;
+				my $fnda;
+
+				if (defined($record{$source}{$func}{fn})) {
+					$filtered .= "FN:" . $record{$source}{$func}{fn} . ",$func\n";
+				}
+				if (defined($record{$source}{$func}{fnda})) {
+					$filtered .= "FNDA:" . $record{$source}{$func}{fnda} . ",$func\n";
+				}
+				if ($record{$source}{fnf}) {
+					$filtered .= "FNF:". $record{$source}{$func}{fnf} ."\n";
+				}
+				if ($record{$source}{fnh}) {
+					$filtered .= "FNH:". $record{$source}{$func}{fnh} ."\n";
+				}
+			}
+
+			foreach my $ln(sort keys %{ $record{$source}{$func}{da} }) {
+				$filtered .= "DA:$ln," . $record{$source}{$func}{da}{$ln} . "\n";
+			}
+			foreach my $where(sort keys %{ $record{$source}{$func}{brda} }) {
+				my $taken = $record{$source}{$func}{brda}{$where};
+				$taken = "-" if (!$taken);
+				$filtered .= "BRDA:$where,$taken\n";
+			}
+			if ($record{$source}{$func}{brf}) {
+				$filtered .= "BRF:". $record{$source}{$func}{brf} ."\n";
+			}
+			if ($record{$source}{$func}{brh}) {
+				$filtered .= "BRH:". $record{$source}{$func}{brh} ."\n";
+			}
+			if ($record{$source}{$func}{lf}) {
+				$filtered .= "LF:". $record{$source}{$func}{lf} ."\n";
+			}
+			if ($record{$source}{$func}{lh}) {
+				$filtered .= "LH:". $record{$source}{$func}{lh} ."\n";
+			}
+		}
+
+		$filtered .= "end_of_record\n";
+	}
+	open OUT, ">$filter" or die "Can't open $filter";
+	print OUT $filtered or die "Failed to write to $filter";
+	close OUT or die "Failed to close to $filter";
+}
+
+sub print_code_coverage($$$)
+{
+	my $print_used = shift;
+	my $print_unused = shift;
+	my $show_lines = shift;
+
+	return if (!$print_used && !$print_unused);
+
+	if ($testname ne "") {
+		$testname =~ s/(.*)_on_(\w+)$/$1 on $2/;
+		print "TEST: $testname\n";
+	}
+	my $prev_file = "";
+
+	foreach my $func (sort keys(%all_func)) {
+		my @keys = sort keys(%{$all_func{$func}});
+		foreach my $file (@keys) {
+			my $count = 0;
+			my $name;
+
+			if ($used_func{$func}) {
+				if ($used_func{$func}->{$file}) {
+					$count = $used_func{$func}->{$file}->{count};
+				}
+			}
+
+			if ($show_lines) {
+				$file =~ s,$prefix,linux/,;
+				$name = "$func() from $file:" . $all_func{$func}{$file}->{ln};
+			} elsif (scalar @keys > 1) {
+				$file =~ s,$prefix,linux/,;
+				$name = "$func() from $file:";
+			} else {
+				$name = "$func():";
+			}
+			if ($print_unused) {
+				if (!$count) {
+					print "$name unused\n";
+				} elsif ($print_used) {
+					print "$name executed $count times\n";
+				}
+			} elsif ($count) {
+				print "$name executed $count times\n";
+			}
+		}
+	}
+}
+
+sub print_summary()
+{
+	# Output per-line coverage statistics
+	my $line_count = 0;
+	my $line_reached = 0;
+
+	foreach my $source (keys(%all_line)) {
+		next if (!$used_source{$source});
+
+		foreach my $where (keys(%{$all_line{$source}})) {
+			$line_count++;
+			$line_reached++ if ($all_line{$source}{$where} != 0);
+		}
+	}
+	if ($line_count) {
+		my $percent = 100. * $line_reached / $line_count;
+		printf "  lines......: %.1f%% (%d of %d lines)\n",
+			$percent, $line_reached, $line_count;
+	} else {
+		print "No line coverage data.\n";
+	}
+
+	# Output per-function coverage statistics
+	my $func_count = 0;
+	my $func_used = 0;
+
+	foreach my $func (keys(%all_func)) {
+		foreach my $file (keys(%{$all_func{$func}})) {
+			$func_count++;
+			if ($used_func{$func}) {
+				if ($used_func{$func}->{$file}) {
+					$func_used++;
+				}
+			}
+		}
+	}
+
+	if ($func_count) {
+		my $percent = 100. * $func_used / $func_count;
+		printf "  functions..: %.1f%% (%d of %d functions)\n",
+			$percent, $func_used, $func_count;
+	} else {
+		print "No functions reported. Wrong filters?\n";
+		return;
+	}
+
+	# Output per-branch coverage statistics
+	my $branch_count = 0;
+	my $branch_reached = 0;
+
+	foreach my $source (keys(%all_branch)) {
+		next if (!$used_source{$source});
+
+		foreach my $where (keys(%{$all_branch{$source}})) {
+			$branch_count++;
+			$branch_reached++ if ($all_branch{$source}{$where} != 0);
+		}
+	}
+	if ($branch_count) {
+		my $percent = 100. * $branch_reached / $branch_count;
+		printf "  branches...: %.1f%% (%d of %d branches)\n",
+			$percent, $branch_reached, $branch_count;
+	} else {
+		print "No branch coverage data.\n";
+	}
+}
+
+#
+# Argument handling
+#
+
+my $print_used;
+my $print_unused;
+my $stat;
+my $filter;
+my $help;
+my $man;
+my $func_filters;
+my $src_filters;
+my $show_files;
+my $show_lines;
+
+GetOptions(
+	"print-coverage|print_coverage|print|p" => \$print_used,
+	"print-unused|u" => \$print_unused,
+	"stat|statistics" => \$stat,
+	"output|o=s" => \$filter,
+	"verbose|v" => \$verbose,
+	"ignore-unused|ignore_unused" => \$ignore_unused,
+	"only-i915|only_i915" => \$only_i915,
+	"only-drm|only_drm" => \$only_drm,
+	"func-filters|f=s" => \$func_filters,
+	"source-filters|S=s" => \$src_filters,
+	"show-files|show_files" => \$show_files,
+	"show-lines|show_lines" => \$show_lines,
+	"help" => \$help,
+	"man" => \$man,
+) or pod2usage(2);
+
+pod2usage(-verbose => 2) if $man;
+pod2usage(1) if $help;
+
+if ($#ARGV < 0) {
+	print "$0: no input files\n";
+	pod2usage(1);
+}
+
+# At least one action should be specified
+pod2usage(1) if (!$print_used && !$filter && !$stat && !$print_unused);
+
+my $filter_str = "";
+my $has_filter;
+
+if ($func_filters) {
+	open IN, $func_filters || die "Can't open $func_filters";
+	while (<IN>) {
+		s/^\s+//;
+		s/\s+$//;
+		next if (m/^#/ || m/^$/);
+		push @func_regexes, qr /$_/;
+	}
+	close IN;
+}
+
+if ($src_filters) {
+	open IN, $src_filters || die "Can't open $src_filters";
+	while (<IN>) {
+		s/^\s+//;
+		s/\s+$//;
+		next if (m/^#/ || m/^$/);
+		push @src_regexes, qr /$_/;
+	}
+	close IN;
+}
+
+$ignore_unused = 1 if (@func_regexes);
+
+if ($only_i915) {
+	$filter_str = " non-i915 files";
+	$has_filter = 1;
+}
+
+if ($only_drm) {
+	$filter_str .= "," if ($filter_str ne "");
+	$filter_str .= " non-drm headers";
+	$has_filter = 1;
+}
+
+if (@func_regexes) {
+	$filter_str .= "," if ($filter_str ne "");
+	$filter_str .= " unmatched functions";
+	foreach my $r (@func_regexes) {
+		$filter_str .= " m/$r/";
+	}
+
+	$has_filter = 1;
+}
+
+if (@src_regexes) {
+	$filter_str .= "," if ($filter_str ne "");
+	$filter_str .= " unmatched source files";
+	foreach my $r (@src_regexes) {
+		$filter_str .= " m/$r/";
+	}
+	$has_filter = 1;
+}
+
+if ($ignore_unused) {
+	$filter_str .= "," if ($filter_str ne "");
+	$filter_str .= " source files where none of its code ran";
+	$has_filter = 1;
+}
+
+foreach my $f (@ARGV) {
+	parse_info_data($f);
+}
+
+print_code_coverage($print_used, $print_unused, $show_lines);
+
+print_summary() if ($stat);
+
+my $all_files = scalar keys(%files);
+
+die "Nothing counted. Wrong input files?" if (!$all_files);
+
+if ($has_filter) {
+	my $all_files = scalar keys(%files);
+	my $filtered_files = scalar keys(%record);
+	my $used_files = scalar keys(%used_source);
+
+	my $percent = 100. * $used_files / $all_files;
+
+	$filter_str =~ s/(.*),/$1 and/;
+	printf "Ignored......:%s.\n", $filter_str;
+	printf "Source files.: %.2f%% (%d of %d total)",
+		$percent, $used_files, $all_files;
+
+	if ($used_files != $filtered_files) {
+		my $percent_filtered = 100. * $used_files / $filtered_files;
+
+		printf ", %.2f%% (%d of %d filtered)",
+			$percent_filtered, $used_files, $filtered_files;
+	}
+	print "\n";
+} else {
+	printf "Source files: %d\n", scalar keys(%files) if($stat);
+}
+
+if ($show_files) {
+	for my $f(sort keys %used_source) {
+		print "\t$f\n";
+	}
+}
+
+if ($filter) {
+	write_filtered_file($filter);
+}
+
+__END__
+
+=head1 NAME
+
+Parses lcov data from .info files.
+
+=head1 SYNOPSIS
+
+code_cov_parse_info <options> [input file(s)]
+
+At least one of the options B<--stat>, B<--print> and/or B<--output>
+should be used.
+
+=head1 OPTIONS
+
+=over 8
+
+=item B<--stat> or B<--statistics>
+
+Prints code coverage statistics.
+
+It displays function, line, branch and file coverage percentage.
+
+It also reports when one or more of the filtering parameters are used.
+
+The statistics report is affected by the applied filters.
+
+=item B<--print-coverage> or B<--print_coverage> or B<--print> or B<-p>
+
+Prints the functions that were executed in runtime and how many times
+they were reached.
+
+The function coverage report is affected by the applied filters.
+
+=item B<--print-unused> or B<-u>
+
+Prints the functions that were never reached.
+
+The function coverage report is affected by the applied filters.
+
+
+=item B<--show-lines> or B<--show_lines>
+
+When printing per-function code coverage data, always output the source
+file and the line number where the function is defined.
+
+=item B<--output> B<[output file]> or B<-o> B<[output file]>
+
+Produce an output file merging all input files.
+
+The generated output file is affected by the applied filters.
+
+=item B<--only-drm> or B<--only_drm>
+
+Filters out includes outside the DRM subsystem, plus trace files.
+E. g. it will exclude *.h files that match the following regular expressions:
+
+	- .*trace.*\.h$
+
+And *.h files that don't match:
+
+	- drm
+
+=item B<--only-i915> or B<--only_i915>
+
+Filters out C files and headers outside drm core and drm/i915.
+
+E. g. code coverage results will include only the files that that match
+the following regular expressions:
+
+	- drm/i915/
+	- drm/ttm
+	- drm/vgem
+
+Excluding files that match:
+
+	- selftest
+
+=item B<--func-filters>  B<[filter's file]> or B<-f>  B<[filter's file]>
+
+Take into account only the code coverage for the functions that match
+the regular expressions contained at the B<[filter's file]>.
+
+When this filter is used, B<--ignore-unused> will be automaticaly enabled,
+as the final goal is to report per-function usage, and not per-file.
+
+=item B<--source-filters>  B<[filter's file]> or B<-S>  B<[filter's file]>
+
+Takes into account only the code coverage for the source files that match
+the regular expressions contained at the B<[filter's file]>.
+
+=item B<--ignore-unused> or B<--ignore_unused>
+
+Filters out unused C files and headers from the code coverage results.
+
+Sometimes, it is desired to ignore files where none of the functions on it
+were tested.
+
+The rationale is that such files may contain platform-specific drivers
+and code that will never be used, so, placing them will just bloat the
+report and decrease the code coverage statistics.
+
+This option is automaticaly enabled when B<--func-filters> is used.
+
+=back
+
+=item B<--show-files> or B<--show_files>
+
+Shows the list of files that were used to produce the code coverage
+results.
+
+=item B<--verbose> or B<-v>
+
+Prints the name of each parsed file.
+
+=item B<--help>
+
+Print a brief help message and exits.
+
+=item B<--man>
+
+Prints the manual page and exits.
+
+=back
+
+=head1 BUGS
+
+Report bugs to Mauro Carvalho Chehab <[email protected]>
+
+=head1 COPYRIGHT
+
+Copyright (c) 2022 Intel Corporation
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the "Software"),
+to deal in the Software without restriction, including without limitation
+the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice (including the next
+paragraph) shall be included in all copies or substantial portions of the
+Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+=cut
Signature · 1 line
2.35.1
#3 lore raw

PATCH v2 3/12 scripts/code_cov_gen_report: add support for filtering info files

In reply to Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

Now that we have a script that allows filtering the results, add support for it when generating code coverage reports.

---

To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

 scripts/code_cov_gen_report | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/code_cov_gen_report +19 -0
index 05efebe19ee4..b7c90b114cde 100755
@@ -16,6 +16,8 @@ Usage:
     $(basename $0)
 	--read <file or dir> --kernel-source <dir> --kernel-object <dir>
 	--output-dir <dir> [--info or --tar] [--force-override]
+	[--print] [--stat] [--ignore-unused] [--only-i915] [--only-drm]
+	[--show-files] [--func-filters <file>] [--source-filters <file>]
 
 --kernel-object is only needed when Kernel was built with make O=dir
 "
@@ -28,6 +30,7 @@ KSRC=
 KOBJ=
 DEST_DIR=
 FORCE=
+PARSE_ARGS=
 
 while [ "$1" != "" ]; do
 	case $1 in
@@ -69,6 +72,17 @@ while [ "$1" != "" ]; do
 			shift
 		fi
 		;;
+	--print|--stat|--ignore-unused|--only-i915|--only-drm|--show-files)
+		PARSE_ARGS="$PARSE_ARGS $1"
+		;;
+	--func-filters|--source-filters)
+		if [ "$2" == "" ]; then
+			usage 1
+		else
+			PARSE_ARGS="$PARSE_ARGS $1 $2"
+			shift
+		fi
+		;;
 	--force-override|-f)
 		FORCE=1
 		;;
@@ -166,5 +180,10 @@ else
 	fi
 fi
 
+if [ "x$PARSE_ARGS" != "x" ]; then
+	${SCRIPT_DIR}/$PARSE_INFO $PARSE_ARGS --output filtered_${TITLE}.info ${TITLE}.info
+	TITLE=filtered_${TITLE}
+fi
+
 echo "Generating HTML files..."
 genhtml -q ${TITLE}.info
Signature · 1 line
2.35.1
#4 lore raw

PATCH v2 4/12 runner: execute code coverage script also from PATH

In reply to Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

In order to prepare to execute the code coverage scripts from the PATH, change the logic at settings in order for it to seek for the script in the PATH, if it doesn't contain any directories on its filename.

Note: file search routines were moved (unchanged) on this path, in order to avoid forward prototype declarations.

---

To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

 runner/settings.c | 109 +++++++++++++++++++++++++++++-----------------
 1 file changed, 68 insertions(+), 41 deletions(-)
runner/settings.c +68 -41
index cd64b964e5ac..bb63a9f4a1e5 100644
@@ -403,6 +403,73 @@ static bool executable_file(const char *filename)
 	return !access(filename, X_OK);
 }
 
+static char *_dirname(const char *path)
+{
+	char *tmppath = strdup(path);
+	char *tmpname = dirname(tmppath);
+	tmpname = strdup(tmpname);
+	free(tmppath);
+	return tmpname;
+}
+
+static char *_basename(const char *path)
+{
+	char *tmppath = strdup(path);
+	char *tmpname = basename(tmppath);
+	tmpname = strdup(tmpname);
+	free(tmppath);
+	return tmpname;
+}
+
+char *absolute_path(char *path)
+{
+	char *result = NULL;
+	char *base, *dir;
+	char *ret;
+
+	result = realpath(path, NULL);
+	if (result != NULL)
+		return result;
+
+	dir = _dirname(path);
+	ret = absolute_path(dir);
+	free(dir);
+
+	base = _basename(path);
+	asprintf(&result, "%s/%s", ret, base);
+	free(base);
+	free(ret);
+
+	return result;
+}
+
+static char *bin_path(char *fname)
+{
+	char *path, *p;
+	char file[PATH_MAX];
+
+	if (strchr(fname, '/'))
+		return absolute_path(fname);
+
+	path = strdup(getenv("PATH"));
+	p = strtok(path, ":");
+	do {
+		if (*p) {
+			strcpy(file, p);
+			strcat(file, "/");
+			strcat(file, fname);
+			if (executable_file(file)) {
+				free(path);
+				return strdup(file);
+			}
+		}
+		p = strtok(NULL, ":");
+	} while (p);
+
+	free(path);
+	return strdup(fname);
+}
+
 static void print_version(void)
 {
 	struct utsname uts;
@@ -536,7 +603,7 @@ bool parse_options(int argc, char **argv,
 			settings->cov_results_per_test = true;
 			break;
 		case OPT_CODE_COV_SCRIPT:
-			settings->code_coverage_script = absolute_path(optarg);
+			settings->code_coverage_script = bin_path(optarg);
 			break;
 
 		case OPT_MULTIPLE:
@@ -703,46 +770,6 @@ bool validate_settings(struct settings *settings)
 	return true;
 }
 
-static char *_dirname(const char *path)
-{
-	char *tmppath = strdup(path);
-	char *tmpname = dirname(tmppath);
-	tmpname = strdup(tmpname);
-	free(tmppath);
-	return tmpname;
-}
-
-static char *_basename(const char *path)
-{
-	char *tmppath = strdup(path);
-	char *tmpname = basename(tmppath);
-	tmpname = strdup(tmpname);
-	free(tmppath);
-	return tmpname;
-}
-
-char *absolute_path(char *path)
-{
-	char *result = NULL;
-	char *base, *dir;
-	char *ret;
-
-	result = realpath(path, NULL);
-	if (result != NULL)
-		return result;
-
-	dir = _dirname(path);
-	ret = absolute_path(dir);
-	free(dir);
-
-	base = _basename(path);
-	asprintf(&result, "%s/%s", ret, base);
-	free(base);
-	free(ret);
-
-	return result;
-}
-
 static char settings_filename[] = "metadata.txt";
 bool serialize_settings(struct settings *settings)
 {
Signature · 1 line
2.35.1
#5 lore raw

PATCH v2 5/12 scripts/meson.build: install code coverage scripts

In reply to Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

Those scripts are needed at runtime. So, place them under bindir.

---

To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

 meson.build         |  1 +
 scripts/meson.build | 13 +++++++++++++
 2 files changed, 14 insertions(+)
 create mode 100644 scripts/meson.build
meson.build +1 -0
index f9a284d844a3..3e937f5a5b98 100644
@@ -315,6 +315,7 @@ if libdrm_intel.found()
 endif
 subdir('overlay')
 subdir('man')
+subdir('scripts')
 
 gtk_doc = dependency('gtk-doc', required : build_docs)
 if build_tests and gtk_doc.found()
scripts/meson.build new file +13 -0
new file mode 100644
index 000000000000..342972e66078
@@ -0,0 +1,13 @@
+scripts = [
+	'code_cov_capture',
+	'code_cov_gather_on_build',
+	'code_cov_gather_on_test',
+	'code_cov_gen_report',
+	'code_cov_parse_info',
+]
+
+if build_tests
+	foreach prog : scripts
+		install_data(prog, install_dir : bindir, install_mode : 'r-xr-xr-x')
+	endforeach
+endif
Signature · 1 line
2.35.1
#6 lore raw

PATCH v2 6/12 scripts/code_cov_selftest.sh: test if IGT code coverage is working

In reply to Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

The runner_tests.c won't be able to test code coverage, as it requires a kernel specially built for such purpose.

So, add a script that will validate possible steps while doing code coverage.

---

To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

 scripts/code_cov_selftest.sh | 47 ++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100755 scripts/code_cov_selftest.sh
scripts/code_cov_selftest.sh new file +47 -0
new file mode 100755
index 000000000000..1c4bd96a0111
@@ -0,0 +1,47 @@
+#!/bin/bash
+
+trap 'catch $LINENO' ERR
+catch() {
+	echo "===> ERROR: Code coverage selftest failed on $0:$1" >&2
+        exit 1
+}
+
+if [ -z "$IGT_KERNEL_TREE" ] ; then
+        echo "Error! IGT_KERNEL_TREE environment var was not defined." >&2
+        exit 1
+fi
+
+TEST="igt@debugfs_test@read_all_entries"
+
+TESTLIST="my_tests.testlist"
+GATHER="scripts/code_cov_gather_on_test.py"
+LCOV_CAP="scripts/code_cov_capture.sh"
+INFO_RESULTS="info_results"
+TAR_RESULTS="tar_results"
+
+sudo rm -rf results/ $INFO_RESULTS/ $TAR_RESULTS/ || true
+
+echo "$TEST" > $TESTLIST
+
+# run-tests.sh
+echo "==> use lcov capture via run-tests.sh"
+./scripts/run-tests.sh -T $TESTLIST -k $IGT_KERNEL_TREE -c $LCOV_CAP
+echo "==> gather sysfs using run-tests.sh"
+./scripts/run-tests.sh -T $TESTLIST -k $IGT_KERNEL_TREE -P -c $GATHER
+echo "==> gather sysfs using run-tests.sh, capturing at the end"
+./scripts/run-tests.sh -T $TESTLIST -k $IGT_KERNEL_TREE -c $GATHER
+
+# igt_runner called directly
+echo "==> use lcov capture via igt_runner"
+sudo IGT_KERNEL_TREE=$IGT_KERNEL_TREE ./build/runner/igt_runner -o --test-list $TESTLIST --coverage-per-test --collect-script $LCOV_CAP  build/tests results
+echo "==> gather sysfs running igt_runner"
+sudo ./build/runner/igt_runner -o --test-list $TESTLIST --coverage-per-test --collect-script $GATHER build/tests results
+
+# html report
+echo "==> generate report from lcov info files"
+scripts/code_cov_gen_report.sh -r results/code_cov/ -k $IGT_KERNEL_TREE -o $INFO_RESULTS -i --only-i915 --ignore-unused
+echo "==> generate report from sysfs gather files"
+scripts/code_cov_gen_report.sh -r results/code_cov/ -k $IGT_KERNEL_TREE -o $TAR_RESULTS -t --only-drm --ignore-unused
+
+echo
+echo "==> All tests passed. <=="
Signature · 1 line
2.35.1
#7 lore raw

PATCH v2 7/12 docs/code_coverage.md: document the code coverage filter script

In reply to Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

Add documentation bits for the code_coverage_parse_info.

While here, also drop the extensions from the scripts that were renamed.

---

To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

 docs/code_coverage.md | 238 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 224 insertions(+), 14 deletions(-)
docs/code_coverage.md +224 -14
index 34d56d283a75..915e800ed68b 100644
@@ -132,15 +132,15 @@ Kernel that was built at the same machine, at the directory `~/linux`,
 and wants to capture one file per test, it would use:
 
 ```
-./scripts/run-tests.sh -T my.testlist -k ~/linux -c scripts/code_cov_capture.sh -P
+./scripts/run-tests.sh -T my.testlist -k ~/linux -c code_cov_capture -P
 ```
 
 ### Code Coverage Collect script
 
 While any script could in thesis be used, currently, there are two ones
-under `scripts/`:
+under the IGT's `scripts/` source directory:
 
-- `scripts/code_cov_capture.sh`:
+- `code_cov_capture`:
 
   Assumes that the Kernel was built at the same machine, and uses
   the lcov tool to generate GCC-independent code coverage data,
@@ -150,7 +150,7 @@ under `scripts/`:
 
   Such script requires `lcov` tool to be installed at the test machine.
 
-- `scripts/code_cov_gather_on_test.py`:
+- `code_cov_gather_on_test`:
 
   Generates a gzipped tarbal with the code coverage counters in
   binary format. Such kind of output should then be parsed at
@@ -162,12 +162,12 @@ For each script, the igt_runner passes just one parameter: the results
 directory + the test name.
 
 For instance, if it is needed to run a test called
-`debugfs_test (read_all_entries)` using `scripts/code_cov_capture.sh`
+`debugfs_test (read_all_entries)` using `code_cov_capture`
 parameter, e. g.:
 
 ```
 $ echo "igt@debugfs_test@read_all_entries" > my.testlist
-$ ./scripts/run-tests.sh -T my.testlist -k ~/linux -c scripts/code_cov_capture.sh -P
+$ ./scripts/run-tests.sh -T my.testlist -k ~/linux -c code_cov_capture -P
 Found test list: "/basedir/igt/build/tests/test-list.txt"
 [31410.499969] [1/1] debugfs_test (read_all_entries)
 [31411.060446] Storing code coverage results...
@@ -178,7 +178,7 @@ Done.
 The script will be called as:
 
 ```
-scripts/code_cov_capture.sh /basedir/igt/results/code_cov/debugfs_test_read_all_entries
+code_cov_capture results/code_cov/debugfs_test_read_all_entries
 ```
 
 Please notice that any character that it is not a number nor a letter at the
@@ -190,6 +190,73 @@ as titles at the lcov files.
 If any extra global parameters are needed by the script, those can be sent
 via shell's environment var.
 
+## Parsing data from code coverage *.info files
+
+The `*.info` files generated by `lcov` are plain text files that list the
+tests that were executed in runtime.
+
+The `code_cov_parse_info` script has some logic on it that allows
+printing the called functions stored inside the `*.info` file. It can also
+optionally apply the following filters. Its main options are:
+
+- `--stat` or `--statistics`
+
+  Prints code coverage statistics.
+
+  It displays function, line, branch and file coverage percentage.
+
+  The statistics report is affected by the applied filters.
+
+- `--print-coverage`, `--print` or `-p`
+
+
+  Prints the functions that were executed in runtime and how many times
+  they were reached.
+
+  The function coverage report is affected by the applied filters.
+
+- `--print-unused` or `-u`
+
+  Prints the functions that were never reached.
+
+  The function coverage report is affected by the applied filters.
+
+- `--show-lines` or `--show_lines`
+
+  When printing per-function code coverage data, always output the source
+  file and the line number where the function is defined.
+
+- `--output` *output file* or `-o` *output file*
+
+  Produces an output file merging all input files.
+
+  The generated output file is affected by the applied filters.
+
+- `--show-files` or `--show_files`
+
+   Shows the list of files that were useed to produce the code coverage
+   results.
+
+- It also has a set of parameters that filters the code coverage results:
+  `--only-drm`, `--only-i915`, `--func-filters`, `--source-filters`,
+  `--ignore-unused`.
+  When used, all coverage displayed reports, and the stored output file
+  will be affected by such filters.
+
+More details can be seen by calling:
+
+```
+code_cov_parse_info --help
+
+```
+
+or:
+
+```
+code_cov_parse_info --man
+
+```
+
 ### The `*.info` file format
 
 The `*.info` files contain several fields on it, grouped into records.
@@ -224,7 +291,7 @@ So, the above example means that, inside
 ## Generating code coverage documentation
 
 The `lcov` package contains the needed tools to parse and generate code
-coverage documentation. It is used by `scripts/code_cov_capture.sh` script
+coverage documentation. It is used by `code_cov_capture` script
 to convery from compiler-dependent `*.gcno` counters into a
 compiler-independent format (`*.info`).
 
@@ -243,11 +310,16 @@ source files. Some optional arguments can be used at the command line, or
 can be stored at `/etc/lcovrc` or `~/.lcovrc` files.
 
 As generating the documentation depends wheather the results were generated
-as with a single or multiple `*.info` files by `scripts/code_cov_capture.sh`
+as with a single or multiple `*.info` files by `code_cov_capture`
 or stored in raw formats inside `*.tar.gz` file(s) by
-`scripts/code_cov_gather_on_test.py`, there's a script that does all the
+`code_cov_gather_on_test`, there's a script that does all the
 required steps to build the code coverage html reports:
-`scripts/code_cov_gen_report.sh`.
+`code_cov_gen_report`. Besides its own command line arguments, it
+also accepts arguments to be passed to `code_cov_parse_info`.
+
+If a `code_cov_parse_info` command line parameter is passed, it will
+also call the script, in order to use a filtered `*.info` file to be
+used when generating the HTML reports.
 
 It requires the following arguments:
 
@@ -276,18 +348,156 @@ It requires the following arguments:
 
   The files specified by `--read` are gzipped tarballs containing all
   `*.gcno` files and all `*.gcda` softlinks from the `/sys/kernel/debug/gcov/`
-  directory at the test machine, created by
-  `scripts/code_cov_gather_on_test.py` script.
+  directory at the test machine, created by `code_cov_gather_on_test` script.
 
 - `--force-override`
 
   Allow using a non-empty directory for `--output-dir`.
 
+- It also accepts `--print`, `--only-drm`, `--only-i915` and `--ignore-unused`
+  options from `code_cov_parse_info`.
+
 `--info` and `--tar` are mutually exclusive and at least one of them should
 be specified.
 
+## Code coverage capture script example
+
+### Capture, parse and generate code coverage html data
+
+The script below provides a simple yet powerful script using code
+coverage capture on a test machine that also contains the Linux
+Kernel source and objects. It assumes that LGT was installed.
+
+```
+#/bin/bash -e
+
+TESTLIST="my_tests.testlist"
+OUT_DIR="${HOME}/results"
+
+mkdir -p $OUT_DIR/html
+
+echo "igt@debugfs_test@read_all_entries" > $TESTLIST
+echo "igt@core_auth@basic-auth" >> $TESTLIST
+echo "igt@gem_exec_basic@basic" >> $TESTLIST
+
+sudo IGT_KERNEL_TREE="${HOME}/linux" igt_runner -s -o --coverage-per-test \
+                  --collect-script code_cov_capture --test-list $TESTLIST \
+                  /usr/local/libexec/igt-gpu-tools $OUT_DIR/ | sed s,$HOME/,,
+
+sudo chown -R $(id -u):$(id -g) $OUT_DIR/
+
+for i in $OUT_DIR/code_cov/*.info; do
+        echo -e "\n$(basename $i):"
+        code_cov_parse_info --only-drm --ignore-unused --stat $i
+done
+echo -e "\nTOTAL:"
+code_cov_parse_info --only-drm --stat --output $OUT_DIR/results.info \
+        $OUT_DIR/code_cov/*.info
+
+cd $OUT_DIR/html
+genhtml -q -s --legend --branch-coverage $OUT_DIR/results.info
+```
+
+Running such script produces the following output:
+
+```
+[3622.993304] [1/3] debugfs_test (read_all_entries)
+[3631.95]     Code coverage wrote to results/code_cov/debugfs_test_read_all_entries.info
+[3626.217016] Storing code coverage results...
+[3631.957998] [2/3] core_auth (basic-auth)
+[3638.03]     Code coverage wrote to results/code_cov/core_auth_basic_auth.info
+[3632.116024] Storing code coverage results...
+[3638.070869] [3/3] gem_exec_basic (basic)
+[3644.24]     Code coverage wrote to results/code_cov/gem_exec_basic_basic.info
+[3638.366790] Storing code coverage results...
+Done.
+
+core_auth_basic_auth.info:
+  lines......: 11.7% (8217 of 70257 lines)
+  functions..: 7.1% (776 of 10971 functions)
+  branches...: 7.0% (3596 of 51041 branches)
+Ignored......: non-drm headers and source files where none of its code ran.
+Source files.: 23.27% (165 of 709 total), 29.57% (165 of 558 filtered)
+
+debugfs_test_read_all_entries.info:
+  lines......: 19.3% (20266 of 104802 lines)
+  functions..: 17.5% (1922 of 10971 functions)
+  branches...: 12.7% (9462 of 74555 branches)
+Ignored......: non-drm headers and source files where none of its code ran.
+Source files.: 34.70% (246 of 709 total), 44.09% (246 of 558 filtered)
+
+gem_exec_basic_basic.info:
+  lines......: 17.1% (14964 of 87503 lines)
+  functions..: 13.0% (1422 of 10971 functions)
+  branches...: 10.1% (6446 of 63758 branches)
+Ignored......: non-drm headers and source files where none of its code ran.
+Source files.: 30.89% (219 of 709 total), 39.25% (219 of 558 filtered)
+
+TOTAL:
+  lines......: 15.5% (25821 of 166849 lines)
+  functions..: 22.1% (2429 of 10971 functions)
+  branches...: 10.5% (11869 of 112665 branches)
+Ignored......: non-drm headers.
+Source files.: 78.70% (558 of 709 total)
+```
+
+### Reporting detailed function coverage stored on *.info files
+
+The `code_cov_parse_info` script can be used alone in order to provide
+a text file output containing code coverage data obtained from a *.info
+file. For example, listing code coverage usage for all functions whose name
+contains "edid_" can be done with:
+
+```
+$ echo edid_ >filter.txt
+$ code_cov_parse_info --func-filters filter.txt results/results.info -p -u --stat
+TEST: Code_coverage_tests
+__drm_get_edid_firmware_path(): unused
+__drm_set_edid_firmware_path(): unused
+displayid_iter_edid_begin(): executed 10 times
+drm_add_edid_modes(): executed 2 times
+drm_add_override_edid_modes(): unused
+drm_connector_attach_edid_property(): unused
+drm_connector_update_edid_property(): executed 8 times
+drm_dp_send_real_edid_checksum(): unused
+drm_edid_are_equal(): executed 4 times
+drm_edid_block_valid(): executed 8 times
+drm_edid_duplicate(): unused
+drm_edid_get_monitor_name(): unused
+drm_edid_header_is_valid(): executed 4 times
+drm_edid_is_valid(): executed 2 times
+drm_edid_to_eld(): executed 2 times
+drm_edid_to_sad(): unused
+drm_edid_to_speaker_allocation(): unused
+drm_find_edid_extension(): executed 22 times
+drm_get_edid_switcheroo(): unused
+drm_load_edid_firmware(): executed 2 times
+edid_firmware_get(): unused
+edid_firmware_set(): unused
+edid_fixup_preferred(): unused
+edid_get_quirks(): executed 6 times
+edid_load(): unused
+edid_open(): executed 4 times
+edid_show() from linux/drivers/gpu/drm/drm_debugfs.c: executed 4 times
+edid_show() from linux/drivers/gpu/drm/drm_sysfs.c: unused
+edid_vendor(): executed 348 times
+edid_write(): unused
+intel_panel_edid_downclock_mode(): unused
+intel_panel_edid_fixed_mode(): unused
+is_edid_digital_input_dp(): unused
+  lines......: 5.5% (5 of 91 lines)
+  functions..: 42.4% (14 of 33 functions)
+  branches...: 1.9% (1 of 52 branches)
+Ignored......: unmatched functions m/(?^:edid_)/ and source files where none of its code ran.
+Source files.: 0.90% (5 of 558 total), 55.56% (5 of 9 filtered)
+```
+
+When the function is unique, it will just display the function name and how
+many times the IGT test(s) executed it. When the same function name exists
+on multiple files (like the `edid_show()` on the above example), it will
+display multiple lines, one for each different function/file combination.
+
 ## References
 
 More information is available at Kernel gcov documentation:
 [Using gcov with the Linux kernel](https://www.kernel.org/doc/html/latest/dev-tools/gcov.html).
-
Signature · 1 line
2.35.1
Reviewed-by on v2 7/12
#8 lore raw

Re: [igt-dev] [PATCH v2 07/12] docs/code_coverage.md: document the code coverage filter script

Quoted 10 lines · Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

Add documentation bits for the code_coverage_parse_info.

While here, also drop the extensions from the scripts that were renamed.

---

Regards
Andrzej

#9 lore raw

PATCH v2 8/12 scripts/code_cov_parse_info: better handle test name

In reply to Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

The TN field generated by standard lcov is weird: it keeps repeating the TN field from time to time. At genhtml, it seems that only the first one is used.

As we're using TN to indicate the test name, preserve all different test names at the output file.

Also, printing such names doesn't really makes sense when --print-used and --print-unused command line options are used, and printing a list of 100+ names won't make much sense.

So, just remove printing the test names.

---

To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

 scripts/code_cov_parse_info | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)
scripts/code_cov_parse_info +8 -12
index 604812b41580..3e1525a67d99 100755
@@ -17,8 +17,8 @@ my %used_source;
 my %record;
 my %files;
 my @func_regexes;
+my %test_names;
 my @src_regexes;
-my $testname = "";
 
 my $verbose = 0;
 my $ignore_unused = 0;
@@ -99,11 +99,7 @@ sub parse_info_data($)
 		if (m/^TN:(.*)/) {
 			if ($1 ne $cur_test) {
 				$cur_test = $1;
-				if (!$testname) {
-					$testname = $cur_test;
-				} else {
-					$testname = "Code_coverage_tests";
-				}
+				$test_names{$cur_test} = 1;
 			}
 			$source = $before_sf;
 			$func = $before_sf;
@@ -297,9 +293,13 @@ sub write_filtered_file($)
 {
 	my $filter = shift;
 
+	my $filtered = "";
+
+	foreach my $testname(sort keys %test_names) {
+		$filtered .= "TN:$testname\n";
+	}
+
 	# Generates filtered data
-	my $filtered = "TN:$testname\n";
-
 	foreach my $source(sort keys %record) {
 		next if (!$used_source{$source});
 
@@ -363,10 +363,6 @@ sub print_code_coverage($$$)
 
 	return if (!$print_used && !$print_unused);
 
-	if ($testname ne "") {
-		$testname =~ s/(.*)_on_(\w+)$/$1 on $2/;
-		print "TEST: $testname\n";
-	}
 	my $prev_file = "";
 
 	foreach my $func (sort keys(%all_func)) {
Signature · 1 line
2.35.1
Reviewed-by on v2 8/12
#10 lore raw

Re: [igt-dev] [PATCH v2 08/12] scripts/code_cov_parse_info: better handle test name

Quoted 13 lines · Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

The TN field generated by standard lcov is weird: it keeps repeating the TN field from time to time. At genhtml, it seems that only the first one is used.

As we're using TN to indicate the test name, preserve all different test names at the output file.

Also, printing such names doesn't really makes sense when --print-used and --print-unused command line options are used, and printing a list of 100+ names won't make much sense.

Quoted 7 lines

So, just remove printing the test names.

---

Regards
Andrzej

#11 lore raw

PATCH v2 9/12 code_cov_parse_info: fix error handling when opening files

In reply to Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

While on bash scripts we use "||" for error handling, perl uses "or" instead. Yet, it doesn't warn when "||" is used.

That causes it to print a warning when the open fail while reading filter files, instead of the original intent of finishing and reporting an error.

---

To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

 scripts/code_cov_parse_info | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/code_cov_parse_info +2 -2
index 3e1525a67d99..c8284a297b33 100755
@@ -513,7 +513,7 @@ my $filter_str = "";
 my $has_filter;
 
 if ($func_filters) {
-	open IN, $func_filters || die "Can't open $func_filters";
+	open IN, $func_filters or die "Can't open $func_filters";
 	while (<IN>) {
 		s/^\s+//;
 		s/\s+$//;
@@ -524,7 +524,7 @@ if ($func_filters) {
 }
 
 if ($src_filters) {
-	open IN, $src_filters || die "Can't open $src_filters";
+	open IN, $src_filters or die "Can't open $src_filters";
 	while (<IN>) {
 		s/^\s+//;
 		s/\s+$//;
Signature · 1 line
2.35.1
Reviewed-by on v2 9/12
#12 lore raw

Re: [igt-dev] [PATCH v2 09/12] code_cov_parse_info: fix error handling when opening files

Quoted 4 lines · Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

While on bash scripts we use "||" for error handling, perl uses "or" instead. Yet, it doesn't warn when "||" is used.

Perl accepts || as well (this is why it does not warn), but it has higher priority so in cases below it will behave different than or. So the above sentence is misleading.

Quoted 19 lines

That causes it to print a warning when the open fail while reading filter files, instead of the original intent of finishing and reporting an error.

---
To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

  scripts/code_cov_parse_info | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
scripts/code_cov_parse_info +0 -0
index 3e1525a67d99..c8284a297b33 100755

scripts/code_cov_parse_info my $filter_str = ""; · line 516

Quoted 5 lines
scripts/code_cov_parse_info +1 -1
@@ -513,7 +513,7 @@ my $filter_str = "";
  my $has_filter;
 
  if ($func_filters) {
-	open IN, $func_filters || die "Can't open $func_filters";
+	open IN, $func_filters or die "Can't open $func_filters";

Replacing with:
    open(IN, $func_filters) || die "Can't open $func_filters"; should help as well.

With or without slightly adjusted commit message.

Regards
Andrzej

Quoted 12 lines
scripts/code_cov_parse_info +1 -1
  	while (<IN>) {
  		s/^\s+//;
  		s/\s+$//;
@@ -524,7 +524,7 @@ if ($func_filters) {
  }
 
  if ($src_filters) {
-	open IN, $src_filters || die "Can't open $src_filters";
+	open IN, $src_filters or die "Can't open $src_filters";
  	while (<IN>) {
  		s/^\s+//;
  		s/\s+$//;
#13 lore raw

Re: [igt-dev] [PATCH v2 09/12] code_cov_parse_info: fix error handling when opening files

Quoted 9 lines · On Wed, 13 Apr 2022 15:27:16 +0200 Andrzej Hajda <[email protected]>…
Quoted 4 lines · Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

While on bash scripts we use "||" for error handling, perl uses "or" instead. Yet, it doesn't warn when "||" is used.

Perl accepts || as well (this is why it does not warn), but it has higher priority so in cases below it will behave different than or. So the above sentence is misleading.

Yeah, I was pretty sure I used || before... didn't notice that it would be doing something different without the parenthesis.

Quoted 29 lines
Quoted 19 lines

That causes it to print a warning when the open fail while reading filter files, instead of the original intent of finishing and reporting an error.

---
To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

  scripts/code_cov_parse_info | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
scripts/code_cov_parse_info +0 -0
index 3e1525a67d99..c8284a297b33 100755

scripts/code_cov_parse_info my $filter_str = ""; · line 516

Quoted 5 lines
scripts/code_cov_parse_info +1 -1
@@ -513,7 +513,7 @@ my $filter_str = "";
  my $has_filter;
 
  if ($func_filters) {
-	open IN, $func_filters || die "Can't open $func_filters";
+	open IN, $func_filters or die "Can't open $func_filters";  

Replacing with:
    open(IN, $func_filters) || die "Can't open $func_filters";
should help as well.

Quoted 3 lines

With or without slightly adjusted commit message.

Thanks!

Mauro

#14 lore raw

PATCH v2 10/12 code_cov_parse_info: fix --show-lines logic

In reply to Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

print_code_coverage function needs to first read the number of lines and then simplify the $file by removing its prefix.

Without that, it ends trying to access an uninitiated value, leading to warnings and not doing what's expected.

While here, also check if the ln data is valid, just in case the .info file might have some FN field(s) missing.

---

To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

 scripts/code_cov_parse_info | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/code_cov_parse_info +3 -1
index c8284a297b33..0d9bac38cea9 100755
@@ -378,8 +378,10 @@ sub print_code_coverage($$$)
 			}
 
 			if ($show_lines) {
+				my $ln = $all_func{$func}{$file}->{ln};
 				$file =~ s,$prefix,linux/,;
-				$name = "$func() from $file:" . $all_func{$func}{$file}->{ln};
+				$name = "$func() from $file";
+				$name .= ":" . $ln if ($ln);
 			} elsif (scalar @keys > 1) {
 				$file =~ s,$prefix,linux/,;
 				$name = "$func() from $file:";
Signature · 1 line
2.35.1
#15 lore raw

PATCH v2 11/12 code_cov_parse_info: add support for exclude filters

In reply to Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

It is interesting to have support not only for including, but also for excluding functions and files. Also, it is trivial to have support for it.

When either one or both source/function filters are enabled, it will print at the console the regular expressions that are applied to functions and sources (if any), e. g.:

$ cat << END >f1
+i915
gem

- display
-selftest
END

$ cat << END >f2
	-/selftests
END
$ code_cov_parse_info dg1.info dg2.info --func-filter f1 --source-filters f2 --stat
  lines......: 72.1% (176 of 244 lines)
  functions..: 77.3% (17 of 22 functions)
  branches...: 50.0% (68 of 136 branches)
Filters......: function regex (not match: m`display` m`selftest` and match: m`i915` m`gem`), source regex (match: m`/selftest`) and ignored source files where none of its code ran.
Source files.: 0.99% (7 of 710 total), 77.78% (7 of 9 filtered)

---

To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

 scripts/code_cov_parse_info | 172 +++++++++++++++++++++++++-----------
 1 file changed, 122 insertions(+), 50 deletions(-)
scripts/code_cov_parse_info +122 -50
index 0d9bac38cea9..7336f2be7978 100755
@@ -17,8 +17,10 @@ my %used_source;
 my %record;
 my %files;
 my @func_regexes;
+my @func_exclude_regexes;
 my %test_names;
 my @src_regexes;
+my @src_exclude_regexes;
 
 my $verbose = 0;
 my $ignore_unused = 0;
@@ -28,10 +30,16 @@ my $skip_func = 0;
 
 sub is_function_excluded($)
 {
+	return 0 if (!@func_regexes && !@func_exclude_regexes);
+
+	my $func = shift;
+
+	foreach my $r (@func_exclude_regexes) {
+		return 1 if ($func =~ m/$r/);
+	}
+
 	return 0 if (!@func_regexes);
 
-	my $func = shift;
-
 	foreach my $r (@func_regexes) {
 		return 0 if ($func =~ m/$r/);
 	}
@@ -64,10 +72,14 @@ sub filter_file($)
 		}
 	}
 
+	return 0 if (!@src_regexes && !@src_exclude_regexes);
+
+	foreach my $r (@src_exclude_regexes) {
+		return 1 if ($s =~ m/$r/);
+	}
+
 	return 0 if (!@src_regexes);
 
-	my $func = shift;
-
 	foreach my $r (@src_regexes) {
 		return 0 if ($s =~ m/$r/);
 	}
@@ -468,6 +480,46 @@ sub print_summary()
 	}
 }
 
+sub open_filter_file($$$)
+{
+	my $fname = shift;
+	my $include = shift;
+	my $exclude = shift;
+	my $match_str = "";
+	my $not_match_str = "";
+	my $filter = "";
+
+	open IN, $fname or die "Can't open $fname";
+	while (<IN>) {
+		s/^\s+//;
+		s/\s+$//;
+		next if (m/^#/ || m/^$/);
+		if (m/^([+\-])\s*(.*)/) {
+			if ($1 eq "+") {
+				$match_str .= sprintf "m`$2` ";
+				push @{$include}, qr /$2/;
+			} else {
+				$not_match_str .= sprintf "m`$2` ";
+				push @{$exclude}, qr /$2/;
+			}
+		} else {
+			$match_str .= sprintf "m`$_` ";
+			push @{$include}, qr /$_/;
+		}
+	}
+	close IN;
+
+	$filter .= "not match: $not_match_str" if ($not_match_str);
+	if ($match_str) {
+		$filter .= "and " if ($filter ne "");
+		$filter .= "match: $match_str";
+	}
+
+	$filter =~ s/\s+$//;
+
+	return $filter;
+}
+
 #
 # Argument handling
 #
@@ -515,62 +567,35 @@ my $filter_str = "";
 my $has_filter;
 
 if ($func_filters) {
-	open IN, $func_filters or die "Can't open $func_filters";
-	while (<IN>) {
-		s/^\s+//;
-		s/\s+$//;
-		next if (m/^#/ || m/^$/);
-		push @func_regexes, qr /$_/;
-	}
-	close IN;
+	my $str = open_filter_file($func_filters, \@func_regexes, \@func_exclude_regexes);
+	$filter_str .= "," if ($filter_str ne "");
+	$filter_str .= " function regex ($str)";
+	$has_filter = 1;
 }
 
 if ($src_filters) {
-	open IN, $src_filters or die "Can't open $src_filters";
-	while (<IN>) {
-		s/^\s+//;
-		s/\s+$//;
-		next if (m/^#/ || m/^$/);
-		push @src_regexes, qr /$_/;
-	}
-	close IN;
+	my $str = open_filter_file($src_filters, \@src_regexes, \@src_exclude_regexes);
+	$filter_str .= "," if ($filter_str ne "");
+	$filter_str .= " source regex ($str)";
+	$has_filter = 1;
 }
 
-$ignore_unused = 1 if (@func_regexes);
+$ignore_unused = 1 if (@func_regexes || @func_exclude_regexes);
 
 if ($only_i915) {
-	$filter_str = " non-i915 files";
+	$filter_str = " ignored non-i915 files";
 	$has_filter = 1;
 }
 
 if ($only_drm) {
 	$filter_str .= "," if ($filter_str ne "");
-	$filter_str .= " non-drm headers";
-	$has_filter = 1;
-}
-
-if (@func_regexes) {
-	$filter_str .= "," if ($filter_str ne "");
-	$filter_str .= " unmatched functions";
-	foreach my $r (@func_regexes) {
-		$filter_str .= " m/$r/";
-	}
-
-	$has_filter = 1;
-}
-
-if (@src_regexes) {
-	$filter_str .= "," if ($filter_str ne "");
-	$filter_str .= " unmatched source files";
-	foreach my $r (@src_regexes) {
-		$filter_str .= " m/$r/";
-	}
+	$filter_str .= " ignored non-drm headers";
 	$has_filter = 1;
 }
 
 if ($ignore_unused) {
 	$filter_str .= "," if ($filter_str ne "");
-	$filter_str .= " source files where none of its code ran";
+	$filter_str .= " ignored source files where none of its code ran";
 	$has_filter = 1;
 }
 
@@ -594,7 +619,7 @@ if ($has_filter) {
 	my $percent = 100. * $used_files / $all_files;
 
 	$filter_str =~ s/(.*),/$1 and/;
-	printf "Ignored......:%s.\n", $filter_str;
+	printf "Filters......:%s.\n", $filter_str;
 	printf "Source files.: %.2f%% (%d of %d total)",
 		$percent, $used_files, $all_files;
 
@@ -699,16 +724,63 @@ Excluding files that match:
 
 =item B<--func-filters>  B<[filter's file]> or B<-f>  B<[filter's file]>
 
-Take into account only the code coverage for the functions that match
-the regular expressions contained at the B<[filter's file]>.
+Use a file containing regular expressions (regex) to filter functions.
 
-When this filter is used, B<--ignore-unused> will be automaticaly enabled,
-as the final goal is to report per-function usage, and not per-file.
+Each line at B<[filter's file]> may contain a new regex:
+
+=over 4
+
+- Blank lines and lines starting with B<#> will be ignored;
+
+- Each line of the file will be handled as a new regex;
+
+- If B<+regex> is used, the filter will include B<regex> to the matches;
+
+- If B<-regex> is used, the filter will exclude B<regex> from the matches;
+
+- If the line doesn't start with neither B<+> nor B<->, containing just
+  B<regex>, the filter will include B<regex> to the matches.
+
+- Any whitespace/tab before or after B<regex> will be ignored.
+
+=back
+
+When both include and exclude regexes are found, exclude regexes are
+applied first and any functions that don't match the include regular
+expressions from the B<[filter's file]> will be ignored.
+
+Please notice that, when this filter is used, B<--ignore-unused> will be
+automaticaly enabled, as the final goal is to report per-function usage.
 
 =item B<--source-filters>  B<[filter's file]> or B<-S>  B<[filter's file]>
 
-Takes into account only the code coverage for the source files that match
-the regular expressions contained at the B<[filter's file]>.
+Use a file containing regular expressions to filter source files.
+
+Each line of the file will be handled as a new regular expressions.
+Blank lines and lines starting with B<#> will be ignored.
+
+Each line at B<[filter's file]> may contain a new regex:
+
+=over 4
+
+- Blank lines and lines starting with B<#> will be ignored;
+
+- Each line of the file will be handled as a new regex;
+
+- If B<+regex> is used, the filter will include B<regex> to the matches;
+
+- If B<-regex> is used, the filter will exclude B<regex> from the matches;
+
+- If the line doesn't start with neither B<+> nor B<->, containing just
+  B<regex>, the filter will include B<regex> to the matches.
+
+- Any whitespace/tab before or after B<regex> will be ignored.
+
+=back
+
+When both include and exclude regexes are found, exclude regexes are
+applied first and any functions that don't match the include regular
+expressions from the B<[filter's file]> will be ignored.
 
 =item B<--ignore-unused> or B<--ignore_unused>
 
Signature · 1 line
2.35.1
Reviewed-by on v2 11/12
#16 lore raw

Re: [igt-dev] [PATCH v2 11/12] code_cov_parse_info: add support for exclude filters

Quoted 17 lines · Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

It is interesting to have support not only for including, but also for excluding functions and files. Also, it is trivial to have support for it.

When either one or both source/function filters are enabled, it will print at the console the regular expressions that are applied to functions and sources (if any), e. g.:

$ cat << END >f1
+i915
gem
(quoted diff) +0 -2
-  display
-selftest

END

Quoted 3 lines
$ cat << END >f2
	-/selftests
END

I am not sure if we really needs so tolerant parser, up to you.

Quoted 1 line
$ code_cov_parse_info dg1.info dg2.info --func-filter f1 --source-filters f2 --stat

s/func-filter/func-filters/

Btw, maybe would be good to have inline fliters:

$ code_cov_parse_info dg1.info dg2.info --func-filter '+i915,+gem,-display,-selftest' --source-filters '-/selftest' --stat

For this I would make '+/-' at the beginning required, just an idea, could be implemented later.

Quoted 4 lines
   lines......: 72.1% (176 of 244 lines)
   functions..: 77.3% (17 of 22 functions)
   branches...: 50.0% (68 of 136 branches)

Filters......: function regex (not match: mdisplay mselftest and match: mi915 mgem), source regex (match: m/selftest) and ignored source files where none of its code ran.

I would use then here "+i915,+gem,-display,-selftest" format, just shorter lines.

Quoted 4 lines

Source files.: 0.99% (7 of 710 total), 77.78% (7 of 9 filtered)

---

With fixed typo, and optionally implemented suggestions:

Regards
Andrzej

#17 lore raw

Re: [igt-dev] [PATCH v2 11/12] code_cov_parse_info: add support for exclude filters

Quoted 26 lines · On Wed, 13 Apr 2022 15:53:01 +0200 Andrzej Hajda <[email protected]>…
Quoted 17 lines · Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

It is interesting to have support not only for including, but also for excluding functions and files. Also, it is trivial to have support for it.

When either one or both source/function filters are enabled, it will print at the console the regular expressions that are applied to functions and sources (if any), e. g.:

$ cat << END >f1
+i915
gem
(quoted diff) +0 -2
-  display
-selftest

END

Quoted 3 lines
$ cat << END >f2
	-/selftests
END  

I am not sure if we really needs so tolerant parser, up to you.

Quoted 1 line
$ code_cov_parse_info dg1.info dg2.info --func-filter f1 --source-filters f2 --stat  
Quoted 8 lines

s/func-filter/func-filters/

Btw, maybe would be good to have inline fliters:

$ code_cov_parse_info dg1.info dg2.info --func-filter '+i915,+gem,-display,-selftest' --source-filters '-/selftest' --stat

For this I would make '+/-' at the beginning required, just an idea, could be implemented later.

Yeah, this would make easier for simple cases. Yet, a file would help to have a more complex ruleset and add some documentation on it, as comments.

One alternative would be to have both (using different options).

Yet, using commas there is not a good idea... The Linux kernel has lots of files now with commas, at DT. Ok, those aren't supposed to contain code, but nothing prevents that someone would add commas also on c files on some future.

So, the safest option would be to use an array at getopt, and having multiple tags, like:

	code_cov_parse_info dg1.info dg2.info --func-filter '+i915" --func-filter "+gem"

On such case, we could use something like:

	code_cov_parse_info dg1.info dg2.info --include-func i915 --include-func gem --exclude-func selftest

There are two interesting size effects with that:

1) no need to add any parser for it. Just adding:

	--- a/scripts/code_cov_parse_info
	+++ b/scripts/code_cov_parse_info
	@@ -811,7 +811,11 @@ GetOptions(
	 	"only-i915|only_i915" => \$only_i915,
	 	"only-drm|only_drm" => \$only_drm,
	 	"func-filters|f=s" => \$func_filters,
	+	"include-func=s" => \@func_regexes,
	+	"exclude-func=s" => \@func_exclude_regexes,
	 	"source-filters|S=s" => \$src_filters,
	+	"include-source=s" => \@src_regexes,
	+	"exclude-source=s" => \@src_exclude_regexes,
	 	"show-files|show_files" => \$show_files,
	 	"show-lines|show_lines" => \$show_lines,
	 	"report|r=s" => \$gen_report,

Is enough for it to work (plus documentation and some extra logic to print the filters).

2) it can be used together with the files.

3) when adding support for properly reporting the filters, the the logic that handles the two fixed filters (--only-i915 and --only-drm filters) can be simplified.

4) by removing the specific implementation for --only-i915 and --only-drm, it is now possible to show all regexes applied to the file, e. g.:

	$ scripts/code_cov_parse_info --stat dg1.info dg2.info --exclude-func intel_display_power --only-drm --only-i915 --source-filters my_filter.txt 
	  lines......: 50.5% (30021 of 59401 lines)
	  functions..: 58.4% (2685 of 4599 functions)
	  branches...: 31.8% (12949 of 40751 branches)
	Filters......: function regex (not match: m`intel_display_power`), source regex (not match: m`selftest` m`trace.*.h` m`drm.*.h` m`display` and match: m`drm/i915` m`drm/ttm` m`drm/vgem` m`i915` m`gem`) and ignored source files where none of its code ran.
	Source files.: 20.56% (146 of 710 total), 82.95% (146 of 176 filtered)
	Number of tests: 152

So, I ended opting to do that. I'm enclosing the diff against this patch at the end.

Quoted 11 lines
Quoted 5 lines
   lines......: 72.1% (176 of 244 lines)
   functions..: 77.3% (17 of 22 functions)
   branches...: 50.0% (68 of 136 branches)

Filters......: function regex (not match: mdisplay mselftest and match: mi915 mgem), source regex (match: m/selftest) and ignored source files where none of its code ran.

I would use then here "+i915,+gem,-display,-selftest" format, just shorter lines.

Quoted 2 lines

Source files.: 0.99% (7 of 710 total), 77.78% (7 of 9 filtered)

Quoted 8 lines
Quoted 2 lines

---

With fixed typo, and optionally implemented suggestions:

Thanks! Will add it to the new version.

Quoted 4 lines

Regards
Andrzej

scripts/code_cov_parse_info +87 -53
index d6ec5695a358..77291eb69405 100755
@@ -26,8 +26,6 @@ my @src_exclude_regexes;
 
 my $verbose = 0;
 my $ignore_unused = 0;
-my $only_i915 = 0;
-my $only_drm = 0;
 my $skip_func = 0;
 
 sub is_function_excluded($)
@@ -53,27 +51,6 @@ sub filter_file($)
 {
 	my $s = shift;
 
-	if ($only_drm) {
-		# Please keep --only-drm doc updated with any changes her
-		if ($s =~ m/\.h$/) {
-			if ($s =~ m/trace/ || !($s =~ m/drm/)) {
-				return 1;
-			}
-		}
-	}
-
-	if ($only_i915) {
-		# Please keep --only-i915 doc updated with any changes here
-		if ($s =~ m/selftest/) {
-			return 1;
-		}
-
-		# Please keep --only-i915 doc updated with any changes here
-		if (!($s =~ m#drm/i915/# || $s =~ m#drm/ttm# || $s =~ m#drm/vgem#)) {
-			return 1;
-		}
-	}
-
 	return 0 if (!@src_regexes && !@src_exclude_regexes);
 
 	foreach my $r (@src_exclude_regexes) {
@@ -506,26 +483,43 @@ sub open_filter_file($$$)
 	my $match_str = "";
 	my $not_match_str = "";
 	my $filter = "";
+	my $i;
 
-	open IN, $fname or die "Can't open $fname";
-	while (<IN>) {
-		s/^\s+//;
-		s/\s+$//;
-		next if (m/^#/ || m/^$/);
-		if (m/^([+\-])\s*(.*)/) {
-			if ($1 eq "+") {
-				$match_str .= sprintf "m`$2` ";
-				push @{$include}, qr /$2/;
+	# Handle regexes that came from command line params
+
+	for ($i = 0;$i < scalar(@{$include}); $i++) {
+		my $op = @{$include}[$i];
+		$match_str .= sprintf "m`$op` ";
+		@{$include}[$i] = qr /$op/;
+	}
+
+	for ($i = 0;$i < scalar(@{$exclude}); $i++) {
+		my $op = @{$exclude}[$i];
+		$not_match_str .= sprintf "m`$op` ";
+		@{$exclude}[$i] = qr /$op/;
+	}
+
+	if ($fname) {
+		open IN, $fname or die "Can't open $fname";
+		while (<IN>) {
+			s/^\s+//;
+			s/\s+$//;
+			next if (m/^#/ || m/^$/);
+			if (m/^([+\-])\s*(.*)/) {
+				if ($1 eq "+") {
+					$match_str .= sprintf "m`$2` ";
+					push @{$include}, qr /$2/;
+				} else {
+					$not_match_str .= sprintf "m`$2` ";
+					push @{$exclude}, qr /$2/;
+				}
 			} else {
-				$not_match_str .= sprintf "m`$2` ";
-				push @{$exclude}, qr /$2/;
+				$match_str .= sprintf "m`$_` ";
+				push @{$include}, qr /$_/;
 			}
-		} else {
-			$match_str .= sprintf "m`$_` ";
-			push @{$include}, qr /$_/;
 		}
+		close IN;
 	}
-	close IN;
 
 	$filter .= "not match: $not_match_str" if ($not_match_str);
 	if ($match_str) {
@@ -800,6 +794,8 @@ my $func_filters;
 my $src_filters;
 my $show_files;
 my $show_lines;
+my $only_i915;
+my $only_drm;
 
 GetOptions(
 	"print-coverage|print_coverage|print|p" => \$print_used,
@@ -811,7 +807,11 @@ GetOptions(
 	"only-i915|only_i915" => \$only_i915,
 	"only-drm|only_drm" => \$only_drm,
 	"func-filters|f=s" => \$func_filters,
+	"include-func=s" => \@func_regexes,
+	"exclude-func=s" => \@func_exclude_regexes,
 	"source-filters|S=s" => \$src_filters,
+	"include-source=s" => \@src_regexes,
+	"exclude-source=s" => \@src_exclude_regexes,
 	"show-files|show_files" => \$show_files,
 	"show-lines|show_lines" => \$show_lines,
 	"report|r=s" => \$gen_report,
@@ -838,16 +838,31 @@ pod2usage(1) if ($gen_report && ($print_used || $filter || $stat || $print_unuse
 
 my $filter_str = "";
 my $has_filter;
+my $str;
 
-if ($func_filters) {
-	my $str = open_filter_file($func_filters, \@func_regexes, \@func_exclude_regexes);
+if ($only_i915) {
+	# Please keep in sync with the documentation
+	push @src_exclude_regexes, "selftest";
+	push @src_regexes, "drm/i915";
+	push @src_regexes, "drm/ttm";
+	push @src_regexes, "drm/vgem";
+}
+
+if ($only_drm) {
+	# Please keep in sync with the documentation
+	push @src_exclude_regexes, "trace.*\.h";
+	push @src_exclude_regexes, "drm.*\.h";
+}
+
+$str = open_filter_file($func_filters, \@func_regexes, \@func_exclude_regexes);
+if ($str) {
 	$filter_str .= "," if ($filter_str ne "");
 	$filter_str .= " function regex ($str)";
 	$has_filter = 1;
 }
 
-if ($src_filters) {
-	my $str = open_filter_file($src_filters, \@src_regexes, \@src_exclude_regexes);
+$str = open_filter_file($src_filters, \@src_regexes, \@src_exclude_regexes);
+if ($str) {
 	$filter_str .= "," if ($filter_str ne "");
 	$filter_str .= " source regex ($str)";
 	$has_filter = 1;
@@ -855,17 +870,6 @@ if ($src_filters) {
 
 $ignore_unused = 1 if (@func_regexes || @func_exclude_regexes);
 
-if ($only_i915) {
-	$filter_str = " ignored non-i915 files";
-	$has_filter = 1;
-}
-
-if ($only_drm) {
-	$filter_str .= "," if ($filter_str ne "");
-	$filter_str .= " ignored non-drm headers";
-	$has_filter = 1;
-}
-
 if ($ignore_unused) {
 	$filter_str .= "," if ($filter_str ne "");
 	$filter_str .= " ignored source files where none of its code ran";
@@ -1086,6 +1090,24 @@ expressions from the B<[filter's file]> will be ignored.
 Please notice that, when this filter is used, B<--ignore-unused> will be
 automaticaly enabled, as the final goal is to report per-function usage.
 
+=item B<--include-func> B<regex>
+
+Include B<regex> to the function filter. Can be used multiple times.
+
+When used together with B<--func-filters>, regexes here are handled first.
+
+Please notice that, when this filter is used, B<--ignore-unused> will be
+automaticaly enabled, as the final goal is to report per-function usage.
+
+=item B<--exclude-func> B<regex>
+
+Include B<regex> to the function filter. Can be used multiple times.
+
+When used together with B<--func-filters>, regexes here are handled first.
+
+Please notice that, when this filter is used, B<--ignore-unused> will be
+automaticaly enabled, as the final goal is to report per-function usage.
+
 =item B<--source-filters>  B<[filter's file]> or B<-S>  B<[filter's file]>
 
 Use a file containing regular expressions to filter source files.
@@ -1116,6 +1138,18 @@ When both include and exclude regexes are found, exclude regexes are
 applied first and any functions that don't match the include regular
 expressions from the B<[filter's file]> will be ignored.
 
+=item B<--include-src> B<regex>
+
+Include B<regex> to the sources filter. Can be used multiple times.
+
+When used together with B<--src-filters>, regexes here are handled first.
+
+=item B<--exclude-src> B<regex>
+
+Include B<regex> to the sources filter. Can be used multiple times.
+
+When used together with B<--src-filters>, regexes here are handled first.
+
 =item B<--ignore-unused> or B<--ignore_unused>
 
 Filters out unused C files and headers from the code coverage results.
#18 lore raw

PATCH v2 12/12 code_cov_parse_info: add support for generating html reports

In reply to Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

While lcov has already its own report generator, it is interesting to be able to deal with multiple files exposing each input in separate.

So, add new command line parameters to allow it to generate html reports. Also add some command lines to setup html title, add a css file and include a prolog/epilog at the html body.

The title option can also be useful to rename the titles when merging multiple info files.

---

To avoid mailbombing on a large number of people, only mailing lists were C/C on the cover. See [PATCH v2 00/12] at: https://lore.kernel.org/all/[email protected]/

 scripts/code_cov_parse_info | 442 +++++++++++++++++++++++++++++++-----
 1 file changed, 388 insertions(+), 54 deletions(-)
scripts/code_cov_parse_info +388 -54
index 7336f2be7978..d6ec5695a358 100755
@@ -9,6 +9,8 @@ use Pod::Man;
 
 my $prefix = qr ".*?(linux)\w*/";
 
+my $title = "";
+
 my %used_func;
 my %all_func;
 my %all_branch;
@@ -307,8 +309,12 @@ sub write_filtered_file($)
 
 	my $filtered = "";
 
-	foreach my $testname(sort keys %test_names) {
-		$filtered .= "TN:$testname\n";
+	if ($title eq "") {
+		foreach my $testname(sort keys %test_names) {
+			$filtered .= "TN:$testname\n";
+		}
+	} else {
+		$filtered .= "TN:$title\n";
 	}
 
 	# Generates filtered data
@@ -413,68 +419,80 @@ sub print_code_coverage($$$)
 	}
 }
 
-sub print_summary()
+my %stats;
+
+sub gen_stats()
 {
-	# Output per-line coverage statistics
-	my $line_count = 0;
-	my $line_reached = 0;
+	# per-line coverage statistics
+	$stats{"line_count"} = 0;
+	$stats{"line_reached"} = 0;
 
 	foreach my $source (keys(%all_line)) {
 		next if (!$used_source{$source});
 
 		foreach my $where (keys(%{$all_line{$source}})) {
-			$line_count++;
-			$line_reached++ if ($all_line{$source}{$where} != 0);
+			$stats{"line_count"}++;
+			$stats{"line_reached"}++ if ($all_line{$source}{$where} != 0);
 		}
 	}
-	if ($line_count) {
-		my $percent = 100. * $line_reached / $line_count;
-		printf "  lines......: %.1f%% (%d of %d lines)\n",
-			$percent, $line_reached, $line_count;
-	} else {
-		print "No line coverage data.\n";
-	}
 
-	# Output per-function coverage statistics
-	my $func_count = 0;
-	my $func_used = 0;
+	# per-function coverage statistics
+	$stats{"func_count"} = 0;
+	$stats{"func_used"} = 0;
 
 	foreach my $func (keys(%all_func)) {
 		foreach my $file (keys(%{$all_func{$func}})) {
-			$func_count++;
+			$stats{"func_count"}++;
 			if ($used_func{$func}) {
 				if ($used_func{$func}->{$file}) {
-					$func_used++;
+					$stats{"func_used"}++;
 				}
 			}
 		}
 	}
 
-	if ($func_count) {
-		my $percent = 100. * $func_used / $func_count;
+	# per-branch coverage statistics
+	$stats{"branch_count"} = 0;
+	$stats{"branch_reached"} = 0;
+
+	foreach my $source (keys(%all_branch)) {
+		next if (!$used_source{$source});
+
+		foreach my $where (keys(%{$all_branch{$source}})) {
+			$stats{"branch_count"}++;
+			$stats{"branch_reached"}++ if ($all_branch{$source}{$where} != 0);
+		}
+	}
+
+	# per-file coverage stats
+	$stats{"all_files"} = scalar keys(%files);
+	$stats{"filtered_files"} = scalar keys(%record);
+	$stats{"used_files"} = scalar keys(%used_source);
+}
+
+sub print_summary()
+{
+	if ($stats{"line_count"}) {
+		my $percent = 100. * $stats{"line_reached"} / $stats{"line_count"};
+		printf "  lines......: %.1f%% (%d of %d lines)\n",
+			$percent, $stats{"line_reached"}, $stats{"line_count"};
+	} else {
+		print "No line coverage data.\n";
+	}
+
+	if ($stats{"func_count"}) {
+		my $percent = 100. * $stats{"func_used"} / $stats{"func_count"};
 		printf "  functions..: %.1f%% (%d of %d functions)\n",
-			$percent, $func_used, $func_count;
+			$percent, $stats{"func_used"}, $stats{"func_count"};
 	} else {
 		print "No functions reported. Wrong filters?\n";
 		return;
 	}
 
-	# Output per-branch coverage statistics
-	my $branch_count = 0;
-	my $branch_reached = 0;
-
-	foreach my $source (keys(%all_branch)) {
-		next if (!$used_source{$source});
-
-		foreach my $where (keys(%{$all_branch{$source}})) {
-			$branch_count++;
-			$branch_reached++ if ($all_branch{$source}{$where} != 0);
-		}
-	}
-	if ($branch_count) {
-		my $percent = 100. * $branch_reached / $branch_count;
+	if ($stats{"branch_count"}) {
+		my $percent = 100. * $stats{"branch_reached"} / $stats{"branch_count"};
 		printf "  branches...: %.1f%% (%d of %d branches)\n",
-			$percent, $branch_reached, $branch_count;
+			$percent, $stats{"branch_reached"}, $stats{"branch_count"};
 	} else {
 		print "No branch coverage data.\n";
 	}
@@ -520,6 +538,254 @@ sub open_filter_file($$$)
 	return $filter;
 }
 
+my $gen_report;
+my $css_file;
+my $html_prolog;
+my $html_epilog;
+my %report;
+
+sub generate_report()
+{
+	my $percent;
+	my $prolog = "";
+	my $epilog = "";
+	my @info_files = sort(keys %report);
+
+	$title = "Code coverage results" if ($title eq "");
+
+	if ($html_prolog) {
+		open IN, $html_prolog or die "Can't open prolog file";
+		$prolog .= $_ while (<IN>);
+		close IN;
+	}
+
+	if ($html_epilog) {
+		open IN, $html_epilog or die "Can't open epilog file";
+		$epilog .= $_ while (<IN>);
+		close IN;
+	}
+
+	# Re-generate the hashes used to report stats in order to procuce the
+	# Total results
+
+	%used_func = ();
+	%all_func = ();
+	%all_branch = ();
+	%all_line = ();
+	%used_source = ();
+	%files = ();
+	%test_names = ();
+
+	foreach my $f (@info_files) {
+		foreach my $source (keys(%{$report{$f}{"all_line"}})) {
+			$used_source{$source} = 1 if ($report{$f}{"used_source"});
+			foreach my $where (keys(%{$report{$f}{"all_line"}{$source}})) {
+				$all_line{$source}{$where} += $report{$f}{"all_line"}{$source}{$where};
+			}
+		}
+		foreach my $func (keys(%{$report{$f}{"all_func"}})) {
+			foreach my $file (keys(%{$report{$f}{"all_func"}{$func}})) {
+				$all_func{$func}{$file}->{ln} = $report{$f}{"all_func"}{$func}{$file}->{ln};
+				$used_func{$func}->{$file} = 1 if ($report{$f}{"used_func"}{$func}->{$file});
+			}
+		}
+		foreach my $source (keys(%{$report{$f}{"all_branch"}})) {
+			foreach my $where (keys(%{$report{$f}{"all_branch"}{$source}})) {
+				$all_branch{$source}{"$where"} += $report{$f}{"all_branch"}{$source}{$where};
+			}
+		}
+		for my $source(keys(%{$report{$f}{"files"}})) {
+			$files{$source} = 1;
+			$used_source{$source} = 1 if ($report{$f}{"used_source"}{$source});
+		}
+		for my $test(keys(%{$report{$f}{"test_names"}})) {
+			$test_names{$test} = 1;
+		}
+	}
+	gen_stats();
+
+	# Colors for the html output
+
+	my $red    = "style=\"background-color:#ffb3b3\"";
+	my $yellow = "style=\"background-color:#ffffb3\"";
+	my $green  = "style=\"background-color:#d9ffd9\"";
+
+	# Open report file
+
+	open OUT, ">$gen_report" or die "Can't open $gen_report";
+
+	print OUT "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
+
+	print OUT "<html lang=\"en\">\n\n";
+	print OUT "<head>\n";
+	print OUT "  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n";
+	print OUT "  <title>$title</title>\n";
+	print OUT "  <link rel=\"stylesheet\" type=\"text/css\" href=\"$css_file\">\n" if ($css_file);
+	print OUT "</head>\n\n<body>\n$prolog";
+
+	print OUT "  <h1>$title</h1>\n";
+
+	print OUT "  <h2>Summary</h2>\n";
+	# Generates a table containing the code coverage statistics per input
+
+	print OUT "<table width=\"100%\" border=1 cellspacing=0 cellpadding=0>\n  <tr>\n";
+	print OUT "    <th></th>\n";
+	foreach my $f (@info_files) {
+		print OUT "    <th>$f</th>\n";
+	}
+	print OUT "    <th>TOTAL</th>\n";
+	print OUT "    <th>Total count</th>\n";
+	print OUT "  </tr><tr>\n";
+
+	print OUT "    <td><b>Functions</b></td>\n";
+	foreach my $f (@info_files) {
+		my %st = %{$report{$f}{"stats"}};
+		if ($st{"func_count"}) {
+			$percent = 100. * $st{"func_used"} / $st{"func_count"};
+
+			printf OUT "    <td>%.1f%%</td>\n", $percent;
+		} else {
+			print OUT "    <td>N. A.</td>\n";
+		}
+	}
+	if ($stats{"func_count"}) {
+		$percent = 100. * $stats{"func_used"} / $stats{"func_count"};
+
+		printf OUT "    <td>%.1f%%</td>\n", $percent;
+	} else {
+		print OUT "    <td>N. A.</td>\n";
+	}
+	print OUT "  <td>" . $stats{"func_count"} . "</td>";
+	print OUT "  </tr><tr>\n";
+
+	print OUT "    <td><b>Branches</b></td>\n";
+	foreach my $f (@info_files) {
+		my %st = %{$report{$f}{"stats"}};
+		if ($st{"branch_count"}) {
+			$percent = 100. * $st{"branch_reached"} / $st{"branch_count"};
+
+			printf OUT "    <td>%.1f%%</td>\n", $percent;
+		} else {
+			print OUT "    <td>N. A.</td>\n";
+		}
+	}
+	if ($stats{"branch_count"}) {
+		$percent = 100. * $stats{"branch_reached"} / $stats{"branch_count"};
+
+		printf OUT "    <td>%.1f%%</td>\n", $percent;
+	} else {
+		print OUT "    <td>N. A.</td>\n";
+	}
+	print OUT "  <td>" . $stats{"branch_count"} . "</td>";
+	print OUT "  </tr><tr>\n";
+
+	print OUT "    <td><b>Lines</b></td>\n";
+	foreach my $f (@info_files) {
+		my %st = %{$report{$f}{"stats"}};
+
+		if ($st{"line_count"}) {
+			$percent = 100. * $st{"line_reached"} / $st{"line_count"};
+
+			printf OUT "    <td>%.1f%%</td>\n", $percent;
+		} else {
+			print OUT "    <td>N. A.</td>\n";
+		}
+	}
+	if ($stats{"line_count"}) {
+		$percent = 100. * $stats{"line_reached"} / $stats{"line_count"};
+
+		printf OUT "    <td>%.1f%%</td>\n", $percent;
+	} else {
+		print OUT "    <td>N. A.</td>\n";
+	}
+	print OUT "  <td>" . $stats{"line_count"} . "</td>";
+
+	# If there are more than one tests per file, report them
+	my $total = scalar(keys %test_names);
+	if ($total > 1) {
+		print OUT "  </tr><tr>\n";
+		print OUT "    <td><b>Number of tests</b></td>\n";
+		foreach my $f (@info_files) {
+			my $count = scalar(keys %{$report{$f}{"test_names"}});
+
+			if ($count == 0) {
+				print OUT "    <td $red>$count</td>\n";
+			} elsif ($count < $total) {
+				print OUT "    <td $yellow>$count</td>\n";
+			} else {
+				print OUT "    <td $green>$count</td>\n";
+			}
+		}
+		print OUT "    <td $green\>$total</td>\n";
+
+	}
+	print OUT "  </tr>\n</table><p/>\n\n";
+
+	if ($total > 1) {
+		print OUT "<h2>Tests coverage</h2>\n";
+
+		print OUT "<table width=\"100%\" border=1 cellspacing=0 cellpadding=0>\n  <tr>\n";
+		print OUT "    <th>Test name</th>\n";
+		foreach my $f (@info_files) {
+			print OUT "    <th>$f</th>\n";
+		}
+
+		foreach my $t (sort keys(%test_names)) {
+			print OUT "  </tr><tr>\n";
+			printf OUT "    <td>%s</td>\n", $t;
+			foreach my $f (@info_files) {
+				if (%{$report{$f}{"test_names"}}{$t}) {
+					print OUT "    <td $green>YES</td>\n";
+				} else {
+					print OUT "    <td $red>NO</td>\n";
+				}
+			}
+		}
+		print OUT "</tr></table>\n";
+	}
+
+
+	# Generates a table containing per-function detailed data
+
+	print OUT "<h2>Functions coverage</h2>\n";
+	print OUT "<table width=\"100%\" border=1 cellspacing=0 cellpadding=0>\n  <tr>\n";
+	print OUT "    <th>Function</th>\n";
+	print OUT "    <th>Used?</th>\n";
+	foreach my $f (@info_files) {
+		print OUT "    <th>$f</th>\n";
+	}
+	print OUT "    <th>File</th>\n";
+
+	foreach my $func (sort keys(%all_func)) {
+		my @keys = sort keys(%{$all_func{$func}});
+		foreach my $file (@keys) {
+			print OUT "  </tr><tr>\n";
+			print OUT "    <td>$func</td>\n";
+			if ($used_func{$func}->{$file}) {
+				print OUT "    <td $green>YES</td>\n";
+			} else {
+				print OUT "    <td $red>NO</td>\n";
+			}
+			foreach my $f (@info_files) {
+				if ($report{$f}{"used_func"}{$func}->{$file}) {
+					print OUT "    <td $green>YES</td>\n";
+				} else {
+					print OUT "    <td $red>NO</td>\n";
+				}
+			}
+			$file =~ s,$prefix,linux/,;
+			print OUT "    <td>$file</td>\n";
+		}
+	}
+	print OUT "</tr></table>\n";
+
+	print OUT "$epilog</body>\n";
+
+	# Close the file and exit
+
+	close OUT;
+}
+
 #
 # Argument handling
 #
@@ -548,6 +814,11 @@ GetOptions(
 	"source-filters|S=s" => \$src_filters,
 	"show-files|show_files" => \$show_files,
 	"show-lines|show_lines" => \$show_lines,
+	"report|r=s" => \$gen_report,
+	"css-file|css|c=s" => \$css_file,
+	"title|t=s" => \$title,
+	"html-prolog|prolog=s" => \$html_prolog,
+	"html-epilog|epilog=s" => \$html_epilog,
 	"help" => \$help,
 	"man" => \$man,
 ) or pod2usage(2);
@@ -561,7 +832,9 @@ if ($#ARGV < 0) {
 }
 
 # At least one action should be specified
-pod2usage(1) if (!$print_used && !$filter && !$stat && !$print_unused);
+pod2usage(1) if (!$print_used && !$filter && !$stat && !$print_unused && !$gen_report);
+
+pod2usage(1) if ($gen_report && ($print_used || $filter || $stat || $print_unused));
 
 my $filter_str = "";
 my $has_filter;
@@ -601,39 +874,67 @@ if ($ignore_unused) {
 
 foreach my $f (@ARGV) {
 	parse_info_data($f);
+
+	if ($gen_report) {
+		$f =~ s,.*/,,;
+		$f =~ s/\.info$//;
+
+		gen_stats();
+
+		$report{$f}{"stats"} = { %stats };
+		$report{$f}{"all_func"} = { %all_func };
+		$report{$f}{"used_func"} = { %used_func };
+		$report{$f}{"all_branch"} = { %all_branch };
+		$report{$f}{"all_line"} = { %all_line };
+		$report{$f}{"used_source"} = { %used_source };
+		$report{$f}{"files"} = { %files };
+		$report{$f}{"test_names"} = { %test_names };
+
+		%used_func = ();
+		%all_func = ();
+		%all_branch = ();
+		%all_line = ();
+		%used_source = ();
+		%files = ();
+		%test_names = ();
+	}
 }
 
+if ($gen_report) {
+	generate_report();
+	exit 0;
+}
+
+gen_stats();
+
+die "Nothing counted. Wrong input files?" if (!$stats{"all_files"});
+
 print_code_coverage($print_used, $print_unused, $show_lines);
 
 print_summary() if ($stat);
 
-my $all_files = scalar keys(%files);
-
-die "Nothing counted. Wrong input files?" if (!$all_files);
-
 if ($has_filter) {
-	my $all_files = scalar keys(%files);
-	my $filtered_files = scalar keys(%record);
-	my $used_files = scalar keys(%used_source);
-
-	my $percent = 100. * $used_files / $all_files;
+	my $percent = 100. * $stats{"used_files"} / $stats{"all_files"};
 
 	$filter_str =~ s/(.*),/$1 and/;
 	printf "Filters......:%s.\n", $filter_str;
 	printf "Source files.: %.2f%% (%d of %d total)",
-		$percent, $used_files, $all_files;
+		$percent, $stats{"used_files"}, $stats{"all_files"};
 
-	if ($used_files != $filtered_files) {
-		my $percent_filtered = 100. * $used_files / $filtered_files;
+	if ($stats{"used_files"} != $stats{"filtered_files"}) {
+		my $percent_filtered = 100. * $stats{"used_files"} / $stats{"filtered_files"};
 
 		printf ", %.2f%% (%d of %d filtered)",
-			$percent_filtered, $used_files, $filtered_files;
+			$percent_filtered, $stats{"used_files"}, $stats{"filtered_files"};
 	}
 	print "\n";
 } else {
 	printf "Source files: %d\n", scalar keys(%files) if($stat);
 }
 
+my $ntests=scalar(%test_names);
+printf "Number of tests: %d\n", $ntests if ($ntests > 1);
+
 if ($show_files) {
 	for my $f(sort keys %used_source) {
 		print "\t$f\n";
@@ -654,8 +955,10 @@ Parses lcov data from .info files.
 
 code_cov_parse_info <options> [input file(s)]
 
-At least one of the options B<--stat>, B<--print> and/or B<--output>
-should be used.
+At least one of the output options should be used, e g.
+B<--stat>, B<--print>, B<--print-unused>, B<--report> and/or B<--output>.
+
+Also, B<--report> can't be used together with other output options.
 
 =head1 OPTIONS
 
@@ -684,6 +987,37 @@ Prints the functions that were never reached.
 
 The function coverage report is affected by the applied filters.
 
+=item B<--report>  B<[output file]> or B<-r>  B<[output file]>
+
+Generates an html report containing per-test and total statistics.
+
+The function coverage report is affected by the applied filters.
+
+=item B<--css-file> B<[css file]> or B<--css> B<[css file]> or B<-c> B<[css file]
+
+Adds an optional css file to the html report.
+Used only with B<--report>.
+
+=item B<--title> B<[title] or B<-t> B<[title]
+
+If used with B<--report>, it defines the title for the for the html report.
+
+If used with B<--output>, it replaces the test names with the title. This
+is useful when merging reports from multiple tests into a summarized file.
+If not used, the B<[output file]> will contain all test names on its
+beginning.
+
+Used with B<--report> AND B<--output>.
+
+=item B<--html-prolog> B<[html file] or B<--prolog> B<[html file]
+
+Adds a prolog at the beginning of the body of the html report.
+Used only with B<--report>.
+
+=item B<--html-epilog>  B<[html file] or B<--epilog>  B<[html file]
+
+Adds an epilog before the end of the body of the html report.
+Used only with B<--report>.
 
 =item B<--show-lines> or B<--show_lines>
 
Signature · 1 line
2.35.1
Acked-by on v2 12/12
#19 lore raw

Re: [igt-dev] [PATCH v2 12/12] code_cov_parse_info: add support for generating html reports

Quoted 10 lines · Mauro Carvalho Chehab

From: Mauro Carvalho Chehab <[email protected]>

While lcov has already its own report generator, it is interesting to be able to deal with multiple files exposing each input in separate.

So, add new command line parameters to allow it to generate html reports. Also add some command lines to setup html title, add a css file and include a prolog/epilog at the html body.

Quoted 6 lines

The title option can also be useful to rename the titles when merging multiple info files.

---

I wouldn't say I've enough patience to analyze everything :), so:

Regards
Andrzej

#20 lore raw

[igt-dev] ✗ GitLab.Pipeline: warning for code coverage: some improvements (rev2)

In reply to Mauro Carvalho Chehab
== Series Details ==

== Series Details ==

Series: code coverage: some improvements (rev2)
URL : https://patchwork.freedesktop.org/series/102136/
State : warning

== Summary ==

Pipeline status: FAILED.

see https://gitlab.freedesktop.org/gfx-ci/igt-ci-tags/-/pipelines/557025 for the overview.

test:ninja-test-arm64 has failed (https://gitlab.freedesktop.org/gfx-ci/igt-ci-tags/-/jobs/21074964):
Ok: 22
Expected Fail: 3
Fail: 289

  Unexpected Pass:       0
  Skipped:               0
  Timeout:               0

Full log written to /builds/gfx-ci/igt-ci-tags/build/meson-logs/testlog.txt section_end:1649756245:step_script
section_start:1649756245:upload_artifacts_on_failure
Uploading artifacts for failed job
Uploading artifacts...
build: found 1725 matching files and directories
Uploading artifacts as "archive" to coordinator... 201 Created id=21074964 responseStatus=201 Created token=xP6DcMSB section_end:1649756256:upload_artifacts_on_failure
section_start:1649756256:cleanup_file_variables
Cleaning up project directory and file based variables section_end:1649756256:cleanup_file_variables
ERROR: Job failed: exit code 1

== Logs ==

For more details see: https://gitlab.freedesktop.org/gfx-ci/igt-ci-tags/-/pipelines/557025

#21 lore raw

[igt-dev] ✗ Fi.CI.BAT: failure for code coverage: some improvements (rev2)

In reply to Mauro Carvalho Chehab
== Series Details ==

== Series Details ==

Series: code coverage: some improvements (rev2)
URL : https://patchwork.freedesktop.org/series/102136/
State : failure

== Summary ==

CI Bug Log - changes from CI_DRM_11487 -> IGTPW_6918
====================================================

Summary
-------

**FAILURE**

Serious unknown changes coming with IGTPW_6918 absolutely need to be verified manually.

If you think the reported changes have nothing to do with the changes introduced in IGTPW_6918, please notify your bug team to allow them to document this new failure mode, which will reduce false positives in CI.

External URL: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/index.html

Participating hosts (37 -> 36)
------------------------------

Additional (2): fi-kbl-soraka fi-pnv-d510
Missing (3): fi-bsw-cyan fi-icl-u2 fi-bdw-samus

Possible new issues
-------------------

Here are the unknown changes that may have been introduced in IGTPW_6918:

### IGT changes ###

#### Possible regressions ####

* igt@i915_selftest@live@execlists:
- fi-kbl-soraka: NOTRUN -> [INCOMPLETE][1]
[1]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/fi-kbl-soraka/igt@i915_selftest@[email protected]

Known issues
------------

Here are the changes found in IGTPW_6918 that come from known issues:

### IGT changes ###

#### Issues hit ####

* igt@gem_exec_fence@basic-busy@bcs0:
- fi-kbl-soraka: NOTRUN -> [SKIP][2] ([fdo#109271]) +9 similar issues
[2]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/fi-kbl-soraka/igt@gem_exec_fence@[email protected]

* igt@gem_huc_copy@huc-copy:
- fi-kbl-soraka: NOTRUN -> [SKIP][3] ([fdo#109271] / [i915#2190])
[3]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/fi-kbl-soraka/igt@[email protected]

* igt@gem_lmem_swapping@basic:
- fi-kbl-soraka: NOTRUN -> [SKIP][4] ([fdo#109271] / [i915#4613]) +3 similar issues
[4]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/fi-kbl-soraka/igt@[email protected]

* igt@i915_selftest@live@gt_pm:
- fi-kbl-soraka: NOTRUN -> [DMESG-FAIL][5] ([i915#1886])
[5]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/fi-kbl-soraka/igt@i915_selftest@live@gt_pm.html

* igt@kms_chamelium@common-hpd-after-suspend:
- fi-kbl-soraka: NOTRUN -> [SKIP][6] ([fdo#109271] / [fdo#111827]) +8 similar issues
[6]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/fi-kbl-soraka/igt@[email protected]

* igt@kms_pipe_crc_basic@compare-crc-sanitycheck-pipe-c:
- fi-pnv-d510: NOTRUN -> [SKIP][7] ([fdo#109271] / [i915#5341])
[7]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/fi-pnv-d510/igt@[email protected]

* igt@kms_pipe_crc_basic@compare-crc-sanitycheck-pipe-d:
- fi-kbl-soraka: NOTRUN -> [SKIP][8] ([fdo#109271] / [i915#533])
[8]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/fi-kbl-soraka/igt@[email protected]

* igt@prime_vgem@basic-userptr:
- fi-pnv-d510: NOTRUN -> [SKIP][9] ([fdo#109271]) +57 similar issues
[9]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/fi-pnv-d510/igt@[email protected]

* igt@runner@aborted:
- fi-bdw-5557u: NOTRUN -> [FAIL][10] ([i915#4312])
[10]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/fi-bdw-5557u/igt@[email protected]

#### Possible fixes ####

* igt@i915_selftest@live@gt_pm:
- fi-tgl-1115g4: [DMESG-FAIL][11] ([i915#3987]) -> [PASS][12]
[11]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11487/fi-tgl-1115g4/igt@i915_selftest@live@gt_pm.html
[12]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/fi-tgl-1115g4/igt@i915_selftest@live@gt_pm.html

* igt@kms_busy@basic@flip:
- fi-tgl-u2: [DMESG-WARN][13] ([i915#402]) -> [PASS][14]
[13]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11487/fi-tgl-u2/igt@kms_busy@[email protected]
[14]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/fi-tgl-u2/igt@kms_busy@[email protected]

{name}: This element is suppressed. This means it is ignored when computing
the status of the difference (SUCCESS, WARNING, or FAILURE).

[fdo#109271]: https://bugs.freedesktop.org/show_bug.cgi?id=109271
[fdo#111827]: https://bugs.freedesktop.org/show_bug.cgi?id=111827
[i915#1886]: https://gitlab.freedesktop.org/drm/intel/issues/1886
[i915#2190]: https://gitlab.freedesktop.org/drm/intel/issues/2190
[i915#3987]: https://gitlab.freedesktop.org/drm/intel/issues/3987
[i915#402]: https://gitlab.freedesktop.org/drm/intel/issues/402
[i915#4312]: https://gitlab.freedesktop.org/drm/intel/issues/4312
[i915#4613]: https://gitlab.freedesktop.org/drm/intel/issues/4613
[i915#5134]: https://gitlab.freedesktop.org/drm/intel/issues/5134
[i915#533]: https://gitlab.freedesktop.org/drm/intel/issues/533
[i915#5341]: https://gitlab.freedesktop.org/drm/intel/issues/5341

Build changes
-------------

* CI: CI-20190529 -> None
* IGT: IGT_6420 -> IGTPW_6918

CI-20190529: 20190529
CI_DRM_11487: 8eab0595435f239d64b974841a46fad5c6e0c528 @ git://anongit.freedesktop.org/gfx-ci/linux
IGTPW_6918: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/index.html
IGT_6420: a3885810ccc0ce9e6552a20c910a0a322eca466c @ https://gitlab.freedesktop.org/drm/igt-gpu-tools.git

== Logs ==

For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_6918/index.html

#22 lore raw

[igt-dev] ✗ Fi.CI.BUILD: failure for code coverage: some improvements (rev3)

In reply to Mauro Carvalho Chehab
== Series Details ==

== Series Details ==

Series: code coverage: some improvements (rev3)
URL : https://patchwork.freedesktop.org/series/102136/
State : failure

== Summary ==

Applying: scripts/code_cov*: remove the extensions from them
Applying: scripts/code_cov_parse_info: add a tool to parse code coverage info files
Applying: scripts/code_cov_gen_report: add support for filtering info files
Applying: runner: execute code coverage script also from PATH
Applying: scripts/meson.build: install code coverage scripts
Applying: scripts/code_cov_selftest.sh: test if IGT code coverage is working
Applying: docs/code_coverage.md: document the code coverage filter script
Applying: scripts/code_cov_parse_info: better handle test name
Applying: code_cov_parse_info: fix error handling when opening files
Applying: code_cov_parse_info: fix --show-lines logic
Applying: code_cov_parse_info: add support for exclude filters
Using index info to reconstruct a base tree...
M scripts/code_cov_parse_info
Falling back to patching base and 3-way merge...
Auto-merging scripts/code_cov_parse_info
CONFLICT (content): Merge conflict in scripts/code_cov_parse_info Patch failed at 0011 code_cov_parse_info: add support for exclude filters When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".