Thread overview
controling callgrind intrumentation from D
Re: controlling callgrind instrumentation from D
Sep 13
bauss
September 10

Hi I've found myself in need of controlling when the profiler callgrind which is part of valgrind starts and stops instrumentation.
Both for performance reasons and to be able to actually read the part of the profile I am interested in, as otherwise it easily gets lost in the noise.

After a bit of digging I found that the callgrind header export macros with which to start and stop the instrumentation as well as telling it to dump the stats out.

But those use inline asm and the preprocessor so cannot be easily translated to D.
So just write extern wrapper functions and create a file that looks like

#include <valgrind/callgrind.h>
extern void callgrind_start(void)
{
  CALLGRIND_START_INSTRUMENTATION;
}

extern void callgrind_stop(void)
{
  CALLGRIND_STOP_INSTRUMENTATION;
}

extern void callgrind_dumpstats(void)
{
  CALLGRIND_DUMP_STATS;
}

after compiling that with gcc and looking at the assembly you will see some useless preamble in those functions.

stack-overflow helpfully revealed that those are part of stack protection.
which -fno-stack-protector disables.
but then you are still left with the eh_frame section that isn't used.

-fno-asynchronous-unwind-tables gets rid of that.

the full commandline looks like
gcc -Os callgrind_instr.c -c -fno-asynchronous-unwind-tables -fno-stack-protector

now just add callgrind_instr.o to your build and use a d header such as

extern (c) void callgrind_start();
extern (c) void callgrind_stop();
extern (c) void callgrind_dumpstats();

And there we go.
Pretty nice to be able to do that.

September 10

On Friday, 10 September 2021 at 14:34:35 UTC, Stefan Koch wrote:

>
extern (c) void callgrind_start();
extern (c) void callgrind_stop();
extern (c) void callgrind_dumpstats();

Correction that's supposed to be:

extern (C) void callgrind_start();
extern (C) void callgrind_stop();
extern (C) void callgrind_dumpstats();
September 13

On Friday, 10 September 2021 at 15:13:06 UTC, Stefan Koch wrote:

>

On Friday, 10 September 2021 at 14:34:35 UTC, Stefan Koch wrote:

>
extern (c) void callgrind_start();
extern (c) void callgrind_stop();
extern (c) void callgrind_dumpstats();

Correction that's supposed to be:

extern (C) void callgrind_start();
extern (C) void callgrind_stop();
extern (C) void callgrind_dumpstats();

Wow it took me a long time to figure out the differences and that it was just the capitalization from c to C.