6 Star 4 Fork 2

Archermind-TI / HWCPipe

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 5.15 KB
一键复制 编辑 原始数据 按行查看 历史
fengrenjie 提交于 2021-06-22 17:49 . feature:hwcpipelib库制作

HWCPipe

简介

HWCPipe是一个arm平台获取CPU和GPU硬件计数器的项目。

功能

  1. 获取ARM平台 CPU计数器
  2. 获取ARM平台(如 Mali) GPU计数器

集成

在openharmony中集成

  1. 首先在project的build.gradle中添加mavenCentral()仓库

    allprojects {
        repositories {
            mavenCentral()
        }
    }
  2. 在需要使用的module的build.gradle中添加依赖:

    implementation "com.gitee.archermind-ti:hwcpipelib:1.0.0"

在C/C++中集成

library/src/main/cpp/HWCPipe目录复制到你的cpp目录,并在你的CMakeLists.txt文件中添加如下内容:

#引入指定目录下的CMakeLists.txt
add_subdirectory(HWCPipe)
#指定头文件查找路径
include_directories(HWCPipe)

#链接hwcpipe,代码可参考如下:
target_link_libraries(hwcpipe hilog_ndk.z)

使用说明

如果在移动设备(模拟器暂不支持,请在真机使用)使用,请先开启性能分析:

hdc shell setprop security.perf_harden 0

openharmony中使用

  1. 首先配置HWCPipe,使用HwcPipeUtil#run(String)方法,例如:

    HwcPipeUtil.run(HwcPipeUtil.ALL_JSON);

    HwcPipeUtil#run(String)的参数是一个json字符串,可通过配置json字符串启用不同的计数器,json字符串的示例如下:

    {
        "cpu": ["Cycles", "Instructions"],
        "gpu": ["GpuCycles"]
    }

    HwcPipeUtil.ALL_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()方法

    HwcInfoEntity hwcInfo=HwcPipeUtil.getSample();
    CpuInfoEntity cpuEntity=hwcInfo.getCpuInfoEntity();
    GpuInfoEntity gpuEntity=hwcInfo.getGpuInfoEntity();
    //上述对象不为空
  3. 停止,使用HwcPipeUtil#stop()方法

    HwcPipeUtil.stop();

注意:目前不支持在应用周期内多次配置HwcPipeUtil#run(String)

在C++中使用

HWCPipe的基本使用

// 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结构,可以这样访问:

// 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<float>();
    }
}
启用计数器

可用的计数器在CpuCounterGpuCounter枚举中指定(分别为cpu_profiler.hgpu_profiler.h)。

平台将支持这些计数器的子集,可通过以下方式查询:

auto cpu_counters = h.cpu_profiler()->supported_counters();
auto gpu_counters = h.gpu_profiler()->supported_counters()

您可以通过以下方式指定要启用的计数器:

// 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字符串的格式应如下所示:

{
    "cpu": ["Cycles", "Instructions"],
    "gpu": ["GpuCycles"]
}

可用的计数器名称可以在cpu_counter_namescpu_profiler.h)和gpu_counter_namesgpu_profiler.h)中找到。

有关马里计数器的更多信息,请参阅 Mali Performance Counters

日志显示请参阅真机HiLog日志显示

编译说明

  1. 将项目通过git clone 至本地
  2. 使用DevEco Studio 打开该项目,然后等待Gradle 构建完成
  3. 点击Run运行即可(真机运行可能需要配置签名)

版权和许可信息

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/archermind-ti/hwcpipe.git
git@gitee.com:archermind-ti/hwcpipe.git
archermind-ti
hwcpipe
HWCPipe
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891