| OLD | NEW |
| 1 // Copyright (c) 2012 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/renderer_host/java/jni_helper.h" |
| 6 |
| 5 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 6 | |
| 7 #include "base/at_exit.h" | 8 #include "base/at_exit.h" |
| 8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 11 |
| 11 namespace base { | |
| 12 namespace android { | |
| 13 | |
| 14 namespace { | 12 namespace { |
| 15 | 13 |
| 16 const char kJavaLangObject[] = "java/lang/Object"; | 14 const char kJavaLangObject[] = "java/lang/Object"; |
| 17 const char kGetClass[] = "getClass"; | 15 const char kGetClass[] = "getClass"; |
| 18 const char kToString[] = "toString"; | 16 const char kToString[] = "toString"; |
| 19 const char kReturningJavaLangClass[] = "()Ljava/lang/Class;"; | 17 const char kReturningJavaLangClass[] = "()Ljava/lang/Class;"; |
| 20 const char kReturningJavaLangString[] = "()Ljava/lang/String;"; | 18 const char kReturningJavaLangString[] = "()Ljava/lang/String;"; |
| 21 | 19 |
| 22 const char* g_last_method; | 20 const char* g_last_method; |
| 23 const char* g_last_jni_signature; | 21 const char* g_last_jni_signature; |
| 24 jmethodID g_last_method_id; | 22 jmethodID g_last_method_id; |
| 25 | 23 |
| 26 const JNINativeInterface* g_previous_functions; | 24 const JNINativeInterface* g_previous_functions; |
| 27 | 25 |
| 28 jmethodID GetMethodIDWrapper(JNIEnv* env, jclass clazz, const char* method, | 26 jmethodID GetMethodIDWrapper(JNIEnv* env, jclass clazz, const char* method, |
| 29 const char* jni_signature) { | 27 const char* jni_signature) { |
| 30 g_last_method = method; | 28 g_last_method = method; |
| 31 g_last_jni_signature = jni_signature; | 29 g_last_jni_signature = jni_signature; |
| 32 g_last_method_id = g_previous_functions->GetMethodID(env, clazz, method, | 30 g_last_method_id = g_previous_functions->GetMethodID(env, clazz, method, |
| 33 jni_signature); | 31 jni_signature); |
| 34 return g_last_method_id; | 32 return g_last_method_id; |
| 35 } | 33 } |
| 36 | 34 |
| 37 } // namespace | 35 } // namespace |
| 38 | 36 |
| 39 class JNIAndroidTest : public testing::Test { | 37 class JNIAndroidTest : public testing::Test { |
| 40 protected: | 38 protected: |
| 41 virtual void SetUp() { | 39 virtual void SetUp() { |
| 42 JNIEnv* env = AttachCurrentThread(); | 40 JNIEnv* env = base::android::AttachCurrentThread(); |
| 43 g_previous_functions = env->functions; | 41 g_previous_functions = env->functions; |
| 44 hooked_functions = *g_previous_functions; | 42 hooked_functions = *g_previous_functions; |
| 45 env->functions = &hooked_functions; | 43 env->functions = &hooked_functions; |
| 46 hooked_functions.GetMethodID = &GetMethodIDWrapper; | 44 hooked_functions.GetMethodID = &GetMethodIDWrapper; |
| 47 } | 45 } |
| 48 | 46 |
| 49 virtual void TearDown() { | 47 virtual void TearDown() { |
| 50 JNIEnv* env = AttachCurrentThread(); | 48 JNIEnv* env = base::android::AttachCurrentThread(); |
| 51 env->functions = g_previous_functions; | 49 env->functions = g_previous_functions; |
| 52 Reset(); | 50 Reset(); |
| 53 } | 51 } |
| 54 | 52 |
| 55 void Reset() { | 53 void Reset() { |
| 56 g_last_method = 0; | 54 g_last_method = 0; |
| 57 g_last_jni_signature = 0; | 55 g_last_jni_signature = 0; |
| 58 g_last_method_id = NULL; | 56 g_last_method_id = NULL; |
| 59 } | 57 } |
| 60 // Needed to cleanup the cached method map in the implementation between | 58 // Needed to cleanup the cached method map in the implementation between |
| 61 // runs (e.g. if using --gtest_repeat) | 59 // runs (e.g. if using --gtest_repeat) |
| 62 base::ShadowingAtExitManager exit_manager; | 60 base::ShadowingAtExitManager exit_manager; |
| 63 // From JellyBean release, the instance of this struct provided in JNIEnv is | 61 // From JellyBean release, the instance of this struct provided in JNIEnv is |
| 64 // read-only, so we deep copy it to allow individual functions to be hooked. | 62 // read-only, so we deep copy it to allow individual functions to be hooked. |
| 65 JNINativeInterface hooked_functions; | 63 JNINativeInterface hooked_functions; |
| 66 }; | 64 }; |
| 67 | 65 |
| 68 TEST_F(JNIAndroidTest, GetMethodIDFromClassNameCaching) { | 66 TEST_F(JNIAndroidTest, GetMethodIDFromClassNameCaching) { |
| 69 JNIEnv* env = AttachCurrentThread(); | 67 JNIEnv* env = base::android::AttachCurrentThread(); |
| 70 | 68 |
| 71 Reset(); | 69 Reset(); |
| 72 jmethodID id1 = GetMethodIDFromClassName(env, kJavaLangObject, kGetClass, | 70 jmethodID id1 = GetMethodIDFromClassName(env, kJavaLangObject, kGetClass, |
| 73 kReturningJavaLangClass); | 71 kReturningJavaLangClass); |
| 74 EXPECT_STREQ(kGetClass, g_last_method); | 72 EXPECT_STREQ(kGetClass, g_last_method); |
| 75 EXPECT_STREQ(kReturningJavaLangClass, g_last_jni_signature); | 73 EXPECT_STREQ(kReturningJavaLangClass, g_last_jni_signature); |
| 76 EXPECT_EQ(g_last_method_id, id1); | 74 EXPECT_EQ(g_last_method_id, id1); |
| 77 | 75 |
| 78 Reset(); | 76 Reset(); |
| 79 jmethodID id2 = GetMethodIDFromClassName(env, kJavaLangObject, kGetClass, | 77 jmethodID id2 = GetMethodIDFromClassName(env, kJavaLangObject, kGetClass, |
| 80 kReturningJavaLangClass); | 78 kReturningJavaLangClass); |
| 81 EXPECT_STREQ(0, g_last_method); | 79 EXPECT_STREQ(0, g_last_method); |
| 82 EXPECT_STREQ(0, g_last_jni_signature); | 80 EXPECT_STREQ(0, g_last_jni_signature); |
| 83 EXPECT_EQ(NULL, g_last_method_id); | 81 EXPECT_EQ(NULL, g_last_method_id); |
| 84 EXPECT_EQ(id1, id2); | 82 EXPECT_EQ(id1, id2); |
| 85 | 83 |
| 86 Reset(); | 84 Reset(); |
| 87 jmethodID id3 = GetMethodIDFromClassName(env, kJavaLangObject, kToString, | 85 jmethodID id3 = GetMethodIDFromClassName(env, kJavaLangObject, kToString, |
| 88 kReturningJavaLangString); | 86 kReturningJavaLangString); |
| 89 EXPECT_STREQ(kToString, g_last_method); | 87 EXPECT_STREQ(kToString, g_last_method); |
| 90 EXPECT_STREQ(kReturningJavaLangString, g_last_jni_signature); | 88 EXPECT_STREQ(kReturningJavaLangString, g_last_jni_signature); |
| 91 EXPECT_EQ(g_last_method_id, id3); | 89 EXPECT_EQ(g_last_method_id, id3); |
| 92 } | 90 } |
| 93 | |
| 94 namespace { | |
| 95 | |
| 96 base::subtle::AtomicWord g_atomic_id = 0; | |
| 97 int LazyMethodIDCall(JNIEnv* env, jclass clazz, int p) { | |
| 98 jmethodID id = base::android::MethodID::LazyGet< | |
| 99 base::android::MethodID::TYPE_STATIC>( | |
| 100 env, clazz, | |
| 101 "abs", | |
| 102 "(I)I", | |
| 103 &g_atomic_id); | |
| 104 | |
| 105 return env->CallStaticIntMethod(clazz, id, p); | |
| 106 } | |
| 107 | |
| 108 int MethodIDCall(JNIEnv* env, jclass clazz, jmethodID id, int p) { | |
| 109 return env->CallStaticIntMethod(clazz, id, p); | |
| 110 } | |
| 111 | |
| 112 } // namespace | |
| 113 | |
| 114 TEST(JNIAndroidMicrobenchmark, MethodId) { | |
| 115 JNIEnv* env = AttachCurrentThread(); | |
| 116 ScopedJavaLocalRef<jclass> clazz(GetClass(env, "java/lang/Math")); | |
| 117 base::Time start_lazy = base::Time::Now(); | |
| 118 int o = 0; | |
| 119 for (int i = 0; i < 1024; ++i) | |
| 120 o += LazyMethodIDCall(env, clazz.obj(), i); | |
| 121 base::Time end_lazy = base::Time::Now(); | |
| 122 | |
| 123 jmethodID id = reinterpret_cast<jmethodID>(g_atomic_id); | |
| 124 base::Time start = base::Time::Now(); | |
| 125 for (int i = 0; i < 1024; ++i) | |
| 126 o += MethodIDCall(env, clazz.obj(), id, i); | |
| 127 base::Time end = base::Time::Now(); | |
| 128 | |
| 129 // On a Galaxy Nexus, results were in the range of: | |
| 130 // JNI LazyMethodIDCall (us) 1984 | |
| 131 // JNI MethodIDCall (us) 1861 | |
| 132 LOG(ERROR) << "JNI LazyMethodIDCall (us) " << | |
| 133 base::TimeDelta(end_lazy - start_lazy).InMicroseconds(); | |
| 134 LOG(ERROR) << "JNI MethodIDCall (us) " << | |
| 135 base::TimeDelta(end - start).InMicroseconds(); | |
| 136 LOG(ERROR) << "JNI " << o; | |
| 137 } | |
| 138 | |
| 139 | |
| 140 } // namespace android | |
| 141 } // namespace base | |
| OLD | NEW |