I want to capture Systrace report on my Android phone while doing automated testing. It is unknown how long the testing will take, so I can't specify the --time period for the Systrace.

Digging deeper in to the systrace.py, I found out that systrace is using atrace to get the kernel logs.

I used adb shell atrace --help and got the following output:

usage: atrace [options] [categories...]
options include:
  -a appname      enable app-level tracing for a comma separated list of cmdlines
  -b N            use a trace buffer size of N KB
  -c              trace into a circular buffer
  -k fname,...    trace the listed kernel functions
  -n              ignore signals
  -s N            sleep for N seconds before tracing [default 0]
  -t N            trace for N seconds [defualt 5]
  -z              compress the trace dump
  --async_start   start circular trace and return immediatly
  --async_dump    dump the current contents of circular trace buffer
  --async_stop    stop tracing and dump the current contents of circular
                    trace buffer
  --list_categories
                  list the available tracing categories

How can I use atrace to start tracing at the beginning of my automated testing, and stop the tracing and dumping the kernel log at the end of my automated testing?

I tried using the following commands, but I don't think it works properly. Only async_dump gives me some data in the log. async_stop dump doesn't have any content in the log. How do I properly start the trace, dump it, and then stop it?

adb shell atrace -b 10000 -c am shedgfx view --async_start > C:\Users\user1\Desktop\log.txt 

adb shell atrace -b 10000 -c am shedgfx view --async_dump  > C:\Users\user1\Desktop\log.txt 

adb shell atrace -b 10000 -c am shedgfx view --async_stop > C:\Users\user1\Desktop\log.txt 
share|improve this question
  1. schedgfx should probably be 2 separate calls: sched and gfx. See the output from your device when running:

    $ adb shell atrace --list_categories

  2. The categories should be the last part of the command line (in your commands, the categories are placed before the async_* options):

    $ adb shell atrace --help usage: atrace [options] [categories...] options include: -a appname enable app-level tracing for a comma separated list of cmdlines -b N use a trace buffer size of N KB -c trace into a circular buffer -k fname,... trace the listed kernel functions -n ignore signals -s N sleep for N seconds before tracing [default 0] -t N trace for N seconds [defualt 5] -z compress the trace dump --async_start start circular trace and return immediatly --async_dump dump the current contents of circular trace buffer --async_stop stop tracing and dump the current contents of circular trace buffer --list_categories list the available tracing categories

  3. The tool runs on a circular buffer, so each time you run the dump command, you'll only get whatever contents are in the buffer at that time. In the Espresso rules:0.5 package (assuming you are using Espresso), there is an AtraceLogger class that can help automate this for you as part of your test harness, by calling the atraceStart(...) method:

    public void atraceStart(Set<String> traceCategoriesSet, int atraceBufferSize, int dumpIntervalSecs, File destDirectory, String traceFileName) throws IOException

    You can do that by creating a @Rule or @ClassRule, or hook into it some other way such as with a TestRunner:

    @Override
    protected void before() throws Throwable {
        mAtrace = AtraceLogger.getAtraceLoggerInstance(InstrumentationRegistry.getInstrumentation());
        mAtrace.atraceStart(new HashSet<>(Arrays.asList("gfx", "sched", ...)),
                1024 /* bufferSize */, 1 /* dump interval */,
                RuleLoggingUtils.getTestRunDir(), "filename");
    }
    
    @Override
    protected void after() {
        try {
            mAtrace.atraceStop();
        } catch (IOException e) {
            Log.w(TAG, "Failed to stop Atrace", e);
        } catch (InterruptedException e) {
            Log.w(TAG, "Failed to stop Atrace", e);
        }
    }
    

    The RuleLoggingUtils.getTestRunDir() method will place the captured dump files into the external files path for your app, so you can pull those files after the test is finished, using:

    $ adb pull /sdcard/Android/data/com.yourcompany.package/files/testdata/

And each atrace file can then be inspected using the systrace viewer by running systrace with the --from-file=<trace file> option.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.