diff options
author | 2024-03-12 12:37:53 +0000 | |
---|---|---|
committer | 2024-03-12 21:45:48 +0000 | |
commit | 81deab754e4728f98e09196528e0c3777ca70ed8 (patch) | |
tree | ccdde83e6ecd8b84322245b05f33af870366f830 | |
parent | b8aebd579c5e8d8ad9bded8a0ce742cdb44d26d1 (diff) |
Fix libtracing_perfetto performance impact
1. Avoided checking isPerfettoSdkTracingEnabled on each tracing call.
We already checked that before registering perfetto and if the flag was
false, toPerfettoCategory will never return a valid category hence we'll
fallback to atrace.
2. Added an isPerfettoRegistered check that encapsulates the sdk check
for use in getEnabledCategories. This allows us skip the sdk check there
We'll need to fix result of getEnabledCategories to only return enabled
categories and not just registered ones, but since the flag is off this
it's fine for now and can address in a separate cl.
Test: atest libtracing_perfetto_tests
Bug: 328942318
Bug: 303199244
Change-Id: Ic8fdebd20aba4ac75566c1a1590667891745b92a
-rw-r--r-- | libs/tracing_perfetto/tracing_perfetto.cpp | 3 | ||||
-rw-r--r-- | libs/tracing_perfetto/tracing_perfetto_internal.cpp | 14 | ||||
-rw-r--r-- | libs/tracing_perfetto/tracing_perfetto_internal.h | 2 |
3 files changed, 10 insertions, 9 deletions
diff --git a/libs/tracing_perfetto/tracing_perfetto.cpp b/libs/tracing_perfetto/tracing_perfetto.cpp index c7fb8bd9a8..19d1eb639e 100644 --- a/libs/tracing_perfetto/tracing_perfetto.cpp +++ b/libs/tracing_perfetto/tracing_perfetto.cpp @@ -131,7 +131,8 @@ Result traceCounter(uint64_t category, const char* name, int64_t value) { } uint64_t getEnabledCategories() { - if (internal::isPerfettoSdkTracingEnabled()) { + if (internal::isPerfettoRegistered()) { + // TODO(b/303199244): Return only enabled categories and not all registered ones return internal::getDefaultCategories(); } else { return atrace_get_enabled_tags(); diff --git a/libs/tracing_perfetto/tracing_perfetto_internal.cpp b/libs/tracing_perfetto/tracing_perfetto_internal.cpp index 58ba428610..976db7e430 100644 --- a/libs/tracing_perfetto/tracing_perfetto_internal.cpp +++ b/libs/tracing_perfetto/tracing_perfetto_internal.cpp @@ -70,6 +70,8 @@ PERFETTO_TE_CATEGORIES_DECLARE(FRAMEWORK_CATEGORIES); PERFETTO_TE_CATEGORIES_DEFINE(FRAMEWORK_CATEGORIES); +std::atomic_bool is_perfetto_registered = false; + struct PerfettoTeCategory* toCategory(uint64_t inCategory) { switch (inCategory) { case TRACE_CATEGORY_ALWAYS: @@ -135,15 +137,11 @@ struct PerfettoTeCategory* toCategory(uint64_t inCategory) { } // namespace -bool isPerfettoSdkTracingEnabled() { - return android::os::perfetto_sdk_tracing(); +bool isPerfettoRegistered() { + return is_perfetto_registered; } struct PerfettoTeCategory* toPerfettoCategory(uint64_t category) { - if (!isPerfettoSdkTracingEnabled()) { - return nullptr; - } - struct PerfettoTeCategory* perfettoCategory = toCategory(category); bool enabled = PERFETTO_UNLIKELY(PERFETTO_ATOMIC_LOAD_EXPLICIT( (*perfettoCategory).enabled, PERFETTO_MEMORY_ORDER_RELAXED)); @@ -151,9 +149,10 @@ struct PerfettoTeCategory* toPerfettoCategory(uint64_t category) { } void registerWithPerfetto(bool test) { - if (!isPerfettoSdkTracingEnabled()) { + if (!android::os::perfetto_sdk_tracing()) { return; } + static std::once_flag registration; std::call_once(registration, [test]() { struct PerfettoProducerInitArgs args = PERFETTO_PRODUCER_INIT_ARGS_INIT(); @@ -161,6 +160,7 @@ void registerWithPerfetto(bool test) { PerfettoProducerInit(args); PerfettoTeInit(); PERFETTO_TE_REGISTER_CATEGORIES(FRAMEWORK_CATEGORIES); + is_perfetto_registered = true; }); } diff --git a/libs/tracing_perfetto/tracing_perfetto_internal.h b/libs/tracing_perfetto/tracing_perfetto_internal.h index 9a579f162a..79e4b8f1b4 100644 --- a/libs/tracing_perfetto/tracing_perfetto_internal.h +++ b/libs/tracing_perfetto/tracing_perfetto_internal.h @@ -26,7 +26,7 @@ namespace tracing_perfetto { namespace internal { -bool isPerfettoSdkTracingEnabled(); +bool isPerfettoRegistered(); struct PerfettoTeCategory* toPerfettoCategory(uint64_t category); |