# HWCPipe **Repository Path**: archermind-ti/hwcpipe ## Basic Information - **Project Name**: HWCPipe - **Description**: HWCPipe是一个arm平台获取CPU和GPU硬件计数器的项目 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 5 - **Forks**: 2 - **Created**: 2021-04-15 - **Last Updated**: 2024-07-09 ## Categories & Tags **Categories**: harmonyos-drivers **Tags**: None ## README ## HWCPipe ### 简介 HWCPipe是一个arm平台获取CPU和GPU硬件计数器的项目。 ### 功能 1. 获取ARM平台 CPU计数器 2. 获取ARM平台(如 Mali) GPU计数器 ### 集成 #### 在openharmony中集成 1. 首先在project的build.gradle中添加mavenCentral()仓库 ```groovy allprojects { repositories { mavenCentral() } } ``` 2. 在需要使用的module的build.gradle中添加依赖: ```groovy implementation "com.gitee.archermind-ti:hwcpipelib:1.0.0" ``` #### 在C/C++中集成 将`library/src/main/cpp/HWCPipe`目录复制到你的`cpp`目录,并在你的`CMakeLists.txt`文件中添加如下内容: ```cmake #引入指定目录下的CMakeLists.txt add_subdirectory(HWCPipe) #指定头文件查找路径 include_directories(HWCPipe) #链接hwcpipe,代码可参考如下: target_link_libraries(hwcpipe hilog_ndk.z) ``` ### 使用说明 如果在移动设备(**模拟器暂不支持,请在真机使用**)使用,请先开启性能分析: ```shell hdc shell setprop security.perf_harden 0 ``` #### openharmony中使用 1. 首先配置HWCPipe,使用HwcPipeUtil#run(String)方法,例如: ```java HwcPipeUtil.run(HwcPipeUtil.ALL_JSON); ``` HwcPipeUtil#run(String)的参数是一个json字符串,可通过配置json字符串启用不同的计数器,json字符串的示例如下: ```json { "cpu": ["Cycles", "Instructions"], "gpu": ["GpuCycles"] } ``` `HwcPipeUtil.ALL_JSON`表示启用所有的计数器,它的完整结构为: ```json { "cpu": ["Cycles", "Instructions", "CacheReferences", "CacheMisses", "BranchInstructions", "BranchMisses", "L1Accesses", "InstrRetired", "L2Accesses", "L3Accesses", "BusReads", "BusWrites", "MemReads", "MemWrites", "ASESpec", "VFPSpec", "CryptoSpec"], "gpu": ["GpuCycles", "VertexComputeCycles", "FragmentCycles", "TilerCycles", "VertexComputeJobs", "Tiles", "TransactionEliminations", "FragmentJobs", "Pixels", "EarlyZTests", "EarlyZKilled", "LateZTests", "LateZKilled", "Instructions", "DivergedInstructions", "ShaderCycles", "ShaderArithmeticCycles", "ShaderLoadStoreCycles", "ShaderTextureCycles", "CacheReadLookups", "CacheWriteLookups", "ExternalMemoryReadAccesses", "ExternalMemoryWriteAccesses", "ExternalMemoryReadStalls", "ExternalMemoryWriteStalls", "ExternalMemoryReadBytes", "ExternalMemoryWriteBytes"] } ``` 2. 取样,使用HwcPipeUtil#getSample()方法 ```java HwcInfoEntity hwcInfo=HwcPipeUtil.getSample(); CpuInfoEntity cpuEntity=hwcInfo.getCpuInfoEntity(); GpuInfoEntity gpuEntity=hwcInfo.getGpuInfoEntity(); //上述对象不为空 ``` 3. 停止,使用HwcPipeUtil#stop()方法 ```java HwcPipeUtil.stop(); ``` **注意:目前不支持在应用周期内多次配置HwcPipeUtil#run(String)** #### 在C++中使用 `HWCPipe`的基本使用 ```c++ // HWCPipe performs automated platform detection for CPU/GPU counters hwcpipe::HWCPipe h; // Start HWCPipe once at the beginning of the profiling session h.run(); while (main_loop) { // Call sample() to sample counters with the frequency you need auto measurements = h.sample(); [...] } // At the end of the profiling session, stop HWCPipe h.stop(); ``` `sample`函数返回一个`Measurements`结构,可以这样访问: ```c++ // Check if CPU measurements are available if (measurements.cpu) { // Look for a counter in the map const auto &counter = measurements.cpu->find(CpuCounter::Cycles); if (counter != measurements.cpu->end()) { // Get the data stored in the counter, casted to the type you need auto value = counter->second.get(); } } ``` ##### 启用计数器 可用的计数器在`CpuCounter`和`GpuCounter`枚举中指定(分别为`cpu_profiler.h`和`gpu_profiler.h`)。 平台将支持这些计数器的子集,可通过以下方式查询: ```c++ auto cpu_counters = h.cpu_profiler()->supported_counters(); auto gpu_counters = h.gpu_profiler()->supported_counters() ``` 您可以通过以下方式指定要启用的计数器: ```c++ // Enable a default set of counters auto h = hwcpipe::HWCPipe(); // Pass sets of CPU and GPU counters to be enabled auto h = hwcpipe::HWCPipe({CpuCounter::Cycles, CpuCounter::Instructions}, {GpuCounter::GpuCycles}); // Pass a JSON string auto h = hwcpipe::HWCPipe(json); ``` JSON字符串的格式应如下所示: ```json { "cpu": ["Cycles", "Instructions"], "gpu": ["GpuCycles"] } ``` 可用的计数器名称可以在`cpu_counter_names`(`cpu_profiler.h`)和`gpu_counter_names`(`gpu_profiler.h`)中找到。 有关马里计数器的更多信息,请参阅 [Mali Performance Counters](https://community.arm.com/graphics/b/blog/posts/mali-bifrost-family-performance-counters)。 **日志显示**请参阅[真机HiLog日志显示](https://developer.huawei.com/consumer/cn/forum/topic/0203536901546500016?fid=0101303901040230869) ### 编译说明 1. 将项目通过git clone 至本地 2. 使用DevEco Studio 打开该项目,然后等待Gradle 构建完成 3. 点击`Run`运行即可(真机运行可能需要配置签名) ### 版权和许可信息 * [MIT License](LICENSE)