| 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 "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/command_line.h" | |
| 10 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/logging.h" | 10 #include "base/logging.h" |
| 13 #include "content/browser/tracing/trace_subscriber_stdio.h" | 11 #include "content/public/browser/tracing_controller.h" |
| 14 #include "content/public/browser/trace_controller.h" | |
| 15 #include "jni/TracingControllerAndroid_jni.h" | 12 #include "jni/TracingControllerAndroid_jni.h" |
| 16 | 13 |
| 17 namespace content { | 14 namespace content { |
| 18 | 15 |
| 19 static jlong Init(JNIEnv* env, jobject obj) { | 16 static jlong Init(JNIEnv* env, jobject obj) { |
| 20 TracingControllerAndroid* profiler = new TracingControllerAndroid(env, obj); | 17 TracingControllerAndroid* profiler = new TracingControllerAndroid(env, obj); |
| 21 return reinterpret_cast<intptr_t>(profiler); | 18 return reinterpret_cast<intptr_t>(profiler); |
| 22 } | 19 } |
| 23 | 20 |
| 24 class TracingControllerAndroid::Subscriber | |
| 25 : public content::TraceSubscriberStdio { | |
| 26 public: | |
| 27 Subscriber(TracingControllerAndroid* profiler, const base::FilePath& path) | |
| 28 : TraceSubscriberStdio(path, FILE_TYPE_ARRAY, false), | |
| 29 profiler_(profiler) {} | |
| 30 | |
| 31 void set_profiler(TracingControllerAndroid* profiler) { | |
| 32 CHECK(!profiler_); | |
| 33 profiler_ = profiler; | |
| 34 } | |
| 35 | |
| 36 virtual void OnEndTracingComplete() OVERRIDE { | |
| 37 TraceSubscriberStdio::OnEndTracingComplete(); | |
| 38 profiler_->OnTracingStopped(); | |
| 39 } | |
| 40 | |
| 41 private: | |
| 42 TracingControllerAndroid* profiler_; | |
| 43 }; | |
| 44 | |
| 45 TracingControllerAndroid::TracingControllerAndroid(JNIEnv* env, jobject obj) | 21 TracingControllerAndroid::TracingControllerAndroid(JNIEnv* env, jobject obj) |
| 46 : weak_java_object_(env, obj) {} | 22 : weak_java_object_(env, obj), |
| 23 weak_factory_(this) {} |
| 47 | 24 |
| 48 TracingControllerAndroid::~TracingControllerAndroid() {} | 25 TracingControllerAndroid::~TracingControllerAndroid() {} |
| 49 | 26 |
| 50 void TracingControllerAndroid::Destroy(JNIEnv* env, jobject obj) { | 27 void TracingControllerAndroid::Destroy(JNIEnv* env, jobject obj) { |
| 51 delete this; | 28 delete this; |
| 52 } | 29 } |
| 53 | 30 |
| 54 bool TracingControllerAndroid::StartTracing(JNIEnv* env, | 31 bool TracingControllerAndroid::StartTracing(JNIEnv* env, |
| 55 jobject obj, | 32 jobject obj, |
| 56 jstring jfilename, | 33 jstring jfilename, |
| 57 jstring jcategories, | 34 jstring jcategories, |
| 58 jboolean record_continuously) { | 35 jboolean record_continuously) { |
| 59 if (subscriber_.get()) { | 36 file_path_ = base::FilePath( |
| 60 return false; | 37 base::android::ConvertJavaStringToUTF8(env, jfilename)); |
| 61 } | |
| 62 std::string filename = base::android::ConvertJavaStringToUTF8(env, jfilename); | |
| 63 std::string categories = | 38 std::string categories = |
| 64 base::android::ConvertJavaStringToUTF8(env, jcategories); | 39 base::android::ConvertJavaStringToUTF8(env, jcategories); |
| 65 subscriber_.reset(new Subscriber(this, base::FilePath(filename))); | 40 |
| 66 return TraceController::GetInstance()->BeginTracing( | 41 // This log is required by adb_profile_chrome.py. |
| 67 subscriber_.get(), | 42 LOG(WARNING) << "Logging performance trace to file: " << file_path_.value(); |
| 43 |
| 44 return TracingController::GetInstance()->EnableRecording( |
| 68 categories, | 45 categories, |
| 69 record_continuously ? base::debug::TraceLog::RECORD_CONTINUOUSLY | 46 record_continuously ? TracingController::RECORD_CONTINUOUSLY |
| 70 : base::debug::TraceLog::RECORD_UNTIL_FULL); | 47 : TracingController::DEFAULT_OPTIONS, |
| 48 TracingController::EnableRecordingDoneCallback()); |
| 71 } | 49 } |
| 72 | 50 |
| 73 void TracingControllerAndroid::StopTracing(JNIEnv* env, jobject obj) { | 51 void TracingControllerAndroid::StopTracing(JNIEnv* env, jobject obj) { |
| 74 if (!subscriber_.get()) { | 52 if (!TracingController::GetInstance()->DisableRecording( |
| 75 return; | 53 file_path_, |
| 76 } | 54 base::Bind(&TracingControllerAndroid::OnTracingStopped, |
| 77 TraceController* controller = TraceController::GetInstance(); | 55 weak_factory_.GetWeakPtr()))) { |
| 78 if (!controller->EndTracingAsync(subscriber_.get())) { | |
| 79 LOG(ERROR) << "EndTracingAsync failed, forcing an immediate stop"; | 56 LOG(ERROR) << "EndTracingAsync failed, forcing an immediate stop"; |
| 80 controller->CancelSubscriber(subscriber_.get()); | 57 OnTracingStopped(file_path_); |
| 81 OnTracingStopped(); | |
| 82 } | 58 } |
| 83 } | 59 } |
| 84 | 60 |
| 85 void TracingControllerAndroid::OnTracingStopped() { | 61 void TracingControllerAndroid::OnTracingStopped( |
| 62 const base::FilePath& file_path) { |
| 86 JNIEnv* env = base::android::AttachCurrentThread(); | 63 JNIEnv* env = base::android::AttachCurrentThread(); |
| 87 base::android::ScopedJavaLocalRef<jobject> obj = weak_java_object_.get(env); | 64 base::android::ScopedJavaLocalRef<jobject> obj = weak_java_object_.get(env); |
| 88 if (obj.obj()) { | 65 if (obj.obj()) |
| 89 Java_TracingControllerAndroid_onTracingStopped(env, obj.obj()); | 66 Java_TracingControllerAndroid_onTracingStopped(env, obj.obj()); |
| 90 } | |
| 91 subscriber_.reset(); | |
| 92 } | 67 } |
| 93 | 68 |
| 94 static jstring GetDefaultCategories(JNIEnv* env, jobject obj) { | 69 static jstring GetDefaultCategories(JNIEnv* env, jobject obj) { |
| 95 return base::android::ConvertUTF8ToJavaString(env, | 70 return base::android::ConvertUTF8ToJavaString(env, |
| 96 base::debug::CategoryFilter::kDefaultCategoryFilterString).Release(); | 71 base::debug::CategoryFilter::kDefaultCategoryFilterString).Release(); |
| 97 } | 72 } |
| 98 | 73 |
| 99 bool RegisterTracingControllerAndroid(JNIEnv* env) { | 74 bool RegisterTracingControllerAndroid(JNIEnv* env) { |
| 100 return RegisterNativesImpl(env); | 75 return RegisterNativesImpl(env); |
| 101 } | 76 } |
| 102 | 77 |
| 103 } // namespace content | 78 } // namespace content |
| OLD | NEW |