0

I am trying to capture Android systrace while running my automation tests. Since i dont know the duration of my tests so i cannot use param "-t" .

I use below command for tracing -

python systrace.py -a com.myapp -o testauto.html

Manually to stop the trace we need to press enter. How do i do it ? How do i stop capturing the traces so that it automatically creates the trace file ?

Rishi
  • 21
  • 4

2 Answers2

0

systrace.py is meant to be run manually so if you want to automate it you could use the underlying atrace command. It's somewhat convoluted so I'll include the steps in the end.

I strongly recommend that you look into the Perfetto command line tool. You can find more detailed documentation here: https://perfetto.dev/docs/quickstart/android-tracing.

TLDR:

adb shell perfetto -o /data/misc/perfetto-traces/trace_file.pftrace -b 32mb <add_list_of_categories>

Then send the termination signal (i.e. Ctrl+C) to stop tracing. Should be trivial to automate based on your host OS.

Another resource worth looking into is the AndroidX Benchmark library for CI/CD integration: https://developer.android.com/studio/profile/benchmark.

--Appendix--

To run atrace directly:

adb shell /system/bin/atrace -z -b <buffer_size_in_kb> -a <app_name> -o /data/local/tmp/test.trace --async_start

Then stop the trace:

adb shell /system/bin/atrace -z -a <app_name> -o /data/local/tmp/test.trace0 --async_stop

Finally you need to combine the files:

cat test.trace test.trace0 > combined.trace
Yi Yang
  • 421
  • 2
  • 6
  • Thanks for answering , when i run below command i can see only 925 bytes file being genrated , am i doing anything wrong ? ```adb shell perfetto -o /data/misc/perfetto-traces/trace_file.pftrace -b 32mb -t 10s sched freq idle am wm gfx view ``` – Rishi Mar 18 '21 at 05:07
  • I am getting below message when i run - ```E/perfetto(32337): probes_producer.cc:204 Data source id=43 not found I/perfetto(32337): probes_producer.cc:314 Producer stop (id=43) E/perfetto(32337): probes_producer.cc:318 Cannot stop data source id=43, not found I/perfetto(32337): probes_producer.cc:314 Producer stop (id=44) I/perfetto(32454): perfetto_cmd.cc:677 Wrote 924 bytes into /data/misc/perfetto-traces/2021-17-18_15-17.pftrace I/perfetto( 653): tracing_service_impl.cc:1660 Tracing session 42 ended, total sessions:0 ``` – Rishi Mar 18 '21 at 09:53
  • Did you run `adb shell setprop persist.traced.enable 1` to enable tracing on the device (see https://perfetto.dev/docs/quickstart/android-tracing#starting-the-tracing-services)? This only needs to be done once per device. – Yi Yang Mar 19 '21 at 22:52
  • Yes, i have done that and it is random , it works sometime and it doesnt – Rishi Mar 20 '21 at 10:51
  • I suspect it has something to do with tuning configuration parameters for your specific use case. Can you open a bug at https://github.com/google/perfetto/issues so that the team can help you with the issue? – Yi Yang Mar 22 '21 at 21:56
0

Just a note following Yi Yang's answer, for Android 9, running atrace directly does not seem to generate the output file when calling with '--async_start', only when the --async_stop is called. I didn't have to combine them to get the trace out.

cberk1
  • 35
  • 6
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 22 '22 at 00:24