https://www.lizenghai.com/archives/26061.html

2 用法

用命令行启动Systrace抓取采样:《Capture a system trace on the command line

自定义采样的方法:《Define custom events》这个方法确实要仔细看,尤其是采样命令的描述,强调了要使用 -a 参数,之前我忽略了这一点,导致自定义的标签总也显示不出来。

python systrace.py -a com.autonavi.amapauto -b 16384 -o my_systrace_report.html sched freq idle am wm gfx view binder_driver hal dalvik camera input res

【坑】采用Android Device Monitor来抓取Systrace时有一个坑,就是经常出现Java heap error(因为我自动插桩所有函数,导致Systrace标签数量庞大),后来发现原因在于Monitor配置的Java堆最大值太小了,收集trace数据时需要大量的堆(怀疑是做数据合并加工)引发堆空间错误。解决方法如下:

1. 找到Monitor脚本所在目录,里面有lib\monitor-x86,例如:C:\Users\liuheng.klh\AppData\Local\Android\Sdk\tools\lib\monitor-x86(或monitor-x86_64)
2. 打开monitor.ini 配置文件,修改如下三项空间配置(尽可能大点就行):
-XX:MaxPermSize=1024m
-Xms2048m
-Xmx4096m

(1)实现自定义标签

在Android 6.0(API 23)中包含trace.h,可以在Native层直接进行引用,获取函数指针进行调用。用 ATRACE_CALL 宏直接在需要插桩的函数入口调用一下即可。关键代码:

 1 #include <android/trace.h>
 2 
 3 /////////////////////////////////////////////////////////////////////
 4 // utils.h
 5 extern void *(*_ATrace_beginSection) (const char* sectionName) __attribute__ ((no_instrument_function));
 6 extern void *(*_ATrace_endSection) (void) __attribute__ ((no_instrument_function));
 7 extern bool *(*_ATrace_isEnabled) (void) __attribute__ ((no_instrument_function));
 8 
 9 // SysTrace
10 class ScopedTrace {
11     const char* _name;
12 public:
13     static bool _bInit;
14     static void init() __attribute__ ((no_instrument_function));
15 
16     typedef void *(*fp_ATrace_beginSection) (const char* sectionName);
17     typedef void *(*fp_ATrace_endSection) (void);
18     typedef bool *(*fp_ATrace_isEnabled) (void);
19 
20 public:
21     inline ScopedTrace(const char* name) __attribute__ ((no_instrument_function)) {
22         if (!_bInit) {
23             init();
24         }
25         _ATrace_beginSection(name) ;
26 
27         _name = name;
28         //LOGD("_ATrace_beginSection(%s) _ATrace_isEnabled()=%d", name, _ATrace_isEnabled());
29     }
30 
31     inline ~ScopedTrace() __attribute__ ((no_instrument_function)) {
32         _ATrace_endSection();
33         //LOGD("_ATrace_endSection(%s) _ATrace_isEnabled()=%d", _name, _ATrace_isEnabled());
34     }
35 };
36 
37 #define ATRACE_NAME(name) ScopedTrace ___tracer(name)
38 #define ATRACE_CALL() ATRACE_NAME(__FUNCTION__)
39 
40 /////////////////////////////////////////////////////////////////////
41 // utils.cpp
42 void *(*_ATrace_beginSection) (const char* sectionName) = NULL;
43 void *(*_ATrace_endSection) (void) = NULL;
44 bool *(*_ATrace_isEnabled) (void) = NULL;
45 
46 bool ScopedTrace::_bInit = false;
47 void ScopedTrace::init() {
48     if (_bInit) {
49         return;
50     }
51 
52     // Retrieve a handle to libandroid.
53     void *lib = dlopen("libandroid.so", RTLD_NOW || RTLD_LOCAL);
54 
55     // Access the native tracing functions.
56     if (lib != NULL) {
57         // Use dlsym() to prevent crashes on devices running Android 5.1
58         // (API level 22) or lower.
59         _ATrace_beginSection = reinterpret_cast<fp_ATrace_beginSection>(
60                 dlsym(lib, "ATrace_beginSection"));
61         _ATrace_endSection = reinterpret_cast<fp_ATrace_endSection>(
62                 dlsym(lib, "ATrace_endSection"));
63         _ATrace_isEnabled = reinterpret_cast<fp_ATrace_isEnabled>(
64                 dlsym(lib, "ATrace_isEnabled"));
65     }
66 
67     LOGI("lib=%p, _ATrace_beginSection=%p, _ATrace_endSection=%p, _ATrace_isEnabled=%p, ATrace_isEnabled()=%d",
68          lib, _ATrace_beginSection, _ATrace_endSection, _ATrace_isEnabled, _ATrace_isEnabled());
69 
70     _bInit = true;
71 }
Systrace(Native)

相关文章:

  • 2021-09-23
  • 2021-10-08
  • 2021-11-01
  • 2021-10-02
  • 2021-07-27
  • 2021-09-11
  • 2021-08-22
  • 2021-07-22
猜你喜欢
  • 2021-09-19
  • 2021-08-10
  • 2021-09-20
  • 2022-12-23
  • 2021-08-24
  • 2021-10-26
相关资源
相似解决方案