androidandroid-studioandroid-profilerandroid-traceviewperfetto

CPU utilisation curve from perfetto trace


I wanted to trace the CPU and memory utilization of one of my algorithm on a mobile device. So far I have implemented it in Android as an app and collected the resource utilization trace using Android profiler as a trace file (filename.trace). I have tried using the tool, perfetto UI which displays the trace like this: enter image description here

How can I extract the original time series data which are being displayed in the android studio like the one below from this trace file?

enter image description here


Solution

  • If I understand your question correctly, you want to see the CPU utilization chart in Perfetto UI?

    The short answer is no, because they're different data.

    Long answer: what Android Studio profiler shows in the chart is somewhat different:

    1. The chart in your screenshot is actually CPU utilization data sampled by Android Studio directly from proc/[pid]/stat, not from system trace (or Perfetto). That means the trace file you exported from Android Studio does not contain that data. It's lost when you exit Android Studio.
    2. However, when you capture a system trace, it also records CPU utilization data and if you click into CPU Profiler in Android Studio and select the system trace you captured, the CPU usage chart on the top will show the same data as collected by system trace. That is part of the trace file you exported.
    3. Moreover, the system trace CPU utilization data is for all processes. What Android Studio does is it extracts only the data for your app process (since the IDE knows the pid) and computes the utilization percentage as follows: time_spent_in_app_per_50_miliseconds / 50_miliseconds * cpu_core_count.
    4. What you can do if you want to compute the utilization from the trace file outside of Android Studio is to use Perfetto TraceProcessor. Write SQL queries to get the CPU scheduling data filtered by your pid, bucket them into 50 milisecond chunks and calculate the utilization same way as Android Studio. You can then visualize it in a spreadsheet but not in Perfetto UI as it doesn't support drawing charts from TraceProcessor data.