Performance/Fenix/Debug API

< Performance‎ | Fenix
Revision as of 18:38, 28 June 2022 by Mcomella (talk | contribs)

The built-in Debug API (documentation) can be used to capture profiles that:

  • execute fairly early in the profile (when the application can start executing code)
  • capture all Java threads
  • can be opened the Firefox Profiler interface

The downsides are that:

  • has noticeable overhead (a few hundred ms in a 1.5-2.5s start up)
  • requires manually instrumenting the code

The Firefox Profiler is generally preferred but the Debug API can be used to work around some of its limitations. For an overview of other profiling tools, see Performance/Fenix/Getting Started.

Firefox Profiler interface

You can open *.trace files generated by the Debug API in the Firefox Profiler. Just go to https://profiler.firefox.com/ and click "Load a profile from a file". The profiler interface works nearly as well as profiles taken within the Firefox Profiler.

Usage for start up

This method might not work on Android 11 due to the permission changes for writing to external storage. Please update this article if you figure it out.

First, you need to start tracing. Add the following to FenixApplication.kt to measure start up as early as possible:

    override fun attachBaseContext(base: Context) {
        if (base.isMainProcess()) {
            Debug.startMethodTracingSampling(
                "startup",
                0,
                TimeUnit.MILLISECONDS.toMicros(1).toInt()
            )
        }
        super.attachBaseContext(base)
    }

Then, we need to stop tracing by calling Debug.stopMethodTracing(): where you call this depends on what you're trying to profile. An easy place to put it is at the end of HomeActivity.onResume.

The trace file will be saved to the device at /sdcard/<filename> by default. The app requires permission to access this so run this before launching the app for the first time:

adb shell pm grant org.mozilla.fenix android.permission.READ_EXTERNAL_STORAGE
adb shell pm grant org.mozilla.fenix android.permission.WRITE_EXTERNAL_STORAGE

Now launch the app!

Once the device has hit stopMethodTracing, you can pull the profile from the device. If you didn't change the file name provided to start* in the code sample above, you can run the following to get the profile:

adb pull /sdcard/startup.trace

This profile can then be opened by the Firefox Profiler (preferred) or the Android Studio profiler (with File -> Open).