HWCPipe是一个arm平台获取CPU和GPU硬件计数器的项目。
首先在project的build.gradle中添加mavenCentral()仓库
allprojects {
repositories {
mavenCentral()
}
}
在需要使用的module的build.gradle中添加依赖:
implementation "com.gitee.archermind-ti:hwcpipelib:1.0.0"
将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
首先配置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"]
}
取样,使用HwcPipeUtil#getSample()方法
HwcInfoEntity hwcInfo=HwcPipeUtil.getSample();
CpuInfoEntity cpuEntity=hwcInfo.getCpuInfoEntity();
GpuInfoEntity gpuEntity=hwcInfo.getGpuInfoEntity();
//上述对象不为空
停止,使用HwcPipeUtil#stop()方法
HwcPipeUtil.stop();
注意:目前不支持在应用周期内多次配置HwcPipeUtil#run(String)
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>();
}
}
可用的计数器在CpuCounter
和GpuCounter
枚举中指定(分别为cpu_profiler.h
和gpu_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_names
(cpu_profiler.h
)和gpu_counter_names
(gpu_profiler.h
)中找到。
有关马里计数器的更多信息,请参阅 Mali Performance Counters。
日志显示请参阅真机HiLog日志显示
Run
运行即可(真机运行可能需要配置签名)此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型