Android Systrace

Android Systrace

为什么使用Systrace

  • 通过捕获应用以及Android系统进程的执行时间来分析应用的性能
  • 能够捕获一段时间内整个Android系统的运行状态,生成html图
  • 分析Android应用的显示、绘制性能问题

如何抓取Systrace

  1. Android Studio(Tools->Android->Android Device Monitor)
  2. Android SDK中的systrace.py
  3. 直接使用atrace

在代码中添加trace

Java代码中添加

  • 使用beginSection API
    注意,使用此方法只有在应用的debuggable为true,并且在抓取systrace的时候选定该进程才有效。
Trace.beginSection("traceName");
registerReceiver(ContextReceiver, new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
Trace.endSection();
  • 反射使用traceBegin API
public class TraceUtil {
    private static final String LOG_TAG = TraceUtil.class.getSimpleName();
    private static final String TRACE_MARKER_PATH = "/sys/kernel/debug/tracing/trace_marker";
    // Using VIEW option, also using others, but APP
    private static final long TRACE_TAG_VIEW = 1L << 3;

    public static void begin(String methodName) {
        Log.i(LOG_TAG, "Trace being...");

        try {
            Method traceBegin = Trace.class.getDeclaredMethod("traceBegin", long.class, String.class);
            traceBegin.invoke(Trace.class, TRACE_TAG_VIEW, methodName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void end() {
        Log.i(LOG_TAG, "Trace end...");

        try {
            Method traceEnd = Trace.class.getDeclaredMethod("traceEnd", long.class);
            traceEnd.invoke(Trace.class, TRACE_TAG_VIEW);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void traceMarkerBegin(String methodName) {
        Log.i(LOG_TAG, "traceMarkerBegin");

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(TRACE_MARKER_PATH));

            try {
                fileOutputStream.write(("B|" + Process.myPid() + "|" + "wlb").getBytes());
                fileOutputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // No Need Close
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void traceMarkerEnd() {
        Log.i(LOG_TAG, "traceMarkerEnd");

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(TRACE_MARKER_PATH));

            try {
                fileOutputStream.write(("E").getBytes());
                fileOutputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // No Need Close
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
TraceUtil.begin("wlb");
registerReceiver(ContextReceiver, new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
TraceUtil.end();
  • 直接写trace_marker
TraceUtil.traceMarkerBegin("traceName");
registerReceiver(ContextReceiver, new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
TraceUtil.traceMarkerEnd();

Native代码中添加

  • 使用NDK提供的API
typedef void (*func_p)(void);

static void trace_func(void)
{
    __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "Test Native Trace");
}

static void test_native_trace(func_p)
{
    if (ATrace_isEnabled()) {
        ATrace_beginSection("traceName");
        func_p();
        ATrace_endSection();
    }
}
  • 直接写trace_marker
static inline void begin_trace(int fd)
{
    char buf[MAX_BUFF_SIZE];
    int len = snprintf(buf, MAX_BUFF_SIZE, "B|%d|%s", getpid(), "traceName");

    if (fd > 0)
        write(fd, buf, len);
}

static inline void end_trace(int fd)
{
    if (fd > 0)
        write(fd, "E", 1);
}

// Note: Not close trace_marker fd
static void test_trace_marker(func_p)
{
    const char *trace_marker_path = "/sys/kernel/debug/tracing/trace_marker";
    int tm_fd = open(trace_marker_path, O_WRONLY);

    if (tm_fd < 0) {
        return;
    }

    begin_trace(tm_fd);
    func_p();
    end_trace(tm_fd);
}

traceName

systrace的工作原理

这里写图片描述

使能systrace workflow

这里写图片描述

j3y17qltecmcc:/sys/kernel/debug/tracing # ls
README                          kprobe_events       trace
available_events                kprobe_profile      trace_clock
available_tracers               options             trace_marker
buffer_size_kb                  per_cpu             trace_options
buffer_total_size_kb            printk_formats      trace_pipe
cpu_freq_switch_profile_enabled saved_cmdlines      tracing_cpumask
current_tracer                  saved_cmdlines_size tracing_max_latency
events                          saved_tgids         tracing_on
free_buffer                     set_event           tracing_thresh
instances                       snapshot


  • trace
  • trace_marker
  • buffer_size_kb
  • tracing_on

细节参考 /android/kernel/Documentation/trace/下Ftrace文档

应用trace workflow

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值