Can Android systrace events be recorded directly from native code without JNI?

Invalid Sign

Is there any way to add events to the main systrace trace buffer or even generate separate logs from native C code?

This earlier question mentioned that atrace / ftrace is the internal system used by Android's systrace. Can this be accessed (easily)?

Bonus twist: since trace calls are usually located in the performance critical part, ideally they can be run after the actual event time. That is, I want to be able to specify the recorded time instead of the call polled by myself. But that will only add to the icing on the cake

resolvent:

I don't think it was exposed from NDK

If you view the source code, you can see that android.os.trace class calls native code for actual work. This code calls atrace_ Begin() and atrace_ End(), which are declared in the header in the cuttils library

If you extract the header from the complete source tree and link it to the internal library, you can use the atrace function directly. However, you can see atrace from the header_ Begin() only:

static inline void atrace_begin(uint64_t tag, const char* name)
{
    if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
        char buf[ATRACE_MESSAGE_LENGTH];
        size_t len;
        len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
        write(atrace_marker_fd, buf, len);
    }
}

Events are written directly to the trace file descriptor. (note that the timestamp is not part of the event; it is automatically added.) you can perform similar operations in your code; See atrace_ init_ Once() in the. C file to see how the file is opened

Remember, unless atrace is released as part of the NDK, any code that uses it will be non portable and may fail in past or future versions of Android. However, since systrace is a debugging tool rather than what you really want to enable in your application, compatibility may not be an issue

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>