Merge "Add async events tracing to android.os.Trace" into jb-mr2-dev

This commit is contained in:
Romain Guy
2013-04-12 20:57:59 +00:00
committed by Android (Google) Code Review
2 changed files with 76 additions and 11 deletions

View File

@@ -74,6 +74,8 @@ public final class Trace {
private static native void nativeTraceCounter(long tag, String name, int value);
private static native void nativeTraceBegin(long tag, String name);
private static native void nativeTraceEnd(long tag);
private static native void nativeAsyncTraceBegin(long tag, String name, int cookie);
private static native void nativeAsyncTraceEnd(long tag, String name, int cookie);
private static native void nativeSetAppTracingAllowed(boolean allowed);
static {
@@ -194,6 +196,42 @@ public final class Trace {
}
}
/**
* Writes a trace message to indicate that a given section of code has
* begun. Must be followed by a call to {@link #asyncTraceEnd} using the same
* tag. Unlike {@link #traceBegin(long, String)} and {@link #traceEnd(long)},
* asynchronous events do not need to be nested. The name and cookie used to
* begin an event must be used to end it.
*
* @param traceTag The trace tag.
* @param methodName The method name to appear in the trace.
* @param cookie Unique identifier for distinguishing simultaneous events
*
* @hide
*/
public static void asyncTraceBegin(long traceTag, String methodName, int cookie) {
if (isTagEnabled(traceTag)) {
nativeAsyncTraceBegin(traceTag, methodName, cookie);
}
}
/**
* Writes a trace message to indicate that the current method has ended.
* Must be called exactly once for each call to {@link #asyncTraceBegin(long, String, int)}
* using the same tag, name and cookie.
*
* @param traceTag The trace tag.
* @param methodName The method name to appear in the trace.
* @param cookie Unique identifier for distinguishing simultaneous events
*
* @hide
*/
public static void asyncTraceEnd(long traceTag, String methodName, int cookie) {
if (isTagEnabled(traceTag)) {
nativeAsyncTraceEnd(traceTag, methodName, cookie);
}
}
/**
* Writes a trace message to indicate that a given section of code has begun. This call must
* be followed by a corresponding call to {@link #endSection()} on the same thread.

View File

@@ -27,6 +27,18 @@
namespace android {
static void sanitizeString(String8& utf8Chars) {
size_t size = utf8Chars.size();
char* str = utf8Chars.lockBuffer(size);
for (size_t i = 0; i < size; i++) {
char c = str[i];
if (c == '\0' || c == '\n' || c == '|') {
str[i] = ' ';
}
}
utf8Chars.unlockBuffer();
}
static jlong android_os_Trace_nativeGetEnabledTags(JNIEnv* env, jclass clazz) {
return atrace_get_enabled_tags();
}
@@ -41,17 +53,8 @@ static void android_os_Trace_nativeTraceBegin(JNIEnv* env, jclass clazz,
jlong tag, jstring nameStr) {
const size_t MAX_SECTION_NAME_LEN = 127;
ScopedStringChars jchars(env, nameStr);
String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()),
jchars.size());
size_t size = utf8Chars.size();
char* str = utf8Chars.lockBuffer(size);
for (size_t i = 0; i < size; i++) {
char c = str[i];
if (c == '\0' || c == '\n' || c == '|') {
str[i] = ' ';
}
}
utf8Chars.unlockBuffer();
String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
sanitizeString(utf8Chars);
atrace_begin(tag, utf8Chars.string());
}
@@ -60,6 +63,24 @@ static void android_os_Trace_nativeTraceEnd(JNIEnv* env, jclass clazz,
atrace_end(tag);
}
static void android_os_Trace_nativeAsyncTraceBegin(JNIEnv* env, jclass clazz,
jlong tag, jstring nameStr, jint cookie) {
const size_t MAX_SECTION_NAME_LEN = 127;
ScopedStringChars jchars(env, nameStr);
String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
sanitizeString(utf8Chars);
atrace_async_begin(tag, utf8Chars.string(), cookie);
}
static void android_os_Trace_nativeAsyncTraceEnd(JNIEnv* env, jclass clazz,
jlong tag, jstring nameStr, jint cookie) {
const size_t MAX_SECTION_NAME_LEN = 127;
ScopedStringChars jchars(env, nameStr);
String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
sanitizeString(utf8Chars);
atrace_async_end(tag, utf8Chars.string(), cookie);
}
static void android_os_Trace_nativeSetAppTracingAllowed(JNIEnv* env,
jclass clazz, jboolean allowed) {
atrace_set_debuggable(allowed);
@@ -79,6 +100,12 @@ static JNINativeMethod gTraceMethods[] = {
{ "nativeTraceEnd",
"(J)V",
(void*)android_os_Trace_nativeTraceEnd },
{ "nativeAsyncTraceBegin",
"(JLjava/lang/String;I)V",
(void*)android_os_Trace_nativeAsyncTraceBegin },
{ "nativeAsyncTraceEnd",
"(JLjava/lang/String;I)V",
(void*)android_os_Trace_nativeAsyncTraceEnd },
{ "nativeSetAppTracingAllowed",
"(Z)V",
(void*)android_os_Trace_nativeSetAppTracingAllowed },