google / perfetto

Performance instrumentation and tracing for Android, Linux and Chrome (read-only mirror of https://android.googlesource.com/platform/external/perfetto/)
https://www.perfetto.dev
Apache License 2.0
2.81k stars 350 forks source link

[Feature Request] Improved support for passing compile time dynamic tracing event names to StaticString #445

Open sudara opened 1 year ago

sudara commented 1 year ago

Hi there!

I don't expect any action, but just wanted to bubble up a feature I'm trying to build in my perfetto integration for the JUCE C++ framework.

A DX/usability improvement I've been trying to provide are some friendlier macros that don't require manual labelling, they just use the current function name.

So instead of TRACE_EVENT ("myCategory", "myFunctionName") I pepper codebase with something like TRACE_DSP() with a compile-time derived function name and a fixed category ("dsp" in this case).

I was hoping to use PERFETTO_DEBUG_FUNCTION_IDENTIFIER() to grab the function identifier, but it's of course.... human-unfriendly and needs trimming.

Doing this at compile time turns out to be a bit annoying, and since it's no longer a literal, it makes StaticString complain:

error: static_assert failed due to requirement 'IsValidEventNameType<char *, void>::value' "Event names must be static strings. To use dynamic event names, see https://perfetto.dev/docs/instrumentation/track-events#dynamic-event-names"

My last effort was to wrap PERFETTO_DEBUG_FUNCTION_IDENTIFIER() in a lambda so it can behave as a template parameter. This.... just about seems to work?

I'm not sure what my "ask" is here, just noting in case anyone has a suggestion, are doing similar things or have a solution that's less hacky. I'm not quite smart enough to know if there's a way for StaticString itself to be happier for my use case. The issue seems to be even though my logic is happily compile-time when run in isolation, it ends up running in a runtime context (or at least triggering the static_assert) when passing to StaticString.

sudara commented 1 year ago

A summary for where things ended up:

Everything works if and only if I store the compile-time string manipulation in a separately declared constexpr variable before feeding it to perfetto::StaticString:

    #define TRACE_DSP(...)                                                                                                            \
        constexpr auto pf = melatonin::compileTimePrettierFunction (WRAP_COMPILE_TIME_STRING (PERFETTO_DEBUG_FUNCTION_IDENTIFIER())); \
        TRACE_EVENT ("dsp", perfetto::StaticString (pf.data()), ##__VA_ARGS__)

In other words, if i call perfetto::StaticString (melatonin::compileTimePrettierFunction... things shift to runtime on msvc/clang.

Not sure if this is compiler behavior (it chooses runtime if it can) or a perfetto limitation (i.e. adding another constexpr ctor would help?) but I'm up and running for now!