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

Can't see my trace event in In System mode #683

Closed YvanMao closed 9 months ago

YvanMao commented 9 months ago

I capture traces on Android13 in System mode, but I can't see my trace event

Step1: invoke startSysTracing Step2: execute cmdline(record_android_trace -t 5s rendering sched freq idle am wm gfx view binder_driver hal dalvik camera input res memory) Step3: invoke testSysTrace (but isEnabled always false) Step4: invoke stopSysTracing Step5: check the trace file

Is there something missing from my steps? Looking forward to your response :)

PERFETTO_TRACK_EVENT_STATIC_STORAGE();

namespace {

    void InitializePerfetto() {
        perfetto::TracingInitArgs args;
        args.backends = perfetto::kSystemBackend;
        args.enable_system_consumer = false;
        perfetto::Tracing::Initialize(args);
        perfetto::TrackEvent::Register();
    }

    void DrawPlayer(int player_number) {
        TRACE_EVENT("rendering", "DrawPlayer", "player_number", player_number);
        // Sleep to simulate a long computation.
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
    }

    void DrawGame() {
        TRACE_EVENT("rendering", "DrawGame");
        DrawPlayer(1);
        DrawPlayer(2);
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_test_trace_PerfettoSdk_testSysTrace(JNIEnv *env, jobject thiz, jstring path) {
    bool isEnabled = perfetto::TrackEvent::IsEnabled();
    PERFETTO_LOG("TrackEvent is %s", isEnabled ? "enabled" : "disabled");

    DrawGame();
}
extern "C"
JNIEXPORT void JNICALL
Java_com_test_trace_PerfettoSdk_startSysTracing(JNIEnv *env, jobject thiz) {
    InitializePerfetto();
}
extern "C"
JNIEXPORT void JNICALL
Java_com_test_trace_PerfettoSdk_stopSysTracing(JNIEnv *env, jobject thiz) {
    perfetto::TrackEvent::Flush();
}
primiano commented 9 months ago

Yes, you need to enable the track-event data source in the config

See https://perfetto.dev/docs/instrumentation/tracing-sdk and specifically the section that mentions

"To include your new track events in the trace, ensure that the track_event data source is included in the trace config. If you do not specify any categories then all non-debug categories will be included by default. However, you can also add just the categories you are interested in like so:"

  config {
    name: "track_event"
    track_event_config {
        enabled_categories: "rendering"
    }
  }
}