130662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin/*
230662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * Copyright 2013 The Android Open Source Project
330662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin *
430662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * Licensed under the Apache License, Version 2.0 (the "License");
530662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * you may not use this file except in compliance with the License.
630662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * You may obtain a copy of the License at
730662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin *
830662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin *      http://www.apache.org/licenses/LICENSE-2.0
930662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin *
1030662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * Unless required by applicable law or agreed to in writing, software
1130662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * distributed under the License is distributed on an "AS IS" BASIS,
1230662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1330662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * See the License for the specific language governing permissions and
1430662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * limitations under the License.
1530662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin */
1630662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkinpackage com.android.ex.camera2.utils;
1730662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin
1830662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkinimport android.util.Log;
1930662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin
2030662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin/**
2130662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * Writes trace events to the system trace buffer.  These trace events can be
2230662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * collected and visualized using the Systrace tool.
2330662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin *
2430662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * <p>
2530662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * This tracing mechanism is independent of the method tracing mechanism
2630662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * offered by {@link Debug#startMethodTracing}.  In particular, it enables
2730662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * tracing of events that occur across multiple processes.
2830662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * </p>
2930662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin *
3030662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * <p>
3130662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * All traces are written using the <pre>APP</pre> tag.
3230662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin * </p>
3330662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin */
3430662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkinpublic final class SysTrace {
3530662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin
3630662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    private static final String TAG = "SysTrace";
3730662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
3830662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin
3930662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    private static int sNestingLevel = 0;
4030662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin
4130662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    /**
4230662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * Writes trace message to indicate the value of a given counter.
4330662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     *
4430662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * @param counterName The counter name to appear in the trace.
4530662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * @param counterValue The counter value.
4630662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     *
4730662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     */
4830662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    public static void traceCounter(String counterName, int counterValue) {
4930662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin        if (VERBOSE) {
5030662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin            Log.v(TAG, "traceCounter " + counterName + " " + counterValue);
5130662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin        }
5230662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    }
5330662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin
5430662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    /**
5530662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * Writes a trace message to indicate that a given section of code has begun. This call must
5630662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * be followed by a corresponding call to {@link #endSection()} on the same thread.
5730662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     *
5830662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * <p class="note"> At this time the vertical bar character '|', newline character '\n', and
5930662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * null character '\0' are used internally by the tracing mechanism.  If sectionName contains
6030662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * these characters they will be replaced with a space character in the trace.
6130662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     *
6230662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * @param sectionName The name of the code section to appear in the trace.  This may be at
6330662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * most 127 Unicode code units long.
6430662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     */
6530662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    public static void beginSection(String sectionName) {
6630662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin        if (VERBOSE) {
6730662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin            Log.v(TAG, String.format("beginSection[%d] %s", sNestingLevel, sectionName));
6830662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin            sNestingLevel++;
6930662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin        }
7030662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    }
7130662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin
7230662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    /**
7330662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * Writes a trace message to indicate that a given section of code has
7430662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * ended.
7530662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * <p>
7630662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * This call must be preceded by a corresponding call to
7730662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * {@link #beginSection(String)}. Calling this method will mark the end of
7830662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * the most recently begun section of code, so care must be taken to ensure
7930662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * that beginSection / endSection pairs are properly nested and called from
8030662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * the same thread.
8130662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * </p>
8230662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     */
8330662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    public static void endSection() {
8430662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin        if (VERBOSE) {
8530662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin            sNestingLevel--;
8630662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin            Log.v(TAG, String.format("endSection[%d]", sNestingLevel));
8730662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin        }
8830662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    }
8930662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin
9030662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    /**
9130662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * Writes a trace message to indicate that a given section of code has
9230662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * begun.
9330662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     *
9430662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * <p>Must be followed by a call to {@link #endSectionAsync} using the same
9530662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * tag. Unlike {@link #beginSection} and {@link #endSection},
9630662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * asynchronous events do not need to be nested. The name and cookie used to
9730662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * begin an event must be used to end it.</p>
9830662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     *
9930662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * @param methodName The method name to appear in the trace.
10030662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * @param cookie Unique identifier for distinguishing simultaneous events
10130662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     */
10230662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    public static void beginSectionAsync(String methodName, int cookie) {
10330662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin        if (VERBOSE) {
10430662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin            Log.v(TAG, "beginSectionAsync " + methodName + " " + cookie);
10530662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin        }
10630662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    }
10730662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin
10830662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    /**
10930662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * Writes a trace message to indicate that the current method has ended.
11030662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * Must be called exactly once for each call to {@link #beginSectionAsync}
11130662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * using the same tag, name and cookie.
11230662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     *
11330662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * @param methodName The method name to appear in the trace.
11430662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     * @param cookie Unique identifier for distinguishing simultaneous events
11530662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin     */
11630662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    public static void endSectionAsync(String methodName, int cookie) {
11730662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin        if (VERBOSE) {
11830662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin            Log.v(TAG, "endSectionAsync " + methodName + " " + cookie);
11930662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin        }
12030662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin    }
12130662b3366c815603d3ed61dc3868e0ea40788e7Igor Murashkin}
122