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 17 #define LOG_TAG "GraphicsStatsService" 18 19 #include <jni.h> 20 #include <log/log.h> 21 #include <nativehelper/JNIHelp.h> 22 #include <ScopedPrimitiveArray.h> 23 #include <ScopedUtfChars.h> 24 #include <JankTracker.h> 25 #include <service/GraphicsStatsService.h> 26 27 namespace android { 28 29 using namespace android::uirenderer; 30 31 static jint getAshmemSize(JNIEnv*, jobject) { 32 return sizeof(ProfileData); 33 } 34 35 static jlong createDump(JNIEnv*, jobject, jint fd, jboolean isProto) { 36 GraphicsStatsService::Dump* dump = GraphicsStatsService::createDump(fd, isProto 37 ? GraphicsStatsService::DumpType::Protobuf : GraphicsStatsService::DumpType::Text); 38 return reinterpret_cast<jlong>(dump); 39 } 40 41 static void addToDump(JNIEnv* env, jobject, jlong dumpPtr, jstring jpath, jstring jpackage, 42 jint versionCode, jlong startTime, jlong endTime, jbyteArray jdata) { 43 std::string path; 44 const ProfileData* data = nullptr; 45 LOG_ALWAYS_FATAL_IF(jdata == nullptr && jpath == nullptr, "Path and data can't both be null"); 46 ScopedByteArrayRO buffer{env}; 47 if (jdata != nullptr) { 48 buffer.reset(jdata); 49 LOG_ALWAYS_FATAL_IF(buffer.size() != sizeof(ProfileData), 50 "Buffer size %zu doesn't match expected %zu!", buffer.size(), sizeof(ProfileData)); 51 data = reinterpret_cast<const ProfileData*>(buffer.get()); 52 } 53 if (jpath != nullptr) { 54 ScopedUtfChars pathChars(env, jpath); 55 LOG_ALWAYS_FATAL_IF(pathChars.size() <= 0 || !pathChars.c_str(), "Failed to get path chars"); 56 path.assign(pathChars.c_str(), pathChars.size()); 57 } 58 ScopedUtfChars packageChars(env, jpackage); 59 LOG_ALWAYS_FATAL_IF(packageChars.size() <= 0 || !packageChars.c_str(), "Failed to get path chars"); 60 GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr); 61 LOG_ALWAYS_FATAL_IF(!dump, "null passed for dump pointer"); 62 63 const std::string package(packageChars.c_str(), packageChars.size()); 64 GraphicsStatsService::addToDump(dump, path, package, versionCode, startTime, endTime, data); 65 } 66 67 static void addFileToDump(JNIEnv* env, jobject, jlong dumpPtr, jstring jpath) { 68 ScopedUtfChars pathChars(env, jpath); 69 LOG_ALWAYS_FATAL_IF(pathChars.size() <= 0 || !pathChars.c_str(), "Failed to get path chars"); 70 const std::string path(pathChars.c_str(), pathChars.size()); 71 GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr); 72 GraphicsStatsService::addToDump(dump, path); 73 } 74 75 static void finishDump(JNIEnv*, jobject, jlong dumpPtr) { 76 GraphicsStatsService::Dump* dump = reinterpret_cast<GraphicsStatsService::Dump*>(dumpPtr); 77 GraphicsStatsService::finishDump(dump); 78 } 79 80 static void saveBuffer(JNIEnv* env, jobject clazz, jstring jpath, jstring jpackage, 81 jint versionCode, jlong startTime, jlong endTime, jbyteArray jdata) { 82 ScopedByteArrayRO buffer(env, jdata); 83 LOG_ALWAYS_FATAL_IF(buffer.size() != sizeof(ProfileData), 84 "Buffer size %zu doesn't match expected %zu!", buffer.size(), sizeof(ProfileData)); 85 ScopedUtfChars pathChars(env, jpath); 86 LOG_ALWAYS_FATAL_IF(pathChars.size() <= 0 || !pathChars.c_str(), "Failed to get path chars"); 87 ScopedUtfChars packageChars(env, jpackage); 88 LOG_ALWAYS_FATAL_IF(packageChars.size() <= 0 || !packageChars.c_str(), "Failed to get path chars"); 89 90 const std::string path(pathChars.c_str(), pathChars.size()); 91 const std::string package(packageChars.c_str(), packageChars.size()); 92 const ProfileData* data = reinterpret_cast<const ProfileData*>(buffer.get()); 93 GraphicsStatsService::saveBuffer(path, package, versionCode, startTime, endTime, data); 94 } 95 96 static const JNINativeMethod sMethods[] = { 97 { "nGetAshmemSize", "()I", (void*) getAshmemSize }, 98 { "nCreateDump", "(IZ)J", (void*) createDump }, 99 { "nAddToDump", "(JLjava/lang/String;Ljava/lang/String;IJJ[B)V", (void*) addToDump }, 100 { "nAddToDump", "(JLjava/lang/String;)V", (void*) addFileToDump }, 101 { "nFinishDump", "(J)V", (void*) finishDump }, 102 { "nSaveBuffer", "(Ljava/lang/String;Ljava/lang/String;IJJ[B)V", (void*) saveBuffer }, 103 }; 104 105 int register_android_server_GraphicsStatsService(JNIEnv* env) 106 { 107 return jniRegisterNativeMethods(env, "com/android/server/GraphicsStatsService", 108 sMethods, NELEM(sMethods)); 109 } 110 111 } // namespace android