Merged
fix(build): Flush gcov data for pytest-forked subprocess coverage #6148
chengding888Add on Sep 10
fix(build): Flush gcov data for pytest-forked subprocess coverage #6148
Merged
chengding888Add on Sep 10
chengding888
chengding888Collaborators
Sep 10

背景

Python UT 通过 --forked(pytest-forked)并行执行用例,子进程以 os._exit() 退出,会跳过 atexit 钩子,导致 gcov 的 __gcov_exit 不被调用、.gcda 覆盖率数据不落盘。因此 --gcov 无法统计到由 Python UT 触发的 C++ 覆盖率(issue #2705)。

解决方案

  1. python/tests/ut/conftest.py:新增 pytest_runtest_teardown 钩子,当环境变量 PYPTO_GCOV_FLUSH=1 时手动调用 __gcov_dump(),把内存中的计数器写入 .gcda;并补充 duration_estimate 装饰器。

  2. build_ci.py:--gcov 时注入 PYPTO_GCOV_FLUSH=1。

  3. cmake/intf.cmake:gcov 模式追加链接选项 -Wl,--export-dynamic,-u,__gcov_dump,--whole-archive,-lgcov,--no-whole-archive。仅加 -u,__gcov_dump 时 _gcov_dump.o 不会被 lgcov.a 拉入,__gcov_dump 保持未定义,ctypes.CDLL(None) 找不到该符号,必须用 --whole-archive 强制拉入。

  4. cmake/scripts/gen_coverage.py:lcov >= 2.0 时追加 --ignore-errors inconsistent,将 lcov 2.3.1 对 pybind11.cpp 的 mismatched end line 由 ERROR 降级为 WARNING,避免覆盖率生成失败。

  5. tools/scripts/pre_commit_check.sh:

    • 修正覆盖率开关(--py_cov → --gcov)、clang 失败退出码、GTEST_FILTER 保存/恢复、覆盖率标签等;
    • 移除本地增量覆盖率门禁:本地 gen_coverage.py --increment 与 CI 的 get_ai_inc_cov.py(addlcov) 是两套独立实现,增量覆盖率无法保证与 CI 一致,门禁统一由 CI 兜底。

验证

  • bash tools/scripts/pre_commit_check.sh --full 全部通过(gnu / clang+ASan / Python / costmodel 仿真,耗时约 4779s)。
  • 确认 Python UT 触发场景下 C++ 覆盖率数据(.gcda)可正常落盘并被 --gcov 统计。

Related Issue: #2705

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for chengding888's contribution.)
chengding888chengding888Collaborators
Sep 10 Created pull request, commit 9d6519bb
atomgit-bot
atomgit-bot
Sep 10 Comments:

变更摘要

该 PR 旨在修复 pytest-forked 子进程模式下 gcov 覆盖率数据无法落盘的问题:--forked 模式下子进程通过 os._exit() 退出,会跳过 atexit,导致 gcov 的 __gcov_exit 不被调用、.gcda 文件不写入。改动通过在构建期导出 __gcov_dump 符号、在测试环境注入 PYPTO_GCOV_FLUSH 开关,并在 UT 的 pytest_runtest_teardown 钩子中手动调用 __gcov_dump() 将内存中的覆盖率计数器刷写为 .gcda 文件。

主要改动

  • 构建脚本注入 gcov flush 开关:build_ci.py 的 BuildCtrl 在构建启用 gcov(self.build.gcov)时,向测试进程环境变量写入 PYPTO_GCOV_FLUSH=1,作为子进程手动 flush 覆盖率数据的开关。
  • 链接选项导出 __gcov_dump 符号:cmake/intf.cmake 在启用 ENABLE_GCOV 且使用 GNU 编译器时,为 tile_fwk_intf_pub_base 增加 --export-dynamic,-u,__gcov_dump 等链接选项,导出 __gcov_dump 符号供测试子进程调用。
  • 新增 UT conftest 实现手动 flush:新增 python/tests/ut/conftest.py,在 pytest_runtest_teardown 中通过 ctypes.CDLL(None) 解析并调用 __gcov_dump();仅当环境变量 PYPTO_GCOV_FLUSH 存在时才执行,从而在 os._exit() 前补齐 gcov 覆盖率数据写入。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Sep 10 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Sep 10 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 10 Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
build_ci.py ✅ 尹杰, lululu40888 (2/2) ✅ 尹杰 (1/1)
cmake ✅ lululu40888, 尹杰 (2/2) ✅ 尹杰 (1/1)
repo-cann/pypto ✅ tsungl4, 尹杰, lululu40888 (3/2) ✅ tsungl4, 尹杰 (2/1)
tools/scripts ✅ tsungl4, 尹杰, lululu40888 (3/2) ✅ tsungl4, 尹杰 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

chengding888, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
91 messages are collapsed here。 See more
huanglu
huangluCollaborators
Sep 14 Comments:

/lgtm

likedislike
lidongsheng
lidongshengCollaborators
Sep 14 Comments:

/approve

likedislike
Jie Yin
Jie YinCollaborators
Sep 15 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Sep 15 Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
Sep 15 Merged pull request