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/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
10 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
11 #include "base/logging.h" | 14 #include "base/logging.h" |
12 #include "content/public/browser/tracing_controller.h" | 15 #include "content/browser/tracing/tracing_controller_impl.h" |
13 #include "jni/TracingControllerAndroid_jni.h" | 16 #include "jni/TracingControllerAndroid_jni.h" |
14 | 17 |
| 18 using base::debug::TraceLog; |
| 19 |
15 namespace content { | 20 namespace content { |
| 21 namespace { |
| 22 |
| 23 const char kCategoryGroup[] = "Java"; |
| 24 |
| 25 // Adds the early Java trace events (i.e. before the native library was called) |
| 26 // to the TraceEvent log before the data gets written to disk. |
| 27 void OnDisableRecording(TraceLog* trace_log) { |
| 28 const unsigned char* const category_group_enabled = |
| 29 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(kCategoryGroup); |
| 30 |
| 31 TraceLog::GetInstance()->SetEnabled( |
| 32 base::debug::CategoryFilter(kCategoryGroup), TraceLog::RECORDING_MODE, |
| 33 base::debug::TraceOptions()); |
| 34 |
| 35 std::vector<base::android::EarlyTraceEvent> trace_events; |
| 36 base::android::GetAllEarlyTraceEvents(&trace_events); |
| 37 |
| 38 for (std::vector<base::android::EarlyTraceEvent>::const_iterator it = |
| 39 trace_events.begin(); |
| 40 it != trace_events.end(); ++it) { |
| 41 const base::android::EarlyTraceEvent& trace_event = *it; |
| 42 const struct { |
| 43 const char phase; |
| 44 const base::TimeTicks timestamp; |
| 45 const base::TimeTicks thread_time; |
| 46 } phases[] = { |
| 47 { TRACE_EVENT_PHASE_BEGIN, trace_event.begin_timestamp, |
| 48 trace_event.begin_thread_time }, |
| 49 { TRACE_EVENT_PHASE_END, trace_event.end_timestamp, |
| 50 trace_event.end_thread_time }, |
| 51 }; |
| 52 |
| 53 for (size_t i = 0; i < sizeof(phases) / sizeof(phases[0]); ++i) { |
| 54 trace_log->AddCustomTraceEvent( |
| 55 phases[i].phase, category_group_enabled, trace_event.name.c_str(), |
| 56 trace_event.thread_id, phases[i].timestamp, phases[i].thread_time); |
| 57 } |
| 58 } |
| 59 |
| 60 TraceLog::GetInstance()->SetDisabled(); |
| 61 } |
| 62 |
| 63 } // namespace |
16 | 64 |
17 static jlong Init(JNIEnv* env, jobject obj) { | 65 static jlong Init(JNIEnv* env, jobject obj) { |
18 TracingControllerAndroid* profiler = new TracingControllerAndroid(env, obj); | 66 TracingControllerAndroid* profiler = new TracingControllerAndroid(env, obj); |
19 return reinterpret_cast<intptr_t>(profiler); | 67 return reinterpret_cast<intptr_t>(profiler); |
20 } | 68 } |
21 | 69 |
22 TracingControllerAndroid::TracingControllerAndroid(JNIEnv* env, jobject obj) | 70 TracingControllerAndroid::TracingControllerAndroid(JNIEnv* env, jobject obj) |
23 : weak_java_object_(env, obj), | 71 : weak_java_object_(env, obj), |
24 weak_factory_(this) {} | 72 weak_factory_(this) {} |
25 | 73 |
26 TracingControllerAndroid::~TracingControllerAndroid() {} | 74 TracingControllerAndroid::~TracingControllerAndroid() {} |
27 | 75 |
28 void TracingControllerAndroid::Destroy(JNIEnv* env, jobject obj) { | 76 void TracingControllerAndroid::Destroy(JNIEnv* env, jobject obj) { |
29 delete this; | 77 delete this; |
30 } | 78 } |
31 | 79 |
| 80 void TracingControllerAndroid::SetupEarlyJavaTracing() { |
| 81 TracingControllerImpl::GetInstance()->SetDisableRecordingCallback( |
| 82 base::Bind(&OnDisableRecording)); |
| 83 } |
| 84 |
32 bool TracingControllerAndroid::StartTracing(JNIEnv* env, | 85 bool TracingControllerAndroid::StartTracing(JNIEnv* env, |
33 jobject obj, | 86 jobject obj, |
34 jstring jcategories, | 87 jstring jcategories, |
35 jstring jtraceoptions) { | 88 jstring jtraceoptions) { |
36 std::string categories = | 89 std::string categories = |
37 base::android::ConvertJavaStringToUTF8(env, jcategories); | 90 base::android::ConvertJavaStringToUTF8(env, jcategories); |
38 base::debug::TraceOptions trace_options; | 91 base::debug::TraceOptions trace_options; |
39 trace_options.SetFromString( | 92 trace_options.SetFromString( |
40 base::android::ConvertJavaStringToUTF8(env, jtraceoptions)); | 93 base::android::ConvertJavaStringToUTF8(env, jtraceoptions)); |
41 | 94 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 static jstring GetDefaultCategories(JNIEnv* env, jobject obj) { | 160 static jstring GetDefaultCategories(JNIEnv* env, jobject obj) { |
108 return base::android::ConvertUTF8ToJavaString(env, | 161 return base::android::ConvertUTF8ToJavaString(env, |
109 base::debug::CategoryFilter::kDefaultCategoryFilterString).Release(); | 162 base::debug::CategoryFilter::kDefaultCategoryFilterString).Release(); |
110 } | 163 } |
111 | 164 |
112 bool RegisterTracingControllerAndroid(JNIEnv* env) { | 165 bool RegisterTracingControllerAndroid(JNIEnv* env) { |
113 return RegisterNativesImpl(env); | 166 return RegisterNativesImpl(env); |
114 } | 167 } |
115 | 168 |
116 } // namespace content | 169 } // namespace content |
OLD | NEW |