Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 17 | #include <pthread.h> |
| 18 | |
| 19 | #include <cstdio> |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 20 | #include <iostream> |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 21 | #include <mutex> |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 24 | #include "android-base/logging.h" |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 25 | #include "android-base/stringprintf.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 26 | #include "jni.h" |
Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame] | 27 | #include "jvmti.h" |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 28 | #include "scoped_local_ref.h" |
| 29 | #include "scoped_utf_chars.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 30 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 31 | // Test infrastructure |
| 32 | #include "jvmti_helper.h" |
| 33 | #include "test_env.h" |
| 34 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 35 | namespace art { |
| 36 | namespace Test904ObjectAllocation { |
| 37 | |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 38 | static JavaVM* vm; |
| 39 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 40 | static std::string GetClassName(JNIEnv* jni_env, jclass cls) { |
| 41 | ScopedLocalRef<jclass> class_class(jni_env, jni_env->GetObjectClass(cls)); |
| 42 | jmethodID mid = jni_env->GetMethodID(class_class.get(), "getName", "()Ljava/lang/String;"); |
| 43 | ScopedLocalRef<jstring> str( |
| 44 | jni_env, reinterpret_cast<jstring>(jni_env->CallObjectMethod(cls, mid))); |
| 45 | ScopedUtfChars utf_chars(jni_env, str.get()); |
| 46 | return utf_chars.c_str(); |
| 47 | } |
| 48 | |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 49 | template <typename T> |
| 50 | class ScopedGlobalRef { |
| 51 | public: |
| 52 | ScopedGlobalRef(JNIEnv* env, T obj) : obj_(env->NewGlobalRef(obj)) {} |
| 53 | ScopedGlobalRef(const ScopedGlobalRef<T>& src) noexcept |
| 54 | : obj_(GetEnv()->NewGlobalRef(src.obj_)) {} |
| 55 | ScopedGlobalRef(ScopedGlobalRef<T>&& src) noexcept : obj_(src.obj_) { |
| 56 | src.obj_ = nullptr; |
| 57 | } |
| 58 | |
| 59 | ~ScopedGlobalRef() { |
| 60 | GetEnv()->DeleteGlobalRef(obj_); |
| 61 | } |
| 62 | |
| 63 | T Get(JNIEnv* env) const { |
Alex Light | 9d3b5ec | 2019-06-20 09:47:01 -0700 | [diff] [blame] | 64 | return reinterpret_cast<T>(env->NewLocalRef(obj_)); |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | private: |
| 68 | JNIEnv* GetEnv() const { |
| 69 | JNIEnv* env = nullptr; |
| 70 | CHECK_EQ(vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6), 0); |
| 71 | return env; |
| 72 | } |
| 73 | |
| 74 | jobject obj_; |
| 75 | }; |
| 76 | |
| 77 | struct EventLog { |
Alex Light | 9d3b5ec | 2019-06-20 09:47:01 -0700 | [diff] [blame] | 78 | ScopedGlobalRef<jclass> object_klass; |
| 79 | ScopedGlobalRef<jclass> object_klass2; |
| 80 | jlong size; |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 81 | ScopedGlobalRef<jthread> thr_; |
| 82 | }; |
| 83 | |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 84 | static std::mutex gEventsMutex; |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 85 | static std::vector<EventLog> gEvents; |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 86 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 87 | static void JNICALL ObjectAllocated(jvmtiEnv* ti_env ATTRIBUTE_UNUSED, |
| 88 | JNIEnv* jni_env, |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 89 | jthread thread, |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 90 | jobject object, |
| 91 | jclass object_klass, |
| 92 | jlong size) { |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 93 | ScopedLocalRef<jclass> object_klass2(jni_env, jni_env->GetObjectClass(object)); |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 94 | std::lock_guard<std::mutex> guard(gEventsMutex); |
Alex Light | 9d3b5ec | 2019-06-20 09:47:01 -0700 | [diff] [blame] | 95 | gEvents.push_back({ScopedGlobalRef<jclass>(jni_env, object_klass), |
| 96 | ScopedGlobalRef<jclass>(jni_env, object_klass2.get()), |
| 97 | size, |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 98 | ScopedGlobalRef<jthread>(jni_env, thread)}); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 101 | extern "C" JNIEXPORT void JNICALL Java_art_Test904_setupObjectAllocCallback( |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 102 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jboolean enable) { |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 103 | env->GetJavaVM(&vm); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 104 | jvmtiEventCallbacks callbacks; |
| 105 | memset(&callbacks, 0, sizeof(jvmtiEventCallbacks)); |
Andreas Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 106 | callbacks.VMObjectAlloc = enable ? ObjectAllocated : nullptr; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 107 | |
| 108 | jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 109 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 112 | extern "C" JNIEXPORT void JNICALL Java_art_Test904_enableAllocationTracking( |
| 113 | JNIEnv* env, jclass, jthread thread, jboolean enable) { |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 114 | jvmtiError ret = jvmti_env->SetEventNotificationMode( |
| 115 | enable ? JVMTI_ENABLE : JVMTI_DISABLE, |
| 116 | JVMTI_EVENT_VM_OBJECT_ALLOC, |
| 117 | thread); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 118 | JvmtiErrorToException(env, jvmti_env, ret); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 121 | extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test904_getTrackingEventMessages( |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 122 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jobjectArray threads) { |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 123 | std::lock_guard<std::mutex> guard(gEventsMutex); |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 124 | std::vector<std::string> real_events; |
| 125 | std::vector<jthread> thread_lst; |
| 126 | jint nthreads = env->GetArrayLength(threads); |
| 127 | { |
| 128 | env->PushLocalFrame(nthreads + 1); |
| 129 | for (jint i = 0; i < nthreads; i++) { |
| 130 | thread_lst.push_back(reinterpret_cast<jthread>(env->GetObjectArrayElement(threads, i))); |
| 131 | } |
| 132 | for (const EventLog& ev : gEvents) { |
| 133 | ScopedLocalRef<jthread> thr(env, ev.thr_.Get(env)); |
| 134 | for (jthread req_thread : thread_lst) { |
| 135 | if (env->IsSameObject(req_thread, thr.get())) { |
Alex Light | 9d3b5ec | 2019-06-20 09:47:01 -0700 | [diff] [blame] | 136 | ScopedLocalRef<jclass> klass(env, ev.object_klass.Get(env)); |
| 137 | ScopedLocalRef<jclass> klass2(env, ev.object_klass2.Get(env)); |
| 138 | std::string object_klass_descriptor = GetClassName(env, klass.get()); |
| 139 | std::string object_klass_descriptor2 = GetClassName(env, klass2.get()); |
| 140 | std::string res(android::base::StringPrintf("ObjectAllocated type %s/%s size %zu", |
| 141 | object_klass_descriptor.c_str(), |
| 142 | object_klass_descriptor2.c_str(), |
| 143 | static_cast<size_t>(ev.size))); |
| 144 | real_events.push_back(res); |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | env->PopLocalFrame(nullptr); |
| 150 | } |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 151 | jobjectArray ret = CreateObjectArray(env, |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 152 | static_cast<jint>(real_events.size()), |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 153 | "java/lang/String", |
| 154 | [&](jint i) { |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 155 | return env->NewStringUTF(real_events[i].c_str()); |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 156 | }); |
| 157 | gEvents.clear(); |
| 158 | return ret; |
| 159 | } |
| 160 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 161 | } // namespace Test904ObjectAllocation |
| 162 | } // namespace art |