OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/android/tracing_controller_android.h" | 5 #include "content/browser/android/tracing_controller_android.h" |
6 | 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/android/early_trace_event.h" |
7 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
9 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
10 #include "base/logging.h" | 13 #include "base/logging.h" |
11 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
| 15 #include "content/browser/tracing/tracing_controller_impl.h" |
12 #include "content/public/browser/tracing_controller.h" | 16 #include "content/public/browser/tracing_controller.h" |
13 #include "jni/TracingControllerAndroid_jni.h" | 17 #include "jni/TracingControllerAndroid_jni.h" |
14 | 18 |
| 19 using base::trace_event::TraceLog; |
| 20 |
15 namespace content { | 21 namespace content { |
| 22 namespace { |
| 23 |
| 24 const char kCategoryGroup[] = "EarlyJava"; |
| 25 |
| 26 // Adds the early Java trace events (i.e. before the native library was called) |
| 27 // to the TraceEvent log before the data gets written to disk. |
| 28 void OnDisableRecording(TraceLog* trace_log) { |
| 29 std::vector<base::android::EarlyTraceEvent> trace_events; |
| 30 base::android::GetAllEarlyTraceEvents(&trace_events); |
| 31 |
| 32 // Enables the tracing recording only for the specific "EarlyJava" category |
| 33 // just for the duration of the dump. Otherwise, the events are dropped in the |
| 34 // AddTraceEventWithThreadIdAndTimestamp call chain. Note that since this |
| 35 // category is not used elsewhere, there is no race condition here. |
| 36 TraceLog::GetInstance()->SetEnabled( |
| 37 base::trace_event::CategoryFilter(kCategoryGroup), |
| 38 TraceLog::RECORDING_MODE, base::trace_event::TraceOptions()); |
| 39 |
| 40 const unsigned char* const category_group_enabled = |
| 41 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(kCategoryGroup); |
| 42 |
| 43 for (std::vector<base::android::EarlyTraceEvent>::const_iterator it = |
| 44 trace_events.begin(); |
| 45 it != trace_events.end(); ++it) { |
| 46 const base::android::EarlyTraceEvent& trace_event = *it; |
| 47 const struct { |
| 48 const char phase; |
| 49 const base::TimeTicks timestamp; |
| 50 const base::TimeTicks thread_time; |
| 51 } phases[] = { |
| 52 {TRACE_EVENT_PHASE_BEGIN, trace_event.begin_timestamp, |
| 53 trace_event.begin_thread_time}, |
| 54 {TRACE_EVENT_PHASE_END, trace_event.end_timestamp, |
| 55 trace_event.end_thread_time}, |
| 56 }; |
| 57 |
| 58 for (size_t i = 0; i < sizeof(phases) / sizeof(phases[0]); ++i) { |
| 59 trace_log->AddTraceEventWithThreadIdAndTimestamp( |
| 60 phases[i].phase, category_group_enabled, trace_event.name.c_str(), |
| 61 trace_event_internal::kNoEventId, |
| 62 trace_event.thread_id, phases[i].timestamp, phases[i].thread_time, |
| 63 0, NULL, NULL, NULL, NULL, |
| 64 TRACE_EVENT_FLAG_COPY | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP); |
| 65 } |
| 66 } |
| 67 |
| 68 TraceLog::GetInstance()->SetDisabled(); |
| 69 } |
| 70 |
| 71 } // namespace |
16 | 72 |
17 static jlong Init(JNIEnv* env, jobject obj) { | 73 static jlong Init(JNIEnv* env, jobject obj) { |
18 TracingControllerAndroid* profiler = new TracingControllerAndroid(env, obj); | 74 TracingControllerAndroid* profiler = new TracingControllerAndroid(env, obj); |
19 return reinterpret_cast<intptr_t>(profiler); | 75 return reinterpret_cast<intptr_t>(profiler); |
20 } | 76 } |
21 | 77 |
22 TracingControllerAndroid::TracingControllerAndroid(JNIEnv* env, jobject obj) | 78 TracingControllerAndroid::TracingControllerAndroid(JNIEnv* env, jobject obj) |
23 : weak_java_object_(env, obj), | 79 : weak_java_object_(env, obj), |
24 weak_factory_(this) {} | 80 weak_factory_(this) {} |
25 | 81 |
26 TracingControllerAndroid::~TracingControllerAndroid() {} | 82 TracingControllerAndroid::~TracingControllerAndroid() {} |
27 | 83 |
28 void TracingControllerAndroid::Destroy(JNIEnv* env, jobject obj) { | 84 void TracingControllerAndroid::Destroy(JNIEnv* env, jobject obj) { |
29 delete this; | 85 delete this; |
30 } | 86 } |
31 | 87 |
| 88 void TracingControllerAndroid::SetupEarlyJavaTracing() { |
| 89 TracingControllerImpl::GetInstance()->SetDisableRecordingCallback( |
| 90 base::Bind(&OnDisableRecording)); |
| 91 } |
| 92 |
32 bool TracingControllerAndroid::StartTracing(JNIEnv* env, | 93 bool TracingControllerAndroid::StartTracing(JNIEnv* env, |
33 jobject obj, | 94 jobject obj, |
34 jstring jcategories, | 95 jstring jcategories, |
35 jstring jtraceoptions) { | 96 jstring jtraceoptions) { |
36 std::string categories = | 97 std::string categories = |
37 base::android::ConvertJavaStringToUTF8(env, jcategories); | 98 base::android::ConvertJavaStringToUTF8(env, jcategories); |
38 base::trace_event::TraceOptions trace_options; | 99 base::trace_event::TraceOptions trace_options; |
39 trace_options.SetFromString( | 100 trace_options.SetFromString( |
40 base::android::ConvertJavaStringToUTF8(env, jtraceoptions)); | 101 base::android::ConvertJavaStringToUTF8(env, jtraceoptions)); |
41 | 102 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 env, | 170 env, |
110 base::trace_event::CategoryFilter::kDefaultCategoryFilterString) | 171 base::trace_event::CategoryFilter::kDefaultCategoryFilterString) |
111 .Release(); | 172 .Release(); |
112 } | 173 } |
113 | 174 |
114 bool RegisterTracingControllerAndroid(JNIEnv* env) { | 175 bool RegisterTracingControllerAndroid(JNIEnv* env) { |
115 return RegisterNativesImpl(env); | 176 return RegisterNativesImpl(env); |
116 } | 177 } |
117 | 178 |
118 } // namespace content | 179 } // namespace content |
OLD | NEW |