-->

如何使用async_start和async_stop Android版systrace / atra

2019-10-23 14:44发布

我想捕捉我的Android手机上Systrace报告,在做自动化测试。 它是未知的测试需要多长时间,所以我不能指定Systrace的--time时期。

挖掘到更深systrace.py,我发现systrace使用atrace得到内核日志。

我用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

如何使用atrace开始在我的自动化测试的开始跟踪和停止跟踪,并在我的自动化测试结束倾销内核日志?

我试着用下面的命令,但我不认为它工作正常。 只有async_dump给我的日志中的一些数据。 async_stop转储没有在日志中的任何内容。 如何正确启动跟踪,转储,然后停止了吗?

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 

Answer 1:

  1. schedgfx大概应该是2个独立的呼叫: schedgfx 。 运行时,会看到您设备的输出:

    $ adb shell atrace --list_categories

  2. 类别应该是命令行的最后一部分(在你的命令,该类别前放置async_*选项):

    $ 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. 该工具在一个圆形缓冲运行,所以每次运行dump命令时,你只会得到什么内容是当时的缓冲区。 在咖啡rules:0.5包(假设你使用的咖啡),有一个AtraceLogger类,它可以帮助您作为测试工具的一部分,通过调用自动完成这个atraceStart(...)方法:

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

    你可以通过创建一个或@rule @ClassRule,或挂接到它的一些其他的方式,例如与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); } } 

    RuleLoggingUtils.getTestRunDir()方法将捕获的转储文件放到你的应用程序的外部文件路径,这样你就可以拉这些文件的测试完成后,使用:

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

且每个文件atrace然后可以通过与正在运行的systrace使用systrace观察者被检查--from-file=<trace file>选项。



文章来源: How do I use async_start and async_stop in systrace/atrace for Android