是否可以在没有JNI的情况下直接从本机代码记录Android systrace事件? [英] Is logging Android systrace events directly from native code possible, without JNI?

查看:167
本文介绍了是否可以在没有JNI的情况下直接从本机代码记录Android systrace事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android systrace日志记录系统很棒,但是只能通过Trace.beginSection()Trace.endSection()在代码的Java部分中使用.在代码的C/C ++ NDK(本机)部分中,只能通过JNI使用它,这很慢或在没有Java环境的线程中不可用...

The Android systrace logging system is fantastic, but it only works in the Java portion of the code, through Trace.beginSection() and Trace.endSection(). In a C/C++ NDK (native) portion of the code it can only be used through JNI, which is slow or unavailable in threads without a Java environment...

是否可以通过本机C代码向主systrace跟踪缓冲区添加事件,甚至生成单独的日志?

这个较早的问题提到atrace/ftrace被Android的systrace使用的内部系统.可以轻松地利用它吗?

This older question mentions atrace/ftrace as being the internal system Android's systrace uses. Can this be tapped into (easily)?

奖金TWIST:由于跟踪调用通常会在性能至关重要的部分中进行,因此理想情况下应该有可能在实际事件时间之后 运行调用.即,我希望能够指定记录时间,而不是自己轮询的时间.但这只是锦上添花.

BONUS TWIST: Since tracing calls would often be in performance-critical sections, it should ideally be possible to run the calls after the actual event time. i.e. I for one would prefer to be able to specify the times to log, instead of the calls polling for it themselves. But that would just be icing on the cake.

推荐答案

我认为它不是从NDK公开的.

I don't think it's exposed from the NDK.

如果您查看来源,则可以看到

If you look at the sources, you can see that the android.os.Trace class calls into native code to do the actual work. That code calls atrace_begin() and atrace_end(), which are declared in a header in the cutils library.

如果您从完整的源代码树中提取标头并链接到内部库,则可以直接使用atrace函数.但是,您可以从标题中看到atrace_begin()只是:

You may be able to use the atrace functions directly if you extract the headers from the full source tree and link against the internal libraries. However, you can see from the header that atrace_begin() is simply:

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);
    }
}

事件直接写入跟踪文件描述符. (请注意,时间戳记不是事件的一部分;它是自动添加的.)您可以在代码中执行类似的操作;请参阅.c文件中的atrace_init_once() 查看文件的打开方式.

Events are written directly to the trace file descriptor. (Note that the timestamp is not part of the event; that's added automatically.) You could do something similar in your code; see atrace_init_once() in the .c file to see how the file is opened.

请记住,除非atrace作为NDK的一部分发布,否则使用它的任何代码都是不可移植的,并且可能会在Android的过去或将来版本中失败.但是,由于systrace是调试工具,而不是您实际上不希望在应用程序中启用的工具,因此兼容性可能不是问题.

Bear in mind that, unless atrace is published as part of the NDK, any code using it would be non-portable and likely to fail in past or future versions of Android. However, as systrace is a debugging tool and not something you'd actually want to ship enabled in an app, compatibility is probably not a concern.

这篇关于是否可以在没有JNI的情况下直接从本机代码记录Android systrace事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆