• Sami Kyostila's avatar
    Redesign ATrace integration · 80207c25
    Sami Kyostila authored
    Chrome and WebView have supported basic Android ATrace tracing, but the
    current approach has some shortcomings:
    
    1) Android R changes the way atrace session activations are broadcast,
       breaking the code in WebView listening for activations
       (OnTraceEnabledChangeListener). This means you can't trace a running
       WebView app with atrace.
    
    2) Neither Chrome nor WebView record early startup events to atrace,
       which means all events before the native library has loaded are
       lost.
    
    3) It's not possible to specify trace categories via atrace, which means
       we need more cumbersome alternative solutions (i.e., command line
       flags) for startup tracing.
    
    4) Writing ATrace events is only supported in WebView.
    
    This patch reworks the ATrace integration to resolve these problems and
    to align the Chrome and WebView implementations. In short, ATrace
    session management is moved to the common (TraceEvent) layer, and Chrome
    and WebView only differ by which trace tags they listen to (APP vs.
    WEBVIEW).
    
    We also add a way to declare trace categories through per-app tags:
    
    $ atrace -a org.chromium.chrome,org.chromium.chrome/<category_filter>
    
    (Note that the plain package name without any category filters must
    always appear in the list on its own.)
    
    Multiple categories can be separated with a double colon:
    
    $ atrace -a org.chromium.chrome,org.chromium.chrome/cat1:cat2
    
    Or by specifying the the app several times to get around the 91
    character limit for each entry:
    
    $ atrace -a org.chromium.chrome,\
                org.chromium.chrome/cat1,\
                org.chromium.chrome/cat2
    
    Finally, to capture Java startup events into a tracing session
    controlled by the system's Perfetto service instead of atrace, a
    special "-atrace" category can be used to only write events into
    Chrome's own tracing service instead of atrace. This way when we
    connect to Perfetto later in the startup sequence and establish
    the real tracing session, startup-related events can be flushed
    into that session without emitting duplicate events into ATrace.
    
    # Basic trace while Chrome is running. Generates ATrace with Chrome
    # events.
    TEST=atrace -a org.chromium.chrome
    
    # Ditto for WebView.
    TEST=atrace webview
    
    # Startup trace -- open Chrome after running this. Results in an ATrace
    # with Chrome pre- and post-startup events.
    TEST=atrace -a org.chromium.chrome
    
    # Ditto for WebView.
    TEST=atrace webview
    
    # Trace with custom categories. Results in a ATrace with only
    # "cc" events from Chrome.
    TEST=atrace -a org.chromium.chrome,org.chromium.chrome/-*:cc
    
    # Ditto for WebView, using GMail as a test app.
    TEST=atrace -a com.google.android.gm,com.google.android.gm/-*:cc webview
    
    # Chrome-only trace. No Chrome events written in the resulting ATrace.
    TEST=atrace -a org.chromium.chrome,org.chromium.chrome/-atrace
    
    Bug: 1095587, b/160768681
    Change-Id: Ia0bee252ce398804c00be8a4179241b75479bf5e
    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2332664Reviewed-by: default avatarBo <boliu@chromium.org>
    Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
    Reviewed-by: default avatarEric Seckler <eseckler@chromium.org>
    Commit-Queue: Bo <boliu@chromium.org>
    Auto-Submit: Sami Kyöstilä <skyostil@chromium.org>
    Cr-Commit-Position: refs/heads/master@{#798791}
    80207c25
trace_event_android_unittest.cc 960 Bytes