Building MongoDB with Branch Coverage Compiler Flag

Hi community,

I am trying to build MongoDB (latest version) with the goal to measure the branch coverage. I have no prior knowledge of building MongoDB, so I could use a little help. First, I wanted to ask as this has probably been done, if you can point me in the direction of any kind of documentation.

Second, I already was partially successful with running:

python buildscripts/scons.py install-mongod CCFLAGS="-g -fprofile-arcs -ftest-coverage" LINKFLAGS="-fprofile-arcs"

It seems to produce the correct files(.gcno) in sconf_temp, but I’m not sure how to proceed from here. The created executable doesn’t seem to create the relevant analysis files(gcda).

Thanks for any help in advance

Hi -

As far as build documentation, I’m sorry to report that it is more or less lacking. We have plans to address that, but it won’t happen right away. For now, your best resource to get build system questions answered is probably to post here.

Regarding your coverage build: by “latest version” do you mean v4.4 (i.e. latest stable release), or the master branch which is used for active development?

Meanwhile I just tested out your build command on the master branch and it seems to work for me. I get .gcno files, and after I run a binary I get .gcda files as well.

The .gcno files should exist in a lot more places than in sconf_temp though: you should find .gcno files for every object file under build/opt (that directory may vary with build options, but is correct for the build command you have provided). After you run a binary, you should also find .gcda files under built/opt:

$ find build/opt -name "*.gcno" | wc -l
341
$ find build/opt -name "*.gcda" | wc -l
317

In the above case I built the target build/install/bin/wt rather than install-mongod since it is much faster to build than the whole mongod binary. I then ran wt --help to produce the .gcda files. You might experiment a bit with that target to see if you can get it working before moving on to building install-mongod. I think it will save you some time.

It might also be useful if you provided details on your OS, distro version, tooolchain, etc., if you still find it isn’t working.

A few other notes:

  • You don’t need to add -g to the build, we do that for you, always.
  • You may want to add -ftest-coverage to LINKFLAGS as well. I don’t know if it makes a difference, and I didn’t happen to need to do so to get .gcda files, but I think it is probably a good idea.

I hope this helps!

Thanks,
Andrew

2 Likes

Thanks Adrew for the quick reply and the helpful answer. First of all, I am running the master branch. Secondly, your absolutely right about the location of the .gcno files.

Now, I built the wired tiger target build/install/bin/wt and it just works with that. I get the same amount of .gcno files that you get and after running it, also the .gcda files. It didn’t matter if I put -g, it didn’t matter if I ran with -ftest-coverage in LINKFLAGS. So I rebuilt the install-mongod target because I thought I missed something, but still no .gcda files after executing mongod. Note: After building install-mongod I get around 3120 .gcno files in build/opt. I am now gonna look into more targets that I can build and test them.

FYI, I am running the build on Ubuntu 18.04.5 with gcc 8.4.0, gcov 8.4.0(probably not used for building, but later on for the analysis tool lcov) and python 3.6.9.

My follow up question would be, if I missed some steps, executing the target. Currently, I just run it from the install/bin folder directly, as the root user. Additionally, what kind of targets could I test.

Hi @Patrick_S -

So, this is a nice find. I reproduced your result that while the wt binary produces .gcda files, the mongod binary apparently does not. Thanks for taking the time to write it up and let us know.

Would you please open a SERVER ticket in the MongoDB JIRA at https://jira.mongodb.org/projects/SERVER describing your findings?

If you would like to continue investigating, other interesting binaries to try out include mongo, mongos, and unit tests like base_test. Please feel free to include any results you may generate for those targets in your ticket and link back to this discussion.

Thanks,
Andrew

@Patrick_S - Actually, hold off on that ticket. I think I know what is going on here, but I need to run a quick build to verify.

Hi @Patrick_S -

If you add CPPDEFINES=MONGO_GCOV to your SCons invocation you will start getting .gcda files. The mongod binary goes through a non-standard exit path. That define is needed so that we can explicitly invoke the gcov related routine to dump the coverage data at exit.

Thanks,
Andrew

2 Likes

Hi Andrew,

the additional SCons target made it also for me possible to produce .gcda files.

Thank you very much for all the help,
Patrick