简介

PerfGenius SDK提供系统状态变化主动上报、显示帧率设置、关键线程设置等接口,为开发者提供一条与硬件直接沟通的通道。开发者通过该通道,能了解系统状态并适时调整部分参数,使硬件响应更及时、更准确,在解决应用在部分场景的卡顿、掉帧问题的同时,避免了低负载场景的性能过剩,从而提升系统的性能功耗体验。
开发者需要:

本篇Codelab将实现的内容

在本篇Codelab中,您将看到如何使用示例代码实现对华为PerfGenius SDK API的调用,通过示例代码您可以获取以下能力:

您将会学到什么

硬件要求

软件要求

需要的知识点

集成HUAWEI HMS能力,需要完成以下准备工作:

  1. 注册成为开发者。
  2. 创建应用。
具体操作,请参见《Accelerate Kit开发者指南》中PerfGenius章节详细说明来完成。
  1. 创建Android Studio Project。
  2. 修改配置参数。
  3. 配置混淆脚本。

本小节您将尝试使用PerfGenius SDK的系统状态变化主动上报、显示帧率设置、关键线程设置等接口。

  1. 创建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(); }
  2. 以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; } }
  3. 初始化函数、系统事件回调函数以及其他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; }

干得好,您已经成功完成了Codelab并学到了:

您可以阅读下面链接,了解更多相关的信息。

源码下载

已复制代码