1. 介绍
简介
PerfGenius SDK提供系统状态变化主动上报、显示帧率设置、关键线程设置等接口,为开发者提供一条与硬件直接沟通的通道。开发者通过该通道,能了解系统状态并适时调整部分参数,使硬件响应更及时、更准确,在解决应用在部分场景的卡顿、掉帧问题的同时,避免了低负载场景的性能过剩,从而提升系统的性能功耗体验。开发者需要:
- 在开发者联盟注册开发者帐号,并在开发者管理台上创建应用。
- 使用Android Studio IDE创建Project,并配置接入PerfGenius SDK。
- 根据PerfGenius SDK开发指南,实现华为PerfGenius SDK API调用以及调试。
本篇Codelab将实现的内容
在本篇Codelab中,您将看到如何使用示例代码实现对华为PerfGenius SDK API的调用,通过示例代码您可以获取以下能力:- 系统状态监控
- 系统性能监控
- 系统刷新率调节
- 软件使用场景调节
- 系统线程负载调节
您将会学到什么
- 如何在华为开发者联盟上创建应用
- 如何集成华为PerfGenius SDK
- 如何调用华为PerfGenius SDK API
2. 您需要什么
硬件要求
- 开发计算机(台式机或笔记本电脑),运行Window10/Window7操作系统。
- 华为手机(带USB线),用于真机调试开发。
软件要求
- JDK 1.8及以上
- Android 7.0及以上
- Android Studio3.6及以上
- Gradle5.0及以上
需要的知识点
- Android基础开发能力
- Android Native C++ APK开发能力
- C/C++基础开发能力
3. 能力接入准备
集成HUAWEI HMS能力,需要完成以下准备工作:
- 注册成为开发者。
- 创建应用。
4. 集成PerfGenius SDK
- 创建Android Studio Project。
- 修改配置参数。
- 配置混淆脚本。
5. 代码开发
本小节您将尝试使用PerfGenius SDK的系统状态变化主动上报、显示帧率设置、关键线程设置等接口。
- 创建PerfGeniusApi_Main()作为入口函数,路径:src/main/cpp/source/Main.cpp。
// sample code entry void PerfGeniusApi_Main() { __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "start Main()"); // open the so source and init socket connection, then call the APIs OpenSoResource(); // get the delete pointer deleteFunc(); } - 以dlopen的方式获取libPerfgeniusApi.so的句柄,然后通过dlsym()方法调用GetPerfGeniusApiHandle(),创建PerfGeniusApi类的全局唯一对象实例指针,再通过该指针调用其他的API,路径:src/main/cpp/source/Main.cpp。
/** * get acckit pointer by dlopen() * */ void *libAccKit; typedef PerfGeniusApi *(*BuildPerfGeniusApi)(); typedef void (*DeletePerfGeniusApi)(); BuildPerfGeniusApi buildFunc; DeletePerfGeniusApi deleteFunc; PerfGeniusApi *kit; void OpenSoResource(JNIEnv *env) { libAccKit = dlopen("libPerfgeniusApi.so", RTLD_LAZY); if (!libAccKit) { __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "dlopen libPerfgeniusApi.so fail, close the handle"); return; } buildFunc = reinterpret_cast<BuildPerfGeniusApi>(dlsym(libAccKit, "GetPerfGeniusApiHandle")); deleteFunc = reinterpret_cast<DeletePerfGeniusApi>(dlsym(libAccKit, "DeletePerfGeniusApiHandle")); if (buildFunc == nullptr || deleteFunc == nullptr) { __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "dlsym GetPerfGeniusApiHandle|DeletePerfGeniusApiHandle fail, close the handle"); dlclose(libAccKit); return; } kit = buildFunc(); if (kit == nullptr) { __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "init kit return null. maybe socket error or selinux error.\n"); dlclose(libAccKit); return; } // call the method whatever you want,but you need get a JNIEnv pointer first auto ret = InitSocket(kit, env); if (ret) { deleteFunc(); dlclose(libAccKit); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "init kit socket fail.\n"); return; } } - 初始化函数、系统事件回调函数以及其他API函数调用方式,路径:src/main/cpp/source/Main.cpp,具体参考如下。
// init socket and others. int InitSocket(PerfGeniusApi *kit, JNIEnv *env) { auto ret = kit->Init(env); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "Init return: %d\n", ret); return ret; } // get the version of PerfGenius server. int GetVersionInfo(PerfGeniusApi *kit) { string outStr; auto ret = kit->GetApiVersion(outStr); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "GetApiVersion return: %s\n", outStr.c_str()); return ret; } // set fps to system, higher or lower. int SetFps(PerfGeniusApi *kit, int fps) { auto ret = kit->SetFrameRate(fps); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "SetFrameRate return %d\n", ret); return ret; } // reset the fps to system. int ResetFps(PerfGeniusApi *kit) { auto ret = kit->ResetFrameRate(); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "ResetFrameRate return %d\n", ret); return ret; } // get current fps. int GetCurrentFps(PerfGeniusApi *kit) { auto ret = kit->GetCurrentFrameRate(); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "GetCurrentFrameRate return %d: ", ret); return ret; } // get supported fps list. int GetFpsList(PerfGeniusApi *kit) { vector<int> fpsList; auto ret = kit->GetSupportedFrameRate(fpsList); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "get fps list return %d: ", ret); for (const auto &fps : fpsList) { __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", " %d", fps); } __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "\n"); return ret; } // set using scene, app, game etc. int SetUsingScence(PerfGeniusApi *kit, const char *sceneDescription) { string scene = sceneDescription; auto ret = kit->SetScene(scene); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "SetScene return %d: ", ret); return ret; } // get performance level of the system int GetPerformanceLevelMethod(PerfGeniusApi *kit) { auto ret = kit->GetPerformanceLevel(); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "GetPerformanceLevel return %d: ", ret); return ret; } // add threads to reduce the pressure in heavy-load scenarios int AddKeyThreadsMethod(PerfGeniusApi *kit) { vector<int32_t> tids; // add tids tids.push_back(10); auto ret = kit->AddKeyThreads(tids); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "AddKeyThreads return %d: ", ret); return ret; } // remove key threads int RemoveKeyThreadsMethod(PerfGeniusApi *kit) { vector<int32_t> tids; // add tids tids.push_back(10); auto ret = kit->RemoveKeyThreads(tids); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "RemoveKeyThreads return %d: ", ret); return ret; } // register system event call back listener int RegisterSystemEventMethod(PerfGeniusApi *kit) { string testStr = "user test str"; auto ret = kit->RegisterSystemEventCallback([&testStr](int currLevel) { printf("level changed %d, %s\n", currLevel, testStr.c_str()); }); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "RegisterSystemEventCallback return %d: ", ret); return ret; } // unregister system event int UnRegisterSystemEventMethod(PerfGeniusApi *kit) { auto ret = kit->UnRegisterSystemEventCallback(); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "UnRegisterSystemEventCallback return %d: ", ret); return ret; } // register performance tracer listener int RegisterPerformanceTracerMethod(PerfGeniusApi *kit, int rate) { auto ret = kit->RegisterPerformanceTracer(rate, [](vector<uint32_t> &data) { if (data.empty()) { __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "performance tracer died\n"); return; } if (data.size() % 4 != 0) { __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "performance tracer callback invalid data len %d\n", static_cast<int>(data.size())); return; } for (int i = 0; i < data.size(); i += 4) { __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", " %u, %u, %u %u\n", data[i], data[i + 1], data[i + 2], data[i + 3]); } }); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "RegisterPerformanceTracer return %d: ", ret); return ret; } // unregister performance tracer int UnRegisterPerformanceTracerMethod(PerfGeniusApi *kit) { auto ret = kit->UnRegisterPerformanceTracer(); __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "UnRegisterPerformanceTracer return %d: ", ret); return ret; }
剩余