目录

调用PerfGenius接口

以下示例将向您展示如何调用PerfGeniusApi.h头文件中的API函数。

  1. 编写API调用逻辑的入口函数PerfGeniusApi_Main()。
    // sample code entry
    void PerfGeniusApi_Main(JNIEnv *env) 
    {
        __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo",
                            "start Main()");
        // open the so source and init socket connection, then call the APIs
        OpenSoResource(env);
        // get the delete pointer
        deleteFunc();
    }
  2. 使用dlopen的方式获取libPerfgeniusApi.so的句柄,并通过该句柄结合dlsym方法调用GetPerfGeniusApiHandle(),创建PerfGeniusApi类的全局唯一对象实例指针,再通过该指针调用其他的API。
    /**
     * get acckit pointer by dlopen()
     * */
    // the global so handles
    void *libAccKit;
    // define the global singleton of PerfGenius api create pointer
    typedef PerfGeniusApi *(*BuildPerfGeniusApi)();
    // define the global singleton of PerfGenius api delete pointer
    typedef void (*DeletePerfGeniusApi)();
    // the pointer of api create
    BuildPerfGeniusApi buildFunc;
    // the pointer of api delete
    DeletePerfGeniusApi deleteFunc;
    // the pointer PerfGenius api
    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函数。
    // 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;
    }