文档管理中心
指南计算加速服务性能加速库调用PerfGenius接口

调用PerfGenius接口

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

  1. 编写API调用逻辑的入口函数PerfGeniusApi_Main()。
    收起
    自动换行
    深色代码主题
    复制
    1. // sample code entry
    2. void PerfGeniusApi_Main(JNIEnv *env)
    3. {
    4. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo",
    5. "start Main()");
    6. // open the so source and init socket connection, then call the APIs
    7. OpenSoResource(env);
    8. // get the delete pointer
    9. deleteFunc();
    10. }
  2. 使用dlopen的方式获取libPerfgeniusApi.so的句柄,并通过该句柄结合dlsym方法调用GetPerfGeniusApiHandle(),创建PerfGeniusApi类的全局唯一对象实例指针,再通过该指针调用其他的API。
    收起
    自动换行
    深色代码主题
    复制
    1. /**
    2. * get acckit pointer by dlopen()
    3. * */
    4. // the global so handles
    5. void *
      libAccKit
      ;
    6. // define the global singleton of PerfGenius api create pointer
    7. typedef PerfGeniusApi *(*BuildPerfGeniusApi)();
    8. // define the global singleton of PerfGenius api delete pointer
    9. typedef void (*DeletePerfGeniusApi)();
    10. // the pointer of api create
    11. BuildPerfGeniusApi
      buildFunc
      ;
    12. // the pointer of api delete
    13. DeletePerfGeniusApi
      deleteFunc
      ;
    14. // the pointer PerfGenius api
    15. PerfGeniusApi *
      kit
      ;
    16. void OpenSoResource(JNIEnv *
      env
      ) {
    17. libAccKit
      = dlopen("libPerfgeniusApi.so", RTLD_LAZY);
    18. if (!
      libAccKit
      ) {
    19. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo",
    20. "dlopen libPerfgeniusApi.so fail, close the handle");
    21. return;
    22. }
    23. buildFunc
      = reinterpret_cast<BuildPerfGeniusApi>(dlsym(
      libAccKit
      , "GetPerfGeniusApiHandle"));
    24. deleteFunc
      = reinterpret_cast<DeletePerfGeniusApi>(dlsym(
      libAccKit
      ,
    25. "DeletePerfGeniusApiHandle"));
    26. if (
      buildFunc
      == nullptr ||
      deleteFunc
      == nullptr) {
    27. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo",
    28. "dlsym GetPerfGeniusApiHandle|DeletePerfGeniusApiHandle fail, close the handle");
    29. dlclose(
      libAccKit
      );
    30. return;
    31. }
    32. kit
      = buildFunc();
    33. if (
      kit
      == nullptr) {
    34. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo",
    35. "init kit return null. maybe socket error or selinux error.\n");
    36. dlclose(
      libAccKit
      );
    37. return;
    38. }
    39. // call the method whatever you want,but you need get a JNIEnv pointer first
    40. auto
      ret
      = InitSocket(
      kit
      ,
      env
      );
    41. if (
      ret
      ) {
    42. deleteFunc();
    43. dlclose(
      libAccKit
      );
    44. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo",
    45. "init kit socket fail.\n");
    46. return;
    47. }
    48. }
  3. 调用环境初始化函数、系统事件回调函数以及其他API函数。
    收起
    自动换行
    深色代码主题
    复制
    1. // init socket and others.
    2. int InitSocket(PerfGeniusApi *
      kit
      , JNIEnv *
      env
      )
    3. {
    4. auto
      ret
      =
      kit
      ->Init(
      env
      );
    5. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "Init return: %d\n",
      ret
      );
    6. return
      ret
      ;
    7. }
    8. // get the version of PerfGenius server.
    9. int GetVersionInfo(PerfGeniusApi *
      kit
      )
    10. {
    11. string
      outStr
      ;
    12. auto
      ret
      =
      kit
      ->GetApiVersion(
      outStr
      );
    13. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "GetApiVersion return: %s\n",
    14. outStr
      .c_str());
    15. return
      ret
      ;
    16. }
    17. // set fps to system, higher or lower.
    18. int SetFps(PerfGeniusApi *
      kit
      , int
      fps
      )
    19. {
    20. auto
      ret
      =
      kit
      ->SetFrameRate(
      fps
      );
    21. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "SetFrameRate return %d\n",
      ret
      );
    22. return
      ret
      ;
    23. }
    24. // reset the fps to system.
    25. int ResetFps(PerfGeniusApi *
      kit
      )
    26. {
    27. auto
      ret
      =
      kit
      ->ResetFrameRate();
    28. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "ResetFrameRate return %d\n",
      ret
      );
    29. return
      ret
      ;
    30. }
    31. // get current fps.
    32. int GetCurrentFps(PerfGeniusApi *
      kit
      )
    33. {
    34. auto
      ret
      =
      kit
      ->GetCurrentFrameRate();
    35. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "GetCurrentFrameRate return %d: ",
    36. ret
      );
    37. return
      ret
      ;
    38. }
    39. // get supported fps list.
    40. int GetFpsList(PerfGeniusApi *
      kit
      )
    41. {
    42. vector<int>
      fpsList
      ;
    43. auto
      ret
      =
      kit
      ->GetSupportedFrameRate(
      fpsList
      );
    44. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "get fps list return %d: ",
      ret
      );
    45. for (const auto &
      fps
      :
      fpsList
      ) {
    46. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", " %d",
      fps
      );
    47. }
    48. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "\n");
    49. return
      ret
      ;
    50. }
    51. // set using scene, app, game etc.
    52. int SetUsingScence(PerfGeniusApi *
      kit
      , const char *
      sceneDescription
      )
    53. {
    54. string
      scene
      =
      sceneDescription
      ;
    55. auto
      ret
      = kit->SetScene(
      scene
      );
    56. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "SetScene return %d: ",
      ret
      );
    57. return
      ret
      ;
    58. }
    59. // get performance level of the system
    60. int GetPerformanceLevelMethod(PerfGeniusApi *
      kit
      )
    61. {
    62. auto
      ret
      =
      kit
      ->GetPerformanceLevel();
    63. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "GetPerformanceLevel return %d: ",
    64. ret
      );
    65. return
      ret
      ;
    66. }
    67. // add threads to reduce the pressure in heavy-load scenarios
    68. int AddKeyThreadsMethod(PerfGeniusApi *
      kit
      )
    69. {
    70. vector<int32_t>
      tids
      ;
    71. // add tids
    72. tids
      .push_back(10);
    73. auto
      ret
      =
      kit
      ->AddKeyThreads(
      tids
      );
    74. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "AddKeyThreads return %d: ",
      ret
      );
    75. return
      ret
      ;
    76. }
    77. // remove key threads
    78. int RemoveKeyThreadsMethod(PerfGeniusApi *
      kit
      )
    79. {
    80. vector<int32_t>
      tids
      ;
    81. // add tids
    82. tids
      .push_back(10);
    83. auto
      ret
      =
      kit
      ->RemoveKeyThreads(
      tids
      );
    84. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", "RemoveKeyThreads return %d: ",
    85. ret
      );
    86. return
      ret
      ;
    87. }
    88. // register system event call back listener
    89. int RegisterSystemEventMethod(PerfGeniusApi *
      kit
      )
    90. {
    91. string
      testStr
      = "user test str";
    92. auto
      ret
      =
      kit
      ->RegisterSystemEventCallback([&
      testStr
      ](int
      currLevel
      ) {
    93. printf("level changed %d, %s\n",
      currLevel
      ,
      testStr
      .c_str());
    94. });
    95. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo",
    96. "RegisterSystemEventCallback return %d: ",
      ret
      );
    97. return
      ret
      ;
    98. }
    99. // unregister system event
    100. int UnRegisterSystemEventMethod(PerfGeniusApi *
      kit
      )
    101. {
    102. auto
      ret
      =
      kit
      ->UnRegisterSystemEventCallback();
    103. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo",
    104. "UnRegisterSystemEventCallback return %d: ",
      ret
      );
    105. return
      ret
      ;
    106. }
    107. // register performance tracer listener
    108. int RegisterPerformanceTracerMethod(PerfGeniusApi *
      kit
      , int
      rate
      )
    109. {
    110. auto
      ret
      =
      kit
      ->RegisterPerformanceTracer(
      rate
      , [](vector<uint32_t> &
      data
      ) {
    111. if (
      data
      .empty()) {
    112. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo",
    113. "performance tracer died\n");
    114. return;
    115. }
    116. if (
      data
      .size() % 4 != 0) {
    117. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo",
    118. "performance tracer callback invalid data len %d\n",
    119. static_cast<int>(
      data
      .size()));
    120. return;
    121. }
    122. for (int
      i
      = 0;
      i
      < data.size();
      i
      += 4) {
    123. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo", " %u, %u, %u %u\n",
    124. data
      [i],
      data
      [i + 1],
      data
      [i + 2],
      data
      [i + 3]);
    125. });
    126. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo",
    127. "RegisterPerformanceTracer return %d: ",
      ret
      );
    128. return
      ret
      ;
    129. }
    130. // unregister performance tracer
    131. int UnRegisterPerformanceTracerMethod(PerfGeniusApi *
      kit
      )
    132. {
    133. auto
      ret
      =
      kit
      ->UnRegisterPerformanceTracer();
    134. __android_log_print(ANDROID_LOG_DEBUG, "perfgeniusdemo",
    135. "UnRegisterPerformanceTracer return %d: ",
      ret
      );
    136. return
      ret
      ;
    137. }
在 指南 中进行搜索
请输入您想要搜索的关键词