简体   繁体   English

将 valgrind 与“R CMD 检查”一起使用

[英]Use valgrind with `R CMD check`

I want to run valgrind on the tests, examples and vignettes of my package.我想在我的 package 的测试、示例和小插曲上运行 valgrind。 Various sources insinuate that the way to do this should be:各种消息来源暗示这样做的方法应该是:

R CMD build my-pkg
R CMD check --use-valgrind my-pkg_0.0.tar.gz

R CMD check seems to run fine, but shows no evidence of valgrind output, even after setting the environment variable VALGRIND_OPTS: --memcheck:leak-check=full . R CMD check似乎运行良好,但没有显示 valgrind output 的证据,即使在设置环境变量VALGRIND_OPTS: --memcheck:leak-check=full I've found sources that hint that R needs to run interactively for valgrind to show output, but R -d CMD check (or R -d "CMD check" ) seems to be the wrong format. I've found sources that hint that R needs to run interactively for valgrind to show output, but R -d CMD check (or R -d "CMD check" ) seems to be the wrong format.

R -d "valgrind --tool=memcheck --leak-check=full" --vanilla < my-pkg.Rcheck/my-pkg-Ex.R does work, but only on the example files; R -d "valgrind --tool=memcheck --leak-check=full" --vanilla < my-pkg.Rcheck/my-pkg-Ex.R确实有效,但仅适用于示例文件; I can't see a simple way to run this against my vignettes and testthat tests.我看不到一种简单的方法来针对我的小插曲运行它并测试测试。

What is the best way to run all relevant scripts through valgrind?通过 valgrind 运行所有相关脚本的最佳方式是什么? For what it's worth, the goal is to integrate this in a GitHub actions script.对于它的价值,目标是将其集成到 GitHub 操作脚本中。

There is a bit more to this: you helps to ensure that the R build is instrumented for it.还有一点:您帮助确保 R 构建已针对它进行检测。 See Section 4.3.2 of Writing R Extensions :请参阅编写 R 扩展的第 4.3.2 节

On platforms where valgrind is installed you can build a version of R with extra instrumentation to help valgrind detect errors in the use of memory allocated from the R heap.在安装了 valgrind 的平台上,您可以使用额外的工具构建 R 版本,以帮助 valgrind 检测从 ZE1E1D3D40573127E9EE0480CAF1283 分配的 memory 使用中的错误。 The configure option is --with-valgrind-instrumentation=level, where level is 0, 1 or 2. Level 0 is the default and does not add anything.配置选项是 --with-valgrind-instrumentation=level,其中 level 是 0、1 或 2。Level 0 是默认值,不添加任何内容。 Level 1 will detect some uses117 of uninitialised memory and has little impact on speed (compared to level 0).级别 1 将检测到未初始化的 memory 的一些使用 117,并且对速度的影响很小(与级别 0 相比)。 Level 2 will detect many other memory-use bugs118 but make R much slower when running under valgrind.级别 2 将检测许多其他内存使用错误 118,但在 valgrind 下运行时会使 R 慢得多。 Using this in conjunction with gctorture can be even more effective (and even slower).将它与 gctorture 结合使用会更有效(甚至更慢)。

So you probably want to build yourself a Docker-ized version of R to call from your GitHub Action.因此,您可能希望自己构建一个 Docker 化版本的 R 以从您的 GitHub 操作中调用。 I think the excellent 'sumo' container by Winston has a valgrind build as well so you could try that as well.我认为 Winston 出色的“相扑”容器也具有 valgrind 构建,因此您也可以尝试一下。 It's huge at over 4gb:超过 4GB 非常大:

edd@rob:~$ docker images| grep wch     # some whitespace edit out
wch1/r-debug       latest     a88fabe8ec81      8 days ago     4.49GB
edd@rob:~$ 

And of course if you test dependent packages you have to get them into the valgrind session too...当然,如果您测试依赖包,您也必须将它们放入 valgrind session...

Unfortunately, I found the learning curve involved in dockerizing R in GitHub actions, per @dirk-eddelbuettel's suggestion , too steep.不幸的是,根据@dirk-eddelbuettel 的建议,我发现在 GitHub 操作中对接 R 所涉及的学习曲线太陡峭。

I came up with the hacky approach of adding a file memcheck.R to the root of the package with the contents我想出了将文件memcheck.R添加到 package 的根目录的 hacky 方法

devtools::load_all()
devtools::run_examples()
devtools::build_vignettes()
devtools::test()

(remembering to add to .Rbuildignore ). (记得添加到.Rbuildignore )。

Running R -d "valgrind --tool=memcheck --leak-check=full" --vanilla < memcheck.R then seems to work, albeit with the reporting of some issues that appear to be false positives, or at least issues that are not identified by CRAN's valgrind implementation.运行R -d "valgrind --tool=memcheck --leak-check=full" --vanilla < memcheck.R然后似乎可以工作,尽管报告了一些似乎是误报的问题,或者至少是这样的问题没有被 CRAN 的 valgrind 实现识别。

(Readers who know what they are doing are invited to suggest shortcomings of this approach in the comments!) (请知道自己在做什么的读者在评论中提出这种方法的缺点!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM