1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/android/trace_event_binding.h"
11 #include "base/debug/trace_event.h"
12 #include "base/debug/trace_event_impl.h"
13 #include "base/lazy_instance.h"
14 #include "jni/TraceEvent_jni.h"
21 const char kJavaCategory
[] = "Java";
22 const char kToplevelCategory
[] = "toplevel";
23 const char kLooperDispatchMessage
[] = "Looper.dispatchMessage";
25 // Boilerplate for safely converting Java data to TRACE_EVENT data.
26 class TraceEventDataConverter
{
28 TraceEventDataConverter(JNIEnv
* env
,
34 name_(env
->GetStringUTFChars(jname
, NULL
)),
35 arg_(jarg
? env
->GetStringUTFChars(jarg
, NULL
) : NULL
) {
37 ~TraceEventDataConverter() {
38 env_
->ReleaseStringUTFChars(jname_
, name_
);
40 env_
->ReleaseStringUTFChars(jarg_
, arg_
);
43 // Return saves values to pass to TRACE_EVENT macros.
44 const char* name() { return name_
; }
45 const char* arg_name() { return arg_
? "arg" : NULL
; }
46 const char* arg() { return arg_
; }
55 DISALLOW_COPY_AND_ASSIGN(TraceEventDataConverter
);
58 class TraceEnabledObserver
: public debug::TraceLog::EnabledStateObserver
{
60 virtual void OnTraceLogEnabled() OVERRIDE
{
61 JNIEnv
* env
= base::android::AttachCurrentThread();
62 base::android::Java_TraceEvent_setEnabled(env
, true);
64 virtual void OnTraceLogDisabled() OVERRIDE
{
65 JNIEnv
* env
= base::android::AttachCurrentThread();
66 base::android::Java_TraceEvent_setEnabled(env
, false);
70 base::LazyInstance
<TraceEnabledObserver
>::Leaky g_trace_enabled_state_observer_
;
74 static void RegisterEnabledObserver(JNIEnv
* env
, jclass clazz
) {
75 bool enabled
= debug::TraceLog::GetInstance()->IsEnabled();
76 base::android::Java_TraceEvent_setEnabled(env
, enabled
);
77 debug::TraceLog::GetInstance()->AddEnabledStateObserver(
78 g_trace_enabled_state_observer_
.Pointer());
81 static void StartATrace(JNIEnv
* env
, jclass clazz
) {
82 base::debug::TraceLog::GetInstance()->StartATrace();
85 static void StopATrace(JNIEnv
* env
, jclass clazz
) {
86 base::debug::TraceLog::GetInstance()->StopATrace();
89 static void Instant(JNIEnv
* env
, jclass clazz
,
90 jstring jname
, jstring jarg
) {
91 TraceEventDataConverter
converter(env
, jname
, jarg
);
92 if (converter
.arg()) {
93 TRACE_EVENT_COPY_INSTANT1(kJavaCategory
, converter
.name(),
94 TRACE_EVENT_SCOPE_THREAD
,
95 converter
.arg_name(), converter
.arg());
97 TRACE_EVENT_COPY_INSTANT0(kJavaCategory
, converter
.name(),
98 TRACE_EVENT_SCOPE_THREAD
);
102 static void Begin(JNIEnv
* env
, jclass clazz
,
103 jstring jname
, jstring jarg
) {
104 TraceEventDataConverter
converter(env
, jname
, jarg
);
105 if (converter
.arg()) {
106 TRACE_EVENT_COPY_BEGIN1(kJavaCategory
, converter
.name(),
107 converter
.arg_name(), converter
.arg());
109 TRACE_EVENT_COPY_BEGIN0(kJavaCategory
, converter
.name());
113 static void End(JNIEnv
* env
, jclass clazz
,
114 jstring jname
, jstring jarg
) {
115 TraceEventDataConverter
converter(env
, jname
, jarg
);
116 if (converter
.arg()) {
117 TRACE_EVENT_COPY_END1(kJavaCategory
, converter
.name(),
118 converter
.arg_name(), converter
.arg());
120 TRACE_EVENT_COPY_END0(kJavaCategory
, converter
.name());
124 static void BeginToplevel(JNIEnv
* env
, jclass clazz
) {
125 TRACE_EVENT_BEGIN0(kToplevelCategory
, kLooperDispatchMessage
);
128 static void EndToplevel(JNIEnv
* env
, jclass clazz
) {
129 TRACE_EVENT_END0(kToplevelCategory
, kLooperDispatchMessage
);
132 static void StartAsync(JNIEnv
* env
, jclass clazz
,
133 jstring jname
, jlong jid
, jstring jarg
) {
134 TraceEventDataConverter
converter(env
, jname
, jarg
);
135 if (converter
.arg()) {
136 TRACE_EVENT_COPY_ASYNC_BEGIN1(kJavaCategory
,
139 converter
.arg_name(),
142 TRACE_EVENT_COPY_ASYNC_BEGIN0(kJavaCategory
,
148 static void FinishAsync(JNIEnv
* env
, jclass clazz
,
149 jstring jname
, jlong jid
, jstring jarg
) {
150 TraceEventDataConverter
converter(env
, jname
, jarg
);
151 if (converter
.arg()) {
152 TRACE_EVENT_COPY_ASYNC_END1(kJavaCategory
,
155 converter
.arg_name(),
158 TRACE_EVENT_COPY_ASYNC_END0(kJavaCategory
,
164 bool RegisterTraceEvent(JNIEnv
* env
) {
165 return RegisterNativesImpl(env
);
168 } // namespace android