问题
I created a console program and run it in my android phone(android 6.0 and API LEVEL is 23). And it has a function named net_test, show as below.
void net_test()
{
if (ATrace_isEnabled())
{
printf("ATrace is enable!\n");
}
else
{
printf("ATrace is disable!\n");
}
ATrace_beginSection("net_test");
net_layer_test();
ATrace_endSection();
}
I want to use systrace to capture the timing information of this function. so I add those ATrace_XXX() API. But ATrace_isEnabled() always returns false.I used python systrace.py -t 4 -o mynewtrace.html perf sched gfx
command to capture profile data, didn't see any information of net_test().Is anyone familiar with ATrace_isEnabled()?How can I catch the timing information of net_test() through systrace? Thanks a lot.
回答1:
For ATrace_isEnabled() (reference), this function finally calls into atrace_is_cmdline_match() in platform/system/core/libcutils/trace-dev.c. (reference).
Since this API is hard-coded as application category (ATRACE_TAG_APP), you have to enable tracer of this category by following command.
adb shell "setprop debug.atrace.tags.enableflags 0x1000"
(You can also set other bits to 1 to enable other categories.)
For example, if your console program is "simapp", you can enter following commands to enable systrace.
adb shell "setprop debug.atrace.app_number 1"
adb shell "setprop debug.atrace.app_0 simapp"
回答2:
Jason, by using those commands that you have written, ATrace_isEnabled
is becoming true but once you give this command python systrace.py -t 4 -o mynewtrace.html perf sched gfx
, as here you have specified t=4 sec, so after 4 secs, it will again become false. So, everytime you need to trace, you have to give following command.
adb shell "setprop debug.atrace.app_number 1"
adb shell "setprop debug.atrace.app_0 appname"
Here, make sure you put correct app name which is visible in logcat, because sometime, appname is different. And now for tracing, you need to specify which app you want to trace, because by default that is disabled. So, here command will be
python systrace.py -t 4 -a appname -o mynewtrace.html perf sched gfx
Another easy way to trace is by using Android P's(or above) predefined systrace option which you can get in android's developers option. There you don't need to give any commands. Just push .so
file of your custom events and trace directly by running your app.
来源:https://stackoverflow.com/questions/48076751/atrace-isenabled-return-false