1# 性能调优组件<a name="ZH-CN_TOPIC_0000001149491319"></a> 2 3- [简介](#section6874544183112) 4- [架构图](#section1514713331342) 5- [目录](#section1742612449345) 6- [说明](#section2165102016359) 7 - [接口说明](#section558917318367) 8 - [使用说明](#section681316903611) 9 - [调测验证](#section35362541215) 10 - [hiprofiler_cmd 使用说明](#section35362541216) 11 - [参数说明](#section35362541290) 12 - [命令展示](#section35362541500) 13 - [基础配置参数](#section35362541219) 14 - [ftrace抓取场景示例](#section35362541220) 15 - [内存信息抓取场景示例](#section35362541221) 16 - [内核内存信息](#section35362541222) 17 - [虚拟内存统计](#section35362541223) 18 - [进程内存使用跟踪](#section35362541224) 19 - [bytrace/hitrace场景示例](#section35362541225) 20 - [hiperf场景示例](#section35362541226) 21- [相关仓](#section1293495681320) 22 23## 简介<a name="section6874544183112"></a> 24 25性能调优组件包含系统和应用调优框架,旨在为开发者提供一套性能调优平台,可以用来分析内存、性能等问题。 26 27该组件整体分为PC端和设备端两部分,PC端最终作为deveco studio的插件进行发布,内部主要包括分为UI绘制、设备管理、进程管理、插件管理、数据导入、数据存储、 数据分析、Session管理、配置管理等模块;设备端主要包括命令行工具、服务进程、插件集合、应用程序组件等模块。设备端提供了插件扩展能力,对外提供了插件接口,基于该扩展能力可以按需定义自己的能力,并集成到框架中来,目前基于插件能力已经完成了实时内存插件,trace插件。下文会重点对设备端提供的插件能力进行介绍。 28 29## 架构图<a name="section1514713331342"></a> 30 31![](figures/zh-cn_image_0000001162598155.png) 32 33## 目录<a name="section1742612449345"></a> 34 35``` 36/developtools/profiler 37├── device # 设备侧代码目录 38│ ├── base # 基础功能代码 39│ │ ├── include # 基础功能的头文件代码目录 40│ | ├── src # 基础功能的源文件代码目录 41│ | └── test # 基础功能的测试代码目录 42│ ├── cmds # 对外命令行模块的代码 43│ | ├── include # 对外命令行模块的头文件代码目录 44│ | ├── src # 对外命令行模块的源文件代码目录 45│ | └── test # 对外命令行模块的测试代码目录 46│ └── plugins # 插件代码目录 47│ ├── api # 插件模块对外提供的接口文件代码目录 48│ | ├── include # 插件模块对外提供的接口头文件代码目录 49│ | └── src # 插件模块对外提供的接口源文件代码目录 50│ ├── memory_plugin # 内存插件模块代码目录 51│ | ├── include # 内存插件模块头文件代码目录 52│ | ├── src # 内存插件模块源文件代码目录 53│ | └── test # 内存插件模块测试代码目录 54│ └── trace_plugin # trace插件模块代码目录 55│ ├── include # trace插件模块头文件代码目录 56│ ├── src # trace插件模块源文件代码目录 57│ └── test # trace插件模块测试代码目录 58├── host # 主机侧代码目录 59│ └── ohosprofiler # 主机侧调优模块代码目录 60│ └── src # 主机侧调优模块源文件代码目录 61├── protos # 项目中的proto格式文件的代码目录 62│ └── innerkits # 对内部子系统暴露的头文件存放目录 63│ └── builtin # JS应用框架对外暴露JS三方module API接口存放目录 64├── trace_analyzer # bytrace解析模块的代码目录 65│ ├── include # bytrace解析模块的公共头文件存放目录 66│ └── src # bytrace解析模块功能源文件存放目录 67├── interfaces # 项目中接口的代码目录 68│ ├── innerkits # 模块间接口的代码目录 69│ └── kits # 对外提供接口存放目录 70``` 71 72## 说明<a name="section2165102016359"></a> 73 74下面针对设备端对外提供的插件扩展能力进行接口和使用说明。 75 76### 接口说明<a name="section558917318367"></a> 77 78下面是设备端插件模块对外提供的接口: 79 80- PluginModuleCallbacks为插件模块对外提供的回调接口,插件管理模块通过该回调接口列表与每一个插件模块进行交互,每一个新增插件都需要实现该接口列表中的函数。 81 82**表 1** PluginModuleCallbacks接口列表 83 84<a name="table214mcpsimp"></a> 85<table><thead align="left"><tr id="row221mcpsimp"><th class="cellrowborder" valign="top" width="30%" id="mcps1.2.4.1.1"><p id="p223mcpsimp"><a name="p223mcpsimp"></a><a name="p223mcpsimp"></a>接口名</p> 86</th> 87<th class="cellrowborder" valign="top" width="30.020000000000003%" id="mcps1.2.4.1.2"><p id="p225mcpsimp"><a name="p225mcpsimp"></a><a name="p225mcpsimp"></a>类型</p> 88</th> 89<th class="cellrowborder" valign="top" width="39.98%" id="mcps1.2.4.1.3"><p id="p227mcpsimp"><a name="p227mcpsimp"></a><a name="p227mcpsimp"></a>描述</p> 90</th> 91</tr> 92</thead> 93<tbody><tr id="row229mcpsimp"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.2.4.1.1 "><p id="p231mcpsimp"><a name="p231mcpsimp"></a><a name="p231mcpsimp"></a>PluginModuleCallbacks::onPluginSessionStart</p> 94</td> 95<td class="cellrowborder" valign="top" width="30.020000000000003%" headers="mcps1.2.4.1.2 "><p id="p233mcpsimp"><a name="p233mcpsimp"></a><a name="p233mcpsimp"></a>int (*PluginSessionStartCallback)(const uint8_t* configData, uint32_t configSize);</p> 96</td> 97<td class="cellrowborder" valign="top" width="39.98%" headers="mcps1.2.4.1.3 "><a name="ul1467809165112"></a><a name="ul1467809165112"></a><ul id="ul1467809165112"><li>功能:<p id="p1208131116548"><a name="p1208131116548"></a><a name="p1208131116548"></a>插件会话开始接口,开始插件会话时会被调用,用来下发插件配置</p> 98</li><li>输入参数:<p id="p104087130518"><a name="p104087130518"></a><a name="p104087130518"></a>configData:配置信息内存块起始地址</p> 99<p id="p32731219115114"><a name="p32731219115114"></a><a name="p32731219115114"></a>configSize:配置信息内存块字节数</p> 100</li><li>返回值:<p id="p4892128135110"><a name="p4892128135110"></a><a name="p4892128135110"></a>0:成功</p> 101<p id="p1621513010517"><a name="p1621513010517"></a><a name="p1621513010517"></a>-1:失败</p> 102</li></ul> 103</td> 104</tr> 105<tr id="row236mcpsimp"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.2.4.1.1 "><p id="p238mcpsimp"><a name="p238mcpsimp"></a><a name="p238mcpsimp"></a>PluginModuleCallbacks::onPluginReportResult</p> 106</td> 107<td class="cellrowborder" valign="top" width="30.020000000000003%" headers="mcps1.2.4.1.2 "><p id="p240mcpsimp"><a name="p240mcpsimp"></a><a name="p240mcpsimp"></a>int (*PluginReportResultCallback)(uint8_t* bufferData, uint32_t bufferSize);</p> 108</td> 109<td class="cellrowborder" valign="top" width="39.98%" headers="mcps1.2.4.1.3 "><a name="ul1046263617524"></a><a name="ul1046263617524"></a><ul id="ul1046263617524"><li>功能:<p id="p621321105415"><a name="p621321105415"></a><a name="p621321105415"></a>插件结果上报接口类型,当任务下发后,框架采集任务会周期性调用此接口请求回填数据</p> 110</li><li>输入参数:<p id="p12796339115211"><a name="p12796339115211"></a><a name="p12796339115211"></a>bufferData: 存放结果的内存缓冲区起始地址</p> 111<p id="p1163214085219"><a name="p1163214085219"></a><a name="p1163214085219"></a>bufferSize: 存放结果的内存缓冲区的字节数</p> 112</li><li>返回值:<p id="p114391444135212"><a name="p114391444135212"></a><a name="p114391444135212"></a>大于0:已经填充的内存字节数</p> 113<p id="p117881419526"><a name="p117881419526"></a><a name="p117881419526"></a>等于0:没有填充任何内容</p> 114<p id="p7337174625218"><a name="p7337174625218"></a><a name="p7337174625218"></a>小于0:失败</p> 115</li></ul> 116</td> 117</tr> 118<tr id="row243mcpsimp"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.2.4.1.1 "><p id="p245mcpsimp"><a name="p245mcpsimp"></a><a name="p245mcpsimp"></a>PluginModuleCallbacks::onPluginSessionStop</p> 119</td> 120<td class="cellrowborder" valign="top" width="30.020000000000003%" headers="mcps1.2.4.1.2 "><p id="p247mcpsimp"><a name="p247mcpsimp"></a><a name="p247mcpsimp"></a>int (*PluginSessionStopCallback)();</p> 121</td> 122<td class="cellrowborder" valign="top" width="39.98%" headers="mcps1.2.4.1.3 "><a name="ul975785275211"></a><a name="ul975785275211"></a><ul id="ul975785275211"><li>功能:<p id="p11471347135914"><a name="p11471347135914"></a><a name="p11471347135914"></a>采集会话结束接口</p> 123</li><li>返回值:<p id="p4301105945210"><a name="p4301105945210"></a><a name="p4301105945210"></a>0:成功</p> 124<p id="p145541757125214"><a name="p145541757125214"></a><a name="p145541757125214"></a>-1:失败</p> 125</li></ul> 126</td> 127</tr> 128<tr id="row250mcpsimp"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.2.4.1.1 "><p id="p252mcpsimp"><a name="p252mcpsimp"></a><a name="p252mcpsimp"></a>PluginModuleCallbacks::onRegisterWriterStruct</p> 129</td> 130<td class="cellrowborder" valign="top" width="30.020000000000003%" headers="mcps1.2.4.1.2 "><p id="p254mcpsimp"><a name="p254mcpsimp"></a><a name="p254mcpsimp"></a>int (*RegisterWriterStructCallback)(WriterStruct* writer);</p> 131</td> 132<td class="cellrowborder" valign="top" width="39.98%" headers="mcps1.2.4.1.3 "><a name="ul11459712105318"></a><a name="ul11459712105318"></a><ul id="ul11459712105318"><li>功能:<p id="p16725350135912"><a name="p16725350135912"></a><a name="p16725350135912"></a>采集框架注册写数据接口,当插件管理模块向插件注册此接口,插件可以主动调用write句柄,进行写入数据</p> 133</li><li>输入参数:<p id="p741312324531"><a name="p741312324531"></a><a name="p741312324531"></a>writer 写者指针</p> 134</li><li>返回值:<p id="p1937282325319"><a name="p1937282325319"></a><a name="p1937282325319"></a>0:成功</p> 135<p id="p3613421185314"><a name="p3613421185314"></a><a name="p3613421185314"></a>-1:失败</p> 136</li></ul> 137</td> 138</tr> 139</tbody> 140</table> 141 142- WriterStruct是上面onRegisterWriterStruct接口中的参数,主要实现写数据接口,将插件中采集的数据通过该接口进行写入。 143 144**表 2** WriterStruct接口列表 145 146<a name="table1469161115240"></a> 147<table><thead align="left"><tr id="row124691911182413"><th class="cellrowborder" valign="top" width="30%" id="mcps1.2.4.1.1"><p id="p14469151118247"><a name="p14469151118247"></a><a name="p14469151118247"></a>接口名</p> 148</th> 149<th class="cellrowborder" valign="top" width="30.020000000000003%" id="mcps1.2.4.1.2"><p id="p114691119249"><a name="p114691119249"></a><a name="p114691119249"></a>类型</p> 150</th> 151<th class="cellrowborder" valign="top" width="39.98%" id="mcps1.2.4.1.3"><p id="p134701611172413"><a name="p134701611172413"></a><a name="p134701611172413"></a>描述</p> 152</th> 153</tr> 154</thead> 155<tbody><tr id="row5470201102420"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.2.4.1.1 "><p id="p144702115242"><a name="p144702115242"></a><a name="p144702115242"></a>WriterStruct::write</p> 156</td> 157<td class="cellrowborder" valign="top" width="30.020000000000003%" headers="mcps1.2.4.1.2 "><p id="p347010119246"><a name="p347010119246"></a><a name="p347010119246"></a>long (*WriteFuncPtr)(WriterStruct* writer, const void* data, size_t size);</p> 158</td> 159<td class="cellrowborder" valign="top" width="39.98%" headers="mcps1.2.4.1.3 "><a name="ul1783144245320"></a><a name="ul1783144245320"></a><ul id="ul1783144245320"><li>功能:<p id="p11675175517597"><a name="p11675175517597"></a><a name="p11675175517597"></a>写接口,将插件中采集的数据通过writer进行写入</p> 160</li><li>输入参数:<p id="p1812314462537"><a name="p1812314462537"></a><a name="p1812314462537"></a>writer:写者指针</p> 161<p id="p498224618534"><a name="p498224618534"></a><a name="p498224618534"></a>data:数据缓冲区首字节指针</p> 162<p id="p368519478533"><a name="p368519478533"></a><a name="p368519478533"></a>size: 数据缓冲区的字节数</p> 163</li><li>返回值:<p id="p162857527531"><a name="p162857527531"></a><a name="p162857527531"></a>0:成功</p> 164<p id="p101121150185318"><a name="p101121150185318"></a><a name="p101121150185318"></a>-1:失败</p> 165</li></ul> 166</td> 167</tr> 168<tr id="row84706116243"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.2.4.1.1 "><p id="p2047081142415"><a name="p2047081142415"></a><a name="p2047081142415"></a>WriterStruct::flush</p> 169</td> 170<td class="cellrowborder" valign="top" width="30.020000000000003%" headers="mcps1.2.4.1.2 "><p id="p194111838267"><a name="p194111838267"></a><a name="p194111838267"></a>bool (*FlushFuncPtr)(WriterStruct* writer);</p> 171</td> 172<td class="cellrowborder" valign="top" width="39.98%" headers="mcps1.2.4.1.3 "><a name="ul9562185717538"></a><a name="ul9562185717538"></a><ul id="ul9562185717538"><li>功能:<p id="p744185910594"><a name="p744185910594"></a><a name="p744185910594"></a>触发数据上传接口</p> 173</li><li>输入参数:<p id="p69601111504"><a name="p69601111504"></a><a name="p69601111504"></a>writer:写者指针</p> 174</li><li>返回值:<p id="p970375016"><a name="p970375016"></a><a name="p970375016"></a>true:成功</p> 175<p id="p1314495407"><a name="p1314495407"></a><a name="p1314495407"></a>false:失败</p> 176</li></ul> 177</td> 178</tr> 179</tbody> 180</table> 181 182- 下面是插件模块对外提供的总入口,主要包括表1中的插件模块回调函数以及插件名称、插件模块需要申请的内存大小。 183 184**表 3** PluginModuleStruct接口列表 185 186<a name="table14418172393018"></a> 187<table><thead align="left"><tr id="row14188236300"><th class="cellrowborder" valign="top" width="30%" id="mcps1.2.4.1.1"><p id="p041820239304"><a name="p041820239304"></a><a name="p041820239304"></a>接口名</p> 188</th> 189<th class="cellrowborder" valign="top" width="30.020000000000003%" id="mcps1.2.4.1.2"><p id="p34181823153010"><a name="p34181823153010"></a><a name="p34181823153010"></a>类型</p> 190</th> 191<th class="cellrowborder" valign="top" width="39.98%" id="mcps1.2.4.1.3"><p id="p10418023123011"><a name="p10418023123011"></a><a name="p10418023123011"></a>描述</p> 192</th> 193</tr> 194</thead> 195<tbody><tr id="row16418623143016"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.2.4.1.1 "><p id="p625018817318"><a name="p625018817318"></a><a name="p625018817318"></a>PluginModuleStruct::callbacks</p> 196</td> 197<td class="cellrowborder" valign="top" width="30.020000000000003%" headers="mcps1.2.4.1.2 "><p id="p162501689312"><a name="p162501689312"></a><a name="p162501689312"></a>PluginModuleCallbacks*</p> 198</td> 199<td class="cellrowborder" valign="top" width="39.98%" headers="mcps1.2.4.1.3 "><p id="p122501085317"><a name="p122501085317"></a><a name="p122501085317"></a>功能:定义插件回调函数列表</p> 200</td> 201</tr> 202<tr id="row241952313015"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.2.4.1.1 "><p id="p325018863113"><a name="p325018863113"></a><a name="p325018863113"></a>PluginModuleStruct::name</p> 203</td> 204<td class="cellrowborder" valign="top" width="30.020000000000003%" headers="mcps1.2.4.1.2 "><p id="p4250138123112"><a name="p4250138123112"></a><a name="p4250138123112"></a>C style string</p> 205</td> 206<td class="cellrowborder" valign="top" width="39.98%" headers="mcps1.2.4.1.3 "><p id="p425019883115"><a name="p425019883115"></a><a name="p425019883115"></a>功能:定义插件名称</p> 207</td> 208</tr> 209<tr id="row49481758173013"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.2.4.1.1 "><p id="p1125010813316"><a name="p1125010813316"></a><a name="p1125010813316"></a>PluginModuleStruct::resultBufferSizeHint</p> 210</td> 211<td class="cellrowborder" valign="top" width="30.020000000000003%" headers="mcps1.2.4.1.2 "><p id="p142509810319"><a name="p142509810319"></a><a name="p142509810319"></a>uint32_t</p> 212</td> 213<td class="cellrowborder" valign="top" width="39.98%" headers="mcps1.2.4.1.3 "><p id="p1525112853115"><a name="p1525112853115"></a><a name="p1525112853115"></a>功能:用于提示插件管理模块调用数据上报接口时使用的内存缓冲区字节数</p> 214</td> 215</tr> 216</tbody> 217</table> 218 219### 使用说明<a name="section681316903611"></a> 220 221下面介绍在设备端基于性能调优框架提供的插件能力,新增一个插件涉及到的关键开发步骤: 222 2231. 编写proto数据定义文件_plugin\_data.proto_,定义数据源格式,数据源格式决定了插件上报哪些数据: 224 225 ``` 226 message PluginData { 227 int32 pid = 1; 228 string name = 2; 229 uint64 count1 = 3; 230 uint64 count2 = 4; 231 uint64 count3 = 5; 232 ...... 233 } 234 ``` 235 2362. 编写数据源配置文件_plugin\_config.proto_,采集的行为可以根据配置进行变化,可以设置数据源上报间隔等信息: 237 238 ``` 239 message PluginConfig { 240 int32 pid = 1; 241 bool report_interval = 2; 242 int report_counter_id_1 = 3; 243 int report_counter_id_2 = 4; 244 ...... 245 } 246 ``` 247 2483. 定义PluginModuleCallbacks实现插件回调接口;定义PluginModuleStruct类型的g\_pluginModule全局变量,注册插件信息。 249 250 ``` 251 static PluginModuleCallbacks callbacks = { 252 PluginSessionStart, 253 PluginReportResult, 254 PluginSessionStop, 255 }; 256 PluginModuleStruct g_pluginModule = {&callbacks, "test-plugin", MAX_BUFFER_SIZE}; 257 ``` 258 2594. 通过PluginSessionStart(名字可以自己定义)实现插件回调接口列表的onPluginSessionStart接口,主要处理插件的开始流程。 260 261 ``` 262 int PluginSessionStart(const uint8_t* configData, uint32_t configSize) 263 { 264 ...... 265 return 0; 266 } 267 ``` 268 2695. 通过PluginReportResult(名字可以自己定义)实现插件回调接口列表的onPluginReportResult接口,将插件内部采集的信息通过该接口进行上报: 270 271 ``` 272 int PluginReportResult(uint8_t* bufferData, uint32_t bufferSize) 273 { 274 ...... 275 return 0; 276 } 277 ``` 278 2796. 通过PluginSessionStop(名字可以自己定义)实现插件回调接口列表的onPluginSessionStop接口,主要进行插件停止后的操作流程。 280 281 ``` 282 int PluginSessionStop() 283 { 284 ...... 285 return 0; 286 } 287 ``` 288 2897. 编写proto gn构建脚本, 生成protobuf源文件,protobuf源文件编译生成目标文件: 290 291 ``` 292 action("plugin_cpp_gen") { 293 script = "${OHOS_PROFILER_DIR}/build/protoc.sh" //依赖的编译工具链 294 sources = [ //定义的插件相关的proto文件,比如插件配置文件、插件数据对应的proto文件 295 "plugin_data.proto", 296 "plugin_config.proto", 297 ] 298 outputs = [ //通过protoc编译生成的结果文件 299 "plugin_data.pb.h", 300 "plugin_data.pb.cc", 301 "plugin_config.pb.h", 302 "plugin_config.pb.cc", 303 ] 304 args = [ 305 "--cpp_out", 306 "$proto_rel_out_dir", 307 "--proto_path", 308 rebase_path(".", root_build_dir), 309 ] 310 deps = [ 311 "${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protoc(${host_toolchain})", 312 ] 313 } 314 ohos_source_set("plug_cpp") { //将定义的proto文件生成cpp文件 315 deps = [ 316 ":plugin_cpp_gen", 317 ] 318 public_deps = [ 319 "${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protobuf", 320 "${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protobuf_lite", 321 ] 322 include_dirs = [ "$proto_out_dir" ] 323 sources = [ //目标plug_cpp中包括的源文件 324 "plugin_data.pb.h", 325 "plugin_data.pb.cc", 326 "plugin_config.pb.h", 327 "plugin_config.pb.cc", 328 ] 329 } 330 ``` 331 3328. 编写插件GN构建脚本: 333 334 ``` 335 ohos_shared_library("***plugin") { 336 output_name = "***plugin" 337 sources = [ 338 "src/***plugin.cpp", //插件中的源文件 339 ] 340 include_dirs = [ 341 "../api/include", 342 "${OHOS_PROFILER_DIR}/device/base/include", 343 ] 344 deps = [ 345 "${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protobuf_lite", 346 "${OHOS_PROFILER_3RDPARTY_PROTOBUF_DIR}:protoc_lib", 347 "${OHOS_PROFILER_DIR}/protos/types/plugins/**:plug_cpp", //上面ohos_source_set中生成的plug_cpp 348 ] 349 install_enable = true 350 subsystem_name = "${OHOS_PROFILER_SUBSYS_NAME}" 351 } 352 ``` 353 354 355### 调测验证:<a name="section35362541215"></a> 356 357插件动态库生成后,可以自己编写测试代码,通过dlopen加载动态库,并调用上面代码中实现的插件模块回调函数进行验证。 358 359``` 360int main(int argc, char** argv) 361{ 362 void* handle; 363 PluginModuleStruct* memplugin; 364 handle = dlopen("./libplugin.z.so", RTLD_LAZY); //动态打开上面生成的插件动态库 365 if (handle == nullptr) { 366 HILOGD("dlopen err:%s.", dlerror()); 367 return 0; 368 } 369 memplugin = (PluginModuleStruct*)dlsym(handle, "g_pluginModule"); //获取开发步骤3中定义的g_pluginModule全局变量 370 //check memplugin->callbacks // 通过该指针调用上面开发步骤3中定义的回调函数 371 return 0; 372``` 373 374## hiprofiler_cmd 使用说明<a name="section35362541216"></a> 375 376### 参数说明<a name="section35362541290"></a> 377 378 执行hiprofiler_cmd 为调优业务的离线命令行抓取工具,具体使用方法及命令行参数介绍如下。 379 380可以使用`-h`或者`--help`参数查看命令的使用描述信息: 381 382```sh 383hiprofiler_cmd -h 384help : 385 --getport -q : get grpc address 386 --time -t : trace time 387 --out -o : output file name 388 --help -h : make some help 389 --list -l : plugin list 390 --start -s : start dependent process 391 --kill -k : kill dependent process 392 --config -c : start trace by config file 393``` 394 395其余参数使用说明如下: 396 397* `-q`或者`--getport`选项,用于查询服务的端口信息; 398* `-t`或者`--time`选项,用于指定抓取时间,单位是秒; 399* `-o`或者`--out`选项,用于指定输出的离线数据文件名; 400* `-h`或者`--help`选项,用于输出帮助信息; 401* `-l`或者`--list`选项,用于查询插件列表; 402* `-s`或者`--start`选项,用于启动依赖的进程; 403* `-k`或者`--kill`选项,用于关闭依赖的进程; 404* `-c`或者`--config`选项,用于指定配置文件; 405 406### 命令展示<a name="section35362541500"></a> 407 408#### 基础配置参数<a name="section35362541219"></a> 409 410```sh 411hiprofiler_cmd \ 412 -c - \ 413 -o /data/local/tmp/hiprofiler_data.htrace \ 414 -t 50 \ 415 -s \ 416 -k \ 417<<CONFIG 418 request_id: 1 419 session_config { 420 buffers { 421 pages: 16384 422 } 423 result_file: "/data/local/tmp/hiprofiler_data.htrace" 424 sample_duration: 50000 425 } 426CONFIG 427``` 428 429命令参数说明: 430 431* request_id:本次请求的id 432* pages:存储trace数据的buffer大小(4 * pages kb) 433* result_file:结果输出的文件路径,与-o参数对应 434* sample_duration:抓取时长(ms),与-t参数对应 435 436#### ftrace抓取场景示例<a name="section35362541220"></a> 437 438```sh 439hiprofiler_cmd \ 440 -c - \ 441 -o /data/local/tmp/hiprofiler_data.htrace \ 442 -t 50 \ 443 -s \ 444 -k \ 445<<CONFIG 446 request_id: 1 447 session_config { 448 buffers { 449 pages: 16384 450 } 451 result_file: "/data/local/tmp/hiprofiler_data.htrace" 452 sample_duration: 50000 453 } 454 plugin_configs { 455 plugin_name: "ftrace-plugin" 456 sample_interval: 1000 457 config_data { 458 ftrace_events: "sched/sched_switch" 459 ftrace_events: "power/suspend_resume" 460 ftrace_events: "sched/sched_wakeup" 461 ftrace_events: "sched/sched_wakeup_new" 462 ftrace_events: "sched/sched_waking" 463 ftrace_events: "sched/sched_process_exit" 464 ftrace_events: "sched/sched_process_free" 465 ftrace_events: "task/task_newtask" 466 ftrace_events: "task/task_rename" 467 buffer_size_kb: 2048 468 flush_interval_ms: 1000 469 flush_threshold_kb: 4096 470 parse_ksyms: true 471 clock: "mono" 472 trace_period_ms: 200 473 debug_on: false 474 hitrace_time: 50 475 } 476 } 477CONFIG 478``` 479 480命令参数说明: 481 482* sample_interval:轮循模式下,插件上报数据的间隔时间(ms) 483* trace_period_ms:ftrace插件读取内核缓冲区数据的间隔时间(ms) 484* hitrace_time:hitrace命令行抓取时间,与hiprofiler_cmd下发的-t配置联动 485 486#### 内存信息抓取场景示例<a name="section35362541221"></a> 487 488##### 内核内存信息<a name="section35362541222"></a> 489 490使用如下命令: 491 492```sh 493hiprofiler_cmd \ 494 -c - \ 495 -o /data/local/tmp/hiprofiler_data.htrace \ 496 -t 50 \ 497 -s \ 498 -k \ 499<<CONFIG 500 request_id: 1 501 session_config { 502 buffers { 503 pages: 16384 504 } 505 result_file: "/data/local/tmp/hiprofiler_data.htrace" 506 sample_duration: 50000 507 } 508 plugin_configs { 509 plugin_name: "memory-plugin" 510 sample_interval: 5000 511 config_data { 512 report_process_tree: true 513 report_sysmem_mem_info: true 514 sys_meminfo_counters: PMEM_ACTIVE 515 sys_meminfo_counters: PMEM_ACTIVE_ANON 516 sys_meminfo_counters: PMEM_ACTIVE_FILE 517 sys_meminfo_counters: PMEM_ANON_PAGES 518 sys_meminfo_counters: PMEM_BUFFERS 519 sys_meminfo_counters: PMEM_CACHED 520 sys_meminfo_counters: PMEM_CMA_FREE 521 sys_meminfo_counters: PMEM_CMA_TOTAL 522 sys_meminfo_counters: PMEM_COMMIT_LIMIT 523 sys_meminfo_counters: PMEM_COMMITED_AS 524 sys_meminfo_counters: PMEM_DIRTY 525 sys_meminfo_counters: PMEM_INACTIVE 526 sys_meminfo_counters: PMEM_INACTIVE_ANON 527 sys_meminfo_counters: PMEM_INACTIVE_FILE 528 sys_meminfo_counters: PMEM_KERNEL_STACK 529 sys_meminfo_counters: PMEM_MAPPED 530 sys_meminfo_counters: PMEM_MEM_AVAILABLE 531 sys_meminfo_counters: PMEM_MEM_FREE 532 sys_meminfo_counters: PMEM_MEM_TOTAL 533 sys_meminfo_counters: PMEM_MLOCKED 534 sys_meminfo_counters: PMEM_PAGE_TABLES 535 sys_meminfo_counters: PMEM_SHMEM 536 sys_meminfo_counters: PMEM_SLAB 537 sys_meminfo_counters: PMEM_SLAB_RECLAIMABLE 538 sys_meminfo_counters: PMEM_SLAB_UNRECLAIMABLE 539 sys_meminfo_counters: PMEM_SWAP_CACHED 540 sys_meminfo_counters: PMEM_SWAP_FREE 541 sys_meminfo_counters: PMEM_SWAP_TOTAL 542 sys_meminfo_counters: PMEM_UNEVICTABLE 543 sys_meminfo_counters: PMEM_VMALLOC_CHUNK 544 sys_meminfo_counters: PMEM_VMALLOC_TOTAL 545 sys_meminfo_counters: PMEM_VMALLOC_USED 546 sys_meminfo_counters: PMEM_WRITEBACK 547 sys_meminfo_counters: PMEM_KERNEL_RECLAIMABLE 548 report_sysmem_vmem_info: true 549 report_process_mem_info: true 550 report_app_mem_info: false 551 report_app_mem_by_memory_service: false 552 } 553 } 554CONFIG 555``` 556 557##### 虚拟内存统计<a name="section35362541223"></a> 558 559使用如下命令: 560 561```sh 562hiprofiler_cmd \ 563 -c - \ 564 -o /data/local/tmp/hiprofiler_data.htrace \ 565 -t 50 \ 566 -s \ 567 -k \ 568<<CONFIG 569 request_id: 1 570 session_config { 571 buffers { 572 pages: 16384 573 } 574 result_file: "/data/local/tmp/hiprofiler_data.htrace" 575 sample_duration: 50000 576 } 577 plugin_configs { 578 plugin_name: "memory-plugin" 579 sample_interval: 5000 580 config_data { 581 report_process_tree: true 582 report_sysmem_mem_info: true 583 report_sysmem_vmem_info: true 584 sys_vmeminfo_counters: VMEMINFO_UNSPECIFIED 585 sys_vmeminfo_counters: VMEMINFO_NR_FREE_PAGES 586 sys_vmeminfo_counters: VMEMINFO_NR_ALLOC_BATCH 587 sys_vmeminfo_counters: VMEMINFO_NR_INACTIVE_ANON 588 sys_vmeminfo_counters: VMEMINFO_NR_ACTIVE_ANON 589 sys_vmeminfo_counters: VMEMINFO_NR_INACTIVE_FILE 590 sys_vmeminfo_counters: VMEMINFO_NR_ACTIVE_FILE 591 sys_vmeminfo_counters: VMEMINFO_NR_UNEVICTABLE 592 sys_vmeminfo_counters: VMEMINFO_NR_MLOCK 593 sys_vmeminfo_counters: VMEMINFO_NR_ANON_PAGES 594 sys_vmeminfo_counters: VMEMINFO_NR_MAPPED 595 sys_vmeminfo_counters: VMEMINFO_NR_FILE_PAGES 596 sys_vmeminfo_counters: VMEMINFO_NR_DIRTY 597 sys_vmeminfo_counters: VMEMINFO_NR_WRITEBACK 598 sys_vmeminfo_counters: VMEMINFO_NR_SLAB_RECLAIMABLE 599 sys_vmeminfo_counters: VMEMINFO_NR_SLAB_UNRECLAIMABLE 600 sys_vmeminfo_counters: VMEMINFO_NR_PAGE_TABLE_PAGES 601 sys_vmeminfo_counters: VMEMINFO_NR_KERNEL_STACK 602 sys_vmeminfo_counters: VMEMINFO_NR_OVERHEAD 603 sys_vmeminfo_counters: VMEMINFO_NR_UNSTABLE 604 sys_vmeminfo_counters: VMEMINFO_NR_BOUNCE 605 sys_vmeminfo_counters: VMEMINFO_NR_VMSCAN_WRITE 606 sys_vmeminfo_counters: VMEMINFO_NR_VMSCAN_IMMEDIATE_RECLAIM 607 sys_vmeminfo_counters: VMEMINFO_NR_WRITEBACK_TEMP 608 sys_vmeminfo_counters: VMEMINFO_NR_ISOLATED_ANON 609 sys_vmeminfo_counters: VMEMINFO_NR_ISOLATED_FILE 610 sys_vmeminfo_counters: VMEMINFO_NR_SHMEM 611 sys_vmeminfo_counters: VMEMINFO_NR_DIRTIED 612 sys_vmeminfo_counters: VMEMINFO_NR_WRITTEN 613 sys_vmeminfo_counters: VMEMINFO_NR_PAGES_SCANNED 614 sys_vmeminfo_counters: VMEMINFO_WORKINGSET_REFAULT 615 sys_vmeminfo_counters: VMEMINFO_WORKINGSET_ACTIVATE 616 sys_vmeminfo_counters: VMEMINFO_WORKINGSET_NODERECLAIM 617 sys_vmeminfo_counters: VMEMINFO_NR_ANON_TRANSPARENT_HUGEPAGES 618 sys_vmeminfo_counters: VMEMINFO_NR_FREE_CMA 619 sys_vmeminfo_counters: VMEMINFO_NR_SWAPCACHE 620 sys_vmeminfo_counters: VMEMINFO_NR_DIRTY_THRESHOLD 621 sys_vmeminfo_counters: VMEMINFO_NR_DIRTY_BACKGROUND_THRESHOLD 622 sys_vmeminfo_counters: VMEMINFO_PGPGIN 623 sys_vmeminfo_counters: VMEMINFO_PGPGOUT 624 sys_vmeminfo_counters: VMEMINFO_PGPGOUTCLEAN 625 sys_vmeminfo_counters: VMEMINFO_PSWPIN 626 sys_vmeminfo_counters: VMEMINFO_PSWPOUT 627 sys_vmeminfo_counters: VMEMINFO_PGALLOC_DMA 628 sys_vmeminfo_counters: VMEMINFO_PGALLOC_NORMAL 629 sys_vmeminfo_counters: VMEMINFO_PGALLOC_MOVABLE 630 sys_vmeminfo_counters: VMEMINFO_PGFREE 631 sys_vmeminfo_counters: VMEMINFO_PGACTIVATE 632 sys_vmeminfo_counters: VMEMINFO_PGDEACTIVATE 633 sys_vmeminfo_counters: VMEMINFO_PGFAULT 634 sys_vmeminfo_counters: VMEMINFO_PGMAJFAULT 635 sys_vmeminfo_counters: VMEMINFO_PGREFILL_DMA 636 sys_vmeminfo_counters: VMEMINFO_PGREFILL_NORMAL 637 sys_vmeminfo_counters: VMEMINFO_PGREFILL_MOVABLE 638 sys_vmeminfo_counters: VMEMINFO_PGSTEAL_KSWAPD_DMA 639 sys_vmeminfo_counters: VMEMINFO_PGSTEAL_KSWAPD_NORMAL 640 sys_vmeminfo_counters: VMEMINFO_PGSTEAL_KSWAPD_MOVABLE 641 sys_vmeminfo_counters: VMEMINFO_PGSTEAL_DIRECT_DMA 642 sys_vmeminfo_counters: VMEMINFO_PGSTEAL_DIRECT_NORMAL 643 sys_vmeminfo_counters: VMEMINFO_PGSTEAL_DIRECT_MOVABLE 644 sys_vmeminfo_counters: VMEMINFO_PGSCAN_KSWAPD_DMA 645 sys_vmeminfo_counters: VMEMINFO_PGSCAN_KSWAPD_NORMAL 646 sys_vmeminfo_counters: VMEMINFO_PGSCAN_KSWAPD_MOVABLE 647 sys_vmeminfo_counters: VMEMINFO_PGSCAN_DIRECT_DMA 648 sys_vmeminfo_counters: VMEMINFO_PGSCAN_DIRECT_NORMAL 649 sys_vmeminfo_counters: VMEMINFO_PGSCAN_DIRECT_MOVABLE 650 sys_vmeminfo_counters: VMEMINFO_PGSCAN_DIRECT_THROTTLE 651 sys_vmeminfo_counters: VMEMINFO_PGINODESTEAL 652 sys_vmeminfo_counters: VMEMINFO_SLABS_SCANNED 653 sys_vmeminfo_counters: VMEMINFO_KSWAPD_INODESTEAL 654 sys_vmeminfo_counters: VMEMINFO_KSWAPD_LOW_WMARK_HIT_QUICKLY 655 sys_vmeminfo_counters: VMEMINFO_KSWAPD_HIGH_WMARK_HIT_QUICKLY 656 sys_vmeminfo_counters: VMEMINFO_PAGEOUTRUN 657 sys_vmeminfo_counters: VMEMINFO_ALLOCSTALL 658 sys_vmeminfo_counters: VMEMINFO_PGROTATED 659 sys_vmeminfo_counters: VMEMINFO_DROP_PAGECACHE 660 sys_vmeminfo_counters: VMEMINFO_DROP_SLAB 661 sys_vmeminfo_counters: VMEMINFO_PGMIGRATE_SUCCESS 662 sys_vmeminfo_counters: VMEMINFO_PGMIGRATE_FAIL 663 sys_vmeminfo_counters: VMEMINFO_COMPACT_MIGRATE_SCANNED 664 sys_vmeminfo_counters: VMEMINFO_COMPACT_FREE_SCANNED 665 sys_vmeminfo_counters: VMEMINFO_COMPACT_ISOLATED 666 sys_vmeminfo_counters: VMEMINFO_COMPACT_STALL 667 sys_vmeminfo_counters: VMEMINFO_COMPACT_FAIL 668 sys_vmeminfo_counters: VMEMINFO_COMPACT_SUCCESS 669 sys_vmeminfo_counters: VMEMINFO_COMPACT_DAEMON_WAKE 670 sys_vmeminfo_counters: VMEMINFO_UNEVICTABLE_PGS_CULLED 671 sys_vmeminfo_counters: VMEMINFO_UNEVICTABLE_PGS_SCANNED 672 sys_vmeminfo_counters: VMEMINFO_UNEVICTABLE_PGS_RESCUED 673 sys_vmeminfo_counters: VMEMINFO_UNEVICTABLE_PGS_MLOCKED 674 sys_vmeminfo_counters: VMEMINFO_UNEVICTABLE_PGS_MUNLOCKED 675 sys_vmeminfo_counters: VMEMINFO_UNEVICTABLE_PGS_CLEARED 676 sys_vmeminfo_counters: VMEMINFO_UNEVICTABLE_PGS_STRANDED 677 sys_vmeminfo_counters: VMEMINFO_NR_ZSPAGES 678 sys_vmeminfo_counters: VMEMINFO_NR_ION_HEAP 679 sys_vmeminfo_counters: VMEMINFO_NR_GPU_HEAP 680 sys_vmeminfo_counters: VMEMINFO_ALLOCSTALL_DMA 681 sys_vmeminfo_counters: VMEMINFO_ALLOCSTALL_MOVABLE 682 sys_vmeminfo_counters: VMEMINFO_ALLOCSTALL_NORMAL 683 sys_vmeminfo_counters: VMEMINFO_COMPACT_DAEMON_FREE_SCANNED 684 sys_vmeminfo_counters: VMEMINFO_COMPACT_DAEMON_MIGRATE_SCANNED 685 sys_vmeminfo_counters: VMEMINFO_NR_FASTRPC 686 sys_vmeminfo_counters: VMEMINFO_NR_INDIRECTLY_RECLAIMABLE 687 sys_vmeminfo_counters: VMEMINFO_NR_ION_HEAP_POOL 688 sys_vmeminfo_counters: VMEMINFO_NR_KERNEL_MISC_RECLAIMABLE 689 sys_vmeminfo_counters: VMEMINFO_NR_SHADOW_CALL_STACK_BYTES 690 sys_vmeminfo_counters: VMEMINFO_NR_SHMEM_HUGEPAGES 691 sys_vmeminfo_counters: VMEMINFO_NR_SHMEM_PMDMAPPED 692 sys_vmeminfo_counters: VMEMINFO_NR_UNRECLAIMABLE_PAGES 693 sys_vmeminfo_counters: VMEMINFO_NR_ZONE_ACTIVE_ANON 694 sys_vmeminfo_counters: VMEMINFO_NR_ZONE_ACTIVE_FILE 695 sys_vmeminfo_counters: VMEMINFO_NR_ZONE_INACTIVE_ANON 696 sys_vmeminfo_counters: VMEMINFO_NR_ZONE_INACTIVE_FILE 697 sys_vmeminfo_counters: VMEMINFO_NR_ZONE_UNEVICTABLE 698 sys_vmeminfo_counters: VMEMINFO_NR_ZONE_WRITE_PENDING 699 sys_vmeminfo_counters: VMEMINFO_OOM_KILL 700 sys_vmeminfo_counters: VMEMINFO_PGLAZYFREE 701 sys_vmeminfo_counters: VMEMINFO_PGLAZYFREED 702 sys_vmeminfo_counters: VMEMINFO_PGREFILL 703 sys_vmeminfo_counters: VMEMINFO_PGSCAN_DIRECT 704 sys_vmeminfo_counters: VMEMINFO_PGSCAN_KSWAPD 705 sys_vmeminfo_counters: VMEMINFO_PGSKIP_DMA 706 sys_vmeminfo_counters: VMEMINFO_PGSKIP_MOVABLE 707 sys_vmeminfo_counters: VMEMINFO_PGSKIP_NORMAL 708 sys_vmeminfo_counters: VMEMINFO_PGSTEAL_DIRECT 709 sys_vmeminfo_counters: VMEMINFO_PGSTEAL_KSWAPD 710 sys_vmeminfo_counters: VMEMINFO_SWAP_RA 711 sys_vmeminfo_counters: VMEMINFO_SWAP_RA_HIT 712 sys_vmeminfo_counters: VMEMINFO_WORKINGSET_RESTORE 713 report_process_mem_info: true 714 report_app_mem_info: false 715 report_app_mem_by_memory_service: false 716 } 717 } 718CONFIG 719``` 720 721##### 进程内存使用跟踪<a name="section35362541224"></a> 722 723如配置抓取的进程名是com.ohos.mms 724 725``` sh 726hiprofiler_cmd \ 727 -c - \ 728 -o /data/local/tmp/hiprofiler_data.htrace \ 729 -t 50 \ 730 -s \ 731 -k \ 732<<CONFIG 733 request_id: 1 734 session_config { 735 buffers { 736 pages: 16384 737 } 738 result_file: "/data/local/tmp/hiprofiler_data.htrace" 739 sample_duration: 50000 740 } 741 plugin_configs { 742 plugin_name: "nativehook" 743 sample_interval: 5000 744 config_data { 745 save_file: false 746 filter_size: 4096 747 smb_pages: 16384 748 max_stack_depth: 10 749 process_name: "com.ohos.mms" 750 malloc_free_matching_interval: 1000 751 malloc_free_matching_cnt: 1000 752 string_compressed: true 753 fp_unwind: true 754 } 755 } 756CONFIG 757``` 758 759配置参数说明: 760 761* pid/process_name:设置抓取的进程ID或者进程名 762* max_stack_depth:抓取的栈的深度 763* smb_pages:native_daemon和native_hook进程之间存储数据的共享内存大小(4KB的倍数) 764* filter_size:只抓取大于该size的malloc数据(free不受影响) 765 766#### bytrace/hitrace场景示例<a name="section35362541225"></a> 767 768运行如下命令: 769 770``` 771hiprofiler_cmd \ 772 -c - \ 773 -o /data/local/tmp/hiprofiler_data.htrace \ 774 -t 30 \ 775 -s \ 776<<CONFIG 777 request_id: 1 778 session_config { 779 buffers { 780 pages: 1000 781 } 782 result_file: "/data/local/tmp/hiprofiler_data.htrace" 783 sample_duration: 30000 784 } 785 plugin_configs { 786 plugin_name: "ftrace-plugin" 787 sample_interval: 1000 788 config_data { 789 hitrace_time: 10 790 hitrace_categories: "ability" 791 hitrace_categories: "ace" 792 hitrace_categories: "binder" 793 hitrace_categories: "dsoftbus" 794 hitrace_categories: "freq" 795 hitrace_categories: "graphic" 796 hitrace_categories: "idle" 797 hitrace_categories: "memory" 798 hitrace_categories: "dcamera" 799 hitrace_categories: "ohos" 800 hitrace_categories: "rpc" 801 hitrace_categories: "sched" 802 hitrace_categories: "sync" 803 hitrace_categories: "window" 804 buffer_size_kb: 51200 805 flush_interval_ms: 1000 806 flush_threshold_kb: 4096 807 parse_ksyms: true 808 clock: "mono" 809 trace_period_ms: 200 810 debug_on: false 811 } 812 } 813CONFIG 814``` 815 816#### hiperf场景示例<a name="section35362541226"></a> 817 818运行如下命令: 819 820``` sh 821hiprofiler_cmd \ 822 -c - \ 823 -o /data/local/tmp/hiprofiler_data.htrace \ 824 -t 50 \ 825 -s \ 826 -k \ 827<<CONFIG 828 request_id: 1 829 session_config { 830 buffers { 831 pages: 16384 832 } 833 result_file: "/data/local/tmp/hiprofiler_data.htrace" 834 sample_duration: 50000 835 } 836 plugin_configs { 837 plugin_name: "hiperf-plugin" 838 sample_interval: 5000 839 config_data { 840 is_root: false 841 outfile_name: "/data/local/tmp/perf.data" 842 record_args: "-f 1000 -a --cpu-limit 100 -e hw-cpu-cycles,sched:sched_waking --call-stack dwarf --clockid monotonic --offcpu -m 256" 843 } 844 } 845CONFIG 846``` 847 848 849 850## 相关仓<a name="section1293495681320"></a> 851 852[研发工具链子系统]() 853 854**developtools\_profiler** 855 856[developtools\_hdc](https://gitee.com/openharmony/developtools_hdc) 857 858[developtools\_bytrace](https://gitee.com/openharmony/developtools_bytrace) 859