Celtoys / Remotery

Single C file, Realtime CPU/GPU Profiler with Remote Web Viewer
Apache License 2.0
3.1k stars 262 forks source link

Auto generate names #46

Open Scraft opened 9 years ago

Scraft commented 9 years ago

I wanted to be able to just drop in a single macro in a bunch of functions, and have the API auto fill-in the function names for me, i.e.

void func1( ) {
    rmt_ScopedCPUSampleAutoName( );
}
void func2( ) {
    rmt_ScopedCPUSampleAutoName( );
}
int main( int argc, char ** argv ) {
    rmt_ScopedCPUSampleAutoName( );
    func1( );
    func2( );
    return 0;
}

I didn't want to have to manually pass in a name to each call. My solution:

#define rmt_BeginCPUSampleAutoName( ) \
    RMT_OPTIONAL(RMT_ENABLED, { \
        static rmtU32 rmt_sample_hash_##__LINE__ = 0;   \
        _rmt_BeginCPUSample(__FUNCTION__, &rmt_sample_hash_##__LINE__); \
    })

#define rmt_ScopedCPUSampleAutoName( ) \
    RMT_OPTIONAL(RMT_ENABLED, rmt_BeginCPUSampleAutoName( )); \
    RMT_OPTIONAL(RMT_ENABLED, rmt_EndCPUSampleOnScopeExit rmt_ScopedCPUSample##__LINE__);

This generates a call tree like:

celtoysautoname

Sorry this isn't a proper full request, but I hoped this might be something you'd consider implementing (either as is, or in a better way if you know of one).

dwilliamson commented 9 years ago

This is a very good idea.

I would love to add this right now but the public interface is very inflexible in terms of adding new permutations. I want:

So instead of adding a new named macro for different sample types it's added as a parameter, like:

rmt_BeginSample(CPU, Name)

Adding auto name would explode existing combinations even more. I should really solve this. In fact, once this is solved the only way I can think of adding the auto name feature is the same way you've done it. Unless you require names to be strings, which I'm not sure I want.

In terms of implementation, it's probably best to generate a unique symbol for the sample variable using the __COUNTER__ macro:

#define JOIN2(x, y) x ## y
#define JOIN(x, y) JOIN2(x, y)
#define UNIQUE(x) JOIN(x, __COUNTER__)