blob: 80a8ae1d8c17314ec7a0e92fb1f605d6ceba5240 [file] [log] [blame]
Stan Ilievc9043812020-02-03 16:57:09 -05001/*
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 Ilievc9043812020-02-03 16:57:09 -050017#include <JankTracker.h>
Stan Ilievc9043812020-02-03 16:57:09 -050018#include <log/log.h>
Stan Ilievc9043812020-02-03 16:57:09 -050019#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 Singh78f65b62021-03-18 16:19:55 -070024#include <statslog_hwui.h>
Derek Sollenberger2173ea22020-02-19 15:37:29 -050025
Derek Sollenbergerc5882c42019-10-25 11:11:32 -040026#include "GraphicsJNI.h"
Tej Singh78f65b62021-03-18 16:19:55 -070027#include "android/graphics/jni_runtime.h"
Stan Ilievc9043812020-02-03 16:57:09 -050028
29namespace android {
30
31using namespace android::uirenderer;
32
33static jint getAshmemSize(JNIEnv*, jobject) {
34 return sizeof(ProfileData);
35}
36
37static 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 Mouri04b0d812024-09-20 16:15:55 +000045static void addToDump(JNIEnv* env, jobject, jlong dumpPtr, jstring jpath, jint uid,
46 jstring jpackage, jlong versionCode, jlong startTime, jlong endTime,
47 jbyteArray jdata) {
Stan Ilievc9043812020-02-03 16:57:09 -050048 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 Mouri04b0d812024-09-20 16:15:55 +000072 GraphicsStatsService::addToDump(dump, path, static_cast<uid_t>(uid), package, versionCode,
73 startTime, endTime, data);
Stan Ilievc9043812020-02-03 16:57:09 -050074}
75
76static 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
84static void finishDump(JNIEnv*, jobject, jlong dumpPtr) {
85 GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr);
86 GraphicsStatsService::finishDump(dump);
87}
88
89static 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 Mouri04b0d812024-09-20 16:15:55 +000096static void saveBuffer(JNIEnv* env, jobject clazz, jstring jpath, jint uid, jstring jpackage,
Stan Ilievc9043812020-02-03 16:57:09 -050097 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 Mouri04b0d812024-09-20 16:15:55 +0000111 GraphicsStatsService::saveBuffer(path, static_cast<uid_t>(uid), package, versionCode, startTime,
112 endTime, data);
Stan Ilievc9043812020-02-03 16:57:09 -0500113}
114
115static jobject gGraphicsStatsServiceObject = nullptr;
116static jmethodID gGraphicsStatsService_pullGraphicsStatsMethodID;
117
118static JNIEnv* getJNIEnv() {
Derek Sollenbergerc5882c42019-10-25 11:11:32 -0400119 JavaVM* vm = GraphicsJNI::getJavaVM();
Stan Ilievc9043812020-02-03 16:57:09 -0500120 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.
130static 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.
158static void nativeInit(JNIEnv* env, jobject javaObject) {
159 gGraphicsStatsServiceObject = env->NewGlobalRef(javaObject);
160 AStatsManager_PullAtomMetadata* metadata = AStatsManager_PullAtomMetadata_obtain();
Tej Singh73597dc2020-03-13 18:42:40 -0700161 AStatsManager_PullAtomMetadata_setCoolDownMillis(metadata, 10); // 10 milliseconds
162 AStatsManager_PullAtomMetadata_setTimeoutMillis(metadata, 2 * MS_PER_SEC); // 2 seconds
Stan Ilievc9043812020-02-03 16:57:09 -0500163
Tej Singh78f65b62021-03-18 16:19:55 -0700164 AStatsManager_setPullAtomCallback(stats::GRAPHICS_STATS, metadata, &graphicsStatsPullCallback,
165 nullptr);
Stan Ilievc9043812020-02-03 16:57:09 -0500166
167 AStatsManager_PullAtomMetadata_release(metadata);
168}
169
170static void nativeDestructor(JNIEnv* env, jobject javaObject) {
Tej Singh78f65b62021-03-18 16:19:55 -0700171 AStatsManager_clearPullAtomCallback(stats::GRAPHICS_STATS);
Stan Ilievc9043812020-02-03 16:57:09 -0500172 env->DeleteGlobalRef(gGraphicsStatsServiceObject);
173 gGraphicsStatsServiceObject = nullptr;
174}
175
Derek Sollenberger2173ea22020-02-19 15:37:29 -0500176} // namespace android
177using namespace android;
178
Alec Mouri04b0d812024-09-20 16:15:55 +0000179static 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 Ilievc9043812020-02-03 16:57:09 -0500189
190int 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}