简体   繁体   中英

android traceview asynchronous events

I'd like to use traceview to measure performance for several asynchronous events. The asynchronous events are passed to me in a callback that looks similar to the below code.

interface EventCallback {

    void onStartEvent(String name);

    void onStopEvent(String name);
}

where every asynchronous event will start with a "onStartEvent" call and end with an "onStopEvent" call.

I'd like to create trace files for every event. From my reading here ( http://developer.android.com/tools/debugging/debugging-tracing.html#creatingtracefiles ), it's not possible to trace asynchronous events since the ordering of the calls must be "structured" in a "stack" like ordering. So, the call to "Debug.stopMethodTracing()" always applies to the most recent call to "Debug.startMethodTracing("calc");"

So, if I receive callbacks in the following order.

onStartEvent(A)

onStartEvent(B)

onStopEvent(A)

onStopEvent(B)

which will get interpreted to

Debug.startMethodTracing("A");

Debug.startMethodTracing("B");

Debug.stopMethodTracing(); // will apply to "B" instead of "A"

Debug.stopMethodTracing(); // will apply to "A" instead of "B"

Using traceview, is there anyway to do what I want? ie trace "non-structured" asynchronous events?

traceview might be the wrong tool. If you really want to go this route you can keep an "active event count", and keep the tracefile open so long as there is an event being handled. This can result in multiple events being present in the same trace file, but you're tracing method calls in the VM, so there's no simple way around that.

If your events happen on different threads, you could separate them out with a post-processing step. This would require some effort to parse the data and strip out the undesirable records. (See eg this or this .)

You don't really say what you're trying to measure. For example, if you just want start/end times, you could just write those to a log file of your own and skip all the traceview fun.

Depending on what you're after, systrace may be easier to work with. Unfortunately the custom event class ( Trace ) only makes the synchronous event APIs public -- if you don't mind using reflection to access non-public interfaces you can also generate async events .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM