Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 17 | #include <JankTracker.h> |
Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 18 | #include <log/log.h> |
Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 19 | #include <nativehelper/ScopedPrimitiveArray.h> |
| 20 | #include <nativehelper/ScopedUtfChars.h> |
| 21 | #include <service/GraphicsStatsService.h> |
| 22 | #include <stats_event.h> |
| 23 | #include <stats_pull_atom_callback.h> |
Tej Singh | 78f65b6 | 2021-03-18 16:19:55 -0700 | [diff] [blame] | 24 | #include <statslog_hwui.h> |
Derek Sollenberger | 2173ea2 | 2020-02-19 15:37:29 -0500 | [diff] [blame] | 25 | |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 26 | #include "GraphicsJNI.h" |
Tej Singh | 78f65b6 | 2021-03-18 16:19:55 -0700 | [diff] [blame] | 27 | #include "android/graphics/jni_runtime.h" |
Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | using namespace android::uirenderer; |
| 32 | |
| 33 | static jint getAshmemSize(JNIEnv*, jobject) { |
| 34 | return sizeof(ProfileData); |
| 35 | } |
| 36 | |
| 37 | static jlong createDump(JNIEnv*, jobject, jint fd, jboolean isProto) { |
| 38 | GraphicsStatsService::Dump* dump = |
| 39 | GraphicsStatsService::createDump(fd, |
| 40 | isProto ? GraphicsStatsService::DumpType::Protobuf |
| 41 | : GraphicsStatsService::DumpType::Text); |
| 42 | return reinterpret_cast<jlong>(dump); |
| 43 | } |
| 44 | |
Alec Mouri | 04b0d81 | 2024-09-20 16:15:55 +0000 | [diff] [blame] | 45 | static void addToDump(JNIEnv* env, jobject, jlong dumpPtr, jstring jpath, jint uid, |
| 46 | jstring jpackage, jlong versionCode, jlong startTime, jlong endTime, |
| 47 | jbyteArray jdata) { |
Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 48 | std::string path; |
| 49 | const ProfileData* data = nullptr; |
| 50 | LOG_ALWAYS_FATAL_IF(jdata == nullptr && jpath == nullptr, "Path and data can't both be null"); |
| 51 | ScopedByteArrayRO buffer{env}; |
| 52 | if (jdata != nullptr) { |
| 53 | buffer.reset(jdata); |
| 54 | LOG_ALWAYS_FATAL_IF(buffer.size() != sizeof(ProfileData), |
| 55 | "Buffer size %zu doesn't match expected %zu!", buffer.size(), |
| 56 | sizeof(ProfileData)); |
| 57 | data = reinterpret_cast<const ProfileData*>(buffer.get()); |
| 58 | } |
| 59 | if (jpath != nullptr) { |
| 60 | ScopedUtfChars pathChars(env, jpath); |
| 61 | LOG_ALWAYS_FATAL_IF(pathChars.size() <= 0 || !pathChars.c_str(), |
| 62 | "Failed to get path chars"); |
| 63 | path.assign(pathChars.c_str(), pathChars.size()); |
| 64 | } |
| 65 | ScopedUtfChars packageChars(env, jpackage); |
| 66 | LOG_ALWAYS_FATAL_IF(packageChars.size() <= 0 || !packageChars.c_str(), |
| 67 | "Failed to get path chars"); |
| 68 | GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr); |
| 69 | LOG_ALWAYS_FATAL_IF(!dump, "null passed for dump pointer"); |
| 70 | |
| 71 | const std::string package(packageChars.c_str(), packageChars.size()); |
Alec Mouri | 04b0d81 | 2024-09-20 16:15:55 +0000 | [diff] [blame] | 72 | GraphicsStatsService::addToDump(dump, path, static_cast<uid_t>(uid), package, versionCode, |
| 73 | startTime, endTime, data); |
Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void addFileToDump(JNIEnv* env, jobject, jlong dumpPtr, jstring jpath) { |
| 77 | ScopedUtfChars pathChars(env, jpath); |
| 78 | LOG_ALWAYS_FATAL_IF(pathChars.size() <= 0 || !pathChars.c_str(), "Failed to get path chars"); |
| 79 | const std::string path(pathChars.c_str(), pathChars.size()); |
| 80 | GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr); |
| 81 | GraphicsStatsService::addToDump(dump, path); |
| 82 | } |
| 83 | |
| 84 | static void finishDump(JNIEnv*, jobject, jlong dumpPtr) { |
| 85 | GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr); |
| 86 | GraphicsStatsService::finishDump(dump); |
| 87 | } |
| 88 | |
| 89 | static void finishDumpInMemory(JNIEnv* env, jobject, jlong dumpPtr, jlong pulledData, |
| 90 | jboolean lastFullDay) { |
| 91 | GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr); |
| 92 | AStatsEventList* data = reinterpret_cast<AStatsEventList*>(pulledData); |
| 93 | GraphicsStatsService::finishDumpInMemory(dump, data, lastFullDay == JNI_TRUE); |
| 94 | } |
| 95 | |
Alec Mouri | 04b0d81 | 2024-09-20 16:15:55 +0000 | [diff] [blame] | 96 | static void saveBuffer(JNIEnv* env, jobject clazz, jstring jpath, jint uid, jstring jpackage, |
Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 97 | jlong versionCode, jlong startTime, jlong endTime, jbyteArray jdata) { |
| 98 | ScopedByteArrayRO buffer(env, jdata); |
| 99 | LOG_ALWAYS_FATAL_IF(buffer.size() != sizeof(ProfileData), |
| 100 | "Buffer size %zu doesn't match expected %zu!", buffer.size(), |
| 101 | sizeof(ProfileData)); |
| 102 | ScopedUtfChars pathChars(env, jpath); |
| 103 | LOG_ALWAYS_FATAL_IF(pathChars.size() <= 0 || !pathChars.c_str(), "Failed to get path chars"); |
| 104 | ScopedUtfChars packageChars(env, jpackage); |
| 105 | LOG_ALWAYS_FATAL_IF(packageChars.size() <= 0 || !packageChars.c_str(), |
| 106 | "Failed to get path chars"); |
| 107 | |
| 108 | const std::string path(pathChars.c_str(), pathChars.size()); |
| 109 | const std::string package(packageChars.c_str(), packageChars.size()); |
| 110 | const ProfileData* data = reinterpret_cast<const ProfileData*>(buffer.get()); |
Alec Mouri | 04b0d81 | 2024-09-20 16:15:55 +0000 | [diff] [blame] | 111 | GraphicsStatsService::saveBuffer(path, static_cast<uid_t>(uid), package, versionCode, startTime, |
| 112 | endTime, data); |
Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static jobject gGraphicsStatsServiceObject = nullptr; |
| 116 | static jmethodID gGraphicsStatsService_pullGraphicsStatsMethodID; |
| 117 | |
| 118 | static JNIEnv* getJNIEnv() { |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 119 | JavaVM* vm = GraphicsJNI::getJavaVM(); |
Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 120 | JNIEnv* env = nullptr; |
| 121 | if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 122 | if (vm->AttachCurrentThreadAsDaemon(&env, nullptr) != JNI_OK) { |
| 123 | LOG_ALWAYS_FATAL("Failed to AttachCurrentThread!"); |
| 124 | } |
| 125 | } |
| 126 | return env; |
| 127 | } |
| 128 | |
| 129 | // graphicsStatsPullCallback is invoked by statsd service to pull GRAPHICS_STATS atom. |
| 130 | static AStatsManager_PullAtomCallbackReturn graphicsStatsPullCallback(int32_t atom_tag, |
| 131 | AStatsEventList* data, |
| 132 | void* cookie) { |
| 133 | JNIEnv* env = getJNIEnv(); |
| 134 | if (!env) { |
| 135 | return false; |
| 136 | } |
| 137 | if (gGraphicsStatsServiceObject == nullptr) { |
| 138 | ALOGE("Failed to get graphicsstats service"); |
| 139 | return AStatsManager_PULL_SKIP; |
| 140 | } |
| 141 | |
| 142 | for (bool lastFullDay : {true, false}) { |
| 143 | env->CallVoidMethod(gGraphicsStatsServiceObject, |
| 144 | gGraphicsStatsService_pullGraphicsStatsMethodID, |
| 145 | (jboolean)(lastFullDay ? JNI_TRUE : JNI_FALSE), |
| 146 | reinterpret_cast<jlong>(data)); |
| 147 | if (env->ExceptionCheck()) { |
| 148 | env->ExceptionDescribe(); |
| 149 | env->ExceptionClear(); |
| 150 | ALOGE("Failed to invoke graphicsstats service"); |
| 151 | return AStatsManager_PULL_SKIP; |
| 152 | } |
| 153 | } |
| 154 | return AStatsManager_PULL_SUCCESS; |
| 155 | } |
| 156 | |
| 157 | // Register a puller for GRAPHICS_STATS atom with the statsd service. |
| 158 | static void nativeInit(JNIEnv* env, jobject javaObject) { |
| 159 | gGraphicsStatsServiceObject = env->NewGlobalRef(javaObject); |
| 160 | AStatsManager_PullAtomMetadata* metadata = AStatsManager_PullAtomMetadata_obtain(); |
Tej Singh | 73597dc | 2020-03-13 18:42:40 -0700 | [diff] [blame] | 161 | AStatsManager_PullAtomMetadata_setCoolDownMillis(metadata, 10); // 10 milliseconds |
| 162 | AStatsManager_PullAtomMetadata_setTimeoutMillis(metadata, 2 * MS_PER_SEC); // 2 seconds |
Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 163 | |
Tej Singh | 78f65b6 | 2021-03-18 16:19:55 -0700 | [diff] [blame] | 164 | AStatsManager_setPullAtomCallback(stats::GRAPHICS_STATS, metadata, &graphicsStatsPullCallback, |
| 165 | nullptr); |
Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 166 | |
| 167 | AStatsManager_PullAtomMetadata_release(metadata); |
| 168 | } |
| 169 | |
| 170 | static void nativeDestructor(JNIEnv* env, jobject javaObject) { |
Tej Singh | 78f65b6 | 2021-03-18 16:19:55 -0700 | [diff] [blame] | 171 | AStatsManager_clearPullAtomCallback(stats::GRAPHICS_STATS); |
Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 172 | env->DeleteGlobalRef(gGraphicsStatsServiceObject); |
| 173 | gGraphicsStatsServiceObject = nullptr; |
| 174 | } |
| 175 | |
Derek Sollenberger | 2173ea2 | 2020-02-19 15:37:29 -0500 | [diff] [blame] | 176 | } // namespace android |
| 177 | using namespace android; |
| 178 | |
Alec Mouri | 04b0d81 | 2024-09-20 16:15:55 +0000 | [diff] [blame] | 179 | static const JNINativeMethod sMethods[] = { |
| 180 | {"nGetAshmemSize", "()I", (void*)getAshmemSize}, |
| 181 | {"nCreateDump", "(IZ)J", (void*)createDump}, |
| 182 | {"nAddToDump", "(JLjava/lang/String;ILjava/lang/String;JJJ[B)V", (void*)addToDump}, |
| 183 | {"nAddToDump", "(JLjava/lang/String;)V", (void*)addFileToDump}, |
| 184 | {"nFinishDump", "(J)V", (void*)finishDump}, |
| 185 | {"nFinishDumpInMemory", "(JJZ)V", (void*)finishDumpInMemory}, |
| 186 | {"nSaveBuffer", "(Ljava/lang/String;ILjava/lang/String;JJJ[B)V", (void*)saveBuffer}, |
| 187 | {"nativeInit", "()V", (void*)nativeInit}, |
| 188 | {"nativeDestructor", "()V", (void*)nativeDestructor}}; |
Stan Iliev | c904381 | 2020-02-03 16:57:09 -0500 | [diff] [blame] | 189 | |
| 190 | int register_android_graphics_GraphicsStatsService(JNIEnv* env) { |
| 191 | jclass graphicsStatsService_class = |
| 192 | FindClassOrDie(env, "android/graphics/GraphicsStatsService"); |
| 193 | gGraphicsStatsService_pullGraphicsStatsMethodID = |
| 194 | GetMethodIDOrDie(env, graphicsStatsService_class, "pullGraphicsStats", "(ZJ)V"); |
| 195 | return jniRegisterNativeMethods(env, "android/graphics/GraphicsStatsService", sMethods, |
| 196 | NELEM(sMethods)); |
| 197 | } |