Skip to content

Commit ccd2c5c

Browse files
committed
Implement lcov coverage collection for libvirt
Signed-off-by: Yingshun Cui <yicui@redhat.com>
1 parent 0ac71ee commit ccd2c5c

3 files changed

Lines changed: 315 additions & 33 deletions

File tree

virttest/env_process.py

Lines changed: 85 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from avocado.core import exceptions
1717
from avocado.utils import archive
1818
from avocado.utils import cpu as cpu_utils
19-
from avocado.utils import crypto
19+
from avocado.utils import crypto, path
2020
from avocado.utils import process as a_process
2121
from six.moves import xrange
2222

@@ -43,7 +43,11 @@
4343
from virttest._wrappers import lazy_import
4444
from virttest.test_setup.aexpect import KillTailThreads
4545
from virttest.test_setup.core import SetupManager
46-
from virttest.test_setup.gcov import ResetQemuGCov
46+
from virttest.test_setup.gcov import (
47+
ResetGCov,
48+
collect_html_coverage,
49+
collect_lcov_coverage,
50+
)
4751
from virttest.test_setup.kernel import KSMSetup, ReloadKVMModules
4852
from virttest.test_setup.libvirt_setup import LibvirtdDebugLogConfig
4953
from virttest.test_setup.memory import HugePagesSetup, TransparentHugePagesSetup
@@ -1000,7 +1004,7 @@ def preprocess(test, params, env):
10001004
# and last running during pre/postprocess. That way vms will be actually
10011005
# off to ensure data is written to disk.
10021006
_setup_manager.register(ProcessVMOff)
1003-
_setup_manager.register(ResetQemuGCov)
1007+
_setup_manager.register(ResetGCov)
10041008
_setup_manager.register(VerifyHostDMesg)
10051009
_setup_manager.register(SwitchSMTOff)
10061010
_setup_manager.register(CheckRunningAsRoot)
@@ -1319,32 +1323,85 @@ def postprocess(test, params, env):
13191323

13201324
# Collect code coverage report for qemu if enabled
13211325
if params.get("gcov_qemu", "no") == "yes":
1322-
qemu_builddir = os.path.join(test.bindir, "build", "qemu")
1323-
if os.path.isdir(qemu_builddir) and utils_package.package_install("gcovr"):
1324-
gcov_qemu_dir = utils_misc.get_path(test.debugdir, "gcov_qemu")
1325-
os.makedirs(gcov_qemu_dir)
1326-
os.chdir(qemu_builddir)
1327-
collect_cmd_opts = params.get("gcov_qemu_collect_cmd_opts", "--html")
1328-
online_count = (
1329-
cpu_utils.online_count()
1330-
if hasattr(cpu_utils, "online_count")
1331-
else cpu_utils.online_cpus_count()
1332-
)
1333-
collect_cmd = "gcovr -j %s -o %s -s %s ." % (
1334-
online_count,
1335-
os.path.join(gcov_qemu_dir, "gcov.html"),
1336-
collect_cmd_opts,
1337-
)
1338-
a_process.system(collect_cmd, shell=True)
1339-
if params.get("gcov_qemu_compress", "no") == "yes":
1340-
os.chdir(test.debugdir)
1341-
archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir)
1342-
shutil.rmtree(gcov_qemu_dir, ignore_errors=True)
1326+
qemu_builddir = params.get(
1327+
"gcov_qemu_builddir", os.path.join(test.bindir, "build", "qemu")
1328+
)
1329+
gcov_format = params.get("gcov_qemu_format", "html")
1330+
test_name = params.get("shortname", getattr(test, "name", "unknown_test"))
1331+
if hasattr(test_name, "uid"):
1332+
test_name = str(test_name.uid)
1333+
1334+
if gcov_format == "lcov":
1335+
try:
1336+
path.find_command("lcov")
1337+
except path.CmdNotFoundError:
1338+
LOG.warning("lcov package not installed, cannot collect QEMU coverage")
1339+
else:
1340+
gcov_qemu_dir = utils_misc.get_path(test.debugdir, "gcov_qemu")
1341+
collect_lcov_coverage(qemu_builddir, gcov_qemu_dir, test_name, "qemu")
1342+
1343+
if params.get("gcov_qemu_compress", "no") == "yes":
1344+
os.chdir(test.debugdir)
1345+
archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir)
1346+
shutil.rmtree(gcov_qemu_dir, ignore_errors=True)
13431347
else:
1344-
LOG.warning(
1345-
"Check either qemu build directory availablilty"
1346-
" or install gcovr package for qemu coverage report"
1347-
)
1348+
if utils_package.package_install("gcovr"):
1349+
gcov_qemu_dir = utils_misc.get_path(test.debugdir, "gcov_qemu")
1350+
collect_cmd_opts = params.get("gcov_qemu_collect_cmd_opts", "--html")
1351+
collect_html_coverage(
1352+
qemu_builddir, gcov_qemu_dir, "qemu", collect_cmd_opts
1353+
)
1354+
1355+
if params.get("gcov_qemu_compress", "no") == "yes":
1356+
os.chdir(test.debugdir)
1357+
archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir)
1358+
shutil.rmtree(gcov_qemu_dir, ignore_errors=True)
1359+
else:
1360+
LOG.warning("gcovr package not installed, cannot collect QEMU coverage")
1361+
1362+
# Collect code coverage report for libvirt if enabled
1363+
if params.get("gcov_libvirt", "no") == "yes":
1364+
libvirt_builddir = params.get("gcov_libvirt_builddir", "/var/tmp/libvirt")
1365+
gcov_format = params.get("gcov_libvirt_format", "html")
1366+
# FIXME: update to a proper testname
1367+
test_name = params.get("shortname", getattr(test, "name", "unknown_test"))
1368+
if hasattr(test_name, "uid"):
1369+
test_name = str(test_name.uid)
1370+
1371+
if gcov_format == "lcov":
1372+
try:
1373+
path.find_command("lcov")
1374+
except path.CmdNotFoundError:
1375+
LOG.warning(
1376+
"lcov package not installed, cannot collect libvirt coverage"
1377+
)
1378+
else:
1379+
gcov_libvirt_dir = utils_misc.get_path(test.debugdir, "gcov_libvirt")
1380+
collect_lcov_coverage(
1381+
libvirt_builddir, gcov_libvirt_dir, test_name, "libvirt"
1382+
)
1383+
1384+
if params.get("gcov_libvirt_compress", "no") == "yes":
1385+
os.chdir(test.debugdir)
1386+
archive.compress("gcov_libvirt.tar.gz", gcov_libvirt_dir)
1387+
shutil.rmtree(gcov_libvirt_dir, ignore_errors=True)
1388+
else:
1389+
if utils_package.package_install("gcovr"):
1390+
gcov_libvirt_dir = utils_misc.get_path(test.debugdir, "gcov_libvirt")
1391+
collect_cmd_opts = params.get("gcov_libvirt_collect_cmd_opts", "--html")
1392+
collect_html_coverage(
1393+
libvirt_builddir, gcov_libvirt_dir, "libvirt", collect_cmd_opts
1394+
)
1395+
1396+
if params.get("gcov_libvirt_compress", "no") == "yes":
1397+
os.chdir(test.debugdir)
1398+
archive.compress("gcov_libvirt.tar.gz", gcov_libvirt_dir)
1399+
shutil.rmtree(gcov_libvirt_dir, ignore_errors=True)
1400+
else:
1401+
LOG.warning(
1402+
"gcovr package not installed, cannot collect libvirt coverage"
1403+
)
1404+
13481405
# Postprocess all VMs and images
13491406
try:
13501407
process(

virttest/shared/cfg/base.cfg

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,13 +904,18 @@ sysprep_options = "--operations machine-id"
904904

905905
# Enable Code Coverage report
906906
# prerequisite: qemu build test is run with --enable-gcov
907+
# or libvirt is built with --enable-coverage#
907908
# configure option and preserve_srcdir = yes
908909
# and qemu build dir is available
909910
# Enable/disable gcov for qemu, default: disable
910911
gcov_qemu = no
911912
# Enable/disable to reset the code coverage report
912913
# generated/collected by previous test
913914
gcov_qemu_reset = yes
915+
# Coverage output format: lcov (generates .info tracefiles) or html (generates HTML reports)
916+
# For per-test lcov tracefiles with --test-name, use "lcov"
917+
# For aggregated HTML reports with gcovr, use "html"
918+
gcov_qemu_format = html
914919
# Additional command options for gcovr
915920
# E:g:- "--html-details --html --exclude-directories=capstone"
916921
# above options will be required to collect detailed html
@@ -920,7 +925,28 @@ gcov_qemu_reset = yes
920925
gcov_qemu_collect_cmd_opts = "--html"
921926
# Enable/disable to compress code coverage report
922927
gcov_qemu_compress = no
928+
# Qemu build directory where .gcda files are generated
929+
gcov_qemu_builddir = /var/tmp/qemu
923930

931+
# Libvirt code coverage configuration
932+
# Enable/disable gcov for libvirt, default: disable
933+
gcov_libvirt = no
934+
# Enable/disable to reset the libvirt coverage report
935+
# generated/collected by previous test
936+
gcov_libvirt_reset = yes
937+
# Coverage output format: lcov or html
938+
gcov_libvirt_format = lcov
939+
# Libvirt build directory where .gcda files are generated
940+
gcov_libvirt_builddir = /var/tmp/libvirt
941+
# Additional command options for gcovr when format is html
942+
# E.g.: "--html-details --html --exclude-unreachable-branches"
943+
gcov_libvirt_collect_cmd_opts = "--html"
944+
# Enable/disable to compress libvirt code coverage report
945+
gcov_libvirt_compress = no
946+
# Libvirt daemon to restart after coverage reset (if needed)
947+
gcov_libvirt_daemon = libvirtd
948+
# Enable/disable restarting libvirt daemon after coverage reset
949+
gcov_libvirt_restart_daemon = no
924950
Linux:
925951
# param for installing stress tool from repo
926952
stress_install_from_repo = "no"

0 commit comments

Comments
 (0)