blob: 2ce392daeb87ad2279f72a4166121b5bc05593e3 [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
Derek Sollenberger5368eda2019-10-25 11:20:03 -040017#undef LOG_TAG
Stan Ilievc9043812020-02-03 16:57:09 -050018#define LOG_TAG "GraphicsStatsService"
19
20#include <JankTracker.h>
21#include <jni.h>
22#include <log/log.h>
23#include <nativehelper/JNIHelp.h>
24#include <nativehelper/ScopedPrimitiveArray.h>
25#include <nativehelper/ScopedUtfChars.h>
26#include <service/GraphicsStatsService.h>
27#include <stats_event.h>
28#include <stats_pull_atom_callback.h>
29#include <statslog.h>
30#include "core_jni_helpers.h"
31
32namespace android {
33
34using namespace android::uirenderer;
35
36static jint getAshmemSize(JNIEnv*, jobject) {
37 return sizeof(ProfileData);
38}
39
40static jlong createDump(JNIEnv*, jobject, jint fd, jboolean isProto) {
41 GraphicsStatsService::Dump* dump =
42 GraphicsStatsService::createDump(fd,
43 isProto ? GraphicsStatsService::DumpType::Protobuf
44 : GraphicsStatsService::DumpType::Text);
45 return reinterpret_cast<jlong>(dump);
46}
47
48static void addToDump(JNIEnv* env, jobject, jlong dumpPtr, jstring jpath, jstring jpackage,
49 jlong versionCode, jlong startTime, jlong endTime, jbyteArray jdata) {
50 std::string path;
51 const ProfileData* data = nullptr;
52 LOG_ALWAYS_FATAL_IF(jdata == nullptr && jpath == nullptr, "Path and data can't both be null");
53 ScopedByteArrayRO buffer{env};
54 if (jdata != nullptr) {
55 buffer.reset(jdata);
56 LOG_ALWAYS_FATAL_IF(buffer.size() != sizeof(ProfileData),
57 "Buffer size %zu doesn't match expected %zu!", buffer.size(),
58 sizeof(ProfileData));
59 data = reinterpret_cast<const ProfileData*>(buffer.get());
60 }
61 if (jpath != nullptr) {
62 ScopedUtfChars pathChars(env, jpath);
63 LOG_ALWAYS_FATAL_IF(pathChars.size() <= 0 || !pathChars.c_str(),
64 "Failed to get path chars");
65 path.assign(pathChars.c_str(), pathChars.size());
66 }
67 ScopedUtfChars packageChars(env, jpackage);
68 LOG_ALWAYS_FATAL_IF(packageChars.size() <= 0 || !packageChars.c_str(),
69 "Failed to get path chars");
70 GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr);
71 LOG_ALWAYS_FATAL_IF(!dump, "null passed for dump pointer");
72
73 const std::string package(packageChars.c_str(), packageChars.size());
74 GraphicsStatsService::addToDump(dump, path, package, versionCode, startTime, endTime, data);
75}
76
77static void addFileToDump(JNIEnv* env, jobject, jlong dumpPtr, jstring jpath) {
78 ScopedUtfChars pathChars(env, jpath);
79 LOG_ALWAYS_FATAL_IF(pathChars.size() <= 0 || !pathChars.c_str(), "Failed to get path chars");
80 const std::string path(pathChars.c_str(), pathChars.size());
81 GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr);
82 GraphicsStatsService::addToDump(dump, path);
83}
84
85static void finishDump(JNIEnv*, jobject, jlong dumpPtr) {
86 GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr);
87 GraphicsStatsService::finishDump(dump);
88}
89
90static void finishDumpInMemory(JNIEnv* env, jobject, jlong dumpPtr, jlong pulledData,
91 jboolean lastFullDay) {
92 GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr);
93 AStatsEventList* data = reinterpret_cast<AStatsEventList*>(pulledData);
94 GraphicsStatsService::finishDumpInMemory(dump, data, lastFullDay == JNI_TRUE);
95}
96
97static void saveBuffer(JNIEnv* env, jobject clazz, jstring jpath, jstring jpackage,
98 jlong versionCode, jlong startTime, jlong endTime, jbyteArray jdata) {
99 ScopedByteArrayRO buffer(env, jdata);
100 LOG_ALWAYS_FATAL_IF(buffer.size() != sizeof(ProfileData),
101 "Buffer size %zu doesn't match expected %zu!", buffer.size(),
102 sizeof(ProfileData));
103 ScopedUtfChars pathChars(env, jpath);
104 LOG_ALWAYS_FATAL_IF(pathChars.size() <= 0 || !pathChars.c_str(), "Failed to get path chars");
105 ScopedUtfChars packageChars(env, jpackage);
106 LOG_ALWAYS_FATAL_IF(packageChars.size() <= 0 || !packageChars.c_str(),
107 "Failed to get path chars");
108
109 const std::string path(pathChars.c_str(), pathChars.size());
110 const std::string package(packageChars.c_str(), packageChars.size());
111 const ProfileData* data = reinterpret_cast<const ProfileData*>(buffer.get());
112 GraphicsStatsService::saveBuffer(path, package, versionCode, startTime, endTime, data);
113}
114
115static jobject gGraphicsStatsServiceObject = nullptr;
116static jmethodID gGraphicsStatsService_pullGraphicsStatsMethodID;
117
118static JNIEnv* getJNIEnv() {
119 JavaVM* vm = AndroidRuntime::getJavaVM();
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.
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();
161 AStatsManager_PullAtomMetadata_setCoolDownNs(metadata, 10 * 1000000); // 10 milliseconds
162 AStatsManager_PullAtomMetadata_setTimeoutNs(metadata, 2 * NS_PER_SEC); // 2 seconds
163
164 AStatsManager_registerPullAtomCallback(android::util::GRAPHICS_STATS,
165 &graphicsStatsPullCallback, metadata, nullptr);
166
167 AStatsManager_PullAtomMetadata_release(metadata);
168}
169
170static void nativeDestructor(JNIEnv* env, jobject javaObject) {
171 AStatsManager_unregisterPullAtomCallback(android::util::GRAPHICS_STATS);
172 env->DeleteGlobalRef(gGraphicsStatsServiceObject);
173 gGraphicsStatsServiceObject = nullptr;
174}
175
176static const JNINativeMethod sMethods[] =
177 {{"nGetAshmemSize", "()I", (void*)getAshmemSize},
178 {"nCreateDump", "(IZ)J", (void*)createDump},
179 {"nAddToDump", "(JLjava/lang/String;Ljava/lang/String;JJJ[B)V", (void*)addToDump},
180 {"nAddToDump", "(JLjava/lang/String;)V", (void*)addFileToDump},
181 {"nFinishDump", "(J)V", (void*)finishDump},
182 {"nFinishDumpInMemory", "(JJZ)V", (void*)finishDumpInMemory},
183 {"nSaveBuffer", "(Ljava/lang/String;Ljava/lang/String;JJJ[B)V", (void*)saveBuffer},
184 {"nativeInit", "()V", (void*)nativeInit},
185 {"nativeDestructor", "()V", (void*)nativeDestructor}};
186
187int register_android_graphics_GraphicsStatsService(JNIEnv* env) {
188 jclass graphicsStatsService_class =
189 FindClassOrDie(env, "android/graphics/GraphicsStatsService");
190 gGraphicsStatsService_pullGraphicsStatsMethodID =
191 GetMethodIDOrDie(env, graphicsStatsService_class, "pullGraphicsStats", "(ZJ)V");
192 return jniRegisterNativeMethods(env, "android/graphics/GraphicsStatsService", sMethods,
193 NELEM(sMethods));
194}
195
196} // namespace android